summaryrefslogtreecommitdiff
path: root/pcl/pcl/pglabel.c
blob: f46f1bbddb67d64cff071fc88d9641d2fc75f168 (plain)
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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* pglabel.c - HP-GL/2 label commands */

#include "math_.h"
#include "memory_.h"
#include "ctype_.h"
#include "stdio_.h"             /* for gdebug.h */
#include "gdebug.h"
#include "pcparse.h"
#include "plvalue.h"
#include "pgmand.h"
#include "pginit.h"
#include "pgfont.h"
#include "pgdraw.h"
#include "pggeom.h"
#include "pgmisc.h"
#include "pcpage.h"
#include "pcfsel.h"
#include "pcsymbol.h"
#include "pcpalet.h"
#include "pcdraw.h"
#include "gscoord.h"
#include "gsline.h"
#include "gspath.h"
#include "gsutil.h"
#include "gxchar.h"             /* for show enumerator */
#include "gxfont.h"
#include "gxstate.h"            /* for gs_gstate_client_data */

#define STICK_FONT_TYPEFACE 48

/* NB this next constant is not quite right.  The STICK_FONT_TYPEFACE
   definition is used in the code to cover both stick and arc fonts.
   Typically the prescription was to choose the stick font typeface
   (48) and enable proportional spacing to get the arc font.  More
   recently we discovered a new typeface family number that can be
   used for the proportionally spaced arc fonts (50).  This has been
   reflected in the hpgl/2 selection code but nowhere else. */
#define ARC_FONT_TYPEFACE 50

/* currently selected font */
static pl_font_t *
hpgl_currentfont(const hpgl_state_t * pgls)
{
    return pgls->g.font_selection[pgls->g.font_selected].font;
}

static bool
hpgl_is_currentfont_stick(const hpgl_state_t * pgls)
{
    pl_font_t *plfont = hpgl_currentfont(pgls);

    if (!plfont)
        return false;
    return (((plfont->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE)
            && (plfont->params.proportional_spacing == false));
}

bool
hpgl_is_currentfont_stick_or_arc(const hpgl_state_t * pgls)
{
    pl_font_t *plfont = hpgl_currentfont(pgls);

    if (!plfont)
        return false;
    return (((plfont->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE)
            || ((plfont->params.typeface_family & 0xfff) ==
                ARC_FONT_TYPEFACE));
}

/* convert points 2 plu - agfa uses 72.307 points per inch */
static double
hpgl_points_2_plu(const hpgl_state_t * pgls, double points)
{
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    double ppi = 72.0;

    if (pfs->font->scaling_technology == plfst_Intellifont)
        ppi = 72.307;
    return points * (1016.0 / ppi);
}

/* ------ Next-character procedure ------ */

/*
 * Map a character through the symbol set, if needed.
 */
static gs_char
hpgl_map_symbol(uint chr, const hpgl_state_t * pgls)
{
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    const pl_symbol_map_t *psm = pfs->map;
    bool db = pcl_downloaded_and_bound(pfs->font);

    return pl_map_symbol((db ? NULL : psm), chr,
                         pfs->font->storage == pcds_internal,
                         pfs->font->font_type == plft_MSL,
                         pgls->memory);
}

/* ------ Font selection ------- */

/* Select primary (0) or alternate (1) font. */
static void
hpgl_select_font_pri_alt(hpgl_state_t * pgls, int index)
{
    if (pgls->g.font_selected != index) {
        hpgl_free_stick_fonts(pgls);
        pgls->g.font_selected = index;
        pgls->g.font = 0;
    }
    return;
}

/* forward decl */
static int hpgl_recompute_font(hpgl_state_t * pgls);

/* Ensure a font is available and selected. */
static int
hpgl_ensure_font(hpgl_state_t * pgls)
{
    pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];

    if (pgls->g.font == 0 || pgls->g.font->pfont == 0 || pfs->font == 0)
        hpgl_call(hpgl_recompute_font(pgls));
    return 0;
}

/*
 * The character complement for the stick font is puzzling: it doesn't seem
 * to correspond directly to any of the MSL *or* Unicode symbol set bits
 * described in the Comparison Guide.  We set the bits for MSL Basic Latin
 * (63) and for Unicode ASCII (31), and Latin 1 (30).
 */
static const byte stick_character_complement[8] = {
    0x7f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xfe
};

/* Select the stick font, creating it if necessary. */
/* We break this out only for readability: it's only called in one place. */
static int
hpgl_select_stick_font(hpgl_state_t * pgls)
{
    pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    pl_font_t *font = &pgls->g.stick_font[pgls->g.font_selected]
        [pfs->params.proportional_spacing];
    gs_font_base *pfont;
    int code;

    /* Create a gs_font if none has been created yet. */
    hpgl_free_stick_fonts(pgls);
    pfont = gs_alloc_struct(pgls->memory, gs_font_base, &st_gs_font_base,
                            "stick/arc font");

    if (pfont == 0)
        return_error(e_Memory);
    code = pl_fill_in_font((gs_font *) pfont, font, pgls->font_dir,
                           pgls->memory, "stick_arc_font");
    if (code < 0)
        return code;
    if (pfs->params.proportional_spacing)
        hpgl_fill_in_arc_font(pfont, gs_next_ids(pgls->memory, 1));
    else
        hpgl_fill_in_stick_font(pfont, gs_next_ids(pgls->memory, 1));
    font->pfont = (gs_font *) pfont;
    font->scaling_technology = plfst_TrueType;    /****** WRONG ******/
    font->font_type = plft_Unicode;
    font->widths_cache = NULL;
    font->widths_cache_nitems = 0;
    font->storage = 0;

    memcpy(font->character_complement, stick_character_complement, 8);
    /*
     * The stick/arc font is protean: set its proportional spacing,
     * style, and stroke weight parameters to the requested ones.
     * We could fill in some of the other characteristics earlier,
     * but it's simpler to do it here.
     */
    font->params = pfs->params;
    font->params.typeface_family = STICK_FONT_TYPEFACE;
    /*
     * The stick font is defined in a cell that's only 2/3
     * the size of the actual character.
     */
    pl_fp_set_pitch_cp(&font->params, 1000.0 * 2 / 3);
    pfs->font = font;
    {
        byte id[2];

        id[0] = pfs->params.symbol_set >> 8;
        id[1] = pfs->params.symbol_set & 0xff;
        pfs->map = pcl_find_symbol_map(pgls,
                                       id, plgv_Unicode,
                                       font->font_type == plft_16bit);
    }
    return 0;
}

static int
hpgl_select_531_font(hpgl_state_t * pgls)
{
    pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    pl_font_t *font = &pgls->g.dl_531_font[pgls->g.font_selected];
    gs_font_base *pfont;
    int code;

    hpgl_free_531_fonts(pgls);
    pfont = gs_alloc_struct(pgls->memory, gs_font_base, &st_gs_font_base,
                            "stick/arc font");

    if (pfont == 0)
        return_error(e_Memory);
    code = pl_fill_in_font((gs_font *) pfont, font, pgls->font_dir,
                           pgls->memory, "531_font");
    if (code < 0)
        return code;

    hpgl_fill_in_531_font(pfont, gs_next_ids(pgls->memory, 1));
    font->pfont = (gs_font *) pfont;
    font->scaling_technology = plfst_TrueType;/****** WRONG ******/
    font->font_type = plft_Unicode;
    font->widths_cache = NULL;
    font->widths_cache_nitems = 0;
    /* we pass the dictionary created during the DL command as the font header data. */
    font->header = (void *)&pgls->g.dl_531_fontdict;
    /* the parameters aren't used so we just fill them in with stick
       font characteristics */
    memcpy(font->character_complement, stick_character_complement, 8);
    font->params = pfs->params;
    font->params.typeface_family = STICK_FONT_TYPEFACE;
    pl_fp_set_pitch_cp(&font->params, 1000.0 * 2 / 3);
    pfs->font = font;
    /* the font is mapped directly */
    pfs->map = 0;
    return 0;
}

/* Check whether the stick font supports a given symbol set. */
static bool
hpgl_stick_font_supports(const pcl_state_t * pcs, uint symbol_set)
{
    pl_glyph_vocabulary_t gv =
        pl_complement_to_vocab(stick_character_complement);
    byte id[2];
    pl_symbol_map_t *map;

    id[0] = symbol_set >> 8;
    id[1] = symbol_set & 0xff;
    if ((map = pcl_find_symbol_map(pcs, id, gv, false)) == 0)
        return false;
    return pcl_check_symbol_support(map->character_requirements,
                                    stick_character_complement);
}

/* Recompute the current font if necessary. */
static int
hpgl_recompute_font(hpgl_state_t * pgls)
{
    pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];

    if ((pfs->params.symbol_set == 531) && (pgls->personality == rtl)) {
        hpgl_call(hpgl_select_531_font(pgls));
    } else
        if ((((pfs->params.typeface_family & 0xfff) == STICK_FONT_TYPEFACE
              || (pfs->params.typeface_family & 0xfff) == ARC_FONT_TYPEFACE)
             && pfs->params.style == 0  /* upright */
             && hpgl_stick_font_supports(pgls, pfs->params.symbol_set))
            /* rtl only has stick fonts */
            || (pgls->personality == rtl)
        )
        hpgl_call(hpgl_select_stick_font(pgls));
    else {
        int code = pcl_reselect_font(pfs, pgls, false);

        if (code < 0)
            return code;
    }
    pgls->g.font = pfs->font;
    pgls->g.map = pfs->map;
    return pl_load_resident_font_data_from_file(pgls->memory, pfs->font);
}

