1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include <memory>
8
9 #include "core/fxcrt/cfx_fixedbufgrow.h"
10 #include "core/fxcrt/fx_system.h"
11
12 #ifndef _SKIA_SUPPORT_
13 #include "core/fxge/agg/fx_agg_driver.h"
14 #endif
15
16 #include "core/fxge/apple/apple_int.h"
17 #include "core/fxge/cfx_cliprgn.h"
18 #include "core/fxge/cfx_facecache.h"
19 #include "core/fxge/cfx_font.h"
20 #include "core/fxge/cfx_gemodule.h"
21 #include "core/fxge/cfx_renderdevice.h"
22 #include "core/fxge/fx_freetype.h"
23
24 #ifndef _SKIA_SUPPORT_
25
26 namespace {
27
DoNothing(void * info,const void * data,size_t size)28 void DoNothing(void* info, const void* data, size_t size) {}
29
CGDrawGlyphRun(CGContextRef pContext,int nChars,const FXTEXT_CHARPOS * pCharPos,CFX_Font * pFont,const CFX_Matrix * pObject2Device,float font_size,uint32_t argb)30 bool CGDrawGlyphRun(CGContextRef pContext,
31 int nChars,
32 const FXTEXT_CHARPOS* pCharPos,
33 CFX_Font* pFont,
34 const CFX_Matrix* pObject2Device,
35 float font_size,
36 uint32_t argb) {
37 if (nChars == 0)
38 return true;
39
40 bool bNegSize = font_size < 0;
41 if (bNegSize)
42 font_size = -font_size;
43
44 CFX_Matrix new_matrix;
45 if (pObject2Device)
46 new_matrix.Concat(*pObject2Device);
47
48 CQuartz2D& quartz2d =
49 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatformData())
50 ->m_quartz2d;
51 if (!pFont->GetPlatformFont()) {
52 if (pFont->GetPsName() == "DFHeiStd-W5")
53 return false;
54
55 pFont->SetPlatformFont(
56 quartz2d.CreateFont(pFont->GetFontData(), pFont->GetSize()));
57 if (!pFont->GetPlatformFont())
58 return false;
59 }
60 CFX_FixedBufGrow<uint16_t, 32> glyph_indices(nChars);
61 CFX_FixedBufGrow<CGPoint, 32> glyph_positions(nChars);
62 for (int i = 0; i < nChars; i++) {
63 glyph_indices[i] =
64 pCharPos[i].m_ExtGID ? pCharPos[i].m_ExtGID : pCharPos[i].m_GlyphIndex;
65 if (bNegSize)
66 glyph_positions[i].x = -pCharPos[i].m_Origin.x;
67 else
68 glyph_positions[i].x = pCharPos[i].m_Origin.x;
69 glyph_positions[i].y = pCharPos[i].m_Origin.y;
70 }
71 if (bNegSize) {
72 new_matrix.a = -new_matrix.a;
73 new_matrix.c = -new_matrix.c;
74 } else {
75 new_matrix.b = -new_matrix.b;
76 new_matrix.d = -new_matrix.d;
77 }
78 quartz2d.setGraphicsTextMatrix(pContext, &new_matrix);
79 return quartz2d.drawGraphicsString(pContext, pFont->GetPlatformFont(),
80 font_size, glyph_indices, glyph_positions,
81 nChars, argb, nullptr);
82 }
83
84 } // namespace
85
InitPlatform()86 void CFX_AggDeviceDriver::InitPlatform() {
87 CQuartz2D& quartz2d =
88 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatformData())
89 ->m_quartz2d;
90 m_pPlatformGraphics = quartz2d.createGraphics(m_pBitmap);
91 }
92
DestroyPlatform()93 void CFX_AggDeviceDriver::DestroyPlatform() {
94 CQuartz2D& quartz2d =
95 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatformData())
96 ->m_quartz2d;
97 if (m_pPlatformGraphics) {
98 quartz2d.destroyGraphics(m_pPlatformGraphics);
99 m_pPlatformGraphics = nullptr;
100 }
101 }
102
DrawDeviceText(int nChars,const FXTEXT_CHARPOS * pCharPos,CFX_Font * pFont,const CFX_Matrix * pObject2Device,float font_size,uint32_t argb)103 bool CFX_AggDeviceDriver::DrawDeviceText(int nChars,
104 const FXTEXT_CHARPOS* pCharPos,
105 CFX_Font* pFont,
106 const CFX_Matrix* pObject2Device,
107 float font_size,
108 uint32_t argb) {
109 if (!pFont)
110 return false;
111
112 bool bBold = pFont->IsBold();
113 if (!bBold && pFont->GetSubstFont() &&
114 pFont->GetSubstFont()->m_Weight >= 500 &&
115 pFont->GetSubstFont()->m_Weight <= 600) {
116 return false;
117 }
118 for (int i = 0; i < nChars; i++) {
119 if (pCharPos[i].m_bGlyphAdjust)
120 return false;
121 }
122 CGContextRef ctx = CGContextRef(m_pPlatformGraphics);
123 if (!ctx)
124 return false;
125
126 CGContextSaveGState(ctx);
127 CGContextSetTextDrawingMode(ctx, kCGTextFillClip);
128 CGRect rect_cg;
129 CGImageRef pImageCG = nullptr;
130 if (m_pClipRgn) {
131 rect_cg =
132 CGRectMake(m_pClipRgn->GetBox().left, m_pClipRgn->GetBox().top,
133 m_pClipRgn->GetBox().Width(), m_pClipRgn->GetBox().Height());
134 RetainPtr<CFX_DIBitmap> pClipMask = m_pClipRgn->GetMask();
135 if (pClipMask) {
136 CGDataProviderRef pClipMaskDataProvider = CGDataProviderCreateWithData(
137 nullptr, pClipMask->GetBuffer(),
138 pClipMask->GetPitch() * pClipMask->GetHeight(), DoNothing);
139 CGFloat decode_f[2] = {255.f, 0.f};
140 pImageCG = CGImageMaskCreate(
141 pClipMask->GetWidth(), pClipMask->GetHeight(), 8, 8,
142 pClipMask->GetPitch(), pClipMaskDataProvider, decode_f, false);
143 CGDataProviderRelease(pClipMaskDataProvider);
144 }
145 } else {
146 rect_cg = CGRectMake(0, 0, m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
147 }
148 rect_cg = CGContextConvertRectToDeviceSpace(ctx, rect_cg);
149 if (pImageCG)
150 CGContextClipToMask(ctx, rect_cg, pImageCG);
151 else
152 CGContextClipToRect(ctx, rect_cg);
153
154 bool ret = CGDrawGlyphRun(ctx, nChars, pCharPos, pFont, pObject2Device,
155 font_size, argb);
156 if (pImageCG)
157 CGImageRelease(pImageCG);
158 CGContextRestoreGState(ctx);
159 return ret;
160 }
161
162 #endif // _SKIA_SUPPORT_
163
InitPlatform()164 void CFX_FaceCache::InitPlatform() {}
165
DestroyPlatform()166 void CFX_FaceCache::DestroyPlatform() {}
167
RenderGlyph_Nativetext(const CFX_Font * pFont,uint32_t glyph_index,const CFX_Matrix * pMatrix,int dest_width,int anti_alias)168 std::unique_ptr<CFX_GlyphBitmap> CFX_FaceCache::RenderGlyph_Nativetext(
169 const CFX_Font* pFont,
170 uint32_t glyph_index,
171 const CFX_Matrix* pMatrix,
172 int dest_width,
173 int anti_alias) {
174 return nullptr;
175 }
176
ReleasePlatformResource()177 void CFX_Font::ReleasePlatformResource() {
178 if (m_pPlatformFont) {
179 CQuartz2D& quartz2d =
180 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatformData())
181 ->m_quartz2d;
182 quartz2d.DestroyFont(m_pPlatformFont);
183 m_pPlatformFont = nullptr;
184 }
185 }
186