• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
42 namespace {
43 
GenerateType42SfntData(const ByteString & psname,pdfium::span<const uint8_t> font_data)44 std::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.
86 constexpr size_t kGlyphsPerDescendantFont = 256;
87 
GenerateType42FontDictionary(const ByteString & psname,const FX_RECT & bbox,size_t num_glyphs,size_t glyphs_per_descendant_font)88 ByteString 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 
GenerateType42FontData(const CFX_Font * font)161 ByteString 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 
191 struct CFX_PSRenderer::Glyph {
GlyphCFX_PSRenderer::Glyph192   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 
198   UnownedPtr<CFX_Font> const font;
199   const uint32_t glyph_index;
200   std::optional<std::array<float, 4>> adjust_matrix;
201 };
202 
203 CFX_PSRenderer::FaxCompressResult::FaxCompressResult() = default;
204 
205 CFX_PSRenderer::FaxCompressResult::FaxCompressResult(
206     FaxCompressResult&&) noexcept = default;
207 
208 CFX_PSRenderer::FaxCompressResult& CFX_PSRenderer::FaxCompressResult::operator=(
209     FaxCompressResult&&) noexcept = default;
210 
211 CFX_PSRenderer::FaxCompressResult::~FaxCompressResult() = default;
212 
213 CFX_PSRenderer::PSCompressResult::PSCompressResult() = default;
214 
215 CFX_PSRenderer::PSCompressResult::PSCompressResult(
216     PSCompressResult&&) noexcept = default;
217 
218 CFX_PSRenderer::PSCompressResult& CFX_PSRenderer::PSCompressResult::operator=(
219     PSCompressResult&&) noexcept = default;
220 
221 CFX_PSRenderer::PSCompressResult::~PSCompressResult() = default;
222 
CFX_PSRenderer(CFX_PSFontTracker * font_tracker,const EncoderIface * encoder_iface)223 CFX_PSRenderer::CFX_PSRenderer(CFX_PSFontTracker* font_tracker,
224                                const EncoderIface* encoder_iface)
225     : m_pFontTracker(font_tracker), m_pEncoderIface(encoder_iface) {
226   DCHECK(m_pFontTracker);
227 }
228 
~CFX_PSRenderer()229 CFX_PSRenderer::~CFX_PSRenderer() {
230   EndRendering();
231 }
232 
Init(const RetainPtr<IFX_RetainableWriteStream> & pStream,RenderingLevel level,int width,int height)233 void CFX_PSRenderer::Init(const RetainPtr<IFX_RetainableWriteStream>& pStream,
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 
StartRendering()247 void 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 
EndRendering()269 void 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 
SaveState()292 void CFX_PSRenderer::SaveState() {
293   StartRendering();
294   WriteString("q\n");
295   m_ClipBoxStack.push_back(m_ClipBox);
296 }
297 
RestoreState(bool bKeepSaved)298 void 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 
OutputPath(const CFX_Path & path,const CFX_Matrix * pObject2Device)314 void 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) {
328       case CFX_Path::Point::Type::kMove:
329         buf << " m ";
330         break;
331       case CFX_Path::Point::Type::kLine:
332         buf << " l ";
333         if (closing)
334           buf << "h ";
335         break;
336       case CFX_Path::Point::Type::kBezier: {
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 
SetClip_PathFill(const CFX_Path & path,const CFX_Matrix * pObject2Device,const CFX_FillRenderOptions & fill_options)356 void CFX_PSRenderer::SetClip_PathFill(
357     const CFX_Path& path,
358     const CFX_Matrix* pObject2Device,
359     const CFX_FillRenderOptions& fill_options) {
360   StartRendering();
361   OutputPath(path, pObject2Device);
362   CFX_FloatRect rect = path.GetBoundingBox();
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");
372   if (fill_options.fill_type != CFX_FillRenderOptions::FillType::kWinding)
373     WriteString("*");
374   WriteString(" n\n");
375 }
376 
SetClip_PathStroke(const CFX_Path & path,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState)377 void CFX_PSRenderer::SetClip_PathStroke(const CFX_Path& path,
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);
390   CFX_FloatRect rect = path.GetBoundingBoxForStrokePath(
391       pGraphState->line_width(), pGraphState->miter_limit());
392   m_ClipBox.Intersect(pObject2Device->TransformRect(rect).GetOuterRect());
393 
394   WriteString("strokepath W n sm\n");
395 }
396 
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)397 bool CFX_PSRenderer::DrawPath(const CFX_Path& path,
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);
425   if (fill_options.fill_type != CFX_FillRenderOptions::FillType::kNoFill &&
426       fill_alpha) {
427     SetColor(fill_color);
428     if (fill_options.fill_type == CFX_FillRenderOptions::FillType::kWinding) {
429       if (stroke_alpha)
430         WriteString("q f Q ");
431       else
432         WriteString("f");
433     } else if (fill_options.fill_type ==
434                CFX_FillRenderOptions::FillType::kEvenOdd) {
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 
SetGraphState(const CFX_GraphStateData * pGraphState)453 void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) {
454   fxcrt::ostringstream buf;
455   if (!m_bGraphStateSet ||
456       m_CurGraphState.line_cap() != pGraphState->line_cap()) {
457     buf << static_cast<int>(pGraphState->line_cap()) << " J\n";
458   }
459   if (!m_bGraphStateSet ||
460       m_CurGraphState.dash_array() != pGraphState->dash_array()) {
461     buf << "[";
462     for (float dash : pGraphState->dash_array()) {
463       buf << dash << " ";
464     }
465     buf << "]" << pGraphState->dash_phase() << " d\n";
466   }
467   if (!m_bGraphStateSet ||
468       m_CurGraphState.line_join() != pGraphState->line_join()) {
469     buf << static_cast<int>(pGraphState->line_join()) << " j\n";
470   }
471   if (!m_bGraphStateSet ||
472       m_CurGraphState.line_width() != pGraphState->line_width()) {
473     buf << pGraphState->line_width() << " w\n";
474   }
475   if (!m_bGraphStateSet ||
476       m_CurGraphState.miter_limit() != pGraphState->miter_limit()) {
477     buf << pGraphState->miter_limit() << " M\n";
478   }
479   m_CurGraphState = *pGraphState;
480   m_bGraphStateSet = true;
481   WriteStream(buf);
482 }
483 
SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,uint32_t color,int left,int top)484 bool CFX_PSRenderer::SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
485                                uint32_t color,
486                                int left,
487                                int top) {
488   StartRendering();
489   CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(
490       bitmap->GetWidth(), bitmap->GetHeight(), left, top);
491   return DrawDIBits(std::move(bitmap), color, matrix, FXDIB_ResampleOptions());
492 }
493 
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)494 bool CFX_PSRenderer::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
495                                    uint32_t color,
496                                    int dest_left,
497                                    int dest_top,
498                                    int dest_width,
499                                    int dest_height,
500                                    const FXDIB_ResampleOptions& options) {
501   StartRendering();
502   CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(dest_width, dest_height,
503                                                       dest_left, dest_top);
504   return DrawDIBits(std::move(bitmap), color, matrix, options);
505 }
506 
DrawDIBits(RetainPtr<const CFX_DIBBase> bitmap,uint32_t color,const CFX_Matrix & matrix,const FXDIB_ResampleOptions & options)507 bool CFX_PSRenderer::DrawDIBits(RetainPtr<const CFX_DIBBase> bitmap,
508                                 uint32_t color,
509                                 const CFX_Matrix& matrix,
510                                 const FXDIB_ResampleOptions& options) {
511   StartRendering();
512   if ((matrix.a == 0 && matrix.b == 0) || (matrix.c == 0 && matrix.d == 0))
513     return true;
514 
515   if (bitmap->IsAlphaFormat()) {
516     return false;
517   }
518 
519   int alpha = FXARGB_A(color);
520   if (bitmap->IsMaskFormat() && (alpha < 255 || bitmap->GetBPP() != 1)) {
521     return false;
522   }
523 
524   WriteString("q\n");
525 
526   fxcrt::ostringstream buf;
527   buf << "[" << matrix.a << " " << matrix.b << " " << matrix.c << " "
528       << matrix.d << " " << matrix.e << " " << matrix.f << "]cm ";
529 
530   const int width = bitmap->GetWidth();
531   const int height = bitmap->GetHeight();
532   buf << width << " " << height;
533 
534   if (bitmap->GetBPP() == 1 && !bitmap->HasPalette()) {
535     FaxCompressResult compress_result = FaxCompressData(bitmap);
536     if (compress_result.data.empty())
537       return false;
538 
539     if (bitmap->IsMaskFormat()) {
540       SetColor(color);
541       m_bColorSet = false;
542       buf << " true[";
543     } else {
544       buf << " 1[";
545     }
546     buf << width << " 0 0 -" << height << " 0 " << height
547         << "]currentfile/ASCII85Decode filter ";
548 
549     if (compress_result.compressed) {
550       buf << "<</K -1/EndOfBlock false/Columns " << width << "/Rows " << height
551           << ">>/CCITTFaxDecode filter ";
552     }
553     if (bitmap->IsMaskFormat()) {
554       buf << "iM\n";
555     } else {
556       buf << "false 1 colorimage\n";
557     }
558 
559     WriteStream(buf);
560     WritePSBinary(compress_result.data);
561   } else {
562     switch (bitmap->GetFormat()) {
563       case FXDIB_Format::k1bppRgb:
564       case FXDIB_Format::kBgrx:
565         bitmap = bitmap->ConvertTo(FXDIB_Format::kBgr);
566         break;
567       case FXDIB_Format::k8bppRgb:
568         if (bitmap->HasPalette()) {
569           bitmap = bitmap->ConvertTo(FXDIB_Format::kBgr);
570         }
571         break;
572       case FXDIB_Format::kInvalid:
573       case FXDIB_Format::k1bppMask:
574       case FXDIB_Format::k8bppMask:
575       case FXDIB_Format::kBgr:
576         break;
577       case FXDIB_Format::kBgra:
578 #if defined(PDF_USE_SKIA)
579       case FXDIB_Format::kBgraPremul:
580 #endif
581         // Should have returned early due to IsAlphaFormat() check above.
582         NOTREACHED_NORETURN();
583     }
584     if (!bitmap) {
585       WriteString("\nQ\n");
586       return false;
587     }
588 
589     const int bytes_per_pixel = bitmap->GetBPP() / 8;
590     uint8_t* output_buf = nullptr;
591     size_t output_size = 0;
592     bool output_buf_is_owned = true;
593     std::optional<PSCompressResult> compress_result;
594     ByteString filter;
595     if ((m_Level.value() == RenderingLevel::kLevel2 || options.bLossy) &&
596         m_pEncoderIface->pJpegEncodeFunc(bitmap, &output_buf, &output_size)) {
597       filter = "/DCTDecode filter ";
598     } else {
599       int src_pitch = width * bytes_per_pixel;
600       output_size = height * src_pitch;
601       output_buf = FX_Alloc(uint8_t, output_size);
602       for (int row = 0; row < height; row++) {
603         const uint8_t* src_scan = bitmap->GetScanline(row).data();
604         uint8_t* dest_scan = UNSAFE_TODO(output_buf + row * src_pitch);
605         if (bytes_per_pixel == 3) {
606           UNSAFE_TODO({
607             for (int col = 0; col < width; col++) {
608               *dest_scan++ = src_scan[2];
609               *dest_scan++ = src_scan[1];
610               *dest_scan++ = *src_scan;
611               src_scan += 3;
612             }
613           });
614         } else {
615           UNSAFE_TODO(FXSYS_memcpy(dest_scan, src_scan, src_pitch));
616         }
617       }
618       // SAFETY: `output_size` passed to FX_Alloc() of `output_buf`.
619       compress_result = PSCompressData(
620           UNSAFE_BUFFERS(pdfium::make_span(output_buf, output_size)));
621       if (compress_result.has_value()) {
622         FX_Free(output_buf);
623         output_buf_is_owned = false;
624         output_buf = compress_result.value().data.data();
625         output_size = compress_result.value().data.size();
626         filter = compress_result.value().filter;
627       }
628     }
629     buf << " 8[";
630     buf << width << " 0 0 -" << height << " 0 " << height << "]";
631     buf << "currentfile/ASCII85Decode filter ";
632     if (!filter.IsEmpty())
633       buf << filter;
634 
635     buf << "false " << bytes_per_pixel;
636     buf << " colorimage\n";
637     WriteStream(buf);
638 
639     // SAFETY: `output_size` passed to FX_Alloc() of `output_buf`.
640     WritePSBinary(UNSAFE_BUFFERS(pdfium::make_span(output_buf, output_size)));
641     if (output_buf_is_owned)
642       FX_Free(output_buf);
643   }
644   WriteString("\nQ\n");
645   return true;
646 }
647 
SetColor(uint32_t color)648 void CFX_PSRenderer::SetColor(uint32_t color) {
649   if (m_bColorSet && m_LastColor == color)
650     return;
651 
652   fxcrt::ostringstream buf;
653   buf << FXARGB_R(color) / 255.0 << " " << FXARGB_G(color) / 255.0 << " "
654       << FXARGB_B(color) / 255.0 << " rg\n";
655   m_bColorSet = true;
656   m_LastColor = color;
657   WriteStream(buf);
658 }
659 
FindPSFontGlyph(CFX_GlyphCache * pGlyphCache,CFX_Font * pFont,const TextCharPos & charpos,int * ps_fontnum,int * ps_glyphindex)660 void CFX_PSRenderer::FindPSFontGlyph(CFX_GlyphCache* pGlyphCache,
661                                      CFX_Font* pFont,
662                                      const TextCharPos& charpos,
663                                      int* ps_fontnum,
664                                      int* ps_glyphindex) {
665   for (size_t i = 0; i < m_PSFontList.size(); ++i) {
666     const Glyph& glyph = *m_PSFontList[i];
667     if (glyph.font == pFont && glyph.glyph_index == charpos.m_GlyphIndex &&
668         glyph.adjust_matrix.has_value() == charpos.m_bGlyphAdjust) {
669       bool found;
670       if (glyph.adjust_matrix.has_value()) {
671         constexpr float kEpsilon = 0.01f;
672         const auto& adjust_matrix = glyph.adjust_matrix.value();
673         found = fabs(adjust_matrix[0] - charpos.m_AdjustMatrix[0]) < kEpsilon &&
674                 fabs(adjust_matrix[1] - charpos.m_AdjustMatrix[1]) < kEpsilon &&
675                 fabs(adjust_matrix[2] - charpos.m_AdjustMatrix[2]) < kEpsilon &&
676                 fabs(adjust_matrix[3] - charpos.m_AdjustMatrix[3]) < kEpsilon;
677       } else {
678         found = true;
679       }
680       if (found) {
681         *ps_fontnum = pdfium::checked_cast<int>(i / 256);
682         *ps_glyphindex = i % 256;
683         return;
684       }
685     }
686   }
687 
688   m_PSFontList.push_back(std::make_unique<Glyph>(pFont, charpos.m_GlyphIndex));
689   *ps_fontnum = pdfium::checked_cast<int>((m_PSFontList.size() - 1) / 256);
690   *ps_glyphindex = (m_PSFontList.size() - 1) % 256;
691   if (*ps_glyphindex == 0) {
692     fxcrt::ostringstream buf;
693     buf << "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n"
694            "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding "
695            "exch/.notdef put}for\n"
696            "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n"
697            "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get "
698            "exch 2 copy known not{pop/.notdef}if get exec}bind def\n"
699            "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get "
700            "exec}bind def\n"
701            "currentdict end\n";
702     buf << "/X" << *ps_fontnum << " exch definefont pop\n";
703     WriteStream(buf);
704   }
705 
706   if (charpos.m_bGlyphAdjust) {
707     m_PSFontList.back()->adjust_matrix = std::array<float, 4>{
708         charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
709         charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3]};
710   }
711 
712   CFX_Matrix matrix;
713   if (charpos.m_bGlyphAdjust) {
714     matrix =
715         CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
716                    charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
717   }
718   const CFX_Path* pPath = pGlyphCache->LoadGlyphPath(
719       pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
720   if (!pPath)
721     return;
722 
723   CFX_Path TransformedPath(*pPath);
724   if (charpos.m_bGlyphAdjust)
725     TransformedPath.Transform(matrix);
726 
727   fxcrt::ostringstream buf;
728   buf << "/X" << *ps_fontnum << " Ff/CharProcs get begin/" << *ps_glyphindex
729       << "{n ";
730   for (size_t p = 0; p < TransformedPath.GetPoints().size(); p++) {
731     CFX_PointF point = TransformedPath.GetPoint(p);
732     switch (TransformedPath.GetType(p)) {
733       case CFX_Path::Point::Type::kMove: {
734         buf << point.x << " " << point.y << " m\n";
735         break;
736       }
737       case CFX_Path::Point::Type::kLine: {
738         buf << point.x << " " << point.y << " l\n";
739         break;
740       }
741       case CFX_Path::Point::Type::kBezier: {
742         CFX_PointF point1 = TransformedPath.GetPoint(p + 1);
743         CFX_PointF point2 = TransformedPath.GetPoint(p + 2);
744         buf << point.x << " " << point.y << " " << point1.x << " " << point1.y
745             << " " << point2.x << " " << point2.y << " c\n";
746         p += 2;
747         break;
748       }
749     }
750   }
751   buf << "f}bind def end\n";
752   buf << "/X" << *ps_fontnum << " Ff/Encoding get " << *ps_glyphindex << "/"
753       << *ps_glyphindex << " put\n";
754   WriteStream(buf);
755 }
756 
DrawTextAsType3Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)757 void CFX_PSRenderer::DrawTextAsType3Font(int char_count,
758                                          const TextCharPos* char_pos,
759                                          CFX_Font* font,
760                                          float font_size,
761                                          fxcrt::ostringstream& buf) {
762   CFX_FontCache* pCache = CFX_GEModule::Get()->GetFontCache();
763   RetainPtr<CFX_GlyphCache> pGlyphCache = pCache->GetGlyphCache(font);
764   int last_fontnum = -1;
765   UNSAFE_TODO({
766     for (int i = 0; i < char_count; i++) {
767       int ps_fontnum;
768       int ps_glyphindex;
769       FindPSFontGlyph(pGlyphCache.Get(), font, char_pos[i], &ps_fontnum,
770                       &ps_glyphindex);
771       if (last_fontnum != ps_fontnum) {
772         buf << "/X" << ps_fontnum << " Ff " << font_size << " Fs Sf ";
773         last_fontnum = ps_fontnum;
774       }
775       buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
776       ByteString hex = ByteString::Format("<%02X>", ps_glyphindex);
777       buf << hex.AsStringView() << "Tj\n";
778     }
779   });
780 }
781 
DrawTextAsType42Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)782 bool CFX_PSRenderer::DrawTextAsType42Font(int char_count,
783                                           const TextCharPos* char_pos,
784                                           CFX_Font* font,
785                                           float font_size,
786                                           fxcrt::ostringstream& buf) {
787   if (m_Level != RenderingLevel::kLevel3Type42) {
788     return false;
789   }
790 
791   RetainPtr<CFX_Face> face = font->GetFace();
792   if (!face || !face->CanEmbed()) {
793     return false;
794   }
795 
796   if (font->GetFontType() != CFX_Font::FontType::kCIDTrueType)
797     return false;
798 
799   bool is_existing_font = m_pFontTracker->SeenFontObject(font);
800   if (!is_existing_font) {
801     ByteString font_data = GenerateType42FontData(font);
802     if (font_data.IsEmpty())
803       return false;
804 
805     m_pFontTracker->AddFontObject(font);
806     WritePreambleString(font_data.AsStringView());
807   }
808 
809   buf << "/" << font->GetPsName() << " " << font_size << " selectfont\n";
810   UNSAFE_TODO({
811     for (int i = 0; i < char_count; ++i) {
812       buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
813       uint8_t hi = char_pos[i].m_GlyphIndex / 256;
814       uint8_t lo = char_pos[i].m_GlyphIndex % 256;
815       ByteString hex = ByteString::Format("<%02X%02X>", hi, lo);
816       buf << hex.AsStringView() << "Tj\n";
817     }
818   });
819   return true;
820 }
821 
DrawText(int nChars,const TextCharPos * pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t color)822 bool CFX_PSRenderer::DrawText(int nChars,
823                               const TextCharPos* pCharPos,
824                               CFX_Font* pFont,
825                               const CFX_Matrix& mtObject2Device,
826                               float font_size,
827                               uint32_t color) {
828   // Check object to device matrix first, since it can scale the font size.
829   if ((mtObject2Device.a == 0 && mtObject2Device.b == 0) ||
830       (mtObject2Device.c == 0 && mtObject2Device.d == 0)) {
831     return true;
832   }
833 
834   // Do not send near zero font sizes to printers. See crbug.com/767343.
835   float scale =
836       std::min(mtObject2Device.GetXUnit(), mtObject2Device.GetYUnit());
837   static constexpr float kEpsilon = 0.01f;
838   if (fabsf(font_size * scale) < kEpsilon)
839     return true;
840 
841   StartRendering();
842   int alpha = FXARGB_A(color);
843   if (alpha < 255)
844     return false;
845 
846   SetColor(color);
847   fxcrt::ostringstream buf;
848   buf << "q[" << mtObject2Device.a << " " << mtObject2Device.b << " "
849       << mtObject2Device.c << " " << mtObject2Device.d << " "
850       << mtObject2Device.e << " " << mtObject2Device.f << "]cm\n";
851 
852   if (!DrawTextAsType42Font(nChars, pCharPos, pFont, font_size, buf)) {
853     DrawTextAsType3Font(nChars, pCharPos, pFont, font_size, buf);
854   }
855 
856   buf << "Q\n";
857   WriteStream(buf);
858   return true;
859 }
860 
FaxCompressData(RetainPtr<const CFX_DIBBase> src) const861 CFX_PSRenderer::FaxCompressResult CFX_PSRenderer::FaxCompressData(
862     RetainPtr<const CFX_DIBBase> src) const {
863   DCHECK_EQ(1, src->GetBPP());
864 
865   FaxCompressResult result;
866   const int width = src->GetWidth();
867   const int height = src->GetHeight();
868   const int pitch = src->GetPitch();
869   DCHECK_GE(width, pitch);
870 
871   FX_SAFE_UINT32 safe_pixel_count = width;
872   safe_pixel_count *= height;
873   if (!safe_pixel_count.IsValid())
874     return result;
875 
876   if (safe_pixel_count.ValueOrDie() > 128) {
877     result.data = m_pEncoderIface->pFaxEncodeFunc(std::move(src));
878     result.compressed = true;
879     return result;
880   }
881 
882   FX_SAFE_UINT32 safe_size = pitch;
883   safe_size *= height;
884   result.data.resize(safe_size.ValueOrDie());
885   auto dest_span = pdfium::make_span(result.data);
886   for (int row = 0; row < height; row++) {
887     fxcrt::Copy(src->GetScanline(row), dest_span.subspan(row * pitch, pitch));
888   }
889   return result;
890 }
891 
PSCompressData(pdfium::span<const uint8_t> src_span) const892 std::optional<CFX_PSRenderer::PSCompressResult> CFX_PSRenderer::PSCompressData(
893     pdfium::span<const uint8_t> src_span) const {
894   if (src_span.size() < 1024)
895     return std::nullopt;
896 
897   DataVector<uint8_t> (*encode_func)(pdfium::span<const uint8_t> src_span);
898   ByteString filter;
899   if (m_Level.value() == RenderingLevel::kLevel3 ||
900       m_Level.value() == RenderingLevel::kLevel3Type42) {
901     encode_func = m_pEncoderIface->pFlateEncodeFunc;
902     filter = "/FlateDecode filter ";
903   } else {
904     encode_func = m_pEncoderIface->pRunLengthEncodeFunc;
905     filter = "/RunLengthDecode filter ";
906   }
907 
908   DataVector<uint8_t> decode_result = encode_func(src_span);
909   if (decode_result.size() == 0 || decode_result.size() >= src_span.size())
910     return std::nullopt;
911 
912   PSCompressResult result;
913   result.data = std::move(decode_result);
914   result.filter = filter;
915   return result;
916 }
917 
WritePreambleString(ByteStringView str)918 void CFX_PSRenderer::WritePreambleString(ByteStringView str) {
919   m_PreambleOutput << str;
920 }
921 
WritePSBinary(pdfium::span<const uint8_t> data)922 void CFX_PSRenderer::WritePSBinary(pdfium::span<const uint8_t> data) {
923   DataVector<uint8_t> encoded_data = m_pEncoderIface->pA85EncodeFunc(data);
924   pdfium::span<const uint8_t> result =
925       encoded_data.empty() ? data : encoded_data;
926   auto chars = pdfium::as_chars(result);
927   m_Output.write(chars.data(), chars.size());
928 }
929 
WriteStream(fxcrt::ostringstream & stream)930 void CFX_PSRenderer::WriteStream(fxcrt::ostringstream& stream) {
931   std::streamoff output_pos = stream.tellp();
932   if (output_pos > 0) {
933     m_Output.write(stream.str().c_str(),
934                    pdfium::checked_cast<size_t>(output_pos));
935   }
936 }
937 
WriteString(ByteStringView str)938 void CFX_PSRenderer::WriteString(ByteStringView str) {
939   m_Output << str;
940 }
941 
942 // static
GenerateType42SfntDataForTesting(const ByteString & psname,pdfium::span<const uint8_t> font_data)943 std::optional<ByteString> CFX_PSRenderer::GenerateType42SfntDataForTesting(
944     const ByteString& psname,
945     pdfium::span<const uint8_t> font_data) {
946   return GenerateType42SfntData(psname, font_data);
947 }
948 
949 // static
GenerateType42FontDictionaryForTesting(const ByteString & psname,const FX_RECT & bbox,size_t num_glyphs,size_t glyphs_per_descendant_font)950 ByteString CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
951     const ByteString& psname,
952     const FX_RECT& bbox,
953     size_t num_glyphs,
954     size_t glyphs_per_descendant_font) {
955   return GenerateType42FontDictionary(psname, bbox, num_glyphs,
956                                       glyphs_per_descendant_font);
957 }
958