1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
/* Copyright (C) 1996, 1997, 1998 Aladdin Enterprises. All rights
reserved. Unauthorized use, copying, and/or distribution
prohibited. */
/* pctext.c - PCL5 text printing commands */
#include "gx.h"
#include "gsimage.h"
#include "plvalue.h"
#include "plvocab.h"
#include "pcommand.h"
#include "pcstate.h"
#include "pcdraw.h"
#include "pcfont.h"
#include "pcursor.h"
#include "gdebug.h"
#include "gscoord.h"
#include "gsline.h"
#include "gspaint.h"
#include "gspath.h"
#include "gspath2.h"
#include "gsrop.h"
#include "gsstate.h"
#include "gxchar.h"
#include "gxfont.h" /* for setting next_char proc */
#include "gxstate.h"
/* Define the text parsing methods. */
private const pcl_text_parsing_method_t pcl_tpm_0 = pcl_tpm_0_data,
pcl_tpm_21 = pcl_tpm_21_data,
pcl_tpm_31 = pcl_tpm_31_data,
pcl_tpm_38 = pcl_tpm_38_data;
/* pseudo-"dots" (actually 1/300" units) used in underline only */
#define dots(n) ((float)(7200 / 300 * n))
/*
* Get next character procedure, to be passed to the graphic library font
* machinery. Note that this will always parse two bytes, always assumes
* two bytes are in the string.
*/
private int
get_gs_next_char(
gs_show_enum * penum,
gs_char * pchr
)
{
const byte * pb = penum->text.data.bytes;
if (penum->index >= 2)
return 2;
*pchr = (((uint)pb[0]) << 8) + pb[1];
penum->index += 2;
return 0;
}
/*
* Install a font in the graphic state.
*/
private void
set_gs_font(
pcl_state_t * pcs
)
{
gs_font * pfont = (gs_font *)pcs->font->pfont;
pfont->procs.next_char = get_gs_next_char;
gs_setfont(pcs->pgs, pfont);
}
/*
* Mapping via symbol sets. Note that this applies only to unbound fonts.
*/
private gs_char
map_symbol(
const pl_font_t * pfont,
const pl_symbol_map_t * psm,
gs_char chr
)
{
uint first_code, last_code;
/*
* If there is no symbol map we assume the the character
* implicitly indexes the font.
*/
if (psm == 0) {
if ( (pfont->scaling_technology == plfst_TrueType) &&
(pfont->storage == pcds_internal) )
return chr + 0xf000;
return chr;
}
first_code = pl_get_uint16(psm->first_code);
last_code = pl_get_uint16(psm->last_code);
/*
* If chr is double-byte but the symbol map is only single-byte,
* just return chr (it is not clear this make any sense - jan).
*/
if ((chr < first_code) || (chr > last_code))
return ((last_code <= 0xff) && (chr > 0xff) ? chr : 0xffff);
else {
pl_glyph_vocabulary_t fgv = /* font glyph vocabulary */
(pl_glyph_vocabulary_t)(~pfont->character_complement[7] & 07);
gs_char code = psm->codes[chr - first_code];
/* simple common case - the glyph vocabs match */
if ( pl_symbol_map_vocabulary(psm) == fgv )
return code;
/* font wants unicode and we have mapped an msl symbol set */
if ( ( pl_symbol_map_vocabulary(psm) == plgv_MSL ) &&
( fgv == plgv_Unicode ) ) {
if ( psm->mapping_type != PLGV_M2U_MAPPING )
/* font selection should not have given us this */
return_error(gs_error_invalidfont);
else
return pl_map_MSL_to_Unicode(code,
(psm->id[0] << 8) + psm->id[1]);
}
/* font wants msl coding we have mapped unicode */
if ( ( pl_symbol_map_vocabulary(psm) == plgv_Unicode ) &&
( fgv == plgv_MSL ) ) {
if ( psm->mapping_type != PLGV_U2M_MAPPING )
/* symbol set doesn't support the mapping - should not happen */
return_error(gs_error_invalidfont);
else
return pl_map_Unicode_to_MSL(code,
(psm->id[0] << 8) + psm->id[1]);
} else
return 0xffff;
}
}
/*
* Check if a character code is considered "printable" by given symbol set.
*/
private bool
is_printable(
const pl_symbol_map_t * psm,
gs_char chr
)
{
if ((psm == 0) || (psm->type >= 2))
return true;
else if (psm->type == 1)
chr &= 0x7f;
return (chr >= ' ') && (chr <= '\177');
}
/*
* Retrieve the next character identifier from a string.
*
* Both the string pointer and the length are modified.
*
* NB: this routine MAY INSTALL A SUBSTITUTE FONT. this is the case even if
* the return value indicates that a character has not been found.
*
* The final operand is true if the text was provided via the literal
* (transparent) text command: ESC & p <nbytes> X. This distinction is
* important for characters that are not considered printable by the
* current symbol set. Normally, such characters are ignored. But if they
* resulted from the literal (transparent) text command, they are handled as
* spaces. Characters that are mapped by the symbol set but are not in a font
* are always dealt with as space characters.
*
* The special handling provided for the character code 32 below is not,
* in fact, correct. PCL fonts may map non-space characters to code 32, and
* if this is done no special handling is provided for this code; in PCL,
* a space character is a character not present in the font. Unfortunately,
* some of the resident fonts used have explicit space characters, and to
* handle the hmi properly when these fonts are used, this code must handle
* fonts that have actual characters at code 32 improperly.
*
* Returns 0 on success, 2 if the string is exhausted. Note that it is not an
* error for the string to end in the middle of a 2-byte sequence.
*/
private int
get_next_char(
pcl_state_t * pcs,
const byte ** ppb,
uint * plen,
gs_char * pchr,
bool * pis_space,
bool literal
)
{
const byte * pb = *ppb;
int len = *plen;
gs_char chr;
gs_matrix dummy_mat;
if (len <= 0)
return 2;
chr = *pb++;
len--;
if (pcl_char_is_2_byte(chr, pcs->text_parsing_method) && (len > 0)) {
chr = (chr << 8) + *pb++;
len--;
*pis_space = false;
} else
*pis_space = (chr == ' ');
*ppb = pb;
*plen = len;
/* check if the code is considered "printable" in the current symbol set */
if (!is_printable(pcs->map, chr)) {
*pis_space = literal;
*pchr = 0xffff;
return 0;
}
/* map the symbol. If it fails to map, quit now */
chr = map_symbol(pcs->font, pcs->map, chr);
*pchr = chr;
if (chr == 0xffff) {
*pis_space = true;
return 0;
}
/* check if the character is in the font */
if (pl_font_includes_char(pcs->font, NULL, &dummy_mat, chr))
return 0;
/*
* The character is not in the font. For unbound fonts, a substitute font
* may be used.
*/
if (pcs->map == 0) {
*pis_space = true;
*pchr = 0xffff;
return 0;
}
pcl_decache_font(pcs, pcs->font_selected);
if (pcl_recompute_substitute_font(pcs, chr) < 0) {
*pis_space = true;
*pchr = 0xffff;
} else
set_gs_font(pcs);
return 0;
}
/*
* Determine the advance vector of a character. In PCL, advance vectors are
* one-dimensional.
*/
private int
char_width(
const pcl_state_t * pcs,
const char * pbuff,
floatp * pwidth
)
{
gs_show_enum * penum = pcs->penum;
int code = gs_stringwidth_n_init(penum, pcs->pgs, pbuff, 2);
if (code >= 0)
code = gs_show_next(penum);
if (code >= 0) {
gs_point ppt;
gs_show_width(penum, &ppt);
*pwidth = ppt.x;
}
return code;
}
/*
* draw the opaque background of a character.
*
* In the graphic library, characters are masks, hence they are always
* transparent. Not so in PCL, where characters may be either opaque or
* transparent.
*
* To deal with this dichotomy, opaque characters are rendered as a pair of
* masks. One is the normal character mask; the other is the bounding box of
* the character less the character itself.
*
* The manner in which the second mask is formed varies based on the font type.
* For bitmap fonts, the inverse mask is formed as an imagemask object, with
* inverted polarity. For scalable fonts (which have only provided a path),
* the inverse is formed by adding the bounding box rectangle as a path to
* the character path, and using eofill on the resultant path.
*
* Special handling is required to achieve the desired raster operation on the
* "background" mask. From the point of view of the graphic library, the
* background mask is a normal mask, and hence would utiltise the S = 0
* portion of the current logical operation (recall that rop's are expressed
* in an additive sense). The desired effect is, however, the S = 1 portion
* of the current rop, so the current rop must be inverted in the sense of the
* source to achive the desired result. In principle, the S = 1 porition of
* the background rop should be set to the no-op rop, but this is not necessary
* as the source is a mask.
*
* An additional complication arises from the specification provided by HP for
* handling the source opaque, pattern transparent situation. In this case,
* the pattern affects only for the foreground pixels of the source; the back-
* ground must be rendered as a solid, opaque white.
*/
private int
show_char_background(
pcl_state_t * pcs,
const char * pbuff
)
{
gs_show_enum * penum = pcs->penum;
gs_state * pgs = pcs->pgs;
gs_rop3_t rop = (gs_rop3_t)(pcs->logical_op);
const pl_font_t * plfont = pcs->font;
gs_font * pfont = plfont->pfont;
gs_point pt;
int code = 0;
/* save the graphic state and set the background raster operation */
pcl_gsave(pcs);
if (pcs->pattern_transparent)
pcl_set_drawing_color(pcs, pcl_pattern_solid_white, 0, false);
gs_setrasterop(pgs, (gs_rop3_t)rop3_know_S_1((int)rop));
gs_currentpoint(pgs, &pt);
if (plfont->scaling_technology == plfst_bitmap) {
gs_char chr = (((uint)pbuff[0]) << 8) + pbuff[1];
gs_glyph glyph = pfont->procs.encode_char(penum, pfont, &chr);
const byte * cdata = pl_font_lookup_glyph(plfont, glyph)->data;
int nbytes;
uint used;
gs_image_enum * pen = 0;
gs_image1_t mask;
/* empty characters have no background */
if (cdata == 0) {
pcl_grestore(pcs);
return 0;
}
/* allocate the image enumerator */
pen = gs_image_enum_alloc(gs_state_memory(pgs), "bitmap font background");
if (pen == 0) {
pcl_grestore(pcs);
return e_Memory;
}
/* translate the origin to the ul corner of the image */
pt.x += (float)pl_get_int16(cdata + 6);
pt.y -= (float)pl_get_int16(cdata + 8);
gs_translate(pgs, pt.x, pt.y);
/* set up and render the image mask */
gs_image_t_init_mask(&mask, false);
mask.adjust = false;
mask.Width = pl_get_uint16(cdata + 10);
mask.Height = pl_get_uint16(cdata + 12);
nbytes = ((mask.Width + 7) / 8) * mask.Height;
gs_image_init(pen, &mask, false, pgs);
code = gs_image_next(pen, cdata + 16, nbytes, &used);
/* clean up */
gs_image_cleanup(pen);
gs_free_object(gs_state_memory(pgs), pen, "bitmap font background");
} else {
gs_rect bbox;
/* clear the path; start the new one from the current point */
gs_newpath(pgs);
gs_moveto(pgs, pt.x, pt.y);
/* get the character path */
gs_charpath_n_init(penum, pgs, pbuff, 2, true);
if ((code = gs_show_next(penum)) >= 0) {
/* append the characters bounding box and use eofill */
gs_pathbbox(pgs, &bbox);
gs_rectappend(pgs, &bbox, 1);
gs_eofill(pgs);
}
}
pcl_grestore(pcs);
return code;
}
/*
* Draw the foreground of a character. For transparent text this is the only
* part that must be drawn.
*/
private int
show_char_foreground(
const pcl_state_t * pcs,
const char * pbuff
)
{
gs_show_enum * penum = pcs->penum;
int code = gs_show_n_init(penum, pcs->pgs, pbuff, 2);
if (code >= 0)
code = gs_show_next(penum);
return code;
}
/*
* Show a string of characters.
*
* As is the case for other parts of this code, this code is made more complex
* by the PostScript-centric nature of the the graphics library, and by a
* long standing flaw in the PostScript view of fonts. Specifically, the
* initial introduction of Encoding arrays into PostScript fonts, followed by
* composite font mechanism, very much confused the concepts of font and text
* parsing method.
*
* A font is an object which accepts a character identifier and returns a
* "rendering" of that character (which may be a bitmap, may be a path, may
* be an advance vector, or may be some combination of the above); it may also
* in some cases apply this rendering to the graphic state (which may include
* modifying the output). Whether or not a font caches or expects its client
* to handle caching is a separate issue; there are good areguments for either
* approach.
*
* A text parsing method is an object that accepts a character string and
* returns one or more character identifiers. A text parsing method is, in
* principle, completely independent of a font, though for historical reasons
* the two concepts are often linked at the application level.
*
* Because of the PostScript origins of the graphic library, its font interface
* handles both text parsing and rendering. To achieve flexibility, the client
* may provide a "get next character" procedure for the graphic library font
* machinery to work with, but this flexibility is not sufficient for PCL, as
* the latter potentially needs to perform additional operations on each
* character. Hence, PCL will not ask the font machiner to render more than
* one character at a time.
*
* Complicating this picture is the nature of memory management in the graphic
* library. The show class operators in PostScript generally take a string as
* an operand. PostScript strings have the "getinterval" property: one string
* may be part of another string. Hence strings cannot have headers. In a
* relocating memory systems, this implies that strings must be dealt with
* separately from other objects: they must be allocated as strings. In the
* case of PCL, this is not necessarily the case (see, for example, the case
* of transparent mode, below).
*
* The original implementation of this routine ignored this distinction and
* could, in principle, have failed if re-location was enabled. It was also
* rather hard to read, because it parsed the input string (at least) twice:
* once the find the character so that PCL-specific actions could be taken,
* then again via the font machiner.
*
* The current re-implementation attempts to re-institute the distinction
* between a font and a text parsing method. The input string is parsed only
* once, and the resultant code stored in a sing two-byte string. The text
* parsing routine passed to the graphics machinery is simplicity itself:
* it resturns the value of the two-byte string. The font machinery is always
* called for just a single character.
*
* The final operand is true if the text was provided via the literal
* (transparent) text command: ESC & p <nbytes> X. This distinction is
* important for characters that are not mapped by the current symbol set.
*/
private int
pcl_show_chars(
pcl_state_t * pcs,
const gs_point * pscale,
const byte * str,
uint size,
bool literal
)
{
gs_state * pgs = pcs->pgs;
char buff[2];
floatp limit1 = pcs->margins.right;
floatp limit2 = pcs->xfm_state.pd_size.x;
bool opaque = !pcs->source_transparent;
bool wrap = pcs->end_of_line_wrap;
bool is_space = false;
bool use_limit1 = (pcs->cap.x <= limit1);
gs_char chr;
int code = 0;
floatp width;
gs_point cpt;
/* the following are saved in case a substitute font must be used */
floatp hmi = pcl_hmi(pcs);
pcl_font_selection_t * pfp = &(pcs->font_selection[pcs->font_selected]);
pcl_font_selection_t saved_font = *pfp;
cpt.x = pcs->cap.x;
cpt.y = pcs->cap.y;
while (get_next_char(pcs, &str, &size, &chr, &is_space, literal) == 0) {
floatp tmp_x;
/* check if a character was found */
buff[0] = (chr >> 8);
buff[1] = (chr & 0xff);
width = 0;
/* advance and check for exceeding the right boundary */
if (chr != 0xffff) {
if (!pfp->params.proportional_spacing || is_space)
width = hmi;
else {
if ((code = char_width(pcs, buff, &width)) < 0)
break;
width *= pscale->x;
}
} else if (is_space)
width = hmi;
else
width = 0.0;
/*
* Check for transitions of the left margin; this check is
* disabled if the immediately preceding character was a back-space.
* A move beyond the "right" logical page edge is also considered
* a margin transition.
*
* A little-known feature of PCL is the effect of the line-wrap
* command on the interpretation of the right margin. If line
* wrap is in effect, a transition of the left margin will cause
* a <cr><lf> operation BEFORE the current character is printed. If
* line-wrap is not in effect, a transition of the right margin will
* stop printing AFTER the current character is printed.
*
* A special case occurs in the non-wrap situation when the current
* position exactly equals the current margin. In that case, no
* character is printed.
*/
if (!pcs->last_was_BS) {
if (wrap) {
if ( (use_limit1 && (cpt.x + width > limit1)) ||
(cpt.x + width > limit2) ) {
pcl_do_CR(pcs);
pcl_do_LF(pcs);
cpt.x = pcs->cap.x;
cpt.y = pcs->cap.y;
use_limit1 = true;
}
} else {
if (use_limit1 && (cpt.x == limit1))
break;
else if (cpt.x >= limit2) {
cpt.x = limit2;
break;
}
}
}
/*
* If the immediately preceding character was a BS, the code will
* center the current character on top of the preceding one. After
* the character is printed, the current point is returned to the
* prior point.
*/
tmp_x = cpt.x;
if (pcs->last_was_BS)
tmp_x += (pcs->last_width - width) / 2;
gs_moveto(pgs, tmp_x / pscale->x, cpt.y / pscale->y);
if (chr != 0xffff) {
/* if source is opaque, show and opaque background */
if (opaque)
code = show_char_background(pcs, buff);
if (code >= 0)
code = show_char_foreground(pcs, buff);
if (code < 0)
break;
}
/*
* Check again for the first character following a back-space. if
* this is the case, go back to the original position.
*/
if (pcs->last_was_BS) {
cpt.x += pcs->last_width;
pcs->last_was_BS = false;
} else
cpt.x += width;
/* check for going beyond the margin if not wrapping */
if (!wrap) {
if (use_limit1 && (cpt.x > limit1)) {
cpt.x = limit1;
break;
} else if (cpt.x >= limit2) {
cpt.x = limit2;
break;
}
}
/* check if a font substitution occurred */
if (saved_font.font != pcs->font) {
pcl_decache_font(pcs, pcs->font_selected);
*pfp = saved_font;
if ((code = pcl_recompute_font(pcs)) < 0)
break;
set_gs_font(pcs);
}
}
/* check if a font substitution occurred */
if (saved_font.font != pcs->font) {
pcl_decache_font(pcs, pcs->font_selected);
*pfp = saved_font;
code = pcl_recompute_font(pcs);
}
/* record the last width */
pcs->last_width = width;
/* update the current position */
pcs->cap.x = cpt.x;
pcs->cap.y = cpt.y;
return code;
}
/*
* Set up to handle a string of text.
*
* The final operand is true if the text was provided via the literal
* (transparent) text command: ESC & p <nbytes> X. This distinction is
* important for characters that are not mapped by the current symbol set.
*/
int
pcl_text(
const byte * str,
uint size,
pcl_state_t * pcs,
bool literal
)
{
gs_state * pgs = pcs->pgs;
gs_matrix user_ctm;
gs_point scale;
int scale_sign;
int code;
/**** WE COULD CACHE MORE AND DO LESS SETUP HERE ****/
/* if there is no enumerator, give up now */
if (pcs->penum == 0)
return e_Memory;
/* set up the current font and HMI */
if ((pcs->font == 0) && ((code = pcl_recompute_font(pcs)) < 0))
return code;
/* set up the graphic state */
code = pcl_set_drawing_color( pcs,
pcs->pattern_type,
pcs->current_pattern_id,
false
);
if (code >= 0)
code = pcl_set_graphics_state(pcs);
if (code < 0)
return code;
set_gs_font(pcs);
/* set up the font transformation */
if (pcs->font->scaling_technology == plfst_bitmap) {
scale.x = pcl_coord_scale / pcs->font->resolution.x;
scale.y = pcl_coord_scale / pcs->font->resolution.y;
/*
* Bitmap fonts use an inverted coordinate system,
* the same as the usual PCL system.
*/
scale_sign = 1;
} else {
/*
* Outline fonts are 1-point; the font height is given in
* (quarter-)points. However, if the font is fixed-width,
* it must be scaled by pitch, not by height, relative to
* the nominal pitch of the outline.
*/
pcl_font_selection_t * pfp = &pcs->font_selection[pcs->font_selected];
if (pfp->params.proportional_spacing)
scale.x = scale.y = pfp->params.height_4ths
* 0.25 * inch2coord(1.0 / 72.0);
else
scale.x = scale.y = pl_fp_pitch_cp(&pfp->params)
* (100.0 / pl_fp_pitch_cp(&pfp->font->params))
* inch2coord(1.0 / 7200.0);
/*
* Scalable fonts use an upright coordinate system,
* the opposite from the usual PCL system.
*/
scale_sign = -1;
}
/*
* If floating underline is on, since we're about to print a real
* character, track the best-underline position.
* XXX Until we have the font's design value for underline position,
* use 0.2 em. This is enough to almost clear descenders in typical
* fonts; it's also large enough for us to check that the mechanism
* works.
*/
if (pcs->underline_enabled && pcs->underline_floating) {
float yu = scale.y / 5.0;
if (yu > pcs->underline_position)
pcs->underline_position = yu;
}
/*
* XXX I'm using the more general, slower approach rather than
* just multiplying/dividing by scale factors, in order to keep it
* correct through orientation changes. Various parts of this should
* be cleaned up when performance time rolls around.
*/
gs_currentmatrix(pgs, &user_ctm);
/* possibly invert text because HP coordinate system is inverted */
scale.y *= scale_sign;
gs_scale(pgs, scale.x, scale.y);
/* Print remaining characters, restore the ctm */
code = pcl_show_chars(pcs, &scale, str, size, literal);
gs_setmatrix(pgs, &user_ctm);
if (code > 0) /* shouldn't happen */
code = gs_note_error(gs_error_invalidfont);
return code;
}
/*
* Individual non-command/control characters
*/
int
pcl_plain_char(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
return pcl_text((const byte *)&(pargs->command), 1, pcs, false);
}
/*
* draw underline up to current point, adjust status
*/
void
pcl_do_underline(
pcl_state_t * pcs
)
{
if (pcs->underline_start.x != pcs->cap.x) {
gs_state * pgs = pcs->pgs;
float y = pcs->underline_start.y + pcs->underline_position;
int code;
code = pcl_set_drawing_color( pcs,
pcs->pattern_type,
pcs->current_pattern_id,
false
);
if (code >= 0)
code = pcl_set_graphics_state(pcs);
if (code < 0)
return;
/*
* TRM says (8-34) that underline is 3 dots. In a victory for
* common sense, it's not. Rather, it's 0.01" (which *is* 3 dots
* at 300 dpi only)
*/
gs_setlinewidth(pgs, dots(3));
gs_moveto(pgs, pcs->underline_start.x, y);
gs_lineto(pgs, pcs->cap.x, y);
gs_stroke(pgs);
}
/*
* Fixed underline is 5 "dots" (actually 5/300") down. Floating
* will be determined on the fly.
*/
pcs->underline_start = pcs->cap;
pcs->underline_position = pcs->underline_floating ? 0.0 : dots(5);
}
/* ------ Commands ------ */
/*
* ESC & p <count> X
*
* Unparsed text command
*/
private int
pcl_transparent_mode(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
return pcl_text(arg_data(pargs), uint_arg(pargs), pcs, true);
}
/*
* ESC & d <0|3> D
*
* Enable floating or fixed-depth underlining.
*
* NB: If underlining is already enabled, this command is ignored. Underlining
* must be specifically disabled to switch from fixed to floating.
*/
private int
pcl_enable_underline(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
int type = int_arg(pargs);
/* ignore command if underlining is already enabled */
if (pcs->underline_enabled)
return 0;
if (type == 0) {
pcs->underline_floating = false;
pcs->underline_position = dots(5);
} else if (type == 3) {
pcs->underline_floating = true;
pcs->underline_position = 0.0;
} else
return 0;
pcs->underline_enabled = true;
pcs->underline_start = pcs->cap;
return 0;
}
/*
* ESC & d @
*
* Disable underlining
*/
private int
pcl_disable_underline(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
pcl_break_underline(pcs);
pcs->underline_enabled = false;
return 0;
}
/* (From PCL5 Comparison Guide, p. 1-56) */
/*
* ESC & t <method> P
*
* Select the text parsing method.
*/
private int
pcl_text_parsing_method(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
switch (int_arg(pargs)) {
case 0: case 1:
pcs->text_parsing_method = &pcl_tpm_0;
break;
case 21:
pcs->text_parsing_method = &pcl_tpm_21;
break;
case 31:
pcs->text_parsing_method = &pcl_tpm_31;
break;
case 38:
pcs->text_parsing_method = &pcl_tpm_38;
break;
default:
return e_Range;
}
return 0;
}
/* (From PCL5 Comparison Guide, p. 1-57) */
/*
* ESC & c <direction> T
*
* Set the text path direction - not yet implemented.
*/
private int
pcl_text_path_direction(
pcl_args_t * pargs,
pcl_state_t * pcs
)
{
int direction = int_arg(pargs);
switch (direction) {
case 0:
case -1:
break;
default:
return e_Range;
}
pcs->text_path = direction;
return e_Unimplemented;
}
/* ------ Initialization ------ */
private int
pctext_do_init(
gs_memory_t * mem
)
{
/* Register commands */
DEFINE_CONTROL(0, "(plain char)", pcl_plain_char);
DEFINE_CLASS('&')
{
'p', 'X',
PCL_COMMAND("Transparent Mode", pcl_transparent_mode, pca_bytes)
},
{
'd', 'D',
PCL_COMMAND( "Enable Underline",
pcl_enable_underline,
pca_neg_ignore | pca_big_ignore
)
},
{
'd', '@',
PCL_COMMAND( "Disable Underline",
pcl_disable_underline,
pca_neg_ignore | pca_big_ignore
)
},
END_CLASS
DEFINE_CLASS('&')
{
't', 'P',
PCL_COMMAND( "Text Parsing Method",
pcl_text_parsing_method,
pca_neg_error | pca_big_error
)
},
{
'c', 'T',
PCL_COMMAND( "Text Path Direction",
pcl_text_path_direction,
pca_neg_ok | pca_big_error
)
},
END_CLASS
DEFINE_CONTROL(1, "(plain char)", pcl_plain_char); /* default "command" */
return 0;
}
private void
pctext_do_reset(
pcl_state_t * pcs,
pcl_reset_type_t type
)
{
static const uint mask = ( pcl_reset_initial
| pcl_reset_printer
| pcl_reset_overlay );
if ((type & mask) != 0) {
pcs->underline_enabled = false;
pcs->last_was_BS = false;
pcs->last_width = inch2coord(1.0 / 10.0);
pcs->text_parsing_method = &pcl_tpm_0;
pcs->text_path = 0;
if ((type & (pcl_reset_initial)) != 0)
pcs->penum = gs_show_enum_alloc( pcs->memory,
pcs->pgs,
"pcl text enumerator"
);
}
}
const pcl_init_t pctext_init = { pctext_do_init, pctext_do_reset, 0 };
|