Debian Codegraph @

pdf/pdf_text.c

raw
1 /* Copyright (C) 2018-2026 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* Text operations for the PDF interpreter */
17
18 #include "pdf_int.h"
19 #include "pdf_array.h"
20 #include "pdf_text.h"
21 #include "pdf_image.h"
22 #include "pdf_colour.h"
23 #include "pdf_stack.h"
24 #include "pdf_font.h"
25 #include "pdf_font_types.h"
26 #include "pdf_gstate.h"
27 #include "pdf_trans.h"
28 #include "pdf_optcontent.h"
29
30 #include "gsstate.h"
31 #include "gsmatrix.h"
32 #include "gdevbbox.h"
33 #include "gspaint.h" /* For gs_fill() and friends */
34 #include "gscoord.h" /* For gs_setmatrix() */
35 #include "gxdevsop.h" /* For special ops */
36
37 static int pdfi_set_TL(pdf_context *ctx, double TL);
38
39 int pdfi_BT(pdf_context *ctx)
40 {
41 int code;
42 gs_matrix m;
43 bool illegal_BT = false;
44
45 if (ctx->text.BlockDepth != 0) {
46 illegal_BT = true;
47 if (ctx->text.TextClip) {
48 gx_device *dev = gs_currentdevice_inline(ctx->pgs);
49
50 ctx->text.TextClip = false;
51 (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
52 }
53 code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
54 if (code < 0)
55 return code;
56 }
57
58 gs_make_identity(&m);
59 code = gs_settextmatrix(ctx->pgs, &m);
60 if (code < 0)
61 return code;
62
63 code = gs_settextlinematrix(ctx->pgs, &m);
64 if (code < 0)
65 return code;
66
67 /* We should not perform the clip (for text rendering modes involving a clip)
68 * when preserving the text rendering mode.
69 */
70 if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && ctx->text.BlockDepth == 0 && !ctx->device_state.preserve_tr_mode) {
71 /* Start a new path (so our clip doesn't include any
72 * already extant path in the graphics state)
73 */
74 gs_newpath(ctx->pgs);
75 }
76
77 ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
78 if (!ctx->pgs->current_point_valid)
79 code = gs_moveto(ctx->pgs, 0, 0);
80
81 ctx->text.BlockDepth++;
82
83 if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
84 gs_begin_transparency_text_group(ctx->pgs);
85
86 return code;
87 }
88
89 static int do_ET(pdf_context *ctx)
90 {
91 int code = 0;
92 gx_clip_path *copy = NULL;
93
94 /* If we have reached the end of a text block (or the outermost block
95 * if we have illegally nested text blocks) and we are using a 'clip'
96 * text rendering mode, then we need to apply the clip. We also need
97 * to grestore back one level as we will have pushed a gsave either in
98 * pdfi_BT or in pdfi_Tr. The extra gsave is so we can accumulate a
99 * clipping path separately to any path already existing in the
100 * graphics state.
101 */
102
103 /* See the note on text rendering modes with clip in pdfi_BT() above */
104 if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
105 gs_point initial_point;
106
107 if (!ctx->device_state.preserve_tr_mode) {
108 ctx->text.TextClip = false;
109 /* Capture the current position */
110 code = gs_currentpoint(ctx->pgs, &initial_point);
111 if (code >= 0 || code == gs_error_nocurrentpoint) {
112 gs_point adjust;
113 bool nocurrentpoint = code >= 0 ? false : true;
114
115 gs_currentfilladjust(ctx->pgs, &adjust);
116 code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
117 if (code < 0)
118 return code;
119
120 code = gs_clip(ctx->pgs);
121 if (code >= 0)
122 copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
123
124 code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
125 if (code < 0)
126 return code;
127
128 if (copy != NULL)
129 (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
130
131 if (nocurrentpoint == false)
132 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
133 }
134 }
135 }
136 if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
137 gs_end_transparency_text_group(ctx->pgs);
138
139 if (!ctx->text.initial_current_point_valid)
140 gs_newpath(ctx->pgs);
141 return code;
142 }
143
144 int pdfi_ET(pdf_context *ctx)
145 {
146 if (ctx->text.BlockDepth == 0) {
147 int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
148 return code;
149 }
150
151 ctx->text.BlockDepth--;
152
153 return do_ET(ctx);
154 }
155
156 int pdfi_T_star(pdf_context *ctx)
157 {
158 int code;
159 gs_matrix m, mat;
160
161 if (ctx->text.BlockDepth == 0) {
162 int code;
163 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_TEXTOPNOBT, "pdfi_T_star", NULL)) < 0)
164 return code;
165 }
166
167 gs_make_identity(&m);
168 m.ty += ctx->pgs->textleading;
169
170 code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
171 if (code < 0)
172 return code;
173
174 code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
175 if (code < 0)
176 return code;
177
178 code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
179 return code;
180 }
181
182 static int pdfi_set_Tc(pdf_context *ctx, double Tc)
183 {
184 return gs_settextspacing(ctx->pgs, Tc);
185 }
186
187 int pdfi_Tc(pdf_context *ctx)
188 {
189 int code;
190 double d;
191
192 code = pdfi_destack_real(ctx, &d);
193 if (code < 0)
194 return code;
195
196 return pdfi_set_Tc(ctx, d);
197 }
198
199 int pdfi_Td(pdf_context *ctx)
200 {
201 int code;
202 double Txy[2];
203 gs_matrix m, mat;
204
205 code = pdfi_destack_reals(ctx, Txy, 2);
206 if (code < 0)
207 return code;
208
209 gs_make_identity(&m);
210
211 m.tx = Txy[0];
212 m.ty = Txy[1];
213
214 if (ctx->text.BlockDepth == 0) {
215 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
216
217 gs_make_identity(&mat);
218 code = gs_settextmatrix(ctx->pgs, &mat);
219 if (code < 0)
220 return code;
221
222 code = gs_settextlinematrix(ctx->pgs, &mat);
223 if (code < 0)
224 return code;
225 }
226
227 code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
228 if (code < 0)
229 return code;
230
231 code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
232 if (code < 0)
233 return code;
234
235 return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
236 }
237
238 int pdfi_TD(pdf_context *ctx)
239 {
240 int code;
241 double Txy[2];
242 gs_matrix m, mat;
243
244 gs_make_identity(&m);
245
246 code = pdfi_destack_reals(ctx, Txy, 2);
247 if (code < 0)
248 return code;
249
250 m.tx = Txy[0];
251 m.ty = Txy[1];
252
253 if (ctx->text.BlockDepth == 0) {
254 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
255
256 gs_make_identity(&mat);
257 code = gs_settextmatrix(ctx->pgs, &mat);
258 if (code < 0)
259 return code;
260
261 code = gs_settextlinematrix(ctx->pgs, &mat);
262 if (code < 0)
263 return code;
264 }
265
266 code = pdfi_set_TL(ctx, m.ty * 1.0f);
267 if (code < 0)
268 return code;
269
270 code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
271 if (code < 0)
272 return code;
273
274 code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
275 if (code < 0)
276 return code;
277
278 return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
279 }
280
281 /* This routine sets up most of the text params structure. In particular it
282 * creates and initialises the x and y widths arrays, and for type 3 fonts
283 * it creates and populates the 'chars' member.
284 * It also sets the delta_sace member and partially set sup the 'operation'
285 * bitfield. It does not set any of the TEXT_DO_* fields because we intend
286 * to use this routine to set up the 'common' parts of the structure and
287 * then we will twiddle the 'TEXT_DO_*' fields as required for the type of
288 * operation we are doing (fill, create path for stroke, create path for fill)
289 */
290 static int pdfi_show_set_params(pdf_context *ctx, pdf_string *s, gs_text_params_t *text)
291 {
292 pdf_font *current_font = NULL;
293 gs_matrix mat;
294 float *x_widths = NULL, *y_widths = NULL, width;
295 double Tw = 0, Tc = 0;
296 int i, code;
297
298 text->data.chars = NULL;
299 text->x_widths = NULL;
300 text->y_widths = NULL;
301
302 /* NOTE: we don't scale the FontMatrix we leave it as the default
303 * and do all our scaling with the textmatrix/ctm. This saves having
304 * to create multiple instances of the same font, and simplifies
305 * composite fonts significantly.
306 */
307 current_font = pdfi_get_current_pdf_font(ctx);
308
309 if (current_font == NULL)
310 return_error(gs_error_invalidfont);
311
312 /* Division by PDFfontsize because these are in unscaled font units,
313 and the font scale is now pickled into the text matrix, so we have to
314 undo that.
315 */
316 Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
317
318 if (current_font->pdfi_font_type == e_pdf_font_type1 ||
319 current_font->pdfi_font_type == e_pdf_font_cff ||
320 current_font->pdfi_font_type == e_pdf_font_type3 ||
321 current_font->pdfi_font_type == e_pdf_font_cff ||
322 current_font->pdfi_font_type == e_pdf_font_truetype ||
323 current_font->pdfi_font_type == e_pdf_font_microtype ||
324 current_font->pdfi_font_type == e_pdf_font_type0)
325 {
326 /* For Type 0 fonts, we apply the DW/W/DW2/W2 values when we retrieve the metrics for
327 setcachedevice - see pdfi_fapi_set_cache()
328 */
329 if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
330 text->operation = TEXT_RETURN_WIDTH;
331 if (Tc != 0) {
332 text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
333 if (current_font->pfont && current_font->pfont->WMode == 0) {
334 text->delta_all.x = Tc;
335 text->delta_all.y = 0;
336 } else {
337 text->delta_all.y = Tc;
338 text->delta_all.x = 0;
339 }
340 }
341 } else {
342 gs_point pt;
343
344 x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
345 y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
346 if (x_widths == NULL || y_widths == NULL) {
347 code = gs_note_error(gs_error_VMerror);
348 goto text_params_error;
349 }
350
351 memset(x_widths, 0x00, s->length * sizeof(float));
352 memset(y_widths, 0x00, s->length * sizeof(float));
353
354 /* To calculate the Width (which is defined in unscaled text units) as a value in user
355 * space we need to transform it by the FontMatrix
356 */
357 mat = current_font->pfont->FontMatrix;
358
359 for (i = 0;i < s->length; i++) {
360 /* Get the width (in unscaled text units) */
361 if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
362 width = current_font->MissingWidth;
363 else
364 width = current_font->Widths[s->data[i] - current_font->FirstChar];
365 /* And convert the width into an appropriate value for the current environment */
366 gs_distance_transform(width, 0, &mat, &pt);
367 x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
368 /* Add any Tc value */
369 x_widths[i] += Tc;
370 }
371 text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
372 text->x_widths = x_widths;
373 text->y_widths = y_widths;
374 text->widths_size = s->length * 2;
375 }
376
377 Tw = gs_currentwordspacing(ctx->pgs);
378 if (Tw != 0) {
379 text->operation |= TEXT_ADD_TO_SPACE_WIDTH;
380 /* Division by PDFfontsize because these are in unscaled font units,
381 and the font scale is now pickled into the text matrix, so we have to
382 undo that.
383 */
384 if (current_font->pfont && current_font->pfont->WMode == 0) {
385 text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
386 text->delta_space.y = 0;
387 } else {
388 text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
389 text->delta_space.x = 0;
390 }
391 text->space.s_char = 0x20;
392 }
393
394 if (current_font->pdfi_font_type == e_pdf_font_type3) {
395 text->operation |= TEXT_FROM_CHARS;
396 text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
397 if (!text->data.chars) {
398 code = gs_note_error(gs_error_VMerror);
399 goto text_params_error;
400 }
401
402 for (i = 0; i < s->length; i++) {
403 ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
404 }
405 }
406 else {
407 text->operation |= TEXT_FROM_BYTES;
408 text->data.bytes = (const byte *)s->data;
409 }
410 text->size = s->length;
411 }
412 else {
413 code = gs_note_error(gs_error_invalidfont);
414 goto text_params_error;
415 }
416
417 return 0;
418
419 text_params_error:
420 gs_free_object(ctx->memory, x_widths, "X widths array for text");
421 text->x_widths = NULL;
422 gs_free_object(ctx->memory, y_widths, "Y widths array for text");
423 text->y_widths = NULL;
424 gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
425 text->data.chars = NULL;
426 return code;
427 }
428
429 /* These routines actually perform the fill/stroke/clip of the text.
430 * We build up the compound cases by reusing the basic cases which
431 * is why we reset the TEXT_DO_ fields after use.
432 */
433
434 static int pdfi_show_simple(pdf_context *ctx, gs_text_params_t *text)
435 {
436 int code = 0;
437 gs_text_enum_t *penum=NULL, *saved_penum=NULL;
438
439 code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
440 if (code >= 0) {
441 penum->single_byte_space = true;
442 saved_penum = ctx->text.current_enum;
443 ctx->text.current_enum = penum;
444 code = gs_text_process(penum);
445 gs_text_release(ctx->pgs, penum, "pdfi_Tj");
446 ctx->text.current_enum = saved_penum;
447 }
448 return code;
449 }
450
451 /* Mode 0 - fill */
452 static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
453 {
454 int code;
455
456 /* just draw the text */
457 text->operation |= TEXT_DO_DRAW;
458
459 code = pdfi_show_simple(ctx, text);
460
461 text->operation &= ~TEXT_DO_DRAW;
462 return code;
463 }
464
465 /* Mode 1 - stroke */
466 static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
467 {
468 int code;
469 gs_text_enum_t *penum=NULL, *saved_penum=NULL;
470 gs_point end_point, initial_point;
471 gx_device *dev = ctx->pgs->device;
472 int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
473
474 end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
475
476 /* Capture the current position */
477 code = gs_currentpoint(ctx->pgs, &initial_point);
478 if (code < 0)
479 return code;
480
481 /* We don't want to disturb the current path, so do a gsave now
482 * We will grestore back to this point after we have stroked the
483 * text, which will leave any current path unchanged.
484 */
485 code = pdfi_gsave(ctx);
486 if (code < 0)
487 goto Tr1_error;
488
489 /* Start a new path (so our stroke doesn't include any
490 * already extant path in the graphics state)
491 */
492 code = gs_newpath(ctx->pgs);
493 if (code < 0)
494 goto Tr1_error;
495 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
496 if (code < 0)
497 goto Tr1_error;
498
499 /* Don't draw the text, create a path suitable for stroking */
500 text->operation |= TEXT_DO_FALSE_CHARPATH;
501
502 /* Run the text methods to create the path */
503 code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
504 if (code < 0)
505 goto Tr1_error;
506
507 penum->single_byte_space = true;
508 saved_penum = ctx->text.current_enum;
509 ctx->text.current_enum = penum;
510 code = gs_text_process(penum);
511 gs_text_release(ctx->pgs, penum, "pdfi_Tj");
512 ctx->text.current_enum = saved_penum;
513 if (code < 0)
514 goto Tr1_error;
515
516 /* After a stroke operation there is no current point and we need
517 * it to be set as if we had drawn the text. So capture it now
518 * and we will append a 'move' to the current point when we have
519 * finished the stroke.
520 */
521 code = gs_currentpoint(ctx->pgs, &end_point);
522 if (code < 0)
523 goto Tr1_error;
524
525 if (talphabits != galphabits)
526 dev->color_info.anti_alias.graphics_bits = talphabits;
527
528 /* Change to the current stroking colour */
529 gs_swapcolors_quick(ctx->pgs);
530 /* Finally, stroke the actual path */
531 code = gs_stroke(ctx->pgs);
532 /* Switch back to the non-stroke colour */
533 gs_swapcolors_quick(ctx->pgs);
534
535 if (talphabits != galphabits)
536 dev->color_info.anti_alias.graphics_bits = galphabits;
537
538 Tr1_error:
539 /* And grestore back to where we started */
540 (void)pdfi_grestore(ctx);
541 /* If everything went well, then move the current point to the
542 * position we captured at the end of the path creation */
543 if (code >= 0)
544 code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
545
546 text->operation &= ~TEXT_DO_FALSE_CHARPATH;
547 return code;
548 }
549
550 /* Mode 2 - fill then stroke */
551 static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
552 {
553 int code, restart = 0;
554 gs_text_enum_t *penum=NULL, *saved_penum=NULL;
555 gs_point end_point, initial_point;
556 gx_device *dev = ctx->pgs->device;
557 int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
558
559 end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
560
561 /* Capture the current position */
562 code = gs_currentpoint(ctx->pgs, &initial_point);
563 if (code < 0)
564 return code;
565
566 /* We don't want to disturb the current path, so do a gsave now
567 * We will grestore back to this point after we have stroked the
568 * text, which will leave any current path unchanged.
569 */
570 code = pdfi_gsave(ctx);
571 if (code < 0)
572 goto Tr1_error;
573
574 /* Start a new path (so our stroke doesn't include any
575 * already extant path in the graphics state)
576 */
577 code = gs_newpath(ctx->pgs);
578 if (code < 0)
579 goto Tr1_error;
580 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
581 if (code < 0)
582 goto Tr1_error;
583
584 /* Don't draw the text, create a path suitable for stroking */
585 text->operation |= TEXT_DO_FALSE_CHARPATH;
586
587 /* Run the text methods to create the path */
588 code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
589 if (code < 0)
590 goto Tr1_error;
591
592 penum->single_byte_space = true;
593 saved_penum = ctx->text.current_enum;
594 ctx->text.current_enum = penum;
595 code = gs_text_process(penum);
596 gs_text_release(ctx->pgs, penum, "pdfi_Tj");
597 ctx->text.current_enum = saved_penum;
598 if (code < 0)
599 goto Tr1_error;
600
601 /* After a stroke operation there is no current point and we need
602 * it to be set as if we had drawn the text. So capture it now
603 * and we will append a 'move' to the current point when we have
604 * finished the stroke.
605 */
606 code = gs_currentpoint(ctx->pgs, &end_point);
607 if (code < 0)
608 goto Tr1_error;
609 if (talphabits != galphabits)
610 dev->color_info.anti_alias.graphics_bits = talphabits;
611 code = gs_fillstroke(ctx->pgs, &restart);
612 if (talphabits != galphabits)
613 dev->color_info.anti_alias.graphics_bits = galphabits;
614
615 Tr1_error:
616 /* And grestore back to where we started */
617 (void)pdfi_grestore(ctx);
618 /* If everything went well, then move the current point to the
619 * position we captured at the end of the path creation */
620 if (code >= 0)
621 code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
622
623 text->operation &= ~TEXT_DO_FALSE_CHARPATH;
624 return code;
625 }
626
627 static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
628 {
629 int code;
630 gs_text_enum_t *penum=NULL, *saved_penum=NULL;
631
632 /* Don't draw the text */
633 text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3;
634
635 /* Run the text methods to create the path */
636 code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
637 if (code < 0)
638 return code;
639
640 penum->single_byte_space = true;
641 saved_penum = ctx->text.current_enum;
642 ctx->text.current_enum = penum;
643 code = gs_text_process(penum);
644 gs_text_release(ctx->pgs, penum, "pdfi_Tj");
645 ctx->text.current_enum = saved_penum;
646
647 return code;
648 }
649
650 /* Prototype the basic 'clip' function, as the following routines will all use it */
651 static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text);
652
653 static int pdfi_show_Tr_4(pdf_context *ctx, gs_text_params_t *text)
654 {
655 int code;
656 gs_point initial_point;
657
658 /* Capture the current position */
659 code = gs_currentpoint(ctx->pgs, &initial_point);
660 if (code < 0)
661 return code;
662
663 ctx->text.TextClip = true;
664 /* First fill the text */
665 code = pdfi_show_Tr_0(ctx, text);
666 if (code < 0)
667 return code;
668
669 /* Return the current point to the initial point */
670 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
671 if (code >= 0)
672 /* And add the text to the acumulated path for clipping */
673 code = pdfi_show_Tr_7(ctx, text);
674
675 return code;
676 }
677
678 static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
679 {
680 int code;
681 gs_point initial_point;
682
683 /* Capture the current position */
684 code = gs_currentpoint(ctx->pgs, &initial_point);
685 if (code < 0)
686 return code;
687
688 ctx->text.TextClip = true;
689 /* First stroke the text */
690 code = pdfi_show_Tr_1(ctx, text);
691 if (code < 0)
692 return code;
693
694 /* Return the current point to the initial point */
695 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
696 if (code >= 0)
697 /* And add the text to the acumulated path for clipping */
698 code = pdfi_show_Tr_7(ctx, text);
699
700 return code;
701 }
702
703 static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
704 {
705 int code;
706 gs_point initial_point;
707
708 /* Capture the current position */
709 code = gs_currentpoint(ctx->pgs, &initial_point);
710 if (code < 0)
711 return code;
712
713 ctx->text.TextClip = true;
714 /* First fill and stroke the text */
715 code = pdfi_show_Tr_2(ctx, text);
716 if (code < 0)
717 return code;
718
719 /* Return the current point to the initial point */
720 code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
721 if (code >= 0)
722 /* And add the text to the acumulated path for clipping */
723 code = pdfi_show_Tr_7(ctx, text);
724
725 return code;
726 }
727
728 static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
729 {
730 int code;
731 gs_text_enum_t *penum=NULL, *saved_penum=NULL;
732
733 ctx->text.TextClip = true;
734 /* Don't draw the text, create a path suitable for filling */
735 text->operation |= TEXT_DO_TRUE_CHARPATH;
736
737 /* Run the text methods to create the path */
738 code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
739 if (code < 0)
740 goto Tr7_error;
741
742 penum->single_byte_space = true;
743 saved_penum = ctx->text.current_enum;
744 ctx->text.current_enum = penum;
745 code = gs_text_process(penum);
746 gs_text_release(ctx->pgs, penum, "pdfi_Tj");
747 ctx->text.current_enum = saved_penum;
748
749 Tr7_error:
750 text->operation &= ~TEXT_DO_TRUE_CHARPATH;
751 return code;
752 }
753
754 static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
755 {
756 int Trmode = 0, code;
757 pdf_font *current_font = NULL;
758 gs_point initial_point;
759
760 current_font = pdfi_get_current_pdf_font(ctx);
761 if (current_font == NULL)
762 return_error(gs_error_invalidfont);
763
764 /* Capture the current position, in case we need it for clipping */
765 code = gs_currentpoint(ctx->pgs, &initial_point);
766
767 Trmode = gs_currenttextrenderingmode(ctx->pgs);
768 if (Trmode == 3) {
769 if (current_font->pdfi_font_type == e_pdf_font_type3)
770 text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
771 else
772 text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
773 }
774 else
775 text->operation |= TEXT_RETURN_WIDTH | TEXT_DO_DRAW;
776
777 /* If we're preserving the text rendering mode, then we don't run a separate
778 * stroke operation, we do effectively run a fill. The fill setup loads the
779 * fill device colour whch, for patterns, creates the pattern tile.
780 * But, because we never load the stroke colour into a device colour, the
781 * pattern tile never gets loaded, so we get an error trying to draw the
782 * text.
783 * We could load the stroke colour in gs_text_begin, but that's potentially
784 * wasteful given that most of the time we won't be using the stroke colour
785 * os I've chosen to load the stroke device colour explicitly here.
786 * But, obviously, only when preserving a stroking text rendering mode.
787 */
788 if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
789 gs_swapcolors_quick(ctx->pgs);
790
791 code = gx_set_dev_color(ctx->pgs);
792 if (code != 0)
793 return code;
794
795 code = gs_gstate_color_load(ctx->pgs);
796 if (code < 0)
797 return code;
798
799 gs_swapcolors_quick(ctx->pgs);
800 }
801
802 /* If we've switched to aemitting text with a 'clip' rendering mode then we
803 * tell pdfwrite that here, so that it can emit a 'q', which it can later
804 * (see pdfi_op_Q) restore to with a Q. It's the only way to get the clipping
805 * right.
806 */
807 if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
808 gx_device *dev = gs_currentdevice_inline(ctx->pgs);
809
810 ctx->text.TextClip = true;
811 code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
812 if (code < 0 && code != gs_error_undefined)
813 return code;
814 }
815
816 code = pdfi_show_simple(ctx, text);
817 return code;
818 }
819
820 static int pdfi_show(pdf_context *ctx, pdf_string *s)
821 {
822 int code = 0, SavedTextDepth = 0;
823 int code1 = 0;
824 gs_text_params_t text;
825 pdf_font *current_font = NULL;
826 int Trmode = 0;
827 int initial_gsave_level = ctx->pgs->level;
828 pdfi_trans_state_t state;
829 int outside_text_block = 0;
830
831 if (ctx->text.BlockDepth == 0) {
832 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
833 outside_text_block = 1;
834 }
835
836 if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
837 || hypot(ctx->pgs->ctm.yy, ctx->pgs->ctm.yx) == 0.0) {
838 /* This can mean a font scaled to 0, which appears to be valid, or a degenerate
839 * ctm/text matrix, which isn't. A degenrate matrix will have triggered a warning
840 * before, so we just skip the operation here.
841 */
842 return 0;
843 }
844
845 current_font = pdfi_get_current_pdf_font(ctx);
846 if (current_font == NULL)
847 return_error(gs_error_invalidfont);
848
849 code = pdfi_show_set_params(ctx, s, &text);
850 if (code < 0)
851 goto show_error;
852
853 /* <sigh> Ugliness......
854 * It seems that transparency can require that we set a transparency Group
855 * during the course of a text operation, and that Group can, of course,
856 * execute operations which are barred during text operations (because
857 * the Group doesn't affect the text as such). So we need to not throw
858 * errors if that happens. Do that by just setting the BlockDepth to 0.
859 */
860 SavedTextDepth = ctx->text.BlockDepth;
861 ctx->text.BlockDepth = 0;
862 code = pdfi_trans_setup_text(ctx, &state, true);
863 ctx->text.BlockDepth = SavedTextDepth;
864
865 if (code >= 0) {
866 if (ctx->device_state.preserve_tr_mode) {
867 code = pdfi_show_Tr_preserve(ctx, &text);
868 } else {
869 Trmode = gs_currenttextrenderingmode(ctx->pgs);
870
871 /* The spec says that we ignore text rendering modes other than 0 for
872 * type 3 fonts, but Acrobat also honours mode 3 (do nothing)
873 */
874 if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
875 Trmode = 0;
876
877 switch(Trmode) {
878 case 0:
879 code = pdfi_show_Tr_0(ctx, &text);
880 break;
881 case 1:
882 code = pdfi_show_Tr_1(ctx, &text);
883 break;
884 case 2:
885 code = pdfi_show_Tr_2(ctx, &text);
886 break;
887 case 3:
888 code = pdfi_show_Tr_3(ctx, &text);
889 break;
890 case 4:
891 code = pdfi_show_Tr_4(ctx, &text);
892 break;
893 case 5:
894 code = pdfi_show_Tr_5(ctx, &text);
895 break;
896 case 6:
897 code = pdfi_show_Tr_6(ctx, &text);
898 break;
899 case 7:
900 code = pdfi_show_Tr_7(ctx, &text);
901 break;
902 default:
903 break;
904 }
905 }
906 code1 = pdfi_trans_teardown_text(ctx, &state);
907 if (code == 0)
908 code = code1;
909 }
910
911 /* We shouldn't need to do this, but..... It turns out that if we have text rendering mode 3 set
912 * then gs_text_begin will execute a gsave, push the nulldevice and alter the saved gsave level.
913 * If we then get an error while processing the text we don't gs_restore enough times which
914 * leaves the nulldevice as the current device, and eats all the following content!
915 * To avoid that, we save the gsave depth at the start of this routine, and grestore enough
916 * times to get back to the same level. I think this is actually a bug in the graphics library
917 * but it doesn't get exposed in normal usage so we'll just work around it here.
918 */
919 while(ctx->pgs->level > initial_gsave_level)
920 gs_grestore(ctx->pgs);
921
922 /* If we were outside a text block when we started this function, then we effectively started
923 * one by calling the show mechanism. Among other things, this can have pushed a transparency
924 * group. We need to ensure that this is properly closed, so do the guts of the 'ET' operator
925 * here (without adjusting the blockDepth, or giving more warnings). See Bug 707753. */
926 if (outside_text_block) {
927 code1 = do_ET(ctx);
928 if (code == 0)
929 code = code1;
930 }
931
932 show_error:
933 if ((void *)text.data.chars != (void *)s->data)
934 gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
935
936 gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
937 gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
938 return code;
939 }
940
941 /* NOTE: the bounding box this generates has llx and lly at 0,0,
942 so is arguably not a "real" bounding box
943 */
944 int pdfi_string_bbox(pdf_context *ctx, pdf_string *s, gs_rect *bboxout, gs_point *advance_width, bool for_stroke)
945 {
946 int code = 0;
947 gx_device_bbox *bbdev;
948 pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
949 gs_matrix tmpmat, Trm, Trm_ctm;
950 gs_point cppt, startpt;
951
952 if (current_font == NULL)
953 return_error(gs_error_invalidfont);
954
955
956 for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
957
958 if (ctx->devbbox == NULL) {
959 bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
960 if (bbdev == NULL)
961 return_error(gs_error_VMerror);
962 gx_device_bbox_init(bbdev, NULL, ctx->memory);
963 ctx->devbbox = (gx_device *)bbdev;
964 rc_increment(ctx->devbbox);
965 }
966 else {
967 bbdev = (gx_device_bbox *)ctx->devbbox;
968 }
969 gx_device_retain((gx_device *)bbdev, true);
970 gx_device_bbox_set_white_opaque(bbdev, true);
971
972 code = pdfi_gsave(ctx);
973 if (code < 0) {
974 gx_device_retain((gx_device *)bbdev, false);
975 return code;
976 }
977 /* The bbox device needs a fairly high resolution to get accurate results */
978 gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
979
980 code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
981 if (code < 0)
982 goto out;
983
984 Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
985 Trm.xy = 0;
986 Trm.yx = 0;
987 Trm.yy = ctx->pgs->PDFfontsize;
988 Trm.tx = 0;
989 Trm.ty = ctx->pgs->textrise;
990
991 memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
992 /* We want to avoid any translations unrelated to the extents of the text */
993 tmpmat.tx = tmpmat.ty = 0;
994 gs_matrix_multiply(&Trm, &tmpmat, &Trm);
995
996 memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
997 /* As above */
998 tmpmat.tx = tmpmat.ty = 0;
999 gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
1000 gs_setmatrix(ctx->pgs, &Trm_ctm);
1001
1002 gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
1003
1004 code = pdfi_gs_setgray(ctx, 1.0);
1005 if (code < 0)
1006 goto out;
1007
1008 /* The bbox device (not surprisingly) clips to the device width/height
1009 so we have to offset our initial point to cope with glyphs that include
1010 negative coordinates. Most significantly, descender features - hence the
1011 larger y offset.
1012 The offsets are guesses.
1013 */
1014 startpt.x = ctx->pgs->PDFfontsize;
1015 startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1016 code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1017 if (code < 0)
1018 goto out;
1019
1020 /* Pretend to have at least one BT or pdfi_show will generate a spurious warning */
1021 ctx->text.BlockDepth++;
1022 code = pdfi_show(ctx, s);
1023 /* And an ET */
1024 ctx->text.BlockDepth--;
1025 if (code < 0)
1026 goto out;
1027
1028 code = gx_device_bbox_bbox(bbdev, bboxout);
1029 if (code < 0)
1030 goto out;
1031
1032 bboxout->q.x -= bboxout->p.x;
1033 bboxout->q.y -= bboxout->p.y;
1034 bboxout->p.x = bboxout->p.y = 0;
1035
1036 code = gs_currentpoint(ctx->pgs, &cppt);
1037 if (code >= 0) {
1038 code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1039 if (code < 0)
1040 goto out;
1041 advance_width->x = ctx->pgs->current_point.x - startpt.x;
1042 advance_width->y = ctx->pgs->current_point.y - startpt.y;
1043 code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1044 }
1045 out:
1046 pdfi_grestore(ctx);
1047 (void)gs_closedevice((gx_device *)bbdev);
1048 gx_device_retain((gx_device *)bbdev, false);
1049
1050 return code;
1051 }
1052
1053 int pdfi_Tj(pdf_context *ctx)
1054 {
1055 int code = 0;
1056 pdf_string *s = NULL;
1057 gs_matrix saved, Trm;
1058 gs_point initial_point, current_point, pt;
1059 double linewidth = ctx->pgs->line_params.half_width;
1060 int initial_point_valid;
1061
1062 if (pdfi_count_stack(ctx) < 1)
1063 return_error(gs_error_stackunderflow);
1064
1065 if (pdfi_oc_is_off(ctx))
1066 goto exit;
1067
1068 s = (pdf_string *)ctx->stack_top[-1];
1069 if (pdfi_type_of(s) != PDF_STRING) {
1070 pdfi_pop(ctx, 1);
1071 return_error(gs_error_typecheck);
1072 }
1073
1074 /* We can't rely on the stack reference because an error during
1075 the text operation (i.e. retrieving objects for glyph metrics
1076 may cause the stack to be cleared.
1077 */
1078 pdfi_countup(s);
1079 pdfi_pop(ctx, 1);
1080
1081 /* Save the CTM for later restoration */
1082 saved = ctm_only(ctx->pgs);
1083 initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1084
1085 Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1086 Trm.xy = 0;
1087 Trm.yx = 0;
1088 Trm.yy = ctx->pgs->PDFfontsize;
1089 Trm.tx = 0;
1090 Trm.ty = ctx->pgs->textrise;
1091
1092 code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1093 if (code < 0)
1094 goto exit;
1095
1096 if (!ctx->device_state.preserve_tr_mode) {
1097 code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1098 if (code < 0)
1099 goto exit;
1100 ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1101 } else {
1102 /* We have to adjust the stroke width for pdfwrite so that we take into
1103 * account the CTM, but we do not spply the font scaling. Because of
1104 * the disconnect between pdfwrite and the interpreter, we also have to
1105 * remove the scaling due to the resolution.
1106 */
1107 gs_matrix devmatrix, matrix;
1108 gx_device *device = gs_currentdevice(ctx->pgs);
1109
1110 devmatrix.xx = 72.0 / device->HWResolution[0];
1111 devmatrix.xy = 0;
1112 devmatrix.yx = 0;
1113 devmatrix.yy = 72.0 / device->HWResolution[1];
1114 devmatrix.tx = 0;
1115 devmatrix.ty = 0;
1116
1117 code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1118 if (code < 0)
1119 goto exit;
1120
1121 code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1122 if (code < 0)
1123 goto exit;
1124 ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1125 }
1126
1127 code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1128 if (code < 0)
1129 goto exit;
1130
1131 code = gs_setmatrix(ctx->pgs, &Trm);
1132 if (code < 0)
1133 goto exit;
1134
1135 code = gs_moveto(ctx->pgs, 0, 0);
1136 if (code < 0)
1137 goto Tj_error;
1138
1139 code = pdfi_show(ctx, s);
1140 if (code < 0)
1141 goto Tj_error;
1142
1143 ctx->pgs->line_params.half_width = linewidth;
1144 /* Update the Text matrix with the current point, for the next operation
1145 */
1146 (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1147 Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1148 Trm.xy = 0;
1149 Trm.yx = 0;
1150 Trm.yy = ctx->pgs->PDFfontsize;
1151 Trm.tx = 0;
1152 Trm.ty = 0;
1153 code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1154 if (code < 0)
1155 goto Tj_error;
1156
1157 code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1158 if (code < 0)
1159 goto Tj_error;
1160 ctx->pgs->textmatrix.tx += pt.x;
1161 ctx->pgs->textmatrix.ty += pt.y;
1162
1163 Tj_error:
1164 /* Restore the CTM to the saved value */
1165 (void)gs_setmatrix(ctx->pgs, &saved);
1166 /* And restore the currentpoint */
1167 if (initial_point_valid)
1168 (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1169 else
1170 code = gs_newpath(ctx->pgs);
1171 /* And the line width */
1172 ctx->pgs->line_params.half_width = linewidth;
1173
1174 exit:
1175 pdfi_countdown(s);
1176 return code;
1177 }
1178
1179 int pdfi_TJ(pdf_context *ctx)
1180 {
1181 int code = 0, i;
1182 pdf_array *a = NULL;
1183 pdf_obj *o = NULL;
1184 double dx = 0;
1185 gs_point pt;
1186 gs_matrix saved, Trm;
1187 gs_point initial_point, current_point;
1188 double linewidth = ctx->pgs->line_params.half_width;
1189 pdf_font *current_font = NULL;
1190 int initial_point_valid;
1191
1192 current_font = pdfi_get_current_pdf_font(ctx);
1193 if (current_font == NULL)
1194 return_error(gs_error_invalidfont);
1195
1196 if (ctx->text.BlockDepth == 0) {
1197 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1198 }
1199
1200 if (pdfi_count_stack(ctx) < 1)
1201 return_error(gs_error_stackunderflow);
1202
1203 if (pdfi_oc_is_off(ctx))
1204 goto exit;
1205
1206 a = (pdf_array *)ctx->stack_top[-1];
1207 if (pdfi_type_of(a) != PDF_ARRAY) {
1208 pdfi_pop(ctx, 1);
1209 return gs_note_error(gs_error_typecheck);
1210 }
1211 pdfi_countup(a);
1212 pdfi_pop(ctx, 1);
1213
1214 /* Save the CTM for later restoration */
1215 saved = ctm_only(ctx->pgs);
1216 ctx->text.initial_current_point_valid = initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1217
1218 /* Calculate the text rendering matrix, see section 1.7 PDF Reference
1219 * page 409, section 5.3.3 Text Space details.
1220 */
1221 Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1222 Trm.xy = 0;
1223 Trm.yx = 0;
1224 Trm.yy = ctx->pgs->PDFfontsize;
1225 Trm.tx = 0;
1226 Trm.ty = ctx->pgs->textrise;
1227
1228 code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1229 if (code < 0)
1230 goto exit;
1231
1232 if (!ctx->device_state.preserve_tr_mode) {
1233 code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1234 if (code < 0)
1235 goto exit;
1236 ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1237 } else {
1238 /* We have to adjust the stroke width for pdfwrite so that we take into
1239 * account the CTM, but we do not spply the font scaling. Because of
1240 * the disconnect between pdfwrite and the interpreter, we also have to
1241 * remove the scaling due to the resolution.
1242 */
1243 gs_matrix devmatrix, matrix;
1244 gx_device *device = gs_currentdevice(ctx->pgs);
1245
1246 devmatrix.xx = 72.0 / device->HWResolution[0];
1247 devmatrix.xy = 0;
1248 devmatrix.yx = 0;
1249 devmatrix.yy = 72.0 / device->HWResolution[1];
1250 devmatrix.tx = 0;
1251 devmatrix.ty = 0;
1252
1253 code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1254 if (code < 0)
1255 goto exit;
1256
1257 code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1258 if (code < 0)
1259 goto exit;
1260 ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1261 }
1262
1263 code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1264 if (code < 0)
1265 goto exit;
1266 code = gs_setmatrix(ctx->pgs, &Trm);
1267 if (code < 0)
1268 goto TJ_error;
1269
1270 code = gs_moveto(ctx->pgs, 0, 0);
1271 if (code < 0)
1272 goto TJ_error;
1273
1274 for (i = 0; i < pdfi_array_size(a); i++) {
1275 code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1276 if (code < 0)
1277 goto TJ_error;
1278
1279 if (pdfi_type_of(o) == PDF_STRING)
1280 code = pdfi_show(ctx, (pdf_string *)o);
1281 else {
1282 code = pdfi_obj_to_real(ctx, o, &dx);
1283 if (code < 0)
1284 goto TJ_error;
1285 dx /= -1000;
1286 if (current_font->pfont && current_font->pfont->WMode == 0)
1287 code = gs_rmoveto(ctx->pgs, dx, 0);
1288 else
1289 code = gs_rmoveto(ctx->pgs, 0, dx);
1290 }
1291 pdfi_countdown(o);
1292 o = NULL;
1293 if (code < 0)
1294 goto TJ_error;
1295 }
1296
1297 /* We don't want to update the Text matrix if we aren't in a text block, there
1298 * is no point because the matrix isn't persistent between text blocks.
1299 */
1300 if (ctx->text.BlockDepth > 0) {
1301 /* Update the Text matrix with the current point, for the next operation
1302 */
1303 (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1304 Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1305 Trm.xy = 0;
1306 Trm.yx = 0;
1307 Trm.yy = ctx->pgs->PDFfontsize;
1308 Trm.tx = 0;
1309 Trm.ty = 0;
1310 code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1311 if (code < 0)
1312 goto TJ_error;
1313
1314 code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1315 if (code < 0)
1316 goto TJ_error;
1317 ctx->pgs->textmatrix.tx += pt.x;
1318 ctx->pgs->textmatrix.ty += pt.y;
1319 }
1320 TJ_error:
1321 pdfi_countdown(o);
1322 /* Restore the CTM to the saved value */
1323 (void)gs_setmatrix(ctx->pgs, &saved);
1324 /* And restore the currentpoint */
1325 if (initial_point_valid)
1326 (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1327 else
1328 code = gs_newpath(ctx->pgs);
1329 /* And the line width */
1330 ctx->pgs->line_params.half_width = linewidth;
1331
1332 exit:
1333 pdfi_countdown(a);
1334 return code;
1335 }
1336
1337 static int pdfi_set_TL(pdf_context *ctx, double TL)
1338 {
1339 return gs_settextleading(ctx->pgs, TL);
1340 }
1341
1342 int pdfi_TL(pdf_context *ctx)
1343 {
1344 int code;
1345 double d;
1346
1347 code = pdfi_destack_real(ctx, &d);
1348 if (code < 0)
1349 return code;
1350
1351 return pdfi_set_TL(ctx, -d);
1352 }
1353
1354 int pdfi_Tm(pdf_context *ctx)
1355 {
1356 int code;
1357 float m[6];
1358 gs_matrix mat;
1359
1360 code = pdfi_destack_floats(ctx, m, 6);
1361 if (code < 0)
1362 return code;
1363
1364 if (ctx->text.BlockDepth == 0) {
1365 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1366
1367 gs_make_identity(&mat);
1368 code = gs_settextmatrix(ctx->pgs, &mat);
1369 if (code < 0)
1370 return code;
1371
1372 code = gs_settextlinematrix(ctx->pgs, &mat);
1373 if (code < 0)
1374 return code;
1375 }
1376
1377 if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1378 pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1379
1380 code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1381 if (code < 0)
1382 return code;
1383
1384 return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1385 }
1386
1387 int pdfi_Tr(pdf_context *ctx)
1388 {
1389 int code;
1390 int64_t mode;
1391
1392 code = pdfi_destack_int(ctx, &mode);
1393 if (code < 0)
1394 return code;
1395
1396 if (mode < 0 || mode > 7)
1397 return_error(gs_error_rangecheck);
1398
1399 /* Detect attempts to switch from a clipping mode to a non-clipping
1400 * mode, this is defined as invalid in the spec. (We don't warn if we haven't yet
1401 * drawn any text in the clipping mode).
1402 */
1403 if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1404 pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1405
1406 if (ctx->device_state.preserve_tr_mode) {
1407 gs_settextrenderingmode(ctx->pgs, mode);
1408 } else
1409 {
1410 gs_point initial_point;
1411
1412 if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1413 gs_settextrenderingmode(ctx->pgs, mode);
1414 /* Capture the current position */
1415 code = gs_currentpoint(ctx->pgs, &initial_point);
1416 /* Start a new path (so our clip doesn't include any
1417 * already extant path in the graphics state)
1418 */
1419 gs_newpath(ctx->pgs);
1420 if (code >= 0)
1421 gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1422 code = 0;
1423 } else if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && mode < 4 && ctx->text.BlockDepth != 0) {
1424 /* If we are switching from a clipping mode to a non-clipping
1425 * mode then behave as if we had an implicit ET to flush the
1426 * accumulated text to a clip, then set the text rendering mode
1427 * to the non-clip mode, and perform an implicit BT.
1428 */
1429 code = pdfi_ET(ctx);
1430 if (code < 0)
1431 return code;
1432 gs_settextrenderingmode(ctx->pgs, mode);
1433 code = pdfi_BT(ctx);
1434 if (code < 0)
1435 return code;
1436 }
1437 else
1438 gs_settextrenderingmode(ctx->pgs, mode);
1439 }
1440 return code;
1441 }
1442
1443 static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1444 {
1445 return gs_settextrise(ctx->pgs, Ts);
1446 }
1447
1448 int pdfi_Ts(pdf_context *ctx)
1449 {
1450 int code;
1451 double d;
1452
1453 code = pdfi_destack_real(ctx, &d);
1454 if (code < 0)
1455 return code;
1456
1457 return pdfi_set_Ts(ctx, d);
1458 }
1459
1460 static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1461 {
1462 return gs_setwordspacing(ctx->pgs, Tw);
1463 }
1464
1465 int pdfi_Tw(pdf_context *ctx)
1466 {
1467 int code;
1468 double d;
1469
1470 code = pdfi_destack_real(ctx, &d);
1471 if (code < 0)
1472 return code;
1473
1474 return pdfi_set_Tw(ctx, d);
1475 }
1476
1477 int pdfi_Tz(pdf_context *ctx)
1478 {
1479 int code;
1480 double d;
1481
1482 code = pdfi_destack_real(ctx, &d);
1483 if (code < 0)
1484 return code;
1485
1486 return gs_settexthscaling(ctx->pgs, d);
1487 }
1488
1489 int pdfi_singlequote(pdf_context *ctx)
1490 {
1491 int code;
1492
1493 if (ctx->text.BlockDepth == 0) {
1494 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1495 }
1496
1497 code = pdfi_T_star(ctx);
1498 if (code < 0)
1499 return code;
1500
1501 return pdfi_Tj(ctx);
1502 }
1503
1504 int pdfi_doublequote(pdf_context *ctx)
1505 {
1506 int code;
1507 double Tw, Tc;
1508
1509 if (ctx->text.BlockDepth == 0) {
1510 pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1511 }
1512
1513 if (pdfi_count_stack(ctx) < 3) {
1514 pdfi_clearstack(ctx);
1515 return_error(gs_error_stackunderflow);
1516 }
1517
1518 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1519 pdfi_pop(ctx, 3);
1520 return gs_note_error(gs_error_typecheck);
1521 }
1522
1523 code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1524 if (code < 0)
1525 goto error;
1526 code = pdfi_set_Tc(ctx, Tc);
1527 if (code < 0)
1528 goto error;
1529
1530 code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1531 if (code < 0)
1532 goto error;
1533 code = pdfi_set_Tw(ctx, Tw);
1534 if (code < 0)
1535 goto error;
1536
1537 code = pdfi_T_star(ctx);
1538 if (code < 0)
1539 goto error;
1540
1541 code = pdfi_Tj(ctx);
1542 /* Tj pops one off the stack for us, leaving us 2 to go. */
1543 pdfi_pop(ctx, 2);
1544 return code;
1545
1546 error:
1547 pdfi_pop(ctx, 3);
1548 return code;
1549 }