xref: /aosp_15_r20/external/pdfium/core/fxge/win32/cfx_psrenderer.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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/freetype/fx_freetype.h"
36 #include "core/fxge/text_char_pos.h"
37 #include "core/fxge/win32/cfx_psfonttracker.h"
38 #include "third_party/base/check_op.h"
39 #include "third_party/base/numerics/safe_conversions.h"
40 
41 namespace {
42 
CanEmbed(CFX_Font * font)43 bool CanEmbed(CFX_Font* font) {
44   FT_UShort fstype = FT_Get_FSType_Flags(font->GetFaceRec());
45   return (fstype & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
46                     FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
47 }
48 
GenerateType42SfntData(const ByteString & psname,pdfium::span<const uint8_t> font_data)49 absl::optional<ByteString> GenerateType42SfntData(
50     const ByteString& psname,
51     pdfium::span<const uint8_t> font_data) {
52   if (font_data.empty())
53     return absl::nullopt;
54 
55   // Per Type 42 font spec.
56   constexpr size_t kMaxSfntStringSize = 65535;
57   if (font_data.size() > kMaxSfntStringSize) {
58     // TODO(thestig): Fonts that are too big need to be written out in sections.
59     return absl::nullopt;
60   }
61 
62   // Each byte is written as 2 ASCIIHex characters, so really 64 chars per line.
63   constexpr size_t kMaxBytesPerLine = 32;
64   fxcrt::ostringstream output;
65   output << "/" << psname << "_sfnts [\n<\n";
66   size_t bytes_per_line = 0;
67   char buf[2];
68   for (uint8_t datum : font_data) {
69     FXSYS_IntToTwoHexChars(datum, buf);
70     output << buf[0];
71     output << buf[1];
72     bytes_per_line++;
73     if (bytes_per_line == kMaxBytesPerLine) {
74       output << "\n";
75       bytes_per_line = 0;
76     }
77   }
78 
79   // Pad with ASCIIHex NUL character per Type 42 font spec if needed.
80   if (!FX_IsOdd(font_data.size()))
81     output << "00";
82 
83   output << "\n>\n] def\n";
84   return ByteString(output);
85 }
86 
87 // The value to use with GenerateType42FontDictionary() below, and the max
88 // number of entries supported for non-CID fonts.
89 // Also used to avoid buggy fonts by writing out at least this many entries,
90 // per note in Poppler's Type 42 generation code.
91 constexpr size_t kGlyphsPerDescendantFont = 256;
92 
GenerateType42FontDictionary(const ByteString & psname,const FX_RECT & bbox,size_t num_glyphs,size_t glyphs_per_descendant_font)93 ByteString GenerateType42FontDictionary(const ByteString& psname,
94                                         const FX_RECT& bbox,
95                                         size_t num_glyphs,
96                                         size_t glyphs_per_descendant_font) {
97   DCHECK_LE(glyphs_per_descendant_font, kGlyphsPerDescendantFont);
98   CHECK_GT(glyphs_per_descendant_font, 0u);
99 
100   const size_t descendant_font_count =
101       (num_glyphs + glyphs_per_descendant_font - 1) /
102       glyphs_per_descendant_font;
103 
104   fxcrt::ostringstream output;
105   for (size_t i = 0; i < descendant_font_count; ++i) {
106     output << "8 dict begin\n";
107     output << "/FontType 42 def\n";
108     output << "/FontMatrix [1 0 0 1 0 0] def\n";
109     output << "/FontName /" << psname << "_" << i << " def\n";
110 
111     output << "/Encoding " << glyphs_per_descendant_font << " array\n";
112     for (size_t j = 0, pos = i * glyphs_per_descendant_font;
113          j < glyphs_per_descendant_font; ++j, ++pos) {
114       if (pos >= num_glyphs)
115         break;
116 
117       output << ByteString::Format("dup %d /c%02x put\n", j, j);
118     }
119     output << "readonly def\n";
120 
121     // Note: `bbox` is LTRB, while /FontBBox is LBRT. Writing it out as LTRB
122     // gets the correct values.
123     output << "/FontBBox [" << bbox.left << " " << bbox.top << " " << bbox.right
124            << " " << bbox.bottom << "] def\n";
125 
126     output << "/PaintType 0 def\n";
127 
128     output << "/CharStrings " << glyphs_per_descendant_font + 1
129            << " dict dup begin\n";
130     output << "/.notdef 0 def\n";
131     for (size_t j = 0, pos = i * glyphs_per_descendant_font;
132          j < glyphs_per_descendant_font; ++j, ++pos) {
133       if (pos >= num_glyphs)
134         break;
135 
136       output << ByteString::Format("/c%02x %d def\n", j, pos);
137     }
138     output << "end readonly def\n";
139 
140     output << "/sfnts " << psname << "_sfnts def\n";
141     output << "FontName currentdict end definefont pop\n";
142   }
143 
144   output << "6 dict begin\n";
145   output << "/FontName /" << psname << " def\n";
146   output << "/FontType 0 def\n";
147   output << "/FontMatrix [1 0 0 1 0 0] def\n";
148   output << "/FMapType 2 def\n";
149 
150   output << "/Encoding [\n";
151   for (size_t i = 0; i < descendant_font_count; ++i)
152     output << i << "\n";
153   output << "] def\n";
154 
155   output << "/FDepVector [\n";
156   for (size_t i = 0; i < descendant_font_count; ++i)
157     output << "/" << psname << "_" << i << " findfont\n";
158   output << "] def\n";
159 
160   output << "FontName currentdict end definefont pop\n";
161   output << "%%EndResource\n";
162 
163   return ByteString(output);
164 }
165 
GenerateType42FontData(const CFX_Font * font)166 ByteString GenerateType42FontData(const CFX_Font* font) {
167   const FXFT_FaceRec* font_face_rec = font->GetFaceRec();
168   if (!font_face_rec)
169     return ByteString();
170 
171   const ByteString psname = font->GetPsName();
172   DCHECK(!psname.IsEmpty());
173 
174   absl::optional<ByteString> sfnt_data =
175       GenerateType42SfntData(psname, font->GetFontSpan());
176   if (!sfnt_data.has_value())
177     return ByteString();
178 
179   ByteString output = "%%BeginResource: font ";
180   output += psname;
181   output += "\n";
182   output += sfnt_data.value();
183   output += GenerateType42FontDictionary(psname, font->GetRawBBox().value(),
184                                          font_face_rec->num_glyphs,
185                                          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   absl::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         {reinterpret_cast<const uint8_t*>(m_PreambleOutput.str().c_str()),
281          pdfium::base::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(
287       {reinterpret_cast<const uint8_t*>(m_Output.str().c_str()),
288        pdfium::base::checked_cast<size_t>(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->m_LineWidth, pGraphState->m_MiterLimit);
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.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 
SetDIBits(const RetainPtr<CFX_DIBBase> & pSource,uint32_t color,int left,int top)483 bool CFX_PSRenderer::SetDIBits(const RetainPtr<CFX_DIBBase>& pSource,
484                                uint32_t color,
485                                int left,
486                                int top) {
487   StartRendering();
488   CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(
489       pSource->GetWidth(), pSource->GetHeight(), left, top);
490   return DrawDIBits(pSource, color, matrix, FXDIB_ResampleOptions());
491 }
492 
StretchDIBits(const RetainPtr<CFX_DIBBase> & pSource,uint32_t color,int dest_left,int dest_top,int dest_width,int dest_height,const FXDIB_ResampleOptions & options)493 bool CFX_PSRenderer::StretchDIBits(const RetainPtr<CFX_DIBBase>& pSource,
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(pSource, color, matrix, options);
504 }
505 
DrawDIBits(const RetainPtr<CFX_DIBBase> & pSource,uint32_t color,const CFX_Matrix & matrix,const FXDIB_ResampleOptions & options)506 bool CFX_PSRenderer::DrawDIBits(const RetainPtr<CFX_DIBBase>& pSource,
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 (pSource->IsAlphaFormat())
515     return false;
516 
517   int alpha = FXARGB_A(color);
518   if (pSource->IsMaskFormat() && (alpha < 255 || pSource->GetBPP() != 1))
519     return false;
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 = pSource->GetWidth();
528   const int height = pSource->GetHeight();
529   buf << width << " " << height;
530 
531   if (pSource->GetBPP() == 1 && !pSource->HasPalette()) {
532     FaxCompressResult compress_result = FaxCompressData(pSource);
533     if (compress_result.data.empty())
534       return false;
535 
536     if (pSource->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 (pSource->IsMaskFormat())
551       buf << "iM\n";
552     else
553       buf << "false 1 colorimage\n";
554 
555     WriteStream(buf);
556     WritePSBinary(compress_result.data);
557   } else {
558     RetainPtr<CFX_DIBBase> pConverted = pSource;
559     switch (pSource->GetFormat()) {
560       case FXDIB_Format::k1bppRgb:
561       case FXDIB_Format::kRgb32:
562         pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
563         break;
564       case FXDIB_Format::k8bppRgb:
565         if (pSource->HasPalette())
566           pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
567         break;
568       default:
569         break;
570     }
571     if (!pConverted) {
572       WriteString("\nQ\n");
573       return false;
574     }
575 
576     int bpp = pConverted->GetBPP() / 8;
577     uint8_t* output_buf = nullptr;
578     size_t output_size = 0;
579     bool output_buf_is_owned = true;
580     absl::optional<PSCompressResult> compress_result;
581     ByteString filter;
582     if ((m_Level.value() == RenderingLevel::kLevel2 || options.bLossy) &&
583         m_pEncoderIface->pJpegEncodeFunc(pConverted, &output_buf,
584                                          &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 = pConverted->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 
SetColor(uint32_t color)631 void 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 
FindPSFontGlyph(CFX_GlyphCache * pGlyphCache,CFX_Font * pFont,const TextCharPos & charpos,int * ps_fontnum,int * ps_glyphindex)643 void 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 =
699         CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
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)) {
717       case CFX_Path::Point::Type::kMove: {
718         buf << point.x << " " << point.y << " m\n";
719         break;
720       }
721       case CFX_Path::Point::Type::kLine: {
722         buf << point.x << " " << point.y << " l\n";
723         break;
724       }
725       case CFX_Path::Point::Type::kBezier: {
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 
DrawTextAsType3Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)741 void CFX_PSRenderer::DrawTextAsType3Font(int char_count,
742                                          const TextCharPos* char_pos,
743                                          CFX_Font* font,
744                                          float font_size,
745                                          fxcrt::ostringstream& buf) {
746   CFX_FontCache* pCache = CFX_GEModule::Get()->GetFontCache();
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 
DrawTextAsType42Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)764 bool 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 || !CanEmbed(font))
770     return false;
771 
772   if (font->GetFontType() != CFX_Font::FontType::kCIDTrueType)
773     return false;
774 
775   bool is_existing_font = m_pFontTracker->SeenFontObject(font);
776   if (!is_existing_font) {
777     ByteString font_data = GenerateType42FontData(font);
778     if (font_data.IsEmpty())
779       return false;
780 
781     m_pFontTracker->AddFontObject(font);
782     WritePreambleString(font_data.AsStringView());
783   }
784 
785   buf << "/" << font->GetPsName() << " " << font_size << " selectfont\n";
786   for (int i = 0; i < char_count; ++i) {
787     buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
788     uint8_t hi = char_pos[i].m_GlyphIndex / 256;
789     uint8_t lo = char_pos[i].m_GlyphIndex % 256;
790     ByteString hex = ByteString::Format("<%02X%02X>", hi, lo);
791     buf << hex.AsStringView() << "Tj\n";
792   }
793   return true;
794 }
795 
DrawText(int nChars,const TextCharPos * pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t color)796 bool CFX_PSRenderer::DrawText(int nChars,
797                               const TextCharPos* pCharPos,
798                               CFX_Font* pFont,
799                               const CFX_Matrix& mtObject2Device,
800                               float font_size,
801                               uint32_t color) {
802   // Check object to device matrix first, since it can scale the font size.
803   if ((mtObject2Device.a == 0 && mtObject2Device.b == 0) ||
804       (mtObject2Device.c == 0 && mtObject2Device.d == 0)) {
805     return true;
806   }
807 
808   // Do not send near zero font sizes to printers. See crbug.com/767343.
809   float scale =
810       std::min(mtObject2Device.GetXUnit(), mtObject2Device.GetYUnit());
811   static constexpr float kEpsilon = 0.01f;
812   if (fabsf(font_size * scale) < kEpsilon)
813     return true;
814 
815   StartRendering();
816   int alpha = FXARGB_A(color);
817   if (alpha < 255)
818     return false;
819 
820   SetColor(color);
821   fxcrt::ostringstream buf;
822   buf << "q[" << mtObject2Device.a << " " << mtObject2Device.b << " "
823       << mtObject2Device.c << " " << mtObject2Device.d << " "
824       << mtObject2Device.e << " " << mtObject2Device.f << "]cm\n";
825 
826   if (!DrawTextAsType42Font(nChars, pCharPos, pFont, font_size, buf)) {
827     DrawTextAsType3Font(nChars, pCharPos, pFont, font_size, buf);
828   }
829 
830   buf << "Q\n";
831   WriteStream(buf);
832   return true;
833 }
834 
FaxCompressData(RetainPtr<CFX_DIBBase> src) const835 CFX_PSRenderer::FaxCompressResult CFX_PSRenderer::FaxCompressData(
836     RetainPtr<CFX_DIBBase> src) const {
837   DCHECK_EQ(1, src->GetBPP());
838 
839   FaxCompressResult result;
840   const int width = src->GetWidth();
841   const int height = src->GetHeight();
842   const int pitch = src->GetPitch();
843   DCHECK_GE(width, pitch);
844 
845   FX_SAFE_UINT32 safe_pixel_count = width;
846   safe_pixel_count *= height;
847   if (!safe_pixel_count.IsValid())
848     return result;
849 
850   if (safe_pixel_count.ValueOrDie() > 128) {
851     result.data = m_pEncoderIface->pFaxEncodeFunc(std::move(src));
852     result.compressed = true;
853     return result;
854   }
855 
856   FX_SAFE_UINT32 safe_size = pitch;
857   safe_size *= height;
858   result.data.resize(safe_size.ValueOrDie());
859   auto dest_span = pdfium::make_span(result.data);
860   for (int row = 0; row < height; row++) {
861     pdfium::span<const uint8_t> src_scan = src->GetScanline(row);
862     fxcrt::spancpy(dest_span.subspan(row * pitch, pitch), src_scan);
863   }
864   return result;
865 }
866 
PSCompressData(pdfium::span<const uint8_t> src_span) const867 absl::optional<CFX_PSRenderer::PSCompressResult> CFX_PSRenderer::PSCompressData(
868     pdfium::span<const uint8_t> src_span) const {
869   if (src_span.size() < 1024)
870     return absl::nullopt;
871 
872   DataVector<uint8_t> (*encode_func)(pdfium::span<const uint8_t> src_span);
873   ByteString filter;
874   if (m_Level.value() == RenderingLevel::kLevel3 ||
875       m_Level.value() == RenderingLevel::kLevel3Type42) {
876     encode_func = m_pEncoderIface->pFlateEncodeFunc;
877     filter = "/FlateDecode filter ";
878   } else {
879     encode_func = m_pEncoderIface->pRunLengthEncodeFunc;
880     filter = "/RunLengthDecode filter ";
881   }
882 
883   DataVector<uint8_t> decode_result = encode_func(src_span);
884   if (decode_result.size() == 0 || decode_result.size() >= src_span.size())
885     return absl::nullopt;
886 
887   PSCompressResult result;
888   result.data = std::move(decode_result);
889   result.filter = filter;
890   return result;
891 }
892 
WritePreambleString(ByteStringView str)893 void CFX_PSRenderer::WritePreambleString(ByteStringView str) {
894   m_PreambleOutput << str;
895 }
896 
WritePSBinary(pdfium::span<const uint8_t> data)897 void CFX_PSRenderer::WritePSBinary(pdfium::span<const uint8_t> data) {
898   DataVector<uint8_t> encoded_data = m_pEncoderIface->pA85EncodeFunc(data);
899   pdfium::span<const uint8_t> result =
900       encoded_data.empty() ? data : encoded_data;
901   m_Output.write(reinterpret_cast<const char*>(result.data()), result.size());
902 }
903 
WriteStream(fxcrt::ostringstream & stream)904 void CFX_PSRenderer::WriteStream(fxcrt::ostringstream& stream) {
905   std::streamoff output_pos = stream.tellp();
906   if (output_pos > 0) {
907     m_Output.write(stream.str().c_str(),
908                    pdfium::base::checked_cast<size_t>(output_pos));
909   }
910 }
911 
WriteString(ByteStringView str)912 void CFX_PSRenderer::WriteString(ByteStringView str) {
913   m_Output << str;
914 }
915 
916 // static
GenerateType42SfntDataForTesting(const ByteString & psname,pdfium::span<const uint8_t> font_data)917 absl::optional<ByteString> CFX_PSRenderer::GenerateType42SfntDataForTesting(
918     const ByteString& psname,
919     pdfium::span<const uint8_t> font_data) {
920   return GenerateType42SfntData(psname, font_data);
921 }
922 
923 // static
GenerateType42FontDictionaryForTesting(const ByteString & psname,const FX_RECT & bbox,size_t num_glyphs,size_t glyphs_per_descendant_font)924 ByteString CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
925     const ByteString& psname,
926     const FX_RECT& bbox,
927     size_t num_glyphs,
928     size_t glyphs_per_descendant_font) {
929   return GenerateType42FontDictionary(psname, bbox, num_glyphs,
930                                       glyphs_per_descendant_font);
931 }
932