summaryrefslogtreecommitdiff
path: root/dviware/dvi2bitmap/PkFont.cc
blob: a74549fa1577f6e0758563e13950851b32a7a851 (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
/* This file is part of dvi2bitmap; see README for copyrights and licence */

#include <config.h>

#include <iostream>		// for debugging code, written to cerr
#include <fstream>
#include <sys/stat.h>
//#include <unistd.h>
#include <vector>
#include <string>
//#include <memory>		// for auto_ptr

#ifdef HAVE_CSTD_INCLUDE
#  include <cstring>
#  include <cmath>
#  include <cstdlib>
#else
#  include <string.h>
#  include <math.h>
#  include <stdlib.h>
#endif

using STD::cerr;
using STD::endl;
using STD::ends;
using STD::ofstream;
using STD::ios;

#include <DviError.h>
#include <FileByteStream.h>
#include <PipeStream.h>
#include <PkFont.h>
#include <Util.h>
#include <stringstream.h>
#ifdef ENABLE_KPATHSEA
#include <KarlPathSearcher.h>
#endif


string_list break_path (string);
    
// define class static variables
verbosities PkRasterdata::verbosity_ = normal;
verbosities PkFont::verbosity_ = normal;
verbosities PkGlyph::verbosity_ = normal;


#ifndef DEFAULT_MFMODE
#define DEFAULT_MFMODE "ibmvga"
#endif
#ifndef DEFAULT_RESOLUTION
#define DEFAULT_RESOLUTION 110
#endif

string PkFont::missingFontMode_ = DEFAULT_MFMODE;
int PkFont::resolution_ = DEFAULT_RESOLUTION;

// Font-search strategies: manipulated by setFontSearchPath() and
// setFontSearchCommand().  This could be made a lot more
// sophisticated, but I can't yet decide in precisely which way.  The
// initialisation would probably be better done using
// setFontSearchStrategy_() in a class initialiser (but I've forgotten
// how to do that in C++!)
//unsigned int PkFont::fontSearchStrategies_ = ~0;
unsigned int PkFont::fontSearchStrategies_ =
	fontSearchStrategyPath_
#ifdef FONT_SEARCH_SCRIPT
	| fontSearchStrategyCommand_
#endif
#ifdef ENABLE_KPATHSEA
	| fontSearchStrategyKpse_
#endif
	;

string PkFont::fontSearchPath_ = "";
#ifdef FONT_SEARCH_SCRIPT
string PkFont::fontSearchCommandTemplate_ = FONT_SEARCH_SCRIPT;
#else
string PkFont::fontSearchCommandTemplate_ = "";
#endif /* defined FONT_SEARCH_SCRIPT */

#ifdef FONT_GEN_TEMPLATE
string PkFont::fontgenCommandTemplate_ = FONT_GEN_TEMPLATE;
bool PkFont::makeMissingFonts_ = true;
#else
string PkFont::fontgenCommandTemplate_ = "";
bool PkFont::makeMissingFonts_ = false;
#endif /* defined FONT_GEN_TEMPLATE */


/**
 * Represents a PK font.
 *
 * <p>The constructor arguments are those read from the DVI file font
 * declaration, after a <code>fnt_def</code> opcode, except for
 * <code>dvimag</code>, which is the overall DVI magnification
 * factor, and includes the file and command-line magnification
 * adjustments.  For more details, see the definition of the font
 * declaration in section `A.4 Font defintions' of the DVI standard,
 * and the use of these factors in method {@link #magnification}.
 *
 * @param dvimag the scale factor (1.0 = no magnification) by which
 * the font is to be magnified as it is read from the PK file
 *
 * @param c the font checksum expected
 *
 * @param s fixed-point scale factor applied to the character widths
 * in the font
 *
 * @param d the fixed-point `design size' of the font
 *
 * @param name the name of the font
 */
PkFont::PkFont(double dvimag,
	       unsigned int c,
	       unsigned int s,
	       unsigned int d,
	       string name)
    : name_(name), pkf_(0), font_loaded_(false), dvimag_(dvimag), seen_in_doc_(false)
{
    font_header_.c = c;
    font_header_.s = s;
    font_header_.d = d;

    path_ = "";

    if (verbosity_ > normal)
	cerr << "PkFont::PkFont " << name
	     << " c=" << c
	     << " s=" << s
	     << " d=" << d
	     << endl;

    try
    {
	string pk_file_path;
	bool got_path = find_font (pk_file_path);
	if (! got_path)
	{
	    if (makeMissingFonts_)
	    {
		string cmd;

		cmd = fontgenCommand();
		if (verbosity_ >= normal)
		    cerr << "mktexpk: " << cmd << endl;
		if (cmd.length() == 0)
		    throw InputByteStreamError
			("can't generate fontgen command");

		PipeStream *PS = new PipeStream(cmd);
		string fontpath = PS->getResult();
		if (PS->getTerminationStatus() != 0)
		    throw InputByteStreamError ("unable to generate font");
		delete PS;
		// Try searching again, rather than relying on the
		// return value from the pipe (there's no reason
		// to expect it to be unreliable, but re-using
		// find_font() seems more consistent).
		got_path = find_font (pk_file_path);
		if (! got_path)
		{
		    // We didn't find it, for some reason, but `fontpath'
		    // returned from the pipe should have the string.  Use that
		    // instead, but warn about it.  I _think_ there's a
		    // race-condition which sometimes makes the find_font() fail
		    // to find the font if it's called immediately after the
		    // font is successfully generated.
		    if (verbosity_ >= normal)
			cerr << "Warning: tried (apparently successfully) to create font, but couldn't find it afterwards."
                             << endl
                             << "Consider setting DVI2BITMAP_PK_PATH (see docs, based on "
                             << fontpath << ")" << endl
                             << "I'll use what I think is the correct path to it (fingers crossed)"
			     << endl;
		    pk_file_path = fontpath;
		}
	    }
	    else
		throw InputByteStreamError
		    ("can't find font file, and font-generation disabled or impossible");
	}
	path_ = pk_file_path;

	pkf_ = new FileByteStream (path_, "", true);
	for (unsigned int i=0; i<nglyphs_; i++)
	    glyphs_[i] = 0;
	read_font (*pkf_);
	delete pkf_;		// don't need the file any more

	if (verbosity_ > normal)
	    cerr << "Opened font " << path_ << " successfully" << endl;

	if (preamble_.cs != c)
	    if (verbosity_ > quiet)
		cerr << "Warning: Font " << name_
		     << "found : expected checksum " << c
		     << ", got checksum " << preamble_.cs
		     << endl;

	font_loaded_ = true;
    }
    catch (InputByteStreamError& e)
    {
	if (verbosity_ > quiet)
	    cerr << "Warning: Font " << name << " at "
		 << dpiScaled() << "dpi ("
		 << path_ << ") not found ("
		 << e.problem()
		 << ")" << endl;
	preamble_.cs = 0;
	glyphs_[0] = new PkGlyph(resolution_, this); // dummy glyph
    }
    //quad_ = ((double)dvimag/1000.0) * d;

    // These next three quantities should be given in DVI units
    // _without_ the overall DVI magnification.  The function
    // magnification() includes that, so remove it.
    quad_ = d * magnification(false);
    //quad_ = d * magnification(true);
    //quad_ = d * dvimag_;
    word_space_ = 0.2*quad_;
    back_space_ = 0.9*quad_;
    if (verbosity_ > normal)
	cerr << "Quad="<<quad_<<" dvi units" << endl;
}

PkFont::~PkFont()
{
}

verbosities PkFont::verbosity (const verbosities level)
{
    enum verbosities oldv = verbosity_;
    verbosity_ = level;
#ifdef ENABLE_KPATHSEA
    KarlPathSearcher::verbosity (level);
#endif
    return oldv;
}

/**
 * Find a font.  Uses one or all of the font-search strategies
 * specified using the <code>setFontSearch...</code> methods.  The
 * result of the first one which succeeds is the one returned.
 *
 * <p>Return true if we found a file to open, and return the path in the
 * argument, which is unchanged otherwise.  Return false on error.
 * Success doesn't guarantee that the file exists, just that it was
 * constructed without problems (in the current implementation, success
 * <em>does</em> mean that the file was found, but this is not guaranteed by
 * this routine).
 *
 * @param path reference to a path, which is filled in on output if
 * the search was successful
 * @return true on success, false on error
 */
bool PkFont::find_font (string& path)
{
    bool got_it = false;
    double scaled_res = resolution_ * magnification();

    if (verbosity_ > normal)
	cerr << "PkFont::find_font: " << name_
	     << ", checksum=" << font_header_.c
	     << ", res " << resolution_
	     << '*' << magnification()
	     << " = " << scaled_res
	     << " (fontSearchStrategies_=" << fontSearchStrategies_ << ')'
	     << endl;

    if (fontSearchStrategies_ & fontSearchStrategyPath_) 
    {
	string pkpath = "";
	if (fontSearchPath_.length() > 0)
	{
	    pkpath = fontSearchPath_;
	    if (verbosity_ > normal)
		cerr << "find_font: using fontpath=" << fontSearchPath_ << endl;
	}
	else
	{
	    const char *pkpath_p = getenv ("DVI2BITMAP_PK_PATH");
	    if (pkpath_p != 0)
	    {
		pkpath = pkpath_p;
		if (verbosity_ > normal)
		    cerr << "find_font: using DVI2BITMAP_PK_PATH="
			 << pkpath << endl;
	    }
	}

	if (pkpath.length() != 0)
	{
	    string& found_file = search_pkpath (pkpath, name_, scaled_res);

	    if (found_file.length() > 0)
	    {
		path = found_file;
		got_it = true;
	    }

	    if (verbosity_ > normal)
		cerr << "PkFont::find_font: search_pkpath produced "
		     << (got_it ? path : "...nothing!") << endl;
	}
    }
    
#ifdef ENABLE_KPATHSEA
    if (! got_it
	&& (fontSearchStrategies_ & fontSearchStrategyKpse_))
    {
	const char *kpse_file;
	//std::auto_ptr<KarlPathSearcher> kps = KarlPathSearcher::getInstance();
	KarlPathSearcher* kps = KarlPathSearcher::getInstance();

	kpse_file = kps->find (name_.c_str(),
			       static_cast<int>(scaled_res));

	if (kpse_file != 0)
	{
	    path = kpse_file;
	    got_it = true;
	}

        if (verbosity_ > normal)
            cerr << "PkFont::find_font: (KarlPathSearcher)->find("
                 << name_ << "," << static_cast<int>(scaled_res) << ") = "
                 << (got_it ? path : "...nothing!") << endl;

    }
#endif

    if (! got_it
	&& (fontSearchStrategies_ & fontSearchStrategyCommand_)
	&& (fontSearchCommandTemplate_.length() != 0))
    {
	string& cmd = substitute_font_string (fontSearchCommandTemplate_,
					      missingFontMode_,
					      name_,
					      dpiScaled(),
					      dpiBase(),
					      magnification());
	if (verbosity_ > normal)
	    cerr << "PkFont::find_font: running cmd <"
		 << cmd << ">..." << endl;

	PipeStream *PS = new PipeStream(cmd);
	string font_found = PS->getResult();
	int status = PS->getTerminationStatus();
	delete PS;

	if (verbosity_ > normal)
	    cerr << "    ...produced <" << font_found
                 << "> (status=" << status << ')' << endl;

        // Checking the length of the found font seems more reliable than
        // checking the exit status, since kpsewhich apparently returns a 
        // non-zero exit status even if it finds the file, which isn't
        // terribly helpful
	if (font_found.length() > 0)
	{
	    path = font_found;
	    got_it = true;
	}
    }

    if (! got_it)
    {
	// write font-generation command string to missfont.log
	string fontgenCmd = fontgenCommand();
        if (verbosity_ > normal)
            cerr << "PkFont::find_font: no font found, writing <"
                 << fontgenCmd << "> to missfont.log" << endl;
        
	if (fontgenCmd.length() != 0)
	{
	    ofstream missfont;
	    missfont.open("./missfont.log", ios::app);
	    if (!missfont)
		cerr << "Can't open ./missfont.log to write" << endl;
	    else
	    {
		missfont << fontgenCmd << endl;
		missfont.close();
	    }
	}
    }

    return got_it;
}

/**
 * Find a file on the font path, with the specified name,
 * at the specified resolution.
 *
 * <p>Do font rounding: Check all the integers between 0.998 and 1.002 of
 * the specified resolution.  If, however, this range falls within
 * (n,n+1) (non-inclusive) (ie, it doesn't span <em>any</em> integers) then
 * simply round the number.
 * See the DVI Driver Standard for more details on the font-rounding
 * algorithm.
 *
 * @param path a colon-separated list of directories to search for fonts
 * @param name the name of the font to look for
 * @param resolution the required resolution of the font
 *
 * @return a reference to a static string; if not found, return a
 * zero-length string
 */
string& PkFont::search_pkpath (string path, string name, double resolution)
{
    static string fname;		// return value

    int size_low, size_high;
    size_low  = static_cast<int>(STD::ceil  (0.998*resolution));
    size_high = static_cast<int>(STD::floor (1.002*resolution));
    if (size_low > size_high)
	// simply round it
	size_low = size_high = static_cast<int>(STD::floor(resolution+0.5));
    if (verbosity_ > normal)
	cerr << "PkFont::search_pkpath: searching "
	     << size_low << ".." << size_high << endl;

    string_list pathlist = break_path(path);
    if (pathlist.size() == 0)
    {
	fname = "";
	return fname;		// ...silently
    }

    bool found = false;

    for (string_list::const_iterator pi = pathlist.begin();
	 pi != pathlist.end();
	 pi++)
    {
	// Each member of pathlist is a template as defined by
	// PkFont::substitute_font_string().  Work through each in turn.

	for (int size=size_low; size<=size_high && !found; size++)
	{
	    fname = substitute_font_string (*pi,
					    PkFont::missingFontMode_,
					    name,
					    size,
					    dpiBase(),
					    magnification());

	    if (verbosity_ > normal)
		cerr << "PkFont::search_pkpath: trying " << *pi 
		     << "->" << fname << endl;

	    struct stat S;
	    if (stat (fname.c_str(), &S) == 0)
	    {
		// file exists
#ifdef WIN32
# define S_IRGRP 0
# define S_IROTH 0
#endif // WIN32
		if (S_ISREG(S.st_mode)
		    && (S.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
		{
		    found = true;	// exit the loop
		    if (verbosity_ > normal)
			cerr << "PkFont::search_pkpath: "
			     << fname << " found" << endl;
		}
		else
		    if (verbosity_ > normal)
			cerr << "PkFont::search_pkpath: " << fname
			     << " mode " << STD::oct << S.st_mode << STD::dec
			     << " (not readable)" << endl;
	    }
	    else
		if (verbosity_ > normal)
		    cerr << "PkFont::search_pkpath: "
			 << fname << " not found" << endl;
	}
    }

    if (!found)
        fname = "";
    
    return fname;
}

/**
 * Given a format string, return a reference to a string with format
 * specifiers replaced by font information.
 *
 * <p>The format specifiers are:
 * <table>
 * <tr><td>%M<td>mode<td>ibmvga
 * <tr><td>%f<td>font name<td>cmr10
 * <tr><td>%d<td>dpi<td>330
 * <tr><td>%b<td>base dpi<td>110
 * <tr><td>%m<td>magnification<td>3
 * <tr><td>%%<td>literal %-character
 * </table>
 * Any other format specifiers constitute an error.  There is no
 * support for any backslash escapes.
 *
 * @param fmt the format string, with the format specifiers described above
 * @param mode a Metafont mode string, such as <code>ibmvga</code>
 * @param fontname the name of a font, such as <code>cmr10</code>
 * @param dpi the requested size of a font in dots-per-inch
 * @param basedpi the design size of a font
 * @param magnification of the font
 *
 * @returns a reference to the formatted string; this is a static
 * string, which is overwritten on each call
 *
 * @throws PkError if it encounters an illegal format element.
 */
string& PkFont::substitute_font_string (const string fmt,
					const string mode,
					const string fontname,
					const int dpi,
					const int basedpi,
					const double magnification)
    throw (PkError)
{
    static string retval;
    SSTREAM newstring;

    if (verbosity_ > normal)
	cerr << "PkFont::substitute_font_string (fmt=" << fmt
	     << " mode=" << mode
	     << " fontname=" << fontname
	     << " dpi=" << dpi
	     << " basedpi=" << basedpi
	     << " magnification=" << magnification
	     << endl;

    for (const char *p = fmt.c_str(); *p != '\0'; p++)
    {
	if (*p == '%')
	    switch (*++p)
	    {
	      case 'M':
		newstring << mode;
		break;
	      case 'm':
	        newstring << magnification;
	        break;
	      case 'f':
		newstring << fontname;
		break;
	      case 'd':
		newstring << dpi;
		break;
	      case 'b':
		newstring << basedpi;
		break;
	      case '%':
		newstring << '%';
		break;
	      default:
		{
		    SSTREAM msg;
		    msg << "substitute_font_string: Invalid format character %"
			<< *p
			<< ".  Allowed characters {Mmfdb%}."
			<< ends;
		    throw PkError (SS_C_STR(msg));
		}
		/* NOTREACHED */
	    }
	else
	    newstring << *p;
    }
    retval = SS_STRING(newstring);

    return retval;
}


void PkFont::read_font (InputByteStream& pkf)
{
    // read the preamble, and check that the requested parameters match
    Byte preamble_opcode = pkf.getByte();
    if (preamble_opcode != 247)
	throw DviError ("PK file doesn't start with preamble");
    if ((preamble_.id = pkf.getByte()) != 89)
	throw DviError ("PK file has wrong ID byte");

    int comment_length = pkf.getByte();
    preamble_.comment = "";
    for (;comment_length > 0; comment_length--)
	preamble_.comment += static_cast<char>(pkf.getByte());
    unsigned int i = pkf.getUIU(4);
    preamble_.designSize = (double)i/(double)two20_;
    preamble_.cs   = pkf.getUIU(4);
    i = pkf.getUIU(4);
    preamble_.hppp = (double)i/(double)two16_;
    i = pkf.getUIU(4);
    preamble_.vppp = (double)i/(double)two16_;


    if (verbosity_ > normal)
	cerr << "PkFont::read_font " << name_
	     << " '" << preamble_.comment << "\'"
	     << " designSize="  << preamble_.designSize
	     << " cs=" << preamble_.cs
	     << " hppp=" << preamble_.hppp
	     << " vppp=" << preamble_.vppp
	     << "..." << endl;

    // Now scan through the file, reporting opcodes and character definitions
    bool end_of_scan = false;
    while (! end_of_scan)
    {
	Byte opcode = pkf.getByte();
	if (opcode <= 239)	// character definition
	{
	    bool two_byte = opcode & 4;
	    Byte pl_prefix = static_cast<Byte>(opcode & 3);
	    unsigned int packet_length;
	    //unsigned int pos;	// primarily for debugging output

	    unsigned int g_cc, g_tfmwidth, g_dm, g_dx, g_dy, g_w, g_h;
	    int g_hoff, g_voff;
	    if (two_byte)
		if (pl_prefix == 3) // long-form character preamble
		{
		    packet_length = pkf.getUIU(4);
		    g_cc       = pkf.getUIU(4);
		    g_tfmwidth = pkf.getUIU(4);
		    g_dx       = pkf.getUIU(4);
		    g_dy       = pkf.getUIU(4);
		    g_w        = pkf.getUIU(4);
		    g_h        = pkf.getUIU(4);
		    g_hoff     = pkf.getSIS(4);
		    g_voff     = pkf.getSIS(4);

		    if (g_cc >= nglyphs_) // g_cc unsigned, so >= 0
			throw DviError
			    ("PK file has out-of-range character code");

		    //pos = pkf.pos();
		    packet_length -= 7*4;

		    if (verbosity_ > debug)
			cerr << "chardef L "
			     << static_cast<int>(g_cc)
			     << "=`"
			     << (g_cc>' ' && g_cc<='~' ?static_cast<char>(g_cc)
						       :'?')
			     << "\' "
			     << STD::hex
			     << ": opcode=" << static_cast<int>(opcode)
			     << " pl=" << packet_length
			     << " cc=" << g_cc
			     << " tfmwidth=" << g_tfmwidth
			     << " dx=" << g_dx
			     << " dy=" << g_dy
			     << " w=" << g_w
			     << " h=" << g_h
			     << " hoff=" << g_hoff
			     << " voff=" << g_voff
			     << STD::dec
			     << "(len="<<packet_length<<")"
				//<< "(pos="<<pos<<" len="<<packet_length<<")"
			     << endl;

		    PkRasterdata *rd
			= new PkRasterdata (opcode,
					    pkf.getBlock(packet_length),
					    packet_length, g_w, g_h);
		    glyphs_[g_cc] = new PkGlyph (g_cc, g_tfmwidth, g_dx, g_dy, 
						 g_w, g_h, g_hoff, g_voff,
						 rd, this);
		    //pkf.skip (packet_length);
		}
		else		// extended short form character preamble
		{
		    packet_length = pl_prefix;
		    packet_length <<= 16;
		    packet_length += pkf.getUIU(2);
		    g_cc       = pkf.getByte();
		    g_tfmwidth = pkf.getUIU(3);
		    g_dm       = pkf.getUIU(2);
		    g_w        = pkf.getUIU(2);
		    g_h        = pkf.getUIU(2);
		    g_hoff     = pkf.getSIS(2);
		    g_voff     = pkf.getSIS(2);

		    if (g_cc >= nglyphs_) // g_cc unsigned, so >= 0
			throw DviError
			    ("PK file has out-of-range character code");

		    //pos = pkf.pos();
		    packet_length -= 3 + 5*2;

		    if (verbosity_ > debug)
			cerr << "chardef XS "
			     << static_cast<int>(g_cc)
			     << "=`"
			     << (g_cc>' ' && g_cc<='~' ?static_cast<char>(g_cc)
						       :'?')
			     << "\' "
			     << STD::hex
			     << ": opcode=" << static_cast<int>(opcode)
			     << " pl=" << packet_length
			     << " cc=" << g_cc
			     << " tfmwidth=" << g_tfmwidth
			     << " dm=" << g_dm
			     << " w=" << g_w
			     << " h=" << g_h
			     << " hoff=" << g_hoff
			     << " voff=" << g_voff
			     << STD::dec
			     << "(len="<<packet_length<<")"
				//<< "(pos="<<pos<<" len="<<packet_length<<")"
			     << endl;

		    PkRasterdata *rd
			= new PkRasterdata (opcode,
					    pkf.getBlock(packet_length),
					    packet_length, g_w, g_h);
		    glyphs_[g_cc] = new PkGlyph (g_cc, g_tfmwidth, g_dm,
						 g_w, g_h, g_hoff, g_voff,
						 rd, this);
		    //pkf.skip (packet_length);
		}
	    else		// short form character preamble
	    {
		packet_length = pl_prefix;
		packet_length <<= 8;
		packet_length += pkf.getByte();
		g_cc       = pkf.getByte();
		g_tfmwidth = pkf.getUIU(3);
		g_dm       = pkf.getUIU(1);
		g_w        = pkf.getUIU(1);
		g_h        = pkf.getUIU(1);
		g_hoff     = pkf.getSIS(1);
		g_voff     = pkf.getSIS(1);

		if (g_cc >= nglyphs_) // g_cc unsigned, so >= 0
		    throw DviError
			("PK file has out-of-range character code");

		//pos = pkf.pos();
		packet_length -= 8;

		if (verbosity_ > debug)
		    cerr << "chardef S "
			 << static_cast<int>(g_cc)
			 << "=`"
			 << (g_cc>' ' && g_cc<='~' ? static_cast<char>(g_cc)
						   : '?')
			 << "\' "
			 << STD::hex
			 << ": opcode=" << static_cast<int>(opcode)
			 << " pl=" << packet_length
			 << " cc=" << g_cc
			 << " tfmwidth=" << g_tfmwidth
			 << " dm=" << g_dm
			 << " w=" << g_w
			 << " h=" << g_h
			 << " hoff=" << g_hoff
			 << " voff=" << g_voff
			 << STD::dec
			 << "(len="<<packet_length<<")"
			    //<< "(pos="<<pos<<" len="<<packet_length<<")"
			 << endl;

		PkRasterdata *rd
		    = new PkRasterdata (opcode,
					pkf.getBlock(packet_length),
					packet_length, g_w, g_h);
		glyphs_[g_cc] = new PkGlyph (g_cc, g_tfmwidth, g_dm,
					     g_w, g_h, g_hoff, g_voff,
					     rd, this);
		//pkf.skip(packet_length);
	    }
	    if (verbosity_ > debug)
		cerr << "charsizes " << g_cc
		     << " tfm=" << g_tfmwidth
		     << " w="   << g_w
		     << " h="   << g_h
		     << " off=(" << g_hoff << ',' << g_voff
			//<< ") at " << pos
		     << "(len " << packet_length << ")" << endl;
	}
	else			// opcode is command
	{
	    int lenb = 0;
	    switch (opcode)
	    {
	      case 243:		// pk_xxx4
		lenb++;
	      case 242:		// pk_xxx3
		lenb++;
	      case 241:		// pk_xxx2
		lenb++;
	      case 240:		// pk_xxx1
		lenb++;
		{
		    string special = "";
		    for (unsigned int special_len = pkf.getUIU(lenb);
			 special_len > 0;
			 special_len--)
			special += static_cast<char>(pkf.getByte());
		    if (verbosity_ > debug)
			cerr << "Special \'" << special << "\'" << endl;
		}
		break;
	      case 244:		// pk_yyy
		for (int ti=0; ti<4; ti++) (void) pkf.getByte();
		break;
	      case 245:		// pk_post
		end_of_scan = true;
		break;
	      case 246:		// pk_no_op
		break;
	      case 247:		// pk_pre
		throw DviError ("Found PK preamble in body of file");
	      default:
		throw DviError ("Found unexpected opcode in PK file");
	    }
	}
    }

    if (verbosity_ > normal)
	cerr << "PkFont::read_font ...finished reading " << name_ << endl;
}

/**
 * Obtain the magnification of this font.  This includes both font scaling
 * and overall DVI file magnification: this number is <em>mag/1000 .
 * s/d</em>, where <em>s</em> and <em>d</em> are taken from the font
 * definition in the DVI file, and <em>mag</em> is the total
 * magnification taking into account DVI preamble magnification and
 * any command-line overriding.
 *
 * @param includeDviMag if true (the default), include the overall
 * DVI file magnification <code>mag</code> (as set in the
 * constructor); if false, do not
 *
 * @return the font magnification
 */
double PkFont::magnification(bool includeDviMag) const
{
    double rval = (double)font_header_.s / (double)font_header_.d;
    if (includeDviMag)
	rval *= dvimag_;
    if (verbosity_ > normal)
	cerr << "PkFont::magnification: "
	     << font_header_.s << '/' << font_header_.d
	     << " * " << dvimag_ << " = " << rval << endl;
    return rval;
}

/**
 * Set the list of directories in which to look for fonts.
 * @param fp the colon-separated font path
 */
void PkFont::setFontSearchPath(string fp) 
{
    if (fp.length() == 0)
	// simply enable this
	fontSearchPath_ = fp;
    setFontSearchPath(true);
}
/**
 * Set the list of directories in which to look for fonts.
 * @param fp the colon-separated font path
 */
void PkFont::setFontSearchPath(char *fp) 
{
    string s = (fp == 0 ? "" : fp);
    setFontSearchPath(s);
}
/**
 * Enable or disable using the font-path when searching for fonts.
 * @param usePath if true, use the font path
 * @see #setFontSearchPath(string)
 */
void PkFont::setFontSearchPath(bool usePath)
{
    setFontSearchStrategy_(fontSearchStrategyPath_, usePath);
}

/**
 * Set the shell command which is used when searching for fonts.  The
 * specified string should be a template string with the formatting
 * characters managed by {@link #substitute_font_string}, and these
 * will be substituted with the required values before the command is
 * run.
 *
 * @param cmd the shell command template
 */
void PkFont::setFontSearchCommand(string cmd)
{
    // if cmd is zero length, simply enable this
    if (cmd.length() != 0)
	fontSearchCommandTemplate_ = cmd;
    setFontSearchCommand(true);
}
/**
 * Sets the shell command which is used when searching for fonts.  The
 * specified string should be a template string with the formatting
 * characters managed by {@link #substitute_font_string}, and these
 * will be substituted with the required values before the command is
 * run.
 *
 * @param cmd the shell command template
 */
void PkFont::setFontSearchCommand(char* cmd)
{
    string s = (cmd == 0 ? "" : cmd);
    setFontSearchCommand(s);
}
/**
 * Enable or disable using a font-search command when searching for fonts.
 * @param useCommand if true, use the command
 * @see #setFontSearchCommand(string)
 */
void PkFont::setFontSearchCommand(bool useCommand)
{
    setFontSearchStrategy_(fontSearchStrategyCommand_, useCommand);
}
 
/**
 * Enable or disable using the <code>kpathsea</code> library when
 * searching for fonts.
 * @param useKpse if true, use the library
 */
void PkFont::setFontSearchKpse(bool useKpse)
{
    setFontSearchStrategy_(fontSearchStrategyKpse_, useKpse);
}

void PkFont::setFontSearchStrategy_(unsigned int strat, bool useit)
{
    if (useit)
	fontSearchStrategies_ |= strat;
    else
	fontSearchStrategies_ &= ~strat;
}


/**
 * Represents a single glyph in a font.  The parameters here
 * correspond to the parameters of the same names which are read from
 * the PK file.
 *
 * @param cc the character code of this glyph
 * @param tfmwidth the width of the character, in DVI units
 * @param dm the horizontal escapement, in pixels; this is the number
 * of pixels rightwards (towards increasing <em>x</em>) that the
 * reference should move after this glyph is set; the vertical
 * escapement is taken to be zero
 * @param w width of the bitmap in pixels
 * @param h height of the bitmap in pixels
 * @param hoff <em>(hoff,voff)</em> is the position of the glyph
 * reference point, as an offset from the top-left pixel, in units of pixels, and with right and down being positive
 * @param voff see parameter <code>hoff</code>
 * @param rasterdata the raster information for this glyph
 * @param f the font which this glyph belongs to
 */
PkGlyph::PkGlyph(unsigned int cc,
		 unsigned int tfmwidth,
		 unsigned int dm,
		 unsigned int w,
		 unsigned int h,
		 int hoff,
		 int voff,
		 PkRasterdata *rasterdata,
		 PkFont *f) 
    : cc_(cc), w_(w), h_(h),
      hoff_(hoff), voff_(voff), font_(f), rasterdata_(rasterdata),
      longform_(false), bitmap_(0) 
{
    tfmwidth_ = (double)tfmwidth/two20_ * f->designSize();
    dx_ = dm;
    dy_ = 0;
}

/**
 * Represents a single glyph in a font.  The parameters here
 * correspond to the parameters of the same names which are read from
 * the PK file.
 *
 * @param cc the character code of this glyph
 * @param tfmwidth the width of the character, in DVI units
 * @param dx the horizontal escapement, in pixels times
 * @manonly 2**16; @endmanonly
 * @latexonly $2^{16}$; @endlatexonly
 * @htmlonly <em>2<sup>16</sup></em>; @endhtmlonly
 * this defines the number
 * of pixels rightwards (towards increasing <em>x</em>) that the
 * reference should move after this glyph is set
 * @param dy the vertical escapement, in pixels times
 * @manonly 2**16 @endmanonly
 * @latexonly $2^{16}$ @endlatexonly
 * @htmlonly <em>2<sup>16</sup></em> @endhtmlonly
 * @param w width of the bitmap in pixels
 * @param h height of the bitmap in pixels
 * @param hoff <em>(hoff,voff)</em> is the position of the glyph
 * reference point, as an offset from the top-left pixel, in units of pixels, and with right and down being positive
 * @param voff see parameter <code>hoff</code>
 * @param rasterdata the raster information for this glyph
 * @param f the font which this glyph belongs to
 */
PkGlyph::PkGlyph(unsigned int cc,
		 unsigned int tfmwidth,
		 unsigned int dx,
		 unsigned int dy,
		 unsigned int w,
		 unsigned int h,
		 int hoff,
		 int voff,
		 PkRasterdata *rasterdata,
		 PkFont *f)
    : cc_(cc), w_(w), h_(h),
      hoff_(hoff), voff_(voff), font_(f), rasterdata_(rasterdata),
      longform_(true), bitmap_(0)
{
    tfmwidth_ = (double)tfmwidth/(double)two20_ * f->designSize();
    dx_ = static_cast<int>(STD::floor(dx / (double)two16_ + 0.5));
    dy_ = static_cast<int>(STD::floor(dy / (double)two16_ + 0.5));
}

/**
 * Constructs a dummy glyph for a font.
 *
 * @param resolution the resolution which this glyph corresponds to
 * @param f the font which this glyph is a member of
 */
PkGlyph::PkGlyph(int resolution, PkFont *f)
    : font_(f)
{
    tfmwidth_ = f->designSize(); // design size in points
    w_ = h_ = 0;
    bitmap_ = 0;
    // make dx_ = 1 quad in pixels (rather large)
    dx_ = static_cast<int>(f->designSize() * resolution / 72.27);
    dy_ = 0;
    hoff_ = voff_ = 0;
}

/**
 * Returns the bitmap which represents this glyph.  This runs from
 * the top-left of the character, with the width and height as given
 * by methods <code>w()</code> and <code>h()</code>.
 * @return the bitmap for this glyph
 */
const Byte *PkGlyph::bitmap()
{
    if (bitmap_ == 0 && w_ != 0 && h_ != 0)
	bitmap_ = rasterdata_->bitmap();
    return bitmap_;
}

/**
 * Creates a Rasterdata object representing the provided data.  The
 * raster data read from the PK file needs to be decoded into a
 * bitmap, and this is the function of this class.
 *
 * @param opcode the character code of this glyph
 * @param rasterdata the rastered glyph, in the format described in
 * the PK file documentation
 * @param len the number of bytes in the rasterdata stream
 * @param w the width of the resulting bitmap
 * @param h the number of rows in the resulting bitmap
 */
PkRasterdata::PkRasterdata(Byte opcode,
			   const Byte *rasterdata, unsigned int len,
			   unsigned int w, unsigned int h)
    : len_(len), w_(w), h_(h),
      bitmap_(0), highnybble_(false)
{
    dyn_f_ = static_cast<Byte>(opcode >> 4);
    start_black_ = opcode&8;
    rasterdata_ = new Byte[len];
    (void) STD::memcpy ((void*)rasterdata_, (void*)rasterdata, len);
    eob_ = rasterdata_+len_;
}

/**
 * Unpacks a PK number.  Numbers in the PK file are stored in a
 * compressed form.  This method consumes as many nybbles as necessary
 * from the input stream and converts them to an unpacked number.
 *
 * @return the unsigned integer corresponding to the next number read
 * from the input stream
 */
unsigned int PkRasterdata::unpackpk ()
{
    unsigned int res = 0;
    
    Byte n = nybble();
    if (n == 0)
    {
	// it's a 'large' number
	int nzero = 1;
	while ((n = nybble()) == 0)
	    nzero++;
	res = n;
	for (; nzero>0; nzero--)
	    res = res*16 + nybble();
	res = res + (13-dyn_f_)*16 + dyn_f_ - 15;
    }
    else if (n <= dyn_f_)
	res = n;
    else if (n < 14)
	res = (n-dyn_f_-1)*16+(dyn_f_+1) + nybble();
    else
    {
	// it's a repeatcount
	if (repeatcount_ != 0)
	    throw DviError ("found double repeatcount in unpackpk");
	if (n == 15)
	    repeatcount_ = 1;
	else
	    repeatcount_ = unpackpk ();
	res = unpackpk();	// get the following runcount
    }
    if (verbosity_ > debug)
	cerr << '=' << res
	     << ' ' << static_cast<int>(repeatcount_) << endl;
    return res;
}

/**
 * Returns the next nybble from the input stream
 * @return the value of the next nybble [0..15], as a Byte
 */
Byte PkRasterdata::nybble() {
    highnybble_ = !highnybble_;
    Byte res;
    if (highnybble_)
    {
	if (rasterdata_ == eob_)
	    throw DviBug ("Run out of nybbles (snackattack!)");
	res = static_cast<Byte>((*rasterdata_)>>4);
    }
    else
	res = static_cast<Byte>((*rasterdata_++)&0xf);
    if (verbosity_ > debug)
	cerr << '<' << static_cast<int>(res) << endl;
    return res;
}

/**
 * Constructs a bitmap from the provided rasterinfo, which has come from
 * the PK file.  Place the resulting bitmap in <code>bitmap_</code>
 */
void PkRasterdata::construct_bitmap()
{
    bitmap_ = new Byte[w_ * h_];

    if (dyn_f_ == 14)
    {
	// rasterinfo is a pure bitmap - no decoding necessary
	unsigned int nbits_req = w_*h_;
	Byte *p = bitmap_;		// build this up...
	const Byte *r = rasterdata_;	// ...from here
	Byte b;

	while (nbits_req >= 8)
	{
	    for (int i=7, bb=*r; i>=0; i--, bb>>=1)
		p[i] = static_cast<Byte>(bb&1);
	    p += 8;
	    r++;
	    nbits_req -= 8;
	}
	if (nbits_req > 0)
	{
	    // get the last few bits
	    b = static_cast<Byte>(*r >> (8-nbits_req));
	    for (int i=nbits_req-1; i>=0; i--, b>>=1)
		p[i] = static_cast<Byte>(b&1);
	}
    }
    else
    {
	// decode the rasterinfo

	Byte *rowp = bitmap_;
	Byte *rowstart = bitmap_;
	unsigned int nrow = 0;
	unsigned int ncol = 0;
	Byte pixelcolour = start_black_;

	repeatcount_ = 0;
	if (verbosity_ > debug)
	{
	    cerr << "dyn_f=" << static_cast<int>(dyn_f_)
		 << " h=" << h_
		 << " w=" << w_
		 << endl << "rasterdata=" << STD::hex;
	    for (const Byte *p=rasterdata_; p<eob_; p++)
		cerr << static_cast<int>(*p) << ' ';
	    cerr << STD::dec << endl;
	}

	while (nrow < h_)
	{
	    unsigned int runcount = unpackpk();
	    for (; runcount>0; runcount--)
	    {
		*rowp++ = pixelcolour;
		ncol++;
		if (ncol == w_)	// end of row
		{
		    ncol = 0;
		    nrow++;
		    if (verbosity_ > debug)
		    {
			cerr << nrow << ':';
			for (const Byte *p=rowstart; p<rowp; p++)
			    cerr << (*p ? '*' : '.');
			cerr << '+' << repeatcount_ << endl;
		    }
		    if (repeatcount_ > 0)
		    {
			Byte *rowend=rowp;
			for (; repeatcount_>0; repeatcount_--)
			{
			    Byte *pt=rowstart;
			    while (pt < rowend)
				*rowp++ = *pt++;
			    nrow++;
			}
		    }
		    rowstart = rowp;
		}
	    }
	    pixelcolour = static_cast<Byte>(pixelcolour==0 ? 1 : 0);
	}
    }
}


/**
 * Utility function: break path at colons, and return list.
 * @param path a colon-separated string
 * @return a List of strings
 */
string_list break_path (string path)
{
    string_list l;
    string tmp = "";
    for (unsigned int i=0; i<path.length(); i++)
	if (path[i] == SRCHPATH_SEP)
	{
	    l.push_back(tmp);
	    tmp = "";
	}
	else
	    tmp += path[i];
    l.push_back(tmp);
    return l;
}

/**
 * Enables or disables font generation.  Font generation will only be
 * enabled if there is a font-generation template command, either set
 * through method <code>setFontgenCommand</code>, or as a compiled-in
 * default.  If there is no such command, this method will have no effect.
 *
 * @param doit if true, font generation is enabled
 */
void PkFont::setFontgen(bool doit)
{
    if (fontgenCommandTemplate_.length() == 0)
	makeMissingFonts_ = false;
    else
	makeMissingFonts_ = doit;
}
/**
 * Set the shell command which is used when generating fonts.  The
 * specified string should be a template string with the formatting
 * characters managed by {@link #substitute_font_string}, and these
 * will be substituted with the required values before the command is
 * run.
 *
 * <p>This also enables font-generation, so that it has the effect of
 * <code>setFontgen(true)</code>.
 *
 * @param cmd a font-generation template
 */
void PkFont::setFontgenCommand(string cmd)
{
    fontgenCommandTemplate_ = cmd;
    makeMissingFonts_ = true;
}

/**
 * Returns a command which can be used to generate fonts.  The command
 * should return a single line containing the path to the generated
 * font file.  Return an empty string on errors, or if such a command
 * is not supported on a particular platform.
 *
 * <p>Note that if a font-generation
 * command template is defined but automatic font generation is
 * disabled, this still returns a font-generation command.
 *
 * @return a font generation command, suitable for passing to a shell
 */
string PkFont::fontgenCommand (void)
    const
{
    string rval;

    if (fontgenCommandTemplate_.length() > 0) {
	try
	{
	    rval = substitute_font_string (fontgenCommandTemplate_,
					   missingFontMode_, name_,
					   dpiScaled(), dpiBase(),
					   magnification());
	    if (verbosity_ > normal)
		cerr << "PkFont:font_gen_string=" << rval << endl;
	}
	catch (PkError& e)
	{
	    if (verbosity_ > quiet)
		cerr << "Warning: can't generate fontgen command ("
		     << e.problem() << ")" << endl;
	    rval = "";
	}
    } else {
	rval = "";
    }

    return rval;
}