/src/ghostpdl/pcl/pcl/pglabel.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-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 | | |
17 | | /* pglabel.c - HP-GL/2 label commands */ |
18 | | |
19 | | #include "math_.h" |
20 | | #include "memory_.h" |
21 | | #include "ctype_.h" |
22 | | #include "stdio_.h" /* for gdebug.h */ |
23 | | #include "gdebug.h" |
24 | | #include "pcparse.h" |
25 | | #include "plvalue.h" |
26 | | #include "pgmand.h" |
27 | | #include "pginit.h" |
28 | | #include "pgfont.h" |
29 | | #include "pgdraw.h" |
30 | | #include "pggeom.h" |
31 | | #include "pgmisc.h" |
32 | | #include "pcpage.h" |
33 | | #include "pcfsel.h" |
34 | | #include "pcsymbol.h" |
35 | | #include "pcpalet.h" |
36 | | #include "pcdraw.h" |
37 | | #include "gscoord.h" |
38 | | #include "gsline.h" |
39 | | #include "gspath.h" |
40 | | #include "gsutil.h" |
41 | | #include "gxchar.h" /* for show enumerator */ |
42 | | #include "gxfont.h" |
43 | | #include "gxstate.h" /* for gs_gstate_client_data */ |
44 | | |
45 | 4.16M | #define STICK_FONT_TYPEFACE 48 |
46 | | |
47 | | /* NB this next constant is not quite right. The STICK_FONT_TYPEFACE |
48 | | definition is used in the code to cover both stick and arc fonts. |
49 | | Typically the prescription was to choose the stick font typeface |
50 | | (48) and enable proportional spacing to get the arc font. More |
51 | | recently we discovered a new typeface family number that can be |
52 | | used for the proportionally spaced arc fonts (50). This has been |
53 | | reflected in the hpgl/2 selection code but nowhere else. */ |
54 | 121k | #define ARC_FONT_TYPEFACE 50 |
55 | | |
56 | | /* currently selected font */ |
57 | | static pl_font_t * |
58 | | hpgl_currentfont(const hpgl_state_t * pgls) |
59 | 2.28M | { |
60 | 2.28M | return pgls->g.font_selection[pgls->g.font_selected].font; |
61 | 2.28M | } |
62 | | |
63 | | static bool |
64 | | hpgl_is_currentfont_stick(const hpgl_state_t * pgls) |
65 | 1.68M | { |
66 | 1.68M | pl_font_t *plfont = hpgl_currentfont(pgls); |
67 | | |
68 | 1.68M | if (!plfont) |
69 | 8 | return false; |
70 | 1.68M | return (((plfont->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE) |
71 | 1.11M | && (plfont->params.proportional_spacing == false)); |
72 | 1.68M | } |
73 | | |
74 | | bool |
75 | | hpgl_is_currentfont_stick_or_arc(const hpgl_state_t * pgls) |
76 | 590k | { |
77 | 590k | pl_font_t *plfont = hpgl_currentfont(pgls); |
78 | | |
79 | 590k | if (!plfont) |
80 | 0 | return false; |
81 | 590k | return (((plfont->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE) |
82 | 1.55k | || ((plfont->params.typeface_family & 0xfff) == |
83 | 1.55k | ARC_FONT_TYPEFACE)); |
84 | 590k | } |
85 | | |
86 | | /* convert points 2 plu - agfa uses 72.307 points per inch */ |
87 | | static double |
88 | | hpgl_points_2_plu(const hpgl_state_t * pgls, double points) |
89 | 4.42M | { |
90 | 4.42M | const pcl_font_selection_t *pfs = |
91 | 4.42M | &pgls->g.font_selection[pgls->g.font_selected]; |
92 | 4.42M | double ppi = 72.0; |
93 | | |
94 | 4.42M | if (pfs->font->scaling_technology == plfst_Intellifont) |
95 | 0 | ppi = 72.307; |
96 | 4.42M | return points * (1016.0 / ppi); |
97 | 4.42M | } |
98 | | |
99 | | /* ------ Next-character procedure ------ */ |
100 | | |
101 | | /* |
102 | | * Map a character through the symbol set, if needed. |
103 | | */ |
104 | | static gs_char |
105 | | hpgl_map_symbol(uint chr, const hpgl_state_t * pgls) |
106 | 4.18M | { |
107 | 4.18M | const pcl_font_selection_t *pfs = |
108 | 4.18M | &pgls->g.font_selection[pgls->g.font_selected]; |
109 | 4.18M | const pl_symbol_map_t *psm = pfs->map; |
110 | 4.18M | bool db = pcl_downloaded_and_bound(pfs->font); |
111 | | |
112 | 4.18M | return pl_map_symbol((db ? NULL : psm), chr, |
113 | 4.18M | pfs->font->storage == pcds_internal, |
114 | 4.18M | pfs->font->font_type == plft_MSL, |
115 | 4.18M | pgls->memory); |
116 | 4.18M | } |
117 | | |
118 | | /* ------ Font selection ------- */ |
119 | | |
120 | | /* Select primary (0) or alternate (1) font. */ |
121 | | static void |
122 | | hpgl_select_font_pri_alt(hpgl_state_t * pgls, int index) |
123 | 265k | { |
124 | 265k | if (pgls->g.font_selected != index) { |
125 | 198k | hpgl_free_stick_fonts(pgls); |
126 | 198k | pgls->g.font_selected = index; |
127 | 198k | pgls->g.font = 0; |
128 | 198k | } |
129 | 265k | return; |
130 | 265k | } |
131 | | |
132 | | /* forward decl */ |
133 | | static int hpgl_recompute_font(hpgl_state_t * pgls); |
134 | | |
135 | | /* Ensure a font is available and selected. */ |
136 | | static int |
137 | | hpgl_ensure_font(hpgl_state_t * pgls) |
138 | 2.64M | { |
139 | 2.64M | pcl_font_selection_t *pfs = |
140 | 2.64M | &pgls->g.font_selection[pgls->g.font_selected]; |
141 | | |
142 | 2.64M | if (pgls->g.font == 0 || pgls->g.font->pfont == 0 || pfs->font == 0) |
143 | 2.64M | hpgl_call(hpgl_recompute_font(pgls)); |
144 | 2.64M | return 0; |
145 | 2.64M | } |
146 | | |
147 | | /* |
148 | | * The character complement for the stick font is puzzling: it doesn't seem |
149 | | * to correspond directly to any of the MSL *or* Unicode symbol set bits |
150 | | * described in the Comparison Guide. We set the bits for MSL Basic Latin |
151 | | * (63) and for Unicode ASCII (31), and Latin 1 (30). |
152 | | */ |
153 | | static const byte stick_character_complement[8] = { |
154 | | 0x7f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xfe |
155 | | }; |
156 | | |
157 | | /* Select the stick font, creating it if necessary. */ |
158 | | /* We break this out only for readability: it's only called in one place. */ |
159 | | static int |
160 | | hpgl_select_stick_font(hpgl_state_t * pgls) |
161 | 96.3k | { |
162 | 96.3k | pcl_font_selection_t *pfs = |
163 | 96.3k | &pgls->g.font_selection[pgls->g.font_selected]; |
164 | 96.3k | pl_font_t *font = &pgls->g.stick_font[pgls->g.font_selected] |
165 | 96.3k | [pfs->params.proportional_spacing]; |
166 | 96.3k | gs_font_base *pfont; |
167 | 96.3k | int code; |
168 | | |
169 | | /* Create a gs_font if none has been created yet. */ |
170 | 96.3k | hpgl_free_stick_fonts(pgls); |
171 | 96.3k | pfont = gs_alloc_struct(pgls->memory, gs_font_base, &st_gs_font_base, |
172 | 96.3k | "stick/arc font"); |
173 | | |
174 | 96.3k | if (pfont == 0) |
175 | 0 | return_error(e_Memory); |
176 | 96.3k | code = pl_fill_in_font((gs_font *) pfont, font, pgls->font_dir, |
177 | 96.3k | pgls->memory, "stick_arc_font"); |
178 | 96.3k | if (code < 0) |
179 | 0 | return code; |
180 | 96.3k | if (pfs->params.proportional_spacing) |
181 | 20.4k | hpgl_fill_in_arc_font(pfont, gs_next_ids(pgls->memory, 1)); |
182 | 75.8k | else |
183 | 75.8k | hpgl_fill_in_stick_font(pfont, gs_next_ids(pgls->memory, 1)); |
184 | 96.3k | font->pfont = (gs_font *) pfont; |
185 | 96.3k | font->scaling_technology = plfst_TrueType; /****** WRONG ******/ |
186 | 96.3k | font->font_type = plft_Unicode; |
187 | 96.3k | font->widths_cache = NULL; |
188 | 96.3k | font->widths_cache_nitems = 0; |
189 | 96.3k | font->storage = 0; |
190 | | |
191 | 96.3k | memcpy(font->character_complement, stick_character_complement, 8); |
192 | | /* |
193 | | * The stick/arc font is protean: set its proportional spacing, |
194 | | * style, and stroke weight parameters to the requested ones. |
195 | | * We could fill in some of the other characteristics earlier, |
196 | | * but it's simpler to do it here. |
197 | | */ |
198 | 96.3k | font->params = pfs->params; |
199 | 96.3k | font->params.typeface_family = STICK_FONT_TYPEFACE; |
200 | | /* |
201 | | * The stick font is defined in a cell that's only 2/3 |
202 | | * the size of the actual character. |
203 | | */ |
204 | 96.3k | pl_fp_set_pitch_cp(&font->params, 1000.0 * 2 / 3); |
205 | 96.3k | pfs->font = font; |
206 | 96.3k | { |
207 | 96.3k | byte id[2]; |
208 | | |
209 | 96.3k | id[0] = pfs->params.symbol_set >> 8; |
210 | 96.3k | id[1] = pfs->params.symbol_set & 0xff; |
211 | 96.3k | pfs->map = pcl_find_symbol_map(pgls, |
212 | 96.3k | id, plgv_Unicode, |
213 | 96.3k | font->font_type == plft_16bit); |
214 | 96.3k | } |
215 | 96.3k | return 0; |
216 | 96.3k | } |
217 | | |
218 | | static int |
219 | | hpgl_select_531_font(hpgl_state_t * pgls) |
220 | 0 | { |
221 | 0 | pcl_font_selection_t *pfs = |
222 | 0 | &pgls->g.font_selection[pgls->g.font_selected]; |
223 | 0 | pl_font_t *font = &pgls->g.dl_531_font[pgls->g.font_selected]; |
224 | 0 | gs_font_base *pfont; |
225 | 0 | int code; |
226 | |
|
227 | 0 | hpgl_free_531_fonts(pgls); |
228 | 0 | pfont = gs_alloc_struct(pgls->memory, gs_font_base, &st_gs_font_base, |
229 | 0 | "stick/arc font"); |
230 | |
|
231 | 0 | if (pfont == 0) |
232 | 0 | return_error(e_Memory); |
233 | 0 | code = pl_fill_in_font((gs_font *) pfont, font, pgls->font_dir, |
234 | 0 | pgls->memory, "531_font"); |
235 | 0 | if (code < 0) |
236 | 0 | return code; |
237 | | |
238 | 0 | hpgl_fill_in_531_font(pfont, gs_next_ids(pgls->memory, 1)); |
239 | 0 | font->pfont = (gs_font *) pfont; |
240 | 0 | font->scaling_technology = plfst_TrueType;/****** WRONG ******/ |
241 | 0 | font->font_type = plft_Unicode; |
242 | 0 | font->widths_cache = NULL; |
243 | 0 | font->widths_cache_nitems = 0; |
244 | | /* we pass the dictionary created during the DL command as the font header data. */ |
245 | 0 | font->header = (void *)&pgls->g.dl_531_fontdict; |
246 | | /* the parameters aren't used so we just fill them in with stick |
247 | | font characteristics */ |
248 | 0 | memcpy(font->character_complement, stick_character_complement, 8); |
249 | 0 | font->params = pfs->params; |
250 | 0 | font->params.typeface_family = STICK_FONT_TYPEFACE; |
251 | 0 | pl_fp_set_pitch_cp(&font->params, 1000.0 * 2 / 3); |
252 | 0 | pfs->font = font; |
253 | | /* the font is mapped directly */ |
254 | 0 | pfs->map = 0; |
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | | /* Check whether the stick font supports a given symbol set. */ |
259 | | static bool |
260 | | hpgl_stick_font_supports(const pcl_state_t * pcs, uint symbol_set) |
261 | 96.5k | { |
262 | 96.5k | pl_glyph_vocabulary_t gv = |
263 | 96.5k | pl_complement_to_vocab(stick_character_complement); |
264 | 96.5k | byte id[2]; |
265 | 96.5k | pl_symbol_map_t *map; |
266 | | |
267 | 96.5k | id[0] = symbol_set >> 8; |
268 | 96.5k | id[1] = symbol_set & 0xff; |
269 | 96.5k | if ((map = pcl_find_symbol_map(pcs, id, gv, false)) == 0) |
270 | 246 | return false; |
271 | 96.3k | return pcl_check_symbol_support(map->character_requirements, |
272 | 96.3k | stick_character_complement); |
273 | 96.5k | } |
274 | | |
275 | | /* Recompute the current font if necessary. */ |
276 | | static int |
277 | | hpgl_recompute_font(hpgl_state_t * pgls) |
278 | 216k | { |
279 | 216k | pcl_font_selection_t *pfs = |
280 | 216k | &pgls->g.font_selection[pgls->g.font_selected]; |
281 | | |
282 | 216k | if ((pfs->params.symbol_set == 531) && (pgls->personality == rtl)) { |
283 | 0 | hpgl_call(hpgl_select_531_font(pgls)); |
284 | 0 | } else |
285 | 216k | if ((((pfs->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE |
286 | 120k | || (pfs->params.typeface_family & 0xfff) == ARC_FONT_TYPEFACE) |
287 | 96.5k | && pfs->params.style == 0 /* upright */ |
288 | 96.5k | && hpgl_stick_font_supports(pgls, pfs->params.symbol_set)) |
289 | | /* rtl only has stick fonts */ |
290 | 120k | || (pgls->personality == rtl) |
291 | 216k | ) |
292 | 216k | hpgl_call(hpgl_select_stick_font(pgls)); |
293 | 120k | else { |
294 | 120k | int code = pcl_reselect_font(pfs, pgls, false); |
295 | | |
296 | 120k | if (code < 0) |
297 | 0 | return code; |
298 | 120k | } |
299 | 216k | pgls->g.font = pfs->font; |
300 | 216k | pgls->g.map = pfs->map; |
301 | 216k | return pl_load_resident_font_data_from_file(pgls->memory, pfs->font); |
302 | 216k | } |
303 | | |
304 | | /* ------ Position management ------ */ |
305 | | |
306 | | /* accessor for character extra space takes line feed direction into account */ |
307 | | static inline hpgl_real_t |
308 | | hpgl_get_character_extra_space_x(const hpgl_state_t * pgls) |
309 | 2.78M | { |
310 | 2.78M | return (pgls->g.character.line_feed_direction < 0) ? |
311 | 2.78M | pgls->g.character.extra_space.y : pgls->g.character.extra_space.x; |
312 | 2.78M | } |
313 | | |
314 | | static inline hpgl_real_t |
315 | | hpgl_get_character_extra_space_y(const hpgl_state_t * pgls) |
316 | 1.60M | { |
317 | 1.60M | return (pgls->g.character.line_feed_direction < 0) ? |
318 | 1.60M | pgls->g.character.extra_space.x : pgls->g.character.extra_space.y; |
319 | 1.60M | } |
320 | | |
321 | | /* Get a character width in the current font, plus extra space if any. */ |
322 | | /* If the character isn't defined, return 1, otherwise return 0. */ |
323 | | static int |
324 | | hpgl_get_char_width(const hpgl_state_t * pgls, gs_char ch, |
325 | | hpgl_real_t * width) |
326 | 1.65M | { |
327 | 1.65M | gs_glyph glyph = hpgl_map_symbol(ch, pgls); |
328 | 1.65M | const pcl_font_selection_t *pfs = |
329 | 1.65M | &pgls->g.font_selection[pgls->g.font_selected]; |
330 | 1.65M | int code = 0; |
331 | 1.65M | gs_point gs_width; |
332 | | |
333 | 1.65M | if (pgls->g.character.size_mode == hpgl_size_not_set) { |
334 | 1.60M | if (pfs->params.proportional_spacing) { |
335 | 1.12M | code = |
336 | 1.12M | pl_font_char_width(pfs->font, (void *)(pgls->pgs), glyph, |
337 | 1.12M | &gs_width); |
338 | | /* hack until this code gets written properly, the |
339 | | following should amount to a percentage of the |
340 | | em-square the space character would occupy... */ |
341 | 1.12M | if (code == 1) { |
342 | 165k | gs_width.y = 0; |
343 | 165k | gs_width.x = pl_fp_pitch_cp(&pfs->font->params) / 1000.0; |
344 | 165k | } |
345 | | |
346 | 1.12M | if (!pl_font_is_scalable(pfs->font)) { |
347 | 0 | if (code == 0) |
348 | 0 | *width = |
349 | 0 | gs_width.x * inches_2_plu(1.0 / |
350 | 0 | pfs->font->resolution.x); |
351 | 0 | else |
352 | 0 | *width = coord_2_plu(pl_fp_pitch_cp(&pfs->font->params)); |
353 | 0 | goto add; |
354 | 1.12M | } else if (code >= 0) { |
355 | 1.12M | *width = |
356 | 1.12M | gs_width.x * hpgl_points_2_plu(pgls, |
357 | 1.12M | pfs->params.height_4ths / |
358 | 1.12M | 4.0); |
359 | 1.12M | goto add; |
360 | 1.12M | } |
361 | 0 | code = 1; |
362 | 0 | } |
363 | 478k | *width = |
364 | 478k | hpgl_points_2_plu(pgls, pl_fp_pitch_cp(&pfs->params) / 100.0); |
365 | 478k | } else { |
366 | 52.6k | *width = pgls->g.character.size.x; |
367 | 52.6k | if (pgls->g.character.size_mode == hpgl_size_relative) |
368 | 32.2k | *width *= pgls->g.P2.x - pgls->g.P1.x; |
369 | | |
370 | 52.6k | } |
371 | 1.65M | add: |
372 | | |
373 | 1.65M | if (hpgl_get_character_extra_space_x(pgls) != 0) { |
374 | | /* Add extra space. */ |
375 | 150k | if (pfs->params.proportional_spacing && ch != ' ') { |
376 | | /* Get the width of the space character. */ |
377 | 119k | int scode = |
378 | 119k | pl_font_char_width(pfs->font, (void *)(pgls->pgs), |
379 | 119k | hpgl_map_symbol(' ', pgls), &gs_width); |
380 | 119k | hpgl_real_t extra; |
381 | | |
382 | 119k | if (scode >= 0) |
383 | 119k | extra = |
384 | 119k | gs_width.x * hpgl_points_2_plu(pgls, |
385 | 119k | pfs->params.height_4ths / |
386 | 119k | 4.0); |
387 | 0 | else |
388 | 0 | extra = |
389 | 0 | hpgl_points_2_plu(pgls, |
390 | 0 | (pl_fp_pitch_cp(&pfs->params)) / 10.0); |
391 | 119k | *width += extra * hpgl_get_character_extra_space_x(pgls); |
392 | 119k | } else { |
393 | | /* All characters have the same width, */ |
394 | | /* or we're already getting the width of a space. */ |
395 | 31.1k | *width *= 1.0 + hpgl_get_character_extra_space_x(pgls); |
396 | 31.1k | } |
397 | 150k | } |
398 | 1.65M | return code; |
399 | 1.65M | } |
400 | | /* Get the cell height or character height in the current font, */ |
401 | | /* plus extra space if any. */ |
402 | | static int |
403 | | hpgl_get_current_cell_height(const hpgl_state_t * pgls, hpgl_real_t * height) |
404 | 1.60M | { |
405 | 1.60M | const pcl_font_selection_t *pfs = |
406 | 1.60M | &pgls->g.font_selection[pgls->g.font_selected]; |
407 | | |
408 | 1.60M | if (pfs->font->scaling_technology != plfst_bitmap) { |
409 | 1.60M | gs_point scale = hpgl_current_char_scale(pgls); |
410 | | |
411 | 1.60M | *height = fabs(scale.y); |
412 | 1.60M | } else { |
413 | | /* NB temporary not correct */ |
414 | 0 | *height = hpgl_points_2_plu(pgls, pfs->params.height_4ths / 4.0); |
415 | 0 | } |
416 | | |
417 | | /* the HP manual says linefeed distance or cell height is 1.33 |
418 | | times point size for stick fonts and "about" 1.2 times the |
419 | | point size for "most" fonts. Empirical results suggest the |
420 | | stick font value is 1.28. NB This value appears to be |
421 | | slightly different for the proportional arc font. */ |
422 | 1.60M | if (pgls->g.character.text_path == hpgl_text_right |
423 | 1.13M | || pgls->g.character.text_path == hpgl_text_left) { |
424 | 1.13M | if (hpgl_is_currentfont_stick(pgls)) |
425 | 376k | *height *= 1.28; |
426 | 754k | else |
427 | 754k | *height *= 1.2; |
428 | 1.13M | } else { |
429 | | /* INC */ |
430 | 474k | if (hpgl_is_currentfont_stick(pgls)) |
431 | 146k | *height *= .96; |
432 | 328k | else |
433 | 328k | *height *= .898; |
434 | 474k | } |
435 | | |
436 | 1.60M | *height *= 1.0 + hpgl_get_character_extra_space_y(pgls); |
437 | 1.60M | return 0; |
438 | 1.60M | } |
439 | | |
440 | | /* distance tranformation for character slant */ |
441 | | static int |
442 | | hpgl_slant_transform_distance(hpgl_state_t * pgls, gs_point * dxy, |
443 | | gs_point * s_dxy) |
444 | 64.8k | { |
445 | 64.8k | if (pgls->g.character.slant && !pgls->g.bitmap_fonts_allowed) { |
446 | 3 | gs_matrix smat; |
447 | 3 | gs_point tmp_dxy = *dxy; |
448 | | |
449 | 3 | gs_make_identity(&smat); |
450 | 3 | smat.yx = pgls->g.character.slant; |
451 | 3 | hpgl_call(gs_distance_transform(tmp_dxy.x, tmp_dxy.y, &smat, s_dxy)); |
452 | 3 | } |
453 | 64.8k | return 0; |
454 | 64.8k | } |
455 | | |
456 | | /* distance tranformation for character direction */ |
457 | | static int |
458 | | hpgl_rotation_transform_distance(hpgl_state_t * pgls, gs_point * dxy, |
459 | | gs_point * r_dxy) |
460 | 83.2k | { |
461 | 83.2k | double run = pgls->g.character.direction.x; |
462 | 83.2k | double rise = pgls->g.character.direction.y; |
463 | 83.2k | int angle = (int)((hpgl_compute_angle(run, rise) * radians_to_degrees) + 0.5); |
464 | 83.2k | gs_point tmp_dxy = *dxy; |
465 | 83.2k | gs_matrix rmat; |
466 | | |
467 | 83.2k | gs_make_rotation((double) angle, &rmat); |
468 | 83.2k | hpgl_call(gs_distance_transform(tmp_dxy.x, tmp_dxy.y, &rmat, r_dxy)); |
469 | 83.2k | return 0; |
470 | 83.2k | } |
471 | | |
472 | | /* Reposition the cursor. This does all the work for CP, and is also */ |
473 | | /* used to handle some control characters within LB. */ |
474 | | /* If pwidth != 0, it points to a precomputed horizontal space width. */ |
475 | | static int |
476 | | hpgl_move_cursor_by_characters(hpgl_state_t * pgls, hpgl_real_t spaces, |
477 | | hpgl_real_t lines, const hpgl_real_t * pwidth) |
478 | 18.4k | { |
479 | 18.4k | double nx, ny; |
480 | 18.4k | double dx = 0, dy = 0; |
481 | 18.4k | hpgl_call(hpgl_ensure_font(pgls)); |
482 | | |
483 | 18.4k | lines *= pgls->g.character.line_feed_direction; |
484 | | /* For vertical text paths, we have to swap spaces and lines. */ |
485 | 18.4k | switch (pgls->g.character.text_path) { |
486 | 16.9k | case hpgl_text_right: |
487 | 16.9k | nx = spaces, ny = lines; |
488 | 16.9k | break; |
489 | 811 | case hpgl_text_down: |
490 | 811 | nx = lines, ny = -spaces; |
491 | 811 | break; |
492 | 273 | case hpgl_text_left: |
493 | 273 | nx = -spaces, ny = -lines; |
494 | 273 | break; |
495 | 400 | case hpgl_text_up: |
496 | 400 | nx = -lines, ny = spaces; |
497 | 400 | break; |
498 | 0 | default: |
499 | 0 | return -1; |
500 | 18.4k | } |
501 | | /* calculate the next label position in relative coordinates. */ |
502 | 18.4k | if (nx != 0) { |
503 | 2.01k | hpgl_real_t width; |
504 | | |
505 | 2.01k | if (pwidth != 0) |
506 | 741 | width = *pwidth; |
507 | 1.26k | else |
508 | 1.26k | hpgl_get_char_width(pgls, ' ', &width); |
509 | 2.01k | dx = width * nx; |
510 | 2.01k | } |
511 | 18.4k | if (ny != 0) { |
512 | 15.8k | hpgl_real_t height; |
513 | | |
514 | 15.8k | hpgl_call(hpgl_get_current_cell_height(pgls, &height)); |
515 | 15.8k | dy = ny * height; |
516 | 15.8k | } |
517 | | |
518 | | /* |
519 | | * We just computed the deltas in user units if characters are |
520 | | * using relative sizing, and in PLU otherwise. |
521 | | * If scaling is on but characters aren't using relative |
522 | | * sizing, we have to convert the deltas to user units. |
523 | | */ |
524 | 18.4k | if (pgls->g.scaling_type != hpgl_scaling_none) { |
525 | 1.09k | gs_matrix mat; |
526 | 1.09k | gs_point user_dxy; |
527 | | |
528 | 1.09k | hpgl_call(hpgl_compute_user_units_to_plu_ctm(pgls, &mat)); |
529 | 1.09k | hpgl_call(gs_distance_transform_inverse(dx, dy, &mat, &user_dxy)); |
530 | 1.09k | dx = user_dxy.x; |
531 | 1.09k | dy = user_dxy.y; |
532 | 1.09k | } |
533 | | |
534 | 18.4k | { |
535 | 18.4k | gs_point dxy; |
536 | | |
537 | 18.4k | dxy.x = dx; |
538 | 18.4k | dxy.y = dy; |
539 | 18.4k | hpgl_rotation_transform_distance(pgls, &dxy, &dxy); |
540 | 18.4k | dx = dxy.x; |
541 | 18.4k | dy = dxy.y; |
542 | 18.4k | } |
543 | | /* a relative move to the new position */ |
544 | 18.4k | hpgl_call(hpgl_add_point_to_path(pgls, dx, dy, |
545 | 18.4k | hpgl_plot_move_relative, true)); |
546 | | |
547 | 18.4k | if (lines != 0) { |
548 | | /* update the position of the carriage return point */ |
549 | 16.2k | pgls->g.carriage_return_pos.x += dx; |
550 | 16.2k | pgls->g.carriage_return_pos.y += dy; |
551 | 16.2k | } |
552 | | /* free any selected stick fonts */ |
553 | 18.4k | hpgl_free_stick_fonts(pgls); |
554 | 18.4k | return 0; |
555 | 18.4k | } |
556 | | |
557 | | /* Execute a CR for CP or LB. */ |
558 | | static int |
559 | | hpgl_do_CR(hpgl_state_t * pgls) |
560 | 11.2k | { |
561 | 11.2k | return hpgl_add_point_to_path(pgls, pgls->g.carriage_return_pos.x, |
562 | 11.2k | pgls->g.carriage_return_pos.y, |
563 | 11.2k | hpgl_plot_move_absolute, true); |
564 | 11.2k | } |
565 | | |
566 | | /* CP [spaces,lines]; */ |
567 | | /* CP [;] */ |
568 | | int |
569 | | hpgl_CP(hpgl_args_t * pargs, hpgl_state_t * pgls) |
570 | 746 | { |
571 | 746 | hpgl_real_t spaces, lines; |
572 | | |
573 | 746 | if (hpgl_arg_c_real(pgls->memory, pargs, &spaces)) { |
574 | 8 | if (!hpgl_arg_c_real(pgls->memory, pargs, &lines)) |
575 | 8 | return e_Range; |
576 | 738 | } else { |
577 | | /* if there are no arguments a carriage return and line feed |
578 | | is executed */ |
579 | 738 | hpgl_call(hpgl_do_CR(pgls)); |
580 | 738 | spaces = 0, lines = -1; |
581 | 738 | } |
582 | 738 | return hpgl_move_cursor_by_characters(pgls, spaces, lines, |
583 | 738 | (const hpgl_real_t *)0); |
584 | 746 | } |
585 | | |
586 | | /* ------ Label buffer management ------ */ |
587 | | |
588 | | /* initialize the character buffer, setting state pointers for the |
589 | | beginning of the character buffer and the current character within |
590 | | the buffer to position 0. */ |
591 | | static int |
592 | | hpgl_init_label_buffer(hpgl_state_t * pgls) |
593 | 65.1k | { |
594 | 65.1k | pgls->g.label.char_count = 0; |
595 | 65.1k | pgls->g.label.buffer_size = hpgl_char_count; |
596 | 65.1k | return ((pgls->g.label.buffer = |
597 | 65.1k | gs_alloc_bytes(pgls->memory, hpgl_char_count, |
598 | 65.1k | "hpgl_init_label_buffer")) == 0 ? e_Memory : 0); |
599 | 65.1k | } |
600 | | |
601 | | /* release the character buffer */ |
602 | | void |
603 | | hpgl_destroy_label_buffer(hpgl_state_t * pgls) |
604 | 64.8k | { |
605 | 64.8k | gs_free_object(pgls->memory, pgls->g.label.buffer, |
606 | 64.8k | "hpgl_destroy_label_buffer"); |
607 | 64.8k | pgls->g.label.char_count = 0; |
608 | 64.8k | pgls->g.label.buffer_size = 0; |
609 | 64.8k | pgls->g.label.buffer = 0; |
610 | 64.8k | } |
611 | | |
612 | | /* add a single character to the line buffer */ |
613 | | static int |
614 | | hpgl_buffer_char(hpgl_state_t * pgls, byte ch) |
615 | 1.56M | { |
616 | | /* check if there is room for the new character and resize if |
617 | | necessary */ |
618 | 1.56M | if (pgls->g.label.buffer_size == pgls->g.label.char_count) { /* Resize the label buffer, currently by doubling its size. */ |
619 | 2.92k | uint new_size = pgls->g.label.buffer_size << 1; |
620 | 2.92k | byte *new_mem = |
621 | 2.92k | gs_resize_object(pgls->memory, pgls->g.label.buffer, new_size, |
622 | 2.92k | "hpgl_resize_label_buffer"); |
623 | | |
624 | 2.92k | if (new_mem == 0) |
625 | 0 | return_error(e_Memory); |
626 | 2.92k | pgls->g.label.buffer = new_mem; |
627 | 2.92k | pgls->g.label.buffer_size = new_size; |
628 | 2.92k | } |
629 | | /* store the character */ |
630 | 1.56M | pgls->g.label.buffer[pgls->g.label.char_count++] = ch; |
631 | 1.56M | return 0; |
632 | 1.56M | } |
633 | | |
634 | | /* |
635 | | * Test if the gl/2 drawing primitives should draw the character or |
636 | | * "show" can be used directly. |
637 | | */ |
638 | | static bool |
639 | | hpgl_use_show(hpgl_state_t * pgls, pl_font_t * pfont) |
640 | 1.26M | { |
641 | | |
642 | | /* NB opaque source must go through the gl/2 line drawing code and |
643 | | treated as a special effect because the normal text processing |
644 | | does not support rendering the background of characters. |
645 | | Bitmap characters must be rendered with the library's "show" |
646 | | machinery, so opaque bitmaps are not properly supported in |
647 | | gl/2. Fortunately we don't expect to see them in the real |
648 | | world */ |
649 | 1.26M | if (pgls->g.source_transparent == 0 |
650 | 590k | && pfont->scaling_technology != plfst_bitmap) |
651 | 590k | return false; |
652 | | |
653 | | /* Show cannot be used if CF is not default since the character |
654 | | may require additional processing by the line drawing code. */ |
655 | 677k | if ((pgls->g.character.fill_mode == 0 && pgls->g.character.edge_pen == 0) |
656 | 1.13k | || (pfont->scaling_technology == plfst_bitmap)) |
657 | 676k | return true; |
658 | 1.13k | else |
659 | 1.13k | return false; |
660 | 677k | } |
661 | | |
662 | | /* get the scaling factors for a gl/2 character */ |
663 | | gs_point |
664 | | hpgl_current_char_scale(const hpgl_state_t * pgls) |
665 | 2.88M | { |
666 | 2.88M | const pcl_font_selection_t *pfs = |
667 | 2.88M | &pgls->g.font_selection[pgls->g.font_selected]; |
668 | 2.88M | pl_font_t *font = pfs->font; |
669 | 2.88M | bool bitmaps_allowed = pgls->g.bitmap_fonts_allowed; |
670 | 2.88M | gs_point scale = {0, 0}; |
671 | | |
672 | 2.88M | if (font == NULL) |
673 | 8 | return scale; |
674 | | |
675 | 2.88M | if (pgls->g.character.size_mode == hpgl_size_not_set |
676 | 2.69M | || font->scaling_technology == plfst_bitmap) { |
677 | 2.69M | if (font->scaling_technology == plfst_bitmap) { |
678 | 0 | scale.x = inches_2_plu(1.0 / font->resolution.x); |
679 | 0 | scale.y = -inches_2_plu(1.0 / font->resolution.y); |
680 | | /* Scale fixed-width fonts by pitch, variable-width by height. */ |
681 | 2.69M | } else if (pfs->params.proportional_spacing) { |
682 | 1.84M | if (pl_font_is_scalable(font)) { |
683 | 1.84M | scale.x = |
684 | 1.84M | hpgl_points_2_plu(pgls, pfs->params.height_4ths / 4.0); |
685 | 1.84M | scale.y = scale.x; |
686 | 1.84M | } else { |
687 | 0 | double ratio = (double)pfs->params.height_4ths |
688 | 0 | / font->params.height_4ths; |
689 | |
|
690 | 0 | scale.x = ratio * inches_2_plu(1.0 / font->resolution.x); |
691 | | |
692 | | /* |
693 | | * Bitmap fonts use the PCL coordinate system, |
694 | | * so we must invert the Y coordinate. |
695 | | */ |
696 | 0 | scale.y = -(ratio * inches_2_plu(1.0 / font->resolution.y)); |
697 | 0 | } |
698 | 1.84M | } else { |
699 | 853k | #define PERCENT_OF_EM ((pl_fp_pitch_cp(&pfs->font->params) / 1000.0)) |
700 | 853k | scale.x = scale.y = |
701 | 853k | (1.0 / PERCENT_OF_EM) * hpgl_points_2_plu(pgls, |
702 | 853k | pl_fp_pitch_cp |
703 | 853k | (&pfs->params) / |
704 | 853k | 100.0); |
705 | 853k | } |
706 | 2.69M | } else { |
707 | | /* |
708 | | * Note that the CTM takes P1/P2 into account unless |
709 | | * an absolute character size is in effect. |
710 | | */ |
711 | | /* HP is really scaling the cap height not the point size. |
712 | | We assume point size is 1.5 times the point size */ |
713 | 189k | scale.x = pgls->g.character.size.x * 1.5 * 1.25; |
714 | 189k | scale.y = pgls->g.character.size.y * 1.5; |
715 | 189k | if (pgls->g.character.size_mode == hpgl_size_relative) |
716 | 63.1k | scale.x *= pgls->g.P2.x - pgls->g.P1.x, |
717 | 63.1k | scale.y *= pgls->g.P2.y - pgls->g.P1.y; |
718 | 189k | if (bitmaps_allowed) /* no mirroring */ |
719 | 0 | scale.x = fabs(scale.x), scale.y = fabs(scale.y); |
720 | 189k | } |
721 | 2.88M | return scale; |
722 | | |
723 | 2.88M | } |
724 | | /* |
725 | | * build the path and render it |
726 | | */ |
727 | | static int |
728 | | hpgl_print_char(hpgl_state_t * pgls, uint ch) |
729 | 1.26M | { |
730 | 1.26M | int text_path = pgls->g.character.text_path; |
731 | 1.26M | const pcl_font_selection_t *pfs = |
732 | 1.26M | &pgls->g.font_selection[pgls->g.font_selected]; |
733 | 1.26M | pl_font_t *font = pfs->font; |
734 | 1.26M | gs_gstate *pgs = pgls->pgs; |
735 | 1.26M | gs_matrix save_ctm; |
736 | 1.26M | gs_font *pfont = pgls->g.font->pfont; |
737 | 1.26M | gs_point scale = hpgl_current_char_scale(pgls); |
738 | | |
739 | | /* |
740 | | * All character data is relative, but we have to start at |
741 | | * the right place. |
742 | | */ |
743 | 1.26M | hpgl_call(hpgl_add_point_to_path(pgls, |
744 | 1.26M | pgls->g.pos.x, |
745 | 1.26M | pgls->g.pos.y, |
746 | 1.26M | hpgl_plot_move_absolute, true)); |
747 | 1.26M | hpgl_call(gs_currentmatrix(pgs, &save_ctm)); |
748 | | |
749 | | /* |
750 | | * Use plotter unit ctm. |
751 | | */ |
752 | 1.26M | hpgl_call(hpgl_set_plu_ctm(pgls)); |
753 | | |
754 | | /* ?? WRONG and UGLY */ |
755 | 1.26M | { |
756 | 1.26M | float metrics[4]; |
757 | | |
758 | 1.26M | if ((pl_font_char_metrics(font, (void *)(pgls->pgs), |
759 | 1.26M | hpgl_map_symbol(ch, pgls), metrics)) == 1) |
760 | 146k | ch = ' '; |
761 | 1.26M | } |
762 | | |
763 | | /* |
764 | | * If we're using a stroked font, patch the pen width to reflect |
765 | | * the stroke weight. Note that when the font's build_char |
766 | | * procedure calls stroke, the CTM is still scaled. |
767 | | ****** WHAT IF scale.x != scale.y? ****** this should be unnecessary. |
768 | | */ |
769 | | |
770 | 1.26M | if (pfont->PaintType != 0) { |
771 | 902k | const float *widths = pcl_palette_get_pen_widths(pgls->ppalet); |
772 | 902k | float save_width = widths[hpgl_get_selected_pen(pgls)]; |
773 | 902k | int weight = pfs->params.stroke_weight; |
774 | 902k | double nwidth; |
775 | | |
776 | 902k | if (weight == 9999) |
777 | 0 | nwidth = save_width; |
778 | 902k | else { |
779 | 902k | nwidth = 0.06 + weight * (weight < 0 ? 0.005 : 0.010); |
780 | 902k | nwidth *= min(scale.x, scale.y) * (hpgl_width_scale(pgls)); |
781 | 902k | } |
782 | | /* in points */ |
783 | 902k | gs_setlinewidth(pgs, nwidth * (72.0 / 1016.0)); |
784 | 902k | } |
785 | | |
786 | | /* |
787 | | * We know that the drawing machinery only sets the CTM |
788 | | * once, at the beginning of the path. We now impose the scale |
789 | | * (and other transformations) on the CTM so that we can add |
790 | | * the symbol outline based on a 1x1-unit cell. |
791 | | */ |
792 | 1.26M | { |
793 | 1.26M | gs_font *pfont = pgls->g.font->pfont; |
794 | 1.26M | bool bitmaps_allowed = pgls->g.bitmap_fonts_allowed; |
795 | 1.26M | bool use_show = hpgl_use_show(pgls, font); |
796 | 1.26M | gs_matrix pre_rmat, advance_mat; |
797 | 1.26M | int angle = 0; |
798 | 1.26M | gs_text_enum_t *penum = NULL; |
799 | 1.26M | int code; |
800 | 1.26M | gs_point start_pt, end_pt; |
801 | 1.26M | hpgl_real_t space_width; |
802 | 1.26M | int space_code; |
803 | 1.26M | hpgl_real_t width; |
804 | | |
805 | | /* Handle size. */ |
806 | | |
807 | 1.26M | gs_scale(pgs, scale.x, scale.y); |
808 | | /* Handle rotation. */ |
809 | 1.26M | { |
810 | 1.26M | double run = pgls->g.character.direction.x, |
811 | 1.26M | rise = pgls->g.character.direction.y; |
812 | | |
813 | | /* gl/2 bitmap fonts are scaled in a left handed coordinate |
814 | | system - so the "rise" direction is opposite. */ |
815 | 1.26M | if (font->scaling_technology == plfst_bitmap) |
816 | 0 | rise *= -1; |
817 | | |
818 | 1.26M | if (pgls->g.character.direction_relative) { |
819 | 1.82k | run *= pgls->g.P2.x - pgls->g.P1.x; |
820 | 1.82k | rise *= pgls->g.P2.y - pgls->g.P1.y; |
821 | 1.82k | } |
822 | | |
823 | 1.26M | angle = |
824 | 1.26M | (int)((hpgl_compute_angle(run, rise) * radians_to_degrees) + 0.5); |
825 | 1.26M | gs_currentmatrix(pgs, &pre_rmat); |
826 | 1.26M | gs_rotate(pgs, angle); |
827 | | /* we do something special if the angle is greater than |
828 | | zero for bitmap fonts (see below) */ |
829 | 1.26M | angle = -1; |
830 | | |
831 | 1.26M | if (bitmaps_allowed && (run != 0) && (rise != 0)) { /* not a multple of 90 degrees */ |
832 | | /* |
833 | | * If bitmap fonts are allowed, rotate to the nearest |
834 | | * multiple of 90 degrees. We have to do something |
835 | | * special at the end to create the correct escapement. |
836 | | */ |
837 | 0 | if (run >= 0) { |
838 | 0 | if (rise >= 0) |
839 | 0 | angle = (run >= rise ? 0 : 90); |
840 | 0 | else |
841 | 0 | angle = (-rise >= run ? 270 : 0); |
842 | 0 | } else { |
843 | 0 | if (rise >= 0) |
844 | 0 | angle = (rise >= -run ? 90 : 180); |
845 | 0 | else |
846 | 0 | angle = (-run >= -rise ? 180 : 270); |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | 1.26M | } |
851 | | |
852 | | /* Handle slant. */ |
853 | 1.26M | if (pgls->g.character.slant && !bitmaps_allowed) { |
854 | 18 | gs_matrix smat; |
855 | | |
856 | 18 | gs_make_identity(&smat); |
857 | 18 | smat.yx = pgls->g.character.slant; |
858 | 18 | gs_concat(pgs, &smat); |
859 | 18 | } |
860 | | |
861 | 1.26M | gs_setfont(pgs, pfont); |
862 | 1.26M | pfont->FontMatrix = pfont->orig_FontMatrix; |
863 | | |
864 | | /* |
865 | | * Adjust the initial position of the character according to |
866 | | * the text path. And the left side bearing. It appears |
867 | | * HPGL/2 renders all characters without a left side bearing. |
868 | | */ |
869 | 1.26M | hpgl_call(gs_currentpoint(pgs, &start_pt)); |
870 | 1.26M | if (text_path == hpgl_text_left) { |
871 | 83.9k | hpgl_get_char_width(pgls, ch, &width); |
872 | 83.9k | start_pt.x -= width / scale.x; |
873 | 83.9k | hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x, start_pt.y, |
874 | 83.9k | hpgl_plot_move_absolute, false)); |
875 | | |
876 | 83.9k | } |
877 | | |
878 | | /* |
879 | | * Reset the rotation if we're using a bitmap font. |
880 | | */ |
881 | 1.26M | gs_currentmatrix(pgs, &advance_mat); |
882 | 1.26M | if (angle >= 0) { |
883 | 0 | gs_setmatrix(pgs, &pre_rmat); |
884 | 0 | gs_rotate(pgs, (double) angle); |
885 | 0 | } |
886 | | |
887 | | /* If SP is a control code, get the width of the space character. */ |
888 | 1.26M | if (ch == ' ') { |
889 | 155k | space_code = hpgl_get_char_width(pgls, ' ', &space_width); |
890 | | |
891 | 155k | if (0 == space_code && |
892 | 155k | pl_font_is_scalable(font) && pfs->params.proportional_spacing) |
893 | 132k | space_code = 1; /* NB hpgl_get_width lies. */ |
894 | | |
895 | 155k | if (space_code == 1) { |
896 | | /* Space is a control code. */ |
897 | 132k | if (pl_font_is_scalable(font)) { |
898 | 132k | if (pfs->params.proportional_spacing) |
899 | 132k | space_width = |
900 | 132k | (coord_2_plu |
901 | 132k | ((pl_fp_pitch_cp(&pfs->font->params) / 10) |
902 | 132k | * pfs->params.height_4ths / 4)) / scale.x; |
903 | 0 | else |
904 | 0 | space_width = 1.0; |
905 | | /* error! NB scalable fixed pitch space_code == 0 */ |
906 | 132k | } else |
907 | 0 | space_width = |
908 | 0 | coord_2_plu(pl_fp_pitch_cp(&pfs->font->params)) / |
909 | 0 | scale.x; |
910 | 132k | space_width *= (1.0 + hpgl_get_character_extra_space_x(pgls)); |
911 | 132k | } |
912 | 155k | } |
913 | | |
914 | | /* Check for SP control code. */ |
915 | 1.26M | if (ch == ' ' && space_code != 0) { |
916 | | /* Space is a control code. Just advance the position. */ |
917 | 132k | gs_setmatrix(pgs, &advance_mat); |
918 | 132k | hpgl_call(hpgl_add_point_to_path(pgls, space_width, 0.0, |
919 | 132k | hpgl_plot_move_relative, false)); |
920 | 132k | hpgl_call(gs_currentpoint(pgs, &end_pt)); |
921 | | /* at this point we will assume the page is marked */ |
922 | 132k | pgls->page_marked = true; |
923 | 1.13M | } else { |
924 | 1.13M | gs_text_params_t text; |
925 | 1.13M | gs_char mychar_buff[1]; |
926 | | |
927 | 1.13M | mychar_buff[0] = hpgl_map_symbol(ch, pgls); |
928 | 1.13M | if (use_show) { |
929 | | /* not a path that needs to be drawn by the hpgl/2 |
930 | | vector drawing code. */ |
931 | 642k | hpgl_call(hpgl_set_drawing_color(pgls, hpgl_rm_character)); |
932 | 642k | text.operation = |
933 | 642k | TEXT_FROM_CHARS | TEXT_DO_DRAW | TEXT_RETURN_WIDTH; |
934 | 642k | } else |
935 | 493k | text.operation = |
936 | 493k | TEXT_FROM_CHARS | TEXT_DO_TRUE_CHARPATH | |
937 | 493k | TEXT_RETURN_WIDTH; |
938 | 1.13M | text.data.chars = mychar_buff; |
939 | | /* always on char (gs_chars (ints)) at a time */ |
940 | 1.13M | text.size = 1; |
941 | 1.13M | code = gs_text_begin(pgs, &text, pgls->memory, &penum); |
942 | 1.13M | if (code >= 0) |
943 | 1.13M | code = gs_text_process(penum); |
944 | | |
945 | 1.13M | if (code >= 0) { |
946 | | /* we just check the current position for |
947 | | "insidedness" - this seems to address the dirty |
948 | | page issue in practice. */ |
949 | 1.13M | code = pcl_mark_page_for_current_pos(pgls); |
950 | 1.13M | } |
951 | 1.13M | gs_text_release(pgs, penum, "hpgl_print_char"); |
952 | 1.13M | if (code < 0) { |
953 | 0 | hpgl_free_stick_fonts(pgls); |
954 | 0 | return code; |
955 | 0 | } |
956 | 1.13M | gs_setmatrix(pgs, &advance_mat); |
957 | 1.13M | if (angle >= 0) { |
958 | | /* Compensate for bitmap font non-rotation. */ |
959 | 0 | if (text_path == hpgl_text_right) { |
960 | 0 | hpgl_get_char_width(pgls, ch, &width); |
961 | 0 | hpgl_call(hpgl_add_point_to_path |
962 | 0 | (pgls, start_pt.x + width / scale.x, start_pt.y, |
963 | 0 | hpgl_plot_move_absolute, false)); |
964 | 0 | } |
965 | 0 | } |
966 | 1.13M | hpgl_call(gs_currentpoint(pgs, &end_pt)); |
967 | 1.13M | if (start_pt.x == end_pt.x && start_pt.y == end_pt.y) { |
968 | | /* freetype doesn't move currentpoint in gs_show(), |
969 | | * since gs cache is not used. NB we don't support |
970 | | * freetype anymore is this necessary? |
971 | | */ |
972 | 1 | hpgl_get_char_width(pgls, ch, &width); |
973 | 1 | hpgl_call(hpgl_add_point_to_path(pgls, width / scale.x, 0.0, |
974 | 1 | hpgl_plot_move_relative, |
975 | 1 | false)); |
976 | 1 | hpgl_call(gs_currentpoint(pgs, &end_pt)); |
977 | 1 | } |
978 | 1.13M | if ((text_path == hpgl_text_right) && |
979 | 842k | (hpgl_get_character_extra_space_x(pgls) != 0)) { |
980 | 60.2k | hpgl_get_char_width(pgls, ch, &width); |
981 | 60.2k | end_pt.x = start_pt.x + width / scale.x; |
982 | 60.2k | hpgl_call(hpgl_add_point_to_path |
983 | 60.2k | (pgls, end_pt.x, end_pt.y, hpgl_plot_move_absolute, |
984 | 60.2k | false)); |
985 | 60.2k | } |
986 | 1.13M | } |
987 | | /* |
988 | | * Adjust the final position according to the text path. |
989 | | */ |
990 | 1.26M | switch (text_path) { |
991 | 951k | case hpgl_text_right: |
992 | 951k | break; |
993 | | |
994 | 158k | case hpgl_text_down: |
995 | 158k | { |
996 | 158k | hpgl_real_t height; |
997 | | |
998 | 158k | hpgl_call(hpgl_get_current_cell_height(pgls, &height)); |
999 | 158k | hpgl_call(hpgl_add_point_to_path |
1000 | 158k | (pgls, start_pt.x, end_pt.y - height / scale.y, |
1001 | 158k | hpgl_plot_move_absolute, false)); |
1002 | | |
1003 | 158k | } |
1004 | 158k | break; |
1005 | | |
1006 | 158k | case hpgl_text_left: |
1007 | 83.9k | hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x, start_pt.y, |
1008 | 83.9k | hpgl_plot_move_absolute, |
1009 | 83.9k | false)); |
1010 | 83.9k | break; |
1011 | 83.9k | case hpgl_text_up: |
1012 | 73.7k | { |
1013 | 73.7k | hpgl_real_t height; |
1014 | | |
1015 | 73.7k | hpgl_call(hpgl_get_current_cell_height(pgls, &height)); |
1016 | 73.7k | hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x, |
1017 | 73.7k | end_pt.y + |
1018 | 73.7k | height / scale.y, |
1019 | 73.7k | hpgl_plot_move_absolute, |
1020 | 73.7k | false)); |
1021 | 73.7k | } |
1022 | 73.7k | break; |
1023 | 1.26M | } |
1024 | | |
1025 | 1.26M | gs_setmatrix(pgs, &save_ctm); |
1026 | 1.26M | hpgl_call(gs_currentpoint(pgs, &end_pt)); |
1027 | 1.26M | if (!use_show) |
1028 | 1.26M | hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_character)); |
1029 | | |
1030 | 1.26M | hpgl_call(hpgl_add_point_to_path(pgls, |
1031 | 1.26M | end_pt.x, |
1032 | 1.26M | end_pt.y, |
1033 | 1.26M | hpgl_plot_move_absolute, true)); |
1034 | 1.26M | } |
1035 | | |
1036 | 1.26M | return 0; |
1037 | 1.26M | } |
1038 | | |
1039 | | /* Determine whether labels can concatenate. */ |
1040 | | /* Note that LO requires a pre-scan iff this is false. */ |
1041 | | static bool |
1042 | | hpgl_can_concat_labels(const hpgl_state_t * pgls) |
1043 | 50.1k | { /* The following is per TRM 23-78. */ |
1044 | 50.1k | static const byte can_concat[22] = { |
1045 | 50.1k | 0, 9, 1, 3, 8, 0, 2, 12, 4, 6, |
1046 | 50.1k | 0, 9, 1, 3, 8, 0, 2, 12, 4, 6, |
1047 | 50.1k | 0, 9 |
1048 | 50.1k | }; |
1049 | | |
1050 | 50.1k | return (can_concat[pgls->g.label.origin] & |
1051 | 50.1k | (1 << pgls->g.character.text_path)) != 0; |
1052 | 50.1k | } |
1053 | | |
1054 | | /* return relative coordinates to compensate for origin placement -- LO */ |
1055 | | static int |
1056 | | hpgl_get_character_origin_offset(hpgl_state_t * pgls, int origin, |
1057 | | hpgl_real_t width, hpgl_real_t height, |
1058 | | gs_point * offset) |
1059 | 64.8k | { |
1060 | 64.8k | double pos_x = 0.0, pos_y = 0.0; |
1061 | 64.8k | double off_x = 0.0, off_y = 0.0; |
1062 | 64.8k | hpgl_real_t adjusted_height = height; |
1063 | | |
1064 | | #ifdef CHECK_UNIMPLEMENTED |
1065 | | if (pgls->g.character.extra_space.x != 0 |
1066 | | || pgls->g.character.extra_space.y != 0) |
1067 | | dmprintf(pgls->memory, |
1068 | | "warning origin offset with non zero extra space not supported\n"); |
1069 | | #endif |
1070 | 64.8k | adjusted_height /= 1.6; |
1071 | 64.8k | if (hpgl_is_currentfont_stick(pgls)) |
1072 | 25.8k | adjusted_height /= 1.4; |
1073 | | |
1074 | | /* offset values specified by the documentation */ |
1075 | 64.8k | if ((origin >= 11 && origin <= 14) || (origin >= 16 && origin <= 19)) { |
1076 | 18.7k | if (hpgl_is_currentfont_stick(pgls)) { |
1077 | 4.47k | off_x = off_y = 0.33 * adjusted_height; |
1078 | 14.2k | } else { |
1079 | | /* the documentation says this should be .25, experiments |
1080 | | indicate .33 like stick fonts. */ |
1081 | 14.2k | off_x = off_y = 0.33 * adjusted_height; |
1082 | 14.2k | } |
1083 | 18.7k | } |
1084 | | |
1085 | 64.8k | switch (origin) { |
1086 | 2.33k | case 11: |
1087 | 2.33k | pos_x = -off_x; |
1088 | 2.33k | pos_y = -off_y; |
1089 | 19.8k | case 1: |
1090 | 19.8k | break; |
1091 | 2.45k | case 12: |
1092 | 2.45k | pos_x = -off_x; |
1093 | 5.86k | case 2: |
1094 | 5.86k | pos_y = .5 * adjusted_height; |
1095 | 5.86k | break; |
1096 | 2.59k | case 13: |
1097 | 2.59k | pos_x = -off_x; |
1098 | 2.59k | pos_y = off_y; |
1099 | 5.64k | case 3: |
1100 | 5.64k | pos_y += adjusted_height; |
1101 | 5.64k | break; |
1102 | 2.38k | case 14: |
1103 | 2.38k | pos_y = -off_y; |
1104 | 5.10k | case 4: |
1105 | 5.10k | pos_x = .5 * width; |
1106 | 5.10k | break; |
1107 | 2.64k | case 15: |
1108 | 9.02k | case 5: |
1109 | 9.02k | pos_x = .5 * width; |
1110 | 9.02k | pos_y = .5 * adjusted_height; |
1111 | 9.02k | break; |
1112 | 2.05k | case 16: |
1113 | 2.05k | pos_y = off_y; |
1114 | 4.70k | case 6: |
1115 | 4.70k | pos_x = .5 * width; |
1116 | 4.70k | pos_y += adjusted_height; |
1117 | 4.70k | break; |
1118 | 2.18k | case 17: |
1119 | 2.18k | pos_x = off_x; |
1120 | 2.18k | pos_y = -off_y; |
1121 | 5.07k | case 7: |
1122 | 5.07k | pos_x += width; |
1123 | 5.07k | break; |
1124 | 2.05k | case 18: |
1125 | 2.05k | pos_x = off_x; |
1126 | 4.39k | case 8: |
1127 | 4.39k | pos_x += width; |
1128 | 4.39k | pos_y = .5 * adjusted_height; |
1129 | 4.39k | break; |
1130 | 2.70k | case 19: |
1131 | 2.70k | pos_x = off_x; |
1132 | 2.70k | pos_y = off_y; |
1133 | 5.13k | case 9: |
1134 | 5.13k | pos_x += width; |
1135 | 5.13k | pos_y += adjusted_height; |
1136 | 5.13k | break; |
1137 | 0 | case 21: |
1138 | 0 | { |
1139 | | /* // LO21 prints at the current position not pcl CAP. |
1140 | | gs_matrix save_ctm; |
1141 | | gs_point pcl_pos_dev, label_origin; |
1142 | | |
1143 | | gs_currentmatrix(pgls->pgs, &save_ctm); |
1144 | | pcl_set_ctm(pgls, false); |
1145 | | hpgl_call(gs_transform(pgls->pgs, (double)pgls->cap.x, |
1146 | | (double)pgls->cap.y, &pcl_pos_dev)); |
1147 | | gs_setmatrix(pgls->pgs, &save_ctm); |
1148 | | hpgl_call(gs_itransform(pgls->pgs, (double)pcl_pos_dev.x, |
1149 | | (double)pcl_pos_dev.y, &label_origin)); |
1150 | | pos_x = -(pgls->g.pos.x - label_origin.x); |
1151 | | pos_y = (pgls->g.pos.y - label_origin.y); |
1152 | | */ |
1153 | 0 | } |
1154 | 0 | break; |
1155 | 0 | default: |
1156 | 0 | dmprintf(pgls->memory, "unknown label parameter"); |
1157 | | |
1158 | 64.8k | } |
1159 | | /* a relative move to the new position */ |
1160 | 64.8k | offset->x = pos_x; |
1161 | 64.8k | offset->y = pos_y; |
1162 | | |
1163 | | /* account for character direction and slant */ |
1164 | 64.8k | hpgl_rotation_transform_distance(pgls, offset, offset); |
1165 | 64.8k | hpgl_slant_transform_distance(pgls, offset, offset); |
1166 | | |
1167 | 64.8k | switch (pgls->g.character.text_path) { |
1168 | 11.0k | case hpgl_text_left: |
1169 | 11.0k | offset->x -= width; |
1170 | 11.0k | break; |
1171 | 12.9k | case hpgl_text_down: |
1172 | 12.9k | offset->y -= height; |
1173 | 12.9k | break; |
1174 | 40.7k | default: |
1175 | 40.7k | DO_NOTHING; |
1176 | 64.8k | } |
1177 | | |
1178 | 64.8k | { |
1179 | 64.8k | gs_matrix mat; |
1180 | | |
1181 | 64.8k | hpgl_compute_user_units_to_plu_ctm(pgls, &mat); |
1182 | 64.8k | offset->x /= mat.xx; |
1183 | 64.8k | offset->y /= mat.yy; |
1184 | 64.8k | } |
1185 | | /* convert to user units */ |
1186 | 64.8k | return 0; |
1187 | 64.8k | } |
1188 | | |
1189 | | static gs_char |
1190 | | hpgl_next_char(hpgl_state_t * pgls, byte ** ppb) |
1191 | 2.97M | { |
1192 | 2.97M | byte *pb = *ppb; |
1193 | 2.97M | gs_char chr = *pb++; |
1194 | | |
1195 | 2.97M | if (pgls->g.label.double_byte) |
1196 | 1.24k | chr = (chr << 8) + *pb++; |
1197 | 2.97M | *ppb = pb; |
1198 | 2.97M | return chr; |
1199 | 2.97M | } |
1200 | | |
1201 | | /* Prints a buffered line of characters. */ |
1202 | | /* If there is a CR, it is the last character in the buffer. */ |
1203 | | static int |
1204 | | hpgl_process_buffer(hpgl_state_t * pgls, gs_point * offset) |
1205 | 64.8k | { |
1206 | 64.8k | hpgl_real_t label_length = 0.0, label_height = 0.0; |
1207 | 64.8k | bool vertical = hpgl_text_is_vertical(pgls->g.character.text_path); |
1208 | 64.8k | int i, inc; |
1209 | 64.8k | const pcl_font_selection_t *pfs =&pgls->g.font_selection[pgls->g.font_selected]; |
1210 | | |
1211 | | /* a peculiar side effect of LABEL parsing double byte characters |
1212 | | is it leaves an extra byte in the buffer - fix that now, and |
1213 | | properly set the increment for the parallel loops below. */ |
1214 | 64.8k | if (pgls->g.label.double_byte) { |
1215 | 4 | pgls->g.label.char_count--; |
1216 | 4 | inc = 2; |
1217 | 64.8k | } else { |
1218 | 64.8k | inc = 1; |
1219 | 64.8k | } |
1220 | | |
1221 | | /* |
1222 | | * NOTE: the two loops below must be consistent with each other! |
1223 | | */ |
1224 | | |
1225 | 64.8k | { |
1226 | 64.8k | hpgl_real_t width = 0.0, height = 0.0; |
1227 | 64.8k | int save_index = pgls->g.font_selected; |
1228 | 64.8k | bool first_char_on_line = true; |
1229 | 64.8k | byte *b = pgls->g.label.buffer; |
1230 | | |
1231 | 1.55M | for (i = 0; i < pgls->g.label.char_count; i += inc) { |
1232 | 1.48M | gs_char ch = hpgl_next_char(pgls, &b); |
1233 | | |
1234 | 1.48M | if (ch < 0x20 && !pgls->g.transparent_data) |
1235 | 213k | switch (ch) { |
1236 | 472 | case BS: |
1237 | 472 | if (width == 0.0) { /* BS as first char of string */ |
1238 | 0 | hpgl_call(hpgl_ensure_font(pgls)); |
1239 | 0 | hpgl_get_char_width(pgls, ' ', &width); |
1240 | 0 | hpgl_call(hpgl_get_current_cell_height |
1241 | 0 | (pgls, &height)); |
1242 | 0 | } |
1243 | 472 | if (vertical) { /* Vertical text path, back up in Y. */ |
1244 | 28 | label_height -= height; |
1245 | 28 | if (label_height < 0.0) |
1246 | 0 | label_height = 0.0; |
1247 | 444 | } else { /* Horizontal text path, back up in X. */ |
1248 | 444 | label_length -= width; |
1249 | 444 | if (label_length < 0.0) |
1250 | 0 | label_length = 0.0; |
1251 | 444 | } |
1252 | 472 | continue; |
1253 | 15.5k | case LF: |
1254 | 15.5k | first_char_on_line = true; |
1255 | 15.5k | continue; |
1256 | 10.4k | case CR: |
1257 | 10.4k | continue; |
1258 | 535 | case FF: |
1259 | 535 | continue; |
1260 | 1.13k | case HT: |
1261 | 1.13k | hpgl_call(hpgl_ensure_font(pgls)); |
1262 | 1.13k | hpgl_get_char_width(pgls, ' ', &width); |
1263 | 1.13k | width *= 5; |
1264 | 1.13k | goto acc_ht; |
1265 | 50.1k | case SI: |
1266 | 50.1k | hpgl_select_font_pri_alt(pgls, 0); |
1267 | 50.1k | continue; |
1268 | 50.3k | case SO: |
1269 | 50.3k | hpgl_select_font_pri_alt(pgls, 1); |
1270 | 50.3k | continue; |
1271 | 85.2k | default: |
1272 | 85.2k | break; |
1273 | 213k | } |
1274 | 1.35M | hpgl_call(hpgl_ensure_font(pgls)); |
1275 | | /* Hackery... I believe that this *ought* to be handled by resizing every character in the |
1276 | | * proportional font as it is drawn, so that it is the size of mandated character (from the SI operator). |
1277 | | * This would mean that the calculation of the width would be correct (num characters * character width) |
1278 | | * which is what gets returned in label_length above. |
1279 | | * But that isn't what hpgl_print_char does. It does use the scale returned by hpgl_current_scale() |
1280 | | * to resize each character as it is drawn, which works perfectly well for a fixed pitch font, but |
1281 | | * results in each character being potentially a different size with a proportional font. |
1282 | | * Ideally I'd fix hpgl_print_char, but I can't see how to do that. It would mean moving a lot of |
1283 | | * logic from here into hpgl_print_char() I think. |
1284 | | * So here, pretend that we are not writing in absolute mode, that means the length returned |
1285 | | * in label_length will be the actual width of the (unscaled) characters. |
1286 | | * Then see below.... |
1287 | | */ |
1288 | 1.35M | if (pgls->g.character.size_mode == hpgl_size_absolute && pfs->params.proportional_spacing) { |
1289 | 39.8k | pgls->g.character.size_mode = hpgl_size_not_set; |
1290 | 39.8k | hpgl_get_char_width(pgls, ch, &width); |
1291 | 39.8k | pgls->g.character.size_mode = hpgl_size_absolute; |
1292 | 39.8k | } else |
1293 | 1.31M | hpgl_get_char_width(pgls, ch, &width); |
1294 | 1.35M | acc_ht:hpgl_call(hpgl_get_current_cell_height |
1295 | 1.35M | (pgls, &height)); |
1296 | 1.35M | if (vertical) { |
1297 | 242k | if (width > label_length) |
1298 | 63.7k | label_length = width; |
1299 | 242k | if (!first_char_on_line) |
1300 | 224k | label_height += height; |
1301 | 18.0k | else |
1302 | 18.0k | first_char_on_line = false; |
1303 | 1.11M | } else { /* Horizontal text path: sum widths, take max of heights. */ |
1304 | 1.11M | label_length += width; |
1305 | 1.11M | if (height > label_height) |
1306 | 60.5k | label_height = height; |
1307 | 1.11M | } |
1308 | 1.35M | } |
1309 | 64.8k | hpgl_select_font_pri_alt(pgls, save_index); |
1310 | 64.8k | } |
1311 | | /* The second part of the hackery. We've calculated the width of the string using 'ordinary' |
1312 | | * drawing. We now find out what scaling will be used when drawing the text in 'absolute' mode. |
1313 | | * Then we revert back to 'not set' mode, and retrieve that scaling. That gives us the scaling |
1314 | | * that ws used (see above) to retrieve label_length. |
1315 | | * Now we know the two scalings, we can adjust label_length so that it will be the actual width |
1316 | | * of the string we draw. We can then use that to adjust the starting position so that it is |
1317 | | * correct. |
1318 | | */ |
1319 | 64.8k | if (pgls->g.character.size_mode == hpgl_size_absolute && pfs->params.proportional_spacing) { |
1320 | 5.35k | gs_point unset_scale, absolute_scale; |
1321 | | |
1322 | 5.35k | pgls->g.character.size_mode = hpgl_size_not_set; |
1323 | 5.35k | absolute_scale = hpgl_current_char_scale(pgls); |
1324 | 5.35k | pgls->g.character.size_mode = hpgl_size_absolute; |
1325 | 5.35k | unset_scale = hpgl_current_char_scale(pgls); |
1326 | 5.35k | label_length = label_length * (unset_scale.x / absolute_scale.x); |
1327 | 5.35k | } |
1328 | 64.8k | hpgl_call(hpgl_get_character_origin_offset(pgls, pgls->g.label.origin, |
1329 | 64.8k | label_length, label_height, |
1330 | 64.8k | offset)); |
1331 | | |
1332 | | /* now add the offsets in relative plu coordinates */ |
1333 | 64.8k | hpgl_call(hpgl_add_point_to_path(pgls, -offset->x, -offset->y, |
1334 | 64.8k | hpgl_plot_move_relative, false)); |
1335 | | |
1336 | 64.8k | { |
1337 | 64.8k | byte *b = pgls->g.label.buffer; |
1338 | | |
1339 | 1.55M | for (i = 0; i < pgls->g.label.char_count; i += inc) { |
1340 | 1.48M | gs_char ch = hpgl_next_char(pgls, &b); |
1341 | | |
1342 | 1.48M | if (ch < 0x20 && !pgls->g.transparent_data) { |
1343 | 213k | hpgl_real_t spaces, lines; |
1344 | | |
1345 | 213k | switch (ch) { |
1346 | 472 | case BS: |
1347 | 472 | spaces = -1, lines = 0; |
1348 | 472 | break; |
1349 | 15.5k | case LF: |
1350 | | /* |
1351 | | * If the text path is vertical, we must use the |
1352 | | * computed label (horizontal) width, not the width |
1353 | | * of a space. |
1354 | | */ |
1355 | 15.5k | if (vertical) { |
1356 | 741 | const pcl_font_selection_t *pfs = |
1357 | 741 | &pgls->g.font_selection[pgls->g. |
1358 | 741 | font_selected]; |
1359 | 741 | hpgl_real_t label_advance; |
1360 | | |
1361 | | /* Handle size. */ |
1362 | 741 | if (pgls->g.character.size_mode == |
1363 | 741 | hpgl_size_not_set) { |
1364 | | /* Scale fixed-width fonts by pitch, variable-width by height. */ |
1365 | 615 | if (pfs->params.proportional_spacing) { |
1366 | 515 | if (pl_font_is_scalable(pfs->font)) |
1367 | 515 | label_advance = |
1368 | 515 | hpgl_points_2_plu(pgls, |
1369 | 515 | pfs->params. |
1370 | 515 | height_4ths / |
1371 | 515 | 4.0); |
1372 | 0 | else { |
1373 | 0 | double ratio = |
1374 | 0 | (double)pfs->params.height_4ths / |
1375 | 0 | pfs->font->params.height_4ths; |
1376 | 0 | label_advance = |
1377 | 0 | ratio * inches_2_plu(1.0 / |
1378 | 0 | pfs->font-> |
1379 | 0 | resolution. |
1380 | 0 | x); |
1381 | 0 | } |
1382 | 515 | } else |
1383 | | /*** WRONG ****/ |
1384 | 100 | label_advance = |
1385 | 100 | hpgl_points_2_plu(pgls, |
1386 | 100 | pl_fp_pitch_cp |
1387 | 100 | (&pfs->params) / |
1388 | 100 | ((pl_fp_pitch_cp |
1389 | 100 | (&pfs->font-> |
1390 | 100 | params) / |
1391 | 100 | 10.0))); |
1392 | 615 | if (hpgl_get_character_extra_space_x(pgls) != |
1393 | 615 | 0) |
1394 | 26 | label_advance *= |
1395 | 26 | 1.0 + |
1396 | 26 | hpgl_get_character_extra_space_x |
1397 | 26 | (pgls); |
1398 | 615 | } else { |
1399 | | /* |
1400 | | * Note that the CTM takes P1/P2 into account unless |
1401 | | * an absolute character size is in effect. |
1402 | | * |
1403 | | * |
1404 | | * HACKS - I am not sure what this should be the |
1405 | | * actual values ??? |
1406 | | */ |
1407 | 126 | label_advance = |
1408 | 126 | pgls->g.character.size.x * 1.5 * 1.25; |
1409 | 126 | if (pgls->g.character.size_mode == |
1410 | 126 | hpgl_size_relative) |
1411 | 89 | label_advance *= |
1412 | 89 | pgls->g.P2.x - pgls->g.P1.x; |
1413 | 126 | if (pgls->g.bitmap_fonts_allowed) /* no mirroring */ |
1414 | 0 | label_advance = fabs(label_advance); |
1415 | 126 | } |
1416 | 741 | hpgl_call(hpgl_move_cursor_by_characters(pgls, 0, -1, |
1417 | 741 | &label_advance)); |
1418 | 741 | continue; |
1419 | 741 | } |
1420 | 14.8k | spaces = 0, lines = -1; |
1421 | 14.8k | break; |
1422 | 10.4k | case CR: |
1423 | 10.4k | hpgl_call(hpgl_do_CR(pgls)); |
1424 | 10.4k | continue; |
1425 | 10.4k | case FF: |
1426 | | /* does nothing */ |
1427 | 535 | spaces = 0, lines = 0; |
1428 | 535 | break; |
1429 | 1.13k | case HT: |
1430 | | /* appears to expand to 5 spaces */ |
1431 | 1.13k | spaces = 5, lines = 0; |
1432 | 1.13k | break; |
1433 | 50.1k | case SI: |
1434 | 50.1k | hpgl_select_font_pri_alt(pgls, 0); |
1435 | 50.1k | continue; |
1436 | 50.3k | case SO: |
1437 | 50.3k | hpgl_select_font_pri_alt(pgls, 1); |
1438 | 50.3k | continue; |
1439 | 85.2k | default: |
1440 | 85.2k | goto print; |
1441 | 213k | } |
1442 | 16.9k | hpgl_call(hpgl_move_cursor_by_characters(pgls, spaces, lines, |
1443 | 16.9k | (const hpgl_real_t *)0)); |
1444 | 16.9k | continue; |
1445 | 16.9k | } |
1446 | 1.35M | print:{ |
1447 | | /* if this a printable character print it |
1448 | | otherwise continue, a character can be |
1449 | | printable and undefined in which case |
1450 | | it is printed as a space character */ |
1451 | 1.35M | const pcl_font_selection_t *pfs = |
1452 | 1.35M | &pgls->g.font_selection[pgls->g.font_selected]; |
1453 | 1.35M | if (!char_is_printable(pfs->font, pfs->map, ch, |
1454 | 1.35M | (pfs->params. |
1455 | 1.35M | typeface_family & 0xfff) == |
1456 | 1.35M | STICK_FONT_TYPEFACE, |
1457 | 1.35M | pgls->g.transparent_data)) |
1458 | 89.2k | continue; |
1459 | 1.35M | } |
1460 | 1.26M | hpgl_call(hpgl_ensure_font(pgls)); |
1461 | 1.26M | hpgl_call(hpgl_print_char(pgls, ch)); |
1462 | 1.26M | } |
1463 | 64.8k | } |
1464 | 64.8k | pgls->g.label.char_count = 0; |
1465 | 64.8k | return 0; |
1466 | 64.8k | } |
1467 | | |
1468 | | /** |
1469 | | * used by hpgl_LB() to find the end of a label |
1470 | | * |
1471 | | * 8bit and 16bit label terminator check |
1472 | | * (prev << 8) & curr -> 16 bit |
1473 | | * have_16bits allows per byte call with true on 16bit boundary. |
1474 | | */ |
1475 | | static bool |
1476 | | is_terminator(hpgl_state_t * pgls, byte prev, byte curr, bool have_16bits) |
1477 | 1.61M | { |
1478 | 1.61M | return |
1479 | 1.61M | pgls->g.label.double_byte ? |
1480 | 1.80k | (have_16bits && prev == 0 && curr == pgls->g.label.terminator) : |
1481 | 1.61M | (curr == pgls->g.label.terminator); |
1482 | 1.61M | } |
1483 | | |
1484 | 4.89M | #define GL_LB_HAVE_16BITS (pgls->g.label.have_16bits) |
1485 | 6.46M | #define GL_LB_CH (pgls->g.label.ch) |
1486 | 3.28M | #define GL_LB_PREV_CH (pgls->g.label.prev_ch) |
1487 | | |
1488 | | /* LB ..text..terminator */ |
1489 | | int |
1490 | | hpgl_LB(hpgl_args_t * pargs, hpgl_state_t * pgls) |
1491 | 51.0k | { |
1492 | 51.0k | const byte *p = pargs->source.ptr; |
1493 | 51.0k | const byte *rlimit = pargs->source.limit; |
1494 | 51.0k | bool print_terminator = pgls->g.label.print_terminator; |
1495 | 51.0k | int code = 0; |
1496 | | |
1497 | 51.0k | if (pargs->phase == 0) { |
1498 | | /* initialize the character buffer and CTM first time only */ |
1499 | 50.4k | hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector)); |
1500 | 50.4k | hpgl_call(hpgl_init_label_buffer(pgls)); |
1501 | 50.4k | hpgl_call(hpgl_set_ctm(pgls)); |
1502 | 50.4k | code = hpgl_set_clipping_region(pgls, hpgl_rm_vector); |
1503 | 50.4k | if (code < 0) { |
1504 | 0 | hpgl_destroy_label_buffer(pgls); |
1505 | 0 | return code; |
1506 | 0 | } |
1507 | 50.4k | pgls->g.label.initial_pos = pgls->g.pos; |
1508 | 50.4k | hpgl_call(hpgl_add_point_to_path |
1509 | 50.4k | (pgls, pgls->g.pos.x, pgls->g.pos.y, |
1510 | 50.4k | hpgl_plot_move_absolute, true)); |
1511 | 50.4k | GL_LB_HAVE_16BITS = true; |
1512 | 50.4k | GL_LB_CH = 0xff; |
1513 | 50.4k | GL_LB_PREV_CH = 0xff; /* for two byte terminators */ |
1514 | 50.4k | pargs->phase = 1; |
1515 | 50.4k | } |
1516 | | |
1517 | 1.61M | while (p < rlimit) { |
1518 | | /* This is not ugly and unintuitive */ |
1519 | 1.61M | GL_LB_HAVE_16BITS = !GL_LB_HAVE_16BITS; |
1520 | 1.61M | GL_LB_PREV_CH = GL_LB_CH; |
1521 | 1.61M | GL_LB_CH = *++p; |
1522 | 1.61M | if_debug1m('i', pgls->memory, |
1523 | 1.61M | (GL_LB_CH == '\\' ? " \\%c" : GL_LB_CH >= 33 |
1524 | 1.61M | && GL_LB_CH <= 126 ? " %c" : " \\%03o"), GL_LB_CH); |
1525 | 1.61M | if (is_terminator(pgls, GL_LB_PREV_CH, GL_LB_CH, GL_LB_HAVE_16BITS)) { |
1526 | 50.1k | if (!print_terminator) { |
1527 | 50.1k | gs_point lo_offsets; |
1528 | | |
1529 | 50.1k | code = hpgl_process_buffer(pgls, &lo_offsets); |
1530 | 50.1k | hpgl_destroy_label_buffer(pgls); |
1531 | 50.1k | if (code < 0) { |
1532 | 0 | hpgl_free_stick_fonts(pgls); |
1533 | 0 | return code; |
1534 | 0 | } |
1535 | 50.1k | pargs->source.ptr = p; |
1536 | | /* |
1537 | | * Depending on the DV/LO combination, conditionally |
1538 | | * restore the initial position, per TRM 23-78. |
1539 | | */ |
1540 | 50.1k | if (!hpgl_can_concat_labels(pgls)) { |
1541 | 30.1k | code = hpgl_add_point_to_path(pgls, |
1542 | 30.1k | pgls->g. |
1543 | 30.1k | carriage_return_pos.x, |
1544 | 30.1k | pgls->g. |
1545 | 30.1k | carriage_return_pos.y, |
1546 | 30.1k | hpgl_plot_move_absolute, |
1547 | 30.1k | true); |
1548 | 30.1k | } else { |
1549 | | /* undo the label origin offsets */ |
1550 | 19.9k | code = hpgl_add_point_to_path |
1551 | 19.9k | (pgls, lo_offsets.x, lo_offsets.y, |
1552 | 19.9k | hpgl_plot_move_relative, false); |
1553 | 19.9k | } |
1554 | | /* also clean up stick fonts - they are likely to |
1555 | | become dangling references in the current font |
1556 | | scheme since they don't have a dictionary entry */ |
1557 | 50.1k | hpgl_free_stick_fonts(pgls); |
1558 | 50.1k | if (code < 0) |
1559 | 0 | return code; |
1560 | | /* clear the current path terminating carriage |
1561 | | returns and linefeeds will leave "moveto's" in |
1562 | | the path */ |
1563 | 50.1k | hpgl_call(hpgl_clear_current_path(pgls)); |
1564 | 50.1k | return 0; |
1565 | 50.1k | } |
1566 | | /* |
1567 | | * Process the character in the ordinary way, then come here |
1568 | | * again. We do things this way to simplify the case where |
1569 | | * the terminator is a control character. |
1570 | | */ |
1571 | 8 | --p; |
1572 | 8 | print_terminator = false; |
1573 | 8 | } |
1574 | | |
1575 | | /* process the buffer for a carriage return so that we can |
1576 | | treat the label origin correctly, and initialize a new |
1577 | | buffer */ |
1578 | | |
1579 | 1.56M | hpgl_call(hpgl_buffer_char(pgls, GL_LB_CH)); |
1580 | 1.56M | if (GL_LB_CH == CR && !pgls->g.transparent_data) { |
1581 | 10.4k | gs_point lo_offsets; |
1582 | | |
1583 | 10.4k | code = hpgl_process_buffer(pgls, &lo_offsets); |
1584 | 10.4k | hpgl_destroy_label_buffer(pgls); |
1585 | 10.4k | if (code >= 0) |
1586 | 10.4k | code = hpgl_init_label_buffer(pgls); |
1587 | 10.4k | if (code < 0) { |
1588 | 0 | hpgl_free_stick_fonts(pgls); |
1589 | 0 | return code; |
1590 | 0 | } |
1591 | 10.4k | } |
1592 | 1.56M | } |
1593 | 948 | pargs->source.ptr = p; |
1594 | 948 | return gs_error_NeedInput; |
1595 | 51.0k | } |
1596 | | |
1597 | | void |
1598 | | hpgl_free_531_fonts(hpgl_state_t * pgls) |
1599 | 0 | { |
1600 | 0 | pl_font_t *font = &pgls->g.dl_531_font[pgls->g.font_selected]; |
1601 | |
|
1602 | 0 | if (font->pfont != 0) { |
1603 | 0 | gs_free_object(pgls->memory, font->pfont, "531 font"); |
1604 | 0 | font->pfont = 0; |
1605 | 0 | } |
1606 | 0 | return; |
1607 | 0 | } |
1608 | | |
1609 | | void |
1610 | | hpgl_free_stick_fonts(hpgl_state_t * pgls) |
1611 | 367k | { |
1612 | 367k | pcl_font_selection_t *pfs = |
1613 | 367k | &pgls->g.font_selection[pgls->g.font_selected]; |
1614 | 367k | pl_font_t *font = &pgls->g.stick_font[pgls->g.font_selected] |
1615 | 367k | [pfs->params.proportional_spacing]; |
1616 | | |
1617 | 367k | pl_font_glyph_width_cache_remove_nodes(font); |
1618 | | |
1619 | | /* no stick fonts - nothing to do */ |
1620 | 367k | if (font->pfont == 0) |
1621 | 271k | return; |
1622 | | |
1623 | 96.2k | gs_free_object(pgls->memory, font->pfont, "stick/arc font"); |
1624 | 96.2k | font->pfont = 0; |
1625 | 96.2k | return; |
1626 | 367k | } |
1627 | | |
1628 | | int |
1629 | | hpgl_print_symbol_mode_char(hpgl_state_t * pgls) |
1630 | 4.26k | { |
1631 | | /* save the original origin since symbol mode character are |
1632 | | always centered */ |
1633 | 4.26k | int saved_origin = pgls->g.label.origin; |
1634 | 4.26k | gs_point save_pos = pgls->g.pos; |
1635 | 4.26k | gs_point lo_offsets; |
1636 | 4.26k | int code; |
1637 | | |
1638 | 4.26k | hpgl_call(hpgl_gsave(pgls)); |
1639 | | /* HAS this need checking. I don't know how text direction |
1640 | | and label origin interact in symbol mode */ |
1641 | 4.26k | pgls->g.label.origin = 5; |
1642 | | /* HAS - alot of work for one character */ |
1643 | 4.26k | if (((code = hpgl_clear_current_path(pgls)) >= 0) && |
1644 | 4.26k | ((code = hpgl_init_label_buffer(pgls)) >= 0)) { |
1645 | 4.26k | if ((code = hpgl_buffer_char(pgls, pgls->g.symbol_mode)) >= 0) |
1646 | 4.26k | code = hpgl_process_buffer(pgls, &lo_offsets); |
1647 | 4.26k | hpgl_destroy_label_buffer(pgls); |
1648 | 4.26k | } |
1649 | 4.26k | { |
1650 | | /* must grestore here from earlier gsave */ |
1651 | 4.26k | int code2 = hpgl_grestore(pgls); |
1652 | 4.26k | if (code >= 0) code = code2; |
1653 | 4.26k | } |
1654 | 4.26k | hpgl_free_stick_fonts(pgls); |
1655 | | /* restore the origin */ |
1656 | 4.26k | pgls->g.label.origin = saved_origin; |
1657 | 4.26k | if (code >= 0) |
1658 | 4.26k | hpgl_call(hpgl_set_current_position(pgls, &save_pos)); |
1659 | 4.26k | return code; |
1660 | 4.26k | } |
1661 | | |
1662 | | /* Initialization */ |
1663 | | static int |
1664 | | pglabel_do_registration(pcl_parser_state_t * pcl_parser_state, |
1665 | | gs_memory_t * mem) |
1666 | 18.4k | { /* Register commands */ |
1667 | 18.4k | DEFINE_HPGL_COMMANDS(mem) |
1668 | 18.4k | HPGL_COMMAND('C', 'P', hpgl_CP, |
1669 | 18.4k | hpgl_cdf_lost_mode_cleared | hpgl_cdf_pcl_rtl_both), |
1670 | | /* LB also has special argument parsing. */ |
1671 | 18.4k | HPGL_COMMAND('L', 'B', hpgl_LB, |
1672 | 18.4k | hpgl_cdf_polygon | hpgl_cdf_lost_mode_cleared | |
1673 | 18.4k | hpgl_cdf_pcl_rtl_both), |
1674 | 18.4k | END_HPGL_COMMANDS |
1675 | 18.4k | return 0; |
1676 | 18.4k | } |
1677 | | |
1678 | | const pcl_init_t pglabel_init = { |
1679 | | pglabel_do_registration, 0 |
1680 | | }; |