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