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

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

Issue 1265503005: clang-format all pdfium code. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: sigh Created 5 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 16 class CPSFont {
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 uint8_t 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((const FX_CHAR*)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 uint8_t 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((const FX_CHAR*)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((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); 166 m_pOutput->OutputPS((const FX_CHAR*)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)
192 return FALSE; 193 : FXARGB_A(stroke_color);
193 } 194 if (fill_alpha && fill_alpha < 255) {
194 if (stroke_alpha && stroke_alpha < 255) { 195 return FALSE;
195 return FALSE; 196 }
196 } 197 if (stroke_alpha && stroke_alpha < 255) {
197 if (fill_alpha == 0 && stroke_alpha == 0) { 198 return FALSE;
198 return FALSE; 199 }
199 } 200 if (fill_alpha == 0 && stroke_alpha == 0) {
200 if (stroke_alpha) { 201 return FALSE;
201 SetGraphState(pGraphState); 202 }
202 if (pObject2Device) { 203 if (stroke_alpha) {
203 CFX_ByteTextBuf buf; 204 SetGraphState(pGraphState);
204 buf << FX_BSTRC("mx Cm [") << pObject2Device->a << FX_BSTRC(" ") << pObject2Device->b << FX_BSTRC(" ") << 205 if (pObject2Device) {
205 pObject2Device->c << FX_BSTRC(" ") << pObject2Device->d << FX_BS TRC(" ") << pObject2Device->e << 206 CFX_ByteTextBuf buf;
206 FX_BSTRC(" ") << pObject2Device->f << FX_BSTRC("]cm "); 207 buf << FX_BSTRC("mx Cm [") << pObject2Device->a << FX_BSTRC(" ")
207 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); 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((const FX_CHAR*)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_memcmp(m_CurGraphState.m_DashArray, pGraphState->m_DashArray,
252 sizeof(FX_FLOAT) * m_CurGraphState.m_DashCount)) {
253 buf << FX_BSTRC("[");
254 for (int i = 0; i < pGraphState->m_DashCount; i++) {
255 buf << pGraphState->m_DashArray[i] << FX_BSTRC(" ");
256 }
257 buf << FX_BSTRC("]") << pGraphState->m_DashPhase << FX_BSTRC(" d\n");
258 }
259 if (!m_bGraphStateSet ||
260 m_CurGraphState.m_LineJoin != pGraphState->m_LineJoin) {
261 buf << pGraphState->m_LineJoin << FX_BSTRC(" j\n");
262 }
263 if (!m_bGraphStateSet ||
264 m_CurGraphState.m_LineWidth != pGraphState->m_LineWidth) {
265 buf << pGraphState->m_LineWidth << FX_BSTRC(" w\n");
266 }
267 if (!m_bGraphStateSet ||
268 m_CurGraphState.m_MiterLimit != pGraphState->m_MiterLimit) {
269 buf << pGraphState->m_MiterLimit << FX_BSTRC(" M\n");
270 }
271 m_CurGraphState.Copy(*pGraphState);
272 m_bGraphStateSet = TRUE;
273 if (buf.GetSize()) {
274 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
275 }
276 }
277 static void FaxCompressData(uint8_t* src_buf,
278 int width,
279 int height,
280 uint8_t*& dest_buf,
281 FX_DWORD& dest_size) {
282 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
283 if (width * height > 128 && pEncoders &&
284 pEncoders->GetFaxModule()->Encode(src_buf, width, height, (width + 7) / 8,
285 dest_buf, dest_size)) {
286 FX_Free(src_buf);
287 } else {
288 dest_buf = src_buf;
289 dest_size = (width + 7) / 8 * height;
290 }
291 }
292 static void PSCompressData(int PSLevel,
293 uint8_t* src_buf,
294 FX_DWORD src_size,
295 uint8_t*& output_buf,
296 FX_DWORD& output_size,
297 const FX_CHAR*& filter) {
298 output_buf = src_buf;
299 output_size = src_size;
300 filter = "";
301 if (src_size < 1024) {
302 return;
303 }
304 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
305 uint8_t* dest_buf = NULL;
306 FX_DWORD dest_size = src_size;
307 if (PSLevel >= 3) {
308 if (pEncoders &&
309 pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf,
310 dest_size)) {
311 filter = "/FlateDecode filter ";
312 }
313 } else {
314 if (pEncoders &&
315 pEncoders->GetBasicModule()->RunLengthEncode(src_buf, src_size,
316 dest_buf, dest_size)) {
317 filter = "/RunLengthDecode filter ";
318 }
319 }
320 if (dest_size < src_size) {
321 output_buf = dest_buf;
322 output_size = dest_size;
323 } else {
324 filter = NULL;
325 if (dest_buf) {
326 FX_Free(dest_buf);
327 }
328 }
329 }
330 FX_BOOL CFX_PSRenderer::SetDIBits(const CFX_DIBSource* pSource,
331 FX_DWORD color,
332 int left,
333 int top,
334 int alpha_flag,
335 void* pIccTransform) {
336 StartRendering();
337 CFX_AffineMatrix matrix((FX_FLOAT)(pSource->GetWidth()), 0.0f, 0.0f,
338 -(FX_FLOAT)(pSource->GetHeight()), (FX_FLOAT)(left),
339 (FX_FLOAT)(top + pSource->GetHeight()));
340 return DrawDIBits(pSource, color, &matrix, 0, alpha_flag, pIccTransform);
341 }
342 FX_BOOL CFX_PSRenderer::StretchDIBits(const CFX_DIBSource* pSource,
343 FX_DWORD color,
344 int dest_left,
345 int dest_top,
346 int dest_width,
347 int dest_height,
348 FX_DWORD flags,
349 int alpha_flag,
350 void* pIccTransform) {
351 StartRendering();
352 CFX_AffineMatrix matrix((FX_FLOAT)(dest_width), 0.0f, 0.0f,
353 (FX_FLOAT)(-dest_height), (FX_FLOAT)(dest_left),
354 (FX_FLOAT)(dest_top + dest_height));
355 return DrawDIBits(pSource, color, &matrix, flags, alpha_flag, pIccTransform);
356 }
357 FX_BOOL CFX_PSRenderer::DrawDIBits(const CFX_DIBSource* pSource,
358 FX_DWORD color,
359 const CFX_AffineMatrix* pMatrix,
360 FX_DWORD flags,
361 int alpha_flag,
362 void* pIccTransform) {
363 StartRendering();
364 if ((pMatrix->a == 0 && pMatrix->b == 0) ||
365 (pMatrix->c == 0 && pMatrix->d == 0)) {
366 return TRUE;
367 }
368 if (pSource->HasAlpha()) {
369 return FALSE;
370 }
371 int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(color)
372 : FXARGB_A(color);
373 if (pSource->IsAlphaMask() && (alpha < 255 || pSource->GetBPP() != 1)) {
374 return FALSE;
375 }
376 OUTPUT_PS("q\n");
377 CFX_ByteTextBuf buf;
378 buf << FX_BSTRC("[") << pMatrix->a << FX_BSTRC(" ") << pMatrix->b
379 << FX_BSTRC(" ") << pMatrix->c << FX_BSTRC(" ") << pMatrix->d
380 << FX_BSTRC(" ") << pMatrix->e << FX_BSTRC(" ") << pMatrix->f
381 << FX_BSTRC("]cm ");
382 int width = pSource->GetWidth();
383 int height = pSource->GetHeight();
384 buf << width << FX_BSTRC(" ") << height;
385 if (pSource->GetBPP() == 1 && pSource->GetPalette() == NULL) {
386 int pitch = (width + 7) / 8;
387 FX_DWORD src_size = height * pitch;
388 uint8_t* src_buf = FX_Alloc(uint8_t, src_size);
389 for (int row = 0; row < height; row++) {
390 const uint8_t* src_scan = pSource->GetScanline(row);
391 FXSYS_memcpy(src_buf + row * pitch, src_scan, pitch);
392 }
393 uint8_t* output_buf;
394 FX_DWORD output_size;
395 FaxCompressData(src_buf, width, height, output_buf, output_size);
396 if (pSource->IsAlphaMask()) {
397 SetColor(color, alpha_flag, pIccTransform);
398 m_bColorSet = FALSE;
399 buf << FX_BSTRC(" true[");
400 } else {
401 buf << FX_BSTRC(" 1[");
402 }
403 buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << height
404 << FX_BSTRC("]currentfile/ASCII85Decode filter ");
405 if (output_buf != src_buf)
406 buf << FX_BSTRC("<</K -1/EndOfBlock false/Columns ") << width
407 << FX_BSTRC("/Rows ") << height
408 << FX_BSTRC(">>/CCITTFaxDecode filter ");
409 if (pSource->IsAlphaMask()) {
410 buf << FX_BSTRC("iM\n");
411 } else {
412 buf << FX_BSTRC("false 1 colorimage\n");
413 }
414 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
415 WritePSBinary(output_buf, output_size);
416 FX_Free(output_buf);
417 } else {
418 CFX_DIBSource* pConverted = (CFX_DIBSource*)pSource;
419 if (pIccTransform) {
420 FXDIB_Format format = m_bCmykOutput ? FXDIB_Cmyk : FXDIB_Rgb;
421 pConverted = pSource->CloneConvert(format, NULL, pIccTransform);
422 } else {
423 switch (pSource->GetFormat()) {
424 case FXDIB_1bppRgb:
425 case FXDIB_Rgb32:
426 pConverted = pSource->CloneConvert(FXDIB_Rgb);
427 break;
428 case FXDIB_8bppRgb:
429 if (pSource->GetPalette() != NULL) {
430 pConverted = pSource->CloneConvert(FXDIB_Rgb);
431 }
432 break;
433 case FXDIB_1bppCmyk:
434 pConverted = pSource->CloneConvert(FXDIB_Cmyk);
435 break;
436 case FXDIB_8bppCmyk:
437 if (pSource->GetPalette() != NULL) {
438 pConverted = pSource->CloneConvert(FXDIB_Cmyk);
439 }
440 break;
441 default:
442 break;
443 }
444 }
445 if (pConverted == NULL) {
446 OUTPUT_PS("\nQ\n");
447 return FALSE;
448 }
449 int Bpp = pConverted->GetBPP() / 8;
450 uint8_t* output_buf = NULL;
451 FX_STRSIZE output_size = 0;
452 const FX_CHAR* filter = NULL;
453 if (flags & FXRENDER_IMAGE_LOSSY) {
454 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
455 if (pEncoders &&
456 pEncoders->GetJpegModule()->Encode(pConverted, output_buf,
457 output_size)) {
458 filter = "/DCTDecode filter ";
459 }
460 }
461 if (filter == NULL) {
462 int src_pitch = width * Bpp;
463 output_size = height * src_pitch;
464 output_buf = FX_Alloc(uint8_t, output_size);
465 for (int row = 0; row < height; row++) {
466 const uint8_t* src_scan = pConverted->GetScanline(row);
467 uint8_t* dest_scan = output_buf + row * src_pitch;
468 if (Bpp == 3) {
469 for (int col = 0; col < width; col++) {
470 *dest_scan++ = src_scan[2];
471 *dest_scan++ = src_scan[1];
472 *dest_scan++ = *src_scan;
473 src_scan += 3;
474 }
475 } else {
476 FXSYS_memcpy(dest_scan, src_scan, src_pitch);
208 } 477 }
209 } 478 }
210 OutputPath(pPathData, stroke_alpha ? NULL : pObject2Device); 479 uint8_t* compressed_buf;
211 if (fill_mode && fill_alpha) { 480 FX_DWORD compressed_size;
212 SetColor(fill_color, alpha_flag, pIccTransform); 481 PSCompressData(m_PSLevel, output_buf, output_size, compressed_buf,
213 if ((fill_mode & 3) == FXFILL_WINDING) { 482 compressed_size, filter);
214 if (stroke_alpha) { 483 if (output_buf != compressed_buf) {
215 OUTPUT_PS("q f Q "); 484 FX_Free(output_buf);
216 } else { 485 }
217 OUTPUT_PS("f"); 486 output_buf = compressed_buf;
218 } 487 output_size = compressed_size;
219 } else if ((fill_mode & 3) == FXFILL_ALTERNATE) { 488 }
220 if (stroke_alpha) { 489 if (pConverted != pSource) {
221 OUTPUT_PS("q F Q "); 490 delete pConverted;
222 } else { 491 pConverted = NULL;
223 OUTPUT_PS("F"); 492 }
224 } 493 buf << FX_BSTRC(" 8[");
494 buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << height
495 << FX_BSTRC("]");
496 buf << FX_BSTRC("currentfile/ASCII85Decode filter ");
497 if (filter) {
498 buf << filter;
499 }
500 buf << FX_BSTRC("false ") << Bpp;
501 buf << FX_BSTRC(" colorimage\n");
502 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
503 WritePSBinary(output_buf, output_size);
504 FX_Free(output_buf);
505 }
506 OUTPUT_PS("\nQ\n");
507 return TRUE;
508 }
509 void CFX_PSRenderer::SetColor(FX_DWORD color,
510 int alpha_flag,
511 void* pIccTransform) {
512 if (!CFX_GEModule::Get()->GetCodecModule() ||
513 !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
514 pIccTransform = NULL;
515 }
516 FX_BOOL bCMYK = FALSE;
517 if (pIccTransform) {
518 ICodec_IccModule* pIccModule =
519 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
520 color = FXGETFLAG_COLORTYPE(alpha_flag) ? FXCMYK_TODIB(color)
521 : FXARGB_TODIB(color);
522 uint8_t* pColor = (uint8_t*)&color;
523 pIccModule->TranslateScanline(pIccTransform, pColor, pColor, 1);
524 color = m_bCmykOutput ? FXCMYK_TODIB(color) : FXARGB_TODIB(color);
525 bCMYK = m_bCmykOutput;
526 } else {
527 bCMYK = FXGETFLAG_COLORTYPE(alpha_flag);
528 }
529 if (bCMYK != m_bCmykOutput || !m_bColorSet || m_LastColor != color) {
530 CFX_ByteTextBuf buf;
531 if (bCMYK) {
532 buf << FXSYS_GetCValue(color) / 255.0 << FX_BSTRC(" ")
533 << FXSYS_GetMValue(color) / 255.0 << FX_BSTRC(" ")
534 << FXSYS_GetYValue(color) / 255.0 << FX_BSTRC(" ")
535 << FXSYS_GetKValue(color) / 255.0 << FX_BSTRC(" k\n");
536 } else {
537 buf << FXARGB_R(color) / 255.0 << FX_BSTRC(" ") << FXARGB_G(color) / 255.0
538 << FX_BSTRC(" ") << FXARGB_B(color) / 255.0 << FX_BSTRC(" rg\n");
539 }
540 if (bCMYK == m_bCmykOutput) {
541 m_bColorSet = TRUE;
542 m_LastColor = color;
543 }
544 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
545 }
546 }
547 void CFX_PSRenderer::FindPSFontGlyph(CFX_FaceCache* pFaceCache,
548 CFX_Font* pFont,
549 const FXTEXT_CHARPOS& charpos,
550 int& ps_fontnum,
551 int& ps_glyphindex) {
552 for (int i = 0; i < (int)m_PSFontList.GetSize(); i++) {
553 CPSFont* pPSFont = m_PSFontList[i];
554 for (int j = 0; j < pPSFont->m_nGlyphs; j++)
555 if (pPSFont->m_Glyphs[j].m_pFont == pFont &&
556 pPSFont->m_Glyphs[j].m_GlyphIndex == charpos.m_GlyphIndex) {
557 if ((!pPSFont->m_Glyphs[j].m_bGlyphAdjust && !charpos.m_bGlyphAdjust) ||
558 (pPSFont->m_Glyphs[j].m_bGlyphAdjust && charpos.m_bGlyphAdjust &&
559 (FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[0] -
560 charpos.m_AdjustMatrix[0]) < 0.01 &&
561 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[1] -
562 charpos.m_AdjustMatrix[1]) < 0.01 &&
563 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[2] -
564 charpos.m_AdjustMatrix[2]) < 0.01 &&
565 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[3] -
566 charpos.m_AdjustMatrix[3]) < 0.01))) {
567 ps_fontnum = i;
568 ps_glyphindex = j;
569 return;
225 } 570 }
226 } 571 }
227 if (stroke_alpha) { 572 }
228 SetColor(stroke_color, alpha_flag, pIccTransform); 573 if (m_PSFontList.GetSize() == 0 ||
229 if (pObject2Device) { 574 m_PSFontList[m_PSFontList.GetSize() - 1]->m_nGlyphs == 256) {
230 OUTPUT_PS("s sm"); 575 CPSFont* pPSFont = new CPSFont;
231 } else { 576 pPSFont->m_nGlyphs = 0;
232 OUTPUT_PS("s"); 577 m_PSFontList.Add(pPSFont);
233 } 578 CFX_ByteTextBuf buf;
234 } 579 buf << FX_BSTRC(
235 OUTPUT_PS("\n"); 580 "8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\n"
581 "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{Encoding "
582 "exch/.notdef put}for\n"
583 "/CharProcs 1 dict def CharProcs begin/.notdef {} def end\n"
584 "/BuildGlyph{1 0 -10 -10 10 10 setcachedevice exch/CharProcs get exch "
585 "2 copy known not{pop/.notdef}if get exec}bind def\n"
586 "/BuildChar{1 index/Encoding get exch get 1 index/BuildGlyph get "
587 "exec}bind def\n"
588 "currentdict end\n");
589 buf << FX_BSTRC("/X") << m_PSFontList.GetSize() - 1
590 << FX_BSTRC(" exch definefont pop\n");
591 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
592 buf.Clear();
593 }
594 ps_fontnum = m_PSFontList.GetSize() - 1;
595 CPSFont* pPSFont = m_PSFontList[ps_fontnum];
596 ps_glyphindex = pPSFont->m_nGlyphs;
597 pPSFont->m_Glyphs[ps_glyphindex].m_GlyphIndex = charpos.m_GlyphIndex;
598 pPSFont->m_Glyphs[ps_glyphindex].m_pFont = pFont;
599 pPSFont->m_Glyphs[ps_glyphindex].m_bGlyphAdjust = charpos.m_bGlyphAdjust;
600 if (charpos.m_bGlyphAdjust) {
601 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[0] =
602 charpos.m_AdjustMatrix[0];
603 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[1] =
604 charpos.m_AdjustMatrix[1];
605 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[2] =
606 charpos.m_AdjustMatrix[2];
607 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[3] =
608 charpos.m_AdjustMatrix[3];
609 }
610 pPSFont->m_nGlyphs++;
611 CFX_AffineMatrix matrix;
612 if (charpos.m_bGlyphAdjust)
613 matrix.Set(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
614 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
615 matrix.Concat(1.0f, 0, 0, 1.0f, 0, 0);
616 const CFX_PathData* pPathData = pFaceCache->LoadGlyphPath(
617 pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
618 if (pPathData == NULL) {
619 return;
620 }
621 CFX_PathData TransformedPath(*pPathData);
622 if (charpos.m_bGlyphAdjust) {
623 TransformedPath.Transform(&matrix);
624 }
625 CFX_ByteTextBuf buf;
626 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff/CharProcs get begin/")
627 << ps_glyphindex << FX_BSTRC("{");
628 buf << FX_BSTRC("n ");
629 for (int p = 0; p < TransformedPath.GetPointCount(); p++) {
630 FX_FLOAT x = TransformedPath.GetPointX(p), y = TransformedPath.GetPointY(p);
631 switch (TransformedPath.GetFlag(p) & FXPT_TYPE) {
632 case FXPT_MOVETO: {
633 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" m\n");
634 break;
635 }
636 case FXPT_LINETO: {
637 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" l\n");
638 break;
639 }
640 case FXPT_BEZIERTO: {
641 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" ")
642 << TransformedPath.GetPointX(p + 1) << FX_BSTRC(" ")
643 << TransformedPath.GetPointY(p + 1) << FX_BSTRC(" ")
644 << TransformedPath.GetPointX(p + 2) << FX_BSTRC(" ")
645 << TransformedPath.GetPointY(p + 2) << FX_BSTRC(" c\n");
646 p += 2;
647 break;
648 }
649 }
650 }
651 buf << FX_BSTRC("f");
652 buf << FX_BSTRC("}bind def end\n");
653 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff/Encoding get ")
654 << ps_glyphindex << FX_BSTRC("/") << ps_glyphindex << FX_BSTRC(" put\n");
655 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
656 }
657 FX_BOOL CFX_PSRenderer::DrawText(int nChars,
658 const FXTEXT_CHARPOS* pCharPos,
659 CFX_Font* pFont,
660 CFX_FontCache* pCache,
661 const CFX_AffineMatrix* pObject2Device,
662 FX_FLOAT font_size,
663 FX_DWORD color,
664 int alpha_flag,
665 void* pIccTransform) {
666 StartRendering();
667 int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(alpha_flag)
668 : FXARGB_A(color);
669 if (alpha < 255) {
670 return FALSE;
671 }
672 if ((pObject2Device->a == 0 && pObject2Device->b == 0) ||
673 (pObject2Device->c == 0 && pObject2Device->d == 0)) {
236 return TRUE; 674 return TRUE;
237 } 675 }
238 void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) 676 SetColor(color, alpha_flag, pIccTransform);
239 { 677 CFX_ByteTextBuf buf;
240 CFX_ByteTextBuf buf; 678 buf << FX_BSTRC("q[") << pObject2Device->a << FX_BSTRC(" ")
241 if (!m_bGraphStateSet || m_CurGraphState.m_LineCap != pGraphState->m_LineCap ) { 679 << pObject2Device->b << FX_BSTRC(" ") << pObject2Device->c
242 buf << pGraphState->m_LineCap << FX_BSTRC(" J\n"); 680 << FX_BSTRC(" ") << pObject2Device->d;
243 } 681 buf << FX_BSTRC(" ") << pObject2Device->e << FX_BSTRC(" ")
244 if (!m_bGraphStateSet || m_CurGraphState.m_DashCount != pGraphState->m_DashC ount || 682 << pObject2Device->f << "]cm\n";
245 FXSYS_memcmp(m_CurGraphState.m_DashArray, pGraphState->m_DashArray, sizeof(FX_FLOAT)*m_CurGraphState.m_DashCount)) { 683 if (pCache == NULL) {
246 buf << FX_BSTRC("["); 684 pCache = CFX_GEModule::Get()->GetFontCache();
247 for (int i = 0; i < pGraphState->m_DashCount; i ++) { 685 }
248 buf << pGraphState->m_DashArray[i] << FX_BSTRC(" "); 686 CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont);
249 } 687 FX_FONTCACHE_DEFINE(pCache, pFont);
250 buf << FX_BSTRC("]") << pGraphState->m_DashPhase << FX_BSTRC(" d\n"); 688 int last_fontnum = -1;
251 } 689 for (int i = 0; i < nChars; i++) {
252 if (!m_bGraphStateSet || m_CurGraphState.m_LineJoin != pGraphState->m_LineJo in) { 690 int ps_fontnum, ps_glyphindex;
253 buf << pGraphState->m_LineJoin << FX_BSTRC(" j\n"); 691 FindPSFontGlyph(pFaceCache, pFont, pCharPos[i], ps_fontnum, ps_glyphindex);
254 } 692 if (last_fontnum != ps_fontnum) {
255 if (!m_bGraphStateSet || m_CurGraphState.m_LineWidth != pGraphState->m_LineW idth) { 693 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff ") << font_size
256 buf << pGraphState->m_LineWidth << FX_BSTRC(" w\n"); 694 << FX_BSTRC(" Fs Sf ");
257 } 695 last_fontnum = ps_fontnum;
258 if (!m_bGraphStateSet || m_CurGraphState.m_MiterLimit != pGraphState->m_Mite rLimit) { 696 }
259 buf << pGraphState->m_MiterLimit << FX_BSTRC(" M\n"); 697 buf << pCharPos[i].m_OriginX << FX_BSTRC(" ") << pCharPos[i].m_OriginY
260 } 698 << FX_BSTRC(" m");
261 m_CurGraphState.Copy(*pGraphState); 699 CFX_ByteString hex;
262 m_bGraphStateSet = TRUE; 700 hex.Format("<%02X>", ps_glyphindex);
263 if (buf.GetSize()) { 701 buf << hex << FX_BSTRC("Tj\n");
264 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize()); 702 }
265 } 703 buf << FX_BSTRC("Q\n");
266 } 704 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
267 static void FaxCompressData(uint8_t* src_buf, int width, int height, uint8_t*& d est_buf, FX_DWORD& dest_size) 705 return TRUE;
268 { 706 }
269 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); 707 void CFX_PSRenderer::WritePSBinary(const uint8_t* data, int len) {
270 if (width * height > 128 && pEncoders && pEncoders->GetFaxModule()->Encode(s rc_buf, width, height, (width + 7) / 8, dest_buf, dest_size)) { 708 uint8_t* dest_buf;
271 FX_Free(src_buf); 709 FX_DWORD dest_size;
272 } else { 710 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
273 dest_buf = src_buf; 711 if (pEncoders &&
274 dest_size = (width + 7) / 8 * height; 712 pEncoders->GetBasicModule()->A85Encode(data, len, dest_buf, dest_size)) {
275 } 713 m_pOutput->OutputPS((const FX_CHAR*)dest_buf, dest_size);
276 } 714 FX_Free(dest_buf);
277 static void PSCompressData(int PSLevel, uint8_t* src_buf, FX_DWORD src_size, 715 } else {
278 uint8_t*& output_buf, FX_DWORD& output_size, const FX _CHAR*& filter) 716 m_pOutput->OutputPS((const FX_CHAR*)data, len);
279 { 717 }
280 output_buf = src_buf; 718 }
281 output_size = src_size;
282 filter = "";
283 if (src_size < 1024) {
284 return;
285 }
286 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
287 uint8_t* 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 uint8_t* src_buf = FX_Alloc(uint8_t, src_size);
352 for (int row = 0; row < height; row ++) {
353 const uint8_t* src_scan = pSource->GetScanline(row);
354 FXSYS_memcpy(src_buf + row * pitch, src_scan, pitch);
355 }
356 uint8_t* output_buf;
357 FX_DWORD output_size;
358 FaxCompressData(src_buf, width, height, output_buf, output_size);
359 if (pSource->IsAlphaMask()) {
360 SetColor(color, alpha_flag, pIccTransform);
361 m_bColorSet = FALSE;
362 buf << FX_BSTRC(" true[");
363 } else {
364 buf << FX_BSTRC(" 1[");
365 }
366 buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << heigh t <<
367 FX_BSTRC("]currentfile/ASCII85Decode filter ");
368 if (output_buf != src_buf)
369 buf << FX_BSTRC("<</K -1/EndOfBlock false/Columns ") << width << FX_ BSTRC("/Rows ") << height <<
370 FX_BSTRC(">>/CCITTFaxDecode filter ");
371 if (pSource->IsAlphaMask()) {
372 buf << FX_BSTRC("iM\n");
373 } else {
374 buf << FX_BSTRC("false 1 colorimage\n");
375 }
376 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
377 WritePSBinary(output_buf, output_size);
378 FX_Free(output_buf);
379 } else {
380 CFX_DIBSource* pConverted = (CFX_DIBSource*)pSource;
381 if (pIccTransform) {
382 FXDIB_Format format = m_bCmykOutput ? FXDIB_Cmyk : FXDIB_Rgb;
383 pConverted = pSource->CloneConvert(format, NULL, pIccTransform);
384 } else {
385 switch (pSource->GetFormat()) {
386 case FXDIB_1bppRgb:
387 case FXDIB_Rgb32:
388 pConverted = pSource->CloneConvert(FXDIB_Rgb);
389 break;
390 case FXDIB_8bppRgb:
391 if (pSource->GetPalette() != NULL) {
392 pConverted = pSource->CloneConvert(FXDIB_Rgb);
393 }
394 break;
395 case FXDIB_1bppCmyk:
396 pConverted = pSource->CloneConvert(FXDIB_Cmyk);
397 break;
398 case FXDIB_8bppCmyk:
399 if (pSource->GetPalette() != NULL) {
400 pConverted = pSource->CloneConvert(FXDIB_Cmyk);
401 }
402 break;
403 default:
404 break;
405 }
406 }
407 if (pConverted == NULL) {
408 OUTPUT_PS("\nQ\n");
409 return FALSE;
410 }
411 int Bpp = pConverted->GetBPP() / 8;
412 uint8_t* output_buf = NULL;
413 FX_STRSIZE output_size = 0;
414 const FX_CHAR* filter = NULL;
415 if (flags & FXRENDER_IMAGE_LOSSY) {
416 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
417 if (pEncoders && pEncoders->GetJpegModule()->Encode(pConverted, outp ut_buf, output_size)) {
418 filter = "/DCTDecode filter ";
419 }
420 }
421 if (filter == NULL) {
422 int src_pitch = width * Bpp;
423 output_size = height * src_pitch;
424 output_buf = FX_Alloc(uint8_t, output_size);
425 for (int row = 0; row < height; row ++) {
426 const uint8_t* src_scan = pConverted->GetScanline(row);
427 uint8_t* dest_scan = output_buf + row * src_pitch;
428 if (Bpp == 3) {
429 for (int col = 0; col < width; col ++) {
430 *dest_scan++ = src_scan[2];
431 *dest_scan++ = src_scan[1];
432 *dest_scan++ = *src_scan;
433 src_scan += 3;
434 }
435 } else {
436 FXSYS_memcpy(dest_scan, src_scan, src_pitch);
437 }
438 }
439 uint8_t* compressed_buf;
440 FX_DWORD compressed_size;
441 PSCompressData(m_PSLevel, output_buf, output_size, compressed_buf, c ompressed_size, filter);
442 if (output_buf != compressed_buf) {
443 FX_Free(output_buf);
444 }
445 output_buf = compressed_buf;
446 output_size = compressed_size;
447 }
448 if (pConverted != pSource) {
449 delete pConverted;
450 pConverted = NULL;
451 }
452 buf << FX_BSTRC(" 8[");
453 buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << heigh t << FX_BSTRC("]");
454 buf << FX_BSTRC("currentfile/ASCII85Decode filter ");
455 if (filter) {
456 buf << filter;
457 }
458 buf << FX_BSTRC("false ") << Bpp;
459 buf << FX_BSTRC(" colorimage\n");
460 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
461 WritePSBinary(output_buf, output_size);
462 FX_Free(output_buf);
463 }
464 OUTPUT_PS("\nQ\n");
465 return TRUE;
466 }
467 void CFX_PSRenderer::SetColor(FX_DWORD color, int alpha_flag, void* pIccTransfor m)
468 {
469 if (!CFX_GEModule::Get()->GetCodecModule() || !CFX_GEModule::Get()->GetCodec Module()->GetIccModule()) {
470 pIccTransform = NULL;
471 }
472 FX_BOOL bCMYK = FALSE;
473 if (pIccTransform) {
474 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->Ge tIccModule();
475 color = FXGETFLAG_COLORTYPE(alpha_flag) ? FXCMYK_TODIB(color) : FXARGB_T ODIB(color);
476 uint8_t* pColor = (uint8_t*)&color;
477 pIccModule->TranslateScanline(pIccTransform, pColor, pColor, 1);
478 color = m_bCmykOutput ? FXCMYK_TODIB(color) : FXARGB_TODIB(color);
479 bCMYK = m_bCmykOutput;
480 } else {
481 bCMYK = FXGETFLAG_COLORTYPE(alpha_flag);
482 }
483 if (bCMYK != m_bCmykOutput || !m_bColorSet || m_LastColor != color) {
484 CFX_ByteTextBuf buf;
485 if (bCMYK) {
486 buf << FXSYS_GetCValue(color) / 255.0 << FX_BSTRC(" ") << FXSYS_GetM Value(color) / 255.0 << FX_BSTRC(" ")
487 << FXSYS_GetYValue(color) / 255.0 << FX_BSTRC(" ") << FXSYS_GetK Value(color) / 255.0 << FX_BSTRC(" k\n");
488 } else {
489 buf << FXARGB_R(color) / 255.0 << FX_BSTRC(" ") << FXARGB_G(color) / 255.0 << FX_BSTRC(" ")
490 << FXARGB_B(color) / 255.0 << FX_BSTRC(" rg\n");
491 }
492 if (bCMYK == m_bCmykOutput) {
493 m_bColorSet = TRUE;
494 m_LastColor = color;
495 }
496 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
497 }
498 }
499 void CFX_PSRenderer::FindPSFontGlyph(CFX_FaceCache* pFaceCache, CFX_Font* pFont, const FXTEXT_CHARPOS& charpos,
500 int& ps_fontnum, int &ps_glyphindex)
501 {
502 for (int i = 0; i < (int)m_PSFontList.GetSize(); i ++) {
503 CPSFont* pPSFont = m_PSFontList[i];
504 for (int j = 0; j < pPSFont->m_nGlyphs; j ++)
505 if (pPSFont->m_Glyphs[j].m_pFont == pFont && pPSFont->m_Glyphs[j].m_ GlyphIndex == charpos.m_GlyphIndex) {
506 if ((!pPSFont->m_Glyphs[j].m_bGlyphAdjust && !charpos.m_bGlyphAd just) ||
507 (pPSFont->m_Glyphs[j].m_bGlyphAdjust && charpos.m_bGlyph Adjust &&
508 (FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[0] - ch arpos.m_AdjustMatrix[0]) < 0.01 &&
509 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[1] - ch arpos.m_AdjustMatrix[1]) < 0.01 &&
510 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[2] - ch arpos.m_AdjustMatrix[2]) < 0.01 &&
511 FXSYS_fabs(pPSFont->m_Glyphs[j].m_AdjustMatrix[3] - ch arpos.m_AdjustMatrix[3]) < 0.01))) {
512 ps_fontnum = i;
513 ps_glyphindex = j;
514 return;
515 }
516 }
517 }
518 if (m_PSFontList.GetSize() == 0 || m_PSFontList[m_PSFontList.GetSize() - 1]- >m_nGlyphs == 256) {
519 CPSFont* pPSFont = new CPSFont;
520 pPSFont->m_nGlyphs = 0;
521 m_PSFontList.Add(pPSFont);
522 CFX_ByteTextBuf buf;
523 buf << FX_BSTRC("8 dict begin/FontType 3 def/FontMatrix[1 0 0 1 0 0]def\ n"
524 "/FontBBox[0 0 0 0]def/Encoding 256 array def 0 1 255{En coding exch/.notdef put}for\n"
525 "/CharProcs 1 dict def CharProcs begin/.notdef {} def en d\n"
526 "/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"
527 "/BuildChar{1 index/Encoding get exch get 1 index/BuildG lyph get exec}bind def\n"
528 "currentdict end\n");
529 buf << FX_BSTRC("/X") << m_PSFontList.GetSize() - 1 << FX_BSTRC(" exch d efinefont pop\n");
530 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
531 buf.Clear();
532 }
533 ps_fontnum = m_PSFontList.GetSize() - 1;
534 CPSFont* pPSFont = m_PSFontList[ps_fontnum];
535 ps_glyphindex = pPSFont->m_nGlyphs;
536 pPSFont->m_Glyphs[ps_glyphindex].m_GlyphIndex = charpos.m_GlyphIndex;
537 pPSFont->m_Glyphs[ps_glyphindex].m_pFont = pFont;
538 pPSFont->m_Glyphs[ps_glyphindex].m_bGlyphAdjust = charpos.m_bGlyphAdjust;
539 if (charpos.m_bGlyphAdjust) {
540 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[0] = charpos.m_AdjustMat rix[0];
541 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[1] = charpos.m_AdjustMat rix[1];
542 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[2] = charpos.m_AdjustMat rix[2];
543 pPSFont->m_Glyphs[ps_glyphindex].m_AdjustMatrix[3] = charpos.m_AdjustMat rix[3];
544 }
545 pPSFont->m_nGlyphs ++;
546 CFX_AffineMatrix matrix;
547 if (charpos.m_bGlyphAdjust)
548 matrix.Set(charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
549 charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
550 matrix.Concat(1.0f, 0, 0, 1.0f, 0, 0);
551 const CFX_PathData* pPathData = pFaceCache->LoadGlyphPath(pFont, charpos.m_G lyphIndex, charpos.m_FontCharWidth);
552 if (pPathData == NULL) {
553 return;
554 }
555 CFX_PathData TransformedPath(*pPathData);
556 if (charpos.m_bGlyphAdjust) {
557 TransformedPath.Transform(&matrix);
558 }
559 CFX_ByteTextBuf buf;
560 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff/CharProcs get begin/")
561 << ps_glyphindex << FX_BSTRC("{");
562 buf << FX_BSTRC("n ");
563 for (int p = 0; p < TransformedPath.GetPointCount(); p ++) {
564 FX_FLOAT x = TransformedPath.GetPointX(p), y = TransformedPath.GetPointY (p);
565 switch (TransformedPath.GetFlag(p) & FXPT_TYPE) {
566 case FXPT_MOVETO: {
567 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" m\n");
568 break;
569 }
570 case FXPT_LINETO: {
571 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" l\n");
572 break;
573 }
574 case FXPT_BEZIERTO: {
575 buf << x << FX_BSTRC(" ") << y << FX_BSTRC(" ")
576 << TransformedPath.GetPointX(p + 1) << FX_BSTRC(" ")
577 << TransformedPath.GetPointY(p + 1) << FX_BSTRC(" ")
578 << TransformedPath.GetPointX(p + 2) << FX_BSTRC(" ")
579 << TransformedPath.GetPointY(p + 2) << FX_BSTRC(" c\n");
580 p += 2;
581 break;
582 }
583 }
584 }
585 buf << FX_BSTRC("f");
586 buf << FX_BSTRC("}bind def end\n");
587 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff/Encoding get ") << ps_g lyphindex
588 << FX_BSTRC("/") << ps_glyphindex << FX_BSTRC(" put\n");
589 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
590 }
591 FX_BOOL CFX_PSRenderer::DrawText(int nChars, const FXTEXT_CHARPOS* pCharPos, CFX _Font* pFont,
592 CFX_FontCache* pCache, const CFX_AffineMatrix* pObject2Device,
593 FX_FLOAT font_size, FX_DWORD color,
594 int alpha_flag, void* pIccTransform)
595 {
596 StartRendering();
597 int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(alpha_fla g) : FXARGB_A(color);
598 if (alpha < 255) {
599 return FALSE;
600 }
601 if ((pObject2Device->a == 0 && pObject2Device->b == 0) || (pObject2Device->c == 0 && pObject2Device->d == 0)) {
602 return TRUE;
603 }
604 SetColor(color, alpha_flag, pIccTransform);
605 CFX_ByteTextBuf buf;
606 buf << FX_BSTRC("q[") << pObject2Device->a << FX_BSTRC(" ") << pObject2Devic e->b << FX_BSTRC(" ")
607 << pObject2Device->c << FX_BSTRC(" ") << pObject2Device->d;
608 buf << FX_BSTRC(" ") << pObject2Device->e << FX_BSTRC(" ") << pObject2Device ->f << "]cm\n";
609 if (pCache == NULL) {
610 pCache = CFX_GEModule::Get()->GetFontCache();
611 }
612 CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont);
613 FX_FONTCACHE_DEFINE(pCache, pFont);
614 int last_fontnum = -1;
615 for (int i = 0; i < nChars; i ++) {
616 int ps_fontnum, ps_glyphindex;
617 FindPSFontGlyph(pFaceCache, pFont, pCharPos[i], ps_fontnum, ps_glyphinde x);
618 if (last_fontnum != ps_fontnum) {
619 buf << FX_BSTRC("/X") << ps_fontnum << FX_BSTRC(" Ff ") << font_size
620 << FX_BSTRC(" Fs Sf ");
621 last_fontnum = ps_fontnum;
622 }
623 buf << pCharPos[i].m_OriginX << FX_BSTRC(" ")
624 << pCharPos[i].m_OriginY << FX_BSTRC(" m");
625 CFX_ByteString hex;
626 hex.Format("<%02X>", ps_glyphindex);
627 buf << hex << FX_BSTRC("Tj\n");
628 }
629 buf << FX_BSTRC("Q\n");
630 m_pOutput->OutputPS((const FX_CHAR*)buf.GetBuffer(), buf.GetSize());
631 return TRUE;
632 }
633 void CFX_PSRenderer::WritePSBinary(const uint8_t* data, int len)
634 {
635 uint8_t* dest_buf;
636 FX_DWORD dest_size;
637 CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule();
638 if (pEncoders && pEncoders->GetBasicModule()->A85Encode(data, len, dest_buf, dest_size)) {
639 m_pOutput->OutputPS((const FX_CHAR*)dest_buf, dest_size);
640 FX_Free(dest_buf);
641 } else {
642 m_pOutput->OutputPS((const FX_CHAR*)data, len);
643 }
644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698