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