/* ------ Position management ------ */

/* accessor for character extra space takes line feed direction into account */
static inline hpgl_real_t
hpgl_get_character_extra_space_x(const hpgl_state_t * pgls)
{
    return (pgls->g.character.line_feed_direction < 0) ?
        pgls->g.character.extra_space.y : pgls->g.character.extra_space.x;
}

static inline hpgl_real_t
hpgl_get_character_extra_space_y(const hpgl_state_t * pgls)
{
    return (pgls->g.character.line_feed_direction < 0) ?
        pgls->g.character.extra_space.x : pgls->g.character.extra_space.y;
}

/* Get a character width in the current font, plus extra space if any. */
/* If the character isn't defined, return 1, otherwise return 0. */
static int
hpgl_get_char_width(const hpgl_state_t * pgls, gs_char ch,
                    hpgl_real_t * width)
{
    gs_glyph glyph = hpgl_map_symbol(ch, pgls);
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    int code = 0;
    gs_point gs_width;

    if (pgls->g.character.size_mode == hpgl_size_not_set) {
        if (pfs->params.proportional_spacing) {
            code =
                pl_font_char_width(pfs->font, (void *)(pgls->pgs), glyph,
                                   &gs_width);
            /* hack until this code gets written properly, the
               following should amount to a percentage of the
               em-square the space character would occupy... */
            if (code == 1) {
                gs_width.y = 0;
                gs_width.x = pl_fp_pitch_cp(&pfs->font->params) / 1000.0;
            }

            if (!pl_font_is_scalable(pfs->font)) {
                if (code == 0)
                    *width =
                        gs_width.x * inches_2_plu(1.0 /
                                                  pfs->font->resolution.x);
                else
                    *width = coord_2_plu(pl_fp_pitch_cp(&pfs->font->params));
                goto add;
            } else if (code >= 0) {
                *width =
                    gs_width.x * hpgl_points_2_plu(pgls,
                                                   pfs->params.height_4ths /
                                                   4.0);
                goto add;
            }
            code = 1;
        }
        *width =
            hpgl_points_2_plu(pgls, pl_fp_pitch_cp(&pfs->params) / 100.0);
    } else {
        *width = pgls->g.character.size.x;
        if (pgls->g.character.size_mode == hpgl_size_relative)
            *width *= pgls->g.P2.x - pgls->g.P1.x;

    }
  add:

    if (hpgl_get_character_extra_space_x(pgls) != 0) {
        /* Add extra space. */
        if (pfs->params.proportional_spacing && ch != ' ') {
            /* Get the width of the space character. */
            int scode =
                pl_font_char_width(pfs->font, (void *)(pgls->pgs),
                                   hpgl_map_symbol(' ', pgls), &gs_width);
            hpgl_real_t extra;

            if (scode >= 0)
                extra =
                    gs_width.x * hpgl_points_2_plu(pgls,
                                                   pfs->params.height_4ths /
                                                   4.0);
            else
                extra =
                    hpgl_points_2_plu(pgls,
                                      (pl_fp_pitch_cp(&pfs->params)) / 10.0);
            *width += extra * hpgl_get_character_extra_space_x(pgls);
        } else {
            /* All characters have the same width, */
            /* or we're already getting the width of a space. */
            *width *= 1.0 + hpgl_get_character_extra_space_x(pgls);
        }
    }
    return code;
}
/* Get the cell height or character height in the current font, */
/* plus extra space if any. */
static int
hpgl_get_current_cell_height(const hpgl_state_t * pgls, hpgl_real_t * height)
{
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];

    if (pfs->font->scaling_technology != plfst_bitmap) {
        gs_point scale = hpgl_current_char_scale(pgls);

        *height = fabs(scale.y);
    } else {
        /* NB temporary not correct */
        *height = hpgl_points_2_plu(pgls, pfs->params.height_4ths / 4.0);
    }

    /* the HP manual says linefeed distance or cell height is 1.33
       times point size for stick fonts and "about" 1.2 times the
       point size for "most" fonts.  Empirical results suggest the
       stick font value is 1.28.  NB This value appears to be
       slightly different for the proportional arc font. */
    if (pgls->g.character.text_path == hpgl_text_right
        || pgls->g.character.text_path == hpgl_text_left) {
        if (hpgl_is_currentfont_stick(pgls))
            *height *= 1.28;
        else
            *height *= 1.2;
    } else {
        /* INC */
        if (hpgl_is_currentfont_stick(pgls))
            *height *= .96;
        else
            *height *= .898;
    }

    *height *= 1.0 + hpgl_get_character_extra_space_y(pgls);
    return 0;
}

