1 // Copyright 2014 PDFium Authors. All rights reserved.
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/include/fxge/fx_ge.h"
8 #include "core/include/fxcodec/fx_codec.h"
9 #include "text_int.h"
10 
11 struct PSGlyph {
12   CFX_Font* m_pFont;
13   FX_DWORD m_GlyphIndex;
14   FX_BOOL m_bGlyphAdjust;
15   FX_FLOAT m_AdjustMatrix[4];
16 };
17 class CPSFont {
18  public:
19   PSGlyph m_Glyphs[256];
20   int m_nGlyphs;
21 };
CFX_PSRenderer()22 CFX_PSRenderer::CFX_PSRenderer() {
23   m_pOutput = NULL;
24   m_bColorSet = m_bGraphStateSet = FALSE;
25   m_bInited = FALSE;
26 }
~CFX_PSRenderer()27 CFX_PSRenderer::~CFX_PSRenderer() {
28   for (int i = 0; i < (int)m_PSFontList.GetSize(); i++) {
29     CPSFont* pFont = m_PSFontList[i];
30     delete pFont;
31   }
32 }
33 #define OUTPUT_PS(str) m_pOutput->OutputPS(str, sizeof str - 1)
Init(IFX_PSOutput * pOutput,int pslevel,int width,int height,FX_BOOL bCmykOutput)34 void CFX_PSRenderer::Init(IFX_PSOutput* pOutput,
35                           int pslevel,
36                           int width,
37                           int height,
38                           FX_BOOL bCmykOutput) {
39   m_PSLevel = pslevel;
40   m_pOutput = pOutput;
41   m_ClipBox.left = m_ClipBox.top = 0;
42   m_ClipBox.right = width;
43   m_ClipBox.bottom = height;
44   m_bCmykOutput = bCmykOutput;
45 }
StartRendering()46 FX_BOOL CFX_PSRenderer::StartRendering() {
47   if (m_bInited) {
48     return TRUE;
49   }
50   static const char init_str[] =
51       "\nsave\n/im/initmatrix load def\n"
52       "/n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load "
53       "def/h/closepath load def\n"
54       "/f/fill load def/F/eofill load def/s/stroke load def/W/clip load "
55       "def/W*/eoclip load def\n"
56       "/rg/setrgbcolor load def/k/setcmykcolor load def\n"
57       "/J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load "
58       "def/M/setmiterlimit load def/d/setdash load def\n"
59       "/q/gsave load def/Q/grestore load def/iM/imagemask load def\n"
60       "/Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont "
61       "load def\n"
62       "/cm/concat load def/Cm/currentmatrix load def/mx/matrix load "
63       "def/sm/setmatrix load def\n";
64   OUTPUT_PS(init_str);
65   m_bInited = TRUE;
66   return TRUE;
67 }
EndRendering()68 void CFX_PSRenderer::EndRendering() {
69   if (m_bInited) {
70     OUTPUT_PS("\nrestore\n");
71   }
72   m_bInited = FALSE;
73 }
SaveState()74 void CFX_PSRenderer::SaveState() {
75   StartRendering();
76   OUTPUT_PS("q\n");
77   m_ClipBoxStack.Add(m_ClipBox);
78 }
RestoreState(FX_BOOL bKeepSaved)79 void CFX_PSRenderer::RestoreState(FX_BOOL bKeepSaved) {
80   StartRendering();
81   if (bKeepSaved) {
82     OUTPUT_PS("Q\nq\n");
83   } else {
84     OUTPUT_PS("Q\n");
85   }
86   m_bColorSet = m_bGraphStateSet = FALSE;
87   m_ClipBox = m_ClipBoxStack.GetAt(m_ClipBoxStack.GetSize() - 1);
88   if (!bKeepSaved) {
89     m_ClipBoxStack.RemoveAt(m_ClipBoxStack.GetSize() - 1);
90   }
91 }
OutputPath(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device)92 void CFX_PSRenderer::OutputPath(const CFX_PathData* pPathData,
93                                 const CFX_Matrix* pObject2Device) {
94   int nPoints = pPathData->GetPointCount();
95   CFX_ByteTextBuf buf;
96   buf.EstimateSize(nPoints * 10);
97   for (int i = 0; i < nPoints; i++) {
98     uint8_t flag = pPathData->GetFlag(i);
99     FX_FLOAT x = pPathData->GetPointX(i);
100     FX_FLOAT y = pPathData->GetPointY(i);
101     if (pObject2Device) {
102       pObject2Device->Transform(x, y);
103     }
104     buf << x << " " << y;
105     switch (flag & FXPT_TYPE) {
106       case FXPT_MOVETO:
107         buf << " m ";
108         break;
109       case FXPT_LINETO:
110         if (flag & FXPT_CLOSEFIGURE) {
111           buf << " l h ";
112         } else {
113           buf << " l ";
114         }
115         break;
116       case FXPT_BEZIERTO: {
117         FX_FLOAT x1 = pPathData->GetPointX(i + 1);
118         FX_FLOAT x2 = pPathData->GetPointX(i + 2);
119         FX_FLOAT y1 = pPathData->GetPointY(i + 1);
120         FX_FLOAT y2 = pPathData->GetPointY(i + 2);
121         if (pObject2Device) {
122           pObject2Device->Transform(x1, y1);
123           pObject2Device->Transform(x2, y2);
124         }
125         buf << " " << x1 << " " << y1 << " " << x2 << " " << y2;
126         if (flag & FXPT_CLOSEFIGURE) {
127           buf << " c h\n";
128         } else {
129           buf << " c\n";
130         }
131         i += 2;
132         break;
133       }
134     }
135   }
136   m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
137 }
SetClip_PathFill(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,int fill_mode)138 void CFX_PSRenderer::SetClip_PathFill(const CFX_PathData* pPathData,
139                                       const CFX_Matrix* pObject2Device,
140                                       int fill_mode) {
141   StartRendering();
142   OutputPath(pPathData, pObject2Device);
143   CFX_FloatRect rect = pPathData->GetBoundingBox();
144   if (pObject2Device) {
145     rect.Transform(pObject2Device);
146   }
147   m_ClipBox.Intersect(rect.GetOutterRect());
148   if ((fill_mode & 3) == FXFILL_WINDING) {
149     OUTPUT_PS("W n\n");
150   } else {
151     OUTPUT_PS("W* n\n");
152   }
153 }
SetClip_PathStroke(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState)154 void CFX_PSRenderer::SetClip_PathStroke(const CFX_PathData* pPathData,
155                                         const CFX_Matrix* pObject2Device,
156                                         const CFX_GraphStateData* pGraphState) {
157   StartRendering();
158   SetGraphState(pGraphState);
159   if (pObject2Device) {
160     CFX_ByteTextBuf buf;
161     buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " "
162         << pObject2Device->c << " " << pObject2Device->d << " "
163         << pObject2Device->e << " " << pObject2Device->f << "]cm ";
164     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
165   }
166   OutputPath(pPathData, NULL);
167   CFX_FloatRect rect = pPathData->GetBoundingBox(pGraphState->m_LineWidth,
168                                                  pGraphState->m_MiterLimit);
169   rect.Transform(pObject2Device);
170   m_ClipBox.Intersect(rect.GetOutterRect());
171   if (pObject2Device) {
172     OUTPUT_PS("strokepath W n sm\n");
173   } else {
174     OUTPUT_PS("strokepath W n\n");
175   }
176 }
DrawPath(const CFX_PathData * pPathData,const CFX_Matrix * pObject2Device,const CFX_GraphStateData * pGraphState,FX_DWORD fill_color,FX_DWORD stroke_color,int fill_mode,int alpha_flag,void * pIccTransform)177 FX_BOOL CFX_PSRenderer::DrawPath(const CFX_PathData* pPathData,
178                                  const CFX_Matrix* pObject2Device,
179                                  const CFX_GraphStateData* pGraphState,
180                                  FX_DWORD fill_color,
181                                  FX_DWORD stroke_color,
182                                  int fill_mode,
183                                  int alpha_flag,
184                                  void* pIccTransform) {
185   StartRendering();
186   int fill_alpha = FXGETFLAG_COLORTYPE(alpha_flag)
187                        ? FXGETFLAG_ALPHA_FILL(alpha_flag)
188                        : FXARGB_A(fill_color);
189   int stroke_alpha = FXGETFLAG_COLORTYPE(alpha_flag)
190                          ? FXGETFLAG_ALPHA_STROKE(alpha_flag)
191                          : FXARGB_A(stroke_color);
192   if (fill_alpha && fill_alpha < 255) {
193     return FALSE;
194   }
195   if (stroke_alpha && stroke_alpha < 255) {
196     return FALSE;
197   }
198   if (fill_alpha == 0 && stroke_alpha == 0) {
199     return FALSE;
200   }
201   if (stroke_alpha) {
202     SetGraphState(pGraphState);
203     if (pObject2Device) {
204       CFX_ByteTextBuf buf;
205       buf << "mx Cm [" << pObject2Device->a << " " << pObject2Device->b << " "
206           << pObject2Device->c << " " << pObject2Device->d << " "
207           << pObject2Device->e << " " << pObject2Device->f << "]cm ";
208       m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
209     }
210   }
211   OutputPath(pPathData, stroke_alpha ? NULL : pObject2Device);
212   if (fill_mode && fill_alpha) {
213     SetColor(fill_color, alpha_flag, pIccTransform);
214     if ((fill_mode & 3) == FXFILL_WINDING) {
215       if (stroke_alpha) {
216         OUTPUT_PS("q f Q ");
217       } else {
218         OUTPUT_PS("f");
219       }
220     } else if ((fill_mode & 3) == FXFILL_ALTERNATE) {
221       if (stroke_alpha) {
222         OUTPUT_PS("q F Q ");
223       } else {
224         OUTPUT_PS("F");
225       }
226     }
227   }
228   if (stroke_alpha) {
229     SetColor(stroke_color, alpha_flag, pIccTransform);
230     if (pObject2Device) {
231       OUTPUT_PS("s sm");
232     } else {
233       OUTPUT_PS("s");
234     }
235   }
236   OUTPUT_PS("\n");
237   return TRUE;
238 }
SetGraphState(const CFX_GraphStateData * pGraphState)239 void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) {
240   CFX_ByteTextBuf buf;
241   if (!m_bGraphStateSet ||
242       m_CurGraphState.m_LineCap != pGraphState->m_LineCap) {
243     buf << pGraphState->m_LineCap << " J\n";
244   }
245   if (!m_bGraphStateSet ||
246       m_CurGraphState.m_DashCount != pGraphState->m_DashCount ||
247       FXSYS_memcmp(m_CurGraphState.m_DashArray, pGraphState->m_DashArray,
248                    sizeof(FX_FLOAT) * m_CurGraphState.m_DashCount)) {
249     buf << "[";
250     for (int i = 0; i < pGraphState->m_DashCount; i++) {
251       buf << pGraphState->m_DashArray[i] << " ";
252     }
253     buf << "]" << pGraphState->m_DashPhase << " d\n";
254   }
255   if (!m_bGraphStateSet ||
256       m_CurGraphState.m_LineJoin != pGraphState->m_LineJoin) {
257     buf << pGraphState->m_LineJoin << " j\n";
258   }
259   if (!m_bGraphStateSet ||
260       m_CurGraphState.m_LineWidth != pGraphState->m_LineWidth) {
261     buf << pGraphState->m_LineWidth << " w\n";
262   }
263   if (!m_bGraphStateSet ||
264       m_CurGraphState.m_MiterLimit != pGraphState->m_MiterLimit) {
265     buf << pGraphState->m_MiterLimit << " M\n";
266   }
267   m_CurGraphState.Copy(*pGraphState);
268   m_bGraphStateSet = TRUE;
269   if (buf.GetSize()) {
270     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
271   }
272 }
FaxCompressData(uint8_t * src_buf,int width,int height,uint8_t * & dest_buf,FX_DWORD & dest_size)273 static void FaxCompressData(uint8_t* src_buf,
274                             int width,
275                             int height,
276                             uint8_t*& dest_buf,
277                             FX_DWORD& dest_size) {
278   CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
279   if (width * height > 128 && pEncoders &&
280       pEncoders->GetFaxModule()->Encode(src_buf, width, height, (width + 7) / 8,
281                                         dest_buf, dest_size)) {
282     FX_Free(src_buf);
283   } else {
284     dest_buf = src_buf;
285     dest_size = (width + 7) / 8 * height;
286   }
287 }
PSCompressData(int PSLevel,uint8_t * src_buf,FX_DWORD src_size,uint8_t * & output_buf,FX_DWORD & output_size,const FX_CHAR * & filter)288 static void PSCompressData(int PSLevel,
289                            uint8_t* src_buf,
290                            FX_DWORD src_size,
291                            uint8_t*& output_buf,
292                            FX_DWORD& output_size,
293                            const FX_CHAR*& filter) {
294   output_buf = src_buf;
295   output_size = src_size;
296   filter = "";
297   if (src_size < 1024) {
298     return;
299   }
300   CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
301   uint8_t* dest_buf = NULL;
302   FX_DWORD dest_size = src_size;
303   if (PSLevel >= 3) {
304     if (pEncoders &&
305         pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf,
306                                             dest_size)) {
307       filter = "/FlateDecode filter ";
308     }
309   } else {
310     if (pEncoders &&
311         pEncoders->GetBasicModule()->RunLengthEncode(src_buf, src_size,
312                                                      dest_buf, dest_size)) {
313       filter = "/RunLengthDecode filter ";
314     }
315   }
316   if (dest_size < src_size) {
317     output_buf = dest_buf;
318     output_size = dest_size;
319   } else {
320     filter = NULL;
321     FX_Free(dest_buf);
322   }
323 }
SetDIBits(const CFX_DIBSource * pSource,FX_DWORD color,int left,int top,int alpha_flag,void * pIccTransform)324 FX_BOOL CFX_PSRenderer::SetDIBits(const CFX_DIBSource* pSource,
325                                   FX_DWORD color,
326                                   int left,
327                                   int top,
328                                   int alpha_flag,
329                                   void* pIccTransform) {
330   StartRendering();
331   CFX_Matrix matrix((FX_FLOAT)(pSource->GetWidth()), 0.0f, 0.0f,
332                     -(FX_FLOAT)(pSource->GetHeight()), (FX_FLOAT)(left),
333                     (FX_FLOAT)(top + pSource->GetHeight()));
334   return DrawDIBits(pSource, color, &matrix, 0, alpha_flag, pIccTransform);
335 }
StretchDIBits(const CFX_DIBSource * pSource,FX_DWORD color,int dest_left,int dest_top,int dest_width,int dest_height,FX_DWORD flags,int alpha_flag,void * pIccTransform)336 FX_BOOL CFX_PSRenderer::StretchDIBits(const CFX_DIBSource* pSource,
337                                       FX_DWORD color,
338                                       int dest_left,
339                                       int dest_top,
340                                       int dest_width,
341                                       int dest_height,
342                                       FX_DWORD flags,
343                                       int alpha_flag,
344                                       void* pIccTransform) {
345   StartRendering();
346   CFX_Matrix matrix((FX_FLOAT)(dest_width), 0.0f, 0.0f,
347                     (FX_FLOAT)(-dest_height), (FX_FLOAT)(dest_left),
348                     (FX_FLOAT)(dest_top + dest_height));
349   return DrawDIBits(pSource, color, &matrix, flags, alpha_flag, pIccTransform);
350 }
DrawDIBits(const CFX_DIBSource * pSource,FX_DWORD color,const CFX_Matrix * pMatrix,FX_DWORD flags,int alpha_flag,void * pIccTransform)351 FX_BOOL CFX_PSRenderer::DrawDIBits(const CFX_DIBSource* pSource,
352                                    FX_DWORD color,
353                                    const CFX_Matrix* pMatrix,
354                                    FX_DWORD flags,
355                                    int alpha_flag,
356                                    void* pIccTransform) {
357   StartRendering();
358   if ((pMatrix->a == 0 && pMatrix->b == 0) ||
359       (pMatrix->c == 0 && pMatrix->d == 0)) {
360     return TRUE;
361   }
362   if (pSource->HasAlpha()) {
363     return FALSE;
364   }
365   int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(color)
366                                               : FXARGB_A(color);
367   if (pSource->IsAlphaMask() && (alpha < 255 || pSource->GetBPP() != 1)) {
368     return FALSE;
369   }
370   OUTPUT_PS("q\n");
371   CFX_ByteTextBuf buf;
372   buf << "[" << pMatrix->a << " " << pMatrix->b << " " << pMatrix->c << " "
373       << pMatrix->d << " " << pMatrix->e << " " << pMatrix->f << "]cm ";
374   int width = pSource->GetWidth();
375   int height = pSource->GetHeight();
376   buf << width << " " << height;
377   if (pSource->GetBPP() == 1 && !pSource->GetPalette()) {
378     int pitch = (width + 7) / 8;
379     FX_DWORD src_size = height * pitch;
380     uint8_t* src_buf = FX_Alloc(uint8_t, src_size);
381     for (int row = 0; row < height; row++) {
382       const uint8_t* src_scan = pSource->GetScanline(row);
383       FXSYS_memcpy(src_buf + row * pitch, src_scan, pitch);
384     }
385     uint8_t* output_buf;
386     FX_DWORD output_size;
387     FaxCompressData(src_buf, width, height, output_buf, output_size);
388     if (pSource->IsAlphaMask()) {
389       SetColor(color, alpha_flag, pIccTransform);
390       m_bColorSet = FALSE;
391       buf << " true[";
392     } else {
393       buf << " 1[";
394     }
395     buf << width << " 0 0 -" << height << " 0 " << height
396         << "]currentfile/ASCII85Decode filter ";
397     if (output_buf != src_buf)
398       buf << "<</K -1/EndOfBlock false/Columns " << width << "/Rows " << height
399           << ">>/CCITTFaxDecode filter ";
400     if (pSource->IsAlphaMask()) {
401       buf << "iM\n";
402     } else {
403       buf << "false 1 colorimage\n";
404     }
405     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
406     WritePSBinary(output_buf, output_size);
407     FX_Free(output_buf);
408   } else {
409     CFX_DIBSource* pConverted = (CFX_DIBSource*)pSource;
410     if (pIccTransform) {
411       FXDIB_Format format = m_bCmykOutput ? FXDIB_Cmyk : FXDIB_Rgb;
412       pConverted = pSource->CloneConvert(format, NULL, pIccTransform);
413     } else {
414       switch (pSource->GetFormat()) {
415         case FXDIB_1bppRgb:
416         case FXDIB_Rgb32:
417           pConverted = pSource->CloneConvert(FXDIB_Rgb);
418           break;
419         case FXDIB_8bppRgb:
420           if (pSource->GetPalette()) {
421             pConverted = pSource->CloneConvert(FXDIB_Rgb);
422           }
423           break;
424         case FXDIB_1bppCmyk:
425           pConverted = pSource->CloneConvert(FXDIB_Cmyk);
426           break;
427         case FXDIB_8bppCmyk:
428           if (pSource->GetPalette()) {
429             pConverted = pSource->CloneConvert(FXDIB_Cmyk);
430           }
431           break;
432         default:
433           break;
434       }
435     }
436     if (!pConverted) {
437       OUTPUT_PS("\nQ\n");
438       return FALSE;
439     }
440     int Bpp = pConverted->GetBPP() / 8;
441     uint8_t* output_buf = NULL;
442     FX_STRSIZE output_size = 0;
443     const FX_CHAR* filter = NULL;
444     if (flags & FXRENDER_IMAGE_LOSSY) {
445       CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
446       if (pEncoders &&
447           pEncoders->GetJpegModule()->Encode(pConverted, output_buf,
448                                              output_size)) {
449         filter = "/DCTDecode filter ";
450       }
451     }
452     if (!filter) {
453       int src_pitch = width * Bpp;
454       output_size = height * src_pitch;
455       output_buf = FX_Alloc(uint8_t, output_size);
456       for (int row = 0; row < height; row++) {
457         const uint8_t* src_scan = pConverted->GetScanline(row);
458         uint8_t* dest_scan = output_buf + row * src_pitch;
459         if (Bpp == 3) {
460           for (int col = 0; col < width; col++) {
461             *dest_scan++ = src_scan[2];
462             *dest_scan++ = src_scan[1];
463             *dest_scan++ = *src_scan;
464             src_scan += 3;
465           }
466         } else {
467           FXSYS_memcpy(dest_scan, src_scan, src_pitch);
468         }
469       }
470       uint8_t* compressed_buf;
471       FX_DWORD compressed_size;
472       PSCompressData(m_PSLevel, output_buf, output_size, compressed_buf,
473                      compressed_size, filter);
474       if (output_buf != compressed_buf) {
475         FX_Free(output_buf);
476       }
477       output_buf = compressed_buf;
478       output_size = compressed_size;
479     }
480     if (pConverted != pSource) {
481       delete pConverted;
482       pConverted = NULL;
483     }
484     buf << " 8[";
485     buf << width << " 0 0 -" << height << " 0 " << height << "]";
486     buf << "currentfile/ASCII85Decode filter ";
487     if (filter) {
488       buf << filter;
489     }
490     buf << "false " << Bpp;
491     buf << " colorimage\n";
492     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
493     WritePSBinary(output_buf, output_size);
494     FX_Free(output_buf);
495   }
496   OUTPUT_PS("\nQ\n");
497   return TRUE;
498 }
SetColor(FX_DWORD color,int alpha_flag,void * pIccTransform)499 void CFX_PSRenderer::SetColor(FX_DWORD color,
500                               int alpha_flag,
501                               void* pIccTransform) {
502   if (!CFX_GEModule::Get()->GetCodecModule() ||
503       !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
504     pIccTransform = NULL;
505   }
506   FX_BOOL bCMYK = FALSE;
507   if (pIccTransform) {
508     ICodec_IccModule* pIccModule =
509         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
510     color = FXGETFLAG_COLORTYPE(alpha_flag) ? FXCMYK_TODIB(color)
511                                             : FXARGB_TODIB(color);
512     uint8_t* pColor = (uint8_t*)&color;
513     pIccModule->TranslateScanline(pIccTransform, pColor, pColor, 1);
514     color = m_bCmykOutput ? FXCMYK_TODIB(color) : FXARGB_TODIB(color);
515     bCMYK = m_bCmykOutput;
516   } else {
517     bCMYK = FXGETFLAG_COLORTYPE(alpha_flag);
518   }
519   if (bCMYK != m_bCmykOutput || !m_bColorSet || m_LastColor != color) {
520     CFX_ByteTextBuf buf;
521     if (bCMYK) {
522       buf << FXSYS_GetCValue(color) / 255.0 << " "
523           << FXSYS_GetMValue(color) / 255.0 << " "
524           << FXSYS_GetYValue(color) / 255.0 << " "
525           << FXSYS_GetKValue(color) / 255.0 << " k\n";
526     } else {
527       buf << FXARGB_R(color) / 255.0 << " " << FXARGB_G(color) / 255.0 << " "
528           << FXARGB_B(color) / 255.0 << " rg\n";
529     }
530     if (bCMYK == m_bCmykOutput) {
531       m_bColorSet = TRUE;
532       m_LastColor = color;
533     }
534     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
535   }
536 }
FindPSFontGlyph(CFX_FaceCache * pFaceCache,CFX_Font * pFont,const FXTEXT_CHARPOS & charpos,int & ps_fontnum,int & ps_glyphindex)537 void CFX_PSRenderer::FindPSFontGlyph(CFX_FaceCache* pFaceCache,
538                                      CFX_Font* pFont,
539                                      const FXTEXT_CHARPOS& charpos,
540                                      int& ps_fontnum,
541                                      int& ps_glyphindex) {
542   for (int i = 0; i < (int)m_PSFontList.GetSize(); i++) {
543     CPSFont* pPSFont = m_PSFontList[i];
544     for (int j = 0; j < pPSFont->m_nGlyphs; j++)
545       if (pPSFont->m_Glyphs[j].m_pFont == pFont &&
546           pPSFont->m_Glyphs[j].m_GlyphIndex == charpos.m_GlyphIndex) {
547         if ((!pPSFont->m_Glyphs[j].m_bGlyphAdjust && !charpos.m_bGlyphAdjust) ||
548             (pPSFont->m_Glyphs[j].m_bGlyphAdjust && charpos.m_bGlyphAdjust &&
549              (FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[0] -
550                          charpos.m_AdjustMatrix[0]) < 0.01 &&
551               FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[1] -
552                          charpos.m_AdjustMatrix[1]) < 0.01 &&
553               FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[2] -
554                          charpos.m_AdjustMatrix[2]) < 0.01 &&
555               FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[3] -
556                          charpos.m_AdjustMatrix[3]) < 0.01))) {
557           ps_fontnum = i;
558           ps_glyphindex = j;
559           return;
560         }
561       }
562   }
563   if (m_PSFontList.GetSize() == 0 ||
564       m_PSFontList[m_PSFontList.GetSize() - 1]->m_nGlyphs == 256) {
565     CPSFont* pPSFont = new CPSFont;
566     pPSFont->m_nGlyphs = 0;
567     m_PSFontList.Add(pPSFont);
568     CFX_ByteTextBuf buf;
569     buf << "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n"
570            "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding "
571            "exch/.notdef put}for\n"
572            "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n"
573            "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get "
574            "exch 2 copy known not{pop/.notdef}if get exec}bind def\n"
575            "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get "
576            "exec}bind def\n"
577            "currentdict end\n";
578     buf << "/X" << m_PSFontList.GetSize() - 1 << " exch definefont pop\n";
579     m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
580     buf.Clear();
581   }
582   ps_fontnum = m_PSFontList.GetSize() - 1;
583   CPSFont* pPSFont = m_PSFontList[ps_fontnum];
584   ps_glyphindex = pPSFont->m_nGlyphs;
585   pPSFont->m_Glyphs[ps_glyphindex].m_GlyphIndex = charpos.m_GlyphIndex;
586   pPSFont->m_Glyphs[ps_glyphindex].m_pFont = pFont;
587   pPSFont->m_Glyphs[ps_glyphindex].m_bGlyphAdjust = charpos.m_bGlyphAdjust;
588   if (charpos.m_bGlyphAdjust) {
589     pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[0] =
590         charpos.m_AdjustMatrix[0];
591     pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[1] =
592         charpos.m_AdjustMatrix[1];
593     pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[2] =
594         charpos.m_AdjustMatrix[2];
595     pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[3] =
596         charpos.m_AdjustMatrix[3];
597   }
598   pPSFont->m_nGlyphs++;
599   CFX_Matrix matrix;
600   if (charpos.m_bGlyphAdjust)
601     matrix.Set(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
602                charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
603   matrix.Concat(1.0f, 0, 0, 1.0f, 0, 0);
604   const CFX_PathData* pPathData = pFaceCache->LoadGlyphPath(
605       pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
606   if (!pPathData) {
607     return;
608   }
609   CFX_PathData TransformedPath(*pPathData);
610   if (charpos.m_bGlyphAdjust) {
611     TransformedPath.Transform(&matrix);
612   }
613   CFX_ByteTextBuf buf;
614   buf << "/X" << ps_fontnum << " Ff/CharProcs get begin/" << ps_glyphindex
615       << "{n ";
616   for (int p = 0; p < TransformedPath.GetPointCount(); p++) {
617     FX_FLOAT x = TransformedPath.GetPointX(p), y = TransformedPath.GetPointY(p);
618     switch (TransformedPath.GetFlag(p) & FXPT_TYPE) {
619       case FXPT_MOVETO: {
620         buf << x << " " << y << " m\n";
621         break;
622       }
623       case FXPT_LINETO: {
624         buf << x << " " << y << " l\n";
625         break;
626       }
627       case FXPT_BEZIERTO: {
628         buf << x << " " << y << " " << TransformedPath.GetPointX(p + 1) << " "
629             << TransformedPath.GetPointY(p + 1) << " "
630             << TransformedPath.GetPointX(p + 2) << " "
631             << TransformedPath.GetPointY(p + 2) << " c\n";
632         p += 2;
633         break;
634       }
635     }
636   }
637   buf << "f}bind def end\n";
638   buf << "/X" << ps_fontnum << " Ff/Encoding get " << ps_glyphindex << "/"
639       << ps_glyphindex << " put\n";
640   m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
641 }
DrawText(int nChars,const FXTEXT_CHARPOS * pCharPos,CFX_Font * pFont,CFX_FontCache * pCache,const CFX_Matrix * pObject2Device,FX_FLOAT font_size,FX_DWORD color,int alpha_flag,void * pIccTransform)642 FX_BOOL CFX_PSRenderer::DrawText(int nChars,
643                                  const FXTEXT_CHARPOS* pCharPos,
644                                  CFX_Font* pFont,
645                                  CFX_FontCache* pCache,
646                                  const CFX_Matrix* pObject2Device,
647                                  FX_FLOAT font_size,
648                                  FX_DWORD color,
649                                  int alpha_flag,
650                                  void* pIccTransform) {
651   StartRendering();
652   int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(alpha_flag)
653                                               : FXARGB_A(color);
654   if (alpha < 255) {
655     return FALSE;
656   }
657   if ((pObject2Device->a == 0 && pObject2Device->b == 0) ||
658       (pObject2Device->c == 0 && pObject2Device->d == 0)) {
659     return TRUE;
660   }
661   SetColor(color, alpha_flag, pIccTransform);
662   CFX_ByteTextBuf buf;
663   buf << "q[" << pObject2Device->a << " " << pObject2Device->b << " "
664       << pObject2Device->c << " " << pObject2Device->d << " "
665       << pObject2Device->e << " " << pObject2Device->f << "]cm\n";
666   if (!pCache) {
667     pCache = CFX_GEModule::Get()->GetFontCache();
668   }
669   CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont);
670   FX_FONTCACHE_DEFINE(pCache, pFont);
671   int last_fontnum = -1;
672   for (int i = 0; i < nChars; i++) {
673     int ps_fontnum, ps_glyphindex;
674     FindPSFontGlyph(pFaceCache, pFont, pCharPos[i], ps_fontnum, ps_glyphindex);
675     if (last_fontnum != ps_fontnum) {
676       buf << "/X" << ps_fontnum << " Ff " << font_size << " Fs Sf ";
677       last_fontnum = ps_fontnum;
678     }
679     buf << pCharPos[i].m_OriginX << " " << pCharPos[i].m_OriginY << " m";
680     CFX_ByteString hex;
681     hex.Format("<%02X>", ps_glyphindex);
682     buf << hex << "Tj\n";
683   }
684   buf << "Q\n";
685   m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
686   return TRUE;
687 }
WritePSBinary(const uint8_t * data,int len)688 void CFX_PSRenderer::WritePSBinary(const uint8_t* data, int len) {
689   uint8_t* dest_buf;
690   FX_DWORD dest_size;
691   CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
692   if (pEncoders &&
693       pEncoders->GetBasicModule()->A85Encode(data, len, dest_buf, dest_size)) {
694     m_pOutput->OutputPS((const FX_CHAR*)dest_buf, dest_size);
695     FX_Free(dest_buf);
696   } else {
697     m_pOutput->OutputPS((const FX_CHAR*)data, len);
698   }
699 }
700