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