/* distance tranformation for character slant */
static int
hpgl_slant_transform_distance(hpgl_state_t * pgls, gs_point * dxy,
                              gs_point * s_dxy)
{
    if (pgls->g.character.slant && !pgls->g.bitmap_fonts_allowed) {
        gs_matrix smat;
        gs_point tmp_dxy = *dxy;

        gs_make_identity(&smat);
        smat.yx = pgls->g.character.slant;
        hpgl_call(gs_distance_transform(tmp_dxy.x, tmp_dxy.y, &smat, s_dxy));
    }
    return 0;
}

/* distance tranformation for character direction */
static int
hpgl_rotation_transform_distance(hpgl_state_t * pgls, gs_point * dxy,
                                 gs_point * r_dxy)
{
    double run = pgls->g.character.direction.x;
    double rise = pgls->g.character.direction.y;
    int angle = (int)((hpgl_compute_angle(run, rise) * radians_to_degrees) + 0.5);
    gs_point tmp_dxy = *dxy;
    gs_matrix rmat;

    gs_make_rotation((double) angle, &rmat);
    hpgl_call(gs_distance_transform(tmp_dxy.x, tmp_dxy.y, &rmat, r_dxy));
    return 0;
}

/* Reposition the cursor.  This does all the work for CP, and is also */
/* used to handle some control characters within LB. */
/* If pwidth != 0, it points to a precomputed horizontal space width. */
static int
hpgl_move_cursor_by_characters(hpgl_state_t * pgls, hpgl_real_t spaces,
                               hpgl_real_t lines, const hpgl_real_t * pwidth)
{
    double nx, ny;
    double dx = 0, dy = 0;
    hpgl_call(hpgl_ensure_font(pgls));

    lines *= pgls->g.character.line_feed_direction;
    /* For vertical text paths, we have to swap spaces and lines. */
    switch (pgls->g.character.text_path) {
        case hpgl_text_right:
            nx = spaces, ny = lines;
            break;
        case hpgl_text_down:
            nx = lines, ny = -spaces;
            break;
        case hpgl_text_left:
            nx = -spaces, ny = -lines;
            break;
        case hpgl_text_up:
            nx = -lines, ny = spaces;
            break;
        default:
            return -1;
    }
    /* calculate the next label position in relative coordinates. */
    if (nx != 0) {
        hpgl_real_t width;

        if (pwidth != 0)
            width = *pwidth;
        else
            hpgl_get_char_width(pgls, ' ', &width);
        dx = width * nx;
    }
    if (ny != 0) {
        hpgl_real_t height;

        hpgl_call(hpgl_get_current_cell_height(pgls, &height));
        dy = ny * height;
    }

    /*
     * We just computed the deltas in user units if characters are
     * using relative sizing, and in PLU otherwise.
     * If scaling is on but characters aren't using relative
     * sizing, we have to convert the deltas to user units.
     */
    if (pgls->g.scaling_type != hpgl_scaling_none) {
        gs_matrix mat;
        gs_point user_dxy;

        hpgl_call(hpgl_compute_user_units_to_plu_ctm(pgls, &mat));
        hpgl_call(gs_distance_transform_inverse(dx, dy, &mat, &user_dxy));
        dx = user_dxy.x;
        dy = user_dxy.y;
    }

    {
        gs_point dxy;

        dxy.x = dx;
        dxy.y = dy;
        hpgl_rotation_transform_distance(pgls, &dxy, &dxy);
        dx = dxy.x;
        dy = dxy.y;
    }
    /* a relative move to the new position */
    hpgl_call(hpgl_add_point_to_path(pgls, dx, dy,
                                     hpgl_plot_move_relative, true));

    if (lines != 0) {
        /* update the position of the carriage return point */
        pgls->g.carriage_return_pos.x += dx;
        pgls->g.carriage_return_pos.y += dy;
    }
    /* free any selected stick fonts */
    hpgl_free_stick_fonts(pgls);
    return 0;
}

/* Execute a CR for CP or LB. */
static int
hpgl_do_CR(hpgl_state_t * pgls)
{
    return hpgl_add_point_to_path(pgls, pgls->g.carriage_return_pos.x,
                                  pgls->g.carriage_return_pos.y,
                                  hpgl_plot_move_absolute, true);
}

/* CP [spaces,lines]; */
/* CP [;] */
int
hpgl_CP(hpgl_args_t * pargs, hpgl_state_t * pgls)
{
    hpgl_real_t spaces, lines;

    if (hpgl_arg_c_real(pgls->memory, pargs, &spaces)) {
        if (!hpgl_arg_c_real(pgls->memory, pargs, &lines))
            return e_Range;
    } else {
        /* if there are no arguments a carriage return and line feed
           is executed */
        hpgl_call(hpgl_do_CR(pgls));
        spaces = 0, lines = -1;
    }
    return hpgl_move_cursor_by_characters(pgls, spaces, lines,
                                          (const hpgl_real_t *)0);
}

/* ------ Label buffer management ------ */

/* initialize the character buffer, setting state pointers for the
   beginning of the character buffer and the current character within
   the buffer to position 0. */
static int
hpgl_init_label_buffer(hpgl_state_t * pgls)
{
    pgls->g.label.char_count = 0;
    pgls->g.label.buffer_size = hpgl_char_count;
    return ((pgls->g.label.buffer =
             gs_alloc_bytes(pgls->memory, hpgl_char_count,
                            "hpgl_init_label_buffer")) == 0 ? e_Memory : 0);
}

/* release the character buffer */
void
hpgl_destroy_label_buffer(hpgl_state_t * pgls)
{
    gs_free_object(pgls->memory, pgls->g.label.buffer,
                   "hpgl_destroy_label_buffer");
    pgls->g.label.char_count = 0;
    pgls->g.label.buffer_size = 0;
    pgls->g.label.buffer = 0;
}

/* add a single character to the line buffer */
static int
hpgl_buffer_char(hpgl_state_t * pgls, byte ch)
{
    /* check if there is room for the new character and resize if
       necessary */
    if (pgls->g.label.buffer_size == pgls->g.label.char_count) {        /* Resize the label buffer, currently by doubling its size. */
        uint new_size = pgls->g.label.buffer_size << 1;
        byte *new_mem =
            gs_resize_object(pgls->memory, pgls->g.label.buffer, new_size,
                             "hpgl_resize_label_buffer");

        if (new_mem == 0)
            return_error(e_Memory);
        pgls->g.label.buffer = new_mem;
        pgls->g.label.buffer_size = new_size;
    }
    /* store the character */
    pgls->g.label.buffer[pgls->g.label.char_count++] = ch;
    return 0;
}

