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/fxge/agg/fx_agg_driver.h"
15 #include "core/fxge/apple/fx_apple_platform.h"
16 #include "core/fxge/cfx_cliprgn.h"
17 #include "core/fxge/cfx_font.h"
18 #include "core/fxge/cfx_gemodule.h"
19 #include "core/fxge/cfx_glyphbitmap.h"
20 #include "core/fxge/cfx_glyphcache.h"
21 #include "core/fxge/cfx_renderdevice.h"
22 #include "core/fxge/cfx_substfont.h"
23 #include "core/fxge/dib/cfx_dibitmap.h"
24 #include "core/fxge/freetype/fx_freetype.h"
25 #include "core/fxge/text_char_pos.h"
26 #include "third_party/base/containers/span.h"
27
28 namespace {
29
DoNothing(void * info,const void * data,size_t size)30 void DoNothing(void* info, const void* data, size_t size) {}
31
CGDrawGlyphRun(CGContextRef pContext,pdfium::span<const TextCharPos> pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t argb)32 bool CGDrawGlyphRun(CGContextRef pContext,
33 pdfium::span<const TextCharPos> pCharPos,
34 CFX_Font* pFont,
35 const CFX_Matrix& mtObject2Device,
36 float font_size,
37 uint32_t argb) {
38 if (pCharPos.empty())
39 return true;
40
41 bool bNegSize = font_size < 0;
42 if (bNegSize)
43 font_size = -font_size;
44
45 CFX_Matrix new_matrix = mtObject2Device;
46 CQuartz2D& quartz2d =
47 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
48 ->m_quartz2d;
49 if (!pFont->GetPlatformFont()) {
50 if (pFont->GetPsName() == "DFHeiStd-W5")
51 return false;
52
53 pFont->SetPlatformFont(quartz2d.CreateFont(pFont->GetFontSpan()));
54 if (!pFont->GetPlatformFont())
55 return false;
56 }
57 DataVector<uint16_t> glyph_indices(pCharPos.size());
58 std::vector<CGPoint> glyph_positions(pCharPos.size());
59 for (size_t i = 0; i < pCharPos.size(); i++) {
60 glyph_indices[i] =
61 pCharPos[i].m_ExtGID ? pCharPos[i].m_ExtGID : pCharPos[i].m_GlyphIndex;
62 if (bNegSize)
63 glyph_positions[i].x = -pCharPos[i].m_Origin.x;
64 else
65 glyph_positions[i].x = pCharPos[i].m_Origin.x;
66 glyph_positions[i].y = pCharPos[i].m_Origin.y;
67 }
68 if (bNegSize) {
69 new_matrix.a = -new_matrix.a;
70 new_matrix.c = -new_matrix.c;
71 } else {
72 new_matrix.b = -new_matrix.b;
73 new_matrix.d = -new_matrix.d;
74 }
75 quartz2d.SetGraphicsTextMatrix(pContext, new_matrix);
76 return quartz2d.DrawGraphicsString(pContext, pFont->GetPlatformFont(),
77 font_size, glyph_indices, glyph_positions,
78 argb);
79 }
80
81 } // namespace
82
83 namespace pdfium {
84
InitPlatform()85 void CFX_AggDeviceDriver::InitPlatform() {
86 CQuartz2D& quartz2d =
87 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
88 ->m_quartz2d;
89 m_pPlatformGraphics = quartz2d.CreateGraphics(m_pBitmap);
90 }
91
DestroyPlatform()92 void CFX_AggDeviceDriver::DestroyPlatform() {
93 CQuartz2D& quartz2d =
94 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
95 ->m_quartz2d;
96 if (m_pPlatformGraphics) {
97 quartz2d.DestroyGraphics(m_pPlatformGraphics);
98 m_pPlatformGraphics = nullptr;
99 }
100 }
101
DrawDeviceText(pdfium::span<const TextCharPos> pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t argb,const CFX_TextRenderOptions &)102 bool CFX_AggDeviceDriver::DrawDeviceText(
103 pdfium::span<const TextCharPos> pCharPos,
104 CFX_Font* pFont,
105 const CFX_Matrix& mtObject2Device,
106 float font_size,
107 uint32_t argb,
108 const CFX_TextRenderOptions& /*options*/) {
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 (const auto& cp : pCharPos) {
119 if (cp.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().data(),
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 =
155 CGDrawGlyphRun(ctx, pCharPos, pFont, mtObject2Device, font_size, argb);
156 if (pImageCG)
157 CGImageRelease(pImageCG);
158 CGContextRestoreGState(ctx);
159 return ret;
160 }
161
162 } // namespace pdfium
163
RenderGlyph_Nativetext(const CFX_Font * pFont,uint32_t glyph_index,const CFX_Matrix & matrix,int dest_width,int anti_alias)164 std::unique_ptr<CFX_GlyphBitmap> CFX_GlyphCache::RenderGlyph_Nativetext(
165 const CFX_Font* pFont,
166 uint32_t glyph_index,
167 const CFX_Matrix& matrix,
168 int dest_width,
169 int anti_alias) {
170 return nullptr;
171 }
172
ReleasePlatformResource()173 void CFX_Font::ReleasePlatformResource() {
174 if (m_pPlatformFont) {
175 CQuartz2D& quartz2d =
176 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatform())
177 ->m_quartz2d;
178 quartz2d.DestroyFont(m_pPlatformFont);
179 m_pPlatformFont = nullptr;
180 }
181 }
182