Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_renderdevice.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxge/cfx_renderdevice.h"
8
9#include <math.h>
10
11#include <algorithm>
12#include <memory>
13#include <utility>
14
15#include "build/build_config.h"
16#include "core/fxcrt/fx_safe_types.h"
17#include "core/fxge/cfx_color.h"
18#include "core/fxge/cfx_defaultrenderdevice.h"
19#include "core/fxge/cfx_fillrenderoptions.h"
20#include "core/fxge/cfx_font.h"
21#include "core/fxge/cfx_fontmgr.h"
22#include "core/fxge/cfx_gemodule.h"
23#include "core/fxge/cfx_glyphbitmap.h"
24#include "core/fxge/cfx_glyphcache.h"
25#include "core/fxge/cfx_graphstatedata.h"
26#include "core/fxge/cfx_path.h"
27#include "core/fxge/cfx_textrenderoptions.h"
28#include "core/fxge/dib/cfx_dibitmap.h"
29#include "core/fxge/dib/cfx_imagerenderer.h"
30#include "core/fxge/fx_font.h"
31#include "core/fxge/renderdevicedriver_iface.h"
32#include "core/fxge/text_char_pos.h"
33#include "core/fxge/text_glyph_pos.h"
34#include "third_party/base/check.h"
35#include "third_party/base/check_op.h"
36#include "third_party/base/containers/span.h"
37
38#if defined(PDF_USE_SKIA)
39#include "third_party/skia/include/core/SkTypes.h" // nogncheck
40#endif
41
42namespace {
43
44void AdjustGlyphSpace(std::vector<TextGlyphPos>* pGlyphAndPos) {
45 DCHECK_GT(pGlyphAndPos->size(), 1u);
46 std::vector<TextGlyphPos>& glyphs = *pGlyphAndPos;
47 bool bVertical = glyphs.back().m_Origin.x == glyphs.front().m_Origin.x;
48 if (!bVertical && (glyphs.back().m_Origin.y != glyphs.front().m_Origin.y))
49 return;
50
51 for (size_t i = glyphs.size() - 1; i > 1; --i) {
52 const TextGlyphPos& next = glyphs[i];
53 int next_origin = bVertical ? next.m_Origin.y : next.m_Origin.x;
54 float next_origin_f =
55 bVertical ? next.m_fDeviceOrigin.y : next.m_fDeviceOrigin.x;
56
57 TextGlyphPos& current = glyphs[i - 1];
58 int& current_origin = bVertical ? current.m_Origin.y : current.m_Origin.x;
59 float current_origin_f =
60 bVertical ? current.m_fDeviceOrigin.y : current.m_fDeviceOrigin.x;
61
62 FX_SAFE_INT32 safe_space = next_origin;
63 safe_space -= current_origin;
64 if (!safe_space.IsValid())
65 continue;
66
67 int space = safe_space.ValueOrDie();
68 float space_f = next_origin_f - current_origin_f;
69 float error = fabs(space_f) - fabs(static_cast<float>(space));
70 if (error <= 0.5f)
71 continue;
72
73 FX_SAFE_INT32 safe_origin = current_origin;
74 safe_origin += space > 0 ? -1 : 1;
75 if (!safe_origin.IsValid())
76 continue;
77
78 current_origin = safe_origin.ValueOrDie();
79 }
80}
81
82constexpr uint8_t kTextGammaAdjust[256] = {
83 0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 18,
84 19, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35,
85 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52,
86 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
87 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
88 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
89 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
90 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
91 129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
92 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 156,
93 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
94 172, 173, 174, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
95 186, 187, 188, 189, 190, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
96 200, 201, 202, 203, 204, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
97 214, 215, 216, 217, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
98 228, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 239, 240,
99 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 250, 251, 252, 253, 254,
100 255,
101};
102
103int TextGammaAdjust(int value) {
104 DCHECK_GE(value, 0);
105 DCHECK_LE(value, 255);
106 return kTextGammaAdjust[value];
107}
108
109int CalcAlpha(int src, int alpha) {
110 return src * alpha / 255;
111}
112
113void MergeGammaAdjust(uint8_t src, int channel, int alpha, uint8_t* dest) {
114 *dest =
115 FXDIB_ALPHA_MERGE(*dest, channel, CalcAlpha(TextGammaAdjust(src), alpha));
116}
117
118void MergeGammaAdjustRgb(const uint8_t* src,
119 int r,
120 int g,
121 int b,
122 int a,
123 uint8_t* dest) {
124 MergeGammaAdjust(src[2], b, a, &dest[0]);
125 MergeGammaAdjust(src[1], g, a, &dest[1]);
126 MergeGammaAdjust(src[0], r, a, &dest[2]);
127}
128
129int AverageRgb(const uint8_t* src) {
130 return (src[0] + src[1] + src[2]) / 3;
131}
132
133uint8_t CalculateDestAlpha(uint8_t back_alpha, int src_alpha) {
134 return back_alpha + src_alpha - back_alpha * src_alpha / 255;
135}
136
137void ApplyAlpha(uint8_t* dest, int b, int g, int r, int alpha) {
138 dest[0] = FXDIB_ALPHA_MERGE(dest[0], b, alpha);
139 dest[1] = FXDIB_ALPHA_MERGE(dest[1], g, alpha);
140 dest[2] = FXDIB_ALPHA_MERGE(dest[2], r, alpha);
141}
142
143void ApplyDestAlpha(uint8_t back_alpha,
144 int src_alpha,
145 int r,
146 int g,
147 int b,
148 uint8_t* dest) {
149 uint8_t dest_alpha = CalculateDestAlpha(back_alpha, src_alpha);
150 ApplyAlpha(dest, b, g, r, src_alpha * 255 / dest_alpha);
151 dest[3] = dest_alpha;
152}
153
154void NormalizeArgb(int src_value,
155 int r,
156 int g,
157 int b,
158 int a,
159 uint8_t* dest,
160 int src_alpha) {
161 uint8_t back_alpha = dest[3];
162 if (back_alpha == 0)
163 FXARGB_SETDIB(dest, ArgbEncode(src_alpha, r, g, b));
164 else if (src_alpha != 0)
165 ApplyDestAlpha(back_alpha, src_alpha, r, g, b, dest);
166}
167
168void NormalizeDest(bool has_alpha,
169 int src_value,
170 int r,
171 int g,
172 int b,
173 int a,
174 uint8_t* dest) {
175 if (has_alpha) {
176 NormalizeArgb(src_value, r, g, b, a, dest,
177 CalcAlpha(TextGammaAdjust(src_value), a));
178 return;
179 }
180 int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
181 if (src_alpha == 0)
182 return;
183
184 ApplyAlpha(dest, b, g, r, src_alpha);
185}
186
187void NormalizeSrc(bool has_alpha,
188 int src_value,
189 int r,
190 int g,
191 int b,
192 int a,
193 uint8_t* dest) {
194 if (!has_alpha) {
195 ApplyAlpha(dest, b, g, r, CalcAlpha(TextGammaAdjust(src_value), a));
196 return;
197 }
198 int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
199 if (src_alpha != 0)
200 NormalizeArgb(src_value, r, g, b, a, dest, src_alpha);
201}
202
203void NextPixel(const uint8_t** src_scan, uint8_t** dst_scan, int bpp) {
204 *src_scan += 3;
205 *dst_scan += bpp;
206}
207
208void SetAlpha(bool has_alpha, uint8_t* alpha) {
209 if (has_alpha)
210 alpha[3] = 255;
211}
212
213void DrawNormalTextHelper(const RetainPtr<CFX_DIBitmap>& bitmap,
214 const RetainPtr<CFX_DIBitmap>& pGlyph,
215 int nrows,
216 int left,
217 int top,
218 int start_col,
219 int end_col,
220 bool normalize,
221 int x_subpixel,
222 int a,
223 int r,
224 int g,
225 int b) {
226 const bool has_alpha = bitmap->GetFormat() == FXDIB_Format::kArgb;
227 const int Bpp = has_alpha ? 4 : bitmap->GetBPP() / 8;
228 for (int row = 0; row < nrows; ++row) {
229 int dest_row = row + top;
230 if (dest_row < 0 || dest_row >= bitmap->GetHeight())
231 continue;
232
233 const uint8_t* src_scan =
234 pGlyph->GetScanline(row).subspan((start_col - left) * 3).data();
235 uint8_t* dest_scan =
236 bitmap->GetWritableScanline(dest_row).subspan(start_col * Bpp).data();
237 if (x_subpixel == 0) {
238 for (int col = start_col; col < end_col; ++col) {
239 if (normalize) {
240 int src_value = AverageRgb(&src_scan[0]);
241 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
242 } else {
243 MergeGammaAdjustRgb(&src_scan[0], r, g, b, a, &dest_scan[0]);
244 SetAlpha(has_alpha, dest_scan);
245 }
246 NextPixel(&src_scan, &dest_scan, Bpp);
247 }
248 continue;
249 }
250 if (x_subpixel == 1) {
251 if (normalize) {
252 int src_value = start_col > left ? AverageRgb(&src_scan[-1])
253 : (src_scan[0] + src_scan[1]) / 3;
254 NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
255 } else {
256 if (start_col > left)
257 MergeGammaAdjust(src_scan[-1], r, a, &dest_scan[2]);
258 MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
259 MergeGammaAdjust(src_scan[1], b, a, &dest_scan[0]);
260 SetAlpha(has_alpha, dest_scan);
261 }
262 NextPixel(&src_scan, &dest_scan, Bpp);
263 for (int col = start_col + 1; col < end_col; ++col) {
264 if (normalize) {
265 int src_value = AverageRgb(&src_scan[-1]);
266 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
267 } else {
268 MergeGammaAdjustRgb(&src_scan[-1], r, g, b, a, &dest_scan[0]);
269 SetAlpha(has_alpha, dest_scan);
270 }
271 NextPixel(&src_scan, &dest_scan, Bpp);
272 }
273 continue;
274 }
275 if (normalize) {
276 int src_value =
277 start_col > left ? AverageRgb(&src_scan[-2]) : src_scan[0] / 3;
278 NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
279 } else {
280 if (start_col > left) {
281 MergeGammaAdjust(src_scan[-2], r, a, &dest_scan[2]);
282 MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
283 }
284 MergeGammaAdjust(src_scan[0], b, a, &dest_scan[0]);
285 SetAlpha(has_alpha, dest_scan);
286 }
287 NextPixel(&src_scan, &dest_scan, Bpp);
288 for (int col = start_col + 1; col < end_col; ++col) {
289 if (normalize) {
290 int src_value = AverageRgb(&src_scan[-2]);
291 NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
292 } else {
293 MergeGammaAdjustRgb(&src_scan[-2], r, g, b, a, &dest_scan[0]);
294 SetAlpha(has_alpha, dest_scan);
295 }
296 NextPixel(&src_scan, &dest_scan, Bpp);
297 }
298 }
299}
300
301bool ShouldDrawDeviceText(const CFX_Font* pFont,
302 const CFX_TextRenderOptions& options) {
303#if BUILDFLAG(IS_APPLE)
304 if (options.font_is_cid)
305 return false;
306
307 const ByteString bsPsName = pFont->GetPsName();
308 if (bsPsName.Contains("+ZJHL"))
309 return false;
310
311 if (bsPsName == "CNAAJI+cmex10")
312 return false;
313#endif
314 return true;
315}
316
317// Returns true if the path is a 3-point path that draws A->B->A and forms a
318// zero area, or a 2-point path which draws A->B.
319bool CheckSimpleLinePath(pdfium::span<const CFX_Path::Point> points,
320 const CFX_Matrix* matrix,
321 bool adjust,
322 CFX_Path* new_path,
323 bool* thin,
324 bool* set_identity) {
325 if (points.size() != 2 && points.size() != 3)
326 return false;
327
328 if (points[0].m_Type != CFX_Path::Point::Type::kMove ||
329 points[1].m_Type != CFX_Path::Point::Type::kLine ||
330 (points.size() == 3 &&
331 (points[2].m_Type != CFX_Path::Point::Type::kLine ||
332 points[0].m_Point != points[2].m_Point))) {
333 return false;
334 }
335
336 // A special case that all points are identical, zero area is formed and no
337 // thin line needs to be drawn.
338 if (points[0].m_Point == points[1].m_Point)
339 return true;
340
341 for (size_t i = 0; i < 2; i++) {
342 CFX_PointF point = points[i].m_Point;
343 if (adjust) {
344 if (matrix)
345 point = matrix->Transform(point);
346
347 point = CFX_PointF(static_cast<int>(point.x) + 0.5f,
348 static_cast<int>(point.y) + 0.5f);
349 }
350 new_path->AppendPoint(point, points[i].m_Type);
351 }
352 if (adjust && matrix)
353 *set_identity = true;
354
355 *thin = true;
356 return true;
357}
358
359// Returns true if `points` is palindromic and forms zero area. Otherwise,
360// returns false.
361bool CheckPalindromicPath(pdfium::span<const CFX_Path::Point> points,
362 CFX_Path* new_path,
363 bool* thin) {
364 if (points.size() <= 3 || !(points.size() % 2))
365 return false;
366
367 const size_t mid = points.size() / 2;
368 CFX_Path temp_path;
369 for (size_t i = 0; i < mid; i++) {
370 const CFX_Path::Point& left = points[mid - i - 1];
371 const CFX_Path::Point& right = points[mid + i + 1];
372 bool zero_area = left.m_Point == right.m_Point &&
375 if (!zero_area)
376 return false;
377
378 temp_path.AppendPoint(points[mid - i].m_Point,
381 }
382
383 new_path->Append(temp_path, nullptr);
384 *thin = true;
385 return true;
386}
387
388bool IsFoldingVerticalLine(const CFX_PointF& a,
389 const CFX_PointF& b,
390 const CFX_PointF& c) {
391 return a.x == b.x && b.x == c.x && (b.y - a.y) * (b.y - c.y) > 0;
392}
393
394bool IsFoldingHorizontalLine(const CFX_PointF& a,
395 const CFX_PointF& b,
396 const CFX_PointF& c) {
397 return a.y == b.y && b.y == c.y && (b.x - a.x) * (b.x - c.x) > 0;
398}
399
400bool IsFoldingDiagonalLine(const CFX_PointF& a,
401 const CFX_PointF& b,
402 const CFX_PointF& c) {
403 return a.x != b.x && c.x != b.x && a.y != b.y && c.y != b.y &&
404 (a.y - b.y) * (c.x - b.x) == (c.y - b.y) * (a.x - b.x);
405}
406
407bool GetZeroAreaPath(pdfium::span<const CFX_Path::Point> points,
408 const CFX_Matrix* matrix,
409 bool adjust,
410 CFX_Path* new_path,
411 bool* thin,
412 bool* set_identity) {
413 *set_identity = false;
414
415 if (points.size() < 2)
416 return false;
417
418 if (CheckSimpleLinePath(points, matrix, adjust, new_path, thin,
419 set_identity)) {
420 return true;
421 }
422
423 if (CheckPalindromicPath(points, new_path, thin))
424 return true;
425
426 for (size_t i = 0; i < points.size(); i++) {
427 CFX_Path::Point::Type point_type = points[i].m_Type;
428 if (point_type == CFX_Path::Point::Type::kMove) {
429 DCHECK_EQ(0u, i);
430 continue;
431 }
432
433 if (point_type == CFX_Path::Point::Type::kBezier) {
434 i += 2;
435 DCHECK_LT(i, points.size());
436 continue;
437 }
438
439 DCHECK_EQ(point_type, CFX_Path::Point::Type::kLine);
440 size_t next_index = (i + 1) % (points.size());
441 const CFX_Path::Point& next = points[next_index];
443 continue;
444
445 const CFX_Path::Point& prev = points[i - 1];
446 const CFX_Path::Point& cur = points[i];
447 if (IsFoldingVerticalLine(prev.m_Point, cur.m_Point, next.m_Point)) {
448 bool use_prev = fabs(cur.m_Point.y - prev.m_Point.y) <
449 fabs(cur.m_Point.y - next.m_Point.y);
450 const CFX_Path::Point& start = use_prev ? prev : cur;
451 const CFX_Path::Point& end = use_prev ? cur : next;
454 continue;
455 }
456
457 if (IsFoldingHorizontalLine(prev.m_Point, cur.m_Point, next.m_Point) ||
458 IsFoldingDiagonalLine(prev.m_Point, cur.m_Point, next.m_Point)) {
459 bool use_prev = fabs(cur.m_Point.x - prev.m_Point.x) <
460 fabs(cur.m_Point.x - next.m_Point.x);
461 const CFX_Path::Point& start = use_prev ? prev : cur;
462 const CFX_Path::Point& end = use_prev ? cur : next;
465 continue;
466 }
467 }
468
469 size_t new_path_size = new_path->GetPoints().size();
470 if (points.size() > 3 && new_path_size > 0)
471 *thin = true;
472 return new_path_size != 0;
473}
474
475FXDIB_Format GetCreateCompatibleBitmapFormat(int render_caps) {
476 if (render_caps & FXRC_BYTEMASK_OUTPUT)
478 if (render_caps & FXRC_ALPHA_OUTPUT)
479 return FXDIB_Format::kArgb;
481}
482
483} // namespace
484
486
490
491// static
493 float height,
494 float left,
495 float top) {
496 return CFX_Matrix(width, 0, 0, -height, left, top + height);
497}
498
500 std::unique_ptr<RenderDeviceDriverIface> pDriver) {
501 DCHECK(pDriver);
502 DCHECK(!m_pDeviceDriver);
503 m_pDeviceDriver = std::move(pDriver);
504 InitDeviceInfo();
505}
506
507void CFX_RenderDevice::InitDeviceInfo() {
508 m_Width = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_WIDTH);
509 m_Height = m_pDeviceDriver->GetDeviceCaps(FXDC_PIXEL_HEIGHT);
510 m_bpp = m_pDeviceDriver->GetDeviceCaps(FXDC_BITS_PIXEL);
511 m_RenderCaps = m_pDeviceDriver->GetDeviceCaps(FXDC_RENDER_CAPS);
512 m_DeviceType = m_pDeviceDriver->GetDeviceType();
513 if (!m_pDeviceDriver->GetClipBox(&m_ClipBox)) {
514 m_ClipBox.left = 0;
515 m_ClipBox.top = 0;
516 m_ClipBox.right = m_Width;
517 m_ClipBox.bottom = m_Height;
518 }
519}
520
522 m_pDeviceDriver->SaveState();
523}
524
525void CFX_RenderDevice::RestoreState(bool bKeepSaved) {
526 if (m_pDeviceDriver) {
527 m_pDeviceDriver->RestoreState(bKeepSaved);
528 UpdateClipBox();
529 }
530}
531
532int CFX_RenderDevice::GetDeviceCaps(int caps_id) const {
533 return m_pDeviceDriver->GetDeviceCaps(caps_id);
534}
535
537 return m_pBitmap;
538}
539
540RetainPtr<const CFX_DIBitmap> CFX_RenderDevice::GetBitmap() const {
541 return m_pBitmap;
542}
543
544void CFX_RenderDevice::SetBitmap(RetainPtr<CFX_DIBitmap> bitmap) {
545 m_pBitmap = std::move(bitmap);
546}
547
549 const RetainPtr<CFX_DIBitmap>& pDIB,
550 int width,
551 int height) const {
552 return pDIB->Create(width, height,
553 GetCreateCompatibleBitmapFormat(m_RenderCaps));
554}
555
557 m_pDeviceDriver->SetBaseClip(rect);
558}
559
561 const CFX_Path& path,
562 const CFX_Matrix* pObject2Device,
563 const CFX_FillRenderOptions& fill_options) {
564 if (!m_pDeviceDriver->SetClip_PathFill(path, pObject2Device, fill_options))
565 return false;
566
567 UpdateClipBox();
568 return true;
569}
570
572 const CFX_Path& path,
573 const CFX_Matrix* pObject2Device,
574 const CFX_GraphStateData* pGraphState) {
575 if (!m_pDeviceDriver->SetClip_PathStroke(path, pObject2Device, pGraphState))
576 return false;
577
578 UpdateClipBox();
579 return true;
580}
581
583 CFX_Path path;
584 path.AppendRect(rect.left, rect.bottom, rect.right, rect.top);
585 if (!SetClip_PathFill(path, nullptr,
587 return false;
588 }
589
590 UpdateClipBox();
591 return true;
592}
593
594void CFX_RenderDevice::UpdateClipBox() {
595 if (m_pDeviceDriver->GetClipBox(&m_ClipBox))
596 return;
597 m_ClipBox.left = 0;
598 m_ClipBox.top = 0;
599 m_ClipBox.right = m_Width;
600 m_ClipBox.bottom = m_Height;
601}
602
604 const CFX_Matrix* pObject2Device,
605 const CFX_GraphStateData* pGraphState,
606 uint32_t fill_color,
607 uint32_t stroke_color,
608 const CFX_FillRenderOptions& fill_options) {
609 return DrawPathWithBlend(path, pObject2Device, pGraphState, fill_color,
610 stroke_color, fill_options, BlendMode::kNormal);
611}
612
614 const CFX_Path& path,
615 const CFX_Matrix* pObject2Device,
616 const CFX_GraphStateData* pGraphState,
617 uint32_t fill_color,
618 uint32_t stroke_color,
619 const CFX_FillRenderOptions& fill_options,
620 BlendMode blend_type) {
621 const bool fill =
623 uint8_t fill_alpha = fill ? FXARGB_A(fill_color) : 0;
624 uint8_t stroke_alpha = pGraphState ? FXARGB_A(stroke_color) : 0;
625 pdfium::span<const CFX_Path::Point> points = path.GetPoints();
626 if (stroke_alpha == 0 && points.size() == 2) {
627 CFX_PointF pos1 = points[0].m_Point;
628 CFX_PointF pos2 = points[1].m_Point;
629 if (pObject2Device) {
630 pos1 = pObject2Device->Transform(pos1);
631 pos2 = pObject2Device->Transform(pos2);
632 }
633 DrawCosmeticLine(pos1, pos2, fill_color, fill_options, blend_type);
634 return true;
635 }
636
637 if (stroke_alpha == 0 && !fill_options.rect_aa) {
638 absl::optional<CFX_FloatRect> maybe_rect_f = path.GetRect(pObject2Device);
639 if (maybe_rect_f.has_value()) {
640 const CFX_FloatRect& rect_f = maybe_rect_f.value();
641 FX_RECT rect_i = rect_f.GetOuterRect();
642
643 // Depending on the top/bottom, left/right values of the rect it's
644 // possible to overflow the Width() and Height() calculations. Check that
645 // the rect will have valid dimension before continuing.
646 if (!rect_i.Valid())
647 return false;
648
649 int width = static_cast<int>(ceil(rect_f.right - rect_f.left));
650 if (width < 1) {
651 width = 1;
652 if (rect_i.left == rect_i.right)
653 ++rect_i.right;
654 }
655 int height = static_cast<int>(ceil(rect_f.top - rect_f.bottom));
656 if (height < 1) {
657 height = 1;
658 if (rect_i.bottom == rect_i.top)
659 ++rect_i.bottom;
660 }
661 if (rect_i.Width() >= width + 1) {
662 if (rect_f.left - static_cast<float>(rect_i.left) >
663 static_cast<float>(rect_i.right) - rect_f.right) {
664 ++rect_i.left;
665 } else {
666 --rect_i.right;
667 }
668 }
669 if (rect_i.Height() >= height + 1) {
670 if (rect_f.top - static_cast<float>(rect_i.top) >
671 static_cast<float>(rect_i.bottom) - rect_f.bottom) {
672 ++rect_i.top;
673 } else {
674 --rect_i.bottom;
675 }
676 }
677 if (FillRectWithBlend(rect_i, fill_color, blend_type))
678 return true;
679 }
680 }
681
682 if (fill && stroke_alpha == 0 && !fill_options.stroke &&
683 !fill_options.text_mode) {
684 bool adjust = !!m_pDeviceDriver->GetDriverType();
685 std::vector<CFX_Path::Point> sub_path;
686 for (size_t i = 0; i < points.size(); i++) {
687 CFX_Path::Point::Type point_type = points[i].m_Type;
688 if (point_type == CFX_Path::Point::Type::kMove) {
689 // Process the existing sub path.
690 DrawZeroAreaPath(sub_path, pObject2Device, adjust,
691 fill_options.aliased_path, fill_color, fill_alpha,
692 blend_type);
693 sub_path.clear();
694
695 // Start forming the next sub path.
696 sub_path.push_back(points[i]);
697 continue;
698 }
699
700 if (point_type == CFX_Path::Point::Type::kBezier) {
701 sub_path.push_back(points[i]);
702 sub_path.push_back(points[i + 1]);
703 sub_path.push_back(points[i + 2]);
704 i += 2;
705 continue;
706 }
707
708 DCHECK_EQ(point_type, CFX_Path::Point::Type::kLine);
709 sub_path.push_back(points[i]);
710 }
711 // Process the last sub paths.
712 DrawZeroAreaPath(sub_path, pObject2Device, adjust,
713 fill_options.aliased_path, fill_color, fill_alpha,
714 blend_type);
715 }
716
717 if (fill && fill_alpha && stroke_alpha < 0xff && fill_options.stroke) {
718 if (m_RenderCaps & FXRC_FILLSTROKE_PATH) {
719#if defined(PDF_USE_SKIA)
720 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
721 m_pDeviceDriver->SetGroupKnockout(true);
722 }
723#endif
724 bool draw_fillstroke_path_result = m_pDeviceDriver->DrawPath(
725 path, pObject2Device, pGraphState, fill_color, stroke_color,
726 fill_options, blend_type);
727
728#if defined(PDF_USE_SKIA)
729 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
730 // Restore the group knockout status for `m_pDeviceDriver` after
731 // finishing painting a fill-and-stroke path.
732 m_pDeviceDriver->SetGroupKnockout(false);
733 }
734#endif
735 return draw_fillstroke_path_result;
736 }
737 return DrawFillStrokePath(path, pObject2Device, pGraphState, fill_color,
738 stroke_color, fill_options, blend_type);
739 }
740 return m_pDeviceDriver->DrawPath(path, pObject2Device, pGraphState,
741 fill_color, stroke_color, fill_options,
742 blend_type);
743}
744
745// This can be removed once PDFium entirely relies on Skia
746bool CFX_RenderDevice::DrawFillStrokePath(
747 const CFX_Path& path,
748 const CFX_Matrix* pObject2Device,
749 const CFX_GraphStateData* pGraphState,
750 uint32_t fill_color,
751 uint32_t stroke_color,
752 const CFX_FillRenderOptions& fill_options,
753 BlendMode blend_type) {
754 if (!(m_RenderCaps & FXRC_GET_BITS))
755 return false;
756 CFX_FloatRect bbox;
757 if (pGraphState) {
759 pGraphState->m_MiterLimit);
760 } else {
761 bbox = path.GetBoundingBox();
762 }
763 if (pObject2Device)
764 bbox = pObject2Device->TransformRect(bbox);
765
766 FX_RECT rect = bbox.GetOuterRect();
767 if (!rect.Valid())
768 return false;
769
770 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
771 auto backdrop = pdfium::MakeRetain<CFX_DIBitmap>();
773 return false;
774
775 if (bitmap->IsAlphaFormat()) {
776 backdrop->Copy(bitmap);
777 } else {
778 if (!m_pDeviceDriver->GetDIBits(bitmap, rect.left, rect.top))
779 return false;
780 backdrop->Copy(bitmap);
781 }
782 CFX_DefaultRenderDevice bitmap_device;
783 bitmap_device.AttachWithBackdropAndGroupKnockout(bitmap, std::move(backdrop),
784 /*bGroupKnockout=*/true);
785
786 CFX_Matrix matrix;
787 if (pObject2Device)
788 matrix = *pObject2Device;
789 matrix.Translate(-rect.left, -rect.top);
790 if (!bitmap_device.GetDeviceDriver()->DrawPath(path, &matrix, pGraphState,
791 fill_color, stroke_color,
792 fill_options, blend_type)) {
793 return false;
794 }
795 FX_RECT src_rect(0, 0, rect.Width(), rect.Height());
796 return m_pDeviceDriver->SetDIBits(std::move(bitmap), 0, src_rect, rect.left,
797 rect.top, BlendMode::kNormal);
798}
799
800bool CFX_RenderDevice::FillRectWithBlend(const FX_RECT& rect,
801 uint32_t fill_color,
802 BlendMode blend_type) {
803 if (m_pDeviceDriver->FillRectWithBlend(rect, fill_color, blend_type))
804 return true;
805
806 if (!(m_RenderCaps & FXRC_GET_BITS))
807 return false;
808
809 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
811 return false;
812
813 if (!m_pDeviceDriver->GetDIBits(bitmap, rect.left, rect.top))
814 return false;
815
816 if (!bitmap->CompositeRect(0, 0, rect.Width(), rect.Height(), fill_color))
817 return false;
818
819 FX_RECT src_rect(0, 0, rect.Width(), rect.Height());
820 m_pDeviceDriver->SetDIBits(bitmap, 0, src_rect, rect.left, rect.top,
821 BlendMode::kNormal);
822 return true;
823}
824
825bool CFX_RenderDevice::DrawCosmeticLine(
826 const CFX_PointF& ptMoveTo,
827 const CFX_PointF& ptLineTo,
828 uint32_t color,
829 const CFX_FillRenderOptions& fill_options,
830 BlendMode blend_type) {
831 if ((color >= 0xff000000) && m_pDeviceDriver->DrawCosmeticLine(
832 ptMoveTo, ptLineTo, color, blend_type)) {
833 return true;
834 }
835 CFX_GraphStateData graph_state;
836 CFX_Path path;
839 return m_pDeviceDriver->DrawPath(path, nullptr, &graph_state, 0, color,
840 fill_options, blend_type);
841}
842
843void CFX_RenderDevice::DrawZeroAreaPath(
844 const std::vector<CFX_Path::Point>& path,
845 const CFX_Matrix* matrix,
846 bool adjust,
847 bool aliased_path,
848 uint32_t fill_color,
849 uint8_t fill_alpha,
850 BlendMode blend_type) {
851 if (path.empty())
852 return;
853
854 CFX_Path new_path;
855 bool thin = false;
856 bool set_identity = false;
857
858 if (!GetZeroAreaPath(path, matrix, adjust, &new_path, &thin, &set_identity))
859 return;
860
861 CFX_GraphStateData graph_state;
862 graph_state.m_LineWidth = 0.0f;
863
864 uint32_t stroke_color = fill_color;
865 if (thin)
866 stroke_color = (((fill_alpha >> 2) << 24) | (stroke_color & 0x00ffffff));
867
868 const CFX_Matrix* new_matrix = nullptr;
869 if (matrix && !matrix->IsIdentity() && !set_identity)
870 new_matrix = matrix;
871
872 CFX_FillRenderOptions path_options;
873 path_options.zero_area = true;
874 path_options.aliased_path = aliased_path;
875
876 m_pDeviceDriver->DrawPath(new_path, new_matrix, &graph_state, 0, stroke_color,
877 path_options, blend_type);
878}
879
880bool CFX_RenderDevice::GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap,
881 int left,
882 int top) {
883 return (m_RenderCaps & FXRC_GET_BITS) &&
884 m_pDeviceDriver->GetDIBits(pBitmap, left, top);
885}
886
888 return m_pDeviceDriver->GetBackDrop();
889}
890
892 const RetainPtr<const CFX_DIBBase>& pBitmap,
893 int left,
894 int top,
895 BlendMode blend_mode) {
896 DCHECK(!pBitmap->IsMaskFormat());
897 FX_RECT dest_rect(left, top, left + pBitmap->GetWidth(),
898 top + pBitmap->GetHeight());
899 dest_rect.Intersect(m_ClipBox);
900 if (dest_rect.IsEmpty())
901 return true;
902
903 FX_RECT src_rect(dest_rect.left - left, dest_rect.top - top,
904 dest_rect.left - left + dest_rect.Width(),
905 dest_rect.top - top + dest_rect.Height());
906 if ((blend_mode == BlendMode::kNormal || (m_RenderCaps & FXRC_BLEND_MODE)) &&
907 (!pBitmap->IsAlphaFormat() || (m_RenderCaps & FXRC_ALPHA_IMAGE))) {
908 return m_pDeviceDriver->SetDIBits(std::move(pBitmap), 0, src_rect,
909 dest_rect.left, dest_rect.top,
910 blend_mode);
911 }
912 if (!(m_RenderCaps & FXRC_GET_BITS))
913 return false;
914
915 int bg_pixel_width = dest_rect.Width();
916 int bg_pixel_height = dest_rect.Height();
917 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
918 if (!background->Create(bg_pixel_width, bg_pixel_height,
920 return false;
921 }
922 if (!m_pDeviceDriver->GetDIBits(background, dest_rect.left, dest_rect.top))
923 return false;
924
925 if (!background->CompositeBitmap(0, 0, bg_pixel_width, bg_pixel_height,
926 std::move(pBitmap), src_rect.left,
927 src_rect.top, blend_mode, nullptr, false)) {
928 return false;
929 }
930 FX_RECT rect(0, 0, bg_pixel_width, bg_pixel_height);
931 return m_pDeviceDriver->SetDIBits(background, 0, rect, dest_rect.left,
932 dest_rect.top, BlendMode::kNormal);
933}
934
936 int left,
937 int top,
938 int dest_width,
939 int dest_height) {
941 std::move(bitmap), left, top, dest_width, dest_height,
943}
944
946 RetainPtr<const CFX_DIBBase> bitmap,
947 int left,
948 int top,
949 int dest_width,
950 int dest_height,
951 const FXDIB_ResampleOptions& options,
952 BlendMode blend_mode) {
953 FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
954 FX_RECT clip_box = m_ClipBox;
955 clip_box.Intersect(dest_rect);
956 return clip_box.IsEmpty() || m_pDeviceDriver->StretchDIBits(
957 std::move(bitmap), 0, left, top, dest_width,
958 dest_height, &clip_box, options, blend_mode);
959}
960
962 int left,
963 int top,
964 uint32_t argb) {
965 FX_RECT src_rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
966 return m_pDeviceDriver->SetDIBits(pBitmap, argb, src_rect, left, top,
967 BlendMode::kNormal);
968}
969
971 int left,
972 int top,
973 int dest_width,
974 int dest_height,
975 uint32_t color) {
976 return StretchBitMaskWithFlags(std::move(bitmap), left, top, dest_width,
977 dest_height, color, FXDIB_ResampleOptions());
978}
979
981 RetainPtr<CFX_DIBBase> bitmap,
982 int left,
983 int top,
984 int dest_width,
985 int dest_height,
986 uint32_t argb,
987 const FXDIB_ResampleOptions& options) {
988 FX_RECT dest_rect(left, top, left + dest_width, top + dest_height);
989 FX_RECT clip_box = m_ClipBox;
990 clip_box.Intersect(dest_rect);
991 return m_pDeviceDriver->StretchDIBits(std::move(bitmap), argb, left, top,
992 dest_width, dest_height, &clip_box,
993 options, BlendMode::kNormal);
994}
995
997 float alpha,
998 uint32_t argb,
999 const CFX_Matrix& matrix,
1000 const FXDIB_ResampleOptions& options,
1001 std::unique_ptr<CFX_ImageRenderer>* handle) {
1002 return StartDIBitsWithBlend(std::move(bitmap), alpha, argb, matrix, options,
1003 handle, BlendMode::kNormal);
1004}
1005
1007 RetainPtr<const CFX_DIBBase> bitmap,
1008 float alpha,
1009 uint32_t argb,
1010 const CFX_Matrix& matrix,
1011 const FXDIB_ResampleOptions& options,
1012 std::unique_ptr<CFX_ImageRenderer>* handle,
1013 BlendMode blend_mode) {
1014 return m_pDeviceDriver->StartDIBits(std::move(bitmap), alpha, argb, matrix,
1015 options, handle, blend_mode);
1016}
1017
1019 PauseIndicatorIface* pPause) {
1020 return m_pDeviceDriver->ContinueDIBits(handle, pPause);
1021}
1022
1023#if defined(PDF_USE_SKIA)
1024bool CFX_RenderDevice::SetBitsWithMask(RetainPtr<const CFX_DIBBase> bitmap,
1025 RetainPtr<const CFX_DIBBase> mask,
1026 int left,
1027 int top,
1028 float alpha,
1029 BlendMode blend_type) {
1030 return m_pDeviceDriver->SetBitsWithMask(std::move(bitmap), std::move(mask),
1031 left, top, alpha, blend_type);
1032}
1033
1034bool CFX_RenderDevice::SyncInternalBitmaps() {
1035 return m_pDeviceDriver->SyncInternalBitmaps();
1036}
1037#endif
1038
1039bool CFX_RenderDevice::DrawNormalText(pdfium::span<const TextCharPos> pCharPos,
1040 CFX_Font* pFont,
1041 float font_size,
1042 const CFX_Matrix& mtText2Device,
1043 uint32_t fill_color,
1044 const CFX_TextRenderOptions& options) {
1045 // `anti_alias` and `normalize` don't affect Skia rendering.
1046 int anti_alias = FT_RENDER_MODE_MONO;
1047 bool normalize = false;
1048 const bool is_text_smooth = options.IsSmooth();
1049 // |text_options| has the potential to affect all derived classes of
1050 // RenderDeviceDriverIface. But now it only affects Skia rendering.
1051 CFX_TextRenderOptions text_options(options);
1052 if (is_text_smooth) {
1053 if (GetDeviceType() == DeviceType::kDisplay && m_bpp > 1) {
1055 // Some Freetype implementations (like the one packaged with Fedora) do
1056 // not support hinting due to patents 6219025, 6239783, 6307566,
1057 // 6225973, 6243070, 6393145, 6421054, 6282327, and 6624828; the latest
1058 // one expires 10/7/19. This makes LCD anti-aliasing very ugly, so we
1059 // instead fall back on NORMAL anti-aliasing.
1060 anti_alias = FT_RENDER_MODE_NORMAL;
1061 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1062 // Since |anti_alias| doesn't affect Skia rendering, and Skia only
1063 // follows strictly to the options provided by |text_options|, we need
1064 // to update |text_options| so that Skia falls back on normal
1065 // anti-aliasing as well.
1067 }
1068 } else if ((m_RenderCaps & FXRC_ALPHA_OUTPUT)) {
1069 // Whether Skia uses LCD optimization should strictly follow the
1070 // rendering options provided by |text_options|. No change needs to be
1071 // done for |text_options| here.
1072 anti_alias = FT_RENDER_MODE_LCD;
1073 normalize = true;
1074 } else if (m_bpp < 16) {
1075 // This case doesn't apply to Skia since Skia always have |m_bpp| = 32.
1076 anti_alias = FT_RENDER_MODE_NORMAL;
1077 } else {
1078 // Whether Skia uses LCD optimization should strictly follow the
1079 // rendering options provided by |text_options|. No change needs to be
1080 // done for |text_options| here.
1081 anti_alias = FT_RENDER_MODE_LCD;
1082 normalize = !pFont->GetFaceRec() ||
1084 }
1085 }
1086 }
1087
1089 if (ShouldDrawDeviceText(pFont, options) &&
1090 m_pDeviceDriver->DrawDeviceText(pCharPos, pFont, mtText2Device,
1091 font_size, fill_color, text_options)) {
1092 return true;
1093 }
1094 if (FXARGB_A(fill_color) < 255)
1095 return false;
1096 } else if (options.native_text) {
1097 if (ShouldDrawDeviceText(pFont, options) &&
1098 m_pDeviceDriver->DrawDeviceText(pCharPos, pFont, mtText2Device,
1099 font_size, fill_color, text_options)) {
1100 return true;
1101 }
1102 }
1103
1104 CFX_Matrix char2device = mtText2Device;
1105 CFX_Matrix text2Device = mtText2Device;
1106 char2device.Scale(font_size, -font_size);
1107 if (fabs(char2device.a) + fabs(char2device.b) > 50 * 1.0f ||
1109 if (pFont->GetFaceRec()) {
1110 CFX_FillRenderOptions path_options;
1111 path_options.aliased_path = !is_text_smooth;
1112 return DrawTextPath(pCharPos, pFont, font_size, mtText2Device, nullptr,
1113 nullptr, fill_color, 0, nullptr, path_options);
1114 }
1115 }
1116 std::vector<TextGlyphPos> glyphs(pCharPos.size());
1117 for (size_t i = 0; i < glyphs.size(); ++i) {
1118 TextGlyphPos& glyph = glyphs[i];
1119 const TextCharPos& charpos = pCharPos[i];
1120
1121 glyph.m_fDeviceOrigin = text2Device.Transform(charpos.m_Origin);
1122 if (anti_alias < FT_RENDER_MODE_LCD)
1123 glyph.m_Origin.x = FXSYS_roundf(glyph.m_fDeviceOrigin.x);
1124 else
1125 glyph.m_Origin.x = static_cast<int>(floor(glyph.m_fDeviceOrigin.x));
1126 glyph.m_Origin.y = FXSYS_roundf(glyph.m_fDeviceOrigin.y);
1127
1128 CFX_Matrix matrix = charpos.GetEffectiveMatrix(char2device);
1129 glyph.m_pGlyph = pFont->LoadGlyphBitmap(
1130 charpos.m_GlyphIndex, charpos.m_bFontStyle, matrix,
1131 charpos.m_FontCharWidth, anti_alias, &text_options);
1132 }
1133 if (anti_alias < FT_RENDER_MODE_LCD && glyphs.size() > 1)
1134 AdjustGlyphSpace(&glyphs);
1135
1136 FX_RECT bmp_rect = GetGlyphsBBox(glyphs, anti_alias);
1137 bmp_rect.Intersect(m_ClipBox);
1138 if (bmp_rect.IsEmpty())
1139 return true;
1140
1141 int pixel_width = bmp_rect.Width();
1142 int pixel_height = bmp_rect.Height();
1143 int pixel_left = bmp_rect.left;
1144 int pixel_top = bmp_rect.top;
1145 if (anti_alias == FT_RENDER_MODE_MONO) {
1146 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
1147 if (!bitmap->Create(pixel_width, pixel_height, FXDIB_Format::k1bppMask))
1148 return false;
1149 for (const TextGlyphPos& glyph : glyphs) {
1150 if (!glyph.m_pGlyph)
1151 continue;
1152
1153 absl::optional<CFX_Point> point =
1154 glyph.GetOrigin({pixel_left, pixel_top});
1155 if (!point.has_value())
1156 continue;
1157
1158 const RetainPtr<CFX_DIBitmap>& pGlyph = glyph.m_pGlyph->GetBitmap();
1159 bitmap->CompositeOneBPPMask(point.value().x, point.value().y,
1160 pGlyph->GetWidth(), pGlyph->GetHeight(),
1161 pGlyph, 0, 0);
1162 }
1163 return SetBitMask(bitmap, bmp_rect.left, bmp_rect.top, fill_color);
1164 }
1165 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
1166 if (m_bpp == 8) {
1167 if (!bitmap->Create(pixel_width, pixel_height, FXDIB_Format::k8bppMask))
1168 return false;
1169 } else {
1170 if (!CreateCompatibleBitmap(bitmap, pixel_width, pixel_height))
1171 return false;
1172 }
1173 if (!bitmap->IsAlphaFormat() && !bitmap->IsMaskFormat()) {
1174 bitmap->Clear(0xFFFFFFFF);
1175 if (!GetDIBits(bitmap, bmp_rect.left, bmp_rect.top))
1176 return false;
1177 }
1178 int dest_width = pixel_width;
1179 int a = 0;
1180 int r = 0;
1181 int g = 0;
1182 int b = 0;
1183 if (anti_alias == FT_RENDER_MODE_LCD)
1184 std::tie(a, r, g, b) = ArgbDecode(fill_color);
1185
1186 for (const TextGlyphPos& glyph : glyphs) {
1187 if (!glyph.m_pGlyph)
1188 continue;
1189
1190 absl::optional<CFX_Point> point = glyph.GetOrigin({pixel_left, pixel_top});
1191 if (!point.has_value())
1192 continue;
1193
1194 const RetainPtr<CFX_DIBitmap>& pGlyph = glyph.m_pGlyph->GetBitmap();
1195 int ncols = pGlyph->GetWidth();
1196 int nrows = pGlyph->GetHeight();
1197 if (anti_alias == FT_RENDER_MODE_NORMAL) {
1198 if (!bitmap->CompositeMask(point.value().x, point.value().y, ncols, nrows,
1199 pGlyph, fill_color, 0, 0, BlendMode::kNormal,
1200 nullptr, false)) {
1201 return false;
1202 }
1203 continue;
1204 }
1205 ncols /= 3;
1206 int x_subpixel = static_cast<int>(glyph.m_fDeviceOrigin.x * 3) % 3;
1207 int start_col = std::max(point->x, 0);
1208 FX_SAFE_INT32 end_col_safe = point->x;
1209 end_col_safe += ncols;
1210 if (!end_col_safe.IsValid())
1211 continue;
1212
1213 int end_col = std::min<int>(end_col_safe.ValueOrDie(), dest_width);
1214 if (start_col >= end_col)
1215 continue;
1216
1217 DrawNormalTextHelper(bitmap, pGlyph, nrows, point->x, point->y, start_col,
1218 end_col, normalize, x_subpixel, a, r, g, b);
1219 }
1220
1221 if (bitmap->IsMaskFormat())
1222 SetBitMask(bitmap, bmp_rect.left, bmp_rect.top, fill_color);
1223 else
1224 SetDIBits(bitmap, bmp_rect.left, bmp_rect.top);
1225 return true;
1226}
1227
1228bool CFX_RenderDevice::DrawTextPath(pdfium::span<const TextCharPos> pCharPos,
1229 CFX_Font* pFont,
1230 float font_size,
1231 const CFX_Matrix& mtText2User,
1232 const CFX_Matrix* pUser2Device,
1233 const CFX_GraphStateData* pGraphState,
1234 uint32_t fill_color,
1235 FX_ARGB stroke_color,
1236 CFX_Path* pClippingPath,
1237 const CFX_FillRenderOptions& fill_options) {
1238 for (const auto& charpos : pCharPos) {
1239 const CFX_Path* pPath =
1240 pFont->LoadGlyphPath(charpos.m_GlyphIndex, charpos.m_FontCharWidth);
1241 if (!pPath)
1242 continue;
1243
1244 CFX_Matrix matrix(font_size, 0, 0, font_size, charpos.m_Origin.x,
1245 charpos.m_Origin.y);
1246 matrix = charpos.GetEffectiveMatrix(matrix);
1247 matrix.Concat(mtText2User);
1248
1249 CFX_Path transformed_path(*pPath);
1250 transformed_path.Transform(matrix);
1251 if (fill_color || stroke_color) {
1252 CFX_FillRenderOptions options(fill_options);
1253 if (fill_color) {
1254 options.fill_type = CFX_FillRenderOptions::FillType::kWinding;
1255 }
1256 options.text_mode = true;
1257 if (!DrawPathWithBlend(transformed_path, pUser2Device, pGraphState,
1258 fill_color, stroke_color, options,
1259 BlendMode::kNormal)) {
1260 return false;
1261 }
1262 }
1263 if (pClippingPath)
1264 pClippingPath->Append(transformed_path, pUser2Device);
1265 }
1266 return true;
1267}
1268
1269void CFX_RenderDevice::DrawFillRect(const CFX_Matrix* pUser2Device,
1270 const CFX_FloatRect& rect,
1271 const FX_COLORREF& color) {
1272 CFX_Path path;
1273 path.AppendFloatRect(rect);
1274 DrawPath(path, pUser2Device, nullptr, color, 0,
1276}
1277
1278void CFX_RenderDevice::DrawFillArea(const CFX_Matrix& mtUser2Device,
1279 const std::vector<CFX_PointF>& points,
1280 const FX_COLORREF& color) {
1281 DCHECK(!points.empty());
1282 CFX_Path path;
1284 for (size_t i = 1; i < points.size(); ++i)
1286
1287 DrawPath(path, &mtUser2Device, nullptr, color, 0,
1289}
1290
1291void CFX_RenderDevice::DrawStrokeRect(const CFX_Matrix& mtUser2Device,
1292 const CFX_FloatRect& rect,
1293 const FX_COLORREF& color,
1294 float fWidth) {
1296 gsd.m_LineWidth = fWidth;
1297
1298 CFX_Path path;
1299 path.AppendFloatRect(rect);
1300 DrawPath(path, &mtUser2Device, &gsd, 0, color,
1302}
1303
1305 const CFX_PointF& ptMoveTo,
1306 const CFX_PointF& ptLineTo,
1307 const FX_COLORREF& color,
1308 float fWidth) {
1309 CFX_Path path;
1312
1314 gsd.m_LineWidth = fWidth;
1315
1316 DrawPath(path, pUser2Device, &gsd, 0, color,
1318}
1319
1320void CFX_RenderDevice::DrawFillRect(const CFX_Matrix* pUser2Device,
1321 const CFX_FloatRect& rect,
1322 const CFX_Color& color,
1323 int32_t nTransparency) {
1324 DrawFillRect(pUser2Device, rect, color.ToFXColor(nTransparency));
1325}
1326
1327void CFX_RenderDevice::DrawShadow(const CFX_Matrix& mtUser2Device,
1328 const CFX_FloatRect& rect,
1329 int32_t nTransparency,
1330 int32_t nStartGray,
1331 int32_t nEndGray) {
1332 constexpr float kBorder = 0.5f;
1333 constexpr float kSegmentWidth = 1.0f;
1334 constexpr float kLineWidth = 1.5f;
1335
1336 float fStepGray = (nEndGray - nStartGray) / rect.Height();
1337 CFX_PointF start(rect.left, 0);
1338 CFX_PointF end(rect.right, 0);
1339
1340 for (float fy = rect.bottom + kBorder; fy <= rect.top - kBorder;
1341 fy += kSegmentWidth) {
1342 start.y = fy;
1343 end.y = fy;
1344 int nGray = nStartGray + static_cast<int>(fStepGray * (fy - rect.bottom));
1345 FX_ARGB color = ArgbEncode(nTransparency, nGray, nGray, nGray);
1346 DrawStrokeLine(&mtUser2Device, start, end, color, kLineWidth);
1347 }
1348}
1349
1350bool CFX_RenderDevice::DrawShading(const CPDF_ShadingPattern* pPattern,
1351 const CFX_Matrix* pMatrix,
1352 const FX_RECT& clip_rect,
1353 int alpha,
1354 bool bAlphaMode) {
1355 return m_pDeviceDriver->DrawShading(pPattern, pMatrix, clip_rect, alpha,
1356 bAlphaMode);
1357}
1358
1359void CFX_RenderDevice::DrawBorder(const CFX_Matrix* pUser2Device,
1360 const CFX_FloatRect& rect,
1361 float fWidth,
1362 const CFX_Color& color,
1363 const CFX_Color& crLeftTop,
1364 const CFX_Color& crRightBottom,
1365 BorderStyle nStyle,
1366 int32_t nTransparency) {
1367 if (fWidth <= 0.0f)
1368 return;
1369
1370 const float fLeft = rect.left;
1371 const float fRight = rect.right;
1372 const float fTop = rect.top;
1373 const float fBottom = rect.bottom;
1374 const float fHalfWidth = fWidth / 2.0f;
1375
1376 switch (nStyle) {
1377 case BorderStyle::kSolid: {
1378 CFX_Path path;
1379 path.AppendRect(fLeft, fBottom, fRight, fTop);
1380 path.AppendRect(fLeft + fWidth, fBottom + fWidth, fRight - fWidth,
1381 fTop - fWidth);
1382 DrawPath(path, pUser2Device, nullptr, color.ToFXColor(nTransparency), 0,
1384 break;
1385 }
1386 case BorderStyle::kDash: {
1388 gsd.m_DashArray = {3.0f, 3.0f};
1389 gsd.m_DashPhase = 0;
1390 gsd.m_LineWidth = fWidth;
1391
1392 CFX_Path path;
1393 path.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1395 path.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fTop - fHalfWidth),
1397 path.AppendPoint(CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1399 path.AppendPoint(CFX_PointF(fRight - fHalfWidth, fBottom + fHalfWidth),
1401 path.AppendPoint(CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1403 DrawPath(path, pUser2Device, &gsd, 0, color.ToFXColor(nTransparency),
1405 break;
1406 }
1408 case BorderStyle::kInset: {
1410 gsd.m_LineWidth = fHalfWidth;
1411
1412 CFX_Path path_left_top;
1413 path_left_top.AppendPoint(
1414 CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1416 path_left_top.AppendPoint(
1417 CFX_PointF(fLeft + fHalfWidth, fTop - fHalfWidth),
1419 path_left_top.AppendPoint(
1420 CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1422 path_left_top.AppendPoint(CFX_PointF(fRight - fWidth, fTop - fWidth),
1424 path_left_top.AppendPoint(CFX_PointF(fLeft + fWidth, fTop - fWidth),
1426 path_left_top.AppendPoint(CFX_PointF(fLeft + fWidth, fBottom + fWidth),
1428 path_left_top.AppendPoint(
1429 CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1431 DrawPath(path_left_top, pUser2Device, &gsd,
1432 crLeftTop.ToFXColor(nTransparency), 0,
1434
1435 CFX_Path path_right_bottom;
1436 path_right_bottom.AppendPoint(
1437 CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1439 path_right_bottom.AppendPoint(
1440 CFX_PointF(fRight - fHalfWidth, fBottom + fHalfWidth),
1442 path_right_bottom.AppendPoint(
1443 CFX_PointF(fLeft + fHalfWidth, fBottom + fHalfWidth),
1445 path_right_bottom.AppendPoint(
1446 CFX_PointF(fLeft + fWidth, fBottom + fWidth),
1448 path_right_bottom.AppendPoint(
1449 CFX_PointF(fRight - fWidth, fBottom + fWidth),
1451 path_right_bottom.AppendPoint(CFX_PointF(fRight - fWidth, fTop - fWidth),
1453 path_right_bottom.AppendPoint(
1454 CFX_PointF(fRight - fHalfWidth, fTop - fHalfWidth),
1456 DrawPath(path_right_bottom, pUser2Device, &gsd,
1457 crRightBottom.ToFXColor(nTransparency), 0,
1459
1460 CFX_Path path;
1461 path.AppendRect(fLeft, fBottom, fRight, fTop);
1462 path.AppendRect(fLeft + fHalfWidth, fBottom + fHalfWidth,
1463 fRight - fHalfWidth, fTop - fHalfWidth);
1464 DrawPath(path, pUser2Device, &gsd, color.ToFXColor(nTransparency), 0,
1466 break;
1467 }
1470 gsd.m_LineWidth = fWidth;
1471
1472 CFX_Path path;
1473 path.AppendPoint(CFX_PointF(fLeft, fBottom + fHalfWidth),
1475 path.AppendPoint(CFX_PointF(fRight, fBottom + fHalfWidth),
1477 DrawPath(path, pUser2Device, &gsd, 0, color.ToFXColor(nTransparency),
1479 break;
1480 }
1481 }
1482}
1483
1485 return m_pDeviceDriver->MultiplyAlpha(alpha);
1486}
1487
1489 const RetainPtr<const CFX_DIBBase>& mask) {
1490 return m_pDeviceDriver->MultiplyAlphaMask(mask);
1491}
1492
1494 : m_pDevice(pDevice) {
1495 m_pDevice->SaveState();
1496}
1497
1499 m_pDevice->RestoreState(false);
1500}
BorderStyle
static constexpr FXDIB_Format kPlatformRGBFormat
Definition cfx_dibbase.h:39
bool AttachWithBackdropAndGroupKnockout(RetainPtr< CFX_DIBitmap > pBitmap, RetainPtr< CFX_DIBitmap > pBackdropBitmap, bool bGroupKnockout)
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
float Height() const
FX_RECT GetOuterRect() const
bool FTLibrarySupportsHinting() const
Definition cfx_fontmgr.h:74
const CFX_GlyphBitmap * LoadGlyphBitmap(uint32_t glyph_index, bool bFontStyle, const CFX_Matrix &matrix, int dest_width, int anti_alias, CFX_TextRenderOptions *text_options) const
Definition cfx_font.cpp:453
FXFT_FaceRec * GetFaceRec() const
Definition cfx_font.h:79
static CFX_GEModule * Get()
CFX_FontMgr * GetFontMgr() const
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_FloatRect TransformRect(const CFX_FloatRect &rect) const
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
CFX_PointF Transform(const CFX_PointF &point) const
void Translate(int32_t x, int32_t y)
bool IsIdentity() const
void Scale(float sx, float sy)
CFX_FloatRect GetBoundingBox() const
Definition cfx_path.cpp:322
void AppendRect(float left, float bottom, float right, float top)
Definition cfx_path.cpp:309
void Append(const CFX_Path &src, const CFX_Matrix *matrix)
Definition cfx_path.cpp:275
CFX_FloatRect GetBoundingBoxForStrokePath(float line_width, float miter_limit) const
Definition cfx_path.cpp:332
void AppendFloatRect(const CFX_FloatRect &rect)
Definition cfx_path.cpp:305
void AppendPoint(const CFX_PointF &point, Point::Type type)
Definition cfx_path.cpp:289
StateRestorer(CFX_RenderDevice *pDevice)
void 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)
bool StretchBitMask(RetainPtr< CFX_DIBBase > bitmap, int left, int top, int dest_width, int dest_height, uint32_t color)
bool MultiplyAlpha(float alpha)
bool DrawTextPath(pdfium::span< 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, uint32_t stroke_color, CFX_Path *pClippingPath, const CFX_FillRenderOptions &fill_options)
bool SetClip_Rect(const FX_RECT &pRect)
bool SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options)
bool StretchDIBits(RetainPtr< const CFX_DIBBase > bitmap, int left, int top, int dest_width, int dest_height)
bool SetClip_PathStroke(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState)
RenderDeviceDriverIface * GetDeviceDriver() const
void SetBaseClip(const FX_RECT &rect)
bool GetDIBits(const RetainPtr< CFX_DIBitmap > &pBitmap, int left, int top)
bool StretchDIBitsWithFlagsAndBlend(RetainPtr< const CFX_DIBBase > bitmap, int left, int top, int dest_width, int dest_height, const FXDIB_ResampleOptions &options, BlendMode blend_mode)
bool MultiplyAlphaMask(const RetainPtr< const CFX_DIBBase > &mask)
bool ContinueDIBits(CFX_ImageRenderer *handle, PauseIndicatorIface *pPause)
void DrawFillRect(const CFX_Matrix *pUser2Device, const CFX_FloatRect &rect, const FX_COLORREF &color)
RetainPtr< const CFX_DIBitmap > GetBitmap() const
void SetDeviceDriver(std::unique_ptr< RenderDeviceDriverIface > pDriver)
bool DrawShading(const CPDF_ShadingPattern *pPattern, const CFX_Matrix *pMatrix, const FX_RECT &clip_rect, int alpha, bool bAlphaMode)
static CFX_Matrix GetFlipMatrix(float width, float height, float left, float top)
void DrawFillArea(const CFX_Matrix &mtUser2Device, const std::vector< CFX_PointF > &points, const FX_COLORREF &color)
bool StartDIBits(RetainPtr< const CFX_DIBBase > bitmap, float alpha, uint32_t argb, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options, std::unique_ptr< CFX_ImageRenderer > *handle)
bool StretchBitMaskWithFlags(RetainPtr< CFX_DIBBase > bitmap, int left, int top, int dest_width, int dest_height, uint32_t argb, const FXDIB_ResampleOptions &options)
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options)
RetainPtr< CFX_DIBitmap > GetBackDrop()
void SetBitmap(RetainPtr< CFX_DIBitmap > bitmap)
void DrawShadow(const CFX_Matrix &mtUser2Device, const CFX_FloatRect &rect, int32_t nTransparency, int32_t nStartGray, int32_t nEndGray)
bool SetDIBitsWithBlend(const RetainPtr< const CFX_DIBBase > &pBitmap, int left, int top, BlendMode blend_mode)
void DrawStrokeLine(const CFX_Matrix *pUser2Device, const CFX_PointF &ptMoveTo, const CFX_PointF &ptLineTo, const FX_COLORREF &color, float fWidth)
bool StartDIBitsWithBlend(RetainPtr< const CFX_DIBBase > bitmap, float alpha, uint32_t argb, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options, std::unique_ptr< CFX_ImageRenderer > *handle, BlendMode blend_mode)
bool DrawNormalText(pdfium::span< const TextCharPos > pCharPos, CFX_Font *pFont, float font_size, const CFX_Matrix &mtText2Device, uint32_t fill_color, const CFX_TextRenderOptions &options)
bool DrawPathWithBlend(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options, BlendMode blend_type)
void DrawStrokeRect(const CFX_Matrix &mtUser2Device, const CFX_FloatRect &rect, const FX_COLORREF &color, float fWidth)
RetainPtr< CFX_DIBitmap > GetBitmap()
int GetDeviceCaps(int id) const
void RestoreState(bool bKeepSaved)
bool SetDIBits(const RetainPtr< const CFX_DIBBase > &pBitmap, int left, int top)
bool CreateCompatibleBitmap(const RetainPtr< CFX_DIBitmap > &pDIB, int width, int height) const
DeviceType GetDeviceType() const
bool SetBitMask(const RetainPtr< CFX_DIBBase > &pBitmap, int left, int top, uint32_t argb)
void DrawFillRect(const CFX_Matrix *pUser2Device, const CFX_FloatRect &rect, const CFX_Color &color, int32_t nTransparency)
uint32_t m_GlyphIndex
CFX_Matrix GetEffectiveMatrix(const CFX_Matrix &matrix) const
BlendMode
Definition fx_dib.h:49
#define FXARGB_A(argb)
Definition fx_dib.h:124
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition fx_dib.h:118
#define FXARGB_SETDIB(p, argb)
Definition fx_dib.h:137
FXDIB_Format
Definition fx_dib.h:19
#define FXDIB_ALPHA_MERGE(backdrop, source, source_alpha)
Definition fx_dib.h:132
#define FXDC_BITS_PIXEL
#define FXRC_FILLSTROKE_PATH
#define FXDC_RENDER_CAPS
#define FXDC_PIXEL_WIDTH
#define FXRC_ALPHA_OUTPUT
#define FXDC_PIXEL_HEIGHT
#define FXRC_BLEND_MODE
#define FXRC_ALPHA_IMAGE
#define FXRC_GET_BITS
#define FXRC_BYTEMASK_OUTPUT
FX_COLORREF ToFXColor(int32_t nTransparency) const
static constexpr CFX_FillRenderOptions EvenOddOptions()
static constexpr CFX_FillRenderOptions WindingOptions()
constexpr CFX_TextRenderOptions(const CFX_TextRenderOptions &other)=default
int Height() const
int32_t bottom
bool Valid() const
int32_t right
int Width() const
int32_t top
int32_t left
void Intersect(const FX_RECT &src)
bool IsEmpty() const
constexpr FX_RECT(int l, int t, int r, int b)