/*
 * Test if the gl/2 drawing primitives should draw the character or
 * "show" can be used directly.
 */
static bool
hpgl_use_show(hpgl_state_t * pgls, pl_font_t * pfont)
{

    /* NB opaque source must go through the gl/2 line drawing code and
       treated as a special effect because the normal text processing
       does not support rendering the background of characters.
       Bitmap characters must be rendered with the library's "show"
       machinery, so opaque bitmaps are not properly supported in
       gl/2.  Fortunately we don't expect to see them in the real
       world */
    if (pgls->g.source_transparent == 0
        && pfont->scaling_technology != plfst_bitmap)
        return false;

    /* Show cannot be used if CF is not default since the character
       may require additional processing by the line drawing code. */
    if ((pgls->g.character.fill_mode == 0 && pgls->g.character.edge_pen == 0)
        || (pfont->scaling_technology == plfst_bitmap))
        return true;
    else
        return false;
}

/* get the scaling factors for a gl/2 character */
gs_point
hpgl_current_char_scale(const hpgl_state_t * pgls)
{
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    pl_font_t *font = pfs->font;
    bool bitmaps_allowed = pgls->g.bitmap_fonts_allowed;
    gs_point scale;

    if (pgls->g.character.size_mode == hpgl_size_not_set
        || font->scaling_technology == plfst_bitmap) {
        if (font->scaling_technology == plfst_bitmap) {
            scale.x = inches_2_plu(1.0 / font->resolution.x);
            scale.y = -inches_2_plu(1.0 / font->resolution.y);
            /* Scale fixed-width fonts by pitch, variable-width by height. */
        } else if (pfs->params.proportional_spacing) {
            if (pl_font_is_scalable(font)) {
                scale.x =
                    hpgl_points_2_plu(pgls, pfs->params.height_4ths / 4.0);
                scale.y = scale.x;
            } else {
                double ratio = (double)pfs->params.height_4ths
                    / font->params.height_4ths;

                scale.x = ratio * inches_2_plu(1.0 / font->resolution.x);

                /*
                 * Bitmap fonts use the PCL coordinate system,
                 * so we must invert the Y coordinate.
                 */
                scale.y = -(ratio * inches_2_plu(1.0 / font->resolution.y));
            }
        } else {
#define PERCENT_OF_EM ((pl_fp_pitch_cp(&pfs->font->params) / 1000.0))
            scale.x = scale.y =
                (1.0 / PERCENT_OF_EM) * hpgl_points_2_plu(pgls,
                                                          pl_fp_pitch_cp
                                                          (&pfs->params) /
                                                          100.0);
        }
    } else {
        /*
         * Note that the CTM takes P1/P2 into account unless
         * an absolute character size is in effect.
         */
        /* HP is really scaling the cap height not the point size.
           We assume point size is 1.5 times the point size */
        scale.x = pgls->g.character.size.x * 1.5 * 1.25;
        scale.y = pgls->g.character.size.y * 1.5;
        if (pgls->g.character.size_mode == hpgl_size_relative)
            scale.x *= pgls->g.P2.x - pgls->g.P1.x,
                scale.y *= pgls->g.P2.y - pgls->g.P1.y;
        if (bitmaps_allowed)    /* no mirroring */
            scale.x = fabs(scale.x), scale.y = fabs(scale.y);
    }
    return scale;

}
/*
 * build the path and render it
 */
