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