1 // Copyright 2016 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/fxge/cfx_renderdevice.h"
8
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12
13 #include "build/build_config.h"
14 #include "core/fxcrt/fx_safe_types.h"
15 #include "core/fxge/cfx_color.h"
16 #include "core/fxge/cfx_defaultrenderdevice.h"
17 #include "core/fxge/cfx_font.h"
18 #include "core/fxge/cfx_fontmgr.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_graphstatedata.h"
23 #include "core/fxge/cfx_pathdata.h"
24 #include "core/fxge/dib/cfx_dibitmap.h"
25 #include "core/fxge/dib/cfx_imagerenderer.h"
26 #include "core/fxge/fx_font.h"
27 #include "core/fxge/renderdevicedriver_iface.h"
28 #include "core/fxge/text_char_pos.h"
29 #include "core/fxge/text_glyph_pos.h"
30
31 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
32 #include "third_party/skia/include/core/SkTypes.h"
33 #endif
34
35 namespace {
36
AdjustGlyphSpace(std::vector<TextGlyphPos> * pGlyphAndPos)37 void AdjustGlyphSpace(std::vector<TextGlyphPos>* pGlyphAndPos) {
38 ASSERT(pGlyphAndPos->size() > 1);
39 std::vector<TextGlyphPos>& glyphs = *pGlyphAndPos;
40 bool bVertical = glyphs.back().m_Origin.x == glyphs.front().m_Origin.x;
41 if (!bVertical && (glyphs.back().m_Origin.y != glyphs.front().m_Origin.y))
42 return;
43
44 for (size_t i = glyphs.size() - 1; i > 1; --i) {
45 const TextGlyphPos& next = glyphs[i];
46 int next_origin = bVertical ? next.m_Origin.y : next.m_Origin.x;
47 float next_origin_f = bVertical ? next.m_fOrigin.y : next.m_fOrigin.x;
48
49 TextGlyphPos& current = glyphs[i - 1];
50 int& current_origin = bVertical ? current.m_Origin.y : current.m_Origin.x;
51 float current_origin_f =
52 bVertical ? current.m_fOrigin.y : current.m_fOrigin.x;
53
54 FX_SAFE_INT32 safe_space = next_origin;
55 safe_space -= current_origin;
56 if (!safe_space.IsValid())
57 continue;
58
59 int space = safe_space.ValueOrDie();
60 float space_f = next_origin_f - current_origin_f;
61 float error = fabs(space_f) - fabs(static_cast<float>(space));
62 if (error <= 0.5f)
63 continue;
64
65 FX_SAFE_INT32 safe_origin = current_origin;
66 safe_origin += space > 0 ? -1 : 1;
67 if (!safe_origin.IsValid())
68 continue;
69
70 current_origin = safe_origin.ValueOrDie();
71 }
72 }
73
74 const uint8_t g_TextGammaAdjust[256] = {
75 0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 18,
76 19, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35,
77 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52,
78 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
79 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
80 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
81 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
82 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
83 129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
84 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 156,
85 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
86 172, 173, 174, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
87 186, 187, 188, 189, 190, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
88 200, 201, 202, 203, 204, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
89 214, 215, 216, 217, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
90 228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 239, 240,
91 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 250, 251, 252, 253, 254,
92 255,
93 };
94
TextGammaAdjust(int value)95 int TextGammaAdjust(int value) {
96 ASSERT(value >= 0);
97 ASSERT(value <= 255);
98 return g_TextGammaAdjust[value];
99 }
100
CalcAlpha(int src,int alpha)101 int CalcAlpha(int src, int alpha) {
102 return src * alpha / 255;
103 }
104
Merge(uint8_t src,int channel,int alpha,uint8_t * dest)105 void Merge(uint8_t src, int channel, int alpha, uint8_t* dest) {
106 *dest = FXDIB_ALPHA_MERGE(*dest, channel, CalcAlpha(src, alpha));
107 }
108
MergeGammaAdjust(uint8_t src,int channel,int alpha,uint8_t * dest)109 void MergeGammaAdjust(uint8_t src, int channel, int alpha, uint8_t* dest) {
110 *dest =
111 FXDIB_ALPHA_MERGE(*dest, channel, CalcAlpha(TextGammaAdjust(src), alpha));
112 }
113
MergeGammaAdjustBgr(const uint8_t * src,int r,int g,int b,int a,uint8_t * dest)114 void MergeGammaAdjustBgr(const uint8_t* src,
115 int r,
116 int g,
117 int b,
118 int a,
119 uint8_t* dest) {
120 MergeGammaAdjust(src[0], b, a, &dest[0]);
121 MergeGammaAdjust(src[1], g, a, &dest[1]);
122 MergeGammaAdjust(src[2], r, a, &dest[2]);
123 }
124
MergeGammaAdjustRgb(const uint8_t * src,int r,int g,int b,int a,uint8_t * dest)125 void MergeGammaAdjustRgb(const uint8_t* src,
126 int r,
127 int g,
128 int b,
129 int a,
130 uint8_t* dest) {
131 MergeGammaAdjust(src[2], b, a, &dest[0]);
132 MergeGammaAdjust(src[1], g, a, &dest[1]);
133 MergeGammaAdjust(src[0], r, a, &dest[2]);
134 }
135
AverageRgb(const uint8_t * src)136 int AverageRgb(const uint8_t* src) {
137 return (src[0] + src[1] + src[2]) / 3;
138 }
139
CalculateDestAlpha(uint8_t back_alpha,int src_alpha)140 uint8_t CalculateDestAlpha(uint8_t back_alpha, int src_alpha) {
141 return back_alpha + src_alpha - back_alpha * src_alpha / 255;
142 }
143
ApplyAlpha(uint8_t * dest,int b,int g,int r,int alpha)144 void ApplyAlpha(uint8_t* dest, int b, int g, int r, int alpha) {
145 dest[0] = FXDIB_ALPHA_MERGE(dest[0], b, alpha);
146 dest[1] = FXDIB_ALPHA_MERGE(dest[1], g, alpha);
147 dest[2] = FXDIB_ALPHA_MERGE(dest[2], r, alpha);
148 }
149
ApplyDestAlpha(uint8_t back_alpha,int src_alpha,int r,int g,int b,uint8_t * dest)150 void ApplyDestAlpha(uint8_t back_alpha,
151 int src_alpha,
152 int r,
153 int g,
154 int b,
155 uint8_t* dest) {
156 uint8_t dest_alpha = CalculateDestAlpha(back_alpha, src_alpha);
157 ApplyAlpha(dest, b, g, r, src_alpha * 255 / dest_alpha);
158 dest[3] = dest_alpha;
159 }
160
NormalizeArgb(int src_value,int r,int g,int b,int a,uint8_t * dest,int src_alpha)161 void NormalizeArgb(int src_value,
162 int r,
163 int g,
164 int b,
165 int a,
166 uint8_t* dest,
167 int src_alpha) {
168 uint8_t back_alpha = dest[3];
169 if (back_alpha == 0)
170 FXARGB_SETDIB(dest, ArgbEncode(src_alpha, r, g, b));
171 else if (src_alpha != 0)
172 ApplyDestAlpha(back_alpha, src_alpha, r, g, b, dest);
173 }
174
NormalizeDest(bool has_alpha,int src_value,int r,int g,int b,int a,uint8_t * dest)175 void NormalizeDest(bool has_alpha,
176 int src_value,
177 int r,
178 int g,
179 int b,
180 int a,
181 uint8_t* dest) {
182 if (has_alpha) {
183 NormalizeArgb(src_value, r, g, b, a, dest,
184 CalcAlpha(TextGammaAdjust(src_value), a));
185 return;
186 }
187 int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
188 if (src_alpha == 0)
189 return;
190
191 ApplyAlpha(dest, b, g, r, src_alpha);
192 }
193
NormalizeSrc(bool has_alpha,int src_value,int r,int g,int b,int a,uint8_t * dest)194 void NormalizeSrc(bool has_alpha,
195 int src_value,
196 int r,
197 int g,
198 int b,
199 int a,
200 uint8_t* dest) {
201 if (!has_alpha) {
202 ApplyAlpha(dest, b, g, r, CalcAlpha(TextGammaAdjust(src_value), a));
203 return;
204 }
205 int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
206 if (src_alpha != 0)
207 NormalizeArgb(src_value, r, g, b, a, dest, src_alpha);
208 }
209
NextPixel(uint8_t ** src_scan,uint8_t ** dst_scan,int bpp)210 void NextPixel(uint8_t** src_scan, uint8_t** dst_scan, int bpp) {
211 *src_scan += 3;
212 *dst_scan += bpp;
213 }
214
SetAlpha(bool has_alpha,uint8_t * alpha)215 void SetAlpha(bool has_alpha, uint8_t* alpha) {
216 if (has_alpha)
217 alpha[3] = 255;
218 }
219
DrawNormalTextHelper(const RetainPtr<CFX_DIBitmap> & bitmap,const RetainPtr<CFX_DIBitmap> & pGlyph,int nrows,int left,int top,int start_col,int end_col,bool bNormal,bool bBGRStripe,int x_subpixel,int a,int r,int g,int b)220 void DrawNormalTextHelper(const RetainPtr<CFX_DIBitmap>& bitmap,
221 const RetainPtr<CFX_DIBitmap>& pGlyph,
222 int nrows,
223 int left,
224 int top,
225 int start_col,
226 int end_col,
227 bool bNormal,
228 bool bBGRStripe,
229 int x_subpixel,
230 int a,
231 int r,
232 int g,
233 int b) {
234 const bool has_alpha = bitmap->GetFormat() == FXDIB_Argb;
235 uint8_t* src_buf = pGlyph->GetBuffer();
236 int src_pitch = pGlyph->GetPitch();
237 uint8_t* dest_buf = bitmap->GetBuffer();
238 int dest_pitch = bitmap->GetPitch();
239 const int Bpp = has_alpha ? 4 : bitmap->GetBPP() / 8;
240 for (int row = 0; row < nrows; ++row) {
241 int dest_row = row + top;
242 if (dest_row < 0 || dest_row >= bitmap->GetHeight())
243 continue;
244
245 uint8_t* src_scan = src_buf + row * src_pitch + (start_col - left) * 3;
246 uint8_t* dest_scan = dest_buf + dest_row * dest_pitch + start_col * Bpp;
247 if (bBGRStripe) {
248 if (x_subpixel == 0) {
249 for (int col = start_col; col < end_col; ++col) {
250 if (has_alpha) {
251 Merge(src_scan[2], r, a, &dest_scan[2]);
252 Merge(src_scan[1], g, a, &dest_scan[1]);
253 Merge(src_scan[0], b, a, &dest_scan[0]);
254 } else {
255 MergeGammaAdjustBgr(&src_scan[0], r, g, b, a, &dest_scan[0]);
256 }
257 SetAlpha(has_alpha, dest_scan);
258 NextPixel(&src_scan, &dest_scan, Bpp);
259 }
260 continue;
261 }
262 if (x_subpixel == 1) {
263 MergeGammaAdjust(src_scan[1], r, a, &dest_scan[2]);
264 MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
265 if (start_col > left)
266 MergeGammaAdjust(src_scan[-1], b, a, &dest_scan[0]);
267 SetAlpha(has_alpha, dest_scan);
268 NextPixel(&src_scan, &dest_scan, Bpp);
269 for (int col = start_col + 1; col < end_col - 1; ++col) {
270 MergeGammaAdjustBgr(&src_scan[-1], r, g, b, a, &dest_scan[0]);
271 SetAlpha(has_alpha, dest_scan);
272 NextPixel(&src_scan, &dest_scan, Bpp);
273 }
274 continue;
275 }
276 MergeGammaAdjust(src_scan[0], r, a, &dest_scan[2]);
277 if (start_col > left) {
278 MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
279 MergeGammaAdjust(src_scan[-2], b, a, &dest_scan[0]);
280 }
281 SetAlpha(has_alpha, dest_scan);
282 NextPixel(&src_scan, &dest_scan, Bpp);
283 for (int col = start_col + 1; col < end_col - 1; ++col) {
284 MergeGammaAdjustBgr(&src_scan[-2], r, g, b, a, &dest_scan[0]);
285 SetAlpha(has_alpha, dest_scan);
286 NextPixel(&src_scan, &dest_scan, Bpp);
287 }
288 continue;
289 }
290 if (x_subpixel == 0) {
291 for (int col = start_col; col < end_col; ++col) {
292 if (bNormal) {
293 int src_value = AverageRgb(&src_scan[0]);
294 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
295 } else {
296 MergeGammaAdjustRgb(&src_scan[0], r, g, b, a, &dest_scan[0]);
297 SetAlpha(has_alpha, dest_scan);
298 }
299 NextPixel(&src_scan, &dest_scan, Bpp);
300 }
301 continue;
302 }
303 if (x_subpixel == 1) {
304 if (bNormal) {
305 int src_value = start_col > left ? AverageRgb(&src_scan[-1])
306 : (src_scan[0] + src_scan[1]) / 3;
307 NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
308 } else {
309 if (start_col > left)
310 MergeGammaAdjust(src_scan[-1], r, a, &dest_scan[2]);
311 MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
312 MergeGammaAdjust(src_scan[1], b, a, &dest_scan[0]);
313 SetAlpha(has_alpha, dest_scan);
314 }
315 NextPixel(&src_scan, &dest_scan, Bpp);
316 for (int col = start_col + 1; col < end_col; ++col) {
317 if (bNormal) {
318 int src_value = AverageRgb(&src_scan[-1]);
319 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
320 } else {
321 MergeGammaAdjustRgb(&src_scan[-1], r, g, b, a, &dest_scan[0]);
322 SetAlpha(has_alpha, dest_scan);
323 }
324 NextPixel(&src_scan, &dest_scan, Bpp);
325 }
326 continue;
327 }
328 if (bNormal) {
329 int src_value =
330 start_col > left ? AverageRgb(&src_scan[-2]) : src_scan[0] / 3;
331 NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
332 } else {
333 if (start_col > left) {
334 MergeGammaAdjust(src_scan[-2], r, a, &dest_scan[2]);
335 MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
336 }
337 MergeGammaAdjust(src_scan[0], b, a, &dest_scan[0]);
338 SetAlpha(has_alpha, dest_scan);
339 }
340 NextPixel(&src_scan, &dest_scan, Bpp);
341 for (int col = start_col + 1; col < end_col; ++col) {
342 if (bNormal) {
343 int src_value = AverageRgb(&src_scan[-2]);
344 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
345 } else {
346 MergeGammaAdjustRgb(&src_scan[-2], r, g, b, a, &dest_scan[0]);
347 SetAlpha(has_alpha, dest_scan);
348 }
349 NextPixel(&src_scan, &dest_scan, Bpp);
350 }
351 }
352 }
353
ShouldDrawDeviceText(const CFX_Font * pFont,uint32_t text_flags)354 bool ShouldDrawDeviceText(const CFX_Font* pFont, uint32_t text_flags) {
355 #if defined(OS_MACOSX)
356 if (text_flags & FXFONT_CIDFONT)
357 return false;
358
359 const ByteString bsPsName = pFont->GetPsName();
360 if (bsPsName.Contains("+ZJHL"))
361 return false;
362
363 if (bsPsName == "CNAAJI+cmex10")
364 return false;
365 #endif
366 return true;
367 }
368
369 } // namespace
370
371 CFX_RenderDevice::CFX_RenderDevice() = default;
372
~CFX_RenderDevice()373 CFX_RenderDevice::~CFX_RenderDevice() {
374 RestoreState(false);
375 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
376 Flush(true);
377 #endif
378 }
379
380 // static
GetFlipMatrix(float width,float height,float left,float top)381 CFX_Matrix CFX_RenderDevice::GetFlipMatrix(float width,
382 float height,
383 float left,
384 float top) {
385 return CFX_Matrix(width, 0, 0, -height, left, top + height);
386 }
387
388 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
Flush(bool release)389 void CFX_RenderDevice::Flush(bool release) {
390 if (release)
391 m_pDeviceDriver.reset();
392 else
393 m_pDeviceDriver->Flush();
394 }
395 #endif
396
SetDeviceDriver(std::unique_ptr<RenderDeviceDriverIface> pDriver)397 void CFX_RenderDevice::SetDeviceDriver(
398 std::unique_ptr<RenderDeviceDriverIface> pDriver) {
399 m_pDeviceDriver = std::move(pDriver);
400 InitDeviceInfo();
401 }
402
InitDeviceInfo()403 void CFX_RenderDevice::InitDeviceInfo() {
404 m_Width = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_WIDTH);
405 m_Height = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_HEIGHT);
406 m_bpp = m_pDeviceDriver->GetDeviceCaps(FXDC_BITS_PIXEL);
407 m_RenderCaps = m_pDeviceDriver->GetDeviceCaps(FXDC_RENDER_CAPS);
408 m_DeviceType = m_pDeviceDriver->GetDeviceType();
409 if (!m_pDeviceDriver->GetClipBox(&m_ClipBox)) {
410 m_ClipBox.left = 0;
411 m_ClipBox.top = 0;
412 m_ClipBox.right = m_Width;
413 m_ClipBox.bottom = m_Height;
414 }
415 }
416
SaveState()417 void CFX_RenderDevice::SaveState() {
418 m_pDeviceDriver->SaveState();
419 }
420
RestoreState(bool bKeepSaved)421 void CFX_RenderDevice::RestoreState(bool bKeepSaved) {
422 if (m_pDeviceDriver) {
423 m_pDeviceDriver->RestoreState(bKeepSaved);
424 UpdateClipBox();
425 }
426 }
427
GetDeviceCaps(int caps_id) const428 int CFX_RenderDevice::GetDeviceCaps(int caps_id) const {
429 return m_pDeviceDriver->GetDeviceCaps(caps_id);
430 }
431
GetBitmap() const432 RetainPtr<CFX_DIBitmap> CFX_RenderDevice::GetBitmap() const {
433 return m_pBitmap;
434 }
435
SetBitmap(const RetainPtr<CFX_DIBitmap> & pBitmap)436 void CFX_RenderDevice::SetBitmap(const RetainPtr<CFX_DIBitmap>& pBitmap) {
437 m_pBitmap = pBitmap;
438 }
439
CreateCompatibleBitmap(const RetainPtr<CFX_DIBitmap> & pDIB,int width,int height) const440 bool CFX_RenderDevice::CreateCompatibleBitmap(
441 const RetainPtr<CFX_DIBitmap>& pDIB,
442 int width,
443 int height) const {
444 if (m_RenderCaps & FXRC_CMYK_OUTPUT) {
445 return pDIB->Create(
446 width, height,
447 m_RenderCaps & FXRC_ALPHA_OUTPUT ? FXDIB_Cmyka : FXDIB_Cmyk);
448 }
449 if (m_RenderCaps & FXRC_BYTEMASK_OUTPUT)
450 return pDIB->Create(width, height, FXDIB_8bppMask);
451 #if defined(OS_MACOSX) || defined _SKIA_SUPPORT_PATHS_
452 constexpr FXDIB_Format kPlatformFormat = FXDIB_Rgb32;
453 #else
454 constexpr FXDIB_Format kPlatformFormat = FXDIB_Rgb;
455 #endif
456 return pDIB->Create(
457 width, height,
458 m_RenderCaps & FXRC_ALPHA_OUTPUT ? FXDIB_Argb : kPlatformFormat);
459 }
460
SetBaseClip(const FX_RECT & rect)461 void CFX_RenderDevice::SetBaseClip(const FX_RECT& rect) {
462 m_pDeviceDriver->SetBaseClip(rect);
463 }
464
SetClip_PathFill(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,int fill_mode)465 bool CFX_RenderDevice::SetClip_PathFill(const CFX_PathData* pPathData,
466 const CFX_Matrix* pObject2Device,
467 int fill_mode) {
468 if (!m_pDeviceDriver->SetClip_PathFill(pPathData, pObject2Device,
469 fill_mode)) {
470 return false;
471 }
472 UpdateClipBox();
473 return true;
474 }
475
SetClip_PathStroke(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState)476 bool CFX_RenderDevice::SetClip_PathStroke(
477 const CFX_PathData* pPathData,
478 const CFX_Matrix* pObject2Device,
479 const CFX_GraphStateData* pGraphState) {
480 if (!m_pDeviceDriver->SetClip_PathStroke(pPathData, pObject2Device,
481 pGraphState)) {
482 return false;
483 }
484 UpdateClipBox();
485 return true;
486 }
487
SetClip_Rect(const FX_RECT & rect)488 bool CFX_RenderDevice::SetClip_Rect(const FX_RECT& rect) {
489 CFX_PathData path;
490 path.AppendRect(rect.left, rect.bottom, rect.right, rect.top);
491 if (!SetClip_PathFill(&path, nullptr, FXFILL_WINDING))
492 return false;
493
494 UpdateClipBox();
495 return true;
496 }
497
UpdateClipBox()498 void CFX_RenderDevice::UpdateClipBox() {
499 if (m_pDeviceDriver->GetClipBox(&m_ClipBox))
500 return;
501 m_ClipBox.left = 0;
502 m_ClipBox.top = 0;
503 m_ClipBox.right = m_Width;
504 m_ClipBox.bottom = m_Height;
505 }
506
DrawPathWithBlend(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState,uint32_t fill_color,uint32_t stroke_color,int fill_mode,BlendMode blend_type)507 bool CFX_RenderDevice::DrawPathWithBlend(const CFX_PathData* pPathData,
508 const CFX_Matrix* pObject2Device,
509 const CFX_GraphStateData* pGraphState,
510 uint32_t fill_color,
511 uint32_t stroke_color,
512 int fill_mode,
513 BlendMode blend_type) {
514 uint8_t stroke_alpha = pGraphState ? FXARGB_A(stroke_color) : 0;
515 uint8_t fill_alpha = (fill_mode & 3) ? FXARGB_A(fill_color) : 0;
516 const std::vector<FX_PATHPOINT>& pPoints = pPathData->GetPoints();
517 if (stroke_alpha == 0 && pPoints.size() == 2) {
518 CFX_PointF pos1 = pPoints[0].m_Point;
519 CFX_PointF pos2 = pPoints[1].m_Point;
520 if (pObject2Device) {
521 pos1 = pObject2Device->Transform(pos1);
522 pos2 = pObject2Device->Transform(pos2);
523 }
524 DrawCosmeticLine(pos1, pos2, fill_color, fill_mode, blend_type);
525 return true;
526 }
527
528 if ((pPoints.size() == 5 || pPoints.size() == 4) && stroke_alpha == 0) {
529 CFX_FloatRect rect_f;
530 if (!(fill_mode & FXFILL_RECT_AA) &&
531 pPathData->IsRect(pObject2Device, &rect_f)) {
532 FX_RECT rect_i = rect_f.GetOuterRect();
533
534 // Depending on the top/bottom, left/right values of the rect it's
535 // possible to overflow the Width() and Height() calculations. Check that
536 // the rect will have valid dimension before continuing.
537 if (!rect_i.Valid())
538 return false;
539
540 int width = static_cast<int>(ceil(rect_f.right - rect_f.left));
541 if (width < 1) {
542 width = 1;
543 if (rect_i.left == rect_i.right)
544 ++rect_i.right;
545 }
546 int height = static_cast<int>(ceil(rect_f.top - rect_f.bottom));
547 if (height < 1) {
548 height = 1;
549 if (rect_i.bottom == rect_i.top)
550 ++rect_i.bottom;
551 }
552 if (rect_i.Width() >= width + 1) {
553 if (rect_f.left - static_cast<float>(rect_i.left) >
554 static_cast<float>(rect_i.right) - rect_f.right) {
555 ++rect_i.left;
556 } else {
557 --rect_i.right;
558 }
559 }
560 if (rect_i.Height() >= height + 1) {
561 if (rect_f.top - static_cast<float>(rect_i.top) >
562 static_cast<float>(rect_i.bottom) - rect_f.bottom) {
563 ++rect_i.top;
564 } else {
565 --rect_i.bottom;
566 }
567 }
568 if (FillRectWithBlend(rect_i, fill_color, blend_type))
569 return true;
570 }
571 }
572 if ((fill_mode & 3) && stroke_alpha == 0 && !(fill_mode & FX_FILL_STROKE) &&
573 !(fill_mode & FX_FILL_TEXT_MODE)) {
574 CFX_PathData newPath;
575 bool bThin = false;
576 bool setIdentity = false;
577 if (pPathData->GetZeroAreaPath(pObject2Device,
578 !!m_pDeviceDriver->GetDriverType(), &newPath,
579 &bThin, &setIdentity)) {
580 CFX_GraphStateData graphState;
581 graphState.m_LineWidth = 0.0f;
582
583 uint32_t strokecolor = fill_color;
584 if (bThin)
585 strokecolor = (((fill_alpha >> 2) << 24) | (strokecolor & 0x00ffffff));
586
587 const CFX_Matrix* pMatrix = nullptr;
588 if (pObject2Device && !pObject2Device->IsIdentity() && !setIdentity)
589 pMatrix = pObject2Device;
590
591 int smooth_path = FX_ZEROAREA_FILL;
592 if (fill_mode & FXFILL_NOPATHSMOOTH)
593 smooth_path |= FXFILL_NOPATHSMOOTH;
594
595 m_pDeviceDriver->DrawPath(&newPath, pMatrix, &graphState, 0, strokecolor,
596 smooth_path, blend_type);
597 }
598 }
599 if ((fill_mode & 3) && fill_alpha && stroke_alpha < 0xff &&
600 (fill_mode & FX_FILL_STROKE)) {
601 if (m_RenderCaps & FXRC_FILLSTROKE_PATH) {
602 return m_pDeviceDriver->DrawPath(pPathData, pObject2Device, pGraphState,
603 fill_color, stroke_color, fill_mode,
604 blend_type);
605 }
606 return DrawFillStrokePath(pPathData, pObject2Device, pGraphState,
607 fill_color, stroke_color, fill_mode, blend_type);
608 }
609 return m_pDeviceDriver->DrawPath(pPathData, pObject2Device, pGraphState,
610 fill_color, stroke_color, fill_mode,
611 blend_type);
612 }
613
614 // This can be removed once PDFium entirely relies on Skia
DrawFillStrokePath(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState,uint32_t fill_color,uint32_t stroke_color,int fill_mode,BlendMode blend_type)615 bool CFX_RenderDevice::DrawFillStrokePath(const CFX_PathData* pPathData,
616 const CFX_Matrix* pObject2Device,
617 const CFX_GraphStateData* pGraphState,
618 uint32_t fill_color,
619 uint32_t stroke_color,
620 int fill_mode,
621 BlendMode blend_type) {
622 if (!(m_RenderCaps & FXRC_GET_BITS))
623 return false;
624 CFX_FloatRect bbox;
625 if (pGraphState) {
626 bbox = pPathData->GetBoundingBox(pGraphState->m_LineWidth,
627 pGraphState->m_MiterLimit);
628 } else {
629 bbox = pPathData->GetBoundingBox();
630 }
631 if (pObject2Device)
632 bbox = pObject2Device->TransformRect(bbox);
633
634 FX_RECT rect = bbox.GetOuterRect();
635 if (!rect.Valid())
636 return false;
637
638 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
639 auto backdrop = pdfium::MakeRetain<CFX_DIBitmap>();
640 if (!CreateCompatibleBitmap(bitmap, rect.Width(), rect.Height()))
641 return false;
642
643 if (bitmap->HasAlpha()) {
644 bitmap->Clear(0);
645 backdrop->Copy(bitmap);
646 } else {
647 if (!m_pDeviceDriver->GetDIBits(bitmap, rect.left, rect.top))
648 return false;
649 backdrop->Copy(bitmap);
650 }
651 CFX_DefaultRenderDevice bitmap_device;
652 bitmap_device.Attach(bitmap, false, backdrop, true);
653
654 CFX_Matrix matrix;
655 if (pObject2Device)
656 matrix = *pObject2Device;
657 matrix.Translate(-rect.left, -rect.top);
658 if (!bitmap_device.GetDeviceDriver()->DrawPath(
659 pPathData, &matrix, pGraphState, fill_color, stroke_color, fill_mode,
660 blend_type)) {
661 return false;
662 }
663 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
664 bitmap_device.GetDeviceDriver()->Flush();
665 #endif
666 FX_RECT src_rect(0, 0, rect.Width(), rect.Height());
667 return m_pDeviceDriver->SetDIBits(bitmap, 0, src_rect, rect.left, rect.top,
668 BlendMode::kNormal);
669 }
670
FillRectWithBlend(const FX_RECT & rect,uint32_t fill_color,BlendMode blend_type)671 bool CFX_RenderDevice::FillRectWithBlend(const FX_RECT& rect,
672 uint32_t fill_color,
673 BlendMode blend_type) {
674 if (m_pDeviceDriver->FillRectWithBlend(rect, fill_color, blend_type))
675 return true;
676
677 if (!(m_RenderCaps & FXRC_GET_BITS))
678 return false;
679
680 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
681 if (!CreateCompatibleBitmap(bitmap, rect.Width(), rect.Height()))
682 return false;
683
684 if (!m_pDeviceDriver->GetDIBits(bitmap, rect.left, rect.top))
685 return false;
686
687 if (!bitmap->CompositeRect(0, 0, rect.Width(), rect.Height(), fill_color,
688 0)) {
689 return false;
690 }
691 FX_RECT src_rect(0, 0, rect.Width(), rect.Height());
692 m_pDeviceDriver->SetDIBits(bitmap, 0, src_rect, rect.left, rect.top,
693 BlendMode::kNormal);
694 return true;
695 }
696
DrawCosmeticLine(const CFX_PointF & ptMoveTo,const CFX_PointF & ptLineTo,uint32_t color,int fill_mode,BlendMode blend_type)697 bool CFX_RenderDevice::DrawCosmeticLine(const CFX_PointF& ptMoveTo,
698 const CFX_PointF& ptLineTo,
699 uint32_t color,
700 int fill_mode,
701 BlendMode blend_type) {
702 if ((color >= 0xff000000) && m_pDeviceDriver->DrawCosmeticLine(
703 ptMoveTo, ptLineTo, color, blend_type)) {
704 return true;
705 }
706 CFX_GraphStateData graph_state;
707 CFX_PathData path;
708 path.AppendPoint(ptMoveTo, FXPT_TYPE::MoveTo, false);
709 path.AppendPoint(ptLineTo, FXPT_TYPE::LineTo, false);
710 return m_pDeviceDriver->DrawPath(&path, nullptr, &graph_state, 0, color,
711 fill_mode, blend_type);
712 }
713
GetDIBits(const RetainPtr<CFX_DIBitmap> & pBitmap,int left,int top)714 bool CFX_RenderDevice::GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap,
715 int left,
716 int top) {
717 return (m_RenderCaps & FXRC_GET_BITS) &&
718 m_pDeviceDriver->GetDIBits(pBitmap, left, top);
719 }
720
GetBackDrop()721 RetainPtr<CFX_DIBitmap> CFX_RenderDevice::GetBackDrop() {
722 return m_pDeviceDriver->GetBackDrop();
723 }
724
SetDIBitsWithBlend(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,BlendMode blend_mode)725 bool CFX_RenderDevice::SetDIBitsWithBlend(const RetainPtr<CFX_DIBBase>& pBitmap,
726 int left,
727 int top,
728 BlendMode blend_mode) {
729 ASSERT(!pBitmap->IsAlphaMask());
730 FX_RECT dest_rect(left, top, left + pBitmap->GetWidth(),
731 top + pBitmap->GetHeight());
732 dest_rect.Intersect(m_ClipBox);
733 if (dest_rect.IsEmpty())
734 return true;
735
736 FX_RECT src_rect(dest_rect.left - left, dest_rect.top - top,
737 dest_rect.left - left + dest_rect.Width(),
738 dest_rect.top - top + dest_rect.Height());
739 if ((blend_mode == BlendMode::kNormal || (m_RenderCaps & FXRC_BLEND_MODE)) &&
740 (!pBitmap->HasAlpha() || (m_RenderCaps & FXRC_ALPHA_IMAGE))) {
741 return m_pDeviceDriver->SetDIBits(pBitmap, 0, src_rect, dest_rect.left,
742 dest_rect.top, blend_mode);
743 }
744 if (!(m_RenderCaps & FXRC_GET_BITS))
745 return false;
746
747 int bg_pixel_width = dest_rect.Width();
748 int bg_pixel_height = dest_rect.Height();
749 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
750 if (!background->Create(
751 bg_pixel_width, bg_pixel_height,
752 (m_RenderCaps & FXRC_CMYK_OUTPUT) ? FXDIB_Cmyk : FXDIB_Rgb32)) {
753 return false;
754 }
755 if (!m_pDeviceDriver->GetDIBits(background, dest_rect.left, dest_rect.top)) {
756 return false;
757 }
758 if (!background->CompositeBitmap(0, 0, bg_pixel_width, bg_pixel_height,
759 pBitmap, src_rect.left, src_rect.top,
760 blend_mode, nullptr, false)) {
761 return false;
762 }
763 FX_RECT rect(0, 0, bg_pixel_width, bg_pixel_height);
764 return m_pDeviceDriver->SetDIBits(background, 0, rect, dest_rect.left,
765 dest_rect.top, BlendMode::kNormal);
766 }
767
StretchDIBitsWithFlagsAndBlend(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,int dest_width,int dest_height,const FXDIB_ResampleOptions & options,BlendMode blend_mode)768 bool CFX_RenderDevice::StretchDIBitsWithFlagsAndBlend(
769 const RetainPtr<CFX_DIBBase>& pBitmap,
770 int left,
771 int top,
772 int dest_width,
773 int dest_height,
774 const FXDIB_ResampleOptions& options,
775 BlendMode blend_mode) {
776 FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
777 FX_RECT clip_box = m_ClipBox;
778 clip_box.Intersect(dest_rect);
779 return clip_box.IsEmpty() || m_pDeviceDriver->StretchDIBits(
780 pBitmap, 0, left, top, dest_width,
781 dest_height, &clip_box, options, blend_mode);
782 }
783
SetBitMask(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,uint32_t argb)784 bool CFX_RenderDevice::SetBitMask(const RetainPtr<CFX_DIBBase>& pBitmap,
785 int left,
786 int top,
787 uint32_t argb) {
788 FX_RECT src_rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
789 return m_pDeviceDriver->SetDIBits(pBitmap, argb, src_rect, left, top,
790 BlendMode::kNormal);
791 }
792
StretchBitMask(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,int dest_width,int dest_height,uint32_t color)793 bool CFX_RenderDevice::StretchBitMask(const RetainPtr<CFX_DIBBase>& pBitmap,
794 int left,
795 int top,
796 int dest_width,
797 int dest_height,
798 uint32_t color) {
799 return StretchBitMaskWithFlags(pBitmap, left, top, dest_width, dest_height,
800 color, FXDIB_ResampleOptions());
801 }
802
StretchBitMaskWithFlags(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,int dest_width,int dest_height,uint32_t argb,const FXDIB_ResampleOptions & options)803 bool CFX_RenderDevice::StretchBitMaskWithFlags(
804 const RetainPtr<CFX_DIBBase>& pBitmap,
805 int left,
806 int top,
807 int dest_width,
808 int dest_height,
809 uint32_t argb,
810 const FXDIB_ResampleOptions& options) {
811 FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
812 FX_RECT clip_box = m_ClipBox;
813 clip_box.Intersect(dest_rect);
814 return m_pDeviceDriver->StretchDIBits(pBitmap, argb, left, top, dest_width,
815 dest_height, &clip_box, options,
816 BlendMode::kNormal);
817 }
818
StartDIBitsWithBlend(const RetainPtr<CFX_DIBBase> & pBitmap,int bitmap_alpha,uint32_t argb,const CFX_Matrix & matrix,const FXDIB_ResampleOptions & options,std::unique_ptr<CFX_ImageRenderer> * handle,BlendMode blend_mode)819 bool CFX_RenderDevice::StartDIBitsWithBlend(
820 const RetainPtr<CFX_DIBBase>& pBitmap,
821 int bitmap_alpha,
822 uint32_t argb,
823 const CFX_Matrix& matrix,
824 const FXDIB_ResampleOptions& options,
825 std::unique_ptr<CFX_ImageRenderer>* handle,
826 BlendMode blend_mode) {
827 return m_pDeviceDriver->StartDIBits(pBitmap, bitmap_alpha, argb, matrix,
828 options, handle, blend_mode);
829 }
830
ContinueDIBits(CFX_ImageRenderer * handle,PauseIndicatorIface * pPause)831 bool CFX_RenderDevice::ContinueDIBits(CFX_ImageRenderer* handle,
832 PauseIndicatorIface* pPause) {
833 return m_pDeviceDriver->ContinueDIBits(handle, pPause);
834 }
835
836 #ifdef _SKIA_SUPPORT_
DebugVerifyBitmapIsPreMultiplied() const837 void CFX_RenderDevice::DebugVerifyBitmapIsPreMultiplied() const {
838 NOTREACHED();
839 }
840
SetBitsWithMask(const RetainPtr<CFX_DIBBase> & pBitmap,const RetainPtr<CFX_DIBBase> & pMask,int left,int top,int bitmap_alpha,BlendMode blend_type)841 bool CFX_RenderDevice::SetBitsWithMask(const RetainPtr<CFX_DIBBase>& pBitmap,
842 const RetainPtr<CFX_DIBBase>& pMask,
843 int left,
844 int top,
845 int bitmap_alpha,
846 BlendMode blend_type) {
847 return m_pDeviceDriver->SetBitsWithMask(pBitmap, pMask, left, top,
848 bitmap_alpha, blend_type);
849 }
850 #endif
851
DrawNormalText(int nChars,const TextCharPos * pCharPos,CFX_Font * pFont,float font_size,const CFX_Matrix & mtText2Device,uint32_t fill_color,uint32_t text_flags)852 bool CFX_RenderDevice::DrawNormalText(int nChars,
853 const TextCharPos* pCharPos,
854 CFX_Font* pFont,
855 float font_size,
856 const CFX_Matrix& mtText2Device,
857 uint32_t fill_color,
858 uint32_t text_flags) {
859 int nativetext_flags = text_flags;
860 if (m_DeviceType != DeviceType::kDisplay) {
861 if (!(text_flags & FXTEXT_PRINTGRAPHICTEXT)) {
862 if (ShouldDrawDeviceText(pFont, text_flags) &&
863 m_pDeviceDriver->DrawDeviceText(
864 nChars, pCharPos, pFont, mtText2Device, font_size, fill_color)) {
865 return true;
866 }
867 }
868 if (FXARGB_A(fill_color) < 255)
869 return false;
870 } else if (!(text_flags & FXTEXT_NO_NATIVETEXT)) {
871 if (ShouldDrawDeviceText(pFont, text_flags) &&
872 m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, mtText2Device,
873 font_size, fill_color)) {
874 return true;
875 }
876 }
877 CFX_Matrix char2device = mtText2Device;
878 CFX_Matrix text2Device = mtText2Device;
879 char2device.Scale(font_size, -font_size);
880 if (fabs(char2device.a) + fabs(char2device.b) > 50 * 1.0f ||
881 (m_DeviceType == DeviceType::kPrinter &&
882 !(text_flags & FXTEXT_PRINTIMAGETEXT))) {
883 if (pFont->GetFaceRec()) {
884 int nPathFlags =
885 (text_flags & FXTEXT_NOSMOOTH) == 0 ? 0 : FXFILL_NOPATHSMOOTH;
886 return DrawTextPath(nChars, pCharPos, pFont, font_size, mtText2Device,
887 nullptr, nullptr, fill_color, 0, nullptr, nPathFlags);
888 }
889 }
890 int anti_alias = FT_RENDER_MODE_MONO;
891 bool bNormal = false;
892 if ((text_flags & FXTEXT_NOSMOOTH) == 0) {
893 if (m_DeviceType == DeviceType::kDisplay && m_bpp > 1) {
894 if (!CFX_GEModule::Get()->GetFontMgr()->FTLibrarySupportsHinting()) {
895 // Some Freetype implementations (like the one packaged with Fedora) do
896 // not support hinting due to patents 6219025, 6239783, 6307566,
897 // 6225973, 6243070, 6393145, 6421054, 6282327, and 6624828; the latest
898 // one expires 10/7/19. This makes LCD antialiasing very ugly, so we
899 // instead fall back on NORMAL antialiasing.
900 anti_alias = FT_RENDER_MODE_NORMAL;
901 } else if ((m_RenderCaps & (FXRC_ALPHA_OUTPUT | FXRC_CMYK_OUTPUT))) {
902 anti_alias = FT_RENDER_MODE_LCD;
903 bNormal = true;
904 } else if (m_bpp < 16) {
905 anti_alias = FT_RENDER_MODE_NORMAL;
906 } else {
907 anti_alias = FT_RENDER_MODE_LCD;
908
909 bool bClearType = false;
910 if (pFont->GetFaceRec())
911 bClearType = !!(text_flags & FXTEXT_CLEARTYPE);
912 bNormal = !bClearType;
913 }
914 }
915 }
916 std::vector<TextGlyphPos> glyphs(nChars);
917 CFX_Matrix deviceCtm = char2device;
918
919 for (size_t i = 0; i < glyphs.size(); ++i) {
920 TextGlyphPos& glyph = glyphs[i];
921 const TextCharPos& charpos = pCharPos[i];
922
923 glyph.m_fOrigin = text2Device.Transform(charpos.m_Origin);
924 if (anti_alias < FT_RENDER_MODE_LCD)
925 glyph.m_Origin.x = FXSYS_roundf(glyph.m_fOrigin.x);
926 else
927 glyph.m_Origin.x = static_cast<int>(floor(glyph.m_fOrigin.x));
928 glyph.m_Origin.y = FXSYS_roundf(glyph.m_fOrigin.y);
929
930 if (charpos.m_bGlyphAdjust) {
931 CFX_Matrix new_matrix(
932 charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
933 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
934 new_matrix.Concat(deviceCtm);
935 glyph.m_pGlyph = pFont->LoadGlyphBitmap(
936 charpos.m_GlyphIndex, charpos.m_bFontStyle, new_matrix,
937 charpos.m_FontCharWidth, anti_alias, &nativetext_flags);
938 } else {
939 glyph.m_pGlyph = pFont->LoadGlyphBitmap(
940 charpos.m_GlyphIndex, charpos.m_bFontStyle, deviceCtm,
941 charpos.m_FontCharWidth, anti_alias, &nativetext_flags);
942 }
943 }
944 if (anti_alias < FT_RENDER_MODE_LCD && glyphs.size() > 1)
945 AdjustGlyphSpace(&glyphs);
946
947 FX_RECT bmp_rect = GetGlyphsBBox(glyphs, anti_alias);
948 bmp_rect.Intersect(m_ClipBox);
949 if (bmp_rect.IsEmpty())
950 return true;
951
952 int pixel_width = bmp_rect.Width();
953 int pixel_height = bmp_rect.Height();
954 int pixel_left = bmp_rect.left;
955 int pixel_top = bmp_rect.top;
956 if (anti_alias == FT_RENDER_MODE_MONO) {
957 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
958 if (!bitmap->Create(pixel_width, pixel_height, FXDIB_1bppMask))
959 return false;
960 bitmap->Clear(0);
961 for (const TextGlyphPos& glyph : glyphs) {
962 if (!glyph.m_pGlyph)
963 continue;
964
965 Optional<CFX_Point> point = glyph.GetOrigin({pixel_left, pixel_top});
966 if (!point.has_value())
967 continue;
968
969 const RetainPtr<CFX_DIBitmap>& pGlyph = glyph.m_pGlyph->GetBitmap();
970 bitmap->TransferBitmap(point.value().x, point.value().y,
971 pGlyph->GetWidth(), pGlyph->GetHeight(), pGlyph, 0,
972 0);
973 }
974 return SetBitMask(bitmap, bmp_rect.left, bmp_rect.top, fill_color);
975 }
976 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
977 if (m_bpp == 8) {
978 if (!bitmap->Create(pixel_width, pixel_height, FXDIB_8bppMask))
979 return false;
980 } else {
981 if (!CreateCompatibleBitmap(bitmap, pixel_width, pixel_height))
982 return false;
983 }
984 if (!bitmap->HasAlpha() && !bitmap->IsAlphaMask()) {
985 bitmap->Clear(0xFFFFFFFF);
986 if (!GetDIBits(bitmap, bmp_rect.left, bmp_rect.top))
987 return false;
988 } else {
989 bitmap->Clear(0);
990 if (bitmap->m_pAlphaMask)
991 bitmap->m_pAlphaMask->Clear(0);
992 }
993 int dest_width = pixel_width;
994 int a = 0;
995 int r = 0;
996 int g = 0;
997 int b = 0;
998 if (anti_alias == FT_RENDER_MODE_LCD)
999 std::tie(a, r, g, b) = ArgbDecode(fill_color);
1000
1001 for (const TextGlyphPos& glyph : glyphs) {
1002 if (!glyph.m_pGlyph)
1003 continue;
1004
1005 Optional<CFX_Point> point = glyph.GetOrigin({pixel_left, pixel_top});
1006 if (!point.has_value())
1007 continue;
1008
1009 const RetainPtr<CFX_DIBitmap>& pGlyph = glyph.m_pGlyph->GetBitmap();
1010 int ncols = pGlyph->GetWidth();
1011 int nrows = pGlyph->GetHeight();
1012 if (anti_alias == FT_RENDER_MODE_NORMAL) {
1013 if (!bitmap->CompositeMask(point.value().x, point.value().y, ncols, nrows,
1014 pGlyph, fill_color, 0, 0, BlendMode::kNormal,
1015 nullptr, false)) {
1016 return false;
1017 }
1018 continue;
1019 }
1020 bool bBGRStripe = !!(text_flags & FXTEXT_BGR_STRIPE);
1021 ncols /= 3;
1022 int x_subpixel = static_cast<int>(glyph.m_fOrigin.x * 3) % 3;
1023 int start_col = std::max(point->x, 0);
1024 FX_SAFE_INT32 end_col_safe = point->x;
1025 end_col_safe += ncols;
1026 if (!end_col_safe.IsValid())
1027 continue;
1028
1029 int end_col = std::min<int>(end_col_safe.ValueOrDie(), dest_width);
1030 if (start_col >= end_col)
1031 continue;
1032
1033 DrawNormalTextHelper(bitmap, pGlyph, nrows, point->x, point->y, start_col,
1034 end_col, bNormal, bBGRStripe, x_subpixel, a, r, g, b);
1035 }
1036 if (bitmap->IsAlphaMask())
1037 SetBitMask(bitmap, bmp_rect.left, bmp_rect.top, fill_color);
1038 else
1039 SetDIBits(bitmap, bmp_rect.left, bmp_rect.top);
1040 return true;
1041 }
1042
DrawTextPath(int nChars,const TextCharPos * pCharPos,CFX_Font * pFont,float font_size,const CFX_Matrix & mtText2User,const CFX_Matrix * pUser2Device,const CFX_GraphStateData * pGraphState,uint32_t fill_color,FX_ARGB stroke_color,CFX_PathData * pClippingPath,int nFlag)1043 bool CFX_RenderDevice::DrawTextPath(int nChars,
1044 const TextCharPos* pCharPos,
1045 CFX_Font* pFont,
1046 float font_size,
1047 const CFX_Matrix& mtText2User,
1048 const CFX_Matrix* pUser2Device,
1049 const CFX_GraphStateData* pGraphState,
1050 uint32_t fill_color,
1051 FX_ARGB stroke_color,
1052 CFX_PathData* pClippingPath,
1053 int nFlag) {
1054 for (int iChar = 0; iChar < nChars; ++iChar) {
1055 const TextCharPos& charpos = pCharPos[iChar];
1056 CFX_Matrix matrix;
1057 if (charpos.m_bGlyphAdjust) {
1058 matrix = CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
1059 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3],
1060 0, 0);
1061 }
1062 matrix.Concat(CFX_Matrix(font_size, 0, 0, font_size, charpos.m_Origin.x,
1063 charpos.m_Origin.y));
1064 const CFX_PathData* pPath =
1065 pFont->LoadGlyphPath(charpos.m_GlyphIndex, charpos.m_FontCharWidth);
1066 if (!pPath)
1067 continue;
1068
1069 matrix.Concat(mtText2User);
1070
1071 CFX_PathData TransformedPath(*pPath);
1072 TransformedPath.Transform(matrix);
1073 if (fill_color || stroke_color) {
1074 int fill_mode = nFlag;
1075 if (fill_color)
1076 fill_mode |= FXFILL_WINDING;
1077 fill_mode |= FX_FILL_TEXT_MODE;
1078 if (!DrawPathWithBlend(&TransformedPath, pUser2Device, pGraphState,
1079 fill_color, stroke_color, fill_mode,
1080 BlendMode::kNormal)) {
1081 return false;
1082 }
1083 }
1084 if (pClippingPath)
1085 pClippingPath->Append(&TransformedPath, pUser2Device);
1086 }
1087 return true;
1088 }
1089
DrawFillRect(const CFX_Matrix * pUser2Device,const CFX_FloatRect & rect,const FX_COLORREF & color)1090 void CFX_RenderDevice::DrawFillRect(const CFX_Matrix* pUser2Device,
1091 const CFX_FloatRect& rect,
1092 const FX_COLORREF& color) {
1093 CFX_PathData path;
1094 path.AppendFloatRect(rect);
1095 DrawPath(&path, pUser2Device, nullptr, color, 0, FXFILL_WINDING);
1096 }
1097
DrawFillArea(const CFX_Matrix & mtUser2Device,const std::vector<CFX_PointF> & points,const FX_COLORREF & color)1098 void CFX_RenderDevice::DrawFillArea(const CFX_Matrix& mtUser2Device,
1099 const std::vector<CFX_PointF>& points,
1100 const FX_COLORREF& color) {
1101 ASSERT(!points.empty());
1102 CFX_PathData path;
1103 path.AppendPoint(points[0], FXPT_TYPE::MoveTo, false);
1104 for (size_t i = 1; i < points.size(); ++i)
1105 path.AppendPoint(points[i], FXPT_TYPE::LineTo, false);
1106
1107 DrawPath(&path, &mtUser2Device, nullptr, color, 0, FXFILL_ALTERNATE);
1108 }
1109
DrawStrokeRect(const CFX_Matrix & mtUser2Device,const CFX_FloatRect & rect,const FX_COLORREF & color,float fWidth)1110 void CFX_RenderDevice::DrawStrokeRect(const CFX_Matrix& mtUser2Device,
1111 const CFX_FloatRect& rect,
1112 const FX_COLORREF& color,
1113 float fWidth) {
1114 CFX_GraphStateData gsd;
1115 gsd.m_LineWidth = fWidth;
1116
1117 CFX_PathData path;
1118 path.AppendFloatRect(rect);
1119 DrawPath(&path, &mtUser2Device, &gsd, 0, color, FXFILL_ALTERNATE);
1120 }
1121
DrawStrokeLine(const CFX_Matrix * pUser2Device,const CFX_PointF & ptMoveTo,const CFX_PointF & ptLineTo,const FX_COLORREF & color,float fWidth)1122 void CFX_RenderDevice::DrawStrokeLine(const CFX_Matrix* pUser2Device,
1123 const CFX_PointF& ptMoveTo,
1124 const CFX_PointF& ptLineTo,
1125 const FX_COLORREF& color,
1126 float fWidth) {
1127 CFX_PathData path;
1128 path.AppendPoint(ptMoveTo, FXPT_TYPE::MoveTo, false);
1129 path.AppendPoint(ptLineTo, FXPT_TYPE::LineTo, false);
1130
1131 CFX_GraphStateData gsd;
1132 gsd.m_LineWidth = fWidth;
1133
1134 DrawPath(&path, pUser2Device, &gsd, 0, color, FXFILL_ALTERNATE);
1135 }
1136
DrawFillRect(const CFX_Matrix * pUser2Device,const CFX_FloatRect & rect,const CFX_Color & color,int32_t nTransparency)1137 void CFX_RenderDevice::DrawFillRect(const CFX_Matrix* pUser2Device,
1138 const CFX_FloatRect& rect,
1139 const CFX_Color& color,
1140 int32_t nTransparency) {
1141 DrawFillRect(pUser2Device, rect, color.ToFXColor(nTransparency));
1142 }
1143
DrawShadow(const CFX_Matrix & mtUser2Device,bool bVertical,bool bHorizontal,const CFX_FloatRect & rect,int32_t nTransparency,int32_t nStartGray,int32_t nEndGray)1144 void CFX_RenderDevice::DrawShadow(const CFX_Matrix& mtUser2Device,
1145 bool bVertical,
1146 bool bHorizontal,
1147 const CFX_FloatRect& rect,
1148 int32_t nTransparency,
1149 int32_t nStartGray,
1150 int32_t nEndGray) {
1151 float fStepGray = 1.0f;
1152
1153 if (bVertical) {
1154 fStepGray = (nEndGray - nStartGray) / rect.Height();
1155
1156 for (float fy = rect.bottom + 0.5f; fy <= rect.top - 0.5f; fy += 1.0f) {
1157 int32_t nGray = nStartGray + (int32_t)(fStepGray * (fy - rect.bottom));
1158 DrawStrokeLine(&mtUser2Device, CFX_PointF(rect.left, fy),
1159 CFX_PointF(rect.right, fy),
1160 ArgbEncode(nTransparency, nGray, nGray, nGray), 1.5f);
1161 }
1162 }
1163
1164 if (bHorizontal) {
1165 fStepGray = (nEndGray - nStartGray) / rect.Width();
1166
1167 for (float fx = rect.left + 0.5f; fx <= rect.right - 0.5f; fx += 1.0f) {
1168 int32_t nGray = nStartGray + (int32_t)(fStepGray * (fx - rect.left));
1169 DrawStrokeLine(&mtUser2Device, CFX_PointF(fx, rect.bottom),
1170 CFX_PointF(fx, rect.top),
1171 ArgbEncode(nTransparency, nGray, nGray, nGray), 1.5f);
1172 }
1173 }
1174 }
1175
DrawBorder(const CFX_Matrix * pUser2Device,const CFX_FloatRect & rect,float fWidth,const CFX_Color & color,const CFX_Color & crLeftTop,const CFX_Color & crRightBottom,BorderStyle nStyle,int32_t nTransparency)1176 void CFX_RenderDevice::DrawBorder(const CFX_Matrix* pUser2Device,
1177 const CFX_FloatRect& rect,
1178 float fWidth,
1179 const CFX_Color& color,
1180 const CFX_Color& crLeftTop,
1181 const CFX_Color& crRightBottom,
1182 BorderStyle nStyle,
1183 int32_t nTransparency) {
1184 float fLeft = rect.left;
1185 float fRight = rect.right;
1186 float fTop = rect.top;
1187 float fBottom = rect.bottom;
1188
1189 if (fWidth > 0.0f) {
1190 float fHalfWidth = fWidth / 2.0f;
1191
1192 switch (nStyle) {
1193 default:
1194 case BorderStyle::SOLID: {
1195 CFX_PathData path;
1196 path.AppendRect(fLeft, fBottom, fRight, fTop);
1197 path.AppendRect(fLeft + fWidth, fBottom + fWidth, fRight - fWidth,
1198 fTop - fWidth);
1199 DrawPath(&path, pUser2Device, nullptr, color.ToFXColor(nTransparency),
1200 0, FXFILL_ALTERNATE);
1201 break;
1202 }
1203 case BorderStyle::DASH: {
1204 CFX_PathData path;
1205 path.AppendPoint(
1206 CFX_PointF(fLeft + fWidth / 2.0f, fBottom + fWidth / 2.0f),
1207 FXPT_TYPE::MoveTo, false);
1208 path.AppendPoint(
1209 CFX_PointF(fLeft + fWidth / 2.0f, fTop - fWidth / 2.0f),
1210 FXPT_TYPE::LineTo, false);
1211 path.AppendPoint(
1212 CFX_PointF(fRight - fWidth / 2.0f, fTop - fWidth / 2.0f),
1213 FXPT_TYPE::LineTo, false);
1214 path.AppendPoint(
1215 CFX_PointF(fRight - fWidth / 2.0f, fBottom + fWidth / 2.0f),
1216 FXPT_TYPE::LineTo, false);
1217 path.AppendPoint(
1218 CFX_PointF(fLeft + fWidth / 2.0f, fBottom + fWidth / 2.0f),
1219 FXPT_TYPE::LineTo, false);
1220
1221 CFX_GraphStateData gsd;
1222 gsd.m_DashArray = {3.0f, 3.0f};
1223 gsd.m_DashPhase = 0;
1224 gsd.m_LineWidth = fWidth;
1225 DrawPath(&path, pUser2Device, &gsd, 0, color.ToFXColor(nTransparency),
1226 FXFILL_WINDING);
1227 break;
1228 }
1229 case BorderStyle::BEVELED:
1230 case BorderStyle::INSET: {
1231 CFX_GraphStateData gsd;
1232 gsd.m_LineWidth = fHalfWidth;
1233
1234 CFX_PathData pathLT;
1235
1236 pathLT.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1237 FXPT_TYPE::MoveTo, false);
1238 pathLT.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fTop - fHalfWidth),
1239 FXPT_TYPE::LineTo, false);
1240 pathLT.AppendPoint(CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1241 FXPT_TYPE::LineTo, false);
1242 pathLT.AppendPoint(
1243 CFX_PointF(fRight - fHalfWidth * 2, fTop - fHalfWidth * 2),
1244 FXPT_TYPE::LineTo, false);
1245 pathLT.AppendPoint(
1246 CFX_PointF(fLeft + fHalfWidth * 2, fTop - fHalfWidth * 2),
1247 FXPT_TYPE::LineTo, false);
1248 pathLT.AppendPoint(
1249 CFX_PointF(fLeft + fHalfWidth * 2, fBottom + fHalfWidth * 2),
1250 FXPT_TYPE::LineTo, false);
1251 pathLT.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1252 FXPT_TYPE::LineTo, false);
1253
1254 DrawPath(&pathLT, pUser2Device, &gsd,
1255 crLeftTop.ToFXColor(nTransparency), 0, FXFILL_ALTERNATE);
1256
1257 CFX_PathData pathRB;
1258 pathRB.AppendPoint(CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1259 FXPT_TYPE::MoveTo, false);
1260 pathRB.AppendPoint(
1261 CFX_PointF(fRight - fHalfWidth, fBottom + fHalfWidth),
1262 FXPT_TYPE::LineTo, false);
1263 pathRB.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1264 FXPT_TYPE::LineTo, false);
1265 pathRB.AppendPoint(
1266 CFX_PointF(fLeft + fHalfWidth * 2, fBottom + fHalfWidth * 2),
1267 FXPT_TYPE::LineTo, false);
1268 pathRB.AppendPoint(
1269 CFX_PointF(fRight - fHalfWidth * 2, fBottom + fHalfWidth * 2),
1270 FXPT_TYPE::LineTo, false);
1271 pathRB.AppendPoint(
1272 CFX_PointF(fRight - fHalfWidth * 2, fTop - fHalfWidth * 2),
1273 FXPT_TYPE::LineTo, false);
1274 pathRB.AppendPoint(CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1275 FXPT_TYPE::LineTo, false);
1276
1277 DrawPath(&pathRB, pUser2Device, &gsd,
1278 crRightBottom.ToFXColor(nTransparency), 0, FXFILL_ALTERNATE);
1279
1280 CFX_PathData path;
1281
1282 path.AppendRect(fLeft, fBottom, fRight, fTop);
1283 path.AppendRect(fLeft + fHalfWidth, fBottom + fHalfWidth,
1284 fRight - fHalfWidth, fTop - fHalfWidth);
1285
1286 DrawPath(&path, pUser2Device, &gsd, color.ToFXColor(nTransparency), 0,
1287 FXFILL_ALTERNATE);
1288 break;
1289 }
1290 case BorderStyle::UNDERLINE: {
1291 CFX_PathData path;
1292 path.AppendPoint(CFX_PointF(fLeft, fBottom + fWidth / 2),
1293 FXPT_TYPE::MoveTo, false);
1294 path.AppendPoint(CFX_PointF(fRight, fBottom + fWidth / 2),
1295 FXPT_TYPE::LineTo, false);
1296
1297 CFX_GraphStateData gsd;
1298 gsd.m_LineWidth = fWidth;
1299
1300 DrawPath(&path, pUser2Device, &gsd, 0, color.ToFXColor(nTransparency),
1301 FXFILL_ALTERNATE);
1302 break;
1303 }
1304 }
1305 }
1306 }
1307
StateRestorer(CFX_RenderDevice * pDevice)1308 CFX_RenderDevice::StateRestorer::StateRestorer(CFX_RenderDevice* pDevice)
1309 : m_pDevice(pDevice) {
1310 m_pDevice->SaveState();
1311 }
1312
~StateRestorer()1313 CFX_RenderDevice::StateRestorer::~StateRestorer() {
1314 m_pDevice->RestoreState(false);
1315 }
1316