Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: core/src/fxge/ge/fx_ge_ps.cpp

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

Powered by Google App Engine
This is Rietveld 408576698