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_psrenderer.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxge/win32/cfx_psrenderer.h"
8
9#include <math.h>
10
11#include <algorithm>
12#include <array>
13#include <memory>
14#include <string>
15#include <utility>
16
17#include "core/fxcrt/bytestring.h"
18#include "core/fxcrt/check_op.h"
19#include "core/fxcrt/compiler_specific.h"
20#include "core/fxcrt/fx_extension.h"
21#include "core/fxcrt/fx_memcpy_wrappers.h"
22#include "core/fxcrt/fx_memory.h"
23#include "core/fxcrt/fx_safe_types.h"
24#include "core/fxcrt/fx_stream.h"
25#include "core/fxcrt/notreached.h"
26#include "core/fxcrt/numerics/safe_conversions.h"
27#include "core/fxcrt/span.h"
28#include "core/fxcrt/stl_util.h"
29#include "core/fxge/cfx_fillrenderoptions.h"
30#include "core/fxge/cfx_font.h"
31#include "core/fxge/cfx_fontcache.h"
32#include "core/fxge/cfx_gemodule.h"
33#include "core/fxge/cfx_glyphcache.h"
34#include "core/fxge/cfx_path.h"
35#include "core/fxge/cfx_renderdevice.h"
36#include "core/fxge/dib/cfx_dibbase.h"
37#include "core/fxge/dib/cfx_dibitmap.h"
38#include "core/fxge/dib/fx_dib.h"
39#include "core/fxge/text_char_pos.h"
40#include "core/fxge/win32/cfx_psfonttracker.h"
41
42namespace {
43
44std::optional<ByteString> GenerateType42SfntData(
45 const ByteString& psname,
46 pdfium::span<const uint8_t> font_data) {
47 if (font_data.empty())
48 return std::nullopt;
49
50 // Per Type 42 font spec.
51 constexpr size_t kMaxSfntStringSize = 65535;
52 if (font_data.size() > kMaxSfntStringSize) {
53 // TODO(thestig): Fonts that are too big need to be written out in sections.
54 return std::nullopt;
55 }
56
57 // Each byte is written as 2 ASCIIHex characters, so really 64 chars per line.
58 constexpr size_t kMaxBytesPerLine = 32;
59 fxcrt::ostringstream output;
60 output << "/" << psname << "_sfnts [\n<\n";
61 size_t bytes_per_line = 0;
62 char buf[2];
63 for (uint8_t datum : font_data) {
64 FXSYS_IntToTwoHexChars(datum, buf);
65 output << buf[0];
66 output << buf[1];
67 bytes_per_line++;
68 if (bytes_per_line == kMaxBytesPerLine) {
69 output << "\n";
70 bytes_per_line = 0;
71 }
72 }
73
74 // Pad with ASCIIHex NUL character per Type 42 font spec if needed.
75 if (!FX_IsOdd(font_data.size()))
76 output << "00";
77
78 output << "\n>\n] def\n";
79 return ByteString(output);
80}
81
82// The value to use with GenerateType42FontDictionary() below, and the max
83// number of entries supported for non-CID fonts.
84// Also used to avoid buggy fonts by writing out at least this many entries,
85// per note in Poppler's Type 42 generation code.
86constexpr size_t kGlyphsPerDescendantFont = 256;
87
88ByteString GenerateType42FontDictionary(const ByteString& psname,
89 const FX_RECT& bbox,
90 size_t num_glyphs,
91 size_t glyphs_per_descendant_font) {
92 DCHECK_LE(glyphs_per_descendant_font, kGlyphsPerDescendantFont);
93 CHECK_GT(glyphs_per_descendant_font, 0u);
94
95 const size_t descendant_font_count =
96 (num_glyphs + glyphs_per_descendant_font - 1) /
97 glyphs_per_descendant_font;
98
99 fxcrt::ostringstream output;
100 for (size_t i = 0; i < descendant_font_count; ++i) {
101 output << "8 dict begin\n";
102 output << "/FontType 42 def\n";
103 output << "/FontMatrix [1 0 0 1 0 0] def\n";
104 output << "/FontName /" << psname << "_" << i << " def\n";
105
106 output << "/Encoding " << glyphs_per_descendant_font << " array\n";
107 for (size_t j = 0, pos = i * glyphs_per_descendant_font;
108 j < glyphs_per_descendant_font; ++j, ++pos) {
109 if (pos >= num_glyphs)
110 break;
111
112 output << ByteString::Format("dup %d /c%02x put\n", j, j);
113 }
114 output << "readonly def\n";
115
116 // Note: `bbox` is LTRB, while /FontBBox is LBRT. Writing it out as LTRB
117 // gets the correct values.
118 output << "/FontBBox [" << bbox.left << " " << bbox.top << " " << bbox.right
119 << " " << bbox.bottom << "] def\n";
120
121 output << "/PaintType 0 def\n";
122
123 output << "/CharStrings " << glyphs_per_descendant_font + 1
124 << " dict dup begin\n";
125 output << "/.notdef 0 def\n";
126 for (size_t j = 0, pos = i * glyphs_per_descendant_font;
127 j < glyphs_per_descendant_font; ++j, ++pos) {
128 if (pos >= num_glyphs)
129 break;
130
131 output << ByteString::Format("/c%02x %d def\n", j, pos);
132 }
133 output << "end readonly def\n";
134
135 output << "/sfnts " << psname << "_sfnts def\n";
136 output << "FontName currentdict end definefont pop\n";
137 }
138
139 output << "6 dict begin\n";
140 output << "/FontName /" << psname << " def\n";
141 output << "/FontType 0 def\n";
142 output << "/FontMatrix [1 0 0 1 0 0] def\n";
143 output << "/FMapType 2 def\n";
144
145 output << "/Encoding [\n";
146 for (size_t i = 0; i < descendant_font_count; ++i)
147 output << i << "\n";
148 output << "] def\n";
149
150 output << "/FDepVector [\n";
151 for (size_t i = 0; i < descendant_font_count; ++i)
152 output << "/" << psname << "_" << i << " findfont\n";
153 output << "] def\n";
154
155 output << "FontName currentdict end definefont pop\n";
156 output << "%%EndResource\n";
157
158 return ByteString(output);
159}
160
161ByteString GenerateType42FontData(const CFX_Font* font) {
162 RetainPtr<const CFX_Face> face = font->GetFace();
163 if (!face) {
164 return ByteString();
165 }
166
167 int num_glyphs = face->GetGlyphCount();
168 if (num_glyphs < 0) {
169 return ByteString();
170 }
171
172 const ByteString psname = font->GetPsName();
173 DCHECK(!psname.IsEmpty());
174
175 std::optional<ByteString> sfnt_data =
176 GenerateType42SfntData(psname, font->GetFontSpan());
177 if (!sfnt_data.has_value())
178 return ByteString();
179
180 ByteString output = "%%BeginResource: font ";
181 output += psname;
182 output += "\n";
183 output += sfnt_data.value();
184 output += GenerateType42FontDictionary(psname, font->GetRawBBox().value(),
185 num_glyphs, kGlyphsPerDescendantFont);
186 return output;
187}
188
189} // namespace
190
192 Glyph(CFX_Font* font, uint32_t glyph_index)
193 : font(font), glyph_index(glyph_index) {}
194 Glyph(const Glyph& other) = delete;
195 Glyph& operator=(const Glyph&) = delete;
196 ~Glyph() = default;
197
199 const uint32_t glyph_index;
200 std::optional<std::array<float, 4>> adjust_matrix;
201};
202
203CFX_PSRenderer::FaxCompressResult::FaxCompressResult() = default;
204
205CFX_PSRenderer::FaxCompressResult::FaxCompressResult(
206 FaxCompressResult&&) noexcept = default;
207
208CFX_PSRenderer::FaxCompressResult& CFX_PSRenderer::FaxCompressResult::operator=(
209 FaxCompressResult&&) noexcept = default;
210
211CFX_PSRenderer::FaxCompressResult::~FaxCompressResult() = default;
212
213CFX_PSRenderer::PSCompressResult::PSCompressResult() = default;
214
215CFX_PSRenderer::PSCompressResult::PSCompressResult(
216 PSCompressResult&&) noexcept = default;
217
218CFX_PSRenderer::PSCompressResult& CFX_PSRenderer::PSCompressResult::operator=(
219 PSCompressResult&&) noexcept = default;
220
221CFX_PSRenderer::PSCompressResult::~PSCompressResult() = default;
222
224 const EncoderIface* encoder_iface)
226 DCHECK(m_pFontTracker);
227}
228
230 EndRendering();
231}
232
234 RenderingLevel level,
235 int width,
236 int height) {
237 DCHECK(pStream);
238
239 m_Level = level;
240 m_pStream = pStream;
241 m_ClipBox.left = 0;
242 m_ClipBox.top = 0;
243 m_ClipBox.right = width;
244 m_ClipBox.bottom = height;
245}
246
247void CFX_PSRenderer::StartRendering() {
248 if (m_bInited)
249 return;
250
251 static const char kInitStr[] =
252 "\nsave\n/im/initmatrix load def\n"
253 "/n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load "
254 "def/h/closepath load def\n"
255 "/f/fill load def/F/eofill load def/s/stroke load def/W/clip load "
256 "def/W*/eoclip load def\n"
257 "/rg/setrgbcolor load def/k/setcmykcolor load def\n"
258 "/J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load "
259 "def/M/setmiterlimit load def/d/setdash load def\n"
260 "/q/gsave load def/Q/grestore load def/iM/imagemask load def\n"
261 "/Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont "
262 "load def\n"
263 "/cm/concat load def/Cm/currentmatrix load def/mx/matrix load "
264 "def/sm/setmatrix load def\n";
265 WriteString(kInitStr);
266 m_bInited = true;
267}
268
269void CFX_PSRenderer::EndRendering() {
270 if (!m_bInited)
271 return;
272
273 WriteString("\nrestore\n");
274 m_bInited = false;
275
276 // Flush `m_PreambleOutput` if it is not empty.
277 std::streamoff preamble_pos = m_PreambleOutput.tellp();
278 if (preamble_pos > 0) {
279 m_pStream->WriteBlock(
280 pdfium::as_byte_span(m_PreambleOutput.str())
281 .first(pdfium::checked_cast<size_t>(preamble_pos)));
282 m_PreambleOutput.str("");
283 }
284
285 // Flush `m_Output`. It's never empty because of the WriteString() call above.
286 m_pStream->WriteBlock(pdfium::as_byte_span(m_Output.str())
287 .first(pdfium::checked_cast<size_t>(
288 std::streamoff(m_Output.tellp()))));
289 m_Output.str("");
290}
291
293 StartRendering();
294 WriteString("q\n");
295 m_ClipBoxStack.push_back(m_ClipBox);
296}
297
298void CFX_PSRenderer::RestoreState(bool bKeepSaved) {
299 StartRendering();
300 WriteString("Q\n");
301 if (bKeepSaved)
302 WriteString("q\n");
303
304 m_bColorSet = false;
305 m_bGraphStateSet = false;
306 if (m_ClipBoxStack.empty())
307 return;
308
309 m_ClipBox = m_ClipBoxStack.back();
310 if (!bKeepSaved)
311 m_ClipBoxStack.pop_back();
312}
313
314void CFX_PSRenderer::OutputPath(const CFX_Path& path,
315 const CFX_Matrix* pObject2Device) {
316 fxcrt::ostringstream buf;
317 size_t size = path.GetPoints().size();
318
319 for (size_t i = 0; i < size; i++) {
320 CFX_Path::Point::Type type = path.GetType(i);
321 bool closing = path.IsClosingFigure(i);
322 CFX_PointF pos = path.GetPoint(i);
323 if (pObject2Device)
324 pos = pObject2Device->Transform(pos);
325
326 buf << pos.x << " " << pos.y;
327 switch (type) {
329 buf << " m ";
330 break;
332 buf << " l ";
333 if (closing)
334 buf << "h ";
335 break;
337 CFX_PointF pos1 = path.GetPoint(i + 1);
338 CFX_PointF pos2 = path.GetPoint(i + 2);
339 if (pObject2Device) {
340 pos1 = pObject2Device->Transform(pos1);
341 pos2 = pObject2Device->Transform(pos2);
342 }
343 buf << " " << pos1.x << " " << pos1.y << " " << pos2.x << " " << pos2.y
344 << " c";
345 if (closing)
346 buf << " h";
347 buf << "\n";
348 i += 2;
349 break;
350 }
351 }
352 }
353 WriteStream(buf);
354}
355
357 const CFX_Path& path,
358 const CFX_Matrix* pObject2Device,
359 const CFX_FillRenderOptions& fill_options) {
360 StartRendering();
361 OutputPath(path, pObject2Device);
363 if (pObject2Device)
364 rect = pObject2Device->TransformRect(rect);
365
366 m_ClipBox.left = static_cast<int>(rect.left);
367 m_ClipBox.right = static_cast<int>(rect.left + rect.right);
368 m_ClipBox.top = static_cast<int>(rect.top + rect.bottom);
369 m_ClipBox.bottom = static_cast<int>(rect.bottom);
370
371 WriteString("W");
373 WriteString("*");
374 WriteString(" n\n");
375}
376
378 const CFX_Matrix* pObject2Device,
379 const CFX_GraphStateData* pGraphState) {
380 StartRendering();
381 SetGraphState(pGraphState);
382
383 fxcrt::ostringstream buf;
384 buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " "
385 << pObject2Device->c << " " << pObject2Device->d << " "
386 << pObject2Device->e << " " << pObject2Device->f << "]cm ";
387 WriteStream(buf);
388
389 OutputPath(path, nullptr);
391 pGraphState->m_LineWidth, pGraphState->m_MiterLimit);
392 m_ClipBox.Intersect(pObject2Device->TransformRect(rect).GetOuterRect());
393
394 WriteString("strokepath W n sm\n");
395}
396
398 const CFX_Matrix* pObject2Device,
399 const CFX_GraphStateData* pGraphState,
400 uint32_t fill_color,
401 uint32_t stroke_color,
402 const CFX_FillRenderOptions& fill_options) {
403 StartRendering();
404 int fill_alpha = FXARGB_A(fill_color);
405 int stroke_alpha = FXARGB_A(stroke_color);
406 if (fill_alpha && fill_alpha < 255)
407 return false;
408 if (stroke_alpha && stroke_alpha < 255)
409 return false;
410 if (fill_alpha == 0 && stroke_alpha == 0)
411 return false;
412
413 if (stroke_alpha) {
414 SetGraphState(pGraphState);
415 if (pObject2Device) {
416 fxcrt::ostringstream buf;
417 buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " "
418 << pObject2Device->c << " " << pObject2Device->d << " "
419 << pObject2Device->e << " " << pObject2Device->f << "]cm ";
420 WriteStream(buf);
421 }
422 }
423
424 OutputPath(path, stroke_alpha ? nullptr : pObject2Device);
426 fill_alpha) {
427 SetColor(fill_color);
429 if (stroke_alpha)
430 WriteString("q f Q ");
431 else
432 WriteString("f");
433 } else if (fill_options.fill_type ==
435 if (stroke_alpha)
436 WriteString("q F Q ");
437 else
438 WriteString("F");
439 }
440 }
441
442 if (stroke_alpha) {
443 SetColor(stroke_color);
444 WriteString("s");
445 if (pObject2Device)
446 WriteString(" sm");
447 }
448
449 WriteString("\n");
450 return true;
451}
452
453void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) {
454 fxcrt::ostringstream buf;
455 if (!m_bGraphStateSet ||
456 m_CurGraphState.m_LineCap != pGraphState->m_LineCap) {
457 buf << static_cast<int>(pGraphState->m_LineCap) << " J\n";
458 }
459 if (!m_bGraphStateSet ||
460 m_CurGraphState.m_DashArray != pGraphState->m_DashArray) {
461 buf << "[";
462 for (const auto& dash : pGraphState->m_DashArray)
463 buf << dash << " ";
464 buf << "]" << pGraphState->m_DashPhase << " d\n";
465 }
466 if (!m_bGraphStateSet ||
467 m_CurGraphState.m_LineJoin != pGraphState->m_LineJoin) {
468 buf << static_cast<int>(pGraphState->m_LineJoin) << " j\n";
469 }
470 if (!m_bGraphStateSet ||
471 m_CurGraphState.m_LineWidth != pGraphState->m_LineWidth) {
472 buf << pGraphState->m_LineWidth << " w\n";
473 }
474 if (!m_bGraphStateSet ||
475 m_CurGraphState.m_MiterLimit != pGraphState->m_MiterLimit) {
476 buf << pGraphState->m_MiterLimit << " M\n";
477 }
478 m_CurGraphState = *pGraphState;
479 m_bGraphStateSet = true;
480 WriteStream(buf);
481}
482
484 uint32_t color,
485 int left,
486 int top) {
487 StartRendering();
489 bitmap->GetWidth(), bitmap->GetHeight(), left, top);
490 return DrawDIBits(std::move(bitmap), color, matrix, FXDIB_ResampleOptions());
491}
492
494 uint32_t color,
495 int dest_left,
496 int dest_top,
497 int dest_width,
498 int dest_height,
499 const FXDIB_ResampleOptions& options) {
500 StartRendering();
501 CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(dest_width, dest_height,
502 dest_left, dest_top);
503 return DrawDIBits(std::move(bitmap), color, matrix, options);
504}
505
507 uint32_t color,
508 const CFX_Matrix& matrix,
509 const FXDIB_ResampleOptions& options) {
510 StartRendering();
511 if ((matrix.a == 0 && matrix.b == 0) || (matrix.c == 0 && matrix.d == 0))
512 return true;
513
514 if (bitmap->IsAlphaFormat()) {
515 return false;
516 }
517
518 int alpha = FXARGB_A(color);
519 if (bitmap->IsMaskFormat() && (alpha < 255 || bitmap->GetBPP() != 1)) {
520 return false;
521 }
522
523 WriteString("q\n");
524
525 fxcrt::ostringstream buf;
526 buf << "[" << matrix.a << " " << matrix.b << " " << matrix.c << " "
527 << matrix.d << " " << matrix.e << " " << matrix.f << "]cm ";
528
529 const int width = bitmap->GetWidth();
530 const int height = bitmap->GetHeight();
531 buf << width << " " << height;
532
533 if (bitmap->GetBPP() == 1 && !bitmap->HasPalette()) {
534 FaxCompressResult compress_result = FaxCompressData(bitmap);
535 if (compress_result.data.empty())
536 return false;
537
538 if (bitmap->IsMaskFormat()) {
539 SetColor(color);
540 m_bColorSet = false;
541 buf << " true[";
542 } else {
543 buf << " 1[";
544 }
545 buf << width << " 0 0 -" << height << " 0 " << height
546 << "]currentfile/ASCII85Decode filter ";
547
548 if (compress_result.compressed) {
549 buf << "<</K -1/EndOfBlock false/Columns " << width << "/Rows " << height
550 << ">>/CCITTFaxDecode filter ";
551 }
552 if (bitmap->IsMaskFormat()) {
553 buf << "iM\n";
554 } else {
555 buf << "false 1 colorimage\n";
556 }
557
558 WriteStream(buf);
559 WritePSBinary(compress_result.data);
560 } else {
561 switch (bitmap->GetFormat()) {
564 bitmap = bitmap->ConvertTo(FXDIB_Format::kBgr);
565 break;
567 if (bitmap->HasPalette()) {
568 bitmap = bitmap->ConvertTo(FXDIB_Format::kBgr);
569 }
570 break;
575 break;
577#if defined(PDF_USE_SKIA)
578 case FXDIB_Format::kBgraPremul:
579#endif
580 // Should have returned early due to IsAlphaFormat() check above.
582 }
583 if (!bitmap) {
584 WriteString("\nQ\n");
585 return false;
586 }
587
588 const int bytes_per_pixel = bitmap->GetBPP() / 8;
589 uint8_t* output_buf = nullptr;
590 size_t output_size = 0;
591 bool output_buf_is_owned = true;
592 std::optional<PSCompressResult> compress_result;
593 ByteString filter;
594 if ((m_Level.value() == RenderingLevel::kLevel2 || options.bLossy) &&
595 m_pEncoderIface->pJpegEncodeFunc(bitmap, &output_buf, &output_size)) {
596 filter = "/DCTDecode filter ";
597 } else {
598 int src_pitch = width * bytes_per_pixel;
599 output_size = height * src_pitch;
600 output_buf = FX_Alloc(uint8_t, output_size);
601 for (int row = 0; row < height; row++) {
602 const uint8_t* src_scan = bitmap->GetScanline(row).data();
603 uint8_t* dest_scan = UNSAFE_TODO(output_buf + row * src_pitch);
604 if (bytes_per_pixel == 3) {
606 for (int col = 0; col < width; col++) {
607 *dest_scan++ = src_scan[2];
608 *dest_scan++ = src_scan[1];
609 *dest_scan++ = *src_scan;
610 src_scan += 3;
611 }
612 });
613 } else {
614 UNSAFE_TODO(FXSYS_memcpy(dest_scan, src_scan, src_pitch));
615 }
616 }
617 // SAFETY: `output_size` passed to FX_Alloc() of `output_buf`.
618 compress_result = PSCompressData(
619 UNSAFE_BUFFERS(pdfium::make_span(output_buf, output_size)));
620 if (compress_result.has_value()) {
621 FX_Free(output_buf);
622 output_buf_is_owned = false;
623 output_buf = compress_result.value().data.data();
624 output_size = compress_result.value().data.size();
625 filter = compress_result.value().filter;
626 }
627 }
628 buf << " 8[";
629 buf << width << " 0 0 -" << height << " 0 " << height << "]";
630 buf << "currentfile/ASCII85Decode filter ";
631 if (!filter.IsEmpty())
632 buf << filter;
633
634 buf << "false " << bytes_per_pixel;
635 buf << " colorimage\n";
636 WriteStream(buf);
637
638 // SAFETY: `output_size` passed to FX_Alloc() of `output_buf`.
639 WritePSBinary(UNSAFE_BUFFERS(pdfium::make_span(output_buf, output_size)));
640 if (output_buf_is_owned)
641 FX_Free(output_buf);
642 }
643 WriteString("\nQ\n");
644 return true;
645}
646
647void CFX_PSRenderer::SetColor(uint32_t color) {
648 if (m_bColorSet && m_LastColor == color)
649 return;
650
651 fxcrt::ostringstream buf;
652 buf << FXARGB_R(color) / 255.0 << " " << FXARGB_G(color) / 255.0 << " "
653 << FXARGB_B(color) / 255.0 << " rg\n";
654 m_bColorSet = true;
655 m_LastColor = color;
656 WriteStream(buf);
657}
658
659void CFX_PSRenderer::FindPSFontGlyph(CFX_GlyphCache* pGlyphCache,
660 CFX_Font* pFont,
661 const TextCharPos& charpos,
662 int* ps_fontnum,
663 int* ps_glyphindex) {
664 for (size_t i = 0; i < m_PSFontList.size(); ++i) {
665 const Glyph& glyph = *m_PSFontList[i];
666 if (glyph.font == pFont && glyph.glyph_index == charpos.m_GlyphIndex &&
667 glyph.adjust_matrix.has_value() == charpos.m_bGlyphAdjust) {
668 bool found;
669 if (glyph.adjust_matrix.has_value()) {
670 constexpr float kEpsilon = 0.01f;
671 const auto& adjust_matrix = glyph.adjust_matrix.value();
672 found = fabs(adjust_matrix[0] - charpos.m_AdjustMatrix[0]) < kEpsilon &&
673 fabs(adjust_matrix[1] - charpos.m_AdjustMatrix[1]) < kEpsilon &&
674 fabs(adjust_matrix[2] - charpos.m_AdjustMatrix[2]) < kEpsilon &&
675 fabs(adjust_matrix[3] - charpos.m_AdjustMatrix[3]) < kEpsilon;
676 } else {
677 found = true;
678 }
679 if (found) {
680 *ps_fontnum = pdfium::checked_cast<int>(i / 256);
681 *ps_glyphindex = i % 256;
682 return;
683 }
684 }
685 }
686
687 m_PSFontList.push_back(std::make_unique<Glyph>(pFont, charpos.m_GlyphIndex));
688 *ps_fontnum = pdfium::checked_cast<int>((m_PSFontList.size() - 1) / 256);
689 *ps_glyphindex = (m_PSFontList.size() - 1) % 256;
690 if (*ps_glyphindex == 0) {
691 fxcrt::ostringstream buf;
692 buf << "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n"
693 "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding "
694 "exch/.notdef put}for\n"
695 "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n"
696 "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get "
697 "exch 2 copy known not{pop/.notdef}if get exec}bind def\n"
698 "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get "
699 "exec}bind def\n"
700 "currentdict end\n";
701 buf << "/X" << *ps_fontnum << " exch definefont pop\n";
702 WriteStream(buf);
703 }
704
705 if (charpos.m_bGlyphAdjust) {
706 m_PSFontList.back()->adjust_matrix = std::array<float, 4>{
707 charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
708 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3]};
709 }
710
711 CFX_Matrix matrix;
712 if (charpos.m_bGlyphAdjust) {
713 matrix =
714 CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
715 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
716 }
717 const CFX_Path* pPath = pGlyphCache->LoadGlyphPath(
718 pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
719 if (!pPath)
720 return;
721
722 CFX_Path TransformedPath(*pPath);
723 if (charpos.m_bGlyphAdjust)
724 TransformedPath.Transform(matrix);
725
726 fxcrt::ostringstream buf;
727 buf << "/X" << *ps_fontnum << " Ff/CharProcs get begin/" << *ps_glyphindex
728 << "{n ";
729 for (size_t p = 0; p < TransformedPath.GetPoints().size(); p++) {
730 CFX_PointF point = TransformedPath.GetPoint(p);
731 switch (TransformedPath.GetType(p)) {
733 buf << point.x << " " << point.y << " m\n";
734 break;
735 }
737 buf << point.x << " " << point.y << " l\n";
738 break;
739 }
741 CFX_PointF point1 = TransformedPath.GetPoint(p + 1);
742 CFX_PointF point2 = TransformedPath.GetPoint(p + 2);
743 buf << point.x << " " << point.y << " " << point1.x << " " << point1.y
744 << " " << point2.x << " " << point2.y << " c\n";
745 p += 2;
746 break;
747 }
748 }
749 }
750 buf << "f}bind def end\n";
751 buf << "/X" << *ps_fontnum << " Ff/Encoding get " << *ps_glyphindex << "/"
752 << *ps_glyphindex << " put\n";
753 WriteStream(buf);
754}
755
756void CFX_PSRenderer::DrawTextAsType3Font(int char_count,
757 const TextCharPos* char_pos,
758 CFX_Font* font,
759 float font_size,
760 fxcrt::ostringstream& buf) {
762 RetainPtr<CFX_GlyphCache> pGlyphCache = pCache->GetGlyphCache(font);
763 int last_fontnum = -1;
765 for (int i = 0; i < char_count; i++) {
766 int ps_fontnum;
767 int ps_glyphindex;
768 FindPSFontGlyph(pGlyphCache.Get(), font, char_pos[i], &ps_fontnum,
769 &ps_glyphindex);
770 if (last_fontnum != ps_fontnum) {
771 buf << "/X" << ps_fontnum << " Ff " << font_size << " Fs Sf ";
772 last_fontnum = ps_fontnum;
773 }
774 buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
775 ByteString hex = ByteString::Format("<%02X>", ps_glyphindex);
776 buf << hex.AsStringView() << "Tj\n";
777 }
778 });
779}
780
781bool CFX_PSRenderer::DrawTextAsType42Font(int char_count,
782 const TextCharPos* char_pos,
783 CFX_Font* font,
784 float font_size,
785 fxcrt::ostringstream& buf) {
786 if (m_Level != RenderingLevel::kLevel3Type42) {
787 return false;
788 }
789
790 RetainPtr<CFX_Face> face = font->GetFace();
791 if (!face || !face->CanEmbed()) {
792 return false;
793 }
794
796 return false;
797
798 bool is_existing_font = m_pFontTracker->SeenFontObject(font);
799 if (!is_existing_font) {
800 ByteString font_data = GenerateType42FontData(font);
801 if (font_data.IsEmpty())
802 return false;
803
804 m_pFontTracker->AddFontObject(font);
805 WritePreambleString(font_data.AsStringView());
806 }
807
808 buf << "/" << font->GetPsName() << " " << font_size << " selectfont\n";
810 for (int i = 0; i < char_count; ++i) {
811 buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
812 uint8_t hi = char_pos[i].m_GlyphIndex / 256;
813 uint8_t lo = char_pos[i].m_GlyphIndex % 256;
814 ByteString hex = ByteString::Format("<%02X%02X>", hi, lo);
815 buf << hex.AsStringView() << "Tj\n";
816 }
817 });
818 return true;
819}
820
821bool CFX_PSRenderer::DrawText(int nChars,
822 const TextCharPos* pCharPos,
823 CFX_Font* pFont,
824 const CFX_Matrix& mtObject2Device,
825 float font_size,
826 uint32_t color) {
827 // Check object to device matrix first, since it can scale the font size.
828 if ((mtObject2Device.a == 0 && mtObject2Device.b == 0) ||
829 (mtObject2Device.c == 0 && mtObject2Device.d == 0)) {
830 return true;
831 }
832
833 // Do not send near zero font sizes to printers. See crbug.com/767343.
834 float scale =
835 std::min(mtObject2Device.GetXUnit(), mtObject2Device.GetYUnit());
836 static constexpr float kEpsilon = 0.01f;
837 if (fabsf(font_size * scale) < kEpsilon)
838 return true;
839
840 StartRendering();
841 int alpha = FXARGB_A(color);
842 if (alpha < 255)
843 return false;
844
845 SetColor(color);
846 fxcrt::ostringstream buf;
847 buf << "q[" << mtObject2Device.a << " " << mtObject2Device.b << " "
848 << mtObject2Device.c << " " << mtObject2Device.d << " "
849 << mtObject2Device.e << " " << mtObject2Device.f << "]cm\n";
850
851 if (!DrawTextAsType42Font(nChars, pCharPos, pFont, font_size, buf)) {
852 DrawTextAsType3Font(nChars, pCharPos, pFont, font_size, buf);
853 }
854
855 buf << "Q\n";
856 WriteStream(buf);
857 return true;
858}
859
860CFX_PSRenderer::FaxCompressResult CFX_PSRenderer::FaxCompressData(
861 RetainPtr<const CFX_DIBBase> src) const {
862 DCHECK_EQ(1, src->GetBPP());
863
864 FaxCompressResult result;
865 const int width = src->GetWidth();
866 const int height = src->GetHeight();
867 const int pitch = src->GetPitch();
868 DCHECK_GE(width, pitch);
869
870 FX_SAFE_UINT32 safe_pixel_count = width;
871 safe_pixel_count *= height;
872 if (!safe_pixel_count.IsValid())
873 return result;
874
875 if (safe_pixel_count.ValueOrDie() > 128) {
876 result.data = m_pEncoderIface->pFaxEncodeFunc(std::move(src));
877 result.compressed = true;
878 return result;
879 }
880
881 FX_SAFE_UINT32 safe_size = pitch;
882 safe_size *= height;
883 result.data.resize(safe_size.ValueOrDie());
884 auto dest_span = pdfium::make_span(result.data);
885 for (int row = 0; row < height; row++) {
886 fxcrt::Copy(src->GetScanline(row), dest_span.subspan(row * pitch, pitch));
887 }
888 return result;
889}
890
891std::optional<CFX_PSRenderer::PSCompressResult> CFX_PSRenderer::PSCompressData(
892 pdfium::span<const uint8_t> src_span) const {
893 if (src_span.size() < 1024)
894 return std::nullopt;
895
896 DataVector<uint8_t> (*encode_func)(pdfium::span<const uint8_t> src_span);
897 ByteString filter;
898 if (m_Level.value() == RenderingLevel::kLevel3 ||
899 m_Level.value() == RenderingLevel::kLevel3Type42) {
900 encode_func = m_pEncoderIface->pFlateEncodeFunc;
901 filter = "/FlateDecode filter ";
902 } else {
903 encode_func = m_pEncoderIface->pRunLengthEncodeFunc;
904 filter = "/RunLengthDecode filter ";
905 }
906
907 DataVector<uint8_t> decode_result = encode_func(src_span);
908 if (decode_result.size() == 0 || decode_result.size() >= src_span.size())
909 return std::nullopt;
910
911 PSCompressResult result;
912 result.data = std::move(decode_result);
913 result.filter = filter;
914 return result;
915}
916
917void CFX_PSRenderer::WritePreambleString(ByteStringView str) {
918 m_PreambleOutput << str;
919}
920
921void CFX_PSRenderer::WritePSBinary(pdfium::span<const uint8_t> data) {
922 DataVector<uint8_t> encoded_data = m_pEncoderIface->pA85EncodeFunc(data);
923 pdfium::span<const uint8_t> result =
924 encoded_data.empty() ? data : encoded_data;
925 auto chars = pdfium::as_chars(result);
926 m_Output.write(chars.data(), chars.size());
927}
928
929void CFX_PSRenderer::WriteStream(fxcrt::ostringstream& stream) {
930 std::streamoff output_pos = stream.tellp();
931 if (output_pos > 0) {
932 m_Output.write(stream.str().c_str(),
933 pdfium::checked_cast<size_t>(output_pos));
934 }
935}
936
937void CFX_PSRenderer::WriteString(ByteStringView str) {
938 m_Output << str;
939}
940
941// static
942std::optional<ByteString> CFX_PSRenderer::GenerateType42SfntDataForTesting(
943 const ByteString& psname,
944 pdfium::span<const uint8_t> font_data) {
945 return GenerateType42SfntData(psname, font_data);
946}
947
948// static
949ByteString CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
950 const ByteString& psname,
951 const FX_RECT& bbox,
952 size_t num_glyphs,
953 size_t glyphs_per_descendant_font) {
954 return GenerateType42FontDictionary(psname, bbox, num_glyphs,
955 glyphs_per_descendant_font);
956}
fxcrt::ByteString ByteString
Definition bytestring.h:180
#define DCHECK
Definition check.h:33
#define DCHECK_GE(x, y)
Definition check_op.h:22
#define CHECK_GT(x, y)
Definition check_op.h:13
#define DCHECK_LE(x, y)
Definition check_op.h:21
#define DCHECK_EQ(x, y)
Definition check_op.h:17
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
FX_RECT GetOuterRect() const
ByteString GetPsName() const
Definition cfx_font.cpp:376
FontType GetFontType() const
Definition cfx_font.h:120
static CFX_GEModule * Get()
CFX_FontCache * GetFontCache() const
const CFX_Path * LoadGlyphPath(const CFX_Font *pFont, uint32_t glyph_index, int dest_width)
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_FloatRect TransformRect(const CFX_FloatRect &rect) const
CFX_PointF Transform(const CFX_PointF &point) const
float GetXUnit() const
float GetYUnit() const
bool DrawDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options)
bool SetDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, int dest_left, int dest_top)
void SetClip_PathStroke(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState)
bool StretchDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t color, int dest_left, int dest_top, int dest_width, int dest_height, const FXDIB_ResampleOptions &options)
bool DrawText(int nChars, const TextCharPos *pCharPos, CFX_Font *pFont, const CFX_Matrix &mtObject2Device, float font_size, uint32_t color)
CFX_PSRenderer(CFX_PSFontTracker *font_tracker, const EncoderIface *encoder_iface)
void SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options)
void Init(const RetainPtr< IFX_RetainableWriteStream > &stream, RenderingLevel level, int width, int height)
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)
void RestoreState(bool bKeepSaved)
CFX_FloatRect GetBoundingBox() const
Definition cfx_path.cpp:323
void Transform(const CFX_Matrix &matrix)
Definition cfx_path.cpp:393
CFX_FloatRect GetBoundingBoxForStrokePath(float line_width, float miter_limit) const
Definition cfx_path.cpp:333
CFX_Path(const CFX_Path &src)
static CFX_Matrix GetFlipMatrix(float width, float height, float left, float top)
uint32_t m_GlyphIndex
bool m_bGlyphAdjust
static ByteString Format(const char *pFormat,...)
ByteString & operator+=(const ByteString &str)
ByteString()=default
ByteString & operator+=(const char *str)
ByteString & operator=(const char *str)
#define UNSAFE_BUFFERS(...)
#define UNSAFE_TODO(...)
CFX_PTemplate< float > CFX_PointF
#define FXARGB_B(argb)
Definition fx_dib.h:199
#define FXARGB_G(argb)
Definition fx_dib.h:198
#define FXARGB_A(argb)
Definition fx_dib.h:196
#define FXARGB_R(argb)
Definition fx_dib.h:197
FXDIB_Format
Definition fx_dib.h:21
#define FX_IsOdd(a)
UNSAFE_BUFFER_USAGE void * FXSYS_memcpy(void *ptr1, const void *ptr2, size_t len)
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32
#define NOTREACHED_NORETURN()
Definition notreached.h:22
fxcrt::ByteStringView ByteStringView
Glyph(CFX_Font *font, uint32_t glyph_index)
UnownedPtr< CFX_Font > const font
Glyph & operator=(const Glyph &)=delete
Glyph(const Glyph &other)=delete
std::optional< std::array< float, 4 > > adjust_matrix
int32_t bottom
int32_t right
int32_t top
int32_t left
void Intersect(const FX_RECT &src)