Merge lp:~matttbe/ubuntu/raring/poppler/lp1072129 into lp:ubuntu/raring/poppler
- Raring (13.04)
- lp1072129
- Merge into raring
Proposed by
Matthieu Baerts
Status: | Merged |
---|---|
Merged at revision: | 120 |
Proposed branch: | lp:~matttbe/ubuntu/raring/poppler/lp1072129 |
Merge into: | lp:ubuntu/raring/poppler |
Diff against target: |
3231 lines (+3184/-4) 6 files modified
.pc/applied-patches (+1/-0) .pc/git_gouraud_shading_support.patch/poppler/CairoOutputDev.cc (+3121/-0) debian/changelog (+8/-0) debian/patches/git_gouraud_shading_support.patch (+38/-0) debian/patches/series (+1/-0) poppler/CairoOutputDev.cc (+15/-4) |
To merge this branch: | bzr merge lp:~matttbe/ubuntu/raring/poppler/lp1072129 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu Sponsors | Pending | ||
Review via email: mp+134177@code.launchpad.net |
Commit message
Description of the change
Hello,
Due to a bug in poppler, Evince crash when loading some PDF files (LP: #1072129).
This new version simply adds the upstream patch in order to fix this bug.
For more details about this new version, please have a look at this bug #1072129.
Thank you for your help! :)
PS: You can easily test the new version by using these packages:
- Ubuntu Raring 13.04: https:/
- Ubuntu Quantal 12.10: https:/
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '.pc/applied-patches' | |||
2 | --- .pc/applied-patches 2012-08-14 13:11:05 +0000 | |||
3 | +++ .pc/applied-patches 2012-11-13 19:26:27 +0000 | |||
4 | @@ -1,2 +1,3 @@ | |||
5 | 1 | ltmain-as-needed.diff | 1 | ltmain-as-needed.diff |
6 | 2 | qt4-visibility.diff | 2 | qt4-visibility.diff |
7 | 3 | git_gouraud_shading_support.patch | ||
8 | 3 | 4 | ||
9 | === added directory '.pc/git_gouraud_shading_support.patch' | |||
10 | === added directory '.pc/git_gouraud_shading_support.patch/poppler' | |||
11 | === added file '.pc/git_gouraud_shading_support.patch/poppler/CairoOutputDev.cc' | |||
12 | --- .pc/git_gouraud_shading_support.patch/poppler/CairoOutputDev.cc 1970-01-01 00:00:00 +0000 | |||
13 | +++ .pc/git_gouraud_shading_support.patch/poppler/CairoOutputDev.cc 2012-11-13 19:26:27 +0000 | |||
14 | @@ -0,0 +1,3121 @@ | |||
15 | 1 | //======================================================================== | ||
16 | 2 | // | ||
17 | 3 | // CairoOutputDev.cc | ||
18 | 4 | // | ||
19 | 5 | // Copyright 2003 Glyph & Cog, LLC | ||
20 | 6 | // Copyright 2004 Red Hat, Inc | ||
21 | 7 | // | ||
22 | 8 | //======================================================================== | ||
23 | 9 | |||
24 | 10 | //======================================================================== | ||
25 | 11 | // | ||
26 | 12 | // Modified under the Poppler project - http://poppler.freedesktop.org | ||
27 | 13 | // | ||
28 | 14 | // All changes made under the Poppler project to this file are licensed | ||
29 | 15 | // under GPL version 2 or later | ||
30 | 16 | // | ||
31 | 17 | // Copyright (C) 2005-2008 Jeff Muizelaar <jeff@infidigm.net> | ||
32 | 18 | // Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com> | ||
33 | 19 | // Copyright (C) 2005, 2009, 2012 Albert Astals Cid <aacid@kde.org> | ||
34 | 20 | // Copyright (C) 2005 Nickolay V. Shmyrev <nshmyrev@yandex.ru> | ||
35 | 21 | // Copyright (C) 2006-2011 Carlos Garcia Campos <carlosgc@gnome.org> | ||
36 | 22 | // Copyright (C) 2008 Carl Worth <cworth@cworth.org> | ||
37 | 23 | // Copyright (C) 2008-2012 Adrian Johnson <ajohnson@redneon.com> | ||
38 | 24 | // Copyright (C) 2008 Michael Vrable <mvrable@cs.ucsd.edu> | ||
39 | 25 | // Copyright (C) 2008, 2009 Chris Wilson <chris@chris-wilson.co.uk> | ||
40 | 26 | // Copyright (C) 2008 Hib Eris <hib@hiberis.nl> | ||
41 | 27 | // Copyright (C) 2009, 2010 David Benjamin <davidben@mit.edu> | ||
42 | 28 | // Copyright (C) 2011, 2012 Thomas Freitag <Thomas.Freitag@alfa.de> | ||
43 | 29 | // Copyright (C) 2012 Patrick Pfeifer <p2000@mailinator.com> | ||
44 | 30 | // | ||
45 | 31 | // To see a description of the changes please see the Changelog file that | ||
46 | 32 | // came with your tarball or type make ChangeLog if you are building from git | ||
47 | 33 | // | ||
48 | 34 | //======================================================================== | ||
49 | 35 | |||
50 | 36 | #include <config.h> | ||
51 | 37 | |||
52 | 38 | #ifdef USE_GCC_PRAGMAS | ||
53 | 39 | #pragma implementation | ||
54 | 40 | #endif | ||
55 | 41 | |||
56 | 42 | #include <string.h> | ||
57 | 43 | #include <math.h> | ||
58 | 44 | #include <assert.h> | ||
59 | 45 | #include <cairo.h> | ||
60 | 46 | |||
61 | 47 | #include "goo/gfile.h" | ||
62 | 48 | #include "goo/gtypes_p.h" | ||
63 | 49 | #include "GlobalParams.h" | ||
64 | 50 | #include "Error.h" | ||
65 | 51 | #include "Object.h" | ||
66 | 52 | #include "Gfx.h" | ||
67 | 53 | #include "GfxState.h" | ||
68 | 54 | #include "GfxFont.h" | ||
69 | 55 | #include "Page.h" | ||
70 | 56 | #include "Link.h" | ||
71 | 57 | #include "FontEncodingTables.h" | ||
72 | 58 | #include "PDFDocEncoding.h" | ||
73 | 59 | #include <fofi/FoFiTrueType.h> | ||
74 | 60 | #include <splash/SplashBitmap.h> | ||
75 | 61 | #include "CairoOutputDev.h" | ||
76 | 62 | #include "CairoFontEngine.h" | ||
77 | 63 | #include "CairoRescaleBox.h" | ||
78 | 64 | #include "UTF8.h" | ||
79 | 65 | //------------------------------------------------------------------------ | ||
80 | 66 | |||
81 | 67 | // #define LOG_CAIRO | ||
82 | 68 | |||
83 | 69 | #ifdef LOG_CAIRO | ||
84 | 70 | #define LOG(x) (x) | ||
85 | 71 | #else | ||
86 | 72 | #define LOG(x) | ||
87 | 73 | #endif | ||
88 | 74 | |||
89 | 75 | static inline void printMatrix(cairo_matrix_t *matrix){ | ||
90 | 76 | printf("%f %f, %f %f (%f %f)\n", matrix->xx, matrix->yx, | ||
91 | 77 | matrix->xy, matrix->yy, | ||
92 | 78 | matrix->x0, matrix->y0); | ||
93 | 79 | } | ||
94 | 80 | |||
95 | 81 | |||
96 | 82 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) | ||
97 | 83 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) | ||
98 | 84 | |||
99 | 85 | |||
100 | 86 | //------------------------------------------------------------------------ | ||
101 | 87 | // CairoImage | ||
102 | 88 | //------------------------------------------------------------------------ | ||
103 | 89 | |||
104 | 90 | CairoImage::CairoImage (double x1, double y1, double x2, double y2) { | ||
105 | 91 | this->image = NULL; | ||
106 | 92 | this->x1 = x1; | ||
107 | 93 | this->y1 = y1; | ||
108 | 94 | this->x2 = x2; | ||
109 | 95 | this->y2 = y2; | ||
110 | 96 | } | ||
111 | 97 | |||
112 | 98 | CairoImage::~CairoImage () { | ||
113 | 99 | if (image) | ||
114 | 100 | cairo_surface_destroy (image); | ||
115 | 101 | } | ||
116 | 102 | |||
117 | 103 | void CairoImage::setImage (cairo_surface_t *image) { | ||
118 | 104 | if (this->image) | ||
119 | 105 | cairo_surface_destroy (this->image); | ||
120 | 106 | this->image = cairo_surface_reference (image); | ||
121 | 107 | } | ||
122 | 108 | |||
123 | 109 | //------------------------------------------------------------------------ | ||
124 | 110 | // CairoOutputDev | ||
125 | 111 | //------------------------------------------------------------------------ | ||
126 | 112 | |||
127 | 113 | // We cannot tie the lifetime of an FT_Library object to that of | ||
128 | 114 | // CairoOutputDev, since any FT_Faces created with it may end up with a | ||
129 | 115 | // reference by Cairo which can be held long after the CairoOutputDev is | ||
130 | 116 | // deleted. The simplest way to avoid problems is to never tear down the | ||
131 | 117 | // FT_Library instance; to avoid leaks, just use a single global instance | ||
132 | 118 | // initialized the first time it is needed. | ||
133 | 119 | FT_Library CairoOutputDev::ft_lib; | ||
134 | 120 | GBool CairoOutputDev::ft_lib_initialized = gFalse; | ||
135 | 121 | |||
136 | 122 | CairoOutputDev::CairoOutputDev() { | ||
137 | 123 | doc = NULL; | ||
138 | 124 | |||
139 | 125 | if (!ft_lib_initialized) { | ||
140 | 126 | FT_Init_FreeType(&ft_lib); | ||
141 | 127 | ft_lib_initialized = gTrue; | ||
142 | 128 | } | ||
143 | 129 | |||
144 | 130 | fontEngine = NULL; | ||
145 | 131 | fontEngine_owner = gFalse; | ||
146 | 132 | glyphs = NULL; | ||
147 | 133 | fill_pattern = NULL; | ||
148 | 134 | fill_color.r = fill_color.g = fill_color.b = 0; | ||
149 | 135 | stroke_pattern = NULL; | ||
150 | 136 | stroke_color.r = stroke_color.g = stroke_color.b = 0; | ||
151 | 137 | stroke_opacity = 1.0; | ||
152 | 138 | fill_opacity = 1.0; | ||
153 | 139 | textClipPath = NULL; | ||
154 | 140 | strokePathClip = NULL; | ||
155 | 141 | cairo = NULL; | ||
156 | 142 | currentFont = NULL; | ||
157 | 143 | prescaleImages = gTrue; | ||
158 | 144 | printing = gTrue; | ||
159 | 145 | use_show_text_glyphs = gFalse; | ||
160 | 146 | inType3Char = gFalse; | ||
161 | 147 | t3_glyph_has_bbox = gFalse; | ||
162 | 148 | |||
163 | 149 | groupColorSpaceStack = NULL; | ||
164 | 150 | maskStack = NULL; | ||
165 | 151 | group = NULL; | ||
166 | 152 | mask = NULL; | ||
167 | 153 | shape = NULL; | ||
168 | 154 | cairo_shape = NULL; | ||
169 | 155 | knockoutCount = 0; | ||
170 | 156 | |||
171 | 157 | text = NULL; | ||
172 | 158 | actualText = NULL; | ||
173 | 159 | |||
174 | 160 | // the SA parameter supposedly defaults to false, but Acrobat | ||
175 | 161 | // apparently hardwires it to true | ||
176 | 162 | stroke_adjust = globalParams->getStrokeAdjust(); | ||
177 | 163 | align_stroke_coords = gFalse; | ||
178 | 164 | adjusted_stroke_width = gFalse; | ||
179 | 165 | } | ||
180 | 166 | |||
181 | 167 | CairoOutputDev::~CairoOutputDev() { | ||
182 | 168 | if (fontEngine_owner && fontEngine) { | ||
183 | 169 | delete fontEngine; | ||
184 | 170 | } | ||
185 | 171 | |||
186 | 172 | if (cairo) | ||
187 | 173 | cairo_destroy (cairo); | ||
188 | 174 | cairo_pattern_destroy (stroke_pattern); | ||
189 | 175 | cairo_pattern_destroy (fill_pattern); | ||
190 | 176 | if (group) | ||
191 | 177 | cairo_pattern_destroy (group); | ||
192 | 178 | if (mask) | ||
193 | 179 | cairo_pattern_destroy (mask); | ||
194 | 180 | if (shape) | ||
195 | 181 | cairo_pattern_destroy (shape); | ||
196 | 182 | if (text) | ||
197 | 183 | text->decRefCnt(); | ||
198 | 184 | if (actualText) | ||
199 | 185 | delete actualText; | ||
200 | 186 | } | ||
201 | 187 | |||
202 | 188 | void CairoOutputDev::setCairo(cairo_t *cairo) | ||
203 | 189 | { | ||
204 | 190 | if (this->cairo != NULL) { | ||
205 | 191 | cairo_status_t status = cairo_status (this->cairo); | ||
206 | 192 | if (status) { | ||
207 | 193 | error(errInternal, -1, "cairo context error: {0:s}\n", cairo_status_to_string(status)); | ||
208 | 194 | } | ||
209 | 195 | cairo_destroy (this->cairo); | ||
210 | 196 | assert(!cairo_shape); | ||
211 | 197 | } | ||
212 | 198 | if (cairo != NULL) { | ||
213 | 199 | this->cairo = cairo_reference (cairo); | ||
214 | 200 | /* save the initial matrix so that we can use it for type3 fonts. */ | ||
215 | 201 | //XXX: is this sufficient? could we miss changes to the matrix somehow? | ||
216 | 202 | cairo_get_matrix(cairo, &orig_matrix); | ||
217 | 203 | } else { | ||
218 | 204 | this->cairo = NULL; | ||
219 | 205 | this->cairo_shape = NULL; | ||
220 | 206 | } | ||
221 | 207 | } | ||
222 | 208 | |||
223 | 209 | void CairoOutputDev::setTextPage(TextPage *text) | ||
224 | 210 | { | ||
225 | 211 | if (this->text) | ||
226 | 212 | this->text->decRefCnt(); | ||
227 | 213 | if (actualText) | ||
228 | 214 | delete actualText; | ||
229 | 215 | if (text) { | ||
230 | 216 | this->text = text; | ||
231 | 217 | this->text->incRefCnt(); | ||
232 | 218 | actualText = new ActualText(text); | ||
233 | 219 | } else { | ||
234 | 220 | this->text = NULL; | ||
235 | 221 | actualText = NULL; | ||
236 | 222 | } | ||
237 | 223 | } | ||
238 | 224 | |||
239 | 225 | void CairoOutputDev::startDoc(PDFDoc *docA, | ||
240 | 226 | CairoFontEngine *parentFontEngine) { | ||
241 | 227 | doc = docA; | ||
242 | 228 | if (parentFontEngine) { | ||
243 | 229 | fontEngine = parentFontEngine; | ||
244 | 230 | } else { | ||
245 | 231 | if (fontEngine) { | ||
246 | 232 | delete fontEngine; | ||
247 | 233 | } | ||
248 | 234 | fontEngine = new CairoFontEngine(ft_lib); | ||
249 | 235 | fontEngine_owner = gTrue; | ||
250 | 236 | } | ||
251 | 237 | } | ||
252 | 238 | |||
253 | 239 | void CairoOutputDev::startPage(int pageNum, GfxState *state) { | ||
254 | 240 | /* set up some per page defaults */ | ||
255 | 241 | cairo_pattern_destroy(fill_pattern); | ||
256 | 242 | cairo_pattern_destroy(stroke_pattern); | ||
257 | 243 | |||
258 | 244 | fill_pattern = cairo_pattern_create_rgb(0., 0., 0.); | ||
259 | 245 | stroke_pattern = cairo_pattern_reference(fill_pattern); | ||
260 | 246 | |||
261 | 247 | if (text) | ||
262 | 248 | text->startPage(state); | ||
263 | 249 | } | ||
264 | 250 | |||
265 | 251 | void CairoOutputDev::endPage() { | ||
266 | 252 | if (text) { | ||
267 | 253 | text->endPage(); | ||
268 | 254 | text->coalesce(gTrue, 0, gFalse); | ||
269 | 255 | } | ||
270 | 256 | } | ||
271 | 257 | |||
272 | 258 | void CairoOutputDev::saveState(GfxState *state) { | ||
273 | 259 | LOG(printf ("save\n")); | ||
274 | 260 | cairo_save (cairo); | ||
275 | 261 | if (cairo_shape) | ||
276 | 262 | cairo_save (cairo_shape); | ||
277 | 263 | |||
278 | 264 | MaskStack *ms = new MaskStack; | ||
279 | 265 | ms->mask = cairo_pattern_reference(mask); | ||
280 | 266 | ms->mask_matrix = mask_matrix; | ||
281 | 267 | ms->next = maskStack; | ||
282 | 268 | maskStack = ms; | ||
283 | 269 | } | ||
284 | 270 | |||
285 | 271 | void CairoOutputDev::restoreState(GfxState *state) { | ||
286 | 272 | LOG(printf ("restore\n")); | ||
287 | 273 | cairo_restore (cairo); | ||
288 | 274 | if (cairo_shape) | ||
289 | 275 | cairo_restore (cairo_shape); | ||
290 | 276 | |||
291 | 277 | /* These aren't restored by cairo_restore() since we keep them in | ||
292 | 278 | * the output device. */ | ||
293 | 279 | updateFillColor(state); | ||
294 | 280 | updateStrokeColor(state); | ||
295 | 281 | updateFillOpacity(state); | ||
296 | 282 | updateStrokeOpacity(state); | ||
297 | 283 | updateBlendMode(state); | ||
298 | 284 | |||
299 | 285 | MaskStack* ms = maskStack; | ||
300 | 286 | if (ms) { | ||
301 | 287 | if (mask) | ||
302 | 288 | cairo_pattern_destroy(mask); | ||
303 | 289 | mask = ms->mask; | ||
304 | 290 | mask_matrix = ms->mask_matrix; | ||
305 | 291 | maskStack = ms->next; | ||
306 | 292 | delete ms; | ||
307 | 293 | } | ||
308 | 294 | } | ||
309 | 295 | |||
310 | 296 | void CairoOutputDev::updateAll(GfxState *state) { | ||
311 | 297 | updateLineDash(state); | ||
312 | 298 | updateLineJoin(state); | ||
313 | 299 | updateLineCap(state); | ||
314 | 300 | updateLineWidth(state); | ||
315 | 301 | updateFlatness(state); | ||
316 | 302 | updateMiterLimit(state); | ||
317 | 303 | updateFillColor(state); | ||
318 | 304 | updateStrokeColor(state); | ||
319 | 305 | updateFillOpacity(state); | ||
320 | 306 | updateStrokeOpacity(state); | ||
321 | 307 | updateBlendMode(state); | ||
322 | 308 | needFontUpdate = gTrue; | ||
323 | 309 | if (text) | ||
324 | 310 | text->updateFont(state); | ||
325 | 311 | } | ||
326 | 312 | |||
327 | 313 | void CairoOutputDev::setDefaultCTM(double *ctm) { | ||
328 | 314 | cairo_matrix_t matrix; | ||
329 | 315 | matrix.xx = ctm[0]; | ||
330 | 316 | matrix.yx = ctm[1]; | ||
331 | 317 | matrix.xy = ctm[2]; | ||
332 | 318 | matrix.yy = ctm[3]; | ||
333 | 319 | matrix.x0 = ctm[4]; | ||
334 | 320 | matrix.y0 = ctm[5]; | ||
335 | 321 | |||
336 | 322 | cairo_transform (cairo, &matrix); | ||
337 | 323 | if (cairo_shape) | ||
338 | 324 | cairo_transform (cairo_shape, &matrix); | ||
339 | 325 | |||
340 | 326 | OutputDev::setDefaultCTM(ctm); | ||
341 | 327 | } | ||
342 | 328 | |||
343 | 329 | void CairoOutputDev::updateCTM(GfxState *state, double m11, double m12, | ||
344 | 330 | double m21, double m22, | ||
345 | 331 | double m31, double m32) { | ||
346 | 332 | cairo_matrix_t matrix, invert_matrix; | ||
347 | 333 | matrix.xx = m11; | ||
348 | 334 | matrix.yx = m12; | ||
349 | 335 | matrix.xy = m21; | ||
350 | 336 | matrix.yy = m22; | ||
351 | 337 | matrix.x0 = m31; | ||
352 | 338 | matrix.y0 = m32; | ||
353 | 339 | |||
354 | 340 | /* Make sure the matrix is invertible before setting it. | ||
355 | 341 | * cairo will blow up if we give it a matrix that's not | ||
356 | 342 | * invertible, so we need to check before passing it | ||
357 | 343 | * to cairo_transform. Ignoring it is likely to give better | ||
358 | 344 | * results than not rendering anything at all. See #14398 | ||
359 | 345 | * | ||
360 | 346 | * Ideally, we could do the cairo_transform | ||
361 | 347 | * and then check if anything went wrong and fix it then | ||
362 | 348 | * instead of having to invert the matrix. */ | ||
363 | 349 | invert_matrix = matrix; | ||
364 | 350 | if (cairo_matrix_invert(&invert_matrix)) { | ||
365 | 351 | error(errSyntaxWarning, -1, "matrix not invertible\n"); | ||
366 | 352 | return; | ||
367 | 353 | } | ||
368 | 354 | |||
369 | 355 | cairo_transform (cairo, &matrix); | ||
370 | 356 | if (cairo_shape) | ||
371 | 357 | cairo_transform (cairo_shape, &matrix); | ||
372 | 358 | updateLineDash(state); | ||
373 | 359 | updateLineJoin(state); | ||
374 | 360 | updateLineCap(state); | ||
375 | 361 | updateLineWidth(state); | ||
376 | 362 | } | ||
377 | 363 | |||
378 | 364 | void CairoOutputDev::updateLineDash(GfxState *state) { | ||
379 | 365 | double *dashPattern; | ||
380 | 366 | int dashLength; | ||
381 | 367 | double dashStart; | ||
382 | 368 | |||
383 | 369 | state->getLineDash(&dashPattern, &dashLength, &dashStart); | ||
384 | 370 | cairo_set_dash (cairo, dashPattern, dashLength, dashStart); | ||
385 | 371 | if (cairo_shape) | ||
386 | 372 | cairo_set_dash (cairo_shape, dashPattern, dashLength, dashStart); | ||
387 | 373 | } | ||
388 | 374 | |||
389 | 375 | void CairoOutputDev::updateFlatness(GfxState *state) { | ||
390 | 376 | // cairo_set_tolerance (cairo, state->getFlatness()); | ||
391 | 377 | } | ||
392 | 378 | |||
393 | 379 | void CairoOutputDev::updateLineJoin(GfxState *state) { | ||
394 | 380 | switch (state->getLineJoin()) { | ||
395 | 381 | case 0: | ||
396 | 382 | cairo_set_line_join (cairo, CAIRO_LINE_JOIN_MITER); | ||
397 | 383 | break; | ||
398 | 384 | case 1: | ||
399 | 385 | cairo_set_line_join (cairo, CAIRO_LINE_JOIN_ROUND); | ||
400 | 386 | break; | ||
401 | 387 | case 2: | ||
402 | 388 | cairo_set_line_join (cairo, CAIRO_LINE_JOIN_BEVEL); | ||
403 | 389 | break; | ||
404 | 390 | } | ||
405 | 391 | if (cairo_shape) | ||
406 | 392 | cairo_set_line_join (cairo_shape, cairo_get_line_join(cairo)); | ||
407 | 393 | } | ||
408 | 394 | |||
409 | 395 | void CairoOutputDev::updateLineCap(GfxState *state) { | ||
410 | 396 | switch (state->getLineCap()) { | ||
411 | 397 | case 0: | ||
412 | 398 | cairo_set_line_cap (cairo, CAIRO_LINE_CAP_BUTT); | ||
413 | 399 | break; | ||
414 | 400 | case 1: | ||
415 | 401 | cairo_set_line_cap (cairo, CAIRO_LINE_CAP_ROUND); | ||
416 | 402 | break; | ||
417 | 403 | case 2: | ||
418 | 404 | cairo_set_line_cap (cairo, CAIRO_LINE_CAP_SQUARE); | ||
419 | 405 | break; | ||
420 | 406 | } | ||
421 | 407 | if (cairo_shape) | ||
422 | 408 | cairo_set_line_cap (cairo_shape, cairo_get_line_cap(cairo)); | ||
423 | 409 | } | ||
424 | 410 | |||
425 | 411 | void CairoOutputDev::updateMiterLimit(GfxState *state) { | ||
426 | 412 | cairo_set_miter_limit (cairo, state->getMiterLimit()); | ||
427 | 413 | if (cairo_shape) | ||
428 | 414 | cairo_set_miter_limit (cairo_shape, state->getMiterLimit()); | ||
429 | 415 | } | ||
430 | 416 | |||
431 | 417 | void CairoOutputDev::updateLineWidth(GfxState *state) { | ||
432 | 418 | LOG(printf ("line width: %f\n", state->getLineWidth())); | ||
433 | 419 | adjusted_stroke_width = gFalse; | ||
434 | 420 | double width = state->getLineWidth(); | ||
435 | 421 | if (stroke_adjust && !printing) { | ||
436 | 422 | double x, y; | ||
437 | 423 | x = y = width; | ||
438 | 424 | |||
439 | 425 | /* find out line width in device units */ | ||
440 | 426 | cairo_user_to_device_distance(cairo, &x, &y); | ||
441 | 427 | if (fabs(x) <= 1.0 && fabs(y) <= 1.0) { | ||
442 | 428 | /* adjust width to at least one device pixel */ | ||
443 | 429 | x = y = 1.0; | ||
444 | 430 | cairo_device_to_user_distance(cairo, &x, &y); | ||
445 | 431 | width = MIN(fabs(x),fabs(y)); | ||
446 | 432 | adjusted_stroke_width = gTrue; | ||
447 | 433 | } | ||
448 | 434 | } else if (width == 0.0) { | ||
449 | 435 | /* Cairo does not support 0 line width == 1 device pixel. Find out | ||
450 | 436 | * how big pixels (device unit) are in the x and y | ||
451 | 437 | * directions. Choose the smaller of the two as our line width. | ||
452 | 438 | */ | ||
453 | 439 | double x = 1.0, y = 1.0; | ||
454 | 440 | if (printing) { | ||
455 | 441 | // assume printer pixel size is 1/600 inch | ||
456 | 442 | x = 72.0/600; | ||
457 | 443 | y = 72.0/600; | ||
458 | 444 | } | ||
459 | 445 | cairo_device_to_user_distance(cairo, &x, &y); | ||
460 | 446 | width = MIN(fabs(x),fabs(y)); | ||
461 | 447 | } | ||
462 | 448 | cairo_set_line_width (cairo, width); | ||
463 | 449 | if (cairo_shape) | ||
464 | 450 | cairo_set_line_width (cairo_shape, cairo_get_line_width (cairo)); | ||
465 | 451 | } | ||
466 | 452 | |||
467 | 453 | void CairoOutputDev::updateFillColor(GfxState *state) { | ||
468 | 454 | GfxRGB color = fill_color; | ||
469 | 455 | |||
470 | 456 | state->getFillRGB(&fill_color); | ||
471 | 457 | if (cairo_pattern_get_type (fill_pattern) != CAIRO_PATTERN_TYPE_SOLID || | ||
472 | 458 | color.r != fill_color.r || | ||
473 | 459 | color.g != fill_color.g || | ||
474 | 460 | color.b != fill_color.b) | ||
475 | 461 | { | ||
476 | 462 | cairo_pattern_destroy(fill_pattern); | ||
477 | 463 | fill_pattern = cairo_pattern_create_rgba(colToDbl(fill_color.r), | ||
478 | 464 | colToDbl(fill_color.g), | ||
479 | 465 | colToDbl(fill_color.b), | ||
480 | 466 | fill_opacity); | ||
481 | 467 | |||
482 | 468 | LOG(printf ("fill color: %d %d %d\n", | ||
483 | 469 | fill_color.r, fill_color.g, fill_color.b)); | ||
484 | 470 | } | ||
485 | 471 | } | ||
486 | 472 | |||
487 | 473 | void CairoOutputDev::updateStrokeColor(GfxState *state) { | ||
488 | 474 | GfxRGB color = stroke_color; | ||
489 | 475 | |||
490 | 476 | state->getStrokeRGB(&stroke_color); | ||
491 | 477 | if (cairo_pattern_get_type (fill_pattern) != CAIRO_PATTERN_TYPE_SOLID || | ||
492 | 478 | color.r != stroke_color.r || | ||
493 | 479 | color.g != stroke_color.g || | ||
494 | 480 | color.b != stroke_color.b) | ||
495 | 481 | { | ||
496 | 482 | cairo_pattern_destroy(stroke_pattern); | ||
497 | 483 | stroke_pattern = cairo_pattern_create_rgba(colToDbl(stroke_color.r), | ||
498 | 484 | colToDbl(stroke_color.g), | ||
499 | 485 | colToDbl(stroke_color.b), | ||
500 | 486 | stroke_opacity); | ||
501 | 487 | |||
502 | 488 | LOG(printf ("stroke color: %d %d %d\n", | ||
503 | 489 | stroke_color.r, stroke_color.g, stroke_color.b)); | ||
504 | 490 | } | ||
505 | 491 | } | ||
506 | 492 | |||
507 | 493 | void CairoOutputDev::updateFillOpacity(GfxState *state) { | ||
508 | 494 | double opacity = fill_opacity; | ||
509 | 495 | |||
510 | 496 | fill_opacity = state->getFillOpacity(); | ||
511 | 497 | if (opacity != fill_opacity) { | ||
512 | 498 | cairo_pattern_destroy(fill_pattern); | ||
513 | 499 | fill_pattern = cairo_pattern_create_rgba(colToDbl(fill_color.r), | ||
514 | 500 | colToDbl(fill_color.g), | ||
515 | 501 | colToDbl(fill_color.b), | ||
516 | 502 | fill_opacity); | ||
517 | 503 | |||
518 | 504 | LOG(printf ("fill opacity: %f\n", fill_opacity)); | ||
519 | 505 | } | ||
520 | 506 | } | ||
521 | 507 | |||
522 | 508 | void CairoOutputDev::updateStrokeOpacity(GfxState *state) { | ||
523 | 509 | double opacity = stroke_opacity; | ||
524 | 510 | |||
525 | 511 | stroke_opacity = state->getStrokeOpacity(); | ||
526 | 512 | if (opacity != stroke_opacity) { | ||
527 | 513 | cairo_pattern_destroy(stroke_pattern); | ||
528 | 514 | stroke_pattern = cairo_pattern_create_rgba(colToDbl(stroke_color.r), | ||
529 | 515 | colToDbl(stroke_color.g), | ||
530 | 516 | colToDbl(stroke_color.b), | ||
531 | 517 | stroke_opacity); | ||
532 | 518 | |||
533 | 519 | LOG(printf ("stroke opacity: %f\n", stroke_opacity)); | ||
534 | 520 | } | ||
535 | 521 | } | ||
536 | 522 | |||
537 | 523 | void CairoOutputDev::updateFillColorStop(GfxState *state, double offset) { | ||
538 | 524 | state->getFillRGB(&fill_color); | ||
539 | 525 | |||
540 | 526 | cairo_pattern_add_color_stop_rgba(fill_pattern, offset, | ||
541 | 527 | colToDbl(fill_color.r), | ||
542 | 528 | colToDbl(fill_color.g), | ||
543 | 529 | colToDbl(fill_color.b), | ||
544 | 530 | fill_opacity); | ||
545 | 531 | LOG(printf ("fill color stop: %f (%d, %d, %d)\n", | ||
546 | 532 | offset, fill_color.r, fill_color.g, fill_color.b)); | ||
547 | 533 | } | ||
548 | 534 | |||
549 | 535 | void CairoOutputDev::updateBlendMode(GfxState *state) { | ||
550 | 536 | switch (state->getBlendMode()) { | ||
551 | 537 | default: | ||
552 | 538 | case gfxBlendNormal: | ||
553 | 539 | cairo_set_operator (cairo, CAIRO_OPERATOR_OVER); | ||
554 | 540 | break; | ||
555 | 541 | case gfxBlendMultiply: | ||
556 | 542 | cairo_set_operator (cairo, CAIRO_OPERATOR_MULTIPLY); | ||
557 | 543 | break; | ||
558 | 544 | case gfxBlendScreen: | ||
559 | 545 | cairo_set_operator (cairo, CAIRO_OPERATOR_SCREEN); | ||
560 | 546 | break; | ||
561 | 547 | case gfxBlendOverlay: | ||
562 | 548 | cairo_set_operator (cairo, CAIRO_OPERATOR_OVERLAY); | ||
563 | 549 | break; | ||
564 | 550 | case gfxBlendDarken: | ||
565 | 551 | cairo_set_operator (cairo, CAIRO_OPERATOR_DARKEN); | ||
566 | 552 | break; | ||
567 | 553 | case gfxBlendLighten: | ||
568 | 554 | cairo_set_operator (cairo, CAIRO_OPERATOR_LIGHTEN); | ||
569 | 555 | break; | ||
570 | 556 | case gfxBlendColorDodge: | ||
571 | 557 | cairo_set_operator (cairo, CAIRO_OPERATOR_COLOR_DODGE); | ||
572 | 558 | break; | ||
573 | 559 | case gfxBlendColorBurn: | ||
574 | 560 | cairo_set_operator (cairo, CAIRO_OPERATOR_COLOR_BURN); | ||
575 | 561 | break; | ||
576 | 562 | case gfxBlendHardLight: | ||
577 | 563 | cairo_set_operator (cairo, CAIRO_OPERATOR_HARD_LIGHT); | ||
578 | 564 | break; | ||
579 | 565 | case gfxBlendSoftLight: | ||
580 | 566 | cairo_set_operator (cairo, CAIRO_OPERATOR_SOFT_LIGHT); | ||
581 | 567 | break; | ||
582 | 568 | case gfxBlendDifference: | ||
583 | 569 | cairo_set_operator (cairo, CAIRO_OPERATOR_DIFFERENCE); | ||
584 | 570 | break; | ||
585 | 571 | case gfxBlendExclusion: | ||
586 | 572 | cairo_set_operator (cairo, CAIRO_OPERATOR_EXCLUSION); | ||
587 | 573 | break; | ||
588 | 574 | case gfxBlendHue: | ||
589 | 575 | cairo_set_operator (cairo, CAIRO_OPERATOR_HSL_HUE); | ||
590 | 576 | break; | ||
591 | 577 | case gfxBlendSaturation: | ||
592 | 578 | cairo_set_operator (cairo, CAIRO_OPERATOR_HSL_SATURATION); | ||
593 | 579 | break; | ||
594 | 580 | case gfxBlendColor: | ||
595 | 581 | cairo_set_operator (cairo, CAIRO_OPERATOR_HSL_COLOR); | ||
596 | 582 | break; | ||
597 | 583 | case gfxBlendLuminosity: | ||
598 | 584 | cairo_set_operator (cairo, CAIRO_OPERATOR_HSL_LUMINOSITY); | ||
599 | 585 | break; | ||
600 | 586 | } | ||
601 | 587 | LOG(printf ("blend mode: %d\n", (int)state->getBlendMode())); | ||
602 | 588 | } | ||
603 | 589 | |||
604 | 590 | void CairoOutputDev::updateFont(GfxState *state) { | ||
605 | 591 | cairo_font_face_t *font_face; | ||
606 | 592 | cairo_matrix_t matrix, invert_matrix; | ||
607 | 593 | |||
608 | 594 | LOG(printf ("updateFont() font=%s\n", state->getFont()->getName()->getCString())); | ||
609 | 595 | |||
610 | 596 | needFontUpdate = gFalse; | ||
611 | 597 | |||
612 | 598 | //FIXME: use cairo font engine? | ||
613 | 599 | if (text) | ||
614 | 600 | text->updateFont(state); | ||
615 | 601 | |||
616 | 602 | currentFont = fontEngine->getFont (state->getFont(), doc, printing); | ||
617 | 603 | |||
618 | 604 | if (!currentFont) | ||
619 | 605 | return; | ||
620 | 606 | |||
621 | 607 | font_face = currentFont->getFontFace(); | ||
622 | 608 | cairo_set_font_face (cairo, font_face); | ||
623 | 609 | |||
624 | 610 | use_show_text_glyphs = state->getFont()->hasToUnicodeCMap() && | ||
625 | 611 | cairo_surface_has_show_text_glyphs (cairo_get_target (cairo)); | ||
626 | 612 | |||
627 | 613 | double fontSize = state->getFontSize(); | ||
628 | 614 | double *m = state->getTextMat(); | ||
629 | 615 | /* NOTE: adjusting by a constant is hack. The correct solution | ||
630 | 616 | * is probably to use user-fonts and compute the scale on a per | ||
631 | 617 | * glyph basis instead of for the entire font */ | ||
632 | 618 | double w = currentFont->getSubstitutionCorrection(state->getFont()); | ||
633 | 619 | matrix.xx = m[0] * fontSize * state->getHorizScaling() * w; | ||
634 | 620 | matrix.yx = m[1] * fontSize * state->getHorizScaling() * w; | ||
635 | 621 | matrix.xy = -m[2] * fontSize; | ||
636 | 622 | matrix.yy = -m[3] * fontSize; | ||
637 | 623 | matrix.x0 = 0; | ||
638 | 624 | matrix.y0 = 0; | ||
639 | 625 | |||
640 | 626 | LOG(printf ("font matrix: %f %f %f %f\n", matrix.xx, matrix.yx, matrix.xy, matrix.yy)); | ||
641 | 627 | |||
642 | 628 | /* Make sure the font matrix is invertible before setting it. cairo | ||
643 | 629 | * will blow up if we give it a matrix that's not invertible, so we | ||
644 | 630 | * need to check before passing it to cairo_set_font_matrix. Ignoring it | ||
645 | 631 | * is likely to give better results than not rendering anything at | ||
646 | 632 | * all. See #18254. | ||
647 | 633 | */ | ||
648 | 634 | invert_matrix = matrix; | ||
649 | 635 | if (cairo_matrix_invert(&invert_matrix)) { | ||
650 | 636 | error(errSyntaxWarning, -1, "font matrix not invertible\n"); | ||
651 | 637 | return; | ||
652 | 638 | } | ||
653 | 639 | |||
654 | 640 | cairo_set_font_matrix (cairo, &matrix); | ||
655 | 641 | } | ||
656 | 642 | |||
657 | 643 | /* Tolerance in pixels for checking if strokes are horizontal or vertical | ||
658 | 644 | * lines in device space */ | ||
659 | 645 | #define STROKE_COORD_TOLERANCE 0.5 | ||
660 | 646 | |||
661 | 647 | /* Align stroke coordinate i if the point is the start or end of a | ||
662 | 648 | * horizontal or vertical line */ | ||
663 | 649 | void CairoOutputDev::alignStrokeCoords(GfxSubpath *subpath, int i, double *x, double *y) | ||
664 | 650 | { | ||
665 | 651 | double x1, y1, x2, y2; | ||
666 | 652 | GBool align = gFalse; | ||
667 | 653 | |||
668 | 654 | x1 = subpath->getX(i); | ||
669 | 655 | y1 = subpath->getY(i); | ||
670 | 656 | cairo_user_to_device (cairo, &x1, &y1); | ||
671 | 657 | |||
672 | 658 | // Does the current coord and prev coord form a horiz or vert line? | ||
673 | 659 | if (i > 0 && !subpath->getCurve(i - 1)) { | ||
674 | 660 | x2 = subpath->getX(i - 1); | ||
675 | 661 | y2 = subpath->getY(i - 1); | ||
676 | 662 | cairo_user_to_device (cairo, &x2, &y2); | ||
677 | 663 | if (fabs(x2 - x1) < STROKE_COORD_TOLERANCE || fabs(y2 - y1) < STROKE_COORD_TOLERANCE) | ||
678 | 664 | align = gTrue; | ||
679 | 665 | } | ||
680 | 666 | |||
681 | 667 | // Does the current coord and next coord form a horiz or vert line? | ||
682 | 668 | if (i < subpath->getNumPoints() - 1 && !subpath->getCurve(i + 1)) { | ||
683 | 669 | x2 = subpath->getX(i + 1); | ||
684 | 670 | y2 = subpath->getY(i + 1); | ||
685 | 671 | cairo_user_to_device (cairo, &x2, &y2); | ||
686 | 672 | if (fabs(x2 - x1) < STROKE_COORD_TOLERANCE || fabs(y2 - y1) < STROKE_COORD_TOLERANCE) | ||
687 | 673 | align = gTrue; | ||
688 | 674 | } | ||
689 | 675 | |||
690 | 676 | *x = subpath->getX(i); | ||
691 | 677 | *y = subpath->getY(i); | ||
692 | 678 | if (align) { | ||
693 | 679 | /* see http://www.cairographics.org/FAQ/#sharp_lines */ | ||
694 | 680 | cairo_user_to_device (cairo, x, y); | ||
695 | 681 | *x = floor(*x) + 0.5; | ||
696 | 682 | *y = floor(*y) + 0.5; | ||
697 | 683 | cairo_device_to_user (cairo, x, y); | ||
698 | 684 | } | ||
699 | 685 | } | ||
700 | 686 | |||
701 | 687 | #undef STROKE_COORD_TOLERANCE | ||
702 | 688 | |||
703 | 689 | void CairoOutputDev::doPath(cairo_t *cairo, GfxState *state, GfxPath *path) { | ||
704 | 690 | GfxSubpath *subpath; | ||
705 | 691 | int i, j; | ||
706 | 692 | double x, y; | ||
707 | 693 | cairo_new_path (cairo); | ||
708 | 694 | for (i = 0; i < path->getNumSubpaths(); ++i) { | ||
709 | 695 | subpath = path->getSubpath(i); | ||
710 | 696 | if (subpath->getNumPoints() > 0) { | ||
711 | 697 | if (align_stroke_coords) { | ||
712 | 698 | alignStrokeCoords(subpath, 0, &x, &y); | ||
713 | 699 | } else { | ||
714 | 700 | x = subpath->getX(0); | ||
715 | 701 | y = subpath->getY(0); | ||
716 | 702 | } | ||
717 | 703 | cairo_move_to (cairo, x, y); | ||
718 | 704 | j = 1; | ||
719 | 705 | while (j < subpath->getNumPoints()) { | ||
720 | 706 | if (subpath->getCurve(j)) { | ||
721 | 707 | if (align_stroke_coords) { | ||
722 | 708 | alignStrokeCoords(subpath, j + 2, &x, &y); | ||
723 | 709 | } else { | ||
724 | 710 | x = subpath->getX(j+2); | ||
725 | 711 | y = subpath->getY(j+2); | ||
726 | 712 | } | ||
727 | 713 | cairo_curve_to( cairo, | ||
728 | 714 | subpath->getX(j), subpath->getY(j), | ||
729 | 715 | subpath->getX(j+1), subpath->getY(j+1), | ||
730 | 716 | x, y); | ||
731 | 717 | |||
732 | 718 | j += 3; | ||
733 | 719 | } else { | ||
734 | 720 | if (align_stroke_coords) { | ||
735 | 721 | alignStrokeCoords(subpath, j, &x, &y); | ||
736 | 722 | } else { | ||
737 | 723 | x = subpath->getX(j); | ||
738 | 724 | y = subpath->getY(j); | ||
739 | 725 | } | ||
740 | 726 | cairo_line_to (cairo, x, y); | ||
741 | 727 | ++j; | ||
742 | 728 | } | ||
743 | 729 | } | ||
744 | 730 | if (subpath->isClosed()) { | ||
745 | 731 | LOG (printf ("close\n")); | ||
746 | 732 | cairo_close_path (cairo); | ||
747 | 733 | } | ||
748 | 734 | } | ||
749 | 735 | } | ||
750 | 736 | } | ||
751 | 737 | |||
752 | 738 | void CairoOutputDev::stroke(GfxState *state) { | ||
753 | 739 | if (inType3Char) { | ||
754 | 740 | GfxGray gray; | ||
755 | 741 | state->getFillGray(&gray); | ||
756 | 742 | if (colToDbl(gray) > 0.5) | ||
757 | 743 | return; | ||
758 | 744 | } | ||
759 | 745 | |||
760 | 746 | if (adjusted_stroke_width) | ||
761 | 747 | align_stroke_coords = gTrue; | ||
762 | 748 | doPath (cairo, state, state->getPath()); | ||
763 | 749 | align_stroke_coords = gFalse; | ||
764 | 750 | cairo_set_source (cairo, stroke_pattern); | ||
765 | 751 | LOG(printf ("stroke\n")); | ||
766 | 752 | cairo_stroke (cairo); | ||
767 | 753 | if (cairo_shape) { | ||
768 | 754 | doPath (cairo_shape, state, state->getPath()); | ||
769 | 755 | cairo_stroke (cairo_shape); | ||
770 | 756 | } | ||
771 | 757 | } | ||
772 | 758 | |||
773 | 759 | void CairoOutputDev::fill(GfxState *state) { | ||
774 | 760 | if (inType3Char) { | ||
775 | 761 | GfxGray gray; | ||
776 | 762 | state->getFillGray(&gray); | ||
777 | 763 | if (colToDbl(gray) > 0.5) | ||
778 | 764 | return; | ||
779 | 765 | } | ||
780 | 766 | |||
781 | 767 | doPath (cairo, state, state->getPath()); | ||
782 | 768 | cairo_set_fill_rule (cairo, CAIRO_FILL_RULE_WINDING); | ||
783 | 769 | cairo_set_source (cairo, fill_pattern); | ||
784 | 770 | LOG(printf ("fill\n")); | ||
785 | 771 | //XXX: how do we get the path | ||
786 | 772 | if (mask) { | ||
787 | 773 | cairo_save (cairo); | ||
788 | 774 | cairo_clip (cairo); | ||
789 | 775 | cairo_set_matrix (cairo, &mask_matrix); | ||
790 | 776 | cairo_mask (cairo, mask); | ||
791 | 777 | cairo_restore (cairo); | ||
792 | 778 | } else if (strokePathClip) { | ||
793 | 779 | fillToStrokePathClip(state); | ||
794 | 780 | } else { | ||
795 | 781 | cairo_fill (cairo); | ||
796 | 782 | } | ||
797 | 783 | if (cairo_shape) { | ||
798 | 784 | cairo_set_fill_rule (cairo_shape, CAIRO_FILL_RULE_WINDING); | ||
799 | 785 | doPath (cairo_shape, state, state->getPath()); | ||
800 | 786 | cairo_fill (cairo_shape); | ||
801 | 787 | } | ||
802 | 788 | } | ||
803 | 789 | |||
804 | 790 | void CairoOutputDev::eoFill(GfxState *state) { | ||
805 | 791 | doPath (cairo, state, state->getPath()); | ||
806 | 792 | cairo_set_fill_rule (cairo, CAIRO_FILL_RULE_EVEN_ODD); | ||
807 | 793 | cairo_set_source (cairo, fill_pattern); | ||
808 | 794 | LOG(printf ("fill-eo\n")); | ||
809 | 795 | cairo_fill (cairo); | ||
810 | 796 | |||
811 | 797 | if (cairo_shape) { | ||
812 | 798 | cairo_set_fill_rule (cairo_shape, CAIRO_FILL_RULE_EVEN_ODD); | ||
813 | 799 | doPath (cairo_shape, state, state->getPath()); | ||
814 | 800 | cairo_fill (cairo_shape); | ||
815 | 801 | } | ||
816 | 802 | |||
817 | 803 | } | ||
818 | 804 | |||
819 | 805 | GBool CairoOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx1, Catalog *cat, Object *str, | ||
820 | 806 | double *pmat, int paintType, int /*tilingType*/, Dict *resDict, | ||
821 | 807 | double *mat, double *bbox, | ||
822 | 808 | int x0, int y0, int x1, int y1, | ||
823 | 809 | double xStep, double yStep) | ||
824 | 810 | { | ||
825 | 811 | PDFRectangle box; | ||
826 | 812 | Gfx *gfx; | ||
827 | 813 | cairo_pattern_t *pattern; | ||
828 | 814 | cairo_surface_t *surface; | ||
829 | 815 | cairo_matrix_t matrix; | ||
830 | 816 | cairo_t *old_cairo; | ||
831 | 817 | double xMin, yMin, xMax, yMax; | ||
832 | 818 | double width, height; | ||
833 | 819 | int surface_width, surface_height; | ||
834 | 820 | StrokePathClip *strokePathTmp; | ||
835 | 821 | |||
836 | 822 | width = bbox[2] - bbox[0]; | ||
837 | 823 | height = bbox[3] - bbox[1]; | ||
838 | 824 | |||
839 | 825 | if (xStep != width || yStep != height) | ||
840 | 826 | return gFalse; | ||
841 | 827 | /* TODO: implement the other cases here too */ | ||
842 | 828 | |||
843 | 829 | surface_width = (int) ceil (width); | ||
844 | 830 | surface_height = (int) ceil (height); | ||
845 | 831 | |||
846 | 832 | surface = cairo_surface_create_similar (cairo_get_target (cairo), | ||
847 | 833 | CAIRO_CONTENT_COLOR_ALPHA, | ||
848 | 834 | surface_width, surface_height); | ||
849 | 835 | if (cairo_surface_status (surface)) | ||
850 | 836 | return gFalse; | ||
851 | 837 | |||
852 | 838 | old_cairo = cairo; | ||
853 | 839 | cairo = cairo_create (surface); | ||
854 | 840 | cairo_surface_destroy (surface); | ||
855 | 841 | |||
856 | 842 | box.x1 = bbox[0]; box.y1 = bbox[1]; | ||
857 | 843 | box.x2 = bbox[2]; box.y2 = bbox[3]; | ||
858 | 844 | strokePathTmp = strokePathClip; | ||
859 | 845 | strokePathClip = NULL; | ||
860 | 846 | gfx = new Gfx(doc, this, resDict, &box, NULL); | ||
861 | 847 | gfx->display(str); | ||
862 | 848 | delete gfx; | ||
863 | 849 | strokePathClip = strokePathTmp; | ||
864 | 850 | |||
865 | 851 | pattern = cairo_pattern_create_for_surface (cairo_get_target (cairo)); | ||
866 | 852 | cairo_destroy (cairo); | ||
867 | 853 | cairo = old_cairo; | ||
868 | 854 | if (cairo_pattern_status (pattern)) | ||
869 | 855 | return gFalse; | ||
870 | 856 | |||
871 | 857 | state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax); | ||
872 | 858 | cairo_rectangle (cairo, xMin, yMin, xMax - xMin, yMax - yMin); | ||
873 | 859 | |||
874 | 860 | cairo_matrix_init_scale (&matrix, surface_width / width, surface_height / height); | ||
875 | 861 | cairo_pattern_set_matrix (pattern, &matrix); | ||
876 | 862 | |||
877 | 863 | cairo_matrix_init (&matrix, mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]); | ||
878 | 864 | cairo_transform (cairo, &matrix); | ||
879 | 865 | cairo_set_source (cairo, pattern); | ||
880 | 866 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT); | ||
881 | 867 | if (strokePathClip) { | ||
882 | 868 | fillToStrokePathClip(state); | ||
883 | 869 | } else { | ||
884 | 870 | cairo_fill (cairo); | ||
885 | 871 | } | ||
886 | 872 | |||
887 | 873 | cairo_pattern_destroy (pattern); | ||
888 | 874 | |||
889 | 875 | return gTrue; | ||
890 | 876 | } | ||
891 | 877 | |||
892 | 878 | GBool CairoOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax) { | ||
893 | 879 | double x0, y0, x1, y1; | ||
894 | 880 | double dx, dy; | ||
895 | 881 | |||
896 | 882 | shading->getCoords(&x0, &y0, &x1, &y1); | ||
897 | 883 | dx = x1 - x0; | ||
898 | 884 | dy = y1 - y0; | ||
899 | 885 | |||
900 | 886 | cairo_pattern_destroy(fill_pattern); | ||
901 | 887 | fill_pattern = cairo_pattern_create_linear (x0 + tMin * dx, y0 + tMin * dy, | ||
902 | 888 | x0 + tMax * dx, y0 + tMax * dy); | ||
903 | 889 | if (!shading->getExtend0() && !shading->getExtend1()) | ||
904 | 890 | cairo_pattern_set_extend (fill_pattern, CAIRO_EXTEND_NONE); | ||
905 | 891 | else | ||
906 | 892 | cairo_pattern_set_extend (fill_pattern, CAIRO_EXTEND_PAD); | ||
907 | 893 | |||
908 | 894 | LOG (printf ("axial-sh\n")); | ||
909 | 895 | |||
910 | 896 | // TODO: use the actual stops in the shading in the case | ||
911 | 897 | // of linear interpolation (Type 2 Exponential functions with N=1) | ||
912 | 898 | return gFalse; | ||
913 | 899 | } | ||
914 | 900 | |||
915 | 901 | GBool CairoOutputDev::axialShadedSupportExtend(GfxState *state, GfxAxialShading *shading) | ||
916 | 902 | { | ||
917 | 903 | return (shading->getExtend0() == shading->getExtend1()); | ||
918 | 904 | } | ||
919 | 905 | |||
920 | 906 | GBool CairoOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading, double sMin, double sMax) { | ||
921 | 907 | double x0, y0, r0, x1, y1, r1; | ||
922 | 908 | double dx, dy, dr; | ||
923 | 909 | |||
924 | 910 | shading->getCoords(&x0, &y0, &r0, &x1, &y1, &r1); | ||
925 | 911 | dx = x1 - x0; | ||
926 | 912 | dy = y1 - y0; | ||
927 | 913 | dr = r1 - r0; | ||
928 | 914 | cairo_pattern_destroy(fill_pattern); | ||
929 | 915 | fill_pattern = cairo_pattern_create_radial (x0 + sMin * dx, | ||
930 | 916 | y0 + sMin * dy, | ||
931 | 917 | r0 + sMin * dr, | ||
932 | 918 | x0 + sMax * dx, | ||
933 | 919 | y0 + sMax * dy, | ||
934 | 920 | r0 + sMax * dr); | ||
935 | 921 | if (shading->getExtend0() && shading->getExtend1()) | ||
936 | 922 | cairo_pattern_set_extend (fill_pattern, CAIRO_EXTEND_PAD); | ||
937 | 923 | else | ||
938 | 924 | cairo_pattern_set_extend (fill_pattern, CAIRO_EXTEND_NONE); | ||
939 | 925 | |||
940 | 926 | LOG (printf ("radial-sh\n")); | ||
941 | 927 | |||
942 | 928 | return gFalse; | ||
943 | 929 | } | ||
944 | 930 | |||
945 | 931 | GBool CairoOutputDev::radialShadedSupportExtend(GfxState *state, GfxRadialShading *shading) | ||
946 | 932 | { | ||
947 | 933 | return (shading->getExtend0() == shading->getExtend1()); | ||
948 | 934 | } | ||
949 | 935 | |||
950 | 936 | #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) | ||
951 | 937 | GBool CairoOutputDev::gouraudTriangleShadedFill(GfxState *state, GfxGouraudTriangleShading *shading) | ||
952 | 938 | { | ||
953 | 939 | double x0, y0, x1, y1, x2, y2; | ||
954 | 940 | GfxColor color[3]; | ||
955 | 941 | int i, j; | ||
956 | 942 | GfxRGB rgb; | ||
957 | 943 | |||
958 | 944 | cairo_pattern_destroy(fill_pattern); | ||
959 | 945 | fill_pattern = cairo_pattern_create_mesh (); | ||
960 | 946 | |||
961 | 947 | for (i = 0; i < shading->getNTriangles(); i++) { | ||
962 | 948 | shading->getTriangle(i, | ||
963 | 949 | &x0, &y0, &color[0], | ||
964 | 950 | &x1, &y1, &color[1], | ||
965 | 951 | &x2, &y2, &color[2]); | ||
966 | 952 | |||
967 | 953 | cairo_mesh_pattern_begin_patch (fill_pattern); | ||
968 | 954 | |||
969 | 955 | cairo_mesh_pattern_move_to (fill_pattern, x0, y0); | ||
970 | 956 | cairo_mesh_pattern_line_to (fill_pattern, x1, y1); | ||
971 | 957 | cairo_mesh_pattern_line_to (fill_pattern, x2, y2); | ||
972 | 958 | |||
973 | 959 | for (j = 0; j < 3; j++) { | ||
974 | 960 | shading->getColorSpace()->getRGB(&color[j], &rgb); | ||
975 | 961 | cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, j, | ||
976 | 962 | colToDbl(rgb.r), | ||
977 | 963 | colToDbl(rgb.g), | ||
978 | 964 | colToDbl(rgb.b)); | ||
979 | 965 | } | ||
980 | 966 | |||
981 | 967 | cairo_mesh_pattern_end_patch (fill_pattern); | ||
982 | 968 | } | ||
983 | 969 | |||
984 | 970 | double xMin, yMin, xMax, yMax; | ||
985 | 971 | // get the clip region bbox | ||
986 | 972 | state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax); | ||
987 | 973 | state->moveTo(xMin, yMin); | ||
988 | 974 | state->lineTo(xMin, yMax); | ||
989 | 975 | state->lineTo(xMax, yMax); | ||
990 | 976 | state->lineTo(xMax, yMin); | ||
991 | 977 | state->closePath(); | ||
992 | 978 | fill(state); | ||
993 | 979 | state->clearPath(); | ||
994 | 980 | |||
995 | 981 | return gTrue; | ||
996 | 982 | } | ||
997 | 983 | |||
998 | 984 | GBool CairoOutputDev::patchMeshShadedFill(GfxState *state, GfxPatchMeshShading *shading) | ||
999 | 985 | { | ||
1000 | 986 | int i, j, k; | ||
1001 | 987 | |||
1002 | 988 | cairo_pattern_destroy(fill_pattern); | ||
1003 | 989 | fill_pattern = cairo_pattern_create_mesh (); | ||
1004 | 990 | |||
1005 | 991 | for (i = 0; i < shading->getNPatches(); i++) { | ||
1006 | 992 | GfxPatch *patch = shading->getPatch(i); | ||
1007 | 993 | GfxColor color; | ||
1008 | 994 | GfxRGB rgb; | ||
1009 | 995 | |||
1010 | 996 | cairo_mesh_pattern_begin_patch (fill_pattern); | ||
1011 | 997 | |||
1012 | 998 | cairo_mesh_pattern_move_to (fill_pattern, patch->x[0][0], patch->y[0][0]); | ||
1013 | 999 | cairo_mesh_pattern_curve_to (fill_pattern, | ||
1014 | 1000 | patch->x[0][1], patch->y[0][1], | ||
1015 | 1001 | patch->x[0][2], patch->y[0][2], | ||
1016 | 1002 | patch->x[0][3], patch->y[0][3]); | ||
1017 | 1003 | |||
1018 | 1004 | cairo_mesh_pattern_curve_to (fill_pattern, | ||
1019 | 1005 | patch->x[1][3], patch->y[1][3], | ||
1020 | 1006 | patch->x[2][3], patch->y[2][3], | ||
1021 | 1007 | patch->x[3][3], patch->y[3][3]); | ||
1022 | 1008 | |||
1023 | 1009 | cairo_mesh_pattern_curve_to (fill_pattern, | ||
1024 | 1010 | patch->x[3][2], patch->y[3][2], | ||
1025 | 1011 | patch->x[3][1], patch->y[3][1], | ||
1026 | 1012 | patch->x[3][0], patch->y[3][0]); | ||
1027 | 1013 | |||
1028 | 1014 | cairo_mesh_pattern_curve_to (fill_pattern, | ||
1029 | 1015 | patch->x[2][0], patch->y[2][0], | ||
1030 | 1016 | patch->x[1][0], patch->y[1][0], | ||
1031 | 1017 | patch->x[0][0], patch->y[0][0]); | ||
1032 | 1018 | |||
1033 | 1019 | cairo_mesh_pattern_set_control_point (fill_pattern, 0, patch->x[1][1], patch->y[1][1]); | ||
1034 | 1020 | cairo_mesh_pattern_set_control_point (fill_pattern, 1, patch->x[1][2], patch->y[1][2]); | ||
1035 | 1021 | cairo_mesh_pattern_set_control_point (fill_pattern, 2, patch->x[2][2], patch->y[2][2]); | ||
1036 | 1022 | cairo_mesh_pattern_set_control_point (fill_pattern, 3, patch->x[2][1], patch->y[2][1]); | ||
1037 | 1023 | |||
1038 | 1024 | for (j = 0; j < 4; j++) { | ||
1039 | 1025 | int u, v; | ||
1040 | 1026 | |||
1041 | 1027 | switch (j) { | ||
1042 | 1028 | case 0: | ||
1043 | 1029 | u = 0; v = 0; | ||
1044 | 1030 | break; | ||
1045 | 1031 | case 1: | ||
1046 | 1032 | u = 0; v = 1; | ||
1047 | 1033 | break; | ||
1048 | 1034 | case 2: | ||
1049 | 1035 | u = 1; v = 1; | ||
1050 | 1036 | break; | ||
1051 | 1037 | case 3: | ||
1052 | 1038 | u = 1; v = 0; | ||
1053 | 1039 | break; | ||
1054 | 1040 | } | ||
1055 | 1041 | |||
1056 | 1042 | if (shading->isParameterized()) { | ||
1057 | 1043 | shading->getParameterizedColor (patch->color[u][v].c[0], &color); | ||
1058 | 1044 | } else { | ||
1059 | 1045 | for (k = 0; k < shading->getColorSpace()->getNComps(); k++) { | ||
1060 | 1046 | // simply cast to the desired type; that's all what is needed. | ||
1061 | 1047 | color.c[k] = GfxColorComp (patch->color[u][v].c[k]); | ||
1062 | 1048 | } | ||
1063 | 1049 | } | ||
1064 | 1050 | |||
1065 | 1051 | shading->getColorSpace()->getRGB(&color, &rgb); | ||
1066 | 1052 | cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, j, | ||
1067 | 1053 | colToDbl(rgb.r), | ||
1068 | 1054 | colToDbl(rgb.g), | ||
1069 | 1055 | colToDbl(rgb.b)); | ||
1070 | 1056 | } | ||
1071 | 1057 | cairo_mesh_pattern_end_patch (fill_pattern); | ||
1072 | 1058 | } | ||
1073 | 1059 | |||
1074 | 1060 | double xMin, yMin, xMax, yMax; | ||
1075 | 1061 | // get the clip region bbox | ||
1076 | 1062 | state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax); | ||
1077 | 1063 | state->moveTo(xMin, yMin); | ||
1078 | 1064 | state->lineTo(xMin, yMax); | ||
1079 | 1065 | state->lineTo(xMax, yMax); | ||
1080 | 1066 | state->lineTo(xMax, yMin); | ||
1081 | 1067 | state->closePath(); | ||
1082 | 1068 | fill(state); | ||
1083 | 1069 | state->clearPath(); | ||
1084 | 1070 | |||
1085 | 1071 | return gTrue; | ||
1086 | 1072 | } | ||
1087 | 1073 | #endif /* CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) */ | ||
1088 | 1074 | |||
1089 | 1075 | void CairoOutputDev::clip(GfxState *state) { | ||
1090 | 1076 | doPath (cairo, state, state->getPath()); | ||
1091 | 1077 | cairo_set_fill_rule (cairo, CAIRO_FILL_RULE_WINDING); | ||
1092 | 1078 | cairo_clip (cairo); | ||
1093 | 1079 | LOG (printf ("clip\n")); | ||
1094 | 1080 | if (cairo_shape) { | ||
1095 | 1081 | doPath (cairo_shape, state, state->getPath()); | ||
1096 | 1082 | cairo_set_fill_rule (cairo_shape, CAIRO_FILL_RULE_WINDING); | ||
1097 | 1083 | cairo_clip (cairo_shape); | ||
1098 | 1084 | } | ||
1099 | 1085 | } | ||
1100 | 1086 | |||
1101 | 1087 | void CairoOutputDev::eoClip(GfxState *state) { | ||
1102 | 1088 | doPath (cairo, state, state->getPath()); | ||
1103 | 1089 | cairo_set_fill_rule (cairo, CAIRO_FILL_RULE_EVEN_ODD); | ||
1104 | 1090 | cairo_clip (cairo); | ||
1105 | 1091 | LOG (printf ("clip-eo\n")); | ||
1106 | 1092 | if (cairo_shape) { | ||
1107 | 1093 | doPath (cairo_shape, state, state->getPath()); | ||
1108 | 1094 | cairo_set_fill_rule (cairo_shape, CAIRO_FILL_RULE_EVEN_ODD); | ||
1109 | 1095 | cairo_clip (cairo_shape); | ||
1110 | 1096 | } | ||
1111 | 1097 | |||
1112 | 1098 | } | ||
1113 | 1099 | |||
1114 | 1100 | void CairoOutputDev::clipToStrokePath(GfxState *state) { | ||
1115 | 1101 | LOG(printf("clip-to-stroke-path\n")); | ||
1116 | 1102 | strokePathClip = (StrokePathClip*)gmalloc (sizeof(*strokePathClip)); | ||
1117 | 1103 | strokePathClip->path = state->getPath()->copy(); | ||
1118 | 1104 | cairo_get_matrix (cairo, &strokePathClip->ctm); | ||
1119 | 1105 | strokePathClip->line_width = cairo_get_line_width (cairo); | ||
1120 | 1106 | strokePathClip->dash_count = cairo_get_dash_count (cairo); | ||
1121 | 1107 | if (strokePathClip->dash_count) { | ||
1122 | 1108 | strokePathClip->dashes = (double*) gmallocn (sizeof(double), strokePathClip->dash_count); | ||
1123 | 1109 | cairo_get_dash (cairo, strokePathClip->dashes, &strokePathClip->dash_offset); | ||
1124 | 1110 | } else { | ||
1125 | 1111 | strokePathClip->dashes = NULL; | ||
1126 | 1112 | } | ||
1127 | 1113 | strokePathClip->cap = cairo_get_line_cap (cairo); | ||
1128 | 1114 | strokePathClip->join = cairo_get_line_join (cairo); | ||
1129 | 1115 | strokePathClip->miter = cairo_get_miter_limit (cairo); | ||
1130 | 1116 | } | ||
1131 | 1117 | |||
1132 | 1118 | void CairoOutputDev::fillToStrokePathClip(GfxState *state) { | ||
1133 | 1119 | cairo_save (cairo); | ||
1134 | 1120 | |||
1135 | 1121 | cairo_set_matrix (cairo, &strokePathClip->ctm); | ||
1136 | 1122 | cairo_set_line_width (cairo, strokePathClip->line_width); | ||
1137 | 1123 | strokePathClip->dash_count = cairo_get_dash_count (cairo); | ||
1138 | 1124 | cairo_set_dash (cairo, strokePathClip->dashes, strokePathClip->dash_count, strokePathClip->dash_offset); | ||
1139 | 1125 | cairo_set_line_cap (cairo, strokePathClip->cap); | ||
1140 | 1126 | cairo_set_line_join (cairo, strokePathClip->join); | ||
1141 | 1127 | cairo_set_miter_limit (cairo, strokePathClip->miter); | ||
1142 | 1128 | doPath (cairo, state, strokePathClip->path); | ||
1143 | 1129 | cairo_stroke (cairo); | ||
1144 | 1130 | |||
1145 | 1131 | cairo_restore (cairo); | ||
1146 | 1132 | |||
1147 | 1133 | delete strokePathClip->path; | ||
1148 | 1134 | if (strokePathClip->dashes) | ||
1149 | 1135 | gfree (strokePathClip->dashes); | ||
1150 | 1136 | gfree (strokePathClip); | ||
1151 | 1137 | strokePathClip = NULL; | ||
1152 | 1138 | } | ||
1153 | 1139 | |||
1154 | 1140 | void CairoOutputDev::beginString(GfxState *state, GooString *s) | ||
1155 | 1141 | { | ||
1156 | 1142 | int len = s->getLength(); | ||
1157 | 1143 | |||
1158 | 1144 | if (needFontUpdate) | ||
1159 | 1145 | updateFont(state); | ||
1160 | 1146 | |||
1161 | 1147 | if (!currentFont) | ||
1162 | 1148 | return; | ||
1163 | 1149 | |||
1164 | 1150 | glyphs = (cairo_glyph_t *) gmallocn (len, sizeof (cairo_glyph_t)); | ||
1165 | 1151 | glyphCount = 0; | ||
1166 | 1152 | if (use_show_text_glyphs) { | ||
1167 | 1153 | clusters = (cairo_text_cluster_t *) gmallocn (len, sizeof (cairo_text_cluster_t)); | ||
1168 | 1154 | clusterCount = 0; | ||
1169 | 1155 | utf8Max = len*2; // start with twice the number of glyphs. we will realloc if we need more. | ||
1170 | 1156 | utf8 = (char *) gmalloc (utf8Max); | ||
1171 | 1157 | utf8Count = 0; | ||
1172 | 1158 | } | ||
1173 | 1159 | } | ||
1174 | 1160 | |||
1175 | 1161 | void CairoOutputDev::drawChar(GfxState *state, double x, double y, | ||
1176 | 1162 | double dx, double dy, | ||
1177 | 1163 | double originX, double originY, | ||
1178 | 1164 | CharCode code, int nBytes, Unicode *u, int uLen) | ||
1179 | 1165 | { | ||
1180 | 1166 | if (currentFont) { | ||
1181 | 1167 | glyphs[glyphCount].index = currentFont->getGlyph (code, u, uLen); | ||
1182 | 1168 | glyphs[glyphCount].x = x - originX; | ||
1183 | 1169 | glyphs[glyphCount].y = y - originY; | ||
1184 | 1170 | glyphCount++; | ||
1185 | 1171 | if (use_show_text_glyphs) { | ||
1186 | 1172 | if (utf8Max - utf8Count < uLen*6) { | ||
1187 | 1173 | // utf8 encoded characters can be up to 6 bytes | ||
1188 | 1174 | if (utf8Max > uLen*6) | ||
1189 | 1175 | utf8Max *= 2; | ||
1190 | 1176 | else | ||
1191 | 1177 | utf8Max += 2*uLen*6; | ||
1192 | 1178 | utf8 = (char *) grealloc (utf8, utf8Max); | ||
1193 | 1179 | } | ||
1194 | 1180 | clusters[clusterCount].num_bytes = 0; | ||
1195 | 1181 | for (int i = 0; i < uLen; i++) { | ||
1196 | 1182 | int size = mapUTF8 (u[i], utf8 + utf8Count, utf8Max - utf8Count); | ||
1197 | 1183 | utf8Count += size; | ||
1198 | 1184 | clusters[clusterCount].num_bytes += size; | ||
1199 | 1185 | } | ||
1200 | 1186 | clusters[clusterCount].num_glyphs = 1; | ||
1201 | 1187 | clusterCount++; | ||
1202 | 1188 | } | ||
1203 | 1189 | } | ||
1204 | 1190 | |||
1205 | 1191 | if (!text) | ||
1206 | 1192 | return; | ||
1207 | 1193 | actualText->addChar (state, x, y, dx, dy, code, nBytes, u, uLen); | ||
1208 | 1194 | } | ||
1209 | 1195 | |||
1210 | 1196 | void CairoOutputDev::endString(GfxState *state) | ||
1211 | 1197 | { | ||
1212 | 1198 | int render; | ||
1213 | 1199 | |||
1214 | 1200 | if (!currentFont) | ||
1215 | 1201 | return; | ||
1216 | 1202 | |||
1217 | 1203 | // endString can be called without a corresponding beginString. If this | ||
1218 | 1204 | // happens glyphs will be null so don't draw anything, just return. | ||
1219 | 1205 | // XXX: OutputDevs should probably not have to deal with this... | ||
1220 | 1206 | if (!glyphs) | ||
1221 | 1207 | return; | ||
1222 | 1208 | |||
1223 | 1209 | // ignore empty strings and invisible text -- this is used by | ||
1224 | 1210 | // Acrobat Capture | ||
1225 | 1211 | render = state->getRender(); | ||
1226 | 1212 | if (render == 3 || glyphCount == 0) { | ||
1227 | 1213 | gfree(glyphs); | ||
1228 | 1214 | glyphs = NULL; | ||
1229 | 1215 | return; | ||
1230 | 1216 | } | ||
1231 | 1217 | |||
1232 | 1218 | if (!(render & 1)) { | ||
1233 | 1219 | LOG (printf ("fill string\n")); | ||
1234 | 1220 | cairo_set_source (cairo, fill_pattern); | ||
1235 | 1221 | if (use_show_text_glyphs) | ||
1236 | 1222 | cairo_show_text_glyphs (cairo, utf8, utf8Count, glyphs, glyphCount, clusters, clusterCount, (cairo_text_cluster_flags_t)0); | ||
1237 | 1223 | else | ||
1238 | 1224 | cairo_show_glyphs (cairo, glyphs, glyphCount); | ||
1239 | 1225 | if (cairo_shape) | ||
1240 | 1226 | cairo_show_glyphs (cairo_shape, glyphs, glyphCount); | ||
1241 | 1227 | } | ||
1242 | 1228 | |||
1243 | 1229 | // stroke | ||
1244 | 1230 | if ((render & 3) == 1 || (render & 3) == 2) { | ||
1245 | 1231 | LOG (printf ("stroke string\n")); | ||
1246 | 1232 | cairo_set_source (cairo, stroke_pattern); | ||
1247 | 1233 | cairo_glyph_path (cairo, glyphs, glyphCount); | ||
1248 | 1234 | cairo_stroke (cairo); | ||
1249 | 1235 | if (cairo_shape) { | ||
1250 | 1236 | cairo_glyph_path (cairo_shape, glyphs, glyphCount); | ||
1251 | 1237 | cairo_stroke (cairo_shape); | ||
1252 | 1238 | } | ||
1253 | 1239 | } | ||
1254 | 1240 | |||
1255 | 1241 | // clip | ||
1256 | 1242 | if ((render & 4)) { | ||
1257 | 1243 | LOG (printf ("clip string\n")); | ||
1258 | 1244 | // append the glyph path to textClipPath. | ||
1259 | 1245 | |||
1260 | 1246 | // set textClipPath as the currentPath | ||
1261 | 1247 | if (textClipPath) { | ||
1262 | 1248 | cairo_append_path (cairo, textClipPath); | ||
1263 | 1249 | if (cairo_shape) { | ||
1264 | 1250 | cairo_append_path (cairo_shape, textClipPath); | ||
1265 | 1251 | } | ||
1266 | 1252 | cairo_path_destroy (textClipPath); | ||
1267 | 1253 | } | ||
1268 | 1254 | |||
1269 | 1255 | // append the glyph path | ||
1270 | 1256 | cairo_glyph_path (cairo, glyphs, glyphCount); | ||
1271 | 1257 | |||
1272 | 1258 | // move the path back into textClipPath | ||
1273 | 1259 | // and clear the current path | ||
1274 | 1260 | textClipPath = cairo_copy_path (cairo); | ||
1275 | 1261 | cairo_new_path (cairo); | ||
1276 | 1262 | if (cairo_shape) { | ||
1277 | 1263 | cairo_new_path (cairo_shape); | ||
1278 | 1264 | } | ||
1279 | 1265 | } | ||
1280 | 1266 | |||
1281 | 1267 | gfree (glyphs); | ||
1282 | 1268 | glyphs = NULL; | ||
1283 | 1269 | if (use_show_text_glyphs) { | ||
1284 | 1270 | gfree (clusters); | ||
1285 | 1271 | clusters = NULL; | ||
1286 | 1272 | gfree (utf8); | ||
1287 | 1273 | utf8 = NULL; | ||
1288 | 1274 | } | ||
1289 | 1275 | } | ||
1290 | 1276 | |||
1291 | 1277 | |||
1292 | 1278 | GBool CairoOutputDev::beginType3Char(GfxState *state, double x, double y, | ||
1293 | 1279 | double dx, double dy, | ||
1294 | 1280 | CharCode code, Unicode *u, int uLen) { | ||
1295 | 1281 | |||
1296 | 1282 | cairo_save (cairo); | ||
1297 | 1283 | double *ctm; | ||
1298 | 1284 | cairo_matrix_t matrix; | ||
1299 | 1285 | |||
1300 | 1286 | ctm = state->getCTM(); | ||
1301 | 1287 | matrix.xx = ctm[0]; | ||
1302 | 1288 | matrix.yx = ctm[1]; | ||
1303 | 1289 | matrix.xy = ctm[2]; | ||
1304 | 1290 | matrix.yy = ctm[3]; | ||
1305 | 1291 | matrix.x0 = ctm[4]; | ||
1306 | 1292 | matrix.y0 = ctm[5]; | ||
1307 | 1293 | /* Restore the original matrix and then transform to matrix needed for the | ||
1308 | 1294 | * type3 font. This is ugly but seems to work. Perhaps there is a better way to do it?*/ | ||
1309 | 1295 | cairo_set_matrix(cairo, &orig_matrix); | ||
1310 | 1296 | cairo_transform(cairo, &matrix); | ||
1311 | 1297 | if (cairo_shape) { | ||
1312 | 1298 | cairo_save (cairo_shape); | ||
1313 | 1299 | cairo_set_matrix(cairo_shape, &orig_matrix); | ||
1314 | 1300 | cairo_transform(cairo_shape, &matrix); | ||
1315 | 1301 | } | ||
1316 | 1302 | cairo_pattern_destroy(stroke_pattern); | ||
1317 | 1303 | cairo_pattern_reference(fill_pattern); | ||
1318 | 1304 | stroke_pattern = fill_pattern; | ||
1319 | 1305 | return gFalse; | ||
1320 | 1306 | } | ||
1321 | 1307 | |||
1322 | 1308 | void CairoOutputDev::endType3Char(GfxState *state) { | ||
1323 | 1309 | cairo_restore (cairo); | ||
1324 | 1310 | if (cairo_shape) { | ||
1325 | 1311 | cairo_restore (cairo_shape); | ||
1326 | 1312 | } | ||
1327 | 1313 | } | ||
1328 | 1314 | |||
1329 | 1315 | void CairoOutputDev::type3D0(GfxState *state, double wx, double wy) { | ||
1330 | 1316 | t3_glyph_wx = wx; | ||
1331 | 1317 | t3_glyph_wy = wy; | ||
1332 | 1318 | } | ||
1333 | 1319 | |||
1334 | 1320 | void CairoOutputDev::type3D1(GfxState *state, double wx, double wy, | ||
1335 | 1321 | double llx, double lly, double urx, double ury) { | ||
1336 | 1322 | t3_glyph_wx = wx; | ||
1337 | 1323 | t3_glyph_wy = wy; | ||
1338 | 1324 | t3_glyph_bbox[0] = llx; | ||
1339 | 1325 | t3_glyph_bbox[1] = lly; | ||
1340 | 1326 | t3_glyph_bbox[2] = urx; | ||
1341 | 1327 | t3_glyph_bbox[3] = ury; | ||
1342 | 1328 | t3_glyph_has_bbox = gTrue; | ||
1343 | 1329 | } | ||
1344 | 1330 | |||
1345 | 1331 | void CairoOutputDev::beginTextObject(GfxState *state) { | ||
1346 | 1332 | } | ||
1347 | 1333 | |||
1348 | 1334 | void CairoOutputDev::endTextObject(GfxState *state) { | ||
1349 | 1335 | if (textClipPath) { | ||
1350 | 1336 | // clip the accumulated text path | ||
1351 | 1337 | cairo_append_path (cairo, textClipPath); | ||
1352 | 1338 | cairo_clip (cairo); | ||
1353 | 1339 | if (cairo_shape) { | ||
1354 | 1340 | cairo_append_path (cairo_shape, textClipPath); | ||
1355 | 1341 | cairo_clip (cairo_shape); | ||
1356 | 1342 | } | ||
1357 | 1343 | cairo_path_destroy (textClipPath); | ||
1358 | 1344 | textClipPath = NULL; | ||
1359 | 1345 | } | ||
1360 | 1346 | } | ||
1361 | 1347 | |||
1362 | 1348 | void CairoOutputDev::beginActualText(GfxState *state, GooString *text) | ||
1363 | 1349 | { | ||
1364 | 1350 | if (this->text) | ||
1365 | 1351 | actualText->begin(state, text); | ||
1366 | 1352 | } | ||
1367 | 1353 | |||
1368 | 1354 | void CairoOutputDev::endActualText(GfxState *state) | ||
1369 | 1355 | { | ||
1370 | 1356 | if (text) | ||
1371 | 1357 | actualText->end(state); | ||
1372 | 1358 | } | ||
1373 | 1359 | |||
1374 | 1360 | static inline int splashRound(SplashCoord x) { | ||
1375 | 1361 | return (int)floor(x + 0.5); | ||
1376 | 1362 | } | ||
1377 | 1363 | |||
1378 | 1364 | static inline int splashCeil(SplashCoord x) { | ||
1379 | 1365 | return (int)ceil(x); | ||
1380 | 1366 | } | ||
1381 | 1367 | |||
1382 | 1368 | static inline int splashFloor(SplashCoord x) { | ||
1383 | 1369 | return (int)floor(x); | ||
1384 | 1370 | } | ||
1385 | 1371 | |||
1386 | 1372 | static | ||
1387 | 1373 | cairo_surface_t *cairo_surface_create_similar_clip (cairo_t *cairo, cairo_content_t content) | ||
1388 | 1374 | { | ||
1389 | 1375 | double x1, y1, x2, y2; | ||
1390 | 1376 | int width, height; | ||
1391 | 1377 | cairo_clip_extents (cairo, &x1, &y1, &x2, &y2); | ||
1392 | 1378 | cairo_matrix_t matrix; | ||
1393 | 1379 | cairo_get_matrix (cairo, &matrix); | ||
1394 | 1380 | //cairo_matrix_transform_point(&matrix, &x1, &y1); | ||
1395 | 1381 | //cairo_matrix_transform_point(&matrix, &x2, &y2);*/ | ||
1396 | 1382 | cairo_user_to_device(cairo, &x1, &y1); | ||
1397 | 1383 | cairo_user_to_device(cairo, &x2, &y2); | ||
1398 | 1384 | width = splashCeil(x2) - splashFloor(x1); | ||
1399 | 1385 | //XXX: negative matrix | ||
1400 | 1386 | ////height = splashCeil(y2) - splashFloor(y1); | ||
1401 | 1387 | height = splashFloor(y1) - splashCeil(y2); | ||
1402 | 1388 | cairo_surface_t *target = cairo_get_target (cairo); | ||
1403 | 1389 | cairo_surface_t *result; | ||
1404 | 1390 | |||
1405 | 1391 | result = cairo_surface_create_similar (target, content, width, height); | ||
1406 | 1392 | double x_offset, y_offset; | ||
1407 | 1393 | cairo_surface_get_device_offset(target, &x_offset, &y_offset); | ||
1408 | 1394 | cairo_surface_set_device_offset(result, x_offset, y_offset); | ||
1409 | 1395 | |||
1410 | 1396 | |||
1411 | 1397 | return result; | ||
1412 | 1398 | } | ||
1413 | 1399 | |||
1414 | 1400 | |||
1415 | 1401 | |||
1416 | 1402 | void CairoOutputDev::beginTransparencyGroup(GfxState * /*state*/, double * /*bbox*/, | ||
1417 | 1403 | GfxColorSpace * blendingColorSpace, | ||
1418 | 1404 | GBool /*isolated*/, GBool knockout, | ||
1419 | 1405 | GBool forSoftMask) { | ||
1420 | 1406 | /* push color space */ | ||
1421 | 1407 | ColorSpaceStack* css = new ColorSpaceStack; | ||
1422 | 1408 | css->cs = blendingColorSpace; | ||
1423 | 1409 | css->knockout = knockout; | ||
1424 | 1410 | cairo_get_matrix(cairo, &css->group_matrix); | ||
1425 | 1411 | css->next = groupColorSpaceStack; | ||
1426 | 1412 | groupColorSpaceStack = css; | ||
1427 | 1413 | |||
1428 | 1414 | LOG(printf ("begin transparency group. knockout: %s\n", knockout ? "yes":"no")); | ||
1429 | 1415 | |||
1430 | 1416 | if (knockout) { | ||
1431 | 1417 | knockoutCount++; | ||
1432 | 1418 | if (!cairo_shape) { | ||
1433 | 1419 | /* create a surface for tracking the shape */ | ||
1434 | 1420 | cairo_surface_t *cairo_shape_surface = cairo_surface_create_similar_clip (cairo, CAIRO_CONTENT_ALPHA); | ||
1435 | 1421 | cairo_shape = cairo_create (cairo_shape_surface); | ||
1436 | 1422 | cairo_surface_destroy (cairo_shape_surface); | ||
1437 | 1423 | |||
1438 | 1424 | /* the color doesn't matter as long as it is opaque */ | ||
1439 | 1425 | cairo_set_source_rgb (cairo_shape, 0, 0, 0); | ||
1440 | 1426 | cairo_matrix_t matrix; | ||
1441 | 1427 | cairo_get_matrix (cairo, &matrix); | ||
1442 | 1428 | //printMatrix(&matrix); | ||
1443 | 1429 | cairo_set_matrix (cairo_shape, &matrix); | ||
1444 | 1430 | } else { | ||
1445 | 1431 | cairo_reference (cairo_shape); | ||
1446 | 1432 | } | ||
1447 | 1433 | } | ||
1448 | 1434 | if (groupColorSpaceStack->next && groupColorSpaceStack->next->knockout) { | ||
1449 | 1435 | /* we need to track the shape */ | ||
1450 | 1436 | cairo_push_group (cairo_shape); | ||
1451 | 1437 | } | ||
1452 | 1438 | if (0 && forSoftMask) | ||
1453 | 1439 | cairo_push_group_with_content (cairo, CAIRO_CONTENT_ALPHA); | ||
1454 | 1440 | else | ||
1455 | 1441 | cairo_push_group (cairo); | ||
1456 | 1442 | |||
1457 | 1443 | /* push_group has an implicit cairo_save() */ | ||
1458 | 1444 | if (knockout) { | ||
1459 | 1445 | /*XXX: let's hope this matches the semantics needed */ | ||
1460 | 1446 | cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); | ||
1461 | 1447 | } else { | ||
1462 | 1448 | cairo_set_operator(cairo, CAIRO_OPERATOR_OVER); | ||
1463 | 1449 | } | ||
1464 | 1450 | } | ||
1465 | 1451 | |||
1466 | 1452 | void CairoOutputDev::endTransparencyGroup(GfxState * /*state*/) { | ||
1467 | 1453 | if (group) | ||
1468 | 1454 | cairo_pattern_destroy(group); | ||
1469 | 1455 | group = cairo_pop_group (cairo); | ||
1470 | 1456 | |||
1471 | 1457 | LOG(printf ("end transparency group\n")); | ||
1472 | 1458 | |||
1473 | 1459 | if (groupColorSpaceStack->next && groupColorSpaceStack->next->knockout) { | ||
1474 | 1460 | if (shape) | ||
1475 | 1461 | cairo_pattern_destroy(shape); | ||
1476 | 1462 | shape = cairo_pop_group (cairo_shape); | ||
1477 | 1463 | } | ||
1478 | 1464 | } | ||
1479 | 1465 | |||
1480 | 1466 | void CairoOutputDev::paintTransparencyGroup(GfxState * /*state*/, double * /*bbox*/) { | ||
1481 | 1467 | LOG(printf ("paint transparency group\n")); | ||
1482 | 1468 | |||
1483 | 1469 | cairo_save (cairo); | ||
1484 | 1470 | cairo_set_matrix (cairo, &groupColorSpaceStack->group_matrix); | ||
1485 | 1471 | cairo_set_source (cairo, group); | ||
1486 | 1472 | |||
1487 | 1473 | if (!mask) { | ||
1488 | 1474 | //XXX: deal with mask && shape case | ||
1489 | 1475 | if (shape) { | ||
1490 | 1476 | cairo_save (cairo); | ||
1491 | 1477 | |||
1492 | 1478 | /* OPERATOR_SOURCE w/ a mask is defined as (src IN mask) ADD (dest OUT mask) | ||
1493 | 1479 | * however our source has already been clipped to mask so we only need to | ||
1494 | 1480 | * do ADD and OUT */ | ||
1495 | 1481 | |||
1496 | 1482 | /* clear the shape mask */ | ||
1497 | 1483 | cairo_set_source (cairo, shape); | ||
1498 | 1484 | cairo_set_operator (cairo, CAIRO_OPERATOR_DEST_OUT); | ||
1499 | 1485 | cairo_paint (cairo); | ||
1500 | 1486 | |||
1501 | 1487 | cairo_set_operator (cairo, CAIRO_OPERATOR_ADD); | ||
1502 | 1488 | cairo_set_source (cairo, group); | ||
1503 | 1489 | cairo_paint (cairo); | ||
1504 | 1490 | |||
1505 | 1491 | cairo_restore (cairo); | ||
1506 | 1492 | |||
1507 | 1493 | cairo_pattern_destroy (shape); | ||
1508 | 1494 | shape = NULL; | ||
1509 | 1495 | } else { | ||
1510 | 1496 | cairo_paint_with_alpha (cairo, fill_opacity); | ||
1511 | 1497 | } | ||
1512 | 1498 | cairo_status_t status = cairo_status(cairo); | ||
1513 | 1499 | if (status) | ||
1514 | 1500 | printf("BAD status: %s\n", cairo_status_to_string(status)); | ||
1515 | 1501 | } else { | ||
1516 | 1502 | if (fill_opacity < 1.0) { | ||
1517 | 1503 | cairo_push_group(cairo); | ||
1518 | 1504 | } | ||
1519 | 1505 | cairo_save(cairo); | ||
1520 | 1506 | cairo_set_matrix(cairo, &mask_matrix); | ||
1521 | 1507 | cairo_mask(cairo, mask); | ||
1522 | 1508 | cairo_restore(cairo); | ||
1523 | 1509 | if (fill_opacity < 1.0) { | ||
1524 | 1510 | cairo_pop_group_to_source(cairo); | ||
1525 | 1511 | cairo_paint_with_alpha (cairo, fill_opacity); | ||
1526 | 1512 | } | ||
1527 | 1513 | cairo_pattern_destroy(mask); | ||
1528 | 1514 | mask = NULL; | ||
1529 | 1515 | } | ||
1530 | 1516 | |||
1531 | 1517 | popTransparencyGroup(); | ||
1532 | 1518 | cairo_restore(cairo); | ||
1533 | 1519 | } | ||
1534 | 1520 | |||
1535 | 1521 | static int luminocity(uint32_t x) | ||
1536 | 1522 | { | ||
1537 | 1523 | int r = (x >> 16) & 0xff; | ||
1538 | 1524 | int g = (x >> 8) & 0xff; | ||
1539 | 1525 | int b = (x >> 0) & 0xff; | ||
1540 | 1526 | // an arbitrary integer approximation of .3*r + .59*g + .11*b | ||
1541 | 1527 | int y = (r*19661+g*38666+b*7209 + 32829)>>16; | ||
1542 | 1528 | return y; | ||
1543 | 1529 | } | ||
1544 | 1530 | |||
1545 | 1531 | |||
1546 | 1532 | /* XXX: do we need to deal with shape here? */ | ||
1547 | 1533 | void CairoOutputDev::setSoftMask(GfxState * state, double * bbox, GBool alpha, | ||
1548 | 1534 | Function * transferFunc, GfxColor * backdropColor) { | ||
1549 | 1535 | cairo_pattern_destroy(mask); | ||
1550 | 1536 | |||
1551 | 1537 | LOG(printf ("set softMask\n")); | ||
1552 | 1538 | |||
1553 | 1539 | if (alpha == false) { | ||
1554 | 1540 | /* We need to mask according to the luminocity of the group. | ||
1555 | 1541 | * So we paint the group to an image surface convert it to a luminocity map | ||
1556 | 1542 | * and then use that as the mask. */ | ||
1557 | 1543 | |||
1558 | 1544 | /* Get clip extents in device space */ | ||
1559 | 1545 | double x1, y1, x2, y2, x_min, y_min, x_max, y_max; | ||
1560 | 1546 | cairo_clip_extents(cairo, &x1, &y1, &x2, &y2); | ||
1561 | 1547 | cairo_user_to_device(cairo, &x1, &y1); | ||
1562 | 1548 | cairo_user_to_device(cairo, &x2, &y2); | ||
1563 | 1549 | x_min = MIN(x1, x2); | ||
1564 | 1550 | y_min = MIN(y1, y2); | ||
1565 | 1551 | x_max = MAX(x1, x2); | ||
1566 | 1552 | y_max = MAX(y1, y2); | ||
1567 | 1553 | cairo_clip_extents(cairo, &x1, &y1, &x2, &y2); | ||
1568 | 1554 | cairo_user_to_device(cairo, &x1, &y2); | ||
1569 | 1555 | cairo_user_to_device(cairo, &x2, &y1); | ||
1570 | 1556 | x_min = MIN(x_min,MIN(x1, x2)); | ||
1571 | 1557 | y_min = MIN(y_min,MIN(y1, y2)); | ||
1572 | 1558 | x_max = MAX(x_max,MAX(x1, x2)); | ||
1573 | 1559 | y_max = MAX(y_max,MAX(y1, y2)); | ||
1574 | 1560 | |||
1575 | 1561 | int width = (int)(ceil(x_max) - floor(x_min)); | ||
1576 | 1562 | int height = (int)(ceil(y_max) - floor(y_min)); | ||
1577 | 1563 | |||
1578 | 1564 | /* Get group device offset */ | ||
1579 | 1565 | double x_offset, y_offset; | ||
1580 | 1566 | if (cairo_get_group_target(cairo) == cairo_get_target(cairo)) { | ||
1581 | 1567 | cairo_surface_get_device_offset(cairo_get_group_target(cairo), &x_offset, &y_offset); | ||
1582 | 1568 | } else { | ||
1583 | 1569 | cairo_surface_t *pats; | ||
1584 | 1570 | cairo_pattern_get_surface(group, &pats); | ||
1585 | 1571 | cairo_surface_get_device_offset(pats, &x_offset, &y_offset); | ||
1586 | 1572 | } | ||
1587 | 1573 | |||
1588 | 1574 | /* Adjust extents by group offset */ | ||
1589 | 1575 | x_min += x_offset; | ||
1590 | 1576 | y_min += y_offset; | ||
1591 | 1577 | |||
1592 | 1578 | cairo_surface_t *source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); | ||
1593 | 1579 | cairo_t *maskCtx = cairo_create(source); | ||
1594 | 1580 | |||
1595 | 1581 | //XXX: hopefully this uses the correct color space */ | ||
1596 | 1582 | GfxRGB backdropColorRGB; | ||
1597 | 1583 | groupColorSpaceStack->cs->getRGB(backdropColor, &backdropColorRGB); | ||
1598 | 1584 | /* paint the backdrop */ | ||
1599 | 1585 | cairo_set_source_rgb(maskCtx, | ||
1600 | 1586 | colToDbl(backdropColorRGB.r), | ||
1601 | 1587 | colToDbl(backdropColorRGB.g), | ||
1602 | 1588 | colToDbl(backdropColorRGB.b)); | ||
1603 | 1589 | cairo_paint(maskCtx); | ||
1604 | 1590 | |||
1605 | 1591 | /* Copy source ctm to mask ctm and translate origin so that the | ||
1606 | 1592 | * mask appears it the same location on the source surface. */ | ||
1607 | 1593 | cairo_matrix_t mat, tmat; | ||
1608 | 1594 | cairo_matrix_init_translate(&tmat, -x_min, -y_min); | ||
1609 | 1595 | cairo_get_matrix(cairo, &mat); | ||
1610 | 1596 | cairo_matrix_multiply(&mat, &mat, &tmat); | ||
1611 | 1597 | cairo_set_matrix(maskCtx, &mat); | ||
1612 | 1598 | |||
1613 | 1599 | /* make the device offset of the new mask match that of the group */ | ||
1614 | 1600 | cairo_surface_set_device_offset(source, x_offset, y_offset); | ||
1615 | 1601 | |||
1616 | 1602 | /* paint the group */ | ||
1617 | 1603 | cairo_set_source(maskCtx, group); | ||
1618 | 1604 | cairo_paint(maskCtx); | ||
1619 | 1605 | |||
1620 | 1606 | /* XXX status = cairo_status(maskCtx); */ | ||
1621 | 1607 | cairo_destroy(maskCtx); | ||
1622 | 1608 | |||
1623 | 1609 | /* convert to a luminocity map */ | ||
1624 | 1610 | uint32_t *source_data = (uint32_t*)cairo_image_surface_get_data(source); | ||
1625 | 1611 | /* get stride in units of 32 bits */ | ||
1626 | 1612 | int stride = cairo_image_surface_get_stride(source)/4; | ||
1627 | 1613 | for (int y=0; y<height; y++) { | ||
1628 | 1614 | for (int x=0; x<width; x++) { | ||
1629 | 1615 | int lum; | ||
1630 | 1616 | lum = luminocity(source_data[y*stride + x]); | ||
1631 | 1617 | if (transferFunc) { | ||
1632 | 1618 | double lum_in, lum_out; | ||
1633 | 1619 | lum_in = lum/256.0; | ||
1634 | 1620 | transferFunc->transform(&lum_in, &lum_out); | ||
1635 | 1621 | lum = (int)(lum_out * 255.0 + 0.5); | ||
1636 | 1622 | } | ||
1637 | 1623 | source_data[y*stride + x] = lum << 24; | ||
1638 | 1624 | } | ||
1639 | 1625 | } | ||
1640 | 1626 | cairo_surface_mark_dirty (source); | ||
1641 | 1627 | |||
1642 | 1628 | /* setup the new mask pattern */ | ||
1643 | 1629 | mask = cairo_pattern_create_for_surface(source); | ||
1644 | 1630 | cairo_get_matrix(cairo, &mask_matrix); | ||
1645 | 1631 | |||
1646 | 1632 | if (cairo_get_group_target(cairo) == cairo_get_target(cairo)) { | ||
1647 | 1633 | cairo_pattern_set_matrix(mask, &mat); | ||
1648 | 1634 | } else { | ||
1649 | 1635 | cairo_matrix_t patMatrix; | ||
1650 | 1636 | cairo_pattern_get_matrix(group, &patMatrix); | ||
1651 | 1637 | /* Apply x_min, y_min offset to it appears in the same location as source. */ | ||
1652 | 1638 | cairo_matrix_multiply(&patMatrix, &patMatrix, &tmat); | ||
1653 | 1639 | cairo_pattern_set_matrix(mask, &patMatrix); | ||
1654 | 1640 | } | ||
1655 | 1641 | |||
1656 | 1642 | cairo_surface_destroy(source); | ||
1657 | 1643 | } else { | ||
1658 | 1644 | mask = cairo_pattern_reference(group); | ||
1659 | 1645 | cairo_get_matrix(cairo, &mask_matrix); | ||
1660 | 1646 | } | ||
1661 | 1647 | |||
1662 | 1648 | popTransparencyGroup(); | ||
1663 | 1649 | } | ||
1664 | 1650 | |||
1665 | 1651 | void CairoOutputDev::popTransparencyGroup() { | ||
1666 | 1652 | /* pop color space */ | ||
1667 | 1653 | ColorSpaceStack *css = groupColorSpaceStack; | ||
1668 | 1654 | if (css->knockout) { | ||
1669 | 1655 | knockoutCount--; | ||
1670 | 1656 | if (!knockoutCount) { | ||
1671 | 1657 | /* we don't need to track the shape anymore because | ||
1672 | 1658 | * we are not above any knockout groups */ | ||
1673 | 1659 | cairo_destroy(cairo_shape); | ||
1674 | 1660 | cairo_shape = NULL; | ||
1675 | 1661 | } | ||
1676 | 1662 | } | ||
1677 | 1663 | groupColorSpaceStack = css->next; | ||
1678 | 1664 | delete css; | ||
1679 | 1665 | } | ||
1680 | 1666 | |||
1681 | 1667 | |||
1682 | 1668 | void CairoOutputDev::clearSoftMask(GfxState * /*state*/) { | ||
1683 | 1669 | if (mask) | ||
1684 | 1670 | cairo_pattern_destroy(mask); | ||
1685 | 1671 | mask = NULL; | ||
1686 | 1672 | } | ||
1687 | 1673 | |||
1688 | 1674 | /* Taken from cairo/doc/tutorial/src/singular.c */ | ||
1689 | 1675 | static void | ||
1690 | 1676 | get_singular_values (const cairo_matrix_t *matrix, | ||
1691 | 1677 | double *major, | ||
1692 | 1678 | double *minor) | ||
1693 | 1679 | { | ||
1694 | 1680 | double xx = matrix->xx, xy = matrix->xy; | ||
1695 | 1681 | double yx = matrix->yx, yy = matrix->yy; | ||
1696 | 1682 | |||
1697 | 1683 | double a = xx*xx+yx*yx; | ||
1698 | 1684 | double b = xy*xy+yy*yy; | ||
1699 | 1685 | double k = xx*xy+yx*yy; | ||
1700 | 1686 | |||
1701 | 1687 | double f = (a+b) * .5; | ||
1702 | 1688 | double g = (a-b) * .5; | ||
1703 | 1689 | double delta = sqrt (g*g + k*k); | ||
1704 | 1690 | |||
1705 | 1691 | if (major) | ||
1706 | 1692 | *major = sqrt (f + delta); | ||
1707 | 1693 | if (minor) | ||
1708 | 1694 | *minor = sqrt (f - delta); | ||
1709 | 1695 | } | ||
1710 | 1696 | |||
1711 | 1697 | void CairoOutputDev::getScaledSize(int orig_width, | ||
1712 | 1698 | int orig_height, | ||
1713 | 1699 | int *scaledWidth, | ||
1714 | 1700 | int *scaledHeight) { | ||
1715 | 1701 | cairo_matrix_t matrix; | ||
1716 | 1702 | cairo_get_matrix(cairo, &matrix); | ||
1717 | 1703 | |||
1718 | 1704 | double xScale; | ||
1719 | 1705 | double yScale; | ||
1720 | 1706 | if (orig_width > orig_height) | ||
1721 | 1707 | get_singular_values (&matrix, &xScale, &yScale); | ||
1722 | 1708 | else | ||
1723 | 1709 | get_singular_values (&matrix, &yScale, &xScale); | ||
1724 | 1710 | |||
1725 | 1711 | int tx, tx2, ty, ty2; /* the integer co-oridinates of the resulting image */ | ||
1726 | 1712 | if (xScale >= 0) { | ||
1727 | 1713 | tx = splashRound(matrix.x0 - 0.01); | ||
1728 | 1714 | tx2 = splashRound(matrix.x0 + xScale + 0.01) - 1; | ||
1729 | 1715 | } else { | ||
1730 | 1716 | tx = splashRound(matrix.x0 + 0.01) - 1; | ||
1731 | 1717 | tx2 = splashRound(matrix.x0 + xScale - 0.01); | ||
1732 | 1718 | } | ||
1733 | 1719 | *scaledWidth = abs(tx2 - tx) + 1; | ||
1734 | 1720 | //scaledWidth = splashRound(fabs(xScale)); | ||
1735 | 1721 | if (*scaledWidth == 0) { | ||
1736 | 1722 | // technically, this should draw nothing, but it generally seems | ||
1737 | 1723 | // better to draw a one-pixel-wide stripe rather than throwing it | ||
1738 | 1724 | // away | ||
1739 | 1725 | *scaledWidth = 1; | ||
1740 | 1726 | } | ||
1741 | 1727 | if (yScale >= 0) { | ||
1742 | 1728 | ty = splashFloor(matrix.y0 + 0.01); | ||
1743 | 1729 | ty2 = splashCeil(matrix.y0 + yScale - 0.01); | ||
1744 | 1730 | } else { | ||
1745 | 1731 | ty = splashCeil(matrix.y0 - 0.01); | ||
1746 | 1732 | ty2 = splashFloor(matrix.y0 + yScale + 0.01); | ||
1747 | 1733 | } | ||
1748 | 1734 | *scaledHeight = abs(ty2 - ty); | ||
1749 | 1735 | if (*scaledHeight == 0) { | ||
1750 | 1736 | *scaledHeight = 1; | ||
1751 | 1737 | } | ||
1752 | 1738 | } | ||
1753 | 1739 | |||
1754 | 1740 | cairo_surface_t *CairoOutputDev::downscaleSurface(cairo_surface_t *orig_surface) { | ||
1755 | 1741 | cairo_surface_t *dest_surface; | ||
1756 | 1742 | unsigned char *dest_buffer; | ||
1757 | 1743 | int dest_stride; | ||
1758 | 1744 | unsigned char *orig_buffer; | ||
1759 | 1745 | int orig_width, orig_height; | ||
1760 | 1746 | int orig_stride; | ||
1761 | 1747 | int scaledHeight; | ||
1762 | 1748 | int scaledWidth; | ||
1763 | 1749 | GBool res; | ||
1764 | 1750 | |||
1765 | 1751 | if (printing) | ||
1766 | 1752 | return NULL; | ||
1767 | 1753 | |||
1768 | 1754 | orig_width = cairo_image_surface_get_width (orig_surface); | ||
1769 | 1755 | orig_height = cairo_image_surface_get_height (orig_surface); | ||
1770 | 1756 | getScaledSize (orig_width, orig_height, &scaledWidth, &scaledHeight); | ||
1771 | 1757 | if (scaledWidth >= orig_width || scaledHeight >= orig_height) | ||
1772 | 1758 | return NULL; | ||
1773 | 1759 | |||
1774 | 1760 | dest_surface = cairo_surface_create_similar (orig_surface, | ||
1775 | 1761 | cairo_surface_get_content (orig_surface), | ||
1776 | 1762 | scaledWidth, scaledHeight); | ||
1777 | 1763 | dest_buffer = cairo_image_surface_get_data (dest_surface); | ||
1778 | 1764 | dest_stride = cairo_image_surface_get_stride (dest_surface); | ||
1779 | 1765 | |||
1780 | 1766 | orig_buffer = cairo_image_surface_get_data (orig_surface); | ||
1781 | 1767 | orig_stride = cairo_image_surface_get_stride (orig_surface); | ||
1782 | 1768 | |||
1783 | 1769 | res = downscale_box_filter((uint32_t *)orig_buffer, | ||
1784 | 1770 | orig_stride, orig_width, orig_height, | ||
1785 | 1771 | scaledWidth, scaledHeight, 0, 0, | ||
1786 | 1772 | scaledWidth, scaledHeight, | ||
1787 | 1773 | (uint32_t *)dest_buffer, dest_stride); | ||
1788 | 1774 | if (!res) { | ||
1789 | 1775 | cairo_surface_destroy (dest_surface); | ||
1790 | 1776 | return NULL; | ||
1791 | 1777 | } | ||
1792 | 1778 | |||
1793 | 1779 | return dest_surface; | ||
1794 | 1780 | |||
1795 | 1781 | } | ||
1796 | 1782 | |||
1797 | 1783 | cairo_filter_t | ||
1798 | 1784 | CairoOutputDev::getFilterForSurface(cairo_surface_t *image, | ||
1799 | 1785 | GBool interpolate) | ||
1800 | 1786 | { | ||
1801 | 1787 | if (interpolate) | ||
1802 | 1788 | return CAIRO_FILTER_BILINEAR; | ||
1803 | 1789 | |||
1804 | 1790 | int orig_width = cairo_image_surface_get_width (image); | ||
1805 | 1791 | int orig_height = cairo_image_surface_get_height (image); | ||
1806 | 1792 | if (orig_width == 0 || orig_height == 0) | ||
1807 | 1793 | return CAIRO_FILTER_NEAREST; | ||
1808 | 1794 | |||
1809 | 1795 | int scaled_width, scaled_height; | ||
1810 | 1796 | getScaledSize (orig_width, orig_height, &scaled_width, &scaled_height); | ||
1811 | 1797 | |||
1812 | 1798 | /* When scale factor is >= 400% we don't interpolate. See bugs #25268, #9860 */ | ||
1813 | 1799 | if (scaled_width / orig_width >= 4 || scaled_height / orig_height >= 4) | ||
1814 | 1800 | return CAIRO_FILTER_NEAREST; | ||
1815 | 1801 | |||
1816 | 1802 | return CAIRO_FILTER_BILINEAR; | ||
1817 | 1803 | } | ||
1818 | 1804 | |||
1819 | 1805 | void CairoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, | ||
1820 | 1806 | int width, int height, GBool invert, | ||
1821 | 1807 | GBool interpolate, GBool inlineImg) { | ||
1822 | 1808 | |||
1823 | 1809 | /* FIXME: Doesn't the image mask support any colorspace? */ | ||
1824 | 1810 | cairo_set_source (cairo, fill_pattern); | ||
1825 | 1811 | |||
1826 | 1812 | /* work around a cairo bug when scaling 1x1 surfaces */ | ||
1827 | 1813 | if (width == 1 && height == 1) { | ||
1828 | 1814 | ImageStream *imgStr; | ||
1829 | 1815 | Guchar pix; | ||
1830 | 1816 | int invert_bit; | ||
1831 | 1817 | |||
1832 | 1818 | imgStr = new ImageStream(str, width, 1, 1); | ||
1833 | 1819 | imgStr->reset(); | ||
1834 | 1820 | imgStr->getPixel(&pix); | ||
1835 | 1821 | imgStr->close(); | ||
1836 | 1822 | delete imgStr; | ||
1837 | 1823 | |||
1838 | 1824 | invert_bit = invert ? 1 : 0; | ||
1839 | 1825 | if (pix ^ invert_bit) | ||
1840 | 1826 | return; | ||
1841 | 1827 | |||
1842 | 1828 | cairo_save (cairo); | ||
1843 | 1829 | cairo_rectangle (cairo, 0., 0., width, height); | ||
1844 | 1830 | cairo_fill (cairo); | ||
1845 | 1831 | cairo_restore (cairo); | ||
1846 | 1832 | if (cairo_shape) { | ||
1847 | 1833 | cairo_save (cairo_shape); | ||
1848 | 1834 | cairo_rectangle (cairo_shape, 0., 0., width, height); | ||
1849 | 1835 | cairo_fill (cairo_shape); | ||
1850 | 1836 | cairo_restore (cairo_shape); | ||
1851 | 1837 | } | ||
1852 | 1838 | return; | ||
1853 | 1839 | } | ||
1854 | 1840 | |||
1855 | 1841 | /* shape is 1.0 for painted areas, 0.0 for unpainted ones */ | ||
1856 | 1842 | |||
1857 | 1843 | cairo_matrix_t matrix; | ||
1858 | 1844 | cairo_get_matrix (cairo, &matrix); | ||
1859 | 1845 | //XXX: it is possible that we should only do sub pixel positioning if | ||
1860 | 1846 | // we are rendering fonts */ | ||
1861 | 1847 | if (!printing && prescaleImages | ||
1862 | 1848 | /* not rotated */ | ||
1863 | 1849 | && matrix.xy == 0 && matrix.yx == 0 | ||
1864 | 1850 | /* axes not flipped / not 180 deg rotated */ | ||
1865 | 1851 | && matrix.xx > 0 && (upsideDown() ? -1 : 1) * matrix.yy > 0) { | ||
1866 | 1852 | drawImageMaskPrescaled(state, ref, str, width, height, invert, interpolate, inlineImg); | ||
1867 | 1853 | } else { | ||
1868 | 1854 | drawImageMaskRegular(state, ref, str, width, height, invert, interpolate, inlineImg); | ||
1869 | 1855 | } | ||
1870 | 1856 | |||
1871 | 1857 | } | ||
1872 | 1858 | |||
1873 | 1859 | void CairoOutputDev::setSoftMaskFromImageMask(GfxState *state, Object *ref, Stream *str, | ||
1874 | 1860 | int width, int height, GBool invert, | ||
1875 | 1861 | GBool inlineImg, double *baseMatrix) { | ||
1876 | 1862 | |||
1877 | 1863 | /* FIXME: Doesn't the image mask support any colorspace? */ | ||
1878 | 1864 | cairo_set_source (cairo, fill_pattern); | ||
1879 | 1865 | |||
1880 | 1866 | /* work around a cairo bug when scaling 1x1 surfaces */ | ||
1881 | 1867 | if (width == 1 && height == 1) { | ||
1882 | 1868 | ImageStream *imgStr; | ||
1883 | 1869 | Guchar pix; | ||
1884 | 1870 | int invert_bit; | ||
1885 | 1871 | |||
1886 | 1872 | imgStr = new ImageStream(str, width, 1, 1); | ||
1887 | 1873 | imgStr->reset(); | ||
1888 | 1874 | imgStr->getPixel(&pix); | ||
1889 | 1875 | imgStr->close(); | ||
1890 | 1876 | delete imgStr; | ||
1891 | 1877 | |||
1892 | 1878 | invert_bit = invert ? 1 : 0; | ||
1893 | 1879 | if (pix ^ invert_bit) | ||
1894 | 1880 | return; | ||
1895 | 1881 | |||
1896 | 1882 | cairo_save (cairo); | ||
1897 | 1883 | cairo_rectangle (cairo, 0., 0., width, height); | ||
1898 | 1884 | cairo_fill (cairo); | ||
1899 | 1885 | cairo_restore (cairo); | ||
1900 | 1886 | if (cairo_shape) { | ||
1901 | 1887 | cairo_save (cairo_shape); | ||
1902 | 1888 | cairo_rectangle (cairo_shape, 0., 0., width, height); | ||
1903 | 1889 | cairo_fill (cairo_shape); | ||
1904 | 1890 | cairo_restore (cairo_shape); | ||
1905 | 1891 | } | ||
1906 | 1892 | return; | ||
1907 | 1893 | } | ||
1908 | 1894 | |||
1909 | 1895 | cairo_push_group_with_content (cairo, CAIRO_CONTENT_ALPHA); | ||
1910 | 1896 | |||
1911 | 1897 | /* shape is 1.0 for painted areas, 0.0 for unpainted ones */ | ||
1912 | 1898 | |||
1913 | 1899 | cairo_matrix_t matrix; | ||
1914 | 1900 | cairo_get_matrix (cairo, &matrix); | ||
1915 | 1901 | //XXX: it is possible that we should only do sub pixel positioning if | ||
1916 | 1902 | // we are rendering fonts */ | ||
1917 | 1903 | if (!printing && prescaleImages && matrix.xy == 0.0 && matrix.yx == 0.0) { | ||
1918 | 1904 | drawImageMaskPrescaled(state, ref, str, width, height, invert, gFalse, inlineImg); | ||
1919 | 1905 | } else { | ||
1920 | 1906 | drawImageMaskRegular(state, ref, str, width, height, invert, gFalse, inlineImg); | ||
1921 | 1907 | } | ||
1922 | 1908 | |||
1923 | 1909 | if (state->getFillColorSpace()->getMode() == csPattern) { | ||
1924 | 1910 | cairo_set_source_rgb (cairo, 1, 1, 1); | ||
1925 | 1911 | cairo_set_matrix (cairo, &mask_matrix); | ||
1926 | 1912 | cairo_mask (cairo, mask); | ||
1927 | 1913 | } | ||
1928 | 1914 | |||
1929 | 1915 | if (mask) | ||
1930 | 1916 | cairo_pattern_destroy (mask); | ||
1931 | 1917 | mask = cairo_pop_group (cairo); | ||
1932 | 1918 | |||
1933 | 1919 | saveState(state); | ||
1934 | 1920 | double bbox[4] = {0,0,1,1}; // dummy | ||
1935 | 1921 | beginTransparencyGroup(state, bbox, state->getFillColorSpace(), | ||
1936 | 1922 | gTrue, gFalse, gFalse); | ||
1937 | 1923 | } | ||
1938 | 1924 | |||
1939 | 1925 | void CairoOutputDev::unsetSoftMaskFromImageMask(GfxState *state, double *baseMatrix) { | ||
1940 | 1926 | double bbox[4] = {0,0,1,1}; // dummy | ||
1941 | 1927 | |||
1942 | 1928 | endTransparencyGroup(state); | ||
1943 | 1929 | restoreState(state); | ||
1944 | 1930 | paintTransparencyGroup(state, bbox); | ||
1945 | 1931 | clearSoftMask(state); | ||
1946 | 1932 | } | ||
1947 | 1933 | |||
1948 | 1934 | void CairoOutputDev::drawImageMaskRegular(GfxState *state, Object *ref, Stream *str, | ||
1949 | 1935 | int width, int height, GBool invert, | ||
1950 | 1936 | GBool interpolate, GBool inlineImg) { | ||
1951 | 1937 | unsigned char *buffer; | ||
1952 | 1938 | unsigned char *dest; | ||
1953 | 1939 | cairo_surface_t *image; | ||
1954 | 1940 | cairo_pattern_t *pattern; | ||
1955 | 1941 | int x, y, i, bit; | ||
1956 | 1942 | ImageStream *imgStr; | ||
1957 | 1943 | Guchar *pix; | ||
1958 | 1944 | cairo_matrix_t matrix; | ||
1959 | 1945 | int invert_bit; | ||
1960 | 1946 | int row_stride; | ||
1961 | 1947 | cairo_filter_t filter; | ||
1962 | 1948 | |||
1963 | 1949 | /* TODO: Do we want to cache these? */ | ||
1964 | 1950 | imgStr = new ImageStream(str, width, 1, 1); | ||
1965 | 1951 | imgStr->reset(); | ||
1966 | 1952 | |||
1967 | 1953 | image = cairo_image_surface_create (CAIRO_FORMAT_A1, width, height); | ||
1968 | 1954 | if (cairo_surface_status (image)) | ||
1969 | 1955 | goto cleanup; | ||
1970 | 1956 | |||
1971 | 1957 | buffer = cairo_image_surface_get_data (image); | ||
1972 | 1958 | row_stride = cairo_image_surface_get_stride (image); | ||
1973 | 1959 | |||
1974 | 1960 | invert_bit = invert ? 1 : 0; | ||
1975 | 1961 | |||
1976 | 1962 | for (y = 0; y < height; y++) { | ||
1977 | 1963 | pix = imgStr->getLine(); | ||
1978 | 1964 | dest = buffer + y * row_stride; | ||
1979 | 1965 | i = 0; | ||
1980 | 1966 | bit = 0; | ||
1981 | 1967 | for (x = 0; x < width; x++) { | ||
1982 | 1968 | if (bit == 0) | ||
1983 | 1969 | dest[i] = 0; | ||
1984 | 1970 | if (!(pix[x] ^ invert_bit)) { | ||
1985 | 1971 | #ifdef WORDS_BIGENDIAN | ||
1986 | 1972 | dest[i] |= (1 << (7 - bit)); | ||
1987 | 1973 | #else | ||
1988 | 1974 | dest[i] |= (1 << bit); | ||
1989 | 1975 | #endif | ||
1990 | 1976 | } | ||
1991 | 1977 | bit++; | ||
1992 | 1978 | if (bit > 7) { | ||
1993 | 1979 | bit = 0; | ||
1994 | 1980 | i++; | ||
1995 | 1981 | } | ||
1996 | 1982 | } | ||
1997 | 1983 | } | ||
1998 | 1984 | |||
1999 | 1985 | filter = getFilterForSurface (image, interpolate); | ||
2000 | 1986 | |||
2001 | 1987 | cairo_surface_mark_dirty (image); | ||
2002 | 1988 | pattern = cairo_pattern_create_for_surface (image); | ||
2003 | 1989 | cairo_surface_destroy (image); | ||
2004 | 1990 | if (cairo_pattern_status (pattern)) | ||
2005 | 1991 | goto cleanup; | ||
2006 | 1992 | |||
2007 | 1993 | LOG (printf ("drawImageMask %dx%d\n", width, height)); | ||
2008 | 1994 | |||
2009 | 1995 | cairo_pattern_set_filter (pattern, filter); | ||
2010 | 1996 | |||
2011 | 1997 | if (!printing) | ||
2012 | 1998 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); | ||
2013 | 1999 | |||
2014 | 2000 | cairo_matrix_init_translate (&matrix, 0, height); | ||
2015 | 2001 | cairo_matrix_scale (&matrix, width, -height); | ||
2016 | 2002 | cairo_pattern_set_matrix (pattern, &matrix); | ||
2017 | 2003 | if (cairo_pattern_status (pattern)) { | ||
2018 | 2004 | cairo_pattern_destroy (pattern); | ||
2019 | 2005 | goto cleanup; | ||
2020 | 2006 | } | ||
2021 | 2007 | |||
2022 | 2008 | if (state->getFillColorSpace()->getMode() == csPattern) { | ||
2023 | 2009 | mask = cairo_pattern_reference (pattern); | ||
2024 | 2010 | cairo_get_matrix (cairo, &mask_matrix); | ||
2025 | 2011 | } else if (!printing) { | ||
2026 | 2012 | cairo_save (cairo); | ||
2027 | 2013 | cairo_rectangle (cairo, 0., 0., 1., 1.); | ||
2028 | 2014 | cairo_clip (cairo); | ||
2029 | 2015 | cairo_mask (cairo, pattern); | ||
2030 | 2016 | cairo_restore (cairo); | ||
2031 | 2017 | } else { | ||
2032 | 2018 | cairo_mask (cairo, pattern); | ||
2033 | 2019 | } | ||
2034 | 2020 | |||
2035 | 2021 | if (cairo_shape) { | ||
2036 | 2022 | cairo_save (cairo_shape); | ||
2037 | 2023 | cairo_set_source (cairo_shape, pattern); | ||
2038 | 2024 | if (!printing) { | ||
2039 | 2025 | cairo_rectangle (cairo_shape, 0., 0., 1., 1.); | ||
2040 | 2026 | cairo_fill (cairo_shape); | ||
2041 | 2027 | } else { | ||
2042 | 2028 | cairo_mask (cairo_shape, pattern); | ||
2043 | 2029 | } | ||
2044 | 2030 | cairo_restore (cairo_shape); | ||
2045 | 2031 | } | ||
2046 | 2032 | |||
2047 | 2033 | cairo_pattern_destroy (pattern); | ||
2048 | 2034 | |||
2049 | 2035 | cleanup: | ||
2050 | 2036 | imgStr->close(); | ||
2051 | 2037 | delete imgStr; | ||
2052 | 2038 | } | ||
2053 | 2039 | |||
2054 | 2040 | |||
2055 | 2041 | void CairoOutputDev::drawImageMaskPrescaled(GfxState *state, Object *ref, Stream *str, | ||
2056 | 2042 | int width, int height, GBool invert, | ||
2057 | 2043 | GBool interpolate, GBool inlineImg) { | ||
2058 | 2044 | unsigned char *buffer; | ||
2059 | 2045 | cairo_surface_t *image; | ||
2060 | 2046 | cairo_pattern_t *pattern; | ||
2061 | 2047 | ImageStream *imgStr; | ||
2062 | 2048 | Guchar *pix; | ||
2063 | 2049 | cairo_matrix_t matrix; | ||
2064 | 2050 | int invert_bit; | ||
2065 | 2051 | int row_stride; | ||
2066 | 2052 | |||
2067 | 2053 | /* cairo does a very poor job of scaling down images so we scale them ourselves */ | ||
2068 | 2054 | |||
2069 | 2055 | LOG (printf ("drawImageMaskPrescaled %dx%d\n", width, height)); | ||
2070 | 2056 | |||
2071 | 2057 | /* this scaling code is adopted from the splash image scaling code */ | ||
2072 | 2058 | cairo_get_matrix(cairo, &matrix); | ||
2073 | 2059 | #if 0 | ||
2074 | 2060 | printf("[%f %f], [%f %f], %f %f\n", matrix.xx, matrix.xy, matrix.yx, matrix.yy, matrix.x0, matrix.y0); | ||
2075 | 2061 | #endif | ||
2076 | 2062 | /* this whole computation should be factored out */ | ||
2077 | 2063 | double xScale = matrix.xx; | ||
2078 | 2064 | double yScale = matrix.yy; | ||
2079 | 2065 | int tx, tx2, ty, ty2; /* the integer co-oridinates of the resulting image */ | ||
2080 | 2066 | int scaledHeight; | ||
2081 | 2067 | int scaledWidth; | ||
2082 | 2068 | if (xScale >= 0) { | ||
2083 | 2069 | tx = splashRound(matrix.x0 - 0.01); | ||
2084 | 2070 | tx2 = splashRound(matrix.x0 + xScale + 0.01) - 1; | ||
2085 | 2071 | } else { | ||
2086 | 2072 | tx = splashRound(matrix.x0 + 0.01) - 1; | ||
2087 | 2073 | tx2 = splashRound(matrix.x0 + xScale - 0.01); | ||
2088 | 2074 | } | ||
2089 | 2075 | scaledWidth = abs(tx2 - tx) + 1; | ||
2090 | 2076 | //scaledWidth = splashRound(fabs(xScale)); | ||
2091 | 2077 | if (scaledWidth == 0) { | ||
2092 | 2078 | // technically, this should draw nothing, but it generally seems | ||
2093 | 2079 | // better to draw a one-pixel-wide stripe rather than throwing it | ||
2094 | 2080 | // away | ||
2095 | 2081 | scaledWidth = 1; | ||
2096 | 2082 | } | ||
2097 | 2083 | if (yScale >= 0) { | ||
2098 | 2084 | ty = splashFloor(matrix.y0 + 0.01); | ||
2099 | 2085 | ty2 = splashCeil(matrix.y0 + yScale - 0.01); | ||
2100 | 2086 | } else { | ||
2101 | 2087 | ty = splashCeil(matrix.y0 - 0.01); | ||
2102 | 2088 | ty2 = splashFloor(matrix.y0 + yScale + 0.01); | ||
2103 | 2089 | } | ||
2104 | 2090 | scaledHeight = abs(ty2 - ty); | ||
2105 | 2091 | if (scaledHeight == 0) { | ||
2106 | 2092 | scaledHeight = 1; | ||
2107 | 2093 | } | ||
2108 | 2094 | #if 0 | ||
2109 | 2095 | printf("xscale: %g, yscale: %g\n", xScale, yScale); | ||
2110 | 2096 | printf("width: %d, height: %d\n", width, height); | ||
2111 | 2097 | printf("scaledWidth: %d, scaledHeight: %d\n", scaledWidth, scaledHeight); | ||
2112 | 2098 | #endif | ||
2113 | 2099 | |||
2114 | 2100 | /* compute the required padding */ | ||
2115 | 2101 | /* Padding is used to preserve the aspect ratio. | ||
2116 | 2102 | We compute total_pad to make (height+total_pad)/scaledHeight as close to height/yScale as possible */ | ||
2117 | 2103 | int head_pad = 0; | ||
2118 | 2104 | int tail_pad = 0; | ||
2119 | 2105 | int total_pad = splashRound(height*(scaledHeight/fabs(yScale)) - height); | ||
2120 | 2106 | |||
2121 | 2107 | /* compute the two pieces of padding */ | ||
2122 | 2108 | if (total_pad > 0) { | ||
2123 | 2109 | //XXX: i'm not positive fabs() is correct | ||
2124 | 2110 | float tail_error = fabs(matrix.y0 - ty); | ||
2125 | 2111 | float head_error = fabs(ty2 - (matrix.y0 + yScale)); | ||
2126 | 2112 | float tail_fraction = tail_error/(tail_error + head_error); | ||
2127 | 2113 | tail_pad = splashRound(total_pad*tail_fraction); | ||
2128 | 2114 | head_pad = total_pad - tail_pad; | ||
2129 | 2115 | } else { | ||
2130 | 2116 | tail_pad = 0; | ||
2131 | 2117 | head_pad = 0; | ||
2132 | 2118 | } | ||
2133 | 2119 | int origHeight = height; | ||
2134 | 2120 | height += tail_pad; | ||
2135 | 2121 | height += head_pad; | ||
2136 | 2122 | #if 0 | ||
2137 | 2123 | printf("head_pad: %d tail_pad: %d\n", head_pad, tail_pad); | ||
2138 | 2124 | printf("origHeight: %d height: %d\n", origHeight, height); | ||
2139 | 2125 | printf("ty: %d, ty2: %d\n", ty, ty2); | ||
2140 | 2126 | #endif | ||
2141 | 2127 | |||
2142 | 2128 | /* TODO: Do we want to cache these? */ | ||
2143 | 2129 | imgStr = new ImageStream(str, width, 1, 1); | ||
2144 | 2130 | imgStr->reset(); | ||
2145 | 2131 | |||
2146 | 2132 | invert_bit = invert ? 1 : 0; | ||
2147 | 2133 | |||
2148 | 2134 | image = cairo_image_surface_create (CAIRO_FORMAT_A8, scaledWidth, scaledHeight); | ||
2149 | 2135 | if (cairo_surface_status (image)) { | ||
2150 | 2136 | imgStr->close(); | ||
2151 | 2137 | delete imgStr; | ||
2152 | 2138 | return; | ||
2153 | 2139 | } | ||
2154 | 2140 | |||
2155 | 2141 | buffer = cairo_image_surface_get_data (image); | ||
2156 | 2142 | row_stride = cairo_image_surface_get_stride (image); | ||
2157 | 2143 | |||
2158 | 2144 | int yp = height / scaledHeight; | ||
2159 | 2145 | int yq = height % scaledHeight; | ||
2160 | 2146 | int xp = width / scaledWidth; | ||
2161 | 2147 | int xq = width % scaledWidth; | ||
2162 | 2148 | int yt = 0; | ||
2163 | 2149 | int origHeight_c = origHeight; | ||
2164 | 2150 | /* use MIN() because yp might be > origHeight because of padding */ | ||
2165 | 2151 | unsigned char *pixBuf = (unsigned char *)malloc(MIN(yp+1, origHeight)*width); | ||
2166 | 2152 | int lastYStep = 1; | ||
2167 | 2153 | int total = 0; | ||
2168 | 2154 | for (int y = 0; y < scaledHeight; y++) { | ||
2169 | 2155 | // y scale Bresenham | ||
2170 | 2156 | int yStep = yp; | ||
2171 | 2157 | yt += yq; | ||
2172 | 2158 | |||
2173 | 2159 | if (yt >= scaledHeight) { | ||
2174 | 2160 | yt -= scaledHeight; | ||
2175 | 2161 | ++yStep; | ||
2176 | 2162 | } | ||
2177 | 2163 | |||
2178 | 2164 | // read row (s) from image ignoring the padding as appropriate | ||
2179 | 2165 | { | ||
2180 | 2166 | int n = (yp > 0) ? yStep : lastYStep; | ||
2181 | 2167 | total += n; | ||
2182 | 2168 | if (n > 0) { | ||
2183 | 2169 | unsigned char *p = pixBuf; | ||
2184 | 2170 | int head_pad_count = head_pad; | ||
2185 | 2171 | int origHeight_count = origHeight; | ||
2186 | 2172 | int tail_pad_count = tail_pad; | ||
2187 | 2173 | for (int i=0; i<n; i++) { | ||
2188 | 2174 | // get row | ||
2189 | 2175 | if (head_pad_count) { | ||
2190 | 2176 | head_pad_count--; | ||
2191 | 2177 | } else if (origHeight_count) { | ||
2192 | 2178 | pix = imgStr->getLine(); | ||
2193 | 2179 | for (int j=0; j<width; j++) { | ||
2194 | 2180 | if (pix[j] ^ invert_bit) | ||
2195 | 2181 | p[j] = 0; | ||
2196 | 2182 | else | ||
2197 | 2183 | p[j] = 255; | ||
2198 | 2184 | } | ||
2199 | 2185 | origHeight_count--; | ||
2200 | 2186 | p += width; | ||
2201 | 2187 | } else if (tail_pad_count) { | ||
2202 | 2188 | tail_pad_count--; | ||
2203 | 2189 | } else { | ||
2204 | 2190 | printf("%d %d\n", n, total); | ||
2205 | 2191 | assert(0 && "over run\n"); | ||
2206 | 2192 | } | ||
2207 | 2193 | } | ||
2208 | 2194 | } | ||
2209 | 2195 | } | ||
2210 | 2196 | |||
2211 | 2197 | lastYStep = yStep; | ||
2212 | 2198 | int k1 = y; | ||
2213 | 2199 | |||
2214 | 2200 | int xt = 0; | ||
2215 | 2201 | int xSrc = 0; | ||
2216 | 2202 | int x1 = k1; | ||
2217 | 2203 | int n = yStep > 0 ? yStep : 1; | ||
2218 | 2204 | int origN = n; | ||
2219 | 2205 | |||
2220 | 2206 | /* compute the size of padding and pixels that will be used for this row */ | ||
2221 | 2207 | int head_pad_size = MIN(n, head_pad); | ||
2222 | 2208 | n -= head_pad_size; | ||
2223 | 2209 | head_pad -= MIN(head_pad_size, yStep); | ||
2224 | 2210 | |||
2225 | 2211 | int pix_size = MIN(n, origHeight); | ||
2226 | 2212 | n -= pix_size; | ||
2227 | 2213 | origHeight -= MIN(pix_size, yStep); | ||
2228 | 2214 | |||
2229 | 2215 | int tail_pad_size = MIN(n, tail_pad); | ||
2230 | 2216 | n -= tail_pad_size; | ||
2231 | 2217 | tail_pad -= MIN(tail_pad_size, yStep); | ||
2232 | 2218 | if (n != 0) { | ||
2233 | 2219 | printf("n = %d (%d %d %d)\n", n, head_pad_size, pix_size, tail_pad_size); | ||
2234 | 2220 | assert(n == 0); | ||
2235 | 2221 | } | ||
2236 | 2222 | |||
2237 | 2223 | for (int x = 0; x < scaledWidth; ++x) { | ||
2238 | 2224 | int xStep = xp; | ||
2239 | 2225 | xt += xq; | ||
2240 | 2226 | if (xt >= scaledWidth) { | ||
2241 | 2227 | xt -= scaledWidth; | ||
2242 | 2228 | ++xStep; | ||
2243 | 2229 | } | ||
2244 | 2230 | int m = xStep > 0 ? xStep : 1; | ||
2245 | 2231 | float pixAcc0 = 0; | ||
2246 | 2232 | /* could m * head_pad_size * tail_pad_size overflow? */ | ||
2247 | 2233 | if (invert_bit) { | ||
2248 | 2234 | pixAcc0 += m * head_pad_size * tail_pad_size * 255; | ||
2249 | 2235 | } else { | ||
2250 | 2236 | pixAcc0 += m * head_pad_size * tail_pad_size * 0; | ||
2251 | 2237 | } | ||
2252 | 2238 | /* Accumulate all of the source pixels for the destination pixel */ | ||
2253 | 2239 | for (int i = 0; i < pix_size; ++i) { | ||
2254 | 2240 | for (int j = 0; j< m; ++j) { | ||
2255 | 2241 | if (xSrc + i*width + j > MIN(yp + 1, origHeight_c)*width) { | ||
2256 | 2242 | printf("%d > %d (%d %d %d %d) (%d %d %d)\n", xSrc + i*width + j, MIN(yp + 1, origHeight_c)*width, xSrc, i , width, j, yp, origHeight_c, width); | ||
2257 | 2243 | printf("%d %d %d\n", head_pad_size, pix_size, tail_pad_size); | ||
2258 | 2244 | assert(0 && "bad access\n"); | ||
2259 | 2245 | } | ||
2260 | 2246 | pixAcc0 += pixBuf[xSrc + i*width + j]; | ||
2261 | 2247 | } | ||
2262 | 2248 | } | ||
2263 | 2249 | buffer[y * row_stride + x] = splashFloor(pixAcc0 / (origN*m)); | ||
2264 | 2250 | xSrc += xStep; | ||
2265 | 2251 | x1 += 1; | ||
2266 | 2252 | } | ||
2267 | 2253 | |||
2268 | 2254 | } | ||
2269 | 2255 | free(pixBuf); | ||
2270 | 2256 | |||
2271 | 2257 | cairo_surface_mark_dirty (image); | ||
2272 | 2258 | pattern = cairo_pattern_create_for_surface (image); | ||
2273 | 2259 | cairo_surface_destroy (image); | ||
2274 | 2260 | if (cairo_pattern_status (pattern)) { | ||
2275 | 2261 | imgStr->close(); | ||
2276 | 2262 | delete imgStr; | ||
2277 | 2263 | return; | ||
2278 | 2264 | } | ||
2279 | 2265 | |||
2280 | 2266 | /* we should actually be using CAIRO_FILTER_NEAREST here. However, | ||
2281 | 2267 | * cairo doesn't yet do minifaction filtering causing scaled down | ||
2282 | 2268 | * images with CAIRO_FILTER_NEAREST to look really bad */ | ||
2283 | 2269 | cairo_pattern_set_filter (pattern, | ||
2284 | 2270 | interpolate ? CAIRO_FILTER_BEST : CAIRO_FILTER_FAST); | ||
2285 | 2271 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); | ||
2286 | 2272 | |||
2287 | 2273 | if (state->getFillColorSpace()->getMode() == csPattern) { | ||
2288 | 2274 | cairo_matrix_init_translate (&matrix, 0, scaledHeight); | ||
2289 | 2275 | cairo_matrix_scale (&matrix, scaledWidth, -scaledHeight); | ||
2290 | 2276 | cairo_pattern_set_matrix (pattern, &matrix); | ||
2291 | 2277 | if (cairo_pattern_status (pattern)) { | ||
2292 | 2278 | cairo_pattern_destroy (pattern); | ||
2293 | 2279 | imgStr->close(); | ||
2294 | 2280 | delete imgStr; | ||
2295 | 2281 | return; | ||
2296 | 2282 | } | ||
2297 | 2283 | |||
2298 | 2284 | mask = cairo_pattern_reference (pattern); | ||
2299 | 2285 | cairo_get_matrix (cairo, &mask_matrix); | ||
2300 | 2286 | } else { | ||
2301 | 2287 | cairo_save (cairo); | ||
2302 | 2288 | |||
2303 | 2289 | /* modify our current transformation so that the prescaled image | ||
2304 | 2290 | * goes where it is supposed to */ | ||
2305 | 2291 | cairo_get_matrix(cairo, &matrix); | ||
2306 | 2292 | cairo_scale(cairo, 1.0/matrix.xx, 1.0/matrix.yy); | ||
2307 | 2293 | // get integer co-ords | ||
2308 | 2294 | cairo_translate (cairo, tx - matrix.x0, ty2 - matrix.y0); | ||
2309 | 2295 | if (yScale > 0) | ||
2310 | 2296 | cairo_scale(cairo, 1, -1); | ||
2311 | 2297 | |||
2312 | 2298 | cairo_rectangle (cairo, 0., 0., scaledWidth, scaledHeight); | ||
2313 | 2299 | cairo_clip (cairo); | ||
2314 | 2300 | cairo_mask (cairo, pattern); | ||
2315 | 2301 | |||
2316 | 2302 | //cairo_get_matrix(cairo, &matrix); | ||
2317 | 2303 | //printf("mask at: [%f %f], [%f %f], %f %f\n\n", matrix.xx, matrix.xy, matrix.yx, matrix.yy, matrix.x0, matrix.y0); | ||
2318 | 2304 | cairo_restore(cairo); | ||
2319 | 2305 | } | ||
2320 | 2306 | |||
2321 | 2307 | if (cairo_shape) { | ||
2322 | 2308 | cairo_save (cairo_shape); | ||
2323 | 2309 | |||
2324 | 2310 | /* modify our current transformation so that the prescaled image | ||
2325 | 2311 | * goes where it is supposed to */ | ||
2326 | 2312 | cairo_get_matrix(cairo_shape, &matrix); | ||
2327 | 2313 | cairo_scale(cairo_shape, 1.0/matrix.xx, 1.0/matrix.yy); | ||
2328 | 2314 | // get integer co-ords | ||
2329 | 2315 | cairo_translate (cairo_shape, tx - matrix.x0, ty2 - matrix.y0); | ||
2330 | 2316 | if (yScale > 0) | ||
2331 | 2317 | cairo_scale(cairo_shape, 1, -1); | ||
2332 | 2318 | |||
2333 | 2319 | cairo_rectangle (cairo_shape, 0., 0., scaledWidth, scaledHeight); | ||
2334 | 2320 | cairo_fill (cairo_shape); | ||
2335 | 2321 | |||
2336 | 2322 | cairo_restore(cairo_shape); | ||
2337 | 2323 | } | ||
2338 | 2324 | |||
2339 | 2325 | cairo_pattern_destroy (pattern); | ||
2340 | 2326 | |||
2341 | 2327 | imgStr->close(); | ||
2342 | 2328 | delete imgStr; | ||
2343 | 2329 | } | ||
2344 | 2330 | |||
2345 | 2331 | void CairoOutputDev::drawMaskedImage(GfxState *state, Object *ref, | ||
2346 | 2332 | Stream *str, int width, int height, | ||
2347 | 2333 | GfxImageColorMap *colorMap, | ||
2348 | 2334 | GBool interpolate, | ||
2349 | 2335 | Stream *maskStr, int maskWidth, | ||
2350 | 2336 | int maskHeight, GBool maskInvert, | ||
2351 | 2337 | GBool maskInterpolate) | ||
2352 | 2338 | { | ||
2353 | 2339 | ImageStream *maskImgStr, *imgStr; | ||
2354 | 2340 | int row_stride; | ||
2355 | 2341 | unsigned char *maskBuffer, *buffer; | ||
2356 | 2342 | unsigned char *maskDest; | ||
2357 | 2343 | unsigned int *dest; | ||
2358 | 2344 | cairo_surface_t *maskImage, *image; | ||
2359 | 2345 | cairo_pattern_t *maskPattern, *pattern; | ||
2360 | 2346 | cairo_matrix_t matrix; | ||
2361 | 2347 | cairo_matrix_t maskMatrix; | ||
2362 | 2348 | Guchar *pix; | ||
2363 | 2349 | int x, y; | ||
2364 | 2350 | int invert_bit; | ||
2365 | 2351 | cairo_filter_t filter; | ||
2366 | 2352 | cairo_filter_t maskFilter; | ||
2367 | 2353 | |||
2368 | 2354 | maskImgStr = new ImageStream(maskStr, maskWidth, 1, 1); | ||
2369 | 2355 | maskImgStr->reset(); | ||
2370 | 2356 | |||
2371 | 2357 | maskImage = cairo_image_surface_create (CAIRO_FORMAT_A8, maskWidth, maskHeight); | ||
2372 | 2358 | if (cairo_surface_status (maskImage)) { | ||
2373 | 2359 | maskImgStr->close(); | ||
2374 | 2360 | delete maskImgStr; | ||
2375 | 2361 | return; | ||
2376 | 2362 | } | ||
2377 | 2363 | |||
2378 | 2364 | maskBuffer = cairo_image_surface_get_data (maskImage); | ||
2379 | 2365 | row_stride = cairo_image_surface_get_stride (maskImage); | ||
2380 | 2366 | |||
2381 | 2367 | invert_bit = maskInvert ? 1 : 0; | ||
2382 | 2368 | |||
2383 | 2369 | for (y = 0; y < maskHeight; y++) { | ||
2384 | 2370 | pix = maskImgStr->getLine(); | ||
2385 | 2371 | maskDest = maskBuffer + y * row_stride; | ||
2386 | 2372 | for (x = 0; x < maskWidth; x++) { | ||
2387 | 2373 | if (pix[x] ^ invert_bit) | ||
2388 | 2374 | *maskDest++ = 0; | ||
2389 | 2375 | else | ||
2390 | 2376 | *maskDest++ = 255; | ||
2391 | 2377 | } | ||
2392 | 2378 | } | ||
2393 | 2379 | |||
2394 | 2380 | maskImgStr->close(); | ||
2395 | 2381 | delete maskImgStr; | ||
2396 | 2382 | |||
2397 | 2383 | maskFilter = getFilterForSurface (maskImage, maskInterpolate); | ||
2398 | 2384 | |||
2399 | 2385 | cairo_surface_mark_dirty (maskImage); | ||
2400 | 2386 | maskPattern = cairo_pattern_create_for_surface (maskImage); | ||
2401 | 2387 | cairo_surface_destroy (maskImage); | ||
2402 | 2388 | if (cairo_pattern_status (maskPattern)) | ||
2403 | 2389 | return; | ||
2404 | 2390 | |||
2405 | 2391 | #if 0 | ||
2406 | 2392 | /* ICCBased color space doesn't do any color correction | ||
2407 | 2393 | * so check its underlying color space as well */ | ||
2408 | 2394 | int is_identity_transform; | ||
2409 | 2395 | is_identity_transform = colorMap->getColorSpace()->getMode() == csDeviceRGB || | ||
2410 | 2396 | (colorMap->getColorSpace()->getMode() == csICCBased && | ||
2411 | 2397 | ((GfxICCBasedColorSpace*)colorMap->getColorSpace())->getAlt()->getMode() == csDeviceRGB); | ||
2412 | 2398 | #endif | ||
2413 | 2399 | |||
2414 | 2400 | /* TODO: Do we want to cache these? */ | ||
2415 | 2401 | imgStr = new ImageStream(str, width, | ||
2416 | 2402 | colorMap->getNumPixelComps(), | ||
2417 | 2403 | colorMap->getBits()); | ||
2418 | 2404 | imgStr->reset(); | ||
2419 | 2405 | |||
2420 | 2406 | image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); | ||
2421 | 2407 | if (cairo_surface_status (image)) | ||
2422 | 2408 | goto cleanup; | ||
2423 | 2409 | |||
2424 | 2410 | buffer = cairo_image_surface_get_data (image); | ||
2425 | 2411 | row_stride = cairo_image_surface_get_stride (image); | ||
2426 | 2412 | for (y = 0; y < height; y++) { | ||
2427 | 2413 | dest = (unsigned int *) (buffer + y * row_stride); | ||
2428 | 2414 | pix = imgStr->getLine(); | ||
2429 | 2415 | colorMap->getRGBLine (pix, dest, width); | ||
2430 | 2416 | } | ||
2431 | 2417 | |||
2432 | 2418 | filter = getFilterForSurface (image, interpolate); | ||
2433 | 2419 | |||
2434 | 2420 | cairo_surface_mark_dirty (image); | ||
2435 | 2421 | pattern = cairo_pattern_create_for_surface (image); | ||
2436 | 2422 | cairo_surface_destroy (image); | ||
2437 | 2423 | if (cairo_pattern_status (pattern)) | ||
2438 | 2424 | goto cleanup; | ||
2439 | 2425 | |||
2440 | 2426 | LOG (printf ("drawMaskedImage %dx%d\n", width, height)); | ||
2441 | 2427 | |||
2442 | 2428 | cairo_pattern_set_filter (pattern, filter); | ||
2443 | 2429 | cairo_pattern_set_filter (maskPattern, maskFilter); | ||
2444 | 2430 | |||
2445 | 2431 | if (!printing) { | ||
2446 | 2432 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); | ||
2447 | 2433 | cairo_pattern_set_extend (maskPattern, CAIRO_EXTEND_PAD); | ||
2448 | 2434 | } | ||
2449 | 2435 | |||
2450 | 2436 | cairo_matrix_init_translate (&matrix, 0, height); | ||
2451 | 2437 | cairo_matrix_scale (&matrix, width, -height); | ||
2452 | 2438 | cairo_pattern_set_matrix (pattern, &matrix); | ||
2453 | 2439 | if (cairo_pattern_status (pattern)) { | ||
2454 | 2440 | cairo_pattern_destroy (pattern); | ||
2455 | 2441 | cairo_pattern_destroy (maskPattern); | ||
2456 | 2442 | goto cleanup; | ||
2457 | 2443 | } | ||
2458 | 2444 | |||
2459 | 2445 | cairo_matrix_init_translate (&maskMatrix, 0, maskHeight); | ||
2460 | 2446 | cairo_matrix_scale (&maskMatrix, maskWidth, -maskHeight); | ||
2461 | 2447 | cairo_pattern_set_matrix (maskPattern, &maskMatrix); | ||
2462 | 2448 | if (cairo_pattern_status (maskPattern)) { | ||
2463 | 2449 | cairo_pattern_destroy (maskPattern); | ||
2464 | 2450 | cairo_pattern_destroy (pattern); | ||
2465 | 2451 | goto cleanup; | ||
2466 | 2452 | } | ||
2467 | 2453 | |||
2468 | 2454 | if (!printing) { | ||
2469 | 2455 | cairo_save (cairo); | ||
2470 | 2456 | cairo_set_source (cairo, pattern); | ||
2471 | 2457 | cairo_rectangle (cairo, 0., 0., 1., 1.); | ||
2472 | 2458 | cairo_clip (cairo); | ||
2473 | 2459 | cairo_mask (cairo, maskPattern); | ||
2474 | 2460 | cairo_restore (cairo); | ||
2475 | 2461 | } else { | ||
2476 | 2462 | cairo_set_source (cairo, pattern); | ||
2477 | 2463 | cairo_mask (cairo, maskPattern); | ||
2478 | 2464 | } | ||
2479 | 2465 | |||
2480 | 2466 | if (cairo_shape) { | ||
2481 | 2467 | cairo_save (cairo_shape); | ||
2482 | 2468 | cairo_set_source (cairo_shape, pattern); | ||
2483 | 2469 | if (!printing) { | ||
2484 | 2470 | cairo_rectangle (cairo_shape, 0., 0., 1., 1.); | ||
2485 | 2471 | cairo_fill (cairo_shape); | ||
2486 | 2472 | } else { | ||
2487 | 2473 | cairo_mask (cairo_shape, pattern); | ||
2488 | 2474 | } | ||
2489 | 2475 | cairo_restore (cairo_shape); | ||
2490 | 2476 | } | ||
2491 | 2477 | |||
2492 | 2478 | cairo_pattern_destroy (maskPattern); | ||
2493 | 2479 | cairo_pattern_destroy (pattern); | ||
2494 | 2480 | |||
2495 | 2481 | cleanup: | ||
2496 | 2482 | imgStr->close(); | ||
2497 | 2483 | delete imgStr; | ||
2498 | 2484 | } | ||
2499 | 2485 | |||
2500 | 2486 | |||
2501 | 2487 | //XXX: is this affect by AIS(alpha is shape)? | ||
2502 | 2488 | void CairoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str, | ||
2503 | 2489 | int width, int height, | ||
2504 | 2490 | GfxImageColorMap *colorMap, | ||
2505 | 2491 | GBool interpolate, | ||
2506 | 2492 | Stream *maskStr, | ||
2507 | 2493 | int maskWidth, int maskHeight, | ||
2508 | 2494 | GfxImageColorMap *maskColorMap, | ||
2509 | 2495 | GBool maskInterpolate) | ||
2510 | 2496 | { | ||
2511 | 2497 | ImageStream *maskImgStr, *imgStr; | ||
2512 | 2498 | int row_stride; | ||
2513 | 2499 | unsigned char *maskBuffer, *buffer; | ||
2514 | 2500 | unsigned char *maskDest; | ||
2515 | 2501 | unsigned int *dest; | ||
2516 | 2502 | cairo_surface_t *maskImage, *image; | ||
2517 | 2503 | cairo_pattern_t *maskPattern, *pattern; | ||
2518 | 2504 | cairo_matrix_t maskMatrix, matrix; | ||
2519 | 2505 | Guchar *pix; | ||
2520 | 2506 | int y; | ||
2521 | 2507 | cairo_filter_t filter; | ||
2522 | 2508 | cairo_filter_t maskFilter; | ||
2523 | 2509 | |||
2524 | 2510 | maskImgStr = new ImageStream(maskStr, maskWidth, | ||
2525 | 2511 | maskColorMap->getNumPixelComps(), | ||
2526 | 2512 | maskColorMap->getBits()); | ||
2527 | 2513 | maskImgStr->reset(); | ||
2528 | 2514 | |||
2529 | 2515 | maskImage = cairo_image_surface_create (CAIRO_FORMAT_A8, maskWidth, maskHeight); | ||
2530 | 2516 | if (cairo_surface_status (maskImage)) { | ||
2531 | 2517 | maskImgStr->close(); | ||
2532 | 2518 | delete maskImgStr; | ||
2533 | 2519 | return; | ||
2534 | 2520 | } | ||
2535 | 2521 | |||
2536 | 2522 | maskBuffer = cairo_image_surface_get_data (maskImage); | ||
2537 | 2523 | row_stride = cairo_image_surface_get_stride (maskImage); | ||
2538 | 2524 | for (y = 0; y < maskHeight; y++) { | ||
2539 | 2525 | maskDest = (unsigned char *) (maskBuffer + y * row_stride); | ||
2540 | 2526 | pix = maskImgStr->getLine(); | ||
2541 | 2527 | maskColorMap->getGrayLine (pix, maskDest, maskWidth); | ||
2542 | 2528 | } | ||
2543 | 2529 | |||
2544 | 2530 | maskImgStr->close(); | ||
2545 | 2531 | delete maskImgStr; | ||
2546 | 2532 | |||
2547 | 2533 | maskFilter = getFilterForSurface (maskImage, maskInterpolate); | ||
2548 | 2534 | |||
2549 | 2535 | cairo_surface_mark_dirty (maskImage); | ||
2550 | 2536 | maskPattern = cairo_pattern_create_for_surface (maskImage); | ||
2551 | 2537 | cairo_surface_destroy (maskImage); | ||
2552 | 2538 | if (cairo_pattern_status (maskPattern)) | ||
2553 | 2539 | return; | ||
2554 | 2540 | |||
2555 | 2541 | #if 0 | ||
2556 | 2542 | /* ICCBased color space doesn't do any color correction | ||
2557 | 2543 | * so check its underlying color space as well */ | ||
2558 | 2544 | int is_identity_transform; | ||
2559 | 2545 | is_identity_transform = colorMap->getColorSpace()->getMode() == csDeviceRGB || | ||
2560 | 2546 | (colorMap->getColorSpace()->getMode() == csICCBased && | ||
2561 | 2547 | ((GfxICCBasedColorSpace*)colorMap->getColorSpace())->getAlt()->getMode() == csDeviceRGB); | ||
2562 | 2548 | #endif | ||
2563 | 2549 | |||
2564 | 2550 | /* TODO: Do we want to cache these? */ | ||
2565 | 2551 | imgStr = new ImageStream(str, width, | ||
2566 | 2552 | colorMap->getNumPixelComps(), | ||
2567 | 2553 | colorMap->getBits()); | ||
2568 | 2554 | imgStr->reset(); | ||
2569 | 2555 | |||
2570 | 2556 | image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); | ||
2571 | 2557 | if (cairo_surface_status (image)) | ||
2572 | 2558 | goto cleanup; | ||
2573 | 2559 | |||
2574 | 2560 | buffer = cairo_image_surface_get_data (image); | ||
2575 | 2561 | row_stride = cairo_image_surface_get_stride (image); | ||
2576 | 2562 | for (y = 0; y < height; y++) { | ||
2577 | 2563 | dest = (unsigned int *) (buffer + y * row_stride); | ||
2578 | 2564 | pix = imgStr->getLine(); | ||
2579 | 2565 | colorMap->getRGBLine (pix, dest, width); | ||
2580 | 2566 | } | ||
2581 | 2567 | |||
2582 | 2568 | filter = getFilterForSurface (image, interpolate); | ||
2583 | 2569 | |||
2584 | 2570 | cairo_surface_mark_dirty (image); | ||
2585 | 2571 | |||
2586 | 2572 | setMimeData(str, ref, image); | ||
2587 | 2573 | |||
2588 | 2574 | pattern = cairo_pattern_create_for_surface (image); | ||
2589 | 2575 | cairo_surface_destroy (image); | ||
2590 | 2576 | if (cairo_pattern_status (pattern)) | ||
2591 | 2577 | goto cleanup; | ||
2592 | 2578 | |||
2593 | 2579 | LOG (printf ("drawSoftMaskedImage %dx%d\n", width, height)); | ||
2594 | 2580 | |||
2595 | 2581 | cairo_pattern_set_filter (pattern, filter); | ||
2596 | 2582 | cairo_pattern_set_filter (maskPattern, maskFilter); | ||
2597 | 2583 | |||
2598 | 2584 | if (!printing) { | ||
2599 | 2585 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); | ||
2600 | 2586 | cairo_pattern_set_extend (maskPattern, CAIRO_EXTEND_PAD); | ||
2601 | 2587 | } | ||
2602 | 2588 | |||
2603 | 2589 | cairo_matrix_init_translate (&matrix, 0, height); | ||
2604 | 2590 | cairo_matrix_scale (&matrix, width, -height); | ||
2605 | 2591 | cairo_pattern_set_matrix (pattern, &matrix); | ||
2606 | 2592 | if (cairo_pattern_status (pattern)) { | ||
2607 | 2593 | cairo_pattern_destroy (pattern); | ||
2608 | 2594 | cairo_pattern_destroy (maskPattern); | ||
2609 | 2595 | goto cleanup; | ||
2610 | 2596 | } | ||
2611 | 2597 | |||
2612 | 2598 | cairo_matrix_init_translate (&maskMatrix, 0, maskHeight); | ||
2613 | 2599 | cairo_matrix_scale (&maskMatrix, maskWidth, -maskHeight); | ||
2614 | 2600 | cairo_pattern_set_matrix (maskPattern, &maskMatrix); | ||
2615 | 2601 | if (cairo_pattern_status (maskPattern)) { | ||
2616 | 2602 | cairo_pattern_destroy (maskPattern); | ||
2617 | 2603 | cairo_pattern_destroy (pattern); | ||
2618 | 2604 | goto cleanup; | ||
2619 | 2605 | } | ||
2620 | 2606 | |||
2621 | 2607 | if (fill_opacity != 1.0) | ||
2622 | 2608 | cairo_push_group (cairo); | ||
2623 | 2609 | else | ||
2624 | 2610 | cairo_save (cairo); | ||
2625 | 2611 | |||
2626 | 2612 | cairo_set_source (cairo, pattern); | ||
2627 | 2613 | if (!printing) { | ||
2628 | 2614 | cairo_rectangle (cairo, 0., 0., | ||
2629 | 2615 | MIN (width, maskWidth) / (double)width, | ||
2630 | 2616 | MIN (height, maskHeight) / (double)height); | ||
2631 | 2617 | cairo_clip (cairo); | ||
2632 | 2618 | } | ||
2633 | 2619 | cairo_mask (cairo, maskPattern); | ||
2634 | 2620 | |||
2635 | 2621 | if (fill_opacity != 1.0) { | ||
2636 | 2622 | cairo_pop_group_to_source (cairo); | ||
2637 | 2623 | cairo_save (cairo); | ||
2638 | 2624 | if (!printing) { | ||
2639 | 2625 | cairo_rectangle (cairo, 0., 0., | ||
2640 | 2626 | MIN (width, maskWidth) / (double)width, | ||
2641 | 2627 | MIN (height, maskHeight) / (double)height); | ||
2642 | 2628 | cairo_clip (cairo); | ||
2643 | 2629 | } | ||
2644 | 2630 | cairo_paint_with_alpha (cairo, fill_opacity); | ||
2645 | 2631 | } | ||
2646 | 2632 | cairo_restore (cairo); | ||
2647 | 2633 | |||
2648 | 2634 | if (cairo_shape) { | ||
2649 | 2635 | cairo_save (cairo_shape); | ||
2650 | 2636 | cairo_set_source (cairo_shape, pattern); | ||
2651 | 2637 | if (!printing) { | ||
2652 | 2638 | cairo_rectangle (cairo_shape, 0., 0., | ||
2653 | 2639 | MIN (width, maskWidth) / (double)width, | ||
2654 | 2640 | MIN (height, maskHeight) / (double)height); | ||
2655 | 2641 | cairo_fill (cairo_shape); | ||
2656 | 2642 | } else { | ||
2657 | 2643 | cairo_mask (cairo_shape, pattern); | ||
2658 | 2644 | } | ||
2659 | 2645 | cairo_restore (cairo_shape); | ||
2660 | 2646 | } | ||
2661 | 2647 | |||
2662 | 2648 | cairo_pattern_destroy (maskPattern); | ||
2663 | 2649 | cairo_pattern_destroy (pattern); | ||
2664 | 2650 | |||
2665 | 2651 | cleanup: | ||
2666 | 2652 | imgStr->close(); | ||
2667 | 2653 | delete imgStr; | ||
2668 | 2654 | } | ||
2669 | 2655 | |||
2670 | 2656 | GBool CairoOutputDev::getStreamData (Stream *str, char **buffer, int *length) | ||
2671 | 2657 | { | ||
2672 | 2658 | int len, i; | ||
2673 | 2659 | char *strBuffer; | ||
2674 | 2660 | |||
2675 | 2661 | len = 0; | ||
2676 | 2662 | str->close(); | ||
2677 | 2663 | str->reset(); | ||
2678 | 2664 | while (str->getChar() != EOF) len++; | ||
2679 | 2665 | if (len == 0) | ||
2680 | 2666 | return gFalse; | ||
2681 | 2667 | |||
2682 | 2668 | strBuffer = (char *)gmalloc (len); | ||
2683 | 2669 | |||
2684 | 2670 | str->close(); | ||
2685 | 2671 | str->reset(); | ||
2686 | 2672 | for (i = 0; i < len; ++i) | ||
2687 | 2673 | strBuffer[i] = str->getChar(); | ||
2688 | 2674 | |||
2689 | 2675 | *buffer = strBuffer; | ||
2690 | 2676 | *length = len; | ||
2691 | 2677 | |||
2692 | 2678 | return gTrue; | ||
2693 | 2679 | } | ||
2694 | 2680 | |||
2695 | 2681 | void CairoOutputDev::setMimeData(Stream *str, Object *ref, cairo_surface_t *image) | ||
2696 | 2682 | { | ||
2697 | 2683 | char *strBuffer; | ||
2698 | 2684 | int len; | ||
2699 | 2685 | Object obj; | ||
2700 | 2686 | |||
2701 | 2687 | if (!printing || !(str->getKind() == strDCT || str->getKind() == strJPX)) | ||
2702 | 2688 | return; | ||
2703 | 2689 | |||
2704 | 2690 | // colorspace in stream dict may be different from colorspace in jpx | ||
2705 | 2691 | // data | ||
2706 | 2692 | if (str->getKind() == strJPX) { | ||
2707 | 2693 | GBool hasColorSpace = !str->getDict()->lookup("ColorSpace", &obj)->isNull(); | ||
2708 | 2694 | obj.free(); | ||
2709 | 2695 | if (hasColorSpace) | ||
2710 | 2696 | return; | ||
2711 | 2697 | } | ||
2712 | 2698 | |||
2713 | 2699 | if (getStreamData (str->getNextStream(), &strBuffer, &len)) { | ||
2714 | 2700 | cairo_status_t st; | ||
2715 | 2701 | |||
2716 | 2702 | #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 11, 2) | ||
2717 | 2703 | if (ref && ref->isRef()) { | ||
2718 | 2704 | Ref imgRef = ref->getRef(); | ||
2719 | 2705 | GooString *surfaceId = new GooString("poppler-surface-"); | ||
2720 | 2706 | surfaceId->appendf("{0:d}-{1:d}", imgRef.gen, imgRef.num); | ||
2721 | 2707 | char *idBuffer = copyString(surfaceId->getCString()); | ||
2722 | 2708 | st = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_UNIQUE_ID, | ||
2723 | 2709 | (const unsigned char *)idBuffer, | ||
2724 | 2710 | surfaceId->getLength(), | ||
2725 | 2711 | gfree, idBuffer); | ||
2726 | 2712 | if (st) | ||
2727 | 2713 | gfree(idBuffer); | ||
2728 | 2714 | delete surfaceId; | ||
2729 | 2715 | } | ||
2730 | 2716 | #endif | ||
2731 | 2717 | |||
2732 | 2718 | st = cairo_surface_set_mime_data (image, | ||
2733 | 2719 | str->getKind() == strDCT ? | ||
2734 | 2720 | CAIRO_MIME_TYPE_JPEG : CAIRO_MIME_TYPE_JP2, | ||
2735 | 2721 | (const unsigned char *)strBuffer, len, | ||
2736 | 2722 | gfree, strBuffer); | ||
2737 | 2723 | if (st) | ||
2738 | 2724 | gfree (strBuffer); | ||
2739 | 2725 | } | ||
2740 | 2726 | } | ||
2741 | 2727 | |||
2742 | 2728 | void CairoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, | ||
2743 | 2729 | int width, int height, | ||
2744 | 2730 | GfxImageColorMap *colorMap, | ||
2745 | 2731 | GBool interpolate, | ||
2746 | 2732 | int *maskColors, GBool inlineImg) | ||
2747 | 2733 | { | ||
2748 | 2734 | cairo_surface_t *image; | ||
2749 | 2735 | cairo_pattern_t *pattern, *maskPattern; | ||
2750 | 2736 | ImageStream *imgStr; | ||
2751 | 2737 | cairo_matrix_t matrix; | ||
2752 | 2738 | unsigned char *buffer; | ||
2753 | 2739 | int stride, i; | ||
2754 | 2740 | GfxRGB *lookup = NULL; | ||
2755 | 2741 | cairo_filter_t filter = CAIRO_FILTER_BILINEAR; | ||
2756 | 2742 | |||
2757 | 2743 | /* TODO: Do we want to cache these? */ | ||
2758 | 2744 | imgStr = new ImageStream(str, width, | ||
2759 | 2745 | colorMap->getNumPixelComps(), | ||
2760 | 2746 | colorMap->getBits()); | ||
2761 | 2747 | imgStr->reset(); | ||
2762 | 2748 | |||
2763 | 2749 | #if 0 | ||
2764 | 2750 | /* ICCBased color space doesn't do any color correction | ||
2765 | 2751 | * so check its underlying color space as well */ | ||
2766 | 2752 | int is_identity_transform; | ||
2767 | 2753 | is_identity_transform = colorMap->getColorSpace()->getMode() == csDeviceRGB || | ||
2768 | 2754 | (colorMap->getColorSpace()->getMode() == csICCBased && | ||
2769 | 2755 | ((GfxICCBasedColorSpace*)colorMap->getColorSpace())->getAlt()->getMode() == csDeviceRGB); | ||
2770 | 2756 | #endif | ||
2771 | 2757 | |||
2772 | 2758 | image = cairo_image_surface_create (maskColors ? | ||
2773 | 2759 | CAIRO_FORMAT_ARGB32 : | ||
2774 | 2760 | CAIRO_FORMAT_RGB24, | ||
2775 | 2761 | width, height); | ||
2776 | 2762 | if (cairo_surface_status (image)) | ||
2777 | 2763 | goto cleanup; | ||
2778 | 2764 | |||
2779 | 2765 | // special case for one-channel (monochrome/gray/separation) images: | ||
2780 | 2766 | // build a lookup table here | ||
2781 | 2767 | if (colorMap->getNumPixelComps() == 1) { | ||
2782 | 2768 | int n; | ||
2783 | 2769 | Guchar pix; | ||
2784 | 2770 | |||
2785 | 2771 | n = 1 << colorMap->getBits(); | ||
2786 | 2772 | lookup = (GfxRGB *)gmallocn(n, sizeof(GfxRGB)); | ||
2787 | 2773 | for (i = 0; i < n; ++i) { | ||
2788 | 2774 | pix = (Guchar)i; | ||
2789 | 2775 | |||
2790 | 2776 | colorMap->getRGB(&pix, &lookup[i]); | ||
2791 | 2777 | } | ||
2792 | 2778 | } | ||
2793 | 2779 | |||
2794 | 2780 | buffer = cairo_image_surface_get_data (image); | ||
2795 | 2781 | stride = cairo_image_surface_get_stride (image); | ||
2796 | 2782 | for (int y = 0; y < height; y++) { | ||
2797 | 2783 | uint32_t *dest = (uint32_t *) (buffer + y * stride); | ||
2798 | 2784 | Guchar *pix = imgStr->getLine(); | ||
2799 | 2785 | |||
2800 | 2786 | if (lookup) { | ||
2801 | 2787 | Guchar *p = pix; | ||
2802 | 2788 | GfxRGB rgb; | ||
2803 | 2789 | |||
2804 | 2790 | for (i = 0; i < width; i++) { | ||
2805 | 2791 | rgb = lookup[*p]; | ||
2806 | 2792 | dest[i] = | ||
2807 | 2793 | ((int) colToByte(rgb.r) << 16) | | ||
2808 | 2794 | ((int) colToByte(rgb.g) << 8) | | ||
2809 | 2795 | ((int) colToByte(rgb.b) << 0); | ||
2810 | 2796 | p++; | ||
2811 | 2797 | } | ||
2812 | 2798 | } else { | ||
2813 | 2799 | colorMap->getRGBLine (pix, dest, width); | ||
2814 | 2800 | } | ||
2815 | 2801 | |||
2816 | 2802 | if (maskColors) { | ||
2817 | 2803 | for (int x = 0; x < width; x++) { | ||
2818 | 2804 | bool is_opaque = false; | ||
2819 | 2805 | for (int i = 0; i < colorMap->getNumPixelComps(); ++i) { | ||
2820 | 2806 | if (pix[i] < maskColors[2*i] || | ||
2821 | 2807 | pix[i] > maskColors[2*i+1]) { | ||
2822 | 2808 | is_opaque = true; | ||
2823 | 2809 | break; | ||
2824 | 2810 | } | ||
2825 | 2811 | } | ||
2826 | 2812 | if (is_opaque) | ||
2827 | 2813 | *dest |= 0xff000000; | ||
2828 | 2814 | else | ||
2829 | 2815 | *dest = 0; | ||
2830 | 2816 | dest++; | ||
2831 | 2817 | pix += colorMap->getNumPixelComps(); | ||
2832 | 2818 | } | ||
2833 | 2819 | } | ||
2834 | 2820 | } | ||
2835 | 2821 | gfree(lookup); | ||
2836 | 2822 | |||
2837 | 2823 | LOG (printf ("drawImage %dx%d\n", width, height)); | ||
2838 | 2824 | |||
2839 | 2825 | cairo_surface_t *scaled_surface; | ||
2840 | 2826 | |||
2841 | 2827 | scaled_surface = downscaleSurface (image); | ||
2842 | 2828 | if (scaled_surface) { | ||
2843 | 2829 | if (cairo_surface_status (scaled_surface)) | ||
2844 | 2830 | goto cleanup; | ||
2845 | 2831 | cairo_surface_destroy (image); | ||
2846 | 2832 | image = scaled_surface; | ||
2847 | 2833 | width = cairo_image_surface_get_width (image); | ||
2848 | 2834 | height = cairo_image_surface_get_height (image); | ||
2849 | 2835 | } else { | ||
2850 | 2836 | filter = getFilterForSurface (image, interpolate); | ||
2851 | 2837 | } | ||
2852 | 2838 | |||
2853 | 2839 | cairo_surface_mark_dirty (image); | ||
2854 | 2840 | |||
2855 | 2841 | if (!inlineImg) /* don't read stream twice if it is an inline image */ | ||
2856 | 2842 | setMimeData(str, ref, image); | ||
2857 | 2843 | |||
2858 | 2844 | pattern = cairo_pattern_create_for_surface (image); | ||
2859 | 2845 | cairo_surface_destroy (image); | ||
2860 | 2846 | if (cairo_pattern_status (pattern)) | ||
2861 | 2847 | goto cleanup; | ||
2862 | 2848 | |||
2863 | 2849 | cairo_pattern_set_filter (pattern, filter); | ||
2864 | 2850 | |||
2865 | 2851 | if (!printing) | ||
2866 | 2852 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); | ||
2867 | 2853 | |||
2868 | 2854 | cairo_matrix_init_translate (&matrix, 0, height); | ||
2869 | 2855 | cairo_matrix_scale (&matrix, width, -height); | ||
2870 | 2856 | cairo_pattern_set_matrix (pattern, &matrix); | ||
2871 | 2857 | if (cairo_pattern_status (pattern)) { | ||
2872 | 2858 | cairo_pattern_destroy (pattern); | ||
2873 | 2859 | goto cleanup; | ||
2874 | 2860 | } | ||
2875 | 2861 | |||
2876 | 2862 | if (!mask && fill_opacity != 1.0) { | ||
2877 | 2863 | maskPattern = cairo_pattern_create_rgba (1., 1., 1., fill_opacity); | ||
2878 | 2864 | } else if (mask) { | ||
2879 | 2865 | maskPattern = cairo_pattern_reference (mask); | ||
2880 | 2866 | } else { | ||
2881 | 2867 | maskPattern = NULL; | ||
2882 | 2868 | } | ||
2883 | 2869 | |||
2884 | 2870 | cairo_save (cairo); | ||
2885 | 2871 | cairo_set_source (cairo, pattern); | ||
2886 | 2872 | if (!printing) | ||
2887 | 2873 | cairo_rectangle (cairo, 0., 0., 1., 1.); | ||
2888 | 2874 | if (maskPattern) { | ||
2889 | 2875 | if (!printing) | ||
2890 | 2876 | cairo_clip (cairo); | ||
2891 | 2877 | cairo_set_matrix (cairo, &mask_matrix); | ||
2892 | 2878 | cairo_mask (cairo, maskPattern); | ||
2893 | 2879 | } else { | ||
2894 | 2880 | if (printing) | ||
2895 | 2881 | cairo_paint (cairo); | ||
2896 | 2882 | else | ||
2897 | 2883 | cairo_fill (cairo); | ||
2898 | 2884 | } | ||
2899 | 2885 | cairo_restore (cairo); | ||
2900 | 2886 | |||
2901 | 2887 | cairo_pattern_destroy (maskPattern); | ||
2902 | 2888 | |||
2903 | 2889 | if (cairo_shape) { | ||
2904 | 2890 | cairo_save (cairo_shape); | ||
2905 | 2891 | cairo_set_source (cairo_shape, pattern); | ||
2906 | 2892 | if (printing) { | ||
2907 | 2893 | cairo_paint (cairo_shape); | ||
2908 | 2894 | } else { | ||
2909 | 2895 | cairo_rectangle (cairo_shape, 0., 0., 1., 1.); | ||
2910 | 2896 | cairo_fill (cairo_shape); | ||
2911 | 2897 | } | ||
2912 | 2898 | cairo_restore (cairo_shape); | ||
2913 | 2899 | } | ||
2914 | 2900 | |||
2915 | 2901 | cairo_pattern_destroy (pattern); | ||
2916 | 2902 | |||
2917 | 2903 | cleanup: | ||
2918 | 2904 | imgStr->close(); | ||
2919 | 2905 | delete imgStr; | ||
2920 | 2906 | } | ||
2921 | 2907 | |||
2922 | 2908 | |||
2923 | 2909 | //------------------------------------------------------------------------ | ||
2924 | 2910 | // ImageOutputDev | ||
2925 | 2911 | //------------------------------------------------------------------------ | ||
2926 | 2912 | |||
2927 | 2913 | CairoImageOutputDev::CairoImageOutputDev() | ||
2928 | 2914 | { | ||
2929 | 2915 | images = NULL; | ||
2930 | 2916 | numImages = 0; | ||
2931 | 2917 | size = 0; | ||
2932 | 2918 | imgDrawCbk = NULL; | ||
2933 | 2919 | imgDrawCbkData = NULL; | ||
2934 | 2920 | } | ||
2935 | 2921 | |||
2936 | 2922 | CairoImageOutputDev::~CairoImageOutputDev() | ||
2937 | 2923 | { | ||
2938 | 2924 | int i; | ||
2939 | 2925 | |||
2940 | 2926 | for (i = 0; i < numImages; i++) | ||
2941 | 2927 | delete images[i]; | ||
2942 | 2928 | gfree (images); | ||
2943 | 2929 | } | ||
2944 | 2930 | |||
2945 | 2931 | void CairoImageOutputDev::saveImage(CairoImage *image) | ||
2946 | 2932 | { | ||
2947 | 2933 | if (numImages >= size) { | ||
2948 | 2934 | size += 16; | ||
2949 | 2935 | images = (CairoImage **) greallocn (images, size, sizeof (CairoImage *)); | ||
2950 | 2936 | } | ||
2951 | 2937 | images[numImages++] = image; | ||
2952 | 2938 | } | ||
2953 | 2939 | |||
2954 | 2940 | void CairoImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, | ||
2955 | 2941 | int width, int height, GBool invert, | ||
2956 | 2942 | GBool interpolate, GBool inlineImg) | ||
2957 | 2943 | { | ||
2958 | 2944 | cairo_t *cr; | ||
2959 | 2945 | cairo_surface_t *surface; | ||
2960 | 2946 | double x1, y1, x2, y2; | ||
2961 | 2947 | double *ctm; | ||
2962 | 2948 | double mat[6]; | ||
2963 | 2949 | CairoImage *image; | ||
2964 | 2950 | |||
2965 | 2951 | ctm = state->getCTM(); | ||
2966 | 2952 | |||
2967 | 2953 | mat[0] = ctm[0]; | ||
2968 | 2954 | mat[1] = ctm[1]; | ||
2969 | 2955 | mat[2] = -ctm[2]; | ||
2970 | 2956 | mat[3] = -ctm[3]; | ||
2971 | 2957 | mat[4] = ctm[2] + ctm[4]; | ||
2972 | 2958 | mat[5] = ctm[3] + ctm[5]; | ||
2973 | 2959 | x1 = mat[4]; | ||
2974 | 2960 | y1 = mat[5]; | ||
2975 | 2961 | x2 = x1 + width; | ||
2976 | 2962 | y2 = y1 + height; | ||
2977 | 2963 | |||
2978 | 2964 | image = new CairoImage (x1, y1, x2, y2); | ||
2979 | 2965 | saveImage (image); | ||
2980 | 2966 | |||
2981 | 2967 | if (imgDrawCbk && imgDrawCbk (numImages - 1, imgDrawCbkData)) { | ||
2982 | 2968 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); | ||
2983 | 2969 | cr = cairo_create (surface); | ||
2984 | 2970 | setCairo (cr); | ||
2985 | 2971 | cairo_translate (cr, 0, height); | ||
2986 | 2972 | cairo_scale (cr, width, -height); | ||
2987 | 2973 | |||
2988 | 2974 | CairoOutputDev::drawImageMask(state, ref, str, width, height, invert, interpolate, inlineImg); | ||
2989 | 2975 | image->setImage (surface); | ||
2990 | 2976 | |||
2991 | 2977 | setCairo (NULL); | ||
2992 | 2978 | cairo_surface_destroy (surface); | ||
2993 | 2979 | cairo_destroy (cr); | ||
2994 | 2980 | } | ||
2995 | 2981 | } | ||
2996 | 2982 | |||
2997 | 2983 | void CairoImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, | ||
2998 | 2984 | int width, int height, GfxImageColorMap *colorMap, | ||
2999 | 2985 | GBool interpolate, int *maskColors, GBool inlineImg) | ||
3000 | 2986 | { | ||
3001 | 2987 | cairo_t *cr; | ||
3002 | 2988 | cairo_surface_t *surface; | ||
3003 | 2989 | double x1, y1, x2, y2; | ||
3004 | 2990 | double *ctm; | ||
3005 | 2991 | double mat[6]; | ||
3006 | 2992 | CairoImage *image; | ||
3007 | 2993 | |||
3008 | 2994 | ctm = state->getCTM(); | ||
3009 | 2995 | |||
3010 | 2996 | mat[0] = ctm[0]; | ||
3011 | 2997 | mat[1] = ctm[1]; | ||
3012 | 2998 | mat[2] = -ctm[2]; | ||
3013 | 2999 | mat[3] = -ctm[3]; | ||
3014 | 3000 | mat[4] = ctm[2] + ctm[4]; | ||
3015 | 3001 | mat[5] = ctm[3] + ctm[5]; | ||
3016 | 3002 | x1 = mat[4]; | ||
3017 | 3003 | y1 = mat[5]; | ||
3018 | 3004 | x2 = x1 + width; | ||
3019 | 3005 | y2 = y1 + height; | ||
3020 | 3006 | |||
3021 | 3007 | image = new CairoImage (x1, y1, x2, y2); | ||
3022 | 3008 | saveImage (image); | ||
3023 | 3009 | |||
3024 | 3010 | if (imgDrawCbk && imgDrawCbk (numImages - 1, imgDrawCbkData)) { | ||
3025 | 3011 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); | ||
3026 | 3012 | cr = cairo_create (surface); | ||
3027 | 3013 | setCairo (cr); | ||
3028 | 3014 | cairo_translate (cr, 0, height); | ||
3029 | 3015 | cairo_scale (cr, width, -height); | ||
3030 | 3016 | |||
3031 | 3017 | CairoOutputDev::drawImage(state, ref, str, width, height, colorMap, interpolate, maskColors, inlineImg); | ||
3032 | 3018 | image->setImage (surface); | ||
3033 | 3019 | |||
3034 | 3020 | setCairo (NULL); | ||
3035 | 3021 | cairo_surface_destroy (surface); | ||
3036 | 3022 | cairo_destroy (cr); | ||
3037 | 3023 | } | ||
3038 | 3024 | } | ||
3039 | 3025 | |||
3040 | 3026 | void CairoImageOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str, | ||
3041 | 3027 | int width, int height, | ||
3042 | 3028 | GfxImageColorMap *colorMap, | ||
3043 | 3029 | GBool interpolate, | ||
3044 | 3030 | Stream *maskStr, | ||
3045 | 3031 | int maskWidth, int maskHeight, | ||
3046 | 3032 | GfxImageColorMap *maskColorMap, | ||
3047 | 3033 | GBool maskInterpolate) | ||
3048 | 3034 | { | ||
3049 | 3035 | cairo_t *cr; | ||
3050 | 3036 | cairo_surface_t *surface; | ||
3051 | 3037 | double x1, y1, x2, y2; | ||
3052 | 3038 | double *ctm; | ||
3053 | 3039 | double mat[6]; | ||
3054 | 3040 | CairoImage *image; | ||
3055 | 3041 | |||
3056 | 3042 | ctm = state->getCTM(); | ||
3057 | 3043 | |||
3058 | 3044 | mat[0] = ctm[0]; | ||
3059 | 3045 | mat[1] = ctm[1]; | ||
3060 | 3046 | mat[2] = -ctm[2]; | ||
3061 | 3047 | mat[3] = -ctm[3]; | ||
3062 | 3048 | mat[4] = ctm[2] + ctm[4]; | ||
3063 | 3049 | mat[5] = ctm[3] + ctm[5]; | ||
3064 | 3050 | x1 = mat[4]; | ||
3065 | 3051 | y1 = mat[5]; | ||
3066 | 3052 | x2 = x1 + width; | ||
3067 | 3053 | y2 = y1 + height; | ||
3068 | 3054 | |||
3069 | 3055 | image = new CairoImage (x1, y1, x2, y2); | ||
3070 | 3056 | saveImage (image); | ||
3071 | 3057 | |||
3072 | 3058 | if (imgDrawCbk && imgDrawCbk (numImages - 1, imgDrawCbkData)) { | ||
3073 | 3059 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); | ||
3074 | 3060 | cr = cairo_create (surface); | ||
3075 | 3061 | setCairo (cr); | ||
3076 | 3062 | cairo_translate (cr, 0, height); | ||
3077 | 3063 | cairo_scale (cr, width, -height); | ||
3078 | 3064 | |||
3079 | 3065 | CairoOutputDev::drawSoftMaskedImage(state, ref, str, width, height, colorMap, interpolate, | ||
3080 | 3066 | maskStr, maskWidth, maskHeight, maskColorMap, maskInterpolate); | ||
3081 | 3067 | image->setImage (surface); | ||
3082 | 3068 | |||
3083 | 3069 | setCairo (NULL); | ||
3084 | 3070 | cairo_surface_destroy (surface); | ||
3085 | 3071 | cairo_destroy (cr); | ||
3086 | 3072 | } | ||
3087 | 3073 | } | ||
3088 | 3074 | |||
3089 | 3075 | void CairoImageOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str, | ||
3090 | 3076 | int width, int height, | ||
3091 | 3077 | GfxImageColorMap *colorMap, | ||
3092 | 3078 | GBool interpolate, | ||
3093 | 3079 | Stream *maskStr, | ||
3094 | 3080 | int maskWidth, int maskHeight, | ||
3095 | 3081 | GBool maskInvert, GBool maskInterpolate) | ||
3096 | 3082 | { | ||
3097 | 3083 | cairo_t *cr; | ||
3098 | 3084 | cairo_surface_t *surface; | ||
3099 | 3085 | double x1, y1, x2, y2; | ||
3100 | 3086 | double *ctm; | ||
3101 | 3087 | double mat[6]; | ||
3102 | 3088 | CairoImage *image; | ||
3103 | 3089 | |||
3104 | 3090 | ctm = state->getCTM(); | ||
3105 | 3091 | |||
3106 | 3092 | mat[0] = ctm[0]; | ||
3107 | 3093 | mat[1] = ctm[1]; | ||
3108 | 3094 | mat[2] = -ctm[2]; | ||
3109 | 3095 | mat[3] = -ctm[3]; | ||
3110 | 3096 | mat[4] = ctm[2] + ctm[4]; | ||
3111 | 3097 | mat[5] = ctm[3] + ctm[5]; | ||
3112 | 3098 | x1 = mat[4]; | ||
3113 | 3099 | y1 = mat[5]; | ||
3114 | 3100 | x2 = x1 + width; | ||
3115 | 3101 | y2 = y1 + height; | ||
3116 | 3102 | |||
3117 | 3103 | image = new CairoImage (x1, y1, x2, y2); | ||
3118 | 3104 | saveImage (image); | ||
3119 | 3105 | |||
3120 | 3106 | if (imgDrawCbk && imgDrawCbk (numImages - 1, imgDrawCbkData)) { | ||
3121 | 3107 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); | ||
3122 | 3108 | cr = cairo_create (surface); | ||
3123 | 3109 | setCairo (cr); | ||
3124 | 3110 | cairo_translate (cr, 0, height); | ||
3125 | 3111 | cairo_scale (cr, width, -height); | ||
3126 | 3112 | |||
3127 | 3113 | CairoOutputDev::drawMaskedImage(state, ref, str, width, height, colorMap, interpolate, | ||
3128 | 3114 | maskStr, maskWidth, maskHeight, maskInvert, maskInterpolate); | ||
3129 | 3115 | image->setImage (surface); | ||
3130 | 3116 | |||
3131 | 3117 | setCairo (NULL); | ||
3132 | 3118 | cairo_surface_destroy (surface); | ||
3133 | 3119 | cairo_destroy (cr); | ||
3134 | 3120 | } | ||
3135 | 3121 | } | ||
3136 | 0 | 3122 | ||
3137 | === modified file 'debian/changelog' | |||
3138 | --- debian/changelog 2012-09-12 10:53:15 +0000 | |||
3139 | +++ debian/changelog 2012-11-13 19:26:27 +0000 | |||
3140 | @@ -1,3 +1,11 @@ | |||
3141 | 1 | poppler (0.20.4-0ubuntu2) raring; urgency=low | ||
3142 | 2 | |||
3143 | 3 | * debian/patches/git_gouraud_shading_support.patch: (LP: #1072129) | ||
3144 | 4 | - Evince crashes after opening certain PDF file because parameterized | ||
3145 | 5 | Gouraud shading is not supported. | ||
3146 | 6 | |||
3147 | 7 | -- Matthieu Baerts (matttbe) <matttbe@ubuntu.com> Sat, 10 Nov 2012 16:51:12 +0100 | ||
3148 | 8 | |||
3149 | 1 | poppler (0.20.4-0ubuntu1) quantal; urgency=low | 9 | poppler (0.20.4-0ubuntu1) quantal; urgency=low |
3150 | 2 | 10 | ||
3151 | 3 | * New upstream bugfix release | 11 | * New upstream bugfix release |
3152 | 4 | 12 | ||
3153 | === added file 'debian/patches/git_gouraud_shading_support.patch' | |||
3154 | --- debian/patches/git_gouraud_shading_support.patch 1970-01-01 00:00:00 +0000 | |||
3155 | +++ debian/patches/git_gouraud_shading_support.patch 2012-11-13 19:26:27 +0000 | |||
3156 | @@ -0,0 +1,38 @@ | |||
3157 | 1 | From ae8fc0cbfc6123189e17b3cf1286e0540f181646 Mon Sep 17 00:00:00 2001 | ||
3158 | 2 | From: Adrian Johnson <ajohnson@redneon.com> | ||
3159 | 3 | Date: Tue, 30 Oct 2012 10:52:04 +0000 | ||
3160 | 4 | Subject: cairo: support parameterized Gouraud shading | ||
3161 | 5 | Bug: https://bugs.freedesktop.org/show_bug.cgi?id=56463 | ||
3162 | 6 | Bug-Ubuntu: https://bugs.launchpad.net/poppler/+bug/1072129 | ||
3163 | 7 | |||
3164 | 8 | --- | ||
3165 | 9 | Index: poppler/poppler/CairoOutputDev.cc | ||
3166 | 10 | =================================================================== | ||
3167 | 11 | --- poppler.orig/poppler/CairoOutputDev.cc 2012-11-10 16:49:31.178020000 +0100 | ||
3168 | 12 | +++ poppler/poppler/CairoOutputDev.cc 2012-11-10 16:50:44.513164938 +0100 | ||
3169 | 13 | @@ -945,10 +945,21 @@ | ||
3170 | 14 | fill_pattern = cairo_pattern_create_mesh (); | ||
3171 | 15 | |||
3172 | 16 | for (i = 0; i < shading->getNTriangles(); i++) { | ||
3173 | 17 | - shading->getTriangle(i, | ||
3174 | 18 | - &x0, &y0, &color[0], | ||
3175 | 19 | - &x1, &y1, &color[1], | ||
3176 | 20 | - &x2, &y2, &color[2]); | ||
3177 | 21 | + if (shading->isParameterized()) { | ||
3178 | 22 | + double color0, color1, color2; | ||
3179 | 23 | + shading->getTriangle(i, &x0, &y0, &color0, | ||
3180 | 24 | + &x1, &y1, &color1, | ||
3181 | 25 | + &x2, &y2, &color2); | ||
3182 | 26 | + shading->getParameterizedColor(color0, &color[0]); | ||
3183 | 27 | + shading->getParameterizedColor(color1, &color[1]); | ||
3184 | 28 | + shading->getParameterizedColor(color2, &color[2]); | ||
3185 | 29 | + } else { | ||
3186 | 30 | + shading->getTriangle(i, | ||
3187 | 31 | + &x0, &y0, &color[0], | ||
3188 | 32 | + &x1, &y1, &color[1], | ||
3189 | 33 | + &x2, &y2, &color[2]); | ||
3190 | 34 | + | ||
3191 | 35 | + } | ||
3192 | 36 | |||
3193 | 37 | cairo_mesh_pattern_begin_patch (fill_pattern); | ||
3194 | 38 | |||
3195 | 0 | 39 | ||
3196 | === modified file 'debian/patches/series' | |||
3197 | --- debian/patches/series 2012-08-14 13:11:05 +0000 | |||
3198 | +++ debian/patches/series 2012-11-13 19:26:27 +0000 | |||
3199 | @@ -1,2 +1,3 @@ | |||
3200 | 1 | ltmain-as-needed.diff | 1 | ltmain-as-needed.diff |
3201 | 2 | qt4-visibility.diff | 2 | qt4-visibility.diff |
3202 | 3 | git_gouraud_shading_support.patch | ||
3203 | 3 | 4 | ||
3204 | === modified file 'poppler/CairoOutputDev.cc' | |||
3205 | --- poppler/CairoOutputDev.cc 2012-06-22 14:50:04 +0000 | |||
3206 | +++ poppler/CairoOutputDev.cc 2012-11-13 19:26:27 +0000 | |||
3207 | @@ -945,10 +945,21 @@ | |||
3208 | 945 | fill_pattern = cairo_pattern_create_mesh (); | 945 | fill_pattern = cairo_pattern_create_mesh (); |
3209 | 946 | 946 | ||
3210 | 947 | for (i = 0; i < shading->getNTriangles(); i++) { | 947 | for (i = 0; i < shading->getNTriangles(); i++) { |
3215 | 948 | shading->getTriangle(i, | 948 | if (shading->isParameterized()) { |
3216 | 949 | &x0, &y0, &color[0], | 949 | double color0, color1, color2; |
3217 | 950 | &x1, &y1, &color[1], | 950 | shading->getTriangle(i, &x0, &y0, &color0, |
3218 | 951 | &x2, &y2, &color[2]); | 951 | &x1, &y1, &color1, |
3219 | 952 | &x2, &y2, &color2); | ||
3220 | 953 | shading->getParameterizedColor(color0, &color[0]); | ||
3221 | 954 | shading->getParameterizedColor(color1, &color[1]); | ||
3222 | 955 | shading->getParameterizedColor(color2, &color[2]); | ||
3223 | 956 | } else { | ||
3224 | 957 | shading->getTriangle(i, | ||
3225 | 958 | &x0, &y0, &color[0], | ||
3226 | 959 | &x1, &y1, &color[1], | ||
3227 | 960 | &x2, &y2, &color[2]); | ||
3228 | 961 | |||
3229 | 962 | } | ||
3230 | 952 | 963 | ||
3231 | 953 | cairo_mesh_pattern_begin_patch (fill_pattern); | 964 | cairo_mesh_pattern_begin_patch (fill_pattern); |
3232 | 954 | 965 |