static int
hpgl_print_char(hpgl_state_t * pgls, uint ch)
{
    int text_path = pgls->g.character.text_path;
    const pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    pl_font_t *font = pfs->font;
    gs_gstate *pgs = pgls->pgs;
    gs_matrix save_ctm;
    gs_font *pfont = pgls->g.font->pfont;
    gs_point scale = hpgl_current_char_scale(pgls);

    /*
     * All character data is relative, but we have to start at
     * the right place.
     */
    hpgl_call(hpgl_add_point_to_path(pgls,
                                     pgls->g.pos.x,
                                     pgls->g.pos.y,
                                     hpgl_plot_move_absolute, true));
    hpgl_call(gs_currentmatrix(pgs, &save_ctm));

    /*
     * Use plotter unit ctm.
     */
    hpgl_call(hpgl_set_plu_ctm(pgls));

    /* ?? WRONG and UGLY */
    {
        float metrics[4];

        if ((pl_font_char_metrics(font, (void *)(pgls->pgs),
                                  hpgl_map_symbol(ch, pgls), metrics)) == 1)
            ch = ' ';
    }

    /*
     * If we're using a stroked font, patch the pen width to reflect
     * the stroke weight.  Note that when the font's build_char
     * procedure calls stroke, the CTM is still scaled.
     ****** WHAT IF scale.x != scale.y? ****** this should be unnecessary.
     */

    if (pfont->PaintType != 0) {
        const float *widths = pcl_palette_get_pen_widths(pgls->ppalet);
        float save_width = widths[hpgl_get_selected_pen(pgls)];
        int weight = pfs->params.stroke_weight;
        double nwidth;

        if (weight == 9999)
            nwidth = save_width;
        else {
            nwidth = 0.06 + weight * (weight < 0 ? 0.005 : 0.010);
            nwidth *= min(scale.x, scale.y) * (hpgl_width_scale(pgls));
        }
        /* in points */
        gs_setlinewidth(pgs, nwidth * (72.0 / 1016.0));
    }

    /*
     * We know that the drawing machinery only sets the CTM
     * once, at the beginning of the path.  We now impose the scale
     * (and other transformations) on the CTM so that we can add
     * the symbol outline based on a 1x1-unit cell.
     */
    {
        gs_font *pfont = pgls->g.font->pfont;
        bool bitmaps_allowed = pgls->g.bitmap_fonts_allowed;
        bool use_show = hpgl_use_show(pgls, font);
        gs_matrix pre_rmat, advance_mat;
        int angle = 0;
        gs_text_enum_t *penum = NULL;
        int code;
        gs_point start_pt, end_pt;
        hpgl_real_t space_width;
        int space_code;
        hpgl_real_t width;

        /* Handle size. */

        gs_scale(pgs, scale.x, scale.y);
        /* Handle rotation. */
        {
            double run = pgls->g.character.direction.x,
                rise = pgls->g.character.direction.y;

            /* gl/2 bitmap fonts are scaled in a left handed coordinate
               system - so the "rise" direction is opposite.  */
            if (font->scaling_technology == plfst_bitmap)
                rise *= -1;

            if (pgls->g.character.direction_relative) {
                run *= pgls->g.P2.x - pgls->g.P1.x;
                rise *= pgls->g.P2.y - pgls->g.P1.y;
            }

            angle =
                (int)((hpgl_compute_angle(run, rise) * radians_to_degrees) + 0.5);
            gs_currentmatrix(pgs, &pre_rmat);
            gs_rotate(pgs, angle);
            /* we do something special if the angle is greater than
               zero for bitmap fonts (see below) */
            angle = -1;

            if (bitmaps_allowed && (run != 0) && (rise != 0)) { /* not a multple of 90 degrees */
                /*
                 * If bitmap fonts are allowed, rotate to the nearest
                 * multiple of 90 degrees.  We have to do something
                 * special at the end to create the correct escapement.
                 */
                if (run >= 0) {
                    if (rise >= 0)
                        angle = (run >= rise ? 0 : 90);
                    else
                        angle = (-rise >= run ? 270 : 0);
                } else {
                    if (rise >= 0)
                        angle = (rise >= -run ? 90 : 180);
                    else
                        angle = (-run >= -rise ? 180 : 270);
                }
            }

        }

        /* Handle slant. */
        if (pgls->g.character.slant && !bitmaps_allowed) {
            gs_matrix smat;

            gs_make_identity(&smat);
            smat.yx = pgls->g.character.slant;
            gs_concat(pgs, &smat);
        }

        gs_setfont(pgs, pfont);
        pfont->FontMatrix = pfont->orig_FontMatrix;

        /*
         * Adjust the initial position of the character according to
         * the text path.  And the left side bearing.  It appears
         * HPGL/2 renders all characters without a left side bearing.
         */
        hpgl_call(gs_currentpoint(pgs, &start_pt));
        if (text_path == hpgl_text_left) {
            hpgl_get_char_width(pgls, ch, &width);
            start_pt.x -= width / scale.x;
            hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x, start_pt.y,
                                             hpgl_plot_move_absolute, false));

        }

        /*
         * Reset the rotation if we're using a bitmap font.
         */
        gs_currentmatrix(pgs, &advance_mat);
        if (angle >= 0) {
            gs_setmatrix(pgs, &pre_rmat);
            gs_rotate(pgs, (double) angle);
        }

        /* If SP is a control code, get the width of the space character. */
        if (ch == ' ') {
            space_code = hpgl_get_char_width(pgls, ' ', &space_width);

            if (0 == space_code &&
                pl_font_is_scalable(font) && pfs->params.proportional_spacing)
                space_code = 1; /* NB hpgl_get_width lies. */

            if (space_code == 1) {
                /* Space is a control code. */
                if (pl_font_is_scalable(font)) {
                    if (pfs->params.proportional_spacing)
                        space_width =
                            (coord_2_plu
                             ((pl_fp_pitch_cp(&pfs->font->params) / 10)
                              * pfs->params.height_4ths / 4)) / scale.x;
                    else
                        space_width = 1.0;
                    /* error! NB scalable fixed pitch space_code == 0 */
                } else
                    space_width =
                        coord_2_plu(pl_fp_pitch_cp(&pfs->font->params)) /
                        scale.x;
                space_width *= (1.0 + hpgl_get_character_extra_space_x(pgls));
            }
        }

        /* Check for SP control code. */
        if (ch == ' ' && space_code != 0) {
            /* Space is a control code.  Just advance the position. */
            gs_setmatrix(pgs, &advance_mat);
            hpgl_call(hpgl_add_point_to_path(pgls, space_width, 0.0,
                                             hpgl_plot_move_relative, false));
            hpgl_call(gs_currentpoint(pgs, &end_pt));
            /* at this point we will assume the page is marked */
            pgls->page_marked = true;
        } else {
            gs_text_params_t text;
            gs_char mychar_buff[1];

            mychar_buff[0] = hpgl_map_symbol(ch, pgls);
            if (use_show) {
                /* not a path that needs to be drawn by the hpgl/2
                   vector drawing code. */
                hpgl_call(hpgl_set_drawing_color(pgls, hpgl_rm_character));
                text.operation =
                    TEXT_FROM_CHARS | TEXT_DO_DRAW | TEXT_RETURN_WIDTH;
            } else
                text.operation =
                    TEXT_FROM_CHARS | TEXT_DO_TRUE_CHARPATH |
                    TEXT_RETURN_WIDTH;
            text.data.chars = mychar_buff;
            /* always on char (gs_chars (ints)) at a time */
            text.size = 1;
            code = gs_text_begin(pgs, &text, pgls->memory, &penum);
            if (code >= 0)
                code = gs_text_process(penum);

            if (code >= 0) {
                /* we just check the current position for
                   "insidedness" - this seems to address the dirty
                   page issue in practice. */
                code = pcl_mark_page_for_current_pos(pgls);
            }
            gs_text_release(pgs, penum, "hpgl_print_char");
            if (code < 0) {
                hpgl_free_stick_fonts(pgls);
                return code;
            }
            gs_setmatrix(pgs, &advance_mat);
            if (angle >= 0) {
                /* Compensate for bitmap font non-rotation. */
                if (text_path == hpgl_text_right) {
                    hpgl_get_char_width(pgls, ch, &width);
                    hpgl_call(hpgl_add_point_to_path
                              (pgls, start_pt.x + width / scale.x, start_pt.y,
                               hpgl_plot_move_absolute, false));
                }
            }
            hpgl_call(gs_currentpoint(pgs, &end_pt));
            if (start_pt.x == end_pt.x && start_pt.y == end_pt.y) {
                /* freetype doesn't move currentpoint in gs_show(),
                 * since gs cache is not used.  NB we don't support
                 * freetype anymore is this necessary?
                 */
                hpgl_get_char_width(pgls, ch, &width);
                hpgl_call(hpgl_add_point_to_path(pgls, width / scale.x, 0.0,
                                                 hpgl_plot_move_relative,
                                                 false));
                hpgl_call(gs_currentpoint(pgs, &end_pt));
            }
            if ((text_path == hpgl_text_right) &&
                (hpgl_get_character_extra_space_x(pgls) != 0)) {
                hpgl_get_char_width(pgls, ch, &width);
                end_pt.x = start_pt.x + width / scale.x;
                hpgl_call(hpgl_add_point_to_path
                          (pgls, end_pt.x, end_pt.y, hpgl_plot_move_absolute,
                           false));
            }
        }
        /*
         * Adjust the final position according to the text path.
         */
        switch (text_path) {
            case hpgl_text_right:
                break;

            case hpgl_text_down:
                {
                    hpgl_real_t height;

                    hpgl_call(hpgl_get_current_cell_height(pgls, &height));
                    hpgl_call(hpgl_add_point_to_path
                              (pgls, start_pt.x, end_pt.y - height / scale.y,
                               hpgl_plot_move_absolute, false));

                }
                break;

            case hpgl_text_left:
                hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x, start_pt.y,
                                                 hpgl_plot_move_absolute,
                                                 false));
                break;
            case hpgl_text_up:
                {
                    hpgl_real_t height;

                    hpgl_call(hpgl_get_current_cell_height(pgls, &height));
                    hpgl_call(hpgl_add_point_to_path(pgls, start_pt.x,
                                                     end_pt.y +
                                                     height / scale.y,
                                                     hpgl_plot_move_absolute,
                                                     false));
                }
                break;
        }

        gs_setmatrix(pgs, &save_ctm);
        hpgl_call(gs_currentpoint(pgs, &end_pt));
        if (!use_show)
            hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_character));

        hpgl_call(hpgl_add_point_to_path(pgls,
                                         end_pt.x,
                                         end_pt.y,
                                         hpgl_plot_move_absolute, true));
    }

    return 0;
}

