1 // Copyright 2014 The PDFium Authors
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 <stdint.h>
8
9 #include <memory>
10 #include <vector>
11
12 #include "core/fxcrt/data_vector.h"
13 #include "core/fxcrt/fx_system.h"
14 #include "core/fxcrt/span.h"
15 #include "core/fxge/agg/cfx_agg_cliprgn.h"
16 #include "core/fxge/agg/cfx_agg_devicedriver.h"
17 #include "core/fxge/apple/fx_apple_platform.h"
18 #include "core/fxge/cfx_font.h"
19 #include "core/fxge/cfx_gemodule.h"
20 #include "core/fxge/cfx_glyphbitmap.h"
21 #include "core/fxge/cfx_glyphcache.h"
22 #include "core/fxge/cfx_renderdevice.h"
23 #include "core/fxge/cfx_substfont.h"
24 #include "core/fxge/dib/cfx_dibitmap.h"
25 #include "core/fxge/text_char_pos.h"
26
27 namespace {
28
DoNothing(void * info,const void * data,size_t size)29 void DoNothing(void* info, const void* data, size_t size) {}
30
CGDrawGlyphRun(CGContextRef pContext,pdfium::span<const TextCharPos> pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t argb)31 bool CGDrawGlyphRun(CGContextRef pContext,
32 pdfium::span<const TextCharPos> pCharPos,
33 CFX_Font* pFont,
34 const CFX_Matrix& mtObject2Device,
35 float font_size,
36 uint32_t argb) {
37 if (pCharPos.empty())
38 return true;
39
40 bool bNegSize = font_size < 0;
41 if (bNegSize)
42 font_size = -font_size;
43
44 CFX_Matrix new_matrix = mtObject2Device;
45 CQuartz2D& quartz2d =
46 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
47 ->m_quartz2d;
48 if (!pFont->GetPlatformFont()) {
49 if (pFont->GetPsName() == "DFHeiStd-W5")
50 return false;
51
52 pFont->SetPlatformFont(quartz2d.CreateFont(pFont->GetFontSpan()));
53 if (!pFont->GetPlatformFont())
54 return false;
55 }
56 DataVector<uint16_t> glyph_indices(pCharPos.size());
57 std::vector<CGPoint> glyph_positions(pCharPos.size());
58 for (size_t i = 0; i < pCharPos.size(); i++) {
59 glyph_indices[i] =
60 pCharPos[i].m_ExtGID ? pCharPos[i].m_ExtGID : pCharPos[i].m_GlyphIndex;
61 if (bNegSize)
62 glyph_positions[i].x = -pCharPos[i].m_Origin.x;
63 else
64 glyph_positions[i].x = pCharPos[i].m_Origin.x;
65 glyph_positions[i].y = pCharPos[i].m_Origin.y;
66 }
67 if (bNegSize) {
68 new_matrix.a = -new_matrix.a;
69 new_matrix.c = -new_matrix.c;
70 } else {
71 new_matrix.b = -new_matrix.b;
72 new_matrix.d = -new_matrix.d;
73 }
74 quartz2d.SetGraphicsTextMatrix(pContext, new_matrix);
75 return quartz2d.DrawGraphicsString(pContext, pFont->GetPlatformFont(),
76 font_size, glyph_indices, glyph_positions,
77 argb);
78 }
79
80 } // namespace
81
82 namespace pdfium {
83
InitPlatform()84 void CFX_AggDeviceDriver::InitPlatform() {
85 CQuartz2D& quartz2d =
86 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
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()->GetPlatform())
94 ->m_quartz2d;
95 if (m_pPlatformGraphics) {
96 quartz2d.DestroyGraphics(m_pPlatformGraphics);
97 m_pPlatformGraphics = nullptr;
98 }
99 }
100
DrawDeviceText(pdfium::span<const TextCharPos> pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t color,const CFX_TextRenderOptions &)101 bool CFX_AggDeviceDriver::DrawDeviceText(
102 pdfium::span<const TextCharPos> pCharPos,
103 CFX_Font* pFont,
104 const CFX_Matrix& mtObject2Device,
105 float font_size,
106 uint32_t color,
107 const CFX_TextRenderOptions& /*options*/) {
108 if (!pFont)
109 return false;
110
111 bool bBold = pFont->IsBold();
112 if (!bBold && pFont->GetSubstFont() &&
113 pFont->GetSubstFont()->m_Weight >= 500 &&
114 pFont->GetSubstFont()->m_Weight <= 600) {
115 return false;
116 }
117 for (const auto& cp : pCharPos) {
118 if (cp.m_bGlyphAdjust)
119 return false;
120 }
121 CGContextRef ctx = CGContextRef(m_pPlatformGraphics);
122 if (!ctx)
123 return false;
124
125 CGContextSaveGState(ctx);
126 CGContextSetTextDrawingMode(ctx, kCGTextFillClip);
127 CGRect rect_cg;
128 CGImageRef pImageCG = nullptr;
129 if (m_pClipRgn) {
130 rect_cg =
131 CGRectMake(m_pClipRgn->GetBox().left, m_pClipRgn->GetBox().top,
132 m_pClipRgn->GetBox().Width(), m_pClipRgn->GetBox().Height());
133 RetainPtr<CFX_DIBitmap> pClipMask = m_pClipRgn->GetMask();
134 if (pClipMask) {
135 CGDataProviderRef pClipMaskDataProvider = CGDataProviderCreateWithData(
136 nullptr, pClipMask->GetBuffer().data(),
137 pClipMask->GetPitch() * pClipMask->GetHeight(), DoNothing);
138 CGFloat decode_f[2] = {255.f, 0.f};
139 pImageCG = CGImageMaskCreate(
140 pClipMask->GetWidth(), pClipMask->GetHeight(), 8, 8,
141 pClipMask->GetPitch(), pClipMaskDataProvider, decode_f, false);
142 CGDataProviderRelease(pClipMaskDataProvider);
143 }
144 } else {
145 rect_cg = CGRectMake(0, 0, m_pBitmap->GetWidth(), m_pBitmap->GetHeight());
146 }
147 rect_cg = CGContextConvertRectToDeviceSpace(ctx, rect_cg);
148 if (pImageCG)
149 CGContextClipToMask(ctx, rect_cg, pImageCG);
150 else
151 CGContextClipToRect(ctx, rect_cg);
152
153 if (m_bRgbByteOrder) {
154 uint8_t a = FXARGB_A(color);
155 uint8_t r = FXARGB_R(color);
156 uint8_t g = FXARGB_G(color);
157 uint8_t b = FXARGB_B(color);
158 color = ArgbEncode(a, b, g, r);
159 }
160 bool ret =
161 CGDrawGlyphRun(ctx, pCharPos, pFont, mtObject2Device, font_size, color);
162 if (pImageCG)
163 CGImageRelease(pImageCG);
164 CGContextRestoreGState(ctx);
165 return ret;
166 }
167
168 } // namespace pdfium
169
RenderGlyph_Nativetext(const CFX_Font * pFont,uint32_t glyph_index,const CFX_Matrix & matrix,int dest_width,int anti_alias)170 std::unique_ptr<CFX_GlyphBitmap> CFX_GlyphCache::RenderGlyph_Nativetext(
171 const CFX_Font* pFont,
172 uint32_t glyph_index,
173 const CFX_Matrix& matrix,
174 int dest_width,
175 int anti_alias) {
176 return nullptr;
177 }
178
ReleasePlatformResource()179 void CFX_Font::ReleasePlatformResource() {
180 if (m_pPlatformFont) {
181 CQuartz2D& quartz2d =
182 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
183 ->m_quartz2d;
184 quartz2d.DestroyFont(m_pPlatformFont);
185 m_pPlatformFont = nullptr;
186 }
187 }
188