Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/pdf/pdf-device.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "mupdf/fitz.h" | |
| 24 #include "mupdf/pdf.h" | |
| 25 | |
| 26 #include <ft2build.h> | |
| 27 #include FT_FREETYPE_H | |
| 28 #include FT_ADVANCES_H | |
| 29 | |
| 30 #define ALLOWED_TEXT_POS_ERROR (0.001f) | |
| 31 | |
| 32 #define ENC_IDENTITY 0 | |
| 33 #define ENC_UNICODE 1 | |
| 34 | |
| 35 typedef struct pdf_device pdf_device; | |
| 36 | |
| 37 typedef struct | |
| 38 { | |
| 39 /* The first few entries aren't really graphics state things, but | |
| 40 * they are recorded here as they are fundamentally intertwined with | |
| 41 * the push/pulling of the gstates. */ | |
| 42 fz_buffer *buf; | |
| 43 void (*on_pop)(fz_context*,pdf_device*,void *); | |
| 44 void *on_pop_arg; | |
| 45 | |
| 46 /* The graphics state proper */ | |
| 47 fz_matrix ctm; | |
| 48 fz_colorspace *colorspace[2]; | |
| 49 float color[2][4]; | |
| 50 float alpha[2]; | |
| 51 fz_stroke_state *stroke_state; | |
| 52 int font; | |
| 53 float font_size; | |
| 54 int text_rendering_mode; | |
| 55 int knockout; | |
| 56 } gstate; | |
| 57 | |
| 58 /* The image digest information, object reference, as well as indirect reference | |
| 59 * ID are all stored in doc->resources->image, and so they are maintained | |
| 60 * through the life of the document not just this page level device. As we | |
| 61 * encounter images on a page, we will add to the hash table if they are not | |
| 62 * already present. When we have an image on a particular page, the resource | |
| 63 * dict will be updated with the proper indirect reference across the document. | |
| 64 * We do need to maintain some information as to what image resources we have | |
| 65 * already specified for this page which is the purpose of image_indices | |
| 66 */ | |
| 67 | |
| 68 typedef struct | |
| 69 { | |
| 70 float alpha; | |
| 71 int stroke; | |
| 72 } alpha_entry; | |
| 73 | |
| 74 typedef struct | |
| 75 { | |
| 76 int alpha; | |
| 77 int isolated; | |
| 78 int knockout; | |
| 79 fz_colorspace *colorspace; | |
| 80 pdf_obj *ref; | |
| 81 } group_entry; | |
| 82 | |
| 83 struct pdf_device | |
| 84 { | |
| 85 fz_device super; | |
| 86 | |
| 87 pdf_document *doc; | |
| 88 pdf_obj *resources; | |
| 89 | |
| 90 int in_text; | |
| 91 | |
| 92 int num_forms; | |
| 93 int num_smasks; | |
| 94 | |
| 95 int num_gstates; | |
| 96 int max_gstates; | |
| 97 gstate *gstates; | |
| 98 | |
| 99 int num_imgs; | |
| 100 int max_imgs; | |
| 101 int *image_indices; | |
| 102 | |
| 103 int num_cid_fonts; | |
| 104 int max_cid_fonts; | |
| 105 fz_font **cid_fonts; | |
| 106 int *cid_fonts_enc; | |
| 107 | |
| 108 int num_alphas; | |
| 109 int max_alphas; | |
| 110 alpha_entry *alphas; | |
| 111 | |
| 112 int num_groups; | |
| 113 int max_groups; | |
| 114 group_entry *groups; | |
| 115 }; | |
| 116 | |
| 117 #define CURRENT_GSTATE(pdev) (&(pdev)->gstates[(pdev)->num_gstates-1]) | |
| 118 | |
| 119 /* Helper functions */ | |
| 120 static void | |
| 121 pdf_dev_stroke_state(fz_context *ctx, pdf_device *pdev, const fz_stroke_state *stroke_state) | |
| 122 { | |
| 123 gstate *gs = CURRENT_GSTATE(pdev); | |
| 124 | |
| 125 if (stroke_state == gs->stroke_state) | |
| 126 return; | |
| 127 if (gs->stroke_state && !memcmp(stroke_state, gs->stroke_state, sizeof(*stroke_state))) | |
| 128 return; | |
| 129 if (!gs->stroke_state || gs->stroke_state->linewidth != stroke_state->linewidth) | |
| 130 { | |
| 131 fz_append_printf(ctx, gs->buf, "%g w\n", stroke_state->linewidth); | |
| 132 } | |
| 133 if (!gs->stroke_state || gs->stroke_state->start_cap != stroke_state->start_cap) | |
| 134 { | |
| 135 int cap = stroke_state->start_cap; | |
| 136 /* FIXME: Triangle caps aren't supported in pdf */ | |
| 137 if (cap == FZ_LINECAP_TRIANGLE) | |
| 138 cap = FZ_LINECAP_BUTT; | |
| 139 fz_append_printf(ctx, gs->buf, "%d J\n", cap); | |
| 140 } | |
| 141 if (!gs->stroke_state || gs->stroke_state->linejoin != stroke_state->linejoin) | |
| 142 { | |
| 143 int join = stroke_state->linejoin; | |
| 144 if (join == FZ_LINEJOIN_MITER_XPS) | |
| 145 join = FZ_LINEJOIN_MITER; | |
| 146 fz_append_printf(ctx, gs->buf, "%d j\n", join); | |
| 147 } | |
| 148 if (!gs->stroke_state || gs->stroke_state->miterlimit != stroke_state->miterlimit) | |
| 149 { | |
| 150 fz_append_printf(ctx, gs->buf, "%g M\n", stroke_state->miterlimit); | |
| 151 } | |
| 152 if (gs->stroke_state == NULL && stroke_state->dash_len == 0) | |
| 153 { | |
| 154 /* No stroke details. */ | |
| 155 } | |
| 156 else if (!gs->stroke_state || gs->stroke_state->dash_phase != stroke_state->dash_phase || gs->stroke_state->dash_len != stroke_state->dash_len || | |
| 157 memcmp(gs->stroke_state->dash_list, stroke_state->dash_list, sizeof(float)*stroke_state->dash_len)) | |
| 158 { | |
| 159 int i; | |
| 160 fz_append_byte(ctx, gs->buf, '['); | |
| 161 for (i = 0; i < stroke_state->dash_len; i++) | |
| 162 { | |
| 163 if (i > 0) | |
| 164 fz_append_byte(ctx, gs->buf, ' '); | |
| 165 fz_append_printf(ctx, gs->buf, "%g", stroke_state->dash_list[i]); | |
| 166 } | |
| 167 fz_append_printf(ctx, gs->buf, "]%g d\n", stroke_state->dash_phase); | |
| 168 } | |
| 169 fz_drop_stroke_state(ctx, gs->stroke_state); | |
| 170 gs->stroke_state = fz_keep_stroke_state(ctx, stroke_state); | |
| 171 } | |
| 172 | |
| 173 typedef struct | |
| 174 { | |
| 175 fz_context *ctx; | |
| 176 fz_buffer *buf; | |
| 177 } pdf_dev_path_arg; | |
| 178 | |
| 179 static void | |
| 180 pdf_dev_path_moveto(fz_context *ctx, void *arg, float x, float y) | |
| 181 { | |
| 182 fz_buffer *buf = (fz_buffer *)arg; | |
| 183 fz_append_printf(ctx, buf, "%g %g m\n", x, y); | |
| 184 } | |
| 185 | |
| 186 static void | |
| 187 pdf_dev_path_lineto(fz_context *ctx, void *arg, float x, float y) | |
| 188 { | |
| 189 fz_buffer *buf = (fz_buffer *)arg; | |
| 190 fz_append_printf(ctx, buf, "%g %g l\n", x, y); | |
| 191 } | |
| 192 | |
| 193 static void | |
| 194 pdf_dev_path_curveto(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3) | |
| 195 { | |
| 196 fz_buffer *buf = (fz_buffer *)arg; | |
| 197 fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3); | |
| 198 } | |
| 199 | |
| 200 static void | |
| 201 pdf_dev_path_close(fz_context *ctx, void *arg) | |
| 202 { | |
| 203 fz_buffer *buf = (fz_buffer *)arg; | |
| 204 fz_append_string(ctx, buf, "h\n"); | |
| 205 } | |
| 206 | |
| 207 static const fz_path_walker pdf_dev_path_proc = | |
| 208 { | |
| 209 pdf_dev_path_moveto, | |
| 210 pdf_dev_path_lineto, | |
| 211 pdf_dev_path_curveto, | |
| 212 pdf_dev_path_close | |
| 213 }; | |
| 214 | |
| 215 static void | |
| 216 pdf_dev_path(fz_context *ctx, pdf_device *pdev, const fz_path *path) | |
| 217 { | |
| 218 gstate *gs = CURRENT_GSTATE(pdev); | |
| 219 fz_rect bounds; | |
| 220 | |
| 221 if (fz_path_is_rect_with_bounds(ctx, path, fz_identity, &bounds)) | |
| 222 { | |
| 223 fz_append_printf(ctx, gs->buf, "%g %g %g %g re\n", bounds.x0, bounds.y0, bounds.x1-bounds.x0, bounds.y1-bounds.y0); | |
| 224 return; | |
| 225 } | |
| 226 | |
| 227 fz_walk_path(ctx, path, &pdf_dev_path_proc, (void *)gs->buf); | |
| 228 } | |
| 229 | |
| 230 static void | |
| 231 pdf_dev_ctm(fz_context *ctx, pdf_device *pdev, fz_matrix ctm) | |
| 232 { | |
| 233 fz_matrix inverse; | |
| 234 gstate *gs = CURRENT_GSTATE(pdev); | |
| 235 | |
| 236 if (memcmp(&gs->ctm, &ctm, sizeof(ctm)) == 0) | |
| 237 return; | |
| 238 inverse = fz_invert_matrix(gs->ctm); | |
| 239 inverse = fz_concat(ctm, inverse); | |
| 240 gs->ctm = ctm; | |
| 241 fz_append_printf(ctx, gs->buf, "%M cm\n", &inverse); | |
| 242 } | |
| 243 | |
| 244 static void | |
| 245 pdf_dev_color(fz_context *ctx, pdf_device *pdev, fz_colorspace *colorspace, const float *color, int stroke, fz_color_params color_params) | |
| 246 { | |
| 247 int diff = 0; | |
| 248 int i; | |
| 249 int cspace = 0; | |
| 250 float rgb[FZ_MAX_COLORS]; | |
| 251 gstate *gs = CURRENT_GSTATE(pdev); | |
| 252 | |
| 253 if (colorspace == fz_device_gray(ctx)) | |
| 254 cspace = 1; | |
| 255 else if (colorspace == fz_device_rgb(ctx)) | |
| 256 cspace = 3; | |
| 257 else if (colorspace == fz_device_cmyk(ctx)) | |
| 258 cspace = 4; | |
| 259 | |
| 260 if (cspace == 0) | |
| 261 { | |
| 262 /* If it's an unknown colorspace, fallback to rgb */ | |
| 263 fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx), rgb, NULL, color_params); | |
| 264 color = rgb; | |
| 265 colorspace = fz_device_rgb(ctx); | |
| 266 cspace = 3; | |
| 267 } | |
| 268 | |
| 269 if (gs->colorspace[stroke] != colorspace) | |
| 270 { | |
| 271 gs->colorspace[stroke] = colorspace; | |
| 272 diff = 1; | |
| 273 } | |
| 274 | |
| 275 for (i=0; i < cspace; i++) | |
| 276 if (gs->color[stroke][i] != color[i]) | |
| 277 { | |
| 278 gs->color[stroke][i] = color[i]; | |
| 279 diff = 1; | |
| 280 } | |
| 281 | |
| 282 if (diff == 0) | |
| 283 return; | |
| 284 | |
| 285 switch (cspace + stroke*8) | |
| 286 { | |
| 287 case 1: | |
| 288 fz_append_printf(ctx, gs->buf, "%g g\n", color[0]); | |
| 289 break; | |
| 290 case 3: | |
| 291 fz_append_printf(ctx, gs->buf, "%g %g %g rg\n", color[0], color[1], color[2]); | |
| 292 break; | |
| 293 case 4: | |
| 294 fz_append_printf(ctx, gs->buf, "%g %g %g %g k\n", color[0], color[1], color[2], color[3]); | |
| 295 break; | |
| 296 case 1+8: | |
| 297 fz_append_printf(ctx, gs->buf, "%g G\n", color[0]); | |
| 298 break; | |
| 299 case 3+8: | |
| 300 fz_append_printf(ctx, gs->buf, "%g %g %g RG\n", color[0], color[1], color[2]); | |
| 301 break; | |
| 302 case 4+8: | |
| 303 fz_append_printf(ctx, gs->buf, "%g %g %g %g K\n", color[0], color[1], color[2], color[3]); | |
| 304 break; | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 static void | |
| 309 pdf_dev_alpha(fz_context *ctx, pdf_device *pdev, float alpha, int stroke) | |
| 310 { | |
| 311 int i; | |
| 312 pdf_document *doc = pdev->doc; | |
| 313 gstate *gs = CURRENT_GSTATE(pdev); | |
| 314 | |
| 315 /* If the alpha is unchanged, nothing to do */ | |
| 316 if (gs->alpha[stroke] == alpha) | |
| 317 return; | |
| 318 | |
| 319 gs->alpha[stroke] = alpha; | |
| 320 | |
| 321 /* Have we sent such an alpha before? */ | |
| 322 for (i = 0; i < pdev->num_alphas; i++) | |
| 323 if (pdev->alphas[i].alpha == alpha && pdev->alphas[i].stroke == stroke) | |
| 324 break; | |
| 325 | |
| 326 if (i == pdev->num_alphas) | |
| 327 { | |
| 328 pdf_obj *o, *ref; | |
| 329 | |
| 330 /* No. Need to make a new one */ | |
| 331 if (pdev->num_alphas == pdev->max_alphas) | |
| 332 { | |
| 333 int newmax = pdev->max_alphas * 2; | |
| 334 if (newmax == 0) | |
| 335 newmax = 4; | |
| 336 pdev->alphas = fz_realloc_array(ctx, pdev->alphas, newmax, alpha_entry); | |
| 337 pdev->max_alphas = newmax; | |
| 338 } | |
| 339 pdev->alphas[i].alpha = alpha; | |
| 340 pdev->alphas[i].stroke = stroke; | |
| 341 | |
| 342 o = pdf_new_dict(ctx, doc, 1); | |
| 343 fz_try(ctx) | |
| 344 { | |
| 345 char text[32]; | |
| 346 pdf_dict_put_real(ctx, o, (stroke ? PDF_NAME(CA) : PDF_NAME(ca)), alpha); | |
| 347 fz_snprintf(text, sizeof(text), "ExtGState/Alp%d", i); | |
| 348 ref = pdf_add_object(ctx, doc, o); | |
| 349 pdf_dict_putp_drop(ctx, pdev->resources, text, ref); | |
| 350 } | |
| 351 fz_always(ctx) | |
| 352 { | |
| 353 pdf_drop_obj(ctx, o); | |
| 354 } | |
| 355 fz_catch(ctx) | |
| 356 { | |
| 357 fz_rethrow(ctx); | |
| 358 } | |
| 359 pdev->num_alphas++; | |
| 360 } | |
| 361 fz_append_printf(ctx, gs->buf, "/Alp%d gs\n", i); | |
| 362 } | |
| 363 | |
| 364 static int | |
| 365 pdf_dev_find_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font) | |
| 366 { | |
| 367 int k; | |
| 368 | |
| 369 /* Check if we already had this one */ | |
| 370 for (k = 0; k < pdev->num_cid_fonts; k++) | |
| 371 if (pdev->cid_fonts[k] == font) | |
| 372 return k; | |
| 373 | |
| 374 return -1; | |
| 375 } | |
| 376 | |
| 377 static int | |
| 378 pdf_dev_add_font_res_imp(fz_context *ctx, pdf_device *pdev, fz_font *font, pdf_obj *fres, int enc) | |
| 379 { | |
| 380 char text[32]; | |
| 381 int num; | |
| 382 | |
| 383 /* Not there so add to resources */ | |
| 384 fz_snprintf(text, sizeof(text), "Font/F%d", pdev->num_cid_fonts); | |
| 385 pdf_dict_putp_drop(ctx, pdev->resources, text, fres); | |
| 386 | |
| 387 /* And add index to our list for this page */ | |
| 388 if (pdev->num_cid_fonts == pdev->max_cid_fonts) | |
| 389 { | |
| 390 int newmax = pdev->max_cid_fonts * 2; | |
| 391 if (newmax == 0) | |
| 392 newmax = 4; | |
| 393 pdev->cid_fonts = fz_realloc_array(ctx, pdev->cid_fonts, newmax, fz_font*); | |
| 394 pdev->cid_fonts_enc = fz_realloc_array(ctx, pdev->cid_fonts_enc, newmax, int); | |
| 395 pdev->max_cid_fonts = newmax; | |
| 396 } | |
| 397 num = pdev->num_cid_fonts++; | |
| 398 pdev->cid_fonts[num] = fz_keep_font(ctx, font); | |
| 399 pdev->cid_fonts_enc[num] = enc; | |
| 400 return num; | |
| 401 } | |
| 402 | |
| 403 static int | |
| 404 pdf_dev_add_substitute_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font) | |
| 405 { | |
| 406 pdf_obj *fres; | |
| 407 int k; | |
| 408 | |
| 409 /* Check if we already had this one */ | |
| 410 k = pdf_dev_find_font_res(ctx, pdev, font); | |
| 411 if (k >= 0) | |
| 412 return k; | |
| 413 | |
| 414 /* This will add it to the xref if needed */ | |
| 415 if (font->flags.cjk) | |
| 416 fres = pdf_add_cjk_font(ctx, pdev->doc, font, font->flags.cjk_lang, 0, font->flags.is_serif); | |
| 417 else | |
| 418 fres = pdf_add_substitute_font(ctx, pdev->doc, font); | |
| 419 | |
| 420 /* And add to the resource dictionary. */ | |
| 421 return pdf_dev_add_font_res_imp(ctx, pdev, font, fres, ENC_UNICODE); | |
| 422 } | |
| 423 | |
| 424 static int | |
| 425 pdf_dev_add_embedded_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font) | |
| 426 { | |
| 427 pdf_obj *fres; | |
| 428 int k; | |
| 429 | |
| 430 /* Check if we already had this one */ | |
| 431 k = pdf_dev_find_font_res(ctx, pdev, font); | |
| 432 if (k >= 0) | |
| 433 return k; | |
| 434 | |
| 435 /* This will add it to the xref if needed */ | |
| 436 fres = pdf_add_cid_font(ctx, pdev->doc, font); | |
| 437 | |
| 438 /* And add to the resource dictionary. */ | |
| 439 return pdf_dev_add_font_res_imp(ctx, pdev, font, fres, ENC_IDENTITY); | |
| 440 } | |
| 441 | |
| 442 static void | |
| 443 pdf_dev_font(fz_context *ctx, pdf_device *pdev, fz_font *font, fz_matrix trm) | |
| 444 { | |
| 445 gstate *gs = CURRENT_GSTATE(pdev); | |
| 446 float font_size = fz_matrix_expansion(trm); | |
| 447 | |
| 448 /* If the font is unchanged, nothing to do */ | |
| 449 if (gs->font >= 0 && pdev->cid_fonts[gs->font] == font && gs->font_size == font_size) | |
| 450 return; | |
| 451 | |
| 452 // TODO: vertical wmode | |
| 453 | |
| 454 if (fz_font_t3_procs(ctx, font)) | |
| 455 fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "pdf device does not support type 3 fonts"); | |
| 456 | |
| 457 if (fz_font_flags(font)->ft_substitute || !pdf_font_writing_supported(ctx, font)) | |
| 458 gs->font = pdf_dev_add_substitute_font_res(ctx, pdev, font); | |
| 459 else | |
| 460 gs->font = pdf_dev_add_embedded_font_res(ctx, pdev, font); | |
| 461 | |
| 462 gs->font_size = font_size; | |
| 463 | |
| 464 fz_append_printf(ctx, gs->buf, "/F%d %g Tf\n", gs->font, gs->font_size); | |
| 465 } | |
| 466 | |
| 467 static void | |
| 468 pdf_dev_push_new_buf(fz_context *ctx, pdf_device *pdev, fz_buffer *buf, void (*on_pop)(fz_context*,pdf_device*,void*), void *on_pop_arg) | |
| 469 { | |
| 470 if (pdev->num_gstates == pdev->max_gstates) | |
| 471 { | |
| 472 int newmax = pdev->max_gstates*2; | |
| 473 pdev->gstates = fz_realloc_array(ctx, pdev->gstates, newmax, gstate); | |
| 474 pdev->max_gstates = newmax; | |
| 475 } | |
| 476 memcpy(&pdev->gstates[pdev->num_gstates], &pdev->gstates[pdev->num_gstates-1], sizeof(*pdev->gstates)); | |
| 477 fz_keep_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state); | |
| 478 if (buf) | |
| 479 pdev->gstates[pdev->num_gstates].buf = buf; | |
| 480 else | |
| 481 fz_keep_buffer(ctx, pdev->gstates[pdev->num_gstates].buf); | |
| 482 pdev->gstates[pdev->num_gstates].on_pop = on_pop; | |
| 483 pdev->gstates[pdev->num_gstates].on_pop_arg = on_pop_arg; | |
| 484 fz_append_string(ctx, pdev->gstates[pdev->num_gstates].buf, "q\n"); | |
| 485 pdev->num_gstates++; | |
| 486 } | |
| 487 | |
| 488 static void | |
| 489 pdf_dev_push(fz_context *ctx, pdf_device *pdev) | |
| 490 { | |
| 491 pdf_dev_push_new_buf(ctx, pdev, NULL, NULL, NULL); | |
| 492 } | |
| 493 | |
| 494 static void * | |
| 495 pdf_dev_pop(fz_context *ctx, pdf_device *pdev) | |
| 496 { | |
| 497 gstate *gs = CURRENT_GSTATE(pdev); | |
| 498 void *arg = gs->on_pop_arg; | |
| 499 | |
| 500 fz_append_string(ctx, gs->buf, "Q\n"); | |
| 501 if (gs->on_pop) | |
| 502 gs->on_pop(ctx, pdev, arg); | |
| 503 pdev->num_gstates--; | |
| 504 fz_drop_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state); | |
| 505 fz_drop_buffer(ctx, pdev->gstates[pdev->num_gstates].buf); | |
| 506 return arg; | |
| 507 } | |
| 508 | |
| 509 static void | |
| 510 pdf_dev_text_span(fz_context *ctx, pdf_device *pdev, fz_text_span *span) | |
| 511 { | |
| 512 gstate *gs = CURRENT_GSTATE(pdev); | |
| 513 fz_matrix trm, tm, tlm, inv_trm, inv_tm; | |
| 514 fz_matrix inv_tfs; | |
| 515 fz_point d; | |
| 516 float adv; | |
| 517 int enc; | |
| 518 int dx, dy; | |
| 519 int i; | |
| 520 | |
| 521 if (span->len == 0) | |
| 522 return; | |
| 523 | |
| 524 inv_tfs = fz_scale(1 / gs->font_size, 1 / gs->font_size); | |
| 525 | |
| 526 trm = span->trm; | |
| 527 trm.e = span->items[0].x; | |
| 528 trm.f = span->items[0].y; | |
| 529 | |
| 530 tm = fz_concat(inv_tfs, trm); | |
| 531 tlm = tm; | |
| 532 | |
| 533 inv_tm = fz_invert_matrix(tm); | |
| 534 inv_trm = fz_invert_matrix(trm); | |
| 535 | |
| 536 enc = pdev->cid_fonts_enc[gs->font]; | |
| 537 | |
| 538 fz_append_printf(ctx, gs->buf, "%M Tm\n[<", &tm); | |
| 539 | |
| 540 for (i = 0; i < span->len; ++i) | |
| 541 { | |
| 542 fz_text_item *it = &span->items[i]; | |
| 543 if (enc == ENC_IDENTITY && it->gid < 0) | |
| 544 continue; | |
| 545 if (enc == ENC_UNICODE && it->ucs < 0) | |
| 546 continue; | |
| 547 | |
| 548 /* transform difference from expected pen position into font units. */ | |
| 549 d.x = it->x - trm.e; | |
| 550 d.y = it->y - trm.f; | |
| 551 d = fz_transform_vector(d, inv_trm); | |
| 552 dx = (int)(d.x * 1000 + (d.x < 0 ? -0.5f : 0.5f)); | |
| 553 dy = (int)(d.y * 1000 + (d.y < 0 ? -0.5f : 0.5f)); | |
| 554 | |
| 555 trm.e = it->x; | |
| 556 trm.f = it->y; | |
| 557 | |
| 558 if (dx != 0 || dy != 0) | |
| 559 { | |
| 560 if (span->wmode == 0 && dy == 0) | |
| 561 fz_append_printf(ctx, gs->buf, ">%d<", -dx); | |
| 562 else if (span->wmode == 1 && dx == 0) | |
| 563 fz_append_printf(ctx, gs->buf, ">%d<", -dy); | |
| 564 else | |
| 565 { | |
| 566 /* Calculate offset from start of the previous line */ | |
| 567 tm = fz_concat(inv_tfs, trm); | |
| 568 d.x = tm.e - tlm.e; | |
| 569 d.y = tm.f - tlm.f; | |
| 570 d = fz_transform_vector(d, inv_tm); | |
| 571 fz_append_printf(ctx, gs->buf, ">]TJ\n%g %g Td\n[<", d.x, d.y); | |
| 572 tlm = tm; | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 if (fz_font_t3_procs(ctx, span->font)) | |
| 577 fz_append_printf(ctx, gs->buf, "%02x", it->gid); | |
| 578 else if (enc == ENC_IDENTITY) | |
| 579 fz_append_printf(ctx, gs->buf, "%04x", it->gid); | |
| 580 else if (enc == ENC_UNICODE) | |
| 581 fz_append_printf(ctx, gs->buf, "%04x", it->ucs); | |
| 582 | |
| 583 if (it->gid != -1) | |
| 584 { | |
| 585 adv = fz_advance_glyph(ctx, span->font, it->gid, span->wmode); | |
| 586 if (span->wmode == 0) | |
| 587 trm = fz_pre_translate(trm, adv, 0); | |
| 588 else | |
| 589 trm = fz_pre_translate(trm, 0, adv); | |
| 590 } | |
| 591 } | |
| 592 | |
| 593 fz_append_string(ctx, gs->buf, ">]TJ\n"); | |
| 594 } | |
| 595 | |
| 596 static void | |
| 597 pdf_dev_trm(fz_context *ctx, pdf_device *pdev, int trm) | |
| 598 { | |
| 599 gstate *gs = CURRENT_GSTATE(pdev); | |
| 600 | |
| 601 if (gs->text_rendering_mode == trm) | |
| 602 return; | |
| 603 gs->text_rendering_mode = trm; | |
| 604 fz_append_printf(ctx, gs->buf, "%d Tr\n", trm); | |
| 605 } | |
| 606 | |
| 607 static void | |
| 608 pdf_dev_begin_text(fz_context *ctx, pdf_device *pdev, int trm) | |
| 609 { | |
| 610 pdf_dev_trm(ctx, pdev, trm); | |
| 611 if (!pdev->in_text) | |
| 612 { | |
| 613 gstate *gs = CURRENT_GSTATE(pdev); | |
| 614 fz_append_string(ctx, gs->buf, "BT\n"); | |
| 615 pdev->in_text = 1; | |
| 616 } | |
| 617 } | |
| 618 | |
| 619 static void | |
| 620 pdf_dev_end_text(fz_context *ctx, pdf_device *pdev) | |
| 621 { | |
| 622 gstate *gs = CURRENT_GSTATE(pdev); | |
| 623 | |
| 624 if (!pdev->in_text) | |
| 625 return; | |
| 626 pdev->in_text = 0; | |
| 627 fz_append_string(ctx, gs->buf, "ET\n"); | |
| 628 } | |
| 629 | |
| 630 static int | |
| 631 pdf_dev_new_form(fz_context *ctx, pdf_obj **form_ref, pdf_device *pdev, fz_rect bbox, int isolated, int knockout, float alpha, fz_colorspace *colorspace) | |
| 632 { | |
| 633 pdf_document *doc = pdev->doc; | |
| 634 int num; | |
| 635 pdf_obj *group_ref = NULL; | |
| 636 pdf_obj *group; | |
| 637 pdf_obj *form; | |
| 638 | |
| 639 *form_ref = NULL; | |
| 640 | |
| 641 /* Find (or make) a new group with the required options. */ | |
| 642 for(num = 0; num < pdev->num_groups; num++) | |
| 643 { | |
| 644 group_entry *g = &pdev->groups[num]; | |
| 645 if (g->isolated == isolated && g->knockout == knockout && g->alpha == alpha && g->colorspace == colorspace) | |
| 646 { | |
| 647 group_ref = pdev->groups[num].ref; | |
| 648 break; | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 /* If we didn't find one, make one */ | |
| 653 if (num == pdev->num_groups) | |
| 654 { | |
| 655 if (pdev->num_groups == pdev->max_groups) | |
| 656 { | |
| 657 int newmax = pdev->max_groups * 2; | |
| 658 if (newmax == 0) | |
| 659 newmax = 4; | |
| 660 pdev->groups = fz_realloc_array(ctx, pdev->groups, newmax, group_entry); | |
| 661 pdev->max_groups = newmax; | |
| 662 } | |
| 663 pdev->num_groups++; | |
| 664 pdev->groups[num].isolated = isolated; | |
| 665 pdev->groups[num].knockout = knockout; | |
| 666 pdev->groups[num].alpha = alpha; | |
| 667 pdev->groups[num].colorspace = fz_keep_colorspace(ctx, colorspace); | |
| 668 pdev->groups[num].ref = NULL; | |
| 669 group = pdf_new_dict(ctx, doc, 5); | |
| 670 fz_try(ctx) | |
| 671 { | |
| 672 pdf_dict_put(ctx, group, PDF_NAME(Type), PDF_NAME(Group)); | |
| 673 pdf_dict_put(ctx, group, PDF_NAME(S), PDF_NAME(Transparency)); | |
| 674 pdf_dict_put_bool(ctx, group, PDF_NAME(K), knockout); | |
| 675 pdf_dict_put_bool(ctx, group, PDF_NAME(I), isolated); | |
| 676 switch (fz_colorspace_type(ctx, colorspace)) | |
| 677 { | |
| 678 case FZ_COLORSPACE_GRAY: | |
| 679 pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceGray)); | |
| 680 break; | |
| 681 case FZ_COLORSPACE_RGB: | |
| 682 pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceRGB)); | |
| 683 break; | |
| 684 case FZ_COLORSPACE_CMYK: | |
| 685 pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceCMYK)); | |
| 686 break; | |
| 687 default: | |
| 688 break; | |
| 689 } | |
| 690 group_ref = pdev->groups[num].ref = pdf_add_object(ctx, doc, group); | |
| 691 } | |
| 692 fz_always(ctx) | |
| 693 { | |
| 694 pdf_drop_obj(ctx, group); | |
| 695 } | |
| 696 fz_catch(ctx) | |
| 697 { | |
| 698 fz_rethrow(ctx); | |
| 699 } | |
| 700 } | |
| 701 | |
| 702 /* Make us a new Forms object that points to that group, and change | |
| 703 * to writing into the buffer for that Forms object. */ | |
| 704 form = pdf_new_dict(ctx, doc, 4); | |
| 705 fz_try(ctx) | |
| 706 { | |
| 707 pdf_dict_put(ctx, form, PDF_NAME(Subtype), PDF_NAME(Form)); | |
| 708 pdf_dict_put(ctx, form, PDF_NAME(Group), group_ref); | |
| 709 pdf_dict_put_int(ctx, form, PDF_NAME(FormType), 1); | |
| 710 pdf_dict_put_rect(ctx, form, PDF_NAME(BBox), bbox); | |
| 711 *form_ref = pdf_add_object(ctx, doc, form); | |
| 712 } | |
| 713 fz_always(ctx) | |
| 714 { | |
| 715 pdf_drop_obj(ctx, form); | |
| 716 } | |
| 717 fz_catch(ctx) | |
| 718 { | |
| 719 fz_rethrow(ctx); | |
| 720 } | |
| 721 | |
| 722 /* Insert the new form object into the resources */ | |
| 723 { | |
| 724 char text[32]; | |
| 725 num = pdev->num_forms++; | |
| 726 fz_snprintf(text, sizeof(text), "XObject/Fm%d", num); | |
| 727 pdf_dict_putp(ctx, pdev->resources, text, *form_ref); | |
| 728 } | |
| 729 | |
| 730 return num; | |
| 731 } | |
| 732 | |
| 733 /* Entry points */ | |
| 734 | |
| 735 static void | |
| 736 pdf_dev_fill_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm, | |
| 737 fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params) | |
| 738 { | |
| 739 pdf_device *pdev = (pdf_device*)dev; | |
| 740 gstate *gs = CURRENT_GSTATE(pdev); | |
| 741 | |
| 742 pdf_dev_end_text(ctx, pdev); | |
| 743 pdf_dev_alpha(ctx, pdev, alpha, 0); | |
| 744 pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); | |
| 745 pdf_dev_ctm(ctx, pdev, ctm); | |
| 746 pdf_dev_path(ctx, pdev, path); | |
| 747 fz_append_string(ctx, gs->buf, (even_odd ? "f*\n" : "f\n")); | |
| 748 } | |
| 749 | |
| 750 static void | |
| 751 pdf_dev_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, | |
| 752 fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params) | |
| 753 { | |
| 754 pdf_device *pdev = (pdf_device*)dev; | |
| 755 gstate *gs = CURRENT_GSTATE(pdev); | |
| 756 | |
| 757 pdf_dev_end_text(ctx, pdev); | |
| 758 pdf_dev_alpha(ctx, pdev, alpha, 1); | |
| 759 pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params); | |
| 760 pdf_dev_ctm(ctx, pdev, ctm); | |
| 761 pdf_dev_stroke_state(ctx, pdev, stroke); | |
| 762 pdf_dev_path(ctx, pdev, path); | |
| 763 fz_append_string(ctx, gs->buf, "S\n"); | |
| 764 } | |
| 765 | |
| 766 static void | |
| 767 pdf_dev_clip_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm, fz_rect scissor) | |
| 768 { | |
| 769 pdf_device *pdev = (pdf_device*)dev; | |
| 770 gstate *gs; | |
| 771 | |
| 772 pdf_dev_end_text(ctx, pdev); | |
| 773 pdf_dev_push(ctx, pdev); | |
| 774 pdf_dev_ctm(ctx, pdev, ctm); | |
| 775 pdf_dev_path(ctx, pdev, path); | |
| 776 gs = CURRENT_GSTATE(pdev); | |
| 777 fz_append_string(ctx, gs->buf, (even_odd ? "W* n\n" : "W n\n")); | |
| 778 } | |
| 779 | |
| 780 static void | |
| 781 pdf_dev_clip_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor) | |
| 782 { | |
| 783 pdf_device *pdev = (pdf_device*)dev; | |
| 784 gstate *gs; | |
| 785 | |
| 786 pdf_dev_end_text(ctx, pdev); | |
| 787 pdf_dev_push(ctx, pdev); | |
| 788 /* FIXME: Need to push a group, select a pattern (or shading) here, | |
| 789 * stroke with the pattern/shading. Then move to defining that pattern | |
| 790 * with the next calls to the device interface until the next pop | |
| 791 * when we pop the group. */ | |
| 792 pdf_dev_ctm(ctx, pdev, ctm); | |
| 793 pdf_dev_path(ctx, pdev, path); | |
| 794 gs = CURRENT_GSTATE(pdev); | |
| 795 fz_append_string(ctx, gs->buf, "W n\n"); | |
| 796 } | |
| 797 | |
| 798 static void | |
| 799 pdf_dev_fill_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, | |
| 800 fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params) | |
| 801 { | |
| 802 pdf_device *pdev = (pdf_device*)dev; | |
| 803 fz_text_span *span; | |
| 804 | |
| 805 pdf_dev_ctm(ctx, pdev, ctm); | |
| 806 pdf_dev_alpha(ctx, pdev, alpha, 0); | |
| 807 pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); | |
| 808 | |
| 809 for (span = text->head; span; span = span->next) | |
| 810 { | |
| 811 pdf_dev_begin_text(ctx, pdev, 0); | |
| 812 pdf_dev_font(ctx, pdev, span->font, span->trm); | |
| 813 pdf_dev_text_span(ctx, pdev, span); | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 static void | |
| 818 pdf_dev_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, | |
| 819 fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params) | |
| 820 { | |
| 821 pdf_device *pdev = (pdf_device*)dev; | |
| 822 fz_text_span *span; | |
| 823 | |
| 824 pdf_dev_ctm(ctx, pdev, ctm); | |
| 825 pdf_dev_alpha(ctx, pdev, alpha, 1); | |
| 826 pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params); | |
| 827 pdf_dev_stroke_state(ctx, pdev, stroke); | |
| 828 | |
| 829 for (span = text->head; span; span = span->next) | |
| 830 { | |
| 831 pdf_dev_begin_text(ctx, pdev, 1); | |
| 832 pdf_dev_font(ctx, pdev, span->font, span->trm); | |
| 833 pdf_dev_text_span(ctx, pdev, span); | |
| 834 } | |
| 835 } | |
| 836 | |
| 837 static void | |
| 838 pdf_dev_clip_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, fz_rect scissor) | |
| 839 { | |
| 840 pdf_device *pdev = (pdf_device*)dev; | |
| 841 fz_text_span *span; | |
| 842 | |
| 843 pdf_dev_end_text(ctx, pdev); | |
| 844 pdf_dev_push(ctx, pdev); | |
| 845 | |
| 846 pdf_dev_ctm(ctx, pdev, ctm); | |
| 847 | |
| 848 for (span = text->head; span; span = span->next) | |
| 849 { | |
| 850 pdf_dev_begin_text(ctx, pdev, 7); | |
| 851 pdf_dev_font(ctx, pdev, span->font, span->trm); | |
| 852 pdf_dev_text_span(ctx, pdev, span); | |
| 853 } | |
| 854 } | |
| 855 | |
| 856 static void | |
| 857 pdf_dev_clip_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor) | |
| 858 { | |
| 859 pdf_device *pdev = (pdf_device*)dev; | |
| 860 fz_text_span *span; | |
| 861 | |
| 862 pdf_dev_end_text(ctx, pdev); | |
| 863 pdf_dev_push(ctx, pdev); | |
| 864 | |
| 865 pdf_dev_ctm(ctx, pdev, ctm); | |
| 866 | |
| 867 for (span = text->head; span; span = span->next) | |
| 868 { | |
| 869 pdf_dev_begin_text(ctx, pdev, 7); | |
| 870 pdf_dev_font(ctx, pdev, span->font, span->trm); | |
| 871 pdf_dev_text_span(ctx, pdev, span); | |
| 872 } | |
| 873 } | |
| 874 | |
| 875 static void | |
| 876 pdf_dev_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm) | |
| 877 { | |
| 878 pdf_device *pdev = (pdf_device*)dev; | |
| 879 fz_text_span *span; | |
| 880 | |
| 881 pdf_dev_ctm(ctx, pdev, ctm); | |
| 882 | |
| 883 for (span = text->head; span; span = span->next) | |
| 884 { | |
| 885 pdf_dev_begin_text(ctx, pdev, 0); | |
| 886 pdf_dev_font(ctx, pdev, span->font, span->trm); | |
| 887 pdf_dev_text_span(ctx, pdev, span); | |
| 888 } | |
| 889 } | |
| 890 | |
| 891 static void | |
| 892 pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_obj *im_res) | |
| 893 { | |
| 894 char text[32]; | |
| 895 pdf_device *pdev = (pdf_device*)dev; | |
| 896 int k; | |
| 897 int num; | |
| 898 | |
| 899 /* Check if we already had this one */ | |
| 900 for (k = 0; k < pdev->num_imgs; k++) | |
| 901 { | |
| 902 if (pdev->image_indices[k] == pdf_to_num(ctx, im_res)) | |
| 903 return; | |
| 904 } | |
| 905 | |
| 906 /* Not there so add to resources */ | |
| 907 fz_snprintf(text, sizeof(text), "XObject/Img%d", pdf_to_num(ctx, im_res)); | |
| 908 pdf_dict_putp(ctx, pdev->resources, text, im_res); | |
| 909 | |
| 910 /* And add index to our list for this page */ | |
| 911 if (pdev->num_imgs == pdev->max_imgs) | |
| 912 { | |
| 913 int newmax = pdev->max_imgs * 2; | |
| 914 if (newmax == 0) | |
| 915 newmax = 4; | |
| 916 pdev->image_indices = fz_realloc_array(ctx, pdev->image_indices, newmax, int); | |
| 917 pdev->max_imgs = newmax; | |
| 918 } | |
| 919 num = pdev->num_imgs++; | |
| 920 pdev->image_indices[num] = pdf_to_num(ctx, im_res); | |
| 921 } | |
| 922 | |
| 923 static void | |
| 924 pdf_dev_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, float alpha, fz_color_params color_params) | |
| 925 { | |
| 926 pdf_device *pdev = (pdf_device*)dev; | |
| 927 pdf_obj *im_res; | |
| 928 gstate *gs = CURRENT_GSTATE(pdev); | |
| 929 | |
| 930 pdf_dev_end_text(ctx, pdev); | |
| 931 im_res = pdf_add_image(ctx, pdev->doc, image); | |
| 932 if (im_res == NULL) | |
| 933 { | |
| 934 fz_warn(ctx, "pdf_add_image: problem adding image resource"); | |
| 935 return; | |
| 936 } | |
| 937 | |
| 938 fz_try(ctx) | |
| 939 { | |
| 940 pdf_dev_alpha(ctx, pdev, alpha, 0); | |
| 941 | |
| 942 /* PDF images are upside down, so fiddle the ctm */ | |
| 943 ctm = fz_pre_scale(ctm, 1, -1); | |
| 944 ctm = fz_pre_translate(ctm, 0, -1); | |
| 945 pdf_dev_ctm(ctx, pdev, ctm); | |
| 946 fz_append_printf(ctx, gs->buf, "/Img%d Do\n", pdf_to_num(ctx, im_res)); | |
| 947 | |
| 948 /* Possibly add to page resources */ | |
| 949 pdf_dev_add_image_res(ctx, dev, im_res); | |
| 950 } | |
| 951 fz_always(ctx) | |
| 952 pdf_drop_obj(ctx, im_res); | |
| 953 fz_catch(ctx) | |
| 954 fz_rethrow(ctx); | |
| 955 } | |
| 956 | |
| 957 static void | |
| 958 pdf_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha, fz_color_params color_params) | |
| 959 { | |
| 960 pdf_device *pdev = (pdf_device*)dev; | |
| 961 | |
| 962 fz_warn(ctx, "the pdf device does not support shadings; output may be incomplete"); | |
| 963 | |
| 964 /* FIXME */ | |
| 965 pdf_dev_end_text(ctx, pdev); | |
| 966 } | |
| 967 | |
| 968 static void | |
| 969 pdf_dev_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, | |
| 970 fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params) | |
| 971 { | |
| 972 pdf_device *pdev = (pdf_device*)dev; | |
| 973 pdf_obj *im_res = NULL; | |
| 974 gstate *gs = CURRENT_GSTATE(pdev); | |
| 975 | |
| 976 pdf_dev_end_text(ctx, pdev); | |
| 977 im_res = pdf_add_image(ctx, pdev->doc, image); | |
| 978 if (im_res == NULL) | |
| 979 { | |
| 980 fz_warn(ctx, "pdf_add_image: problem adding image resource"); | |
| 981 return; | |
| 982 } | |
| 983 | |
| 984 fz_try(ctx) | |
| 985 { | |
| 986 fz_append_string(ctx, gs->buf, "q\n"); | |
| 987 pdf_dev_alpha(ctx, pdev, alpha, 0); | |
| 988 pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); | |
| 989 | |
| 990 /* PDF images are upside down, so fiddle the ctm */ | |
| 991 ctm = fz_pre_scale(ctm, 1, -1); | |
| 992 ctm = fz_pre_translate(ctm, 0, -1); | |
| 993 pdf_dev_ctm(ctx, pdev, ctm); | |
| 994 fz_append_printf(ctx, gs->buf, "/Img%d Do Q\n", pdf_to_num(ctx, im_res)); | |
| 995 | |
| 996 /* Possibly add to page resources */ | |
| 997 pdf_dev_add_image_res(ctx, dev, im_res); | |
| 998 } | |
| 999 fz_always(ctx) | |
| 1000 pdf_drop_obj(ctx, im_res); | |
| 1001 fz_catch(ctx) | |
| 1002 fz_rethrow(ctx); | |
| 1003 } | |
| 1004 | |
| 1005 static void | |
| 1006 pdf_dev_clip_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, fz_rect scissor) | |
| 1007 { | |
| 1008 pdf_device *pdev = (pdf_device*)dev; | |
| 1009 | |
| 1010 fz_warn(ctx, "the pdf device does not support image masks; output may be incomplete"); | |
| 1011 | |
| 1012 /* FIXME */ | |
| 1013 pdf_dev_end_text(ctx, pdev); | |
| 1014 pdf_dev_push(ctx, pdev); | |
| 1015 } | |
| 1016 | |
| 1017 static void | |
| 1018 pdf_dev_pop_clip(fz_context *ctx, fz_device *dev) | |
| 1019 { | |
| 1020 pdf_device *pdev = (pdf_device*)dev; | |
| 1021 | |
| 1022 /* FIXME */ | |
| 1023 pdf_dev_end_text(ctx, pdev); | |
| 1024 pdf_dev_pop(ctx, pdev); | |
| 1025 } | |
| 1026 | |
| 1027 static void | |
| 1028 pdf_dev_begin_mask(fz_context *ctx, fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, const float *color, fz_color_params color_params) | |
| 1029 { | |
| 1030 pdf_device *pdev = (pdf_device*)dev; | |
| 1031 gstate *gs; | |
| 1032 pdf_obj *smask = NULL; | |
| 1033 char egsname[32]; | |
| 1034 pdf_obj *egs = NULL; | |
| 1035 pdf_obj *egss; | |
| 1036 pdf_obj *form_ref; | |
| 1037 pdf_obj *color_obj = NULL; | |
| 1038 int i, n; | |
| 1039 | |
| 1040 fz_var(smask); | |
| 1041 fz_var(egs); | |
| 1042 fz_var(color_obj); | |
| 1043 | |
| 1044 pdf_dev_end_text(ctx, pdev); | |
| 1045 | |
| 1046 pdf_dev_ctm(ctx, pdev, fz_identity); | |
| 1047 | |
| 1048 /* Make a new form to contain the contents of the softmask */ | |
| 1049 pdf_dev_new_form(ctx, &form_ref, pdev, bbox, 0, 0, 1, colorspace); | |
| 1050 | |
| 1051 fz_try(ctx) | |
| 1052 { | |
| 1053 fz_snprintf(egsname, sizeof(egsname), "SM%d", pdev->num_smasks++); | |
| 1054 egss = pdf_dict_get(ctx, pdev->resources, PDF_NAME(ExtGState)); | |
| 1055 if (!egss) | |
| 1056 egss = pdf_dict_put_dict(ctx, pdev->resources, PDF_NAME(ExtGState), 10); | |
| 1057 egs = pdf_dict_puts_dict(ctx, egss, egsname, 1); | |
| 1058 | |
| 1059 pdf_dict_put(ctx, egs, PDF_NAME(Type), PDF_NAME(ExtGState)); | |
| 1060 smask = pdf_dict_put_dict(ctx, egs, PDF_NAME(SMask), 4); | |
| 1061 | |
| 1062 pdf_dict_put(ctx, smask, PDF_NAME(Type), PDF_NAME(Mask)); | |
| 1063 pdf_dict_put(ctx, smask, PDF_NAME(S), (luminosity ? PDF_NAME(Luminosity) : PDF_NAME(Alpha))); | |
| 1064 pdf_dict_put(ctx, smask, PDF_NAME(G), form_ref); | |
| 1065 | |
| 1066 n = fz_colorspace_n(ctx, colorspace); | |
| 1067 color_obj = pdf_dict_put_array(ctx, smask, PDF_NAME(BC), n); | |
| 1068 for (i = 0; i < n; i++) | |
| 1069 pdf_array_push_real(ctx, color_obj, color[i]); | |
| 1070 | |
| 1071 gs = CURRENT_GSTATE(pdev); | |
| 1072 fz_append_printf(ctx, gs->buf, "/SM%d gs\n", pdev->num_smasks-1); | |
| 1073 } | |
| 1074 fz_catch(ctx) | |
| 1075 { | |
| 1076 pdf_drop_obj(ctx, form_ref); | |
| 1077 fz_rethrow(ctx); | |
| 1078 } | |
| 1079 | |
| 1080 /* Now, everything we get until the end_mask needs to go into a | |
| 1081 * new buffer, which will be the stream contents for the form. */ | |
| 1082 pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref); | |
| 1083 } | |
| 1084 | |
| 1085 static void | |
| 1086 pdf_dev_end_mask(fz_context *ctx, fz_device *dev, fz_function *tr) | |
| 1087 { | |
| 1088 pdf_device *pdev = (pdf_device*)dev; | |
| 1089 pdf_document *doc = pdev->doc; | |
| 1090 gstate *gs = CURRENT_GSTATE(pdev); | |
| 1091 pdf_obj *form_ref = (pdf_obj *)gs->on_pop_arg; | |
| 1092 | |
| 1093 if (tr) | |
| 1094 fz_warn(ctx, "Ignoring Transfer function"); | |
| 1095 | |
| 1096 /* Here we do part of the pop, but not all of it. */ | |
| 1097 pdf_dev_end_text(ctx, pdev); | |
| 1098 fz_append_string(ctx, gs->buf, "Q\n"); | |
| 1099 pdf_update_stream(ctx, doc, form_ref, gs->buf, 0); | |
| 1100 fz_drop_buffer(ctx, gs->buf); | |
| 1101 gs->buf = fz_keep_buffer(ctx, gs[-1].buf); | |
| 1102 gs->on_pop_arg = NULL; | |
| 1103 pdf_drop_obj(ctx, form_ref); | |
| 1104 fz_append_string(ctx, gs->buf, "q\n"); | |
| 1105 } | |
| 1106 | |
| 1107 static void | |
| 1108 pdf_dev_begin_group(fz_context *ctx, fz_device *dev, fz_rect bbox, fz_colorspace *cs, int isolated, int knockout, int blendmode, float alpha) | |
| 1109 { | |
| 1110 pdf_device *pdev = (pdf_device*)dev; | |
| 1111 int num; | |
| 1112 pdf_obj *form_ref; | |
| 1113 gstate *gs; | |
| 1114 | |
| 1115 pdf_dev_end_text(ctx, pdev); | |
| 1116 | |
| 1117 pdf_dev_ctm(ctx, pdev, fz_identity); | |
| 1118 | |
| 1119 num = pdf_dev_new_form(ctx, &form_ref, pdev, bbox, isolated, knockout, alpha, cs); | |
| 1120 | |
| 1121 /* Do we have an appropriate blending extgstate already? */ | |
| 1122 { | |
| 1123 char text[32]; | |
| 1124 pdf_obj *obj; | |
| 1125 pdf_obj *egs = pdf_dict_get(ctx, pdev->resources, PDF_NAME(ExtGState)); | |
| 1126 if (egs == NULL) | |
| 1127 egs = pdf_dict_put_dict(ctx, pdev->resources, PDF_NAME(ExtGState), 4); | |
| 1128 fz_snprintf(text, sizeof(text), "BlendMode%d", blendmode); | |
| 1129 obj = pdf_dict_gets(ctx, egs, text); | |
| 1130 if (obj == NULL) | |
| 1131 { | |
| 1132 /* No, better make one */ | |
| 1133 obj = pdf_dict_puts_dict(ctx, egs, text, 2); | |
| 1134 pdf_dict_put(ctx, obj, PDF_NAME(Type), PDF_NAME(ExtGState)); | |
| 1135 pdf_dict_put_name(ctx, obj, PDF_NAME(BM), fz_blendmode_name(blendmode)); | |
| 1136 } | |
| 1137 } | |
| 1138 | |
| 1139 /* Add the call to this group */ | |
| 1140 gs = CURRENT_GSTATE(pdev); | |
| 1141 fz_append_printf(ctx, gs->buf, "/BlendMode%d gs /Fm%d Do\n", blendmode, num); | |
| 1142 | |
| 1143 /* Now, everything we get until the end of group needs to go into a | |
| 1144 * new buffer, which will be the stream contents for the form. */ | |
| 1145 pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref); | |
| 1146 } | |
| 1147 | |
| 1148 static void | |
| 1149 pdf_dev_end_group(fz_context *ctx, fz_device *dev) | |
| 1150 { | |
| 1151 pdf_device *pdev = (pdf_device*)dev; | |
| 1152 pdf_document *doc = pdev->doc; | |
| 1153 gstate *gs = CURRENT_GSTATE(pdev); | |
| 1154 fz_buffer *buf = fz_keep_buffer(ctx, gs->buf); | |
| 1155 pdf_obj *form_ref; | |
| 1156 | |
| 1157 pdf_dev_end_text(ctx, pdev); | |
| 1158 form_ref = (pdf_obj *)pdf_dev_pop(ctx, pdev); | |
| 1159 pdf_update_stream(ctx, doc, form_ref, buf, 0); | |
| 1160 fz_drop_buffer(ctx, buf); | |
| 1161 pdf_drop_obj(ctx, form_ref); | |
| 1162 } | |
| 1163 | |
| 1164 static int | |
| 1165 pdf_dev_begin_tile(fz_context *ctx, fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm, int id, int doc_id) | |
| 1166 { | |
| 1167 pdf_device *pdev = (pdf_device*)dev; | |
| 1168 | |
| 1169 /* FIXME */ | |
| 1170 pdf_dev_end_text(ctx, pdev); | |
| 1171 return 0; | |
| 1172 } | |
| 1173 | |
| 1174 static void | |
| 1175 pdf_dev_end_tile(fz_context *ctx, fz_device *dev) | |
| 1176 { | |
| 1177 pdf_device *pdev = (pdf_device*)dev; | |
| 1178 | |
| 1179 /* FIXME */ | |
| 1180 pdf_dev_end_text(ctx, pdev); | |
| 1181 } | |
| 1182 | |
| 1183 static void | |
| 1184 pdf_dev_close_device(fz_context *ctx, fz_device *dev) | |
| 1185 { | |
| 1186 pdf_device *pdev = (pdf_device*)dev; | |
| 1187 pdf_dev_end_text(ctx, pdev); | |
| 1188 } | |
| 1189 | |
| 1190 static void | |
| 1191 pdf_dev_drop_device(fz_context *ctx, fz_device *dev) | |
| 1192 { | |
| 1193 pdf_device *pdev = (pdf_device*)dev; | |
| 1194 int i; | |
| 1195 | |
| 1196 for (i = pdev->num_gstates-1; i >= 0; i--) | |
| 1197 { | |
| 1198 fz_drop_buffer(ctx, pdev->gstates[i].buf); | |
| 1199 fz_drop_stroke_state(ctx, pdev->gstates[i].stroke_state); | |
| 1200 } | |
| 1201 | |
| 1202 for (i = pdev->num_cid_fonts-1; i >= 0; i--) | |
| 1203 fz_drop_font(ctx, pdev->cid_fonts[i]); | |
| 1204 | |
| 1205 for (i = pdev->num_groups - 1; i >= 0; i--) | |
| 1206 { | |
| 1207 pdf_drop_obj(ctx, pdev->groups[i].ref); | |
| 1208 fz_drop_colorspace(ctx, pdev->groups[i].colorspace); | |
| 1209 } | |
| 1210 | |
| 1211 pdf_drop_obj(ctx, pdev->resources); | |
| 1212 fz_free(ctx, pdev->cid_fonts); | |
| 1213 fz_free(ctx, pdev->cid_fonts_enc); | |
| 1214 fz_free(ctx, pdev->image_indices); | |
| 1215 fz_free(ctx, pdev->groups); | |
| 1216 fz_free(ctx, pdev->alphas); | |
| 1217 fz_free(ctx, pdev->gstates); | |
| 1218 } | |
| 1219 | |
| 1220 fz_device *pdf_new_pdf_device(fz_context *ctx, pdf_document *doc, fz_matrix topctm, pdf_obj *resources, fz_buffer *buf) | |
| 1221 { | |
| 1222 pdf_device *dev = fz_new_derived_device(ctx, pdf_device); | |
| 1223 | |
| 1224 dev->super.close_device = pdf_dev_close_device; | |
| 1225 dev->super.drop_device = pdf_dev_drop_device; | |
| 1226 | |
| 1227 dev->super.fill_path = pdf_dev_fill_path; | |
| 1228 dev->super.stroke_path = pdf_dev_stroke_path; | |
| 1229 dev->super.clip_path = pdf_dev_clip_path; | |
| 1230 dev->super.clip_stroke_path = pdf_dev_clip_stroke_path; | |
| 1231 | |
| 1232 dev->super.fill_text = pdf_dev_fill_text; | |
| 1233 dev->super.stroke_text = pdf_dev_stroke_text; | |
| 1234 dev->super.clip_text = pdf_dev_clip_text; | |
| 1235 dev->super.clip_stroke_text = pdf_dev_clip_stroke_text; | |
| 1236 dev->super.ignore_text = pdf_dev_ignore_text; | |
| 1237 | |
| 1238 dev->super.fill_shade = pdf_dev_fill_shade; | |
| 1239 dev->super.fill_image = pdf_dev_fill_image; | |
| 1240 dev->super.fill_image_mask = pdf_dev_fill_image_mask; | |
| 1241 dev->super.clip_image_mask = pdf_dev_clip_image_mask; | |
| 1242 | |
| 1243 dev->super.pop_clip = pdf_dev_pop_clip; | |
| 1244 | |
| 1245 dev->super.begin_mask = pdf_dev_begin_mask; | |
| 1246 dev->super.end_mask = pdf_dev_end_mask; | |
| 1247 dev->super.begin_group = pdf_dev_begin_group; | |
| 1248 dev->super.end_group = pdf_dev_end_group; | |
| 1249 | |
| 1250 dev->super.begin_tile = pdf_dev_begin_tile; | |
| 1251 dev->super.end_tile = pdf_dev_end_tile; | |
| 1252 | |
| 1253 fz_var(buf); | |
| 1254 | |
| 1255 fz_try(ctx) | |
| 1256 { | |
| 1257 dev->doc = doc; | |
| 1258 dev->resources = pdf_keep_obj(ctx, resources); | |
| 1259 dev->gstates = fz_malloc_struct(ctx, gstate); | |
| 1260 if (buf) | |
| 1261 dev->gstates[0].buf = fz_keep_buffer(ctx, buf); | |
| 1262 else | |
| 1263 dev->gstates[0].buf = fz_new_buffer(ctx, 256); | |
| 1264 dev->gstates[0].ctm = fz_identity; // XXX | |
| 1265 dev->gstates[0].colorspace[0] = fz_device_gray(ctx); | |
| 1266 dev->gstates[0].colorspace[1] = fz_device_gray(ctx); | |
| 1267 dev->gstates[0].color[0][0] = 0; | |
| 1268 dev->gstates[0].color[1][0] = 0; | |
| 1269 dev->gstates[0].alpha[0] = 1.0f; | |
| 1270 dev->gstates[0].alpha[1] = 1.0f; | |
| 1271 dev->gstates[0].font = -1; | |
| 1272 dev->num_gstates = 1; | |
| 1273 dev->max_gstates = 1; | |
| 1274 | |
| 1275 if (!fz_is_identity(topctm)) | |
| 1276 fz_append_printf(ctx, dev->gstates[0].buf, "%M cm\n", &topctm); | |
| 1277 } | |
| 1278 fz_catch(ctx) | |
| 1279 { | |
| 1280 fz_drop_device(ctx, &dev->super); | |
| 1281 fz_rethrow(ctx); | |
| 1282 } | |
| 1283 | |
| 1284 return (fz_device*)dev; | |
| 1285 } | |
| 1286 | |
| 1287 fz_device *pdf_page_write(fz_context *ctx, pdf_document *doc, fz_rect mediabox, pdf_obj **presources, fz_buffer **pcontents) | |
| 1288 { | |
| 1289 fz_matrix pagectm = { 1, 0, 0, -1, -mediabox.x0, mediabox.y1 }; | |
| 1290 if (!*presources) | |
| 1291 *presources = pdf_new_dict(ctx, doc, 0); | |
| 1292 if (!*pcontents) | |
| 1293 *pcontents = fz_new_buffer(ctx, 0); | |
| 1294 return pdf_new_pdf_device(ctx, doc, pagectm, *presources, *pcontents); | |
| 1295 } |