/* Determine whether labels can concatenate. */
/* Note that LO requires a pre-scan iff this is false. */
static bool
hpgl_can_concat_labels(const hpgl_state_t * pgls)
{                               /* The following is per TRM 23-78. */
    static const byte can_concat[22] = {
        0, 9, 1, 3, 8, 0, 2, 12, 4, 6,
        0, 9, 1, 3, 8, 0, 2, 12, 4, 6,
        0, 9
    };

    return (can_concat[pgls->g.label.origin] &
            (1 << pgls->g.character.text_path)) != 0;
}

/* return relative coordinates to compensate for origin placement -- LO */
static int
hpgl_get_character_origin_offset(hpgl_state_t * pgls, int origin,
                                 hpgl_real_t width, hpgl_real_t height,
                                 gs_point * offset)
{
    double pos_x = 0.0, pos_y = 0.0;
    double off_x = 0.0, off_y = 0.0;
    hpgl_real_t adjusted_height = height;

#ifdef CHECK_UNIMPLEMENTED
    if (pgls->g.character.extra_space.x != 0
        || pgls->g.character.extra_space.y != 0)
        dmprintf(pgls->memory,
                 "warning origin offset with non zero extra space not supported\n");
#endif
    adjusted_height /= 1.6;
    if (hpgl_is_currentfont_stick(pgls))
        adjusted_height /= 1.4;

    /* offset values specified by the documentation */
    if ((origin >= 11 && origin <= 14) || (origin >= 16 && origin <= 19)) {
        if (hpgl_is_currentfont_stick(pgls)) {
            off_x = off_y = 0.33 * adjusted_height;
        } else {
            /* the documentation says this should be .25, experiments
               indicate .33 like stick fonts. */
            off_x = off_y = 0.33 * adjusted_height;
        }
    }

    switch (origin) {
        case 11:
            pos_x = -off_x;
            pos_y = -off_y;
        case 1:
            break;
        case 12:
            pos_x = -off_x;
        case 2:
            pos_y = .5 * adjusted_height;
            break;
        case 13:
            pos_x = -off_x;
            pos_y = off_y;
        case 3:
            pos_y += adjusted_height;
            break;
        case 14:
            pos_y = -off_y;
        case 4:
            pos_x = .5 * width;
            break;
        case 15:
        case 5:
            pos_x = .5 * width;
            pos_y = .5 * adjusted_height;
            break;
        case 16:
            pos_y = off_y;
        case 6:
            pos_x = .5 * width;
            pos_y += adjusted_height;
            break;
        case 17:
            pos_x = off_x;
            pos_y = -off_y;
        case 7:
            pos_x += width;
            break;
        case 18:
            pos_x = off_x;
        case 8:
            pos_x += width;
            pos_y = .5 * adjusted_height;
            break;
        case 19:
            pos_x = off_x;
            pos_y = off_y;
        case 9:
            pos_x += width;
            pos_y += adjusted_height;
            break;
        case 21:
            {
                /* // LO21 prints at the current position  not pcl CAP.
                   gs_matrix save_ctm;
                   gs_point pcl_pos_dev, label_origin;

                   gs_currentmatrix(pgls->pgs, &save_ctm);
                   pcl_set_ctm(pgls, false);
                   hpgl_call(gs_transform(pgls->pgs, (double)pgls->cap.x,
                   (double)pgls->cap.y, &pcl_pos_dev));
                   gs_setmatrix(pgls->pgs, &save_ctm);
                   hpgl_call(gs_itransform(pgls->pgs, (double)pcl_pos_dev.x,
                   (double)pcl_pos_dev.y, &label_origin));
                   pos_x = -(pgls->g.pos.x - label_origin.x);
                   pos_y = (pgls->g.pos.y - label_origin.y);
                 */
            }
            break;
        default:
            dmprintf(pgls->memory, "unknown label parameter");

    }
    /* a relative move to the new position */
    offset->x = pos_x;
    offset->y = pos_y;

    /* account for character direction and slant */
    hpgl_rotation_transform_distance(pgls, offset, offset);
    hpgl_slant_transform_distance(pgls, offset, offset);

    switch (pgls->g.character.text_path) {
        case hpgl_text_left:
            offset->x -= width;
            break;
        case hpgl_text_down:
            offset->y -= height;
            break;
        default:
            DO_NOTHING;
    }

    {
        gs_matrix mat;

        hpgl_compute_user_units_to_plu_ctm(pgls, &mat);
        offset->x /= mat.xx;
        offset->y /= mat.yy;
    }
    /* convert to user units */
    return 0;
}

static gs_char
hpgl_next_char(hpgl_state_t * pgls, byte ** ppb)
{
    byte *pb = *ppb;
    gs_char chr = *pb++;

    if (pgls->g.label.double_byte)
        chr = (chr << 8) + *pb++;
    *ppb = pb;
    return chr;
}

/* Prints a buffered line of characters. */
/* If there is a CR, it is the last character in the buffer. */
static int
hpgl_process_buffer(hpgl_state_t * pgls, gs_point * offset)
{
    hpgl_real_t label_length = 0.0, label_height = 0.0;
    bool vertical = hpgl_text_is_vertical(pgls->g.character.text_path);
    int i, inc;

    /* a peculiar side effect of LABEL parsing double byte characters
       is it leaves an extra byte in the buffer - fix that now, and
       properly set the increment for the parallel loops below. */
    if (pgls->g.label.double_byte) {
        pgls->g.label.char_count--;
        inc = 2;
    } else {
        inc = 1;
    }

    /*
     * NOTE: the two loops below must be consistent with each other!
     */

    {
        hpgl_real_t width = 0.0, height = 0.0;
        int save_index = pgls->g.font_selected;
        bool first_char_on_line = true;
        byte *b = pgls->g.label.buffer;

        for (i = 0; i < pgls->g.label.char_count; i += inc) {
            gs_char ch = hpgl_next_char(pgls, &b);

            if (ch < 0x20 && !pgls->g.transparent_data)
                switch (ch) {
                    case BS:
                        if (width == 0.0) {     /* BS as first char of string */
                            hpgl_call(hpgl_ensure_font(pgls));
                            hpgl_get_char_width(pgls, ' ', &width);
                            hpgl_call(hpgl_get_current_cell_height
                                      (pgls, &height));
                        }
                        if (vertical) { /* Vertical text path, back up in Y. */
                            label_height -= height;
                            if (label_height < 0.0)
                                label_height = 0.0;
                        } else {        /* Horizontal text path, back up in X. */
                            label_length -= width;
                            if (label_length < 0.0)
                                label_length = 0.0;
                        }
                        continue;
                    case LF:
                        first_char_on_line = true;
                        continue;
                    case CR:
                        continue;
                    case FF:
                        continue;
                    case HT:
                        hpgl_call(hpgl_ensure_font(pgls));
                        hpgl_get_char_width(pgls, ' ', &width);
                        width *= 5;
                        goto acc_ht;
                    case SI:
                        hpgl_select_font_pri_alt(pgls, 0);
                        continue;
                    case SO:
                        hpgl_select_font_pri_alt(pgls, 1);
                        continue;
                    default:
                        break;
                }
            hpgl_call(hpgl_ensure_font(pgls));
            hpgl_get_char_width(pgls, ch, &width);
          acc_ht:hpgl_call(hpgl_get_current_cell_height
                      (pgls, &height));
            if (vertical) {
                if (width > label_length)
                    label_length = width;
                if (!first_char_on_line)
                    label_height += height;
                else
                    first_char_on_line = false;
            } else {            /* Horizontal text path: sum widths, take max of heights. */
                label_length += width;
                if (height > label_height)
                    label_height = height;
            }
        }
        hpgl_select_font_pri_alt(pgls, save_index);
    }
    hpgl_call(hpgl_get_character_origin_offset(pgls, pgls->g.label.origin,
                                               label_length, label_height,
                                               offset));

    /* now add the offsets in relative plu coordinates */
    hpgl_call(hpgl_add_point_to_path(pgls, -offset->x, -offset->y,
                                     hpgl_plot_move_relative, false));

    {
        byte *b = pgls->g.label.buffer;

        for (i = 0; i < pgls->g.label.char_count; i += inc) {
            gs_char ch = hpgl_next_char(pgls, &b);

            if (ch < 0x20 && !pgls->g.transparent_data) {
                hpgl_real_t spaces, lines;

                switch (ch) {
                    case BS:
                        spaces = -1, lines = 0;
                        break;
                    case LF:
                        /*
                         * If the text path is vertical, we must use the
                         * computed label (horizontal) width, not the width
                         * of a space.
                         */
                        if (vertical) {
                            const pcl_font_selection_t *pfs =
                                &pgls->g.font_selection[pgls->g.
                                                        font_selected];
                            hpgl_real_t label_advance;

                            /* Handle size. */
                            if (pgls->g.character.size_mode ==
                                hpgl_size_not_set) {
                                /* Scale fixed-width fonts by pitch, variable-width by height. */
                                if (pfs->params.proportional_spacing) {
                                    if (pl_font_is_scalable(pfs->font))
                                        label_advance =
                                            hpgl_points_2_plu(pgls,
                                                              pfs->params.
                                                              height_4ths /
                                                              4.0);
                                    else {
                                        double ratio =
                                            (double)pfs->params.height_4ths /
                                            pfs->font->params.height_4ths;
                                        label_advance =
                                            ratio * inches_2_plu(1.0 /
                                                                 pfs->font->
                                                                 resolution.
                                                                 x);
                                    }
                                } else
                                /*** WRONG ****/
                                    label_advance =
                                        hpgl_points_2_plu(pgls,
                                                          pl_fp_pitch_cp
                                                          (&pfs->params) /
                                                          ((pl_fp_pitch_cp
                                                            (&pfs->font->
                                                             params) /
                                                            10.0)));
                                if (hpgl_get_character_extra_space_x(pgls) !=
                                    0)
                                    label_advance *=
                                        1.0 +
                                        hpgl_get_character_extra_space_x
                                        (pgls);
                            } else {
                                /*
                                 * Note that the CTM takes P1/P2 into account unless
                                 * an absolute character size is in effect.
                                 *
                                 *
                                 * HACKS - I am not sure what this should be the
                                 * actual values ???
                                 */
                                label_advance =
                                    pgls->g.character.size.x * 1.5 * 1.25;
                                if (pgls->g.character.size_mode ==
                                    hpgl_size_relative)
                                    label_advance *=
                                        pgls->g.P2.x - pgls->g.P1.x;
                                if (pgls->g.bitmap_fonts_allowed)       /* no mirroring */
                                    label_advance = fabs(label_advance);
                            }
                            hpgl_call(hpgl_move_cursor_by_characters(pgls, 0, -1,
                                                           &label_advance));
                            continue;
                        }
                        spaces = 0, lines = -1;
                        break;
                    case CR:
                        hpgl_call(hpgl_do_CR(pgls));
                        continue;
                    case FF:
                        /* does nothing */
                        spaces = 0, lines = 0;
                        break;
                    case HT:
                        /* appears to expand to 5 spaces */
                        spaces = 5, lines = 0;
                        break;
                    case SI:
                        hpgl_select_font_pri_alt(pgls, 0);
                        continue;
                    case SO:
                        hpgl_select_font_pri_alt(pgls, 1);
                        continue;
                    default:
                        goto print;
                }
                hpgl_call(hpgl_move_cursor_by_characters(pgls, spaces, lines,
                                               (const hpgl_real_t *)0));
                continue;
            }
          print:{
                /* if this a printable character print it
                   otherwise continue, a character can be
                   printable and undefined in which case
                   it is printed as a space character */
                const pcl_font_selection_t *pfs =
                    &pgls->g.font_selection[pgls->g.font_selected];
                if (!char_is_printable(pfs->font, pfs->map, ch,
                                       (pfs->params.
                                        typeface_family & 0xfff) ==
                                       STICK_FONT_TYPEFACE,
                                       pgls->g.transparent_data))
                    continue;
            }
            hpgl_call(hpgl_ensure_font(pgls));
            hpgl_call(hpgl_print_char(pgls, ch));
        }
    }
    pgls->g.label.char_count = 0;
    return 0;
}

/**
 *  used by hpgl_LB() to find the end of a label
 *
 * 8bit and 16bit label terminator check
 *  (prev << 8) & curr -> 16 bit
 *  have_16bits allows per byte call with true on 16bit boundary.
 */
static bool
is_terminator(hpgl_state_t * pgls, byte prev, byte curr, bool have_16bits)
{
    return
        pgls->g.label.double_byte ?
        (have_16bits && prev == 0 && curr == pgls->g.label.terminator) :
        (curr == pgls->g.label.terminator);
}

#define GL_LB_HAVE_16BITS (pgls->g.label.have_16bits)
#define GL_LB_CH (pgls->g.label.ch)
#define GL_LB_PREV_CH (pgls->g.label.prev_ch)

