/src/ghostpdl/pdf/pdf_text.c
Line | Count | Source |
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 | 2.13M | { |
41 | 2.13M | int code; |
42 | 2.13M | gs_matrix m; |
43 | 2.13M | bool illegal_BT = false; |
44 | | |
45 | 2.13M | if (ctx->text.BlockDepth != 0) { |
46 | 44.4k | illegal_BT = true; |
47 | 44.4k | if (ctx->text.TextClip) { |
48 | 122 | gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
49 | | |
50 | 122 | ctx->text.TextClip = false; |
51 | 122 | (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
52 | 122 | } |
53 | 44.4k | code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL); |
54 | 44.4k | if (code < 0) |
55 | 0 | return code; |
56 | 44.4k | } |
57 | | |
58 | 2.13M | gs_make_identity(&m); |
59 | 2.13M | code = gs_settextmatrix(ctx->pgs, &m); |
60 | 2.13M | if (code < 0) |
61 | 0 | return code; |
62 | | |
63 | 2.13M | code = gs_settextlinematrix(ctx->pgs, &m); |
64 | 2.13M | if (code < 0) |
65 | 0 | 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 | 2.13M | 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 | 134 | gs_newpath(ctx->pgs); |
75 | 134 | } |
76 | | |
77 | 2.13M | ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid; |
78 | 2.13M | if (!ctx->pgs->current_point_valid) |
79 | 1.76M | code = gs_moveto(ctx->pgs, 0, 0); |
80 | | |
81 | 2.13M | ctx->text.BlockDepth++; |
82 | | |
83 | 2.13M | if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT) |
84 | 445k | gs_begin_transparency_text_group(ctx->pgs); |
85 | | |
86 | 2.13M | return code; |
87 | 2.13M | } |
88 | | |
89 | | static int do_ET(pdf_context *ctx) |
90 | 2.07M | { |
91 | 2.07M | int code = 0; |
92 | 2.07M | 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 | 2.07M | if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) { |
105 | 943 | gs_point initial_point; |
106 | | |
107 | 943 | if (!ctx->device_state.preserve_tr_mode) { |
108 | 924 | ctx->text.TextClip = false; |
109 | | /* Capture the current position */ |
110 | 924 | code = gs_currentpoint(ctx->pgs, &initial_point); |
111 | 924 | if (code >= 0 || code == gs_error_nocurrentpoint) { |
112 | 922 | gs_point adjust; |
113 | 922 | bool nocurrentpoint = code >= 0 ? false : true; |
114 | | |
115 | 922 | gs_currentfilladjust(ctx->pgs, &adjust); |
116 | 922 | code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0); |
117 | 922 | if (code < 0) |
118 | 0 | return code; |
119 | | |
120 | 922 | code = gs_clip(ctx->pgs); |
121 | 922 | if (code >= 0) |
122 | 922 | copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path"); |
123 | | |
124 | 922 | code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y); |
125 | 922 | if (code < 0) |
126 | 0 | return code; |
127 | | |
128 | 922 | if (copy != NULL) |
129 | 922 | (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy); |
130 | | |
131 | 922 | if (nocurrentpoint == false) |
132 | 922 | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
133 | 922 | } |
134 | 924 | } |
135 | 943 | } |
136 | 2.07M | if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs)) |
137 | 450k | gs_end_transparency_text_group(ctx->pgs); |
138 | | |
139 | 2.07M | if (!ctx->text.initial_current_point_valid) |
140 | 1.13M | gs_newpath(ctx->pgs); |
141 | 2.07M | return code; |
142 | 2.07M | } |
143 | | |
144 | | int pdfi_ET(pdf_context *ctx) |
145 | 2.03M | { |
146 | 2.03M | if (ctx->text.BlockDepth == 0) { |
147 | 79.5k | int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL); |
148 | 79.5k | return code; |
149 | 79.5k | } |
150 | | |
151 | 1.95M | ctx->text.BlockDepth--; |
152 | | |
153 | 1.95M | return do_ET(ctx); |
154 | 2.03M | } |
155 | | |
156 | | int pdfi_T_star(pdf_context *ctx) |
157 | 164k | { |
158 | 164k | int code; |
159 | 164k | gs_matrix m, mat; |
160 | | |
161 | 164k | if (ctx->text.BlockDepth == 0) { |
162 | 37.7k | int code; |
163 | 37.7k | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_TEXTOPNOBT, "pdfi_T_star", NULL)) < 0) |
164 | 0 | return code; |
165 | 37.7k | } |
166 | | |
167 | 164k | gs_make_identity(&m); |
168 | 164k | m.ty += ctx->pgs->textleading; |
169 | | |
170 | 164k | code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat); |
171 | 164k | if (code < 0) |
172 | 0 | return code; |
173 | | |
174 | 164k | code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat); |
175 | 164k | if (code < 0) |
176 | 0 | return code; |
177 | | |
178 | 164k | code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat); |
179 | 164k | return code; |
180 | 164k | } |
181 | | |
182 | | static int pdfi_set_Tc(pdf_context *ctx, double Tc) |
183 | 733k | { |
184 | 733k | return gs_settextspacing(ctx->pgs, Tc); |
185 | 733k | } |
186 | | |
187 | | int pdfi_Tc(pdf_context *ctx) |
188 | 748k | { |
189 | 748k | int code; |
190 | 748k | double d; |
191 | | |
192 | 748k | code = pdfi_destack_real(ctx, &d); |
193 | 748k | if (code < 0) |
194 | 15.0k | return code; |
195 | | |
196 | 733k | return pdfi_set_Tc(ctx, d); |
197 | 748k | } |
198 | | |
199 | | int pdfi_Td(pdf_context *ctx) |
200 | 1.33M | { |
201 | 1.33M | int code; |
202 | 1.33M | double Txy[2]; |
203 | 1.33M | gs_matrix m, mat; |
204 | | |
205 | 1.33M | code = pdfi_destack_reals(ctx, Txy, 2); |
206 | 1.33M | if (code < 0) |
207 | 24.6k | return code; |
208 | | |
209 | 1.31M | gs_make_identity(&m); |
210 | | |
211 | 1.31M | m.tx = Txy[0]; |
212 | 1.31M | m.ty = Txy[1]; |
213 | | |
214 | 1.31M | if (ctx->text.BlockDepth == 0) { |
215 | 15.1k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL); |
216 | | |
217 | 15.1k | gs_make_identity(&mat); |
218 | 15.1k | code = gs_settextmatrix(ctx->pgs, &mat); |
219 | 15.1k | if (code < 0) |
220 | 0 | return code; |
221 | | |
222 | 15.1k | code = gs_settextlinematrix(ctx->pgs, &mat); |
223 | 15.1k | if (code < 0) |
224 | 0 | return code; |
225 | 15.1k | } |
226 | | |
227 | 1.31M | code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat); |
228 | 1.31M | if (code < 0) |
229 | 0 | return code; |
230 | | |
231 | 1.31M | code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat); |
232 | 1.31M | if (code < 0) |
233 | 0 | return code; |
234 | | |
235 | 1.31M | return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat); |
236 | 1.31M | } |
237 | | |
238 | | int pdfi_TD(pdf_context *ctx) |
239 | 394k | { |
240 | 394k | int code; |
241 | 394k | double Txy[2]; |
242 | 394k | gs_matrix m, mat; |
243 | | |
244 | 394k | gs_make_identity(&m); |
245 | | |
246 | 394k | code = pdfi_destack_reals(ctx, Txy, 2); |
247 | 394k | if (code < 0) |
248 | 3.14k | return code; |
249 | | |
250 | 391k | m.tx = Txy[0]; |
251 | 391k | m.ty = Txy[1]; |
252 | | |
253 | 391k | if (ctx->text.BlockDepth == 0) { |
254 | 1.68k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL); |
255 | | |
256 | 1.68k | gs_make_identity(&mat); |
257 | 1.68k | code = gs_settextmatrix(ctx->pgs, &mat); |
258 | 1.68k | if (code < 0) |
259 | 0 | return code; |
260 | | |
261 | 1.68k | code = gs_settextlinematrix(ctx->pgs, &mat); |
262 | 1.68k | if (code < 0) |
263 | 0 | return code; |
264 | 1.68k | } |
265 | | |
266 | 391k | code = pdfi_set_TL(ctx, m.ty * 1.0f); |
267 | 391k | if (code < 0) |
268 | 0 | return code; |
269 | | |
270 | 391k | code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat); |
271 | 391k | if (code < 0) |
272 | 0 | return code; |
273 | | |
274 | 391k | code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat); |
275 | 391k | if (code < 0) |
276 | 0 | return code; |
277 | | |
278 | 391k | return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat); |
279 | 391k | } |
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 | 12.3M | { |
292 | 12.3M | pdf_font *current_font = NULL; |
293 | 12.3M | gs_matrix mat; |
294 | 12.3M | float *x_widths = NULL, *y_widths = NULL, width; |
295 | 12.3M | double Tw = 0, Tc = 0; |
296 | 12.3M | int i, code; |
297 | | |
298 | 12.3M | text->data.chars = NULL; |
299 | 12.3M | text->x_widths = NULL; |
300 | 12.3M | 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 | 12.3M | current_font = pdfi_get_current_pdf_font(ctx); |
308 | | |
309 | 12.3M | if (current_font == NULL) |
310 | 0 | 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 | 12.3M | Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize; |
317 | | |
318 | 12.3M | if (current_font->pdfi_font_type == e_pdf_font_type1 || |
319 | 4.31M | current_font->pdfi_font_type == e_pdf_font_cff || |
320 | 2.81M | current_font->pdfi_font_type == e_pdf_font_type3 || |
321 | 2.76M | current_font->pdfi_font_type == e_pdf_font_cff || |
322 | 2.76M | current_font->pdfi_font_type == e_pdf_font_truetype || |
323 | 1.54M | current_font->pdfi_font_type == e_pdf_font_microtype || |
324 | 1.54M | current_font->pdfi_font_type == e_pdf_font_type0) |
325 | 12.3M | { |
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 | 12.3M | if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) { |
330 | 5.41M | text->operation = TEXT_RETURN_WIDTH; |
331 | 5.41M | if (Tc != 0) { |
332 | 1.11M | text->operation |= TEXT_ADD_TO_ALL_WIDTHS; |
333 | 1.11M | if (current_font->pfont && current_font->pfont->WMode == 0) { |
334 | 1.11M | text->delta_all.x = Tc; |
335 | 1.11M | text->delta_all.y = 0; |
336 | 1.11M | } else { |
337 | 3 | text->delta_all.y = Tc; |
338 | 3 | text->delta_all.x = 0; |
339 | 3 | } |
340 | 1.11M | } |
341 | 6.92M | } else { |
342 | 6.92M | gs_point pt; |
343 | | |
344 | 6.92M | x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text"); |
345 | 6.92M | y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text"); |
346 | 6.92M | if (x_widths == NULL || y_widths == NULL) { |
347 | 0 | code = gs_note_error(gs_error_VMerror); |
348 | 0 | goto text_params_error; |
349 | 0 | } |
350 | | |
351 | 6.92M | memset(x_widths, 0x00, s->length * sizeof(float)); |
352 | 6.92M | 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 | 6.92M | mat = current_font->pfont->FontMatrix; |
358 | | |
359 | 30.8M | for (i = 0;i < s->length; i++) { |
360 | | /* Get the width (in unscaled text units) */ |
361 | 23.9M | if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar) |
362 | 101k | width = current_font->MissingWidth; |
363 | 23.8M | else |
364 | 23.8M | width = current_font->Widths[s->data[i] - current_font->FirstChar]; |
365 | | /* And convert the width into an appropriate value for the current environment */ |
366 | 23.9M | gs_distance_transform(width, 0, &mat, &pt); |
367 | 23.9M | x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0); |
368 | | /* Add any Tc value */ |
369 | 23.9M | x_widths[i] += Tc; |
370 | 23.9M | } |
371 | 6.92M | text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS; |
372 | 6.92M | text->x_widths = x_widths; |
373 | 6.92M | text->y_widths = y_widths; |
374 | 6.92M | text->widths_size = s->length * 2; |
375 | 6.92M | } |
376 | | |
377 | 12.3M | Tw = gs_currentwordspacing(ctx->pgs); |
378 | 12.3M | if (Tw != 0) { |
379 | 839k | 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 | 839k | if (current_font->pfont && current_font->pfont->WMode == 0) { |
385 | 839k | text->delta_space.x = Tw / ctx->pgs->PDFfontsize; |
386 | 839k | text->delta_space.y = 0; |
387 | 839k | } else { |
388 | 0 | text->delta_space.y = Tw / ctx->pgs->PDFfontsize; |
389 | 0 | text->delta_space.x = 0; |
390 | 0 | } |
391 | 839k | text->space.s_char = 0x20; |
392 | 839k | } |
393 | | |
394 | 12.3M | if (current_font->pdfi_font_type == e_pdf_font_type3) { |
395 | 49.8k | text->operation |= TEXT_FROM_CHARS; |
396 | 49.8k | text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars"); |
397 | 49.8k | if (!text->data.chars) { |
398 | 0 | code = gs_note_error(gs_error_VMerror); |
399 | 0 | goto text_params_error; |
400 | 0 | } |
401 | | |
402 | 247k | for (i = 0; i < s->length; i++) { |
403 | 197k | ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i]; |
404 | 197k | } |
405 | 49.8k | } |
406 | 12.2M | else { |
407 | 12.2M | text->operation |= TEXT_FROM_BYTES; |
408 | 12.2M | text->data.bytes = (const byte *)s->data; |
409 | 12.2M | } |
410 | 12.3M | text->size = s->length; |
411 | 12.3M | } |
412 | 30 | else { |
413 | 30 | code = gs_note_error(gs_error_invalidfont); |
414 | 30 | goto text_params_error; |
415 | 30 | } |
416 | | |
417 | 12.3M | return 0; |
418 | | |
419 | 30 | text_params_error: |
420 | 30 | gs_free_object(ctx->memory, x_widths, "X widths array for text"); |
421 | 30 | text->x_widths = NULL; |
422 | 30 | gs_free_object(ctx->memory, y_widths, "Y widths array for text"); |
423 | 30 | text->y_widths = NULL; |
424 | 30 | gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars"); |
425 | 30 | text->data.chars = NULL; |
426 | 30 | return code; |
427 | 12.3M | } |
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 | 12.1M | { |
436 | 12.1M | int code = 0; |
437 | 12.1M | gs_text_enum_t *penum=NULL, *saved_penum=NULL; |
438 | | |
439 | 12.1M | code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum); |
440 | 12.1M | if (code >= 0) { |
441 | 12.1M | penum->single_byte_space = true; |
442 | 12.1M | saved_penum = ctx->text.current_enum; |
443 | 12.1M | ctx->text.current_enum = penum; |
444 | 12.1M | code = gs_text_process(penum); |
445 | 12.1M | gs_text_release(ctx->pgs, penum, "pdfi_Tj"); |
446 | 12.1M | ctx->text.current_enum = saved_penum; |
447 | 12.1M | } |
448 | 12.1M | return code; |
449 | 12.1M | } |
450 | | |
451 | | /* Mode 0 - fill */ |
452 | | static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text) |
453 | 10.5M | { |
454 | 10.5M | int code; |
455 | | |
456 | | /* just draw the text */ |
457 | 10.5M | text->operation |= TEXT_DO_DRAW; |
458 | | |
459 | 10.5M | code = pdfi_show_simple(ctx, text); |
460 | | |
461 | 10.5M | text->operation &= ~TEXT_DO_DRAW; |
462 | 10.5M | return code; |
463 | 10.5M | } |
464 | | |
465 | | /* Mode 1 - stroke */ |
466 | | static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text) |
467 | 1.51k | { |
468 | 1.51k | int code; |
469 | 1.51k | gs_text_enum_t *penum=NULL, *saved_penum=NULL; |
470 | 1.51k | gs_point end_point, initial_point; |
471 | 1.51k | gx_device *dev = ctx->pgs->device; |
472 | 1.51k | int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits; |
473 | | |
474 | 1.51k | end_point.x = end_point.y = initial_point.x = initial_point.y = 0; |
475 | | |
476 | | /* Capture the current position */ |
477 | 1.51k | code = gs_currentpoint(ctx->pgs, &initial_point); |
478 | 1.51k | if (code < 0) |
479 | 0 | 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 | 1.51k | code = pdfi_gsave(ctx); |
486 | 1.51k | if (code < 0) |
487 | 0 | 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 | 1.51k | code = gs_newpath(ctx->pgs); |
493 | 1.51k | if (code < 0) |
494 | 0 | goto Tr1_error; |
495 | 1.51k | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
496 | 1.51k | if (code < 0) |
497 | 0 | goto Tr1_error; |
498 | | |
499 | | /* Don't draw the text, create a path suitable for stroking */ |
500 | 1.51k | text->operation |= TEXT_DO_FALSE_CHARPATH; |
501 | | |
502 | | /* Run the text methods to create the path */ |
503 | 1.51k | code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum); |
504 | 1.51k | if (code < 0) |
505 | 45 | goto Tr1_error; |
506 | | |
507 | 1.46k | penum->single_byte_space = true; |
508 | 1.46k | saved_penum = ctx->text.current_enum; |
509 | 1.46k | ctx->text.current_enum = penum; |
510 | 1.46k | code = gs_text_process(penum); |
511 | 1.46k | gs_text_release(ctx->pgs, penum, "pdfi_Tj"); |
512 | 1.46k | ctx->text.current_enum = saved_penum; |
513 | 1.46k | if (code < 0) |
514 | 0 | 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 | 1.46k | code = gs_currentpoint(ctx->pgs, &end_point); |
522 | 1.46k | if (code < 0) |
523 | 0 | goto Tr1_error; |
524 | | |
525 | 1.46k | if (talphabits != galphabits) |
526 | 0 | dev->color_info.anti_alias.graphics_bits = talphabits; |
527 | | |
528 | | /* Change to the current stroking colour */ |
529 | 1.46k | gs_swapcolors_quick(ctx->pgs); |
530 | | /* Finally, stroke the actual path */ |
531 | 1.46k | code = gs_stroke(ctx->pgs); |
532 | | /* Switch back to the non-stroke colour */ |
533 | 1.46k | gs_swapcolors_quick(ctx->pgs); |
534 | | |
535 | 1.46k | if (talphabits != galphabits) |
536 | 0 | dev->color_info.anti_alias.graphics_bits = galphabits; |
537 | | |
538 | 1.51k | Tr1_error: |
539 | | /* And grestore back to where we started */ |
540 | 1.51k | (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 | 1.51k | if (code >= 0) |
544 | 1.46k | code = gs_moveto(ctx->pgs, end_point.x, end_point.y); |
545 | | |
546 | 1.51k | text->operation &= ~TEXT_DO_FALSE_CHARPATH; |
547 | 1.51k | return code; |
548 | 1.46k | } |
549 | | |
550 | | /* Mode 2 - fill then stroke */ |
551 | | static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text) |
552 | 9.24k | { |
553 | 9.24k | int code, restart = 0; |
554 | 9.24k | gs_text_enum_t *penum=NULL, *saved_penum=NULL; |
555 | 9.24k | gs_point end_point, initial_point; |
556 | 9.24k | gx_device *dev = ctx->pgs->device; |
557 | 9.24k | int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits; |
558 | | |
559 | 9.24k | end_point.x = end_point.y = initial_point.x = initial_point.y = 0; |
560 | | |
561 | | /* Capture the current position */ |
562 | 9.24k | code = gs_currentpoint(ctx->pgs, &initial_point); |
563 | 9.24k | if (code < 0) |
564 | 0 | 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 | 9.24k | code = pdfi_gsave(ctx); |
571 | 9.24k | if (code < 0) |
572 | 0 | 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 | 9.24k | code = gs_newpath(ctx->pgs); |
578 | 9.24k | if (code < 0) |
579 | 0 | goto Tr1_error; |
580 | 9.24k | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
581 | 9.24k | if (code < 0) |
582 | 0 | goto Tr1_error; |
583 | | |
584 | | /* Don't draw the text, create a path suitable for stroking */ |
585 | 9.24k | text->operation |= TEXT_DO_FALSE_CHARPATH; |
586 | | |
587 | | /* Run the text methods to create the path */ |
588 | 9.24k | code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum); |
589 | 9.24k | if (code < 0) |
590 | 0 | goto Tr1_error; |
591 | | |
592 | 9.24k | penum->single_byte_space = true; |
593 | 9.24k | saved_penum = ctx->text.current_enum; |
594 | 9.24k | ctx->text.current_enum = penum; |
595 | 9.24k | code = gs_text_process(penum); |
596 | 9.24k | gs_text_release(ctx->pgs, penum, "pdfi_Tj"); |
597 | 9.24k | ctx->text.current_enum = saved_penum; |
598 | 9.24k | if (code < 0) |
599 | 50 | 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 | 9.19k | code = gs_currentpoint(ctx->pgs, &end_point); |
607 | 9.19k | if (code < 0) |
608 | 0 | goto Tr1_error; |
609 | 9.19k | if (talphabits != galphabits) |
610 | 0 | dev->color_info.anti_alias.graphics_bits = talphabits; |
611 | 9.19k | code = gs_fillstroke(ctx->pgs, &restart); |
612 | 9.19k | if (talphabits != galphabits) |
613 | 0 | dev->color_info.anti_alias.graphics_bits = galphabits; |
614 | | |
615 | 9.24k | Tr1_error: |
616 | | /* And grestore back to where we started */ |
617 | 9.24k | (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 | 9.24k | if (code >= 0) |
621 | 9.19k | code = gs_moveto(ctx->pgs, end_point.x, end_point.y); |
622 | | |
623 | 9.24k | text->operation &= ~TEXT_DO_FALSE_CHARPATH; |
624 | 9.24k | return code; |
625 | 9.19k | } |
626 | | |
627 | | static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text) |
628 | 143k | { |
629 | 143k | int code; |
630 | 143k | gs_text_enum_t *penum=NULL, *saved_penum=NULL; |
631 | | |
632 | | /* Don't draw the text */ |
633 | 143k | text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3; |
634 | | |
635 | | /* Run the text methods to create the path */ |
636 | 143k | code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum); |
637 | 143k | if (code < 0) |
638 | 0 | return code; |
639 | | |
640 | 143k | penum->single_byte_space = true; |
641 | 143k | saved_penum = ctx->text.current_enum; |
642 | 143k | ctx->text.current_enum = penum; |
643 | 143k | code = gs_text_process(penum); |
644 | 143k | gs_text_release(ctx->pgs, penum, "pdfi_Tj"); |
645 | 143k | ctx->text.current_enum = saved_penum; |
646 | | |
647 | 143k | return code; |
648 | 143k | } |
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 | 2.60k | { |
655 | 2.60k | int code; |
656 | 2.60k | gs_point initial_point; |
657 | | |
658 | | /* Capture the current position */ |
659 | 2.60k | code = gs_currentpoint(ctx->pgs, &initial_point); |
660 | 2.60k | if (code < 0) |
661 | 0 | return code; |
662 | | |
663 | 2.60k | ctx->text.TextClip = true; |
664 | | /* First fill the text */ |
665 | 2.60k | code = pdfi_show_Tr_0(ctx, text); |
666 | 2.60k | if (code < 0) |
667 | 0 | return code; |
668 | | |
669 | | /* Return the current point to the initial point */ |
670 | 2.60k | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
671 | 2.60k | if (code >= 0) |
672 | | /* And add the text to the acumulated path for clipping */ |
673 | 2.60k | code = pdfi_show_Tr_7(ctx, text); |
674 | | |
675 | 2.60k | return code; |
676 | 2.60k | } |
677 | | |
678 | | static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text) |
679 | 298 | { |
680 | 298 | int code; |
681 | 298 | gs_point initial_point; |
682 | | |
683 | | /* Capture the current position */ |
684 | 298 | code = gs_currentpoint(ctx->pgs, &initial_point); |
685 | 298 | if (code < 0) |
686 | 0 | return code; |
687 | | |
688 | 298 | ctx->text.TextClip = true; |
689 | | /* First stroke the text */ |
690 | 298 | code = pdfi_show_Tr_1(ctx, text); |
691 | 298 | if (code < 0) |
692 | 0 | return code; |
693 | | |
694 | | /* Return the current point to the initial point */ |
695 | 298 | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
696 | 298 | if (code >= 0) |
697 | | /* And add the text to the acumulated path for clipping */ |
698 | 298 | code = pdfi_show_Tr_7(ctx, text); |
699 | | |
700 | 298 | return code; |
701 | 298 | } |
702 | | |
703 | | static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text) |
704 | 474 | { |
705 | 474 | int code; |
706 | 474 | gs_point initial_point; |
707 | | |
708 | | /* Capture the current position */ |
709 | 474 | code = gs_currentpoint(ctx->pgs, &initial_point); |
710 | 474 | if (code < 0) |
711 | 0 | return code; |
712 | | |
713 | 474 | ctx->text.TextClip = true; |
714 | | /* First fill and stroke the text */ |
715 | 474 | code = pdfi_show_Tr_2(ctx, text); |
716 | 474 | if (code < 0) |
717 | 0 | return code; |
718 | | |
719 | | /* Return the current point to the initial point */ |
720 | 474 | code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
721 | 474 | if (code >= 0) |
722 | | /* And add the text to the acumulated path for clipping */ |
723 | 474 | code = pdfi_show_Tr_7(ctx, text); |
724 | | |
725 | 474 | return code; |
726 | 474 | } |
727 | | |
728 | | static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text) |
729 | 3.50k | { |
730 | 3.50k | int code; |
731 | 3.50k | gs_text_enum_t *penum=NULL, *saved_penum=NULL; |
732 | | |
733 | 3.50k | ctx->text.TextClip = true; |
734 | | /* Don't draw the text, create a path suitable for filling */ |
735 | 3.50k | text->operation |= TEXT_DO_TRUE_CHARPATH; |
736 | | |
737 | | /* Run the text methods to create the path */ |
738 | 3.50k | code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum); |
739 | 3.50k | if (code < 0) |
740 | 0 | goto Tr7_error; |
741 | | |
742 | 3.50k | penum->single_byte_space = true; |
743 | 3.50k | saved_penum = ctx->text.current_enum; |
744 | 3.50k | ctx->text.current_enum = penum; |
745 | 3.50k | code = gs_text_process(penum); |
746 | 3.50k | gs_text_release(ctx->pgs, penum, "pdfi_Tj"); |
747 | 3.50k | ctx->text.current_enum = saved_penum; |
748 | | |
749 | 3.50k | Tr7_error: |
750 | 3.50k | text->operation &= ~TEXT_DO_TRUE_CHARPATH; |
751 | 3.50k | return code; |
752 | 3.50k | } |
753 | | |
754 | | static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text) |
755 | 1.62M | { |
756 | 1.62M | int Trmode = 0, code; |
757 | 1.62M | pdf_font *current_font = NULL; |
758 | 1.62M | gs_point initial_point; |
759 | | |
760 | 1.62M | current_font = pdfi_get_current_pdf_font(ctx); |
761 | 1.62M | if (current_font == NULL) |
762 | 0 | return_error(gs_error_invalidfont); |
763 | | |
764 | | /* Capture the current position, in case we need it for clipping */ |
765 | 1.62M | code = gs_currentpoint(ctx->pgs, &initial_point); |
766 | | |
767 | 1.62M | Trmode = gs_currenttextrenderingmode(ctx->pgs); |
768 | 1.62M | if (Trmode == 3) { |
769 | 4.93k | if (current_font->pdfi_font_type == e_pdf_font_type3) |
770 | 1 | text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3; |
771 | 4.93k | else |
772 | 4.93k | text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3; |
773 | 4.93k | } |
774 | 1.62M | else |
775 | 1.62M | 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 | 1.62M | if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) { |
789 | 689 | gs_swapcolors_quick(ctx->pgs); |
790 | | |
791 | 689 | code = gx_set_dev_color(ctx->pgs); |
792 | 689 | if (code != 0) |
793 | 0 | return code; |
794 | | |
795 | 689 | code = gs_gstate_color_load(ctx->pgs); |
796 | 689 | if (code < 0) |
797 | 0 | return code; |
798 | | |
799 | 689 | gs_swapcolors_quick(ctx->pgs); |
800 | 689 | } |
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 | 1.62M | if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) { |
808 | 17 | gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
809 | | |
810 | 17 | ctx->text.TextClip = true; |
811 | 17 | code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1); |
812 | 17 | if (code < 0 && code != gs_error_undefined) |
813 | 0 | return code; |
814 | 17 | } |
815 | | |
816 | 1.62M | code = pdfi_show_simple(ctx, text); |
817 | 1.62M | return code; |
818 | 1.62M | } |
819 | | |
820 | | static int pdfi_show(pdf_context *ctx, pdf_string *s) |
821 | 12.3M | { |
822 | 12.3M | int code = 0, SavedTextDepth = 0; |
823 | 12.3M | int code1 = 0; |
824 | 12.3M | gs_text_params_t text; |
825 | 12.3M | pdf_font *current_font = NULL; |
826 | 12.3M | int Trmode = 0; |
827 | 12.3M | int initial_gsave_level = ctx->pgs->level; |
828 | 12.3M | pdfi_trans_state_t state; |
829 | 12.3M | int outside_text_block = 0; |
830 | | |
831 | 12.3M | if (ctx->text.BlockDepth == 0) { |
832 | 126k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL); |
833 | 126k | outside_text_block = 1; |
834 | 126k | } |
835 | | |
836 | 12.3M | if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0 |
837 | 12.3M | || 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 | 12.1k | return 0; |
843 | 12.1k | } |
844 | | |
845 | 12.3M | current_font = pdfi_get_current_pdf_font(ctx); |
846 | 12.3M | if (current_font == NULL) |
847 | 13 | return_error(gs_error_invalidfont); |
848 | | |
849 | 12.3M | code = pdfi_show_set_params(ctx, s, &text); |
850 | 12.3M | if (code < 0) |
851 | 30 | 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 | 12.3M | SavedTextDepth = ctx->text.BlockDepth; |
861 | 12.3M | ctx->text.BlockDepth = 0; |
862 | 12.3M | code = pdfi_trans_setup_text(ctx, &state, true); |
863 | 12.3M | ctx->text.BlockDepth = SavedTextDepth; |
864 | | |
865 | 12.3M | if (code >= 0) { |
866 | 12.3M | if (ctx->device_state.preserve_tr_mode) { |
867 | 1.62M | code = pdfi_show_Tr_preserve(ctx, &text); |
868 | 10.7M | } else { |
869 | 10.7M | 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 | 10.7M | if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3) |
875 | 307 | Trmode = 0; |
876 | | |
877 | 10.7M | switch(Trmode) { |
878 | 10.5M | case 0: |
879 | 10.5M | code = pdfi_show_Tr_0(ctx, &text); |
880 | 10.5M | break; |
881 | 1.21k | case 1: |
882 | 1.21k | code = pdfi_show_Tr_1(ctx, &text); |
883 | 1.21k | break; |
884 | 8.77k | case 2: |
885 | 8.77k | code = pdfi_show_Tr_2(ctx, &text); |
886 | 8.77k | break; |
887 | 143k | case 3: |
888 | 143k | code = pdfi_show_Tr_3(ctx, &text); |
889 | 143k | break; |
890 | 2.60k | case 4: |
891 | 2.60k | code = pdfi_show_Tr_4(ctx, &text); |
892 | 2.60k | break; |
893 | 298 | case 5: |
894 | 298 | code = pdfi_show_Tr_5(ctx, &text); |
895 | 298 | break; |
896 | 474 | case 6: |
897 | 474 | code = pdfi_show_Tr_6(ctx, &text); |
898 | 474 | break; |
899 | 134 | case 7: |
900 | 134 | code = pdfi_show_Tr_7(ctx, &text); |
901 | 134 | break; |
902 | 0 | default: |
903 | 0 | break; |
904 | 10.7M | } |
905 | 10.7M | } |
906 | 12.3M | code1 = pdfi_trans_teardown_text(ctx, &state); |
907 | 12.3M | if (code == 0) |
908 | 12.2M | code = code1; |
909 | 12.3M | } |
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 | 12.3M | while(ctx->pgs->level > initial_gsave_level) |
920 | 441 | 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 | 12.3M | if (outside_text_block) { |
927 | 120k | code1 = do_ET(ctx); |
928 | 120k | if (code == 0) |
929 | 115k | code = code1; |
930 | 120k | } |
931 | | |
932 | 12.3M | show_error: |
933 | 12.3M | if ((void *)text.data.chars != (void *)s->data) |
934 | 49.8k | gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars"); |
935 | | |
936 | 12.3M | gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error"); |
937 | 12.3M | gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error"); |
938 | 12.3M | return code; |
939 | 12.3M | } |
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 | 27.2k | { |
946 | 27.2k | int code = 0; |
947 | 27.2k | gx_device_bbox *bbdev; |
948 | 27.2k | pdf_font *current_font = pdfi_get_current_pdf_font(ctx); |
949 | 27.2k | gs_matrix tmpmat, Trm, Trm_ctm; |
950 | 27.2k | gs_point cppt, startpt; |
951 | | |
952 | 27.2k | if (current_font == NULL) |
953 | 0 | return_error(gs_error_invalidfont); |
954 | | |
955 | | |
956 | 27.2k | for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke; |
957 | | |
958 | 27.2k | if (ctx->devbbox == NULL) { |
959 | 261 | bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)"); |
960 | 261 | if (bbdev == NULL) |
961 | 0 | return_error(gs_error_VMerror); |
962 | 261 | gx_device_bbox_init(bbdev, NULL, ctx->memory); |
963 | 261 | ctx->devbbox = (gx_device *)bbdev; |
964 | 261 | rc_increment(ctx->devbbox); |
965 | 261 | } |
966 | 27.0k | else { |
967 | 27.0k | bbdev = (gx_device_bbox *)ctx->devbbox; |
968 | 27.0k | } |
969 | 27.2k | gx_device_retain((gx_device *)bbdev, true); |
970 | 27.2k | gx_device_bbox_set_white_opaque(bbdev, true); |
971 | | |
972 | 27.2k | code = pdfi_gsave(ctx); |
973 | 27.2k | if (code < 0) { |
974 | 0 | gx_device_retain((gx_device *)bbdev, false); |
975 | 0 | return code; |
976 | 0 | } |
977 | | /* The bbox device needs a fairly high resolution to get accurate results */ |
978 | 27.2k | gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0); |
979 | | |
980 | 27.2k | code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev); |
981 | 27.2k | if (code < 0) |
982 | 0 | goto out; |
983 | | |
984 | 27.2k | Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100); |
985 | 27.2k | Trm.xy = 0; |
986 | 27.2k | Trm.yx = 0; |
987 | 27.2k | Trm.yy = ctx->pgs->PDFfontsize; |
988 | 27.2k | Trm.tx = 0; |
989 | 27.2k | Trm.ty = ctx->pgs->textrise; |
990 | | |
991 | 27.2k | memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat)); |
992 | | /* We want to avoid any translations unrelated to the extents of the text */ |
993 | 27.2k | tmpmat.tx = tmpmat.ty = 0; |
994 | 27.2k | gs_matrix_multiply(&Trm, &tmpmat, &Trm); |
995 | | |
996 | 27.2k | memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat)); |
997 | | /* As above */ |
998 | 27.2k | tmpmat.tx = tmpmat.ty = 0; |
999 | 27.2k | gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm); |
1000 | 27.2k | gs_setmatrix(ctx->pgs, &Trm_ctm); |
1001 | | |
1002 | 27.2k | gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0); |
1003 | | |
1004 | 27.2k | code = pdfi_gs_setgray(ctx, 1.0); |
1005 | 27.2k | if (code < 0) |
1006 | 0 | 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 | 27.2k | startpt.x = ctx->pgs->PDFfontsize; |
1015 | 27.2k | startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise); |
1016 | 27.2k | code = gs_moveto(ctx->pgs, startpt.x, startpt.y); |
1017 | 27.2k | if (code < 0) |
1018 | 0 | goto out; |
1019 | | |
1020 | | /* Pretend to have at least one BT or pdfi_show will generate a spurious warning */ |
1021 | 27.2k | ctx->text.BlockDepth++; |
1022 | 27.2k | code = pdfi_show(ctx, s); |
1023 | | /* And an ET */ |
1024 | 27.2k | ctx->text.BlockDepth--; |
1025 | 27.2k | if (code < 0) |
1026 | 0 | goto out; |
1027 | | |
1028 | 27.2k | code = gx_device_bbox_bbox(bbdev, bboxout); |
1029 | 27.2k | if (code < 0) |
1030 | 0 | goto out; |
1031 | | |
1032 | 27.2k | bboxout->q.x -= bboxout->p.x; |
1033 | 27.2k | bboxout->q.y -= bboxout->p.y; |
1034 | 27.2k | bboxout->p.x = bboxout->p.y = 0; |
1035 | | |
1036 | 27.2k | code = gs_currentpoint(ctx->pgs, &cppt); |
1037 | 27.2k | if (code >= 0) { |
1038 | 27.2k | code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt); |
1039 | 27.2k | if (code < 0) |
1040 | 0 | goto out; |
1041 | 27.2k | advance_width->x = ctx->pgs->current_point.x - startpt.x; |
1042 | 27.2k | advance_width->y = ctx->pgs->current_point.y - startpt.y; |
1043 | 27.2k | code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width); |
1044 | 27.2k | } |
1045 | 27.2k | out: |
1046 | 27.2k | pdfi_grestore(ctx); |
1047 | 27.2k | (void)gs_closedevice((gx_device *)bbdev); |
1048 | 27.2k | gx_device_retain((gx_device *)bbdev, false); |
1049 | | |
1050 | 27.2k | return code; |
1051 | 27.2k | } |
1052 | | |
1053 | | int pdfi_Tj(pdf_context *ctx) |
1054 | 1.85M | { |
1055 | 1.85M | int code = 0; |
1056 | 1.85M | pdf_string *s = NULL; |
1057 | 1.85M | gs_matrix saved, Trm; |
1058 | 1.85M | gs_point initial_point, current_point, pt; |
1059 | 1.85M | double linewidth = ctx->pgs->line_params.half_width; |
1060 | 1.85M | int initial_point_valid; |
1061 | | |
1062 | 1.85M | if (pdfi_count_stack(ctx) < 1) |
1063 | 37.2k | return_error(gs_error_stackunderflow); |
1064 | | |
1065 | 1.81M | if (pdfi_oc_is_off(ctx)) |
1066 | 2.29k | goto exit; |
1067 | | |
1068 | 1.81M | s = (pdf_string *)ctx->stack_top[-1]; |
1069 | 1.81M | if (pdfi_type_of(s) != PDF_STRING) { |
1070 | 62.1k | pdfi_pop(ctx, 1); |
1071 | 62.1k | return_error(gs_error_typecheck); |
1072 | 62.1k | } |
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 | 1.74M | pdfi_countup(s); |
1079 | 1.74M | pdfi_pop(ctx, 1); |
1080 | | |
1081 | | /* Save the CTM for later restoration */ |
1082 | 1.74M | saved = ctm_only(ctx->pgs); |
1083 | 1.74M | initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0); |
1084 | | |
1085 | 1.74M | Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100); |
1086 | 1.74M | Trm.xy = 0; |
1087 | 1.74M | Trm.yx = 0; |
1088 | 1.74M | Trm.yy = ctx->pgs->PDFfontsize; |
1089 | 1.74M | Trm.tx = 0; |
1090 | 1.74M | Trm.ty = ctx->pgs->textrise; |
1091 | | |
1092 | 1.74M | code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm); |
1093 | 1.74M | if (code < 0) |
1094 | 0 | goto exit; |
1095 | | |
1096 | 1.74M | if (!ctx->device_state.preserve_tr_mode) { |
1097 | 1.57M | code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt); |
1098 | 1.57M | if (code < 0) |
1099 | 58.8k | goto exit; |
1100 | 1.51M | ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y)); |
1101 | 1.51M | } 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 | 178k | gs_matrix devmatrix, matrix; |
1108 | 178k | gx_device *device = gs_currentdevice(ctx->pgs); |
1109 | | |
1110 | 178k | devmatrix.xx = 72.0 / device->HWResolution[0]; |
1111 | 178k | devmatrix.xy = 0; |
1112 | 178k | devmatrix.yx = 0; |
1113 | 178k | devmatrix.yy = 72.0 / device->HWResolution[1]; |
1114 | 178k | devmatrix.tx = 0; |
1115 | 178k | devmatrix.ty = 0; |
1116 | | |
1117 | 178k | code = gs_matrix_multiply(&saved, &devmatrix, &matrix); |
1118 | 178k | if (code < 0) |
1119 | 0 | goto exit; |
1120 | | |
1121 | 178k | code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt); |
1122 | 178k | if (code < 0) |
1123 | 0 | goto exit; |
1124 | 178k | ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y)); |
1125 | 178k | } |
1126 | | |
1127 | 1.69M | code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm); |
1128 | 1.69M | if (code < 0) |
1129 | 0 | goto exit; |
1130 | | |
1131 | 1.69M | code = gs_setmatrix(ctx->pgs, &Trm); |
1132 | 1.69M | if (code < 0) |
1133 | 0 | goto exit; |
1134 | | |
1135 | 1.69M | code = gs_moveto(ctx->pgs, 0, 0); |
1136 | 1.69M | if (code < 0) |
1137 | 0 | goto Tj_error; |
1138 | | |
1139 | 1.69M | code = pdfi_show(ctx, s); |
1140 | 1.69M | if (code < 0) |
1141 | 44.3k | goto Tj_error; |
1142 | | |
1143 | 1.64M | ctx->pgs->line_params.half_width = linewidth; |
1144 | | /* Update the Text matrix with the current point, for the next operation |
1145 | | */ |
1146 | 1.64M | (void)gs_currentpoint(ctx->pgs, ¤t_point); /* Always valid */ |
1147 | 1.64M | Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100); |
1148 | 1.64M | Trm.xy = 0; |
1149 | 1.64M | Trm.yx = 0; |
1150 | 1.64M | Trm.yy = ctx->pgs->PDFfontsize; |
1151 | 1.64M | Trm.tx = 0; |
1152 | 1.64M | Trm.ty = 0; |
1153 | 1.64M | code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm); |
1154 | 1.64M | if (code < 0) |
1155 | 0 | goto Tj_error; |
1156 | | |
1157 | 1.64M | code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt); |
1158 | 1.64M | if (code < 0) |
1159 | 0 | goto Tj_error; |
1160 | 1.64M | ctx->pgs->textmatrix.tx += pt.x; |
1161 | 1.64M | ctx->pgs->textmatrix.ty += pt.y; |
1162 | | |
1163 | 1.69M | Tj_error: |
1164 | | /* Restore the CTM to the saved value */ |
1165 | 1.69M | (void)gs_setmatrix(ctx->pgs, &saved); |
1166 | | /* And restore the currentpoint */ |
1167 | 1.69M | if (initial_point_valid) |
1168 | 1.61M | (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
1169 | 75.1k | else |
1170 | 75.1k | code = gs_newpath(ctx->pgs); |
1171 | | /* And the line width */ |
1172 | 1.69M | ctx->pgs->line_params.half_width = linewidth; |
1173 | | |
1174 | 1.75M | exit: |
1175 | 1.75M | pdfi_countdown(s); |
1176 | 1.75M | return code; |
1177 | 1.69M | } |
1178 | | |
1179 | | int pdfi_TJ(pdf_context *ctx) |
1180 | 1.88M | { |
1181 | 1.88M | int code = 0, i; |
1182 | 1.88M | pdf_array *a = NULL; |
1183 | 1.88M | pdf_obj *o = NULL; |
1184 | 1.88M | double dx = 0; |
1185 | 1.88M | gs_point pt; |
1186 | 1.88M | gs_matrix saved, Trm; |
1187 | 1.88M | gs_point initial_point, current_point; |
1188 | 1.88M | double linewidth = ctx->pgs->line_params.half_width; |
1189 | 1.88M | pdf_font *current_font = NULL; |
1190 | 1.88M | int initial_point_valid; |
1191 | | |
1192 | 1.88M | current_font = pdfi_get_current_pdf_font(ctx); |
1193 | 1.88M | if (current_font == NULL) |
1194 | 6.56k | return_error(gs_error_invalidfont); |
1195 | | |
1196 | 1.87M | if (ctx->text.BlockDepth == 0) { |
1197 | 72.0k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL); |
1198 | 72.0k | } |
1199 | | |
1200 | 1.87M | if (pdfi_count_stack(ctx) < 1) |
1201 | 16.0k | return_error(gs_error_stackunderflow); |
1202 | | |
1203 | 1.85M | if (pdfi_oc_is_off(ctx)) |
1204 | 490 | goto exit; |
1205 | | |
1206 | 1.85M | a = (pdf_array *)ctx->stack_top[-1]; |
1207 | 1.85M | if (pdfi_type_of(a) != PDF_ARRAY) { |
1208 | 51.5k | pdfi_pop(ctx, 1); |
1209 | 51.5k | return gs_note_error(gs_error_typecheck); |
1210 | 51.5k | } |
1211 | 1.80M | pdfi_countup(a); |
1212 | 1.80M | pdfi_pop(ctx, 1); |
1213 | | |
1214 | | /* Save the CTM for later restoration */ |
1215 | 1.80M | saved = ctm_only(ctx->pgs); |
1216 | 1.80M | 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 | 1.80M | Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100); |
1222 | 1.80M | Trm.xy = 0; |
1223 | 1.80M | Trm.yx = 0; |
1224 | 1.80M | Trm.yy = ctx->pgs->PDFfontsize; |
1225 | 1.80M | Trm.tx = 0; |
1226 | 1.80M | Trm.ty = ctx->pgs->textrise; |
1227 | | |
1228 | 1.80M | code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm); |
1229 | 1.80M | if (code < 0) |
1230 | 0 | goto exit; |
1231 | | |
1232 | 1.80M | if (!ctx->device_state.preserve_tr_mode) { |
1233 | 1.60M | code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt); |
1234 | 1.60M | if (code < 0) |
1235 | 12.1k | goto exit; |
1236 | 1.59M | ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y)); |
1237 | 1.59M | } 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 | 202k | gs_matrix devmatrix, matrix; |
1244 | 202k | gx_device *device = gs_currentdevice(ctx->pgs); |
1245 | | |
1246 | 202k | devmatrix.xx = 72.0 / device->HWResolution[0]; |
1247 | 202k | devmatrix.xy = 0; |
1248 | 202k | devmatrix.yx = 0; |
1249 | 202k | devmatrix.yy = 72.0 / device->HWResolution[1]; |
1250 | 202k | devmatrix.tx = 0; |
1251 | 202k | devmatrix.ty = 0; |
1252 | | |
1253 | 202k | code = gs_matrix_multiply(&saved, &devmatrix, &matrix); |
1254 | 202k | if (code < 0) |
1255 | 0 | goto exit; |
1256 | | |
1257 | 202k | code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt); |
1258 | 202k | if (code < 0) |
1259 | 0 | goto exit; |
1260 | 202k | ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y)); |
1261 | 202k | } |
1262 | | |
1263 | 1.79M | code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm); |
1264 | 1.79M | if (code < 0) |
1265 | 0 | goto exit; |
1266 | 1.79M | code = gs_setmatrix(ctx->pgs, &Trm); |
1267 | 1.79M | if (code < 0) |
1268 | 0 | goto TJ_error; |
1269 | | |
1270 | 1.79M | code = gs_moveto(ctx->pgs, 0, 0); |
1271 | 1.79M | if (code < 0) |
1272 | 0 | goto TJ_error; |
1273 | | |
1274 | 21.2M | for (i = 0; i < pdfi_array_size(a); i++) { |
1275 | 19.5M | code = pdfi_array_get(ctx, a, (uint64_t)i, &o); |
1276 | 19.5M | if (code < 0) |
1277 | 1 | goto TJ_error; |
1278 | | |
1279 | 19.5M | if (pdfi_type_of(o) == PDF_STRING) |
1280 | 10.6M | code = pdfi_show(ctx, (pdf_string *)o); |
1281 | 8.90M | else { |
1282 | 8.90M | code = pdfi_obj_to_real(ctx, o, &dx); |
1283 | 8.90M | if (code < 0) |
1284 | 103 | goto TJ_error; |
1285 | 8.90M | dx /= -1000; |
1286 | 8.90M | if (current_font->pfont && current_font->pfont->WMode == 0) |
1287 | 8.89M | code = gs_rmoveto(ctx->pgs, dx, 0); |
1288 | 7.25k | else |
1289 | 7.25k | code = gs_rmoveto(ctx->pgs, 0, dx); |
1290 | 8.90M | } |
1291 | 19.5M | pdfi_countdown(o); |
1292 | 19.5M | o = NULL; |
1293 | 19.5M | if (code < 0) |
1294 | 44.3k | goto TJ_error; |
1295 | 19.5M | } |
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 | 1.74M | if (ctx->text.BlockDepth > 0) { |
1301 | | /* Update the Text matrix with the current point, for the next operation |
1302 | | */ |
1303 | 1.72M | (void)gs_currentpoint(ctx->pgs, ¤t_point); /* Always valid */ |
1304 | 1.72M | Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100); |
1305 | 1.72M | Trm.xy = 0; |
1306 | 1.72M | Trm.yx = 0; |
1307 | 1.72M | Trm.yy = ctx->pgs->PDFfontsize; |
1308 | 1.72M | Trm.tx = 0; |
1309 | 1.72M | Trm.ty = 0; |
1310 | 1.72M | code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm); |
1311 | 1.72M | if (code < 0) |
1312 | 0 | goto TJ_error; |
1313 | | |
1314 | 1.72M | code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt); |
1315 | 1.72M | if (code < 0) |
1316 | 0 | goto TJ_error; |
1317 | 1.72M | ctx->pgs->textmatrix.tx += pt.x; |
1318 | 1.72M | ctx->pgs->textmatrix.ty += pt.y; |
1319 | 1.72M | } |
1320 | 1.79M | TJ_error: |
1321 | 1.79M | pdfi_countdown(o); |
1322 | | /* Restore the CTM to the saved value */ |
1323 | 1.79M | (void)gs_setmatrix(ctx->pgs, &saved); |
1324 | | /* And restore the currentpoint */ |
1325 | 1.79M | if (initial_point_valid) |
1326 | 1.75M | (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
1327 | 35.8k | else |
1328 | 35.8k | code = gs_newpath(ctx->pgs); |
1329 | | /* And the line width */ |
1330 | 1.79M | ctx->pgs->line_params.half_width = linewidth; |
1331 | | |
1332 | 1.80M | exit: |
1333 | 1.80M | pdfi_countdown(a); |
1334 | 1.80M | return code; |
1335 | 1.79M | } |
1336 | | |
1337 | | static int pdfi_set_TL(pdf_context *ctx, double TL) |
1338 | 404k | { |
1339 | 404k | return gs_settextleading(ctx->pgs, TL); |
1340 | 404k | } |
1341 | | |
1342 | | int pdfi_TL(pdf_context *ctx) |
1343 | 12.3k | { |
1344 | 12.3k | int code; |
1345 | 12.3k | double d; |
1346 | | |
1347 | 12.3k | code = pdfi_destack_real(ctx, &d); |
1348 | 12.3k | if (code < 0) |
1349 | 49 | return code; |
1350 | | |
1351 | 12.3k | return pdfi_set_TL(ctx, -d); |
1352 | 12.3k | } |
1353 | | |
1354 | | int pdfi_Tm(pdf_context *ctx) |
1355 | 1.85M | { |
1356 | 1.85M | int code; |
1357 | 1.85M | float m[6]; |
1358 | 1.85M | gs_matrix mat; |
1359 | | |
1360 | 1.85M | code = pdfi_destack_floats(ctx, m, 6); |
1361 | 1.85M | if (code < 0) |
1362 | 205k | return code; |
1363 | | |
1364 | 1.64M | if (ctx->text.BlockDepth == 0) { |
1365 | 67.7k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL); |
1366 | | |
1367 | 67.7k | gs_make_identity(&mat); |
1368 | 67.7k | code = gs_settextmatrix(ctx->pgs, &mat); |
1369 | 67.7k | if (code < 0) |
1370 | 0 | return code; |
1371 | | |
1372 | 67.7k | code = gs_settextlinematrix(ctx->pgs, &mat); |
1373 | 67.7k | if (code < 0) |
1374 | 0 | return code; |
1375 | 67.7k | } |
1376 | | |
1377 | 1.64M | if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0) |
1378 | 19.8k | pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL); |
1379 | | |
1380 | 1.64M | code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m); |
1381 | 1.64M | if (code < 0) |
1382 | 0 | return code; |
1383 | | |
1384 | 1.64M | return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m); |
1385 | 1.64M | } |
1386 | | |
1387 | | int pdfi_Tr(pdf_context *ctx) |
1388 | 794k | { |
1389 | 794k | int code; |
1390 | 794k | int64_t mode; |
1391 | | |
1392 | 794k | code = pdfi_destack_int(ctx, &mode); |
1393 | 794k | if (code < 0) |
1394 | 9.80k | return code; |
1395 | | |
1396 | 784k | if (mode < 0 || mode > 7) |
1397 | 5.60k | 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 | 778k | if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip) |
1404 | 65 | pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL); |
1405 | | |
1406 | 778k | if (ctx->device_state.preserve_tr_mode) { |
1407 | 41.3k | gs_settextrenderingmode(ctx->pgs, mode); |
1408 | 41.3k | } else |
1409 | 737k | { |
1410 | 737k | gs_point initial_point; |
1411 | | |
1412 | 737k | if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) { |
1413 | 479 | gs_settextrenderingmode(ctx->pgs, mode); |
1414 | | /* Capture the current position */ |
1415 | 479 | 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 | 479 | gs_newpath(ctx->pgs); |
1420 | 479 | if (code >= 0) |
1421 | 430 | gs_moveto(ctx->pgs, initial_point.x, initial_point.y); |
1422 | 479 | code = 0; |
1423 | 736k | } 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 | 73 | code = pdfi_ET(ctx); |
1430 | 73 | if (code < 0) |
1431 | 2 | return code; |
1432 | 71 | gs_settextrenderingmode(ctx->pgs, mode); |
1433 | 71 | code = pdfi_BT(ctx); |
1434 | 71 | if (code < 0) |
1435 | 0 | return code; |
1436 | 71 | } |
1437 | 736k | else |
1438 | 736k | gs_settextrenderingmode(ctx->pgs, mode); |
1439 | 737k | } |
1440 | 778k | return code; |
1441 | 778k | } |
1442 | | |
1443 | | static int pdfi_set_Ts(pdf_context *ctx, double Ts) |
1444 | 4.56k | { |
1445 | 4.56k | return gs_settextrise(ctx->pgs, Ts); |
1446 | 4.56k | } |
1447 | | |
1448 | | int pdfi_Ts(pdf_context *ctx) |
1449 | 5.14k | { |
1450 | 5.14k | int code; |
1451 | 5.14k | double d; |
1452 | | |
1453 | 5.14k | code = pdfi_destack_real(ctx, &d); |
1454 | 5.14k | if (code < 0) |
1455 | 582 | return code; |
1456 | | |
1457 | 4.56k | return pdfi_set_Ts(ctx, d); |
1458 | 5.14k | } |
1459 | | |
1460 | | static int pdfi_set_Tw(pdf_context *ctx, double Tw) |
1461 | 151k | { |
1462 | 151k | return gs_setwordspacing(ctx->pgs, Tw); |
1463 | 151k | } |
1464 | | |
1465 | | int pdfi_Tw(pdf_context *ctx) |
1466 | 151k | { |
1467 | 151k | int code; |
1468 | 151k | double d; |
1469 | | |
1470 | 151k | code = pdfi_destack_real(ctx, &d); |
1471 | 151k | if (code < 0) |
1472 | 1.04k | return code; |
1473 | | |
1474 | 150k | return pdfi_set_Tw(ctx, d); |
1475 | 151k | } |
1476 | | |
1477 | | int pdfi_Tz(pdf_context *ctx) |
1478 | 109k | { |
1479 | 109k | int code; |
1480 | 109k | double d; |
1481 | | |
1482 | 109k | code = pdfi_destack_real(ctx, &d); |
1483 | 109k | if (code < 0) |
1484 | 1.92k | return code; |
1485 | | |
1486 | 107k | return gs_settexthscaling(ctx->pgs, d); |
1487 | 109k | } |
1488 | | |
1489 | | int pdfi_singlequote(pdf_context *ctx) |
1490 | 21.1k | { |
1491 | 21.1k | int code; |
1492 | | |
1493 | 21.1k | if (ctx->text.BlockDepth == 0) { |
1494 | 10.9k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL); |
1495 | 10.9k | } |
1496 | | |
1497 | 21.1k | code = pdfi_T_star(ctx); |
1498 | 21.1k | if (code < 0) |
1499 | 0 | return code; |
1500 | | |
1501 | 21.1k | return pdfi_Tj(ctx); |
1502 | 21.1k | } |
1503 | | |
1504 | | int pdfi_doublequote(pdf_context *ctx) |
1505 | 8.14k | { |
1506 | 8.14k | int code; |
1507 | 8.14k | double Tw, Tc; |
1508 | | |
1509 | 8.14k | if (ctx->text.BlockDepth == 0) { |
1510 | 7.84k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL); |
1511 | 7.84k | } |
1512 | | |
1513 | 8.14k | if (pdfi_count_stack(ctx) < 3) { |
1514 | 7.73k | pdfi_clearstack(ctx); |
1515 | 7.73k | return_error(gs_error_stackunderflow); |
1516 | 7.73k | } |
1517 | | |
1518 | 405 | if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) { |
1519 | 189 | pdfi_pop(ctx, 3); |
1520 | 189 | return gs_note_error(gs_error_typecheck); |
1521 | 189 | } |
1522 | | |
1523 | 216 | code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc); |
1524 | 216 | if (code < 0) |
1525 | 3 | goto error; |
1526 | 213 | code = pdfi_set_Tc(ctx, Tc); |
1527 | 213 | if (code < 0) |
1528 | 0 | goto error; |
1529 | | |
1530 | 213 | code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw); |
1531 | 213 | if (code < 0) |
1532 | 19 | goto error; |
1533 | 194 | code = pdfi_set_Tw(ctx, Tw); |
1534 | 194 | if (code < 0) |
1535 | 0 | goto error; |
1536 | | |
1537 | 194 | code = pdfi_T_star(ctx); |
1538 | 194 | if (code < 0) |
1539 | 0 | goto error; |
1540 | | |
1541 | 194 | code = pdfi_Tj(ctx); |
1542 | | /* Tj pops one off the stack for us, leaving us 2 to go. */ |
1543 | 194 | pdfi_pop(ctx, 2); |
1544 | 194 | return code; |
1545 | | |
1546 | 22 | error: |
1547 | 22 | pdfi_pop(ctx, 3); |
1548 | 22 | return code; |
1549 | 194 | } |