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#include <vector>
13
14#include "core/fxcrt/fx_safe_types.h"
15#include "core/fxge/cfx_facecache.h"
16#include "core/fxge/cfx_fxgedevice.h"
17#include "core/fxge/cfx_graphstatedata.h"
18#include "core/fxge/cfx_pathdata.h"
19#include "core/fxge/ifx_renderdevicedriver.h"
20
21#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
22#include "third_party/skia/include/core/SkTypes.h"
23#endif
24
25namespace {
26
27void AdjustGlyphSpace(std::vector<FXTEXT_GLYPHPOS>* pGlyphAndPos) {
28  ASSERT(pGlyphAndPos->size() > 1);
29  std::vector<FXTEXT_GLYPHPOS>& glyphs = *pGlyphAndPos;
30  bool bVertical = glyphs.back().m_Origin.x == glyphs.front().m_Origin.x;
31  if (!bVertical && (glyphs.back().m_Origin.y != glyphs.front().m_Origin.y))
32    return;
33
34  for (size_t i = glyphs.size() - 1; i > 1; --i) {
35    FXTEXT_GLYPHPOS& next = glyphs[i];
36    int next_origin = bVertical ? next.m_Origin.y : next.m_Origin.x;
37    FX_FLOAT next_origin_f = bVertical ? next.m_fOrigin.y : next.m_fOrigin.x;
38
39    FXTEXT_GLYPHPOS& current = glyphs[i - 1];
40    int& current_origin = bVertical ? current.m_Origin.y : current.m_Origin.x;
41    FX_FLOAT current_origin_f =
42        bVertical ? current.m_fOrigin.y : current.m_fOrigin.x;
43
44    int space = next_origin - current_origin;
45    FX_FLOAT space_f = next_origin_f - current_origin_f;
46    FX_FLOAT error =
47        FXSYS_fabs(space_f) - FXSYS_fabs(static_cast<FX_FLOAT>(space));
48    if (error > 0.5f)
49      current_origin += space > 0 ? -1 : 1;
50  }
51}
52
53const uint8_t g_TextGammaAdjust[256] = {
54    0,   2,   3,   4,   6,   7,   8,   10,  11,  12,  13,  15,  16,  17,  18,
55    19,  21,  22,  23,  24,  25,  26,  27,  29,  30,  31,  32,  33,  34,  35,
56    36,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  51,  52,
57    53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,
58    68,  69,  71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,
59    84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
60    99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
61    114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
62    129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
63    143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 156,
64    157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
65    172, 173, 174, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
66    186, 187, 188, 189, 190, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
67    200, 201, 202, 203, 204, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
68    214, 215, 216, 217, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
69    228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 239, 240,
70    241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 250, 251, 252, 253, 254,
71    255,
72};
73
74int TextGammaAdjust(int value) {
75  ASSERT(value >= 0);
76  ASSERT(value <= 255);
77  return g_TextGammaAdjust[value];
78}
79
80int CalcAlpha(int src, int alpha) {
81  return src * alpha / 255;
82}
83
84void Merge(uint8_t src, int channel, int alpha, uint8_t* dest) {
85  *dest = FXDIB_ALPHA_MERGE(*dest, channel, CalcAlpha(src, alpha));
86}
87
88void MergeGammaAdjust(uint8_t src, int channel, int alpha, uint8_t* dest) {
89  *dest =
90      FXDIB_ALPHA_MERGE(*dest, channel, CalcAlpha(TextGammaAdjust(src), alpha));
91}
92
93void MergeGammaAdjustBgr(const uint8_t* src,
94                         int r,
95                         int g,
96                         int b,
97                         int a,
98                         uint8_t* dest) {
99  MergeGammaAdjust(src[0], b, a, &dest[0]);
100  MergeGammaAdjust(src[1], g, a, &dest[1]);
101  MergeGammaAdjust(src[2], r, a, &dest[2]);
102}
103
104void MergeGammaAdjustRgb(const uint8_t* src,
105                         int r,
106                         int g,
107                         int b,
108                         int a,
109                         uint8_t* dest) {
110  MergeGammaAdjust(src[2], b, a, &dest[0]);
111  MergeGammaAdjust(src[1], g, a, &dest[1]);
112  MergeGammaAdjust(src[0], r, a, &dest[2]);
113}
114
115int AverageRgb(const uint8_t* src) {
116  return (src[0] + src[1] + src[2]) / 3;
117}
118
119uint8_t CalculateDestAlpha(uint8_t back_alpha, int src_alpha) {
120  return back_alpha + src_alpha - back_alpha * src_alpha / 255;
121}
122
123void ApplyDestAlpha(uint8_t back_alpha,
124                    int src_alpha,
125                    int r,
126                    int g,
127                    int b,
128                    uint8_t* dest) {
129  uint8_t dest_alpha = CalculateDestAlpha(back_alpha, src_alpha);
130  int alpha_ratio = src_alpha * 255 / dest_alpha;
131  dest[0] = FXDIB_ALPHA_MERGE(dest[0], b, alpha_ratio);
132  dest[1] = FXDIB_ALPHA_MERGE(dest[1], g, alpha_ratio);
133  dest[2] = FXDIB_ALPHA_MERGE(dest[2], r, alpha_ratio);
134  dest[3] = dest_alpha;
135}
136
137void NormalizeRgbDst(int src_value, int r, int g, int b, int a, uint8_t* dest) {
138  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
139  dest[0] = FXDIB_ALPHA_MERGE(dest[0], b, src_alpha);
140  dest[1] = FXDIB_ALPHA_MERGE(dest[1], g, src_alpha);
141  dest[2] = FXDIB_ALPHA_MERGE(dest[2], r, src_alpha);
142}
143
144void NormalizeRgbSrc(int src_value, int r, int g, int b, int a, uint8_t* dest) {
145  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
146  if (src_alpha == 0)
147    return;
148
149  dest[0] = FXDIB_ALPHA_MERGE(dest[0], b, src_alpha);
150  dest[1] = FXDIB_ALPHA_MERGE(dest[1], g, src_alpha);
151  dest[2] = FXDIB_ALPHA_MERGE(dest[2], r, src_alpha);
152}
153
154void NormalizeArgbDest(int src_value,
155                       int r,
156                       int g,
157                       int b,
158                       int a,
159                       uint8_t* dest) {
160  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
161  uint8_t back_alpha = dest[3];
162  if (back_alpha == 0) {
163    FXARGB_SETDIB(dest, FXARGB_MAKE(src_alpha, r, g, b));
164  } else if (src_alpha != 0) {
165    ApplyDestAlpha(back_alpha, src_alpha, r, g, b, dest);
166  }
167}
168
169void NormalizeArgbSrc(int src_value,
170                      int r,
171                      int g,
172                      int b,
173                      int a,
174                      uint8_t* dest) {
175  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
176  if (src_alpha == 0)
177    return;
178
179  uint8_t back_alpha = dest[3];
180  if (back_alpha == 0) {
181    FXARGB_SETDIB(dest, FXARGB_MAKE(src_alpha, r, g, b));
182  } else {
183    ApplyDestAlpha(back_alpha, src_alpha, r, g, b, dest);
184  }
185}
186
187void NextPixel(uint8_t** src_scan, uint8_t** dst_scan, int bpp) {
188  *src_scan += 3;
189  *dst_scan += bpp;
190}
191
192void SetAlpha(uint8_t* alpha) {
193  alpha[3] = 255;
194}
195
196void SetAlphaDoNothing(uint8_t* alpha) {}
197
198void DrawNormalTextHelper(CFX_DIBitmap* bitmap,
199                          const CFX_DIBitmap* pGlyph,
200                          int nrows,
201                          int left,
202                          int top,
203                          int start_col,
204                          int end_col,
205                          bool bNormal,
206                          bool bBGRStripe,
207                          int x_subpixel,
208                          int a,
209                          int r,
210                          int g,
211                          int b) {
212  const bool has_alpha = bitmap->GetFormat() == FXDIB_Argb;
213  uint8_t* src_buf = pGlyph->GetBuffer();
214  int src_pitch = pGlyph->GetPitch();
215  uint8_t* dest_buf = bitmap->GetBuffer();
216  int dest_pitch = bitmap->GetPitch();
217  const int Bpp = has_alpha ? 4 : bitmap->GetBPP() / 8;
218  auto* pNormalizeSrcFunc = has_alpha ? &NormalizeArgbSrc : &NormalizeRgbDst;
219  auto* pNormalizeDstFunc = has_alpha ? &NormalizeArgbDest : &NormalizeRgbSrc;
220  auto* pSetAlpha = has_alpha ? &SetAlpha : &SetAlphaDoNothing;
221
222  for (int row = 0; row < nrows; row++) {
223    int dest_row = row + top;
224    if (dest_row < 0 || dest_row >= bitmap->GetHeight())
225      continue;
226
227    uint8_t* src_scan = src_buf + row * src_pitch + (start_col - left) * 3;
228    uint8_t* dest_scan = dest_buf + dest_row * dest_pitch + start_col * Bpp;
229    if (bBGRStripe) {
230      if (x_subpixel == 0) {
231        for (int col = start_col; col < end_col; col++) {
232          if (has_alpha) {
233            Merge(src_scan[2], r, a, &dest_scan[2]);
234            Merge(src_scan[1], g, a, &dest_scan[1]);
235            Merge(src_scan[0], b, a, &dest_scan[0]);
236          } else {
237            MergeGammaAdjustBgr(&src_scan[0], r, g, b, a, &dest_scan[0]);
238          }
239          pSetAlpha(dest_scan);
240          NextPixel(&src_scan, &dest_scan, Bpp);
241        }
242      } else if (x_subpixel == 1) {
243        MergeGammaAdjust(src_scan[1], r, a, &dest_scan[2]);
244        MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
245        if (start_col > left)
246          MergeGammaAdjust(src_scan[-1], b, a, &dest_scan[0]);
247        pSetAlpha(dest_scan);
248        NextPixel(&src_scan, &dest_scan, Bpp);
249        for (int col = start_col + 1; col < end_col - 1; col++) {
250          MergeGammaAdjustBgr(&src_scan[-1], r, g, b, a, &dest_scan[0]);
251          pSetAlpha(dest_scan);
252          NextPixel(&src_scan, &dest_scan, Bpp);
253        }
254      } else {
255        MergeGammaAdjust(src_scan[0], r, a, &dest_scan[2]);
256        if (start_col > left) {
257          MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
258          MergeGammaAdjust(src_scan[-2], b, a, &dest_scan[0]);
259        }
260        pSetAlpha(dest_scan);
261        NextPixel(&src_scan, &dest_scan, Bpp);
262        for (int col = start_col + 1; col < end_col - 1; col++) {
263          MergeGammaAdjustBgr(&src_scan[-2], r, g, b, a, &dest_scan[0]);
264          pSetAlpha(dest_scan);
265          NextPixel(&src_scan, &dest_scan, Bpp);
266        }
267      }
268    } else {
269      if (x_subpixel == 0) {
270        for (int col = start_col; col < end_col; col++) {
271          if (bNormal) {
272            int src_value = AverageRgb(&src_scan[0]);
273            pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
274          } else {
275            MergeGammaAdjustRgb(&src_scan[0], r, g, b, a, &dest_scan[0]);
276            pSetAlpha(dest_scan);
277          }
278          NextPixel(&src_scan, &dest_scan, Bpp);
279        }
280      } else if (x_subpixel == 1) {
281        if (bNormal) {
282          int src_value = start_col > left ? AverageRgb(&src_scan[-1])
283                                           : (src_scan[0] + src_scan[1]) / 3;
284          pNormalizeSrcFunc(src_value, r, g, b, a, dest_scan);
285        } else {
286          if (start_col > left)
287            MergeGammaAdjust(src_scan[-1], r, a, &dest_scan[2]);
288          MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
289          MergeGammaAdjust(src_scan[1], b, a, &dest_scan[0]);
290          pSetAlpha(dest_scan);
291        }
292        NextPixel(&src_scan, &dest_scan, Bpp);
293        for (int col = start_col + 1; col < end_col; col++) {
294          if (bNormal) {
295            int src_value = AverageRgb(&src_scan[-1]);
296            pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
297          } else {
298            MergeGammaAdjustRgb(&src_scan[-1], r, g, b, a, &dest_scan[0]);
299            pSetAlpha(dest_scan);
300          }
301          NextPixel(&src_scan, &dest_scan, Bpp);
302        }
303      } else {
304        if (bNormal) {
305          int src_value =
306              start_col > left ? AverageRgb(&src_scan[-2]) : src_scan[0] / 3;
307          pNormalizeSrcFunc(src_value, r, g, b, a, dest_scan);
308        } else {
309          if (start_col > left) {
310            MergeGammaAdjust(src_scan[-2], r, a, &dest_scan[2]);
311            MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
312          }
313          MergeGammaAdjust(src_scan[0], b, a, &dest_scan[0]);
314          pSetAlpha(dest_scan);
315        }
316        NextPixel(&src_scan, &dest_scan, Bpp);
317        for (int col = start_col + 1; col < end_col; col++) {
318          if (bNormal) {
319            int src_value = AverageRgb(&src_scan[-2]);
320            pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
321          } else {
322            MergeGammaAdjustRgb(&src_scan[-2], r, g, b, a, &dest_scan[0]);
323            pSetAlpha(dest_scan);
324          }
325          NextPixel(&src_scan, &dest_scan, Bpp);
326        }
327      }
328    }
329  }
330}
331
332bool ShouldDrawDeviceText(const CFX_Font* pFont, uint32_t text_flags) {
333#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
334  if (text_flags & FXFONT_CIDFONT)
335    return false;
336
337  const CFX_ByteString bsPsName = pFont->GetPsName();
338  if (bsPsName.Find("+ZJHL") != -1)
339    return false;
340
341  if (bsPsName == "CNAAJI+cmex10")
342    return false;
343#endif
344  return true;
345}
346
347}  // namespace
348
349FXTEXT_CHARPOS::FXTEXT_CHARPOS()
350    : m_GlyphIndex(0),
351      m_FontCharWidth(0),
352#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
353      m_ExtGID(0),
354#endif
355      m_FallbackFontPosition(0),
356      m_bGlyphAdjust(false),
357      m_bFontStyle(false) {
358}
359
360FXTEXT_CHARPOS::FXTEXT_CHARPOS(const FXTEXT_CHARPOS&) = default;
361
362FXTEXT_CHARPOS::~FXTEXT_CHARPOS(){};
363
364CFX_RenderDevice::CFX_RenderDevice()
365    : m_pBitmap(nullptr),
366      m_Width(0),
367      m_Height(0),
368      m_bpp(0),
369      m_RenderCaps(0),
370      m_DeviceClass(0) {}
371
372CFX_RenderDevice::~CFX_RenderDevice() {
373#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
374  Flush();
375#endif
376}
377
378#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
379void CFX_RenderDevice::Flush() {
380  m_pDeviceDriver.reset();
381}
382#endif
383
384void CFX_RenderDevice::SetDeviceDriver(
385    std::unique_ptr<IFX_RenderDeviceDriver> pDriver) {
386  m_pDeviceDriver = std::move(pDriver);
387  InitDeviceInfo();
388}
389
390void CFX_RenderDevice::InitDeviceInfo() {
391  m_Width = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_WIDTH);
392  m_Height = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_HEIGHT);
393  m_bpp = m_pDeviceDriver->GetDeviceCaps(FXDC_BITS_PIXEL);
394  m_RenderCaps = m_pDeviceDriver->GetDeviceCaps(FXDC_RENDER_CAPS);
395  m_DeviceClass = m_pDeviceDriver->GetDeviceCaps(FXDC_DEVICE_CLASS);
396  if (!m_pDeviceDriver->GetClipBox(&m_ClipBox)) {
397    m_ClipBox.left = 0;
398    m_ClipBox.top = 0;
399    m_ClipBox.right = m_Width;
400    m_ClipBox.bottom = m_Height;
401  }
402}
403
404void CFX_RenderDevice::SaveState() {
405  m_pDeviceDriver->SaveState();
406}
407
408void CFX_RenderDevice::RestoreState(bool bKeepSaved) {
409  m_pDeviceDriver->RestoreState(bKeepSaved);
410  UpdateClipBox();
411}
412
413int CFX_RenderDevice::GetDeviceCaps(int caps_id) const {
414  return m_pDeviceDriver->GetDeviceCaps(caps_id);
415}
416CFX_Matrix CFX_RenderDevice::GetCTM() const {
417  return m_pDeviceDriver->GetCTM();
418}
419
420bool CFX_RenderDevice::CreateCompatibleBitmap(CFX_DIBitmap* pDIB,
421                                              int width,
422                                              int height) const {
423  if (m_RenderCaps & FXRC_CMYK_OUTPUT) {
424    return pDIB->Create(width, height, m_RenderCaps & FXRC_ALPHA_OUTPUT
425                                           ? FXDIB_Cmyka
426                                           : FXDIB_Cmyk);
427  }
428  if (m_RenderCaps & FXRC_BYTEMASK_OUTPUT)
429    return pDIB->Create(width, height, FXDIB_8bppMask);
430#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || defined _SKIA_SUPPORT_PATHS_
431  return pDIB->Create(width, height, m_RenderCaps & FXRC_ALPHA_OUTPUT
432                                         ? FXDIB_Argb
433                                         : FXDIB_Rgb32);
434#else
435  return pDIB->Create(
436      width, height, m_RenderCaps & FXRC_ALPHA_OUTPUT ? FXDIB_Argb : FXDIB_Rgb);
437#endif
438}
439
440bool CFX_RenderDevice::SetClip_PathFill(const CFX_PathData* pPathData,
441                                        const CFX_Matrix* pObject2Device,
442                                        int fill_mode) {
443  if (!m_pDeviceDriver->SetClip_PathFill(pPathData, pObject2Device,
444                                         fill_mode)) {
445    return false;
446  }
447  UpdateClipBox();
448  return true;
449}
450
451bool CFX_RenderDevice::SetClip_PathStroke(
452    const CFX_PathData* pPathData,
453    const CFX_Matrix* pObject2Device,
454    const CFX_GraphStateData* pGraphState) {
455  if (!m_pDeviceDriver->SetClip_PathStroke(pPathData, pObject2Device,
456                                           pGraphState)) {
457    return false;
458  }
459  UpdateClipBox();
460  return true;
461}
462
463bool CFX_RenderDevice::SetClip_Rect(const FX_RECT& rect) {
464  CFX_PathData path;
465  path.AppendRect(rect.left, rect.bottom, rect.right, rect.top);
466  if (!SetClip_PathFill(&path, nullptr, FXFILL_WINDING))
467    return false;
468
469  UpdateClipBox();
470  return true;
471}
472
473void CFX_RenderDevice::UpdateClipBox() {
474  if (m_pDeviceDriver->GetClipBox(&m_ClipBox))
475    return;
476  m_ClipBox.left = 0;
477  m_ClipBox.top = 0;
478  m_ClipBox.right = m_Width;
479  m_ClipBox.bottom = m_Height;
480}
481
482bool CFX_RenderDevice::DrawPathWithBlend(const CFX_PathData* pPathData,
483                                         const CFX_Matrix* pObject2Device,
484                                         const CFX_GraphStateData* pGraphState,
485                                         uint32_t fill_color,
486                                         uint32_t stroke_color,
487                                         int fill_mode,
488                                         int blend_type) {
489  uint8_t stroke_alpha = pGraphState ? FXARGB_A(stroke_color) : 0;
490  uint8_t fill_alpha = (fill_mode & 3) ? FXARGB_A(fill_color) : 0;
491  const std::vector<FX_PATHPOINT>& pPoints = pPathData->GetPoints();
492  if (stroke_alpha == 0 && pPoints.size() == 2) {
493    CFX_PointF pos1 = pPoints[0].m_Point;
494    CFX_PointF pos2 = pPoints[1].m_Point;
495    if (pObject2Device) {
496      pos1 = pObject2Device->Transform(pos1);
497      pos2 = pObject2Device->Transform(pos2);
498    }
499    DrawCosmeticLine(pos1.x, pos1.y, pos2.x, pos2.y, fill_color, fill_mode,
500                     blend_type);
501    return true;
502  }
503
504  if ((pPoints.size() == 5 || pPoints.size() == 4) && stroke_alpha == 0) {
505    CFX_FloatRect rect_f;
506    if (!(fill_mode & FXFILL_RECT_AA) &&
507        pPathData->IsRect(pObject2Device, &rect_f)) {
508      FX_RECT rect_i = rect_f.GetOuterRect();
509
510      // Depending on the top/bottom, left/right values of the rect it's
511      // possible to overflow the Width() and Height() calculations. Check that
512      // the rect will have valid dimension before continuing.
513      if (!rect_i.Valid())
514        return false;
515
516      int width = (int)FXSYS_ceil(rect_f.right - rect_f.left);
517      if (width < 1) {
518        width = 1;
519        if (rect_i.left == rect_i.right)
520          rect_i.right++;
521      }
522      int height = (int)FXSYS_ceil(rect_f.top - rect_f.bottom);
523      if (height < 1) {
524        height = 1;
525        if (rect_i.bottom == rect_i.top)
526          rect_i.bottom++;
527      }
528      if (rect_i.Width() >= width + 1) {
529        if (rect_f.left - (FX_FLOAT)(rect_i.left) >
530            (FX_FLOAT)(rect_i.right) - rect_f.right) {
531          rect_i.left++;
532        } else {
533          rect_i.right--;
534        }
535      }
536      if (rect_i.Height() >= height + 1) {
537        if (rect_f.top - (FX_FLOAT)(rect_i.top) >
538            (FX_FLOAT)(rect_i.bottom) - rect_f.bottom) {
539          rect_i.top++;
540        } else {
541          rect_i.bottom--;
542        }
543      }
544      if (FillRectWithBlend(&rect_i, fill_color, blend_type))
545        return true;
546    }
547  }
548  if ((fill_mode & 3) && stroke_alpha == 0 && !(fill_mode & FX_FILL_STROKE) &&
549      !(fill_mode & FX_FILL_TEXT_MODE)) {
550    CFX_PathData newPath;
551    bool bThin = false;
552    bool setIdentity = false;
553    if (pPathData->GetZeroAreaPath(pObject2Device,
554                                   !!m_pDeviceDriver->GetDriverType(), &newPath,
555                                   &bThin, &setIdentity)) {
556      CFX_GraphStateData graphState;
557      graphState.m_LineWidth = 0.0f;
558
559      uint32_t strokecolor = fill_color;
560      if (bThin)
561        strokecolor = (((fill_alpha >> 2) << 24) | (strokecolor & 0x00ffffff));
562
563      const CFX_Matrix* pMatrix = nullptr;
564      if (pObject2Device && !pObject2Device->IsIdentity() && !setIdentity)
565        pMatrix = pObject2Device;
566
567      int smooth_path = FX_ZEROAREA_FILL;
568      if (fill_mode & FXFILL_NOPATHSMOOTH)
569        smooth_path |= FXFILL_NOPATHSMOOTH;
570
571      m_pDeviceDriver->DrawPath(&newPath, pMatrix, &graphState, 0, strokecolor,
572                                smooth_path, blend_type);
573    }
574  }
575  if ((fill_mode & 3) && fill_alpha && stroke_alpha < 0xff &&
576      (fill_mode & FX_FILL_STROKE)) {
577    if (m_RenderCaps & FXRC_FILLSTROKE_PATH) {
578      return m_pDeviceDriver->DrawPath(pPathData, pObject2Device, pGraphState,
579                                       fill_color, stroke_color, fill_mode,
580                                       blend_type);
581    }
582    return DrawFillStrokePath(pPathData, pObject2Device, pGraphState,
583                              fill_color, stroke_color, fill_mode, blend_type);
584  }
585  return m_pDeviceDriver->DrawPath(pPathData, pObject2Device, pGraphState,
586                                   fill_color, stroke_color, fill_mode,
587                                   blend_type);
588}
589
590// This can be removed once PDFium entirely relies on Skia
591bool CFX_RenderDevice::DrawFillStrokePath(const CFX_PathData* pPathData,
592                                          const CFX_Matrix* pObject2Device,
593                                          const CFX_GraphStateData* pGraphState,
594                                          uint32_t fill_color,
595                                          uint32_t stroke_color,
596                                          int fill_mode,
597                                          int blend_type) {
598  if (!(m_RenderCaps & FXRC_GET_BITS))
599    return false;
600  CFX_FloatRect bbox;
601  if (pGraphState) {
602    bbox = pPathData->GetBoundingBox(pGraphState->m_LineWidth,
603                                     pGraphState->m_MiterLimit);
604  } else {
605    bbox = pPathData->GetBoundingBox();
606  }
607  if (pObject2Device)
608    pObject2Device->TransformRect(bbox);
609
610  CFX_Matrix ctm = GetCTM();
611  FX_FLOAT fScaleX = FXSYS_fabs(ctm.a);
612  FX_FLOAT fScaleY = FXSYS_fabs(ctm.d);
613  FX_RECT rect = bbox.GetOuterRect();
614  CFX_DIBitmap bitmap, Backdrop;
615  if (!CreateCompatibleBitmap(&bitmap, FXSYS_round(rect.Width() * fScaleX),
616                              FXSYS_round(rect.Height() * fScaleY))) {
617    return false;
618  }
619  if (bitmap.HasAlpha()) {
620    bitmap.Clear(0);
621    Backdrop.Copy(&bitmap);
622  } else {
623    if (!m_pDeviceDriver->GetDIBits(&bitmap, rect.left, rect.top))
624      return false;
625    Backdrop.Copy(&bitmap);
626  }
627  CFX_FxgeDevice bitmap_device;
628  bitmap_device.Attach(&bitmap, false, &Backdrop, true);
629  CFX_Matrix matrix;
630  if (pObject2Device)
631    matrix = *pObject2Device;
632  matrix.Translate(-rect.left, -rect.top);
633  matrix.Concat(CFX_Matrix(fScaleX, 0, 0, fScaleY, 0, 0));
634  if (!bitmap_device.GetDeviceDriver()->DrawPath(
635          pPathData, &matrix, pGraphState, fill_color, stroke_color, fill_mode,
636          blend_type)) {
637    return false;
638  }
639#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
640  bitmap_device.GetDeviceDriver()->Flush();
641#endif
642  FX_RECT src_rect(0, 0, FXSYS_round(rect.Width() * fScaleX),
643                   FXSYS_round(rect.Height() * fScaleY));
644  return m_pDeviceDriver->SetDIBits(&bitmap, 0, &src_rect, rect.left, rect.top,
645                                    FXDIB_BLEND_NORMAL);
646}
647
648bool CFX_RenderDevice::SetPixel(int x, int y, uint32_t color) {
649  if (m_pDeviceDriver->SetPixel(x, y, color))
650    return true;
651
652  FX_RECT rect(x, y, x + 1, y + 1);
653  return FillRectWithBlend(&rect, color, FXDIB_BLEND_NORMAL);
654}
655
656bool CFX_RenderDevice::FillRectWithBlend(const FX_RECT* pRect,
657                                         uint32_t fill_color,
658                                         int blend_type) {
659  if (m_pDeviceDriver->FillRectWithBlend(pRect, fill_color, blend_type))
660    return true;
661
662  if (!(m_RenderCaps & FXRC_GET_BITS))
663    return false;
664
665  CFX_DIBitmap bitmap;
666  if (!CreateCompatibleBitmap(&bitmap, pRect->Width(), pRect->Height()))
667    return false;
668
669  if (!m_pDeviceDriver->GetDIBits(&bitmap, pRect->left, pRect->top))
670    return false;
671
672  if (!bitmap.CompositeRect(0, 0, pRect->Width(), pRect->Height(), fill_color,
673                            0, nullptr)) {
674    return false;
675  }
676  FX_RECT src_rect(0, 0, pRect->Width(), pRect->Height());
677  m_pDeviceDriver->SetDIBits(&bitmap, 0, &src_rect, pRect->left, pRect->top,
678                             FXDIB_BLEND_NORMAL);
679  return true;
680}
681
682bool CFX_RenderDevice::DrawCosmeticLine(FX_FLOAT x1,
683                                        FX_FLOAT y1,
684                                        FX_FLOAT x2,
685                                        FX_FLOAT y2,
686                                        uint32_t color,
687                                        int fill_mode,
688                                        int blend_type) {
689  if ((color >= 0xff000000) &&
690      m_pDeviceDriver->DrawCosmeticLine(x1, y1, x2, y2, color, blend_type)) {
691    return true;
692  }
693  CFX_GraphStateData graph_state;
694  CFX_PathData path;
695  path.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::MoveTo, false);
696  path.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::LineTo, false);
697  return m_pDeviceDriver->DrawPath(&path, nullptr, &graph_state, 0, color,
698                                   fill_mode, blend_type);
699}
700
701bool CFX_RenderDevice::GetDIBits(CFX_DIBitmap* pBitmap, int left, int top) {
702  if (!(m_RenderCaps & FXRC_GET_BITS))
703    return false;
704  return m_pDeviceDriver->GetDIBits(pBitmap, left, top);
705}
706
707CFX_DIBitmap* CFX_RenderDevice::GetBackDrop() {
708  return m_pDeviceDriver->GetBackDrop();
709}
710
711bool CFX_RenderDevice::SetDIBitsWithBlend(const CFX_DIBSource* pBitmap,
712                                          int left,
713                                          int top,
714                                          int blend_mode) {
715  ASSERT(!pBitmap->IsAlphaMask());
716  CFX_Matrix ctm = GetCTM();
717  FX_FLOAT fScaleX = FXSYS_fabs(ctm.a);
718  FX_FLOAT fScaleY = FXSYS_fabs(ctm.d);
719  FX_RECT dest_rect(left, top,
720                    FXSYS_round(left + pBitmap->GetWidth() / fScaleX),
721                    FXSYS_round(top + pBitmap->GetHeight() / fScaleY));
722  dest_rect.Intersect(m_ClipBox);
723  if (dest_rect.IsEmpty())
724    return true;
725  FX_RECT src_rect(dest_rect.left - left, dest_rect.top - top,
726                   dest_rect.left - left + dest_rect.Width(),
727                   dest_rect.top - top + dest_rect.Height());
728  src_rect.left = FXSYS_round(src_rect.left * fScaleX);
729  src_rect.top = FXSYS_round(src_rect.top * fScaleY);
730  src_rect.right = FXSYS_round(src_rect.right * fScaleX);
731  src_rect.bottom = FXSYS_round(src_rect.bottom * fScaleY);
732  if ((blend_mode != FXDIB_BLEND_NORMAL && !(m_RenderCaps & FXRC_BLEND_MODE)) ||
733      (pBitmap->HasAlpha() && !(m_RenderCaps & FXRC_ALPHA_IMAGE))) {
734    if (!(m_RenderCaps & FXRC_GET_BITS))
735      return false;
736    int bg_pixel_width = FXSYS_round(dest_rect.Width() * fScaleX);
737    int bg_pixel_height = FXSYS_round(dest_rect.Height() * fScaleY);
738    CFX_DIBitmap background;
739    if (!background.Create(
740            bg_pixel_width, bg_pixel_height,
741            (m_RenderCaps & FXRC_CMYK_OUTPUT) ? FXDIB_Cmyk : FXDIB_Rgb32)) {
742      return false;
743    }
744    if (!m_pDeviceDriver->GetDIBits(&background, dest_rect.left,
745                                    dest_rect.top)) {
746      return false;
747    }
748    if (!background.CompositeBitmap(0, 0, bg_pixel_width, bg_pixel_height,
749                                    pBitmap, src_rect.left, src_rect.top,
750                                    blend_mode, nullptr, false, nullptr)) {
751      return false;
752    }
753    FX_RECT rect(0, 0, bg_pixel_width, bg_pixel_height);
754    return m_pDeviceDriver->SetDIBits(&background, 0, &rect, dest_rect.left,
755                                      dest_rect.top, FXDIB_BLEND_NORMAL);
756  }
757  return m_pDeviceDriver->SetDIBits(pBitmap, 0, &src_rect, dest_rect.left,
758                                    dest_rect.top, blend_mode);
759}
760
761bool CFX_RenderDevice::StretchDIBitsWithFlagsAndBlend(
762    const CFX_DIBSource* pBitmap,
763    int left,
764    int top,
765    int dest_width,
766    int dest_height,
767    uint32_t flags,
768    int blend_mode) {
769  FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
770  FX_RECT clip_box = m_ClipBox;
771  clip_box.Intersect(dest_rect);
772  if (clip_box.IsEmpty())
773    return true;
774  return m_pDeviceDriver->StretchDIBits(pBitmap, 0, left, top, dest_width,
775                                        dest_height, &clip_box, flags,
776                                        blend_mode);
777}
778
779bool CFX_RenderDevice::SetBitMask(const CFX_DIBSource* pBitmap,
780                                  int left,
781                                  int top,
782                                  uint32_t argb) {
783  FX_RECT src_rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
784  return m_pDeviceDriver->SetDIBits(pBitmap, argb, &src_rect, left, top,
785                                    FXDIB_BLEND_NORMAL);
786}
787
788bool CFX_RenderDevice::StretchBitMask(const CFX_DIBSource* pBitmap,
789                                      int left,
790                                      int top,
791                                      int dest_width,
792                                      int dest_height,
793                                      uint32_t color) {
794  return StretchBitMaskWithFlags(pBitmap, left, top, dest_width, dest_height,
795                                 color, 0);
796}
797
798bool CFX_RenderDevice::StretchBitMaskWithFlags(const CFX_DIBSource* pBitmap,
799                                               int left,
800                                               int top,
801                                               int dest_width,
802                                               int dest_height,
803                                               uint32_t argb,
804                                               uint32_t flags) {
805  FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
806  FX_RECT clip_box = m_ClipBox;
807  clip_box.Intersect(dest_rect);
808  return m_pDeviceDriver->StretchDIBits(pBitmap, argb, left, top, dest_width,
809                                        dest_height, &clip_box, flags,
810                                        FXDIB_BLEND_NORMAL);
811}
812
813bool CFX_RenderDevice::StartDIBitsWithBlend(const CFX_DIBSource* pBitmap,
814                                            int bitmap_alpha,
815                                            uint32_t argb,
816                                            const CFX_Matrix* pMatrix,
817                                            uint32_t flags,
818                                            void*& handle,
819                                            int blend_mode) {
820  return m_pDeviceDriver->StartDIBits(pBitmap, bitmap_alpha, argb, pMatrix,
821                                      flags, handle, blend_mode);
822}
823
824bool CFX_RenderDevice::ContinueDIBits(void* handle, IFX_Pause* pPause) {
825  return m_pDeviceDriver->ContinueDIBits(handle, pPause);
826}
827
828void CFX_RenderDevice::CancelDIBits(void* handle) {
829  m_pDeviceDriver->CancelDIBits(handle);
830}
831
832#ifdef _SKIA_SUPPORT_
833void CFX_RenderDevice::DebugVerifyBitmapIsPreMultiplied() const {
834  SkASSERT(0);
835}
836
837bool CFX_RenderDevice::SetBitsWithMask(const CFX_DIBSource* pBitmap,
838                                       const CFX_DIBSource* pMask,
839                                       int left,
840                                       int top,
841                                       int bitmap_alpha,
842                                       int blend_type) {
843  return m_pDeviceDriver->SetBitsWithMask(pBitmap, pMask, left, top,
844                                          bitmap_alpha, blend_type);
845}
846#endif
847
848bool CFX_RenderDevice::DrawNormalText(int nChars,
849                                      const FXTEXT_CHARPOS* pCharPos,
850                                      CFX_Font* pFont,
851                                      FX_FLOAT font_size,
852                                      const CFX_Matrix* pText2Device,
853                                      uint32_t fill_color,
854                                      uint32_t text_flags) {
855  int nativetext_flags = text_flags;
856  if (m_DeviceClass != FXDC_DISPLAY) {
857    if (!(text_flags & FXTEXT_PRINTGRAPHICTEXT)) {
858      if (ShouldDrawDeviceText(pFont, text_flags) &&
859          m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pText2Device,
860                                          font_size, fill_color)) {
861        return true;
862      }
863    }
864    if (FXARGB_A(fill_color) < 255)
865      return false;
866  } else if (!(text_flags & FXTEXT_NO_NATIVETEXT)) {
867    if (ShouldDrawDeviceText(pFont, text_flags) &&
868        m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pText2Device,
869                                        font_size, fill_color)) {
870      return true;
871    }
872  }
873  CFX_Matrix char2device;
874  CFX_Matrix text2Device;
875  if (pText2Device) {
876    char2device = *pText2Device;
877    text2Device = *pText2Device;
878  }
879
880  char2device.Scale(font_size, -font_size);
881  if (FXSYS_fabs(char2device.a) + FXSYS_fabs(char2device.b) > 50 * 1.0f ||
882      ((m_DeviceClass == FXDC_PRINTER) &&
883       !(text_flags & FXTEXT_PRINTIMAGETEXT))) {
884    if (pFont->GetFace()) {
885      int nPathFlags =
886          (text_flags & FXTEXT_NOSMOOTH) == 0 ? 0 : FXFILL_NOPATHSMOOTH;
887      return DrawTextPath(nChars, pCharPos, pFont, font_size, pText2Device,
888                          nullptr, nullptr, fill_color, 0, nullptr, nPathFlags);
889    }
890  }
891  int anti_alias = FXFT_RENDER_MODE_MONO;
892  bool bNormal = false;
893  if ((text_flags & FXTEXT_NOSMOOTH) == 0) {
894    if (m_DeviceClass == FXDC_DISPLAY && m_bpp > 1) {
895      if (!CFX_GEModule::Get()->GetFontMgr()->FTLibrarySupportsHinting()) {
896        // Some Freetype implementations (like the one packaged with Fedora) do
897        // not support hinting due to patents 6219025, 6239783, 6307566,
898        // 6225973, 6243070, 6393145, 6421054, 6282327, and 6624828; the latest
899        // one expires 10/7/19.  This makes LCD antialiasing very ugly, so we
900        // instead fall back on NORMAL antialiasing.
901        anti_alias = FXFT_RENDER_MODE_NORMAL;
902      } else if ((m_RenderCaps & (FXRC_ALPHA_OUTPUT | FXRC_CMYK_OUTPUT))) {
903        anti_alias = FXFT_RENDER_MODE_LCD;
904        bNormal = true;
905      } else if (m_bpp < 16) {
906        anti_alias = FXFT_RENDER_MODE_NORMAL;
907      } else {
908        anti_alias = FXFT_RENDER_MODE_LCD;
909
910        bool bClearType = false;
911        if (pFont->GetFace())
912          bClearType = !!(text_flags & FXTEXT_CLEARTYPE);
913        bNormal = !bClearType;
914      }
915    }
916  }
917  std::vector<FXTEXT_GLYPHPOS> glyphs(nChars);
918  CFX_Matrix matrixCTM = GetCTM();
919  FX_FLOAT scale_x = FXSYS_fabs(matrixCTM.a);
920  FX_FLOAT scale_y = FXSYS_fabs(matrixCTM.d);
921  CFX_Matrix deviceCtm = char2device;
922  CFX_Matrix m(scale_x, 0, 0, scale_y, 0, 0);
923  deviceCtm.Concat(m);
924  text2Device.Concat(m);
925
926  for (size_t i = 0; i < glyphs.size(); ++i) {
927    FXTEXT_GLYPHPOS& glyph = glyphs[i];
928    const FXTEXT_CHARPOS& charpos = pCharPos[i];
929
930    glyph.m_fOrigin = text2Device.Transform(charpos.m_Origin);
931    if (anti_alias < FXFT_RENDER_MODE_LCD)
932      glyph.m_Origin.x = FXSYS_round(glyph.m_fOrigin.x);
933    else
934      glyph.m_Origin.x = static_cast<int>(FXSYS_floor(glyph.m_fOrigin.x));
935    glyph.m_Origin.y = FXSYS_round(glyph.m_fOrigin.y);
936
937    if (charpos.m_bGlyphAdjust) {
938      CFX_Matrix new_matrix(
939          charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
940          charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
941      new_matrix.Concat(deviceCtm);
942      glyph.m_pGlyph = pFont->LoadGlyphBitmap(
943          charpos.m_GlyphIndex, charpos.m_bFontStyle, &new_matrix,
944          charpos.m_FontCharWidth, anti_alias, nativetext_flags);
945    } else {
946      glyph.m_pGlyph = pFont->LoadGlyphBitmap(
947          charpos.m_GlyphIndex, charpos.m_bFontStyle, &deviceCtm,
948          charpos.m_FontCharWidth, anti_alias, nativetext_flags);
949    }
950  }
951  if (anti_alias < FXFT_RENDER_MODE_LCD && glyphs.size() > 1)
952    AdjustGlyphSpace(&glyphs);
953
954  FX_RECT bmp_rect1 = FXGE_GetGlyphsBBox(glyphs, anti_alias);
955  if (scale_x > 1 && scale_y > 1) {
956    bmp_rect1.left--;
957    bmp_rect1.top--;
958    bmp_rect1.right++;
959    bmp_rect1.bottom++;
960  }
961  FX_RECT bmp_rect(FXSYS_round((FX_FLOAT)(bmp_rect1.left) / scale_x),
962                   FXSYS_round((FX_FLOAT)(bmp_rect1.top) / scale_y),
963                   FXSYS_round((FX_FLOAT)bmp_rect1.right / scale_x),
964                   FXSYS_round((FX_FLOAT)bmp_rect1.bottom / scale_y));
965  bmp_rect.Intersect(m_ClipBox);
966  if (bmp_rect.IsEmpty())
967    return true;
968  int pixel_width = FXSYS_round(bmp_rect.Width() * scale_x);
969  int pixel_height = FXSYS_round(bmp_rect.Height() * scale_y);
970  int pixel_left = FXSYS_round(bmp_rect.left * scale_x);
971  int pixel_top = FXSYS_round(bmp_rect.top * scale_y);
972  if (anti_alias == FXFT_RENDER_MODE_MONO) {
973    CFX_DIBitmap bitmap;
974    if (!bitmap.Create(pixel_width, pixel_height, FXDIB_1bppMask))
975      return false;
976    bitmap.Clear(0);
977    for (const FXTEXT_GLYPHPOS& glyph : glyphs) {
978      if (!glyph.m_pGlyph)
979        continue;
980      const CFX_DIBitmap* pGlyph = &glyph.m_pGlyph->m_Bitmap;
981      bitmap.TransferBitmap(
982          glyph.m_Origin.x + glyph.m_pGlyph->m_Left - pixel_left,
983          glyph.m_Origin.y - glyph.m_pGlyph->m_Top - pixel_top,
984          pGlyph->GetWidth(), pGlyph->GetHeight(), pGlyph, 0, 0);
985    }
986    return SetBitMask(&bitmap, bmp_rect.left, bmp_rect.top, fill_color);
987  }
988  CFX_DIBitmap bitmap;
989  if (m_bpp == 8) {
990    if (!bitmap.Create(pixel_width, pixel_height, FXDIB_8bppMask))
991      return false;
992  } else {
993    if (!CreateCompatibleBitmap(&bitmap, pixel_width, pixel_height))
994      return false;
995  }
996  if (!bitmap.HasAlpha() && !bitmap.IsAlphaMask()) {
997    bitmap.Clear(0xFFFFFFFF);
998    if (!GetDIBits(&bitmap, bmp_rect.left, bmp_rect.top))
999      return false;
1000  } else {
1001    bitmap.Clear(0);
1002    if (bitmap.m_pAlphaMask)
1003      bitmap.m_pAlphaMask->Clear(0);
1004  }
1005  int dest_width = pixel_width;
1006  int a = 0;
1007  int r = 0;
1008  int g = 0;
1009  int b = 0;
1010  if (anti_alias == FXFT_RENDER_MODE_LCD)
1011    ArgbDecode(fill_color, a, r, g, b);
1012
1013  for (const FXTEXT_GLYPHPOS& glyph : glyphs) {
1014    if (!glyph.m_pGlyph)
1015      continue;
1016
1017    pdfium::base::CheckedNumeric<int> left = glyph.m_Origin.x;
1018    left += glyph.m_pGlyph->m_Left;
1019    left -= pixel_left;
1020    if (!left.IsValid())
1021      return false;
1022
1023    pdfium::base::CheckedNumeric<int> top = glyph.m_Origin.y;
1024    top -= glyph.m_pGlyph->m_Top;
1025    top -= pixel_top;
1026    if (!top.IsValid())
1027      return false;
1028
1029    const CFX_DIBitmap* pGlyph = &glyph.m_pGlyph->m_Bitmap;
1030    int ncols = pGlyph->GetWidth();
1031    int nrows = pGlyph->GetHeight();
1032    if (anti_alias == FXFT_RENDER_MODE_NORMAL) {
1033      if (!bitmap.CompositeMask(left.ValueOrDie(), top.ValueOrDie(), ncols,
1034                                nrows, pGlyph, fill_color, 0, 0,
1035                                FXDIB_BLEND_NORMAL, nullptr, false, 0,
1036                                nullptr)) {
1037        return false;
1038      }
1039      continue;
1040    }
1041    bool bBGRStripe = !!(text_flags & FXTEXT_BGR_STRIPE);
1042    ncols /= 3;
1043    int x_subpixel = static_cast<int>(glyph.m_fOrigin.x * 3) % 3;
1044    int start_col =
1045        pdfium::base::ValueOrDieForType<int>(pdfium::base::CheckMax(left, 0));
1046    pdfium::base::CheckedNumeric<int> end_col_safe = left;
1047    end_col_safe += ncols;
1048    if (!end_col_safe.IsValid())
1049      return false;
1050
1051    int end_col =
1052        std::min(static_cast<int>(end_col_safe.ValueOrDie<int>()), dest_width);
1053    if (start_col >= end_col)
1054      continue;
1055
1056    DrawNormalTextHelper(&bitmap, pGlyph, nrows, left.ValueOrDie(),
1057                         top.ValueOrDie(), start_col, end_col, bNormal,
1058                         bBGRStripe, x_subpixel, a, r, g, b);
1059  }
1060  if (bitmap.IsAlphaMask())
1061    SetBitMask(&bitmap, bmp_rect.left, bmp_rect.top, fill_color);
1062  else
1063    SetDIBits(&bitmap, bmp_rect.left, bmp_rect.top);
1064  return true;
1065}
1066
1067bool CFX_RenderDevice::DrawTextPath(int nChars,
1068                                    const FXTEXT_CHARPOS* pCharPos,
1069                                    CFX_Font* pFont,
1070                                    FX_FLOAT font_size,
1071                                    const CFX_Matrix* pText2User,
1072                                    const CFX_Matrix* pUser2Device,
1073                                    const CFX_GraphStateData* pGraphState,
1074                                    uint32_t fill_color,
1075                                    FX_ARGB stroke_color,
1076                                    CFX_PathData* pClippingPath,
1077                                    int nFlag) {
1078  for (int iChar = 0; iChar < nChars; iChar++) {
1079    const FXTEXT_CHARPOS& charpos = pCharPos[iChar];
1080    CFX_Matrix matrix;
1081    if (charpos.m_bGlyphAdjust) {
1082      matrix = CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
1083                          charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3],
1084                          0, 0);
1085    }
1086    matrix.Concat(CFX_Matrix(font_size, 0, 0, font_size, charpos.m_Origin.x,
1087                             charpos.m_Origin.y));
1088    const CFX_PathData* pPath =
1089        pFont->LoadGlyphPath(charpos.m_GlyphIndex, charpos.m_FontCharWidth);
1090    if (!pPath)
1091      continue;
1092
1093    matrix.Concat(*pText2User);
1094
1095    CFX_PathData TransformedPath(*pPath);
1096    TransformedPath.Transform(&matrix);
1097    if (fill_color || stroke_color) {
1098      int fill_mode = nFlag;
1099      if (fill_color)
1100        fill_mode |= FXFILL_WINDING;
1101      fill_mode |= FX_FILL_TEXT_MODE;
1102      if (!DrawPathWithBlend(&TransformedPath, pUser2Device, pGraphState,
1103                             fill_color, stroke_color, fill_mode,
1104                             FXDIB_BLEND_NORMAL)) {
1105        return false;
1106      }
1107    }
1108    if (pClippingPath)
1109      pClippingPath->Append(&TransformedPath, pUser2Device);
1110  }
1111  return true;
1112}
1113