/* LB ..text..terminator */
int
hpgl_LB(hpgl_args_t * pargs, hpgl_state_t * pgls)
{
    const byte *p = pargs->source.ptr;
    const byte *rlimit = pargs->source.limit;
    bool print_terminator = pgls->g.label.print_terminator;
    int code = 0;

    if (pargs->phase == 0) {
        /* initialize the character buffer and CTM first time only */
        hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector));
        hpgl_call(hpgl_init_label_buffer(pgls));
        hpgl_call(hpgl_set_ctm(pgls));
        code = hpgl_set_clipping_region(pgls, hpgl_rm_vector);
        if (code < 0) {
            hpgl_destroy_label_buffer(pgls);
            return code;
        }
        pgls->g.label.initial_pos = pgls->g.pos;
        hpgl_call(hpgl_add_point_to_path
                  (pgls, pgls->g.pos.x, pgls->g.pos.y,
                   hpgl_plot_move_absolute, true));
        GL_LB_HAVE_16BITS = true;
        GL_LB_CH = 0xff;
        GL_LB_PREV_CH = 0xff;   /* for two byte terminators */
        pargs->phase = 1;
    }

    while (p < rlimit) {
        /* This is not ugly and unintuitive */
        GL_LB_HAVE_16BITS = !GL_LB_HAVE_16BITS;
        GL_LB_PREV_CH = GL_LB_CH;
        GL_LB_CH = *++p;
        if_debug1m('i', pgls->memory,
                   (GL_LB_CH == '\\' ? " \\%c" : GL_LB_CH >= 33
                    && GL_LB_CH <= 126 ? " %c" : " \\%03o"), GL_LB_CH);
        if (is_terminator(pgls, GL_LB_PREV_CH, GL_LB_CH, GL_LB_HAVE_16BITS)) {
            if (!print_terminator) {
                gs_point lo_offsets;

                code = hpgl_process_buffer(pgls, &lo_offsets);
                hpgl_destroy_label_buffer(pgls);
                if (code < 0) {
                    hpgl_free_stick_fonts(pgls);
                    return code;
                }
                pargs->source.ptr = p;
                /*
                 * Depending on the DV/LO combination, conditionally
                 * restore the initial position, per TRM 23-78.
                 */
                if (!hpgl_can_concat_labels(pgls)) {
                    code = hpgl_add_point_to_path(pgls,
                                                     pgls->g.
                                                     carriage_return_pos.x,
                                                     pgls->g.
                                                     carriage_return_pos.y,
                                                     hpgl_plot_move_absolute,
                                                     true);
                } else {
                    /* undo the label origin offsets */
                    code = hpgl_add_point_to_path
                              (pgls, lo_offsets.x, lo_offsets.y,
                               hpgl_plot_move_relative, false);
                }
                /* also clean up stick fonts - they are likely to
                   become dangling references in the current font
                   scheme since they don't have a dictionary entry */
                hpgl_free_stick_fonts(pgls);
                if (code < 0)
                    return code;
                /* clear the current path terminating carriage
                   returns and linefeeds will leave "moveto's" in
                   the path */
                hpgl_call(hpgl_clear_current_path(pgls));
                return 0;
            }
            /*
             * Process the character in the ordinary way, then come here
             * again.  We do things this way to simplify the case where
             * the terminator is a control character.
             */
            --p;
            print_terminator = false;
        }

        /* process the buffer for a carriage return so that we can
           treat the label origin correctly, and initialize a new
           buffer */

        hpgl_call(hpgl_buffer_char(pgls, GL_LB_CH));
        if (GL_LB_CH == CR && !pgls->g.transparent_data) {
            gs_point lo_offsets;

            code = hpgl_process_buffer(pgls, &lo_offsets);
            hpgl_destroy_label_buffer(pgls);
            if (code >= 0)
                code = hpgl_init_label_buffer(pgls);
            if (code < 0) {
                hpgl_free_stick_fonts(pgls);
                return code;
            }
        }
    }
    pargs->source.ptr = p;
    return gs_error_NeedInput;
}

void
hpgl_free_531_fonts(hpgl_state_t * pgls)
{
    pl_font_t *font = &pgls->g.dl_531_font[pgls->g.font_selected];

    if (font->pfont != 0) {
        gs_free_object(pgls->memory, font->pfont, "531 font");
        font->pfont = 0;
    }
    return;
}

void
hpgl_free_stick_fonts(hpgl_state_t * pgls)
{
    pcl_font_selection_t *pfs =
        &pgls->g.font_selection[pgls->g.font_selected];
    pl_font_t *font = &pgls->g.stick_font[pgls->g.font_selected]
        [pfs->params.proportional_spacing];

    pl_font_glyph_width_cache_remove_nodes(font);

    /* no stick fonts - nothing to do */
    if (font->pfont == 0)
        return;

    gs_free_object(pgls->memory, font->pfont, "stick/arc font");
    font->pfont = 0;
    return;
}

int
hpgl_print_symbol_mode_char(hpgl_state_t * pgls)
{
    /* save the original origin since symbol mode character are
       always centered */
    int saved_origin = pgls->g.label.origin;
    gs_point save_pos = pgls->g.pos;
    gs_point lo_offsets;
    int code;

    hpgl_call(hpgl_gsave(pgls));
    /* HAS this need checking.  I don't know how text direction
       and label origin interact in symbol mode */
    pgls->g.label.origin = 5;
    /* HAS - alot of work for one character */
    if (((code = hpgl_clear_current_path(pgls)) >= 0) &&
        ((code = hpgl_init_label_buffer(pgls)) >= 0)) {
        if ((code = hpgl_buffer_char(pgls, pgls->g.symbol_mode)) >= 0)
            code = hpgl_process_buffer(pgls, &lo_offsets);
        hpgl_destroy_label_buffer(pgls);
    }
    {
        /* must grestore here from earlier gsave */
        int code2 = hpgl_grestore(pgls);
        if (code >= 0) code = code2;
    }
    hpgl_free_stick_fonts(pgls);
    /* restore the origin */
    pgls->g.label.origin = saved_origin;
    if (code >= 0)
        hpgl_call(hpgl_set_current_position(pgls, &save_pos));
    return code;
}

/* Initialization */
static int
pglabel_do_registration(pcl_parser_state_t * pcl_parser_state,
                        gs_memory_t * mem)
{                               /* Register commands */
    DEFINE_HPGL_COMMANDS(mem)
        HPGL_COMMAND('C', 'P', hpgl_CP,
                     hpgl_cdf_lost_mode_cleared | hpgl_cdf_pcl_rtl_both),
        /* LB also has special argument parsing. */
        HPGL_COMMAND('L', 'B', hpgl_LB,
                     hpgl_cdf_polygon | hpgl_cdf_lost_mode_cleared |
                     hpgl_cdf_pcl_rtl_both),
    END_HPGL_COMMANDS
    return 0;
}

const pcl_init_t pglabel_init = {
    pglabel_do_registration, 0
};