• 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 #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_dibextractor.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     CFX_DIBExtractor source_extractor(pSource);
559     RetainPtr<CFX_DIBBase> pConverted = source_extractor.GetBitmap();
560     if (!pConverted)
561       return false;
562     switch (pSource->GetFormat()) {
563       case FXDIB_Format::k1bppRgb:
564       case FXDIB_Format::kRgb32:
565         pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
566         break;
567       case FXDIB_Format::k8bppRgb:
568         if (pSource->HasPalette())
569           pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
570         break;
571       default:
572         break;
573     }
574     if (!pConverted) {
575       WriteString("\nQ\n");
576       return false;
577     }
578 
579     int bpp = pConverted->GetBPP() / 8;
580     uint8_t* output_buf = nullptr;
581     size_t output_size = 0;
582     bool output_buf_is_owned = true;
583     absl::optional<PSCompressResult> compress_result;
584     ByteString filter;
585     if ((m_Level.value() == RenderingLevel::kLevel2 || options.bLossy) &&
586         m_pEncoderIface->pJpegEncodeFunc(pConverted, &output_buf,
587                                          &output_size)) {
588       filter = "/DCTDecode filter ";
589     } else {
590       int src_pitch = width * bpp;
591       output_size = height * src_pitch;
592       output_buf = FX_Alloc(uint8_t, output_size);
593       for (int row = 0; row < height; row++) {
594         const uint8_t* src_scan = pConverted->GetScanline(row).data();
595         uint8_t* dest_scan = output_buf + row * src_pitch;
596         if (bpp == 3) {
597           for (int col = 0; col < width; col++) {
598             *dest_scan++ = src_scan[2];
599             *dest_scan++ = src_scan[1];
600             *dest_scan++ = *src_scan;
601             src_scan += 3;
602           }
603         } else {
604           memcpy(dest_scan, src_scan, src_pitch);
605         }
606       }
607       compress_result = PSCompressData({output_buf, output_size});
608       if (compress_result.has_value()) {
609         FX_Free(output_buf);
610         output_buf_is_owned = false;
611         output_buf = compress_result.value().data.data();
612         output_size = compress_result.value().data.size();
613         filter = compress_result.value().filter;
614       }
615     }
616     buf << " 8[";
617     buf << width << " 0 0 -" << height << " 0 " << height << "]";
618     buf << "currentfile/ASCII85Decode filter ";
619     if (!filter.IsEmpty())
620       buf << filter;
621 
622     buf << "false " << bpp;
623     buf << " colorimage\n";
624     WriteStream(buf);
625 
626     WritePSBinary({output_buf, output_size});
627     if (output_buf_is_owned)
628       FX_Free(output_buf);
629   }
630   WriteString("\nQ\n");
631   return true;
632 }
633 
SetColor(uint32_t color)634 void CFX_PSRenderer::SetColor(uint32_t color) {
635   if (m_bColorSet && m_LastColor == color)
636     return;
637 
638   fxcrt::ostringstream buf;
639   buf << FXARGB_R(color) / 255.0 << " " << FXARGB_G(color) / 255.0 << " "
640       << FXARGB_B(color) / 255.0 << " rg\n";
641   m_bColorSet = true;
642   m_LastColor = color;
643   WriteStream(buf);
644 }
645 
FindPSFontGlyph(CFX_GlyphCache * pGlyphCache,CFX_Font * pFont,const TextCharPos & charpos,int * ps_fontnum,int * ps_glyphindex)646 void CFX_PSRenderer::FindPSFontGlyph(CFX_GlyphCache* pGlyphCache,
647                                      CFX_Font* pFont,
648                                      const TextCharPos& charpos,
649                                      int* ps_fontnum,
650                                      int* ps_glyphindex) {
651   for (size_t i = 0; i < m_PSFontList.size(); ++i) {
652     const Glyph& glyph = *m_PSFontList[i];
653     if (glyph.font == pFont && glyph.glyph_index == charpos.m_GlyphIndex &&
654         glyph.adjust_matrix.has_value() == charpos.m_bGlyphAdjust) {
655       bool found;
656       if (glyph.adjust_matrix.has_value()) {
657         constexpr float kEpsilon = 0.01f;
658         const auto& adjust_matrix = glyph.adjust_matrix.value();
659         found = fabs(adjust_matrix[0] - charpos.m_AdjustMatrix[0]) < kEpsilon &&
660                 fabs(adjust_matrix[1] - charpos.m_AdjustMatrix[1]) < kEpsilon &&
661                 fabs(adjust_matrix[2] - charpos.m_AdjustMatrix[2]) < kEpsilon &&
662                 fabs(adjust_matrix[3] - charpos.m_AdjustMatrix[3]) < kEpsilon;
663       } else {
664         found = true;
665       }
666       if (found) {
667         *ps_fontnum = pdfium::base::checked_cast<int>(i / 256);
668         *ps_glyphindex = i % 256;
669         return;
670       }
671     }
672   }
673 
674   m_PSFontList.push_back(std::make_unique<Glyph>(pFont, charpos.m_GlyphIndex));
675   *ps_fontnum =
676       pdfium::base::checked_cast<int>((m_PSFontList.size() - 1) / 256);
677   *ps_glyphindex = (m_PSFontList.size() - 1) % 256;
678   if (*ps_glyphindex == 0) {
679     fxcrt::ostringstream buf;
680     buf << "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n"
681            "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding "
682            "exch/.notdef put}for\n"
683            "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n"
684            "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get "
685            "exch 2 copy known not{pop/.notdef}if get exec}bind def\n"
686            "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get "
687            "exec}bind def\n"
688            "currentdict end\n";
689     buf << "/X" << *ps_fontnum << " exch definefont pop\n";
690     WriteStream(buf);
691   }
692 
693   if (charpos.m_bGlyphAdjust) {
694     m_PSFontList.back()->adjust_matrix = std::array<float, 4>{
695         charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
696         charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3]};
697   }
698 
699   CFX_Matrix matrix;
700   if (charpos.m_bGlyphAdjust) {
701     matrix =
702         CFX_Matrix(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
703                    charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
704   }
705   const CFX_Path* pPath = pGlyphCache->LoadGlyphPath(
706       pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
707   if (!pPath)
708     return;
709 
710   CFX_Path TransformedPath(*pPath);
711   if (charpos.m_bGlyphAdjust)
712     TransformedPath.Transform(matrix);
713 
714   fxcrt::ostringstream buf;
715   buf << "/X" << *ps_fontnum << " Ff/CharProcs get begin/" << *ps_glyphindex
716       << "{n ";
717   for (size_t p = 0; p < TransformedPath.GetPoints().size(); p++) {
718     CFX_PointF point = TransformedPath.GetPoint(p);
719     switch (TransformedPath.GetType(p)) {
720       case CFX_Path::Point::Type::kMove: {
721         buf << point.x << " " << point.y << " m\n";
722         break;
723       }
724       case CFX_Path::Point::Type::kLine: {
725         buf << point.x << " " << point.y << " l\n";
726         break;
727       }
728       case CFX_Path::Point::Type::kBezier: {
729         CFX_PointF point1 = TransformedPath.GetPoint(p + 1);
730         CFX_PointF point2 = TransformedPath.GetPoint(p + 2);
731         buf << point.x << " " << point.y << " " << point1.x << " " << point1.y
732             << " " << point2.x << " " << point2.y << " c\n";
733         p += 2;
734         break;
735       }
736     }
737   }
738   buf << "f}bind def end\n";
739   buf << "/X" << *ps_fontnum << " Ff/Encoding get " << *ps_glyphindex << "/"
740       << *ps_glyphindex << " put\n";
741   WriteStream(buf);
742 }
743 
DrawTextAsType3Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)744 void CFX_PSRenderer::DrawTextAsType3Font(int char_count,
745                                          const TextCharPos* char_pos,
746                                          CFX_Font* font,
747                                          float font_size,
748                                          fxcrt::ostringstream& buf) {
749   CFX_FontCache* pCache = CFX_GEModule::Get()->GetFontCache();
750   RetainPtr<CFX_GlyphCache> pGlyphCache = pCache->GetGlyphCache(font);
751   int last_fontnum = -1;
752   for (int i = 0; i < char_count; i++) {
753     int ps_fontnum;
754     int ps_glyphindex;
755     FindPSFontGlyph(pGlyphCache.Get(), font, char_pos[i], &ps_fontnum,
756                     &ps_glyphindex);
757     if (last_fontnum != ps_fontnum) {
758       buf << "/X" << ps_fontnum << " Ff " << font_size << " Fs Sf ";
759       last_fontnum = ps_fontnum;
760     }
761     buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
762     ByteString hex = ByteString::Format("<%02X>", ps_glyphindex);
763     buf << hex.AsStringView() << "Tj\n";
764   }
765 }
766 
DrawTextAsType42Font(int char_count,const TextCharPos * char_pos,CFX_Font * font,float font_size,fxcrt::ostringstream & buf)767 bool CFX_PSRenderer::DrawTextAsType42Font(int char_count,
768                                           const TextCharPos* char_pos,
769                                           CFX_Font* font,
770                                           float font_size,
771                                           fxcrt::ostringstream& buf) {
772   if (m_Level != RenderingLevel::kLevel3Type42 || !CanEmbed(font))
773     return false;
774 
775   if (font->GetFontType() != CFX_Font::FontType::kCIDTrueType)
776     return false;
777 
778   bool is_existing_font = m_pFontTracker->SeenFontObject(font);
779   if (!is_existing_font) {
780     ByteString font_data = GenerateType42FontData(font);
781     if (font_data.IsEmpty())
782       return false;
783 
784     m_pFontTracker->AddFontObject(font);
785     WritePreambleString(font_data.AsStringView());
786   }
787 
788   buf << "/" << font->GetPsName() << " " << font_size << " selectfont\n";
789   for (int i = 0; i < char_count; ++i) {
790     buf << char_pos[i].m_Origin.x << " " << char_pos[i].m_Origin.y << " m";
791     uint8_t hi = char_pos[i].m_GlyphIndex / 256;
792     uint8_t lo = char_pos[i].m_GlyphIndex % 256;
793     ByteString hex = ByteString::Format("<%02X%02X>", hi, lo);
794     buf << hex.AsStringView() << "Tj\n";
795   }
796   return true;
797 }
798 
DrawText(int nChars,const TextCharPos * pCharPos,CFX_Font * pFont,const CFX_Matrix & mtObject2Device,float font_size,uint32_t color)799 bool CFX_PSRenderer::DrawText(int nChars,
800                               const TextCharPos* pCharPos,
801                               CFX_Font* pFont,
802                               const CFX_Matrix& mtObject2Device,
803                               float font_size,
804                               uint32_t color) {
805   // Check object to device matrix first, since it can scale the font size.
806   if ((mtObject2Device.a == 0 && mtObject2Device.b == 0) ||
807       (mtObject2Device.c == 0 && mtObject2Device.d == 0)) {
808     return true;
809   }
810 
811   // Do not send near zero font sizes to printers. See crbug.com/767343.
812   float scale =
813       std::min(mtObject2Device.GetXUnit(), mtObject2Device.GetYUnit());
814   static constexpr float kEpsilon = 0.01f;
815   if (fabsf(font_size * scale) < kEpsilon)
816     return true;
817 
818   StartRendering();
819   int alpha = FXARGB_A(color);
820   if (alpha < 255)
821     return false;
822 
823   SetColor(color);
824   fxcrt::ostringstream buf;
825   buf << "q[" << mtObject2Device.a << " " << mtObject2Device.b << " "
826       << mtObject2Device.c << " " << mtObject2Device.d << " "
827       << mtObject2Device.e << " " << mtObject2Device.f << "]cm\n";
828 
829   if (!DrawTextAsType42Font(nChars, pCharPos, pFont, font_size, buf)) {
830     DrawTextAsType3Font(nChars, pCharPos, pFont, font_size, buf);
831   }
832 
833   buf << "Q\n";
834   WriteStream(buf);
835   return true;
836 }
837 
FaxCompressData(RetainPtr<CFX_DIBBase> src) const838 CFX_PSRenderer::FaxCompressResult CFX_PSRenderer::FaxCompressData(
839     RetainPtr<CFX_DIBBase> src) const {
840   DCHECK_EQ(1, src->GetBPP());
841 
842   FaxCompressResult result;
843   const int width = src->GetWidth();
844   const int height = src->GetHeight();
845   const int pitch = src->GetPitch();
846   DCHECK_GE(width, pitch);
847 
848   FX_SAFE_UINT32 safe_pixel_count = width;
849   safe_pixel_count *= height;
850   if (!safe_pixel_count.IsValid())
851     return result;
852 
853   if (safe_pixel_count.ValueOrDie() > 128) {
854     result.data = m_pEncoderIface->pFaxEncodeFunc(std::move(src));
855     result.compressed = true;
856     return result;
857   }
858 
859   FX_SAFE_UINT32 safe_size = pitch;
860   safe_size *= height;
861   result.data.resize(safe_size.ValueOrDie());
862   auto dest_span = pdfium::make_span(result.data);
863   for (int row = 0; row < height; row++) {
864     pdfium::span<const uint8_t> src_scan = src->GetScanline(row);
865     fxcrt::spancpy(dest_span.subspan(row * pitch, pitch), src_scan);
866   }
867   return result;
868 }
869 
PSCompressData(pdfium::span<const uint8_t> src_span) const870 absl::optional<CFX_PSRenderer::PSCompressResult> CFX_PSRenderer::PSCompressData(
871     pdfium::span<const uint8_t> src_span) const {
872   if (src_span.size() < 1024)
873     return absl::nullopt;
874 
875   DataVector<uint8_t> (*encode_func)(pdfium::span<const uint8_t> src_span);
876   ByteString filter;
877   if (m_Level.value() == RenderingLevel::kLevel3 ||
878       m_Level.value() == RenderingLevel::kLevel3Type42) {
879     encode_func = m_pEncoderIface->pFlateEncodeFunc;
880     filter = "/FlateDecode filter ";
881   } else {
882     encode_func = m_pEncoderIface->pRunLengthEncodeFunc;
883     filter = "/RunLengthDecode filter ";
884   }
885 
886   DataVector<uint8_t> decode_result = encode_func(src_span);
887   if (decode_result.size() == 0 || decode_result.size() >= src_span.size())
888     return absl::nullopt;
889 
890   PSCompressResult result;
891   result.data = std::move(decode_result);
892   result.filter = filter;
893   return result;
894 }
895 
WritePreambleString(ByteStringView str)896 void CFX_PSRenderer::WritePreambleString(ByteStringView str) {
897   m_PreambleOutput << str;
898 }
899 
WritePSBinary(pdfium::span<const uint8_t> data)900 void CFX_PSRenderer::WritePSBinary(pdfium::span<const uint8_t> data) {
901   DataVector<uint8_t> encoded_data = m_pEncoderIface->pA85EncodeFunc(data);
902   pdfium::span<const uint8_t> result =
903       encoded_data.empty() ? data : encoded_data;
904   m_Output.write(reinterpret_cast<const char*>(result.data()), result.size());
905 }
906 
WriteStream(fxcrt::ostringstream & stream)907 void CFX_PSRenderer::WriteStream(fxcrt::ostringstream& stream) {
908   std::streamoff output_pos = stream.tellp();
909   if (output_pos > 0) {
910     m_Output.write(stream.str().c_str(),
911                    pdfium::base::checked_cast<size_t>(output_pos));
912   }
913 }
914 
WriteString(ByteStringView str)915 void CFX_PSRenderer::WriteString(ByteStringView str) {
916   m_Output << str;
917 }
918 
919 // static
GenerateType42SfntDataForTesting(const ByteString & psname,pdfium::span<const uint8_t> font_data)920 absl::optional<ByteString> CFX_PSRenderer::GenerateType42SfntDataForTesting(
921     const ByteString& psname,
922     pdfium::span<const uint8_t> font_data) {
923   return GenerateType42SfntData(psname, font_data);
924 }
925 
926 // static
GenerateType42FontDictionaryForTesting(const ByteString & psname,const FX_RECT & bbox,size_t num_glyphs,size_t glyphs_per_descendant_font)927 ByteString CFX_PSRenderer::GenerateType42FontDictionaryForTesting(
928     const ByteString& psname,
929     const FX_RECT& bbox,
930     size_t num_glyphs,
931     size_t glyphs_per_descendant_font) {
932   return GenerateType42FontDictionary(psname, bbox, num_glyphs,
933                                       glyphs_per_descendant_font);
934 }
935