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