2
/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
3
Free Software Foundation, Inc.
4
Written by James Clark (jjc@jclark.com)
6
This file is part of groff.
8
groff is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 2, or (at your option) any later
13
groff is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18
You should have received a copy of the GNU General Public License along
19
with groff; see the file COPYING. If not, write to the Free Software
20
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34
const char *const WS = " \t\n\r";
36
struct font_char_metric {
43
int italic_correction;
44
int subscript_correction;
45
char *special_device_coding;
48
struct font_kern_list {
54
font_kern_list(int, int, int, font_kern_list * = 0);
57
struct font_widths_cache {
58
font_widths_cache *next;
62
font_widths_cache(int, int, font_widths_cache * = 0);
75
text_file(FILE *fp, char *p);
78
void error(const char *format,
79
const errarg &arg1 = empty_errarg,
80
const errarg &arg2 = empty_errarg,
81
const errarg &arg3 = empty_errarg);
84
text_file::text_file(FILE *p, char *s)
85
: fp(p), path(s), lineno(0), size(0), skip_comments(1), buf(0)
89
text_file::~text_file()
103
buf = new char [128];
112
if (illegal_input_char(c))
113
error("illegal input character code `%1'", int(c));
117
buf = new char[size*2];
118
memcpy(buf, old_buf, size);
132
while (csspace(*ptr))
134
if (*ptr != 0 && (!skip_comments || *ptr != '#'))
140
void text_file::error(const char *format,
145
error_with_file_and_line(path, lineno, format, arg1, arg2, arg3);
151
font::font(const char *s)
152
: ligatures(0), kern_hash_table(0), space_width(0), ch_index(0), nindices(0),
153
ch(0), ch_used(0), ch_size(0), special(0), widths_cache(0)
155
name = new char[strlen(s) + 1];
159
// load(); // for testing
166
if (kern_hash_table) {
167
for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++) {
168
font_kern_list *kerns = kern_hash_table[i];
170
font_kern_list *tem = kerns;
175
a_delete kern_hash_table;
178
a_delete internalname;
179
while (widths_cache) {
180
font_widths_cache *tem = widths_cache;
181
widths_cache = widths_cache->next;
186
static int scale_round(int n, int x, int y)
188
assert(x >= 0 && y > 0);
193
if (n <= (INT_MAX - y2)/x)
195
return int(n*double(x)/double(y) + .5);
198
if (-(unsigned)n <= (-(unsigned)INT_MIN - y2)/x)
200
return int(n*double(x)/double(y) - .5);
204
inline int font::scale(int w, int sz)
206
return sz == unitwidth ? w : scale_round(w, sz, unitwidth);
209
int font::get_skew(int c, int point_size, int sl)
211
int h = get_height(c, point_size);
212
return int(h*tan((slant+sl)*PI/180.0) + .5);
215
int font::contains(int c)
217
return c >= 0 && c < nindices && ch_index[c] >= 0;
220
int font::is_special()
225
font_widths_cache::font_widths_cache(int ps, int ch_size,
226
font_widths_cache *p)
227
: next(p), point_size(ps)
229
width = new int[ch_size];
230
for (int i = 0; i < ch_size; i++)
234
font_widths_cache::~font_widths_cache()
239
int font::get_width(int c, int point_size)
241
assert(c >= 0 && c < nindices);
245
if (point_size == unitwidth)
249
widths_cache = new font_widths_cache(point_size, ch_size);
250
else if (widths_cache->point_size != point_size) {
251
font_widths_cache **p;
252
for (p = &widths_cache; *p; p = &(*p)->next)
253
if ((*p)->point_size == point_size)
256
font_widths_cache *tem = *p;
258
tem->next = widths_cache;
262
widths_cache = new font_widths_cache(point_size, ch_size, widths_cache);
264
int &w = widths_cache->width[i];
266
w = scale(ch[i].width, point_size);
270
int font::get_height(int c, int point_size)
272
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
273
return scale(ch[ch_index[c]].height, point_size);
276
int font::get_depth(int c, int point_size)
278
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
279
return scale(ch[ch_index[c]].depth, point_size);
282
int font::get_italic_correction(int c, int point_size)
284
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
285
return scale(ch[ch_index[c]].italic_correction, point_size);
288
int font::get_left_italic_correction(int c, int point_size)
290
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
291
return scale(ch[ch_index[c]].pre_math_space, point_size);
294
int font::get_subscript_correction(int c, int point_size)
296
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
297
return scale(ch[ch_index[c]].subscript_correction, point_size);
300
int font::get_space_width(int point_size)
302
return scale(space_width, point_size);
305
font_kern_list::font_kern_list(int c1, int c2, int n, font_kern_list *p)
306
: i1(c1), i2(c2), amount(n), next(p)
310
inline int font::hash_kern(int i1, int i2)
312
int n = ((i1 << 10) + i2) % KERN_HASH_TABLE_SIZE;
313
return n < 0 ? -n : n;
316
void font::add_kern(int i1, int i2, int amount)
318
if (!kern_hash_table) {
319
kern_hash_table = new font_kern_list *[KERN_HASH_TABLE_SIZE];
320
for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++)
321
kern_hash_table[i] = 0;
323
font_kern_list **p = kern_hash_table + hash_kern(i1, i2);
324
*p = new font_kern_list(i1, i2, amount, *p);
327
int font::get_kern(int i1, int i2, int point_size)
329
if (kern_hash_table) {
330
for (font_kern_list *p = kern_hash_table[hash_kern(i1, i2)]; p; p = p->next)
331
if (i1 == p->i1 && i2 == p->i2)
332
return scale(p->amount, point_size);
337
int font::has_ligature(int mask)
339
return mask & ligatures;
342
int font::get_character_type(int c)
344
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
345
return ch[ch_index[c]].type;
348
int font::get_code(int c)
350
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
351
return ch[ch_index[c]].code;
354
const char *font::get_name()
359
const char *font::get_internal_name()
364
const char *font::get_special_device_encoding(int c)
366
assert(c >= 0 && c < nindices && ch_index[c] >= 0);
367
return( ch[ch_index[c]].special_device_coding );
370
void font::alloc_ch_index(int index)
374
if (index >= nindices)
375
nindices = index + 10;
376
ch_index = new short[nindices];
377
for (int i = 0; i < nindices; i++)
381
int old_nindices = nindices;
383
if (index >= nindices)
384
nindices = index + 10;
385
short *old_ch_index = ch_index;
386
ch_index = new short[nindices];
387
memcpy(ch_index, old_ch_index, sizeof(short)*old_nindices);
388
for (int i = old_nindices; i < nindices; i++)
390
a_delete old_ch_index;
394
void font::extend_ch()
397
ch = new font_char_metric[ch_size = 16];
399
int old_ch_size = ch_size;
401
font_char_metric *old_ch = ch;
402
ch = new font_char_metric[ch_size];
403
memcpy(ch, old_ch, old_ch_size*sizeof(font_char_metric));
411
for (i = nindices - 1; i >= 0; i--)
412
if (ch_index[i] >= 0)
416
short *old_ch_index = ch_index;
417
ch_index = new short[i];
418
memcpy(ch_index, old_ch_index, i*sizeof(short));
419
a_delete old_ch_index;
422
if (ch_used < ch_size) {
423
font_char_metric *old_ch = ch;
424
ch = new font_char_metric[ch_used];
425
memcpy(ch, old_ch, ch_used*sizeof(font_char_metric));
431
void font::add_entry(int index, const font_char_metric &metric)
434
if (index >= nindices)
435
alloc_ch_index(index);
436
assert(index < nindices);
437
if (ch_used + 1 >= ch_size)
439
assert(ch_used + 1 < ch_size);
440
ch_index[index] = ch_used;
441
ch[ch_used++] = metric;
444
void font::copy_entry(int new_index, int old_index)
446
assert(new_index >= 0 && old_index >= 0 && old_index < nindices);
447
if (new_index >= nindices)
448
alloc_ch_index(new_index);
449
ch_index[new_index] = ch_index[old_index];
452
font *font::load_font(const char *s, int *not_found)
454
font *f = new font(s);
455
if (!f->load(not_found)) {
462
static char *trim_arg(char *p)
468
char *q = strchr(p, '\0');
469
while (q > p && csspace(q[-1]))
475
// If the font can't be found, then if not_found is non-NULL, it will be set
476
// to 1 otherwise a message will be printed.
478
int font::load(int *not_found)
482
if ((fp = open_file(name, &path)) == NULL) {
486
error("can't find font file `%1'", name);
489
text_file t(fp, path);
494
t.error("missing charset command");
497
p = strtok(t.buf, WS);
498
if (strcmp(p, "name") == 0) {
500
else if (strcmp(p, "spacewidth") == 0) {
503
if (p == 0 || sscanf(p, "%d", &n) != 1 || n <= 0) {
504
t.error("bad argument for spacewidth command");
509
else if (strcmp(p, "slant") == 0) {
512
if (p == 0 || sscanf(p, "%lf", &n) != 1 || n >= 90.0 || n <= -90.0) {
513
t.error("bad argument for slant command", p);
518
else if (strcmp(p, "ligatures") == 0) {
521
if (p == 0 || strcmp(p, "0") == 0)
523
if (strcmp(p, "ff") == 0)
525
else if (strcmp(p, "fi") == 0)
527
else if (strcmp(p, "fl") == 0)
529
else if (strcmp(p, "ffi") == 0)
530
ligatures |= LIG_ffi;
531
else if (strcmp(p, "ffl") == 0)
532
ligatures |= LIG_ffl;
534
t.error("unrecognised ligature `%1'", p);
539
else if (strcmp(p, "internalname") == 0) {
542
t.error("`internalname command requires argument");
545
internalname = new char[strlen(p) + 1];
546
strcpy(internalname, p);
548
else if (strcmp(p, "special") == 0) {
551
else if (strcmp(p, "kernpairs") != 0 && strcmp(p, "charset") != 0) {
554
handle_unknown_font_command(command, trim_arg(p), t.path, t.lineno);
563
if (strcmp(command, "kernpairs") == 0) {
569
char *c1 = strtok(t.buf, WS);
572
char *c2 = strtok(0, WS);
579
t.error("missing kern amount");
583
if (sscanf(p, "%d", &n) != 1) {
584
t.error("bad kern amount `%1'", p);
587
int i1 = name_to_index(c1);
589
t.error("illegal character `%1'", c1);
592
int i2 = name_to_index(c2);
594
t.error("illegal character `%1'", c2);
600
else if (strcmp(command, "charset") == 0) {
608
char *nm = strtok(t.buf, WS);
610
continue; // I dont think this should happen
617
if (last_index == -1) {
618
t.error("first charset entry is duplicate");
621
if (strcmp(nm, "---") == 0) {
622
t.error("unnamed character cannot be duplicate");
625
int index = name_to_index(nm);
627
t.error("illegal character `%1'", nm);
630
copy_entry(index, last_index);
633
font_char_metric metric;
636
metric.pre_math_space = 0;
637
metric.italic_correction = 0;
638
metric.subscript_correction = 0;
639
int nparms = sscanf(p, "%d,%d,%d,%d,%d,%d",
640
&metric.width, &metric.height, &metric.depth,
641
&metric.italic_correction,
642
&metric.pre_math_space,
643
&metric.subscript_correction);
645
t.error("bad width for `%1'", nm);
650
t.error("missing character type for `%1'", nm);
654
if (sscanf(p, "%d", &type) != 1) {
655
t.error("bad character type for `%1'", nm);
658
if (type < 0 || type > 255) {
659
t.error("character code `%1' out of range", type);
665
t.error("missing code for `%1'", nm);
669
metric.code = (int)strtol(p, &ptr, 0);
670
if (metric.code == 0 && ptr == p) {
671
t.error("bad code `%1' for character `%2'", p, nm);
676
if ((p == NULL) || (strcmp(p, "--") == 0)) {
677
metric.special_device_coding = NULL;
679
char *name=(char *)malloc(strlen(p)+1);
682
fatal("malloc failed while reading character encoding");
685
metric.special_device_coding = name;
688
if (strcmp(nm, "---") == 0) {
689
last_index = number_to_index(metric.code);
690
add_entry(last_index, metric);
693
last_index = name_to_index(nm);
694
if (last_index < 0) {
695
t.error("illegal character `%1'", nm);
698
add_entry(last_index, metric);
699
copy_entry(number_to_index(metric.code), last_index);
703
if (last_index == -1) {
704
t.error("I didn't seem to find any characters");
709
t.error("unrecognised command `%1' after `kernpairs' or `charset' command", command);
714
t.error("missing charset command");
717
if (space_width == 0)
718
space_width = scale_round(unitwidth, res, 72*3*sizescale);
727
{ "res", &font::res },
728
{ "hor", &font::hor },
729
{ "vert", &font::vert },
730
{ "unitwidth", &font::unitwidth },
731
{ "paperwidth", &font::paperwidth },
732
{ "paperlength", &font::paperlength },
733
{ "spare1", &font::biggestfont },
734
{ "biggestfont", &font::biggestfont },
735
{ "spare2", &font::spare2 },
736
{ "sizescale", &font::sizescale }
740
int font::load_desc()
745
if ((fp = open_file("DESC", &path)) == 0) {
746
error("can't find `DESC' file");
749
text_file t(fp, path);
753
char *p = strtok(t.buf, WS);
756
for (idx = 0; !found && idx < sizeof(table)/sizeof(table[0]); idx++)
757
if (strcmp(table[idx].command, p) == 0)
760
char *q = strtok(0, WS);
762
t.error("missing value for command `%1'", p);
765
//int *ptr = &(this->*(table[idx-1].ptr));
766
int *ptr = table[idx-1].ptr;
767
if (sscanf(q, "%d", ptr) != 1) {
768
t.error("bad number `%1'", q);
772
else if (strcmp("tcommand", p) == 0) {
775
else if (strcmp("pass_filenames", p) == 0) {
778
else if (strcmp("use_charnames_in_special", p) == 0) {
779
use_charnames_in_special = 1;
781
else if (strcmp("family", p) == 0) {
784
t.error("family command requires an argument");
787
char *tem = new char[strlen(p)+1];
791
else if (strcmp("fonts", p) == 0) {
793
if (!p || sscanf(p, "%d", &nfonts) != 1 || nfonts <= 0) {
794
t.error("bad number of fonts `%1'", p);
797
font_name_table = (const char **)new char *[nfonts+1];
798
for (int i = 0; i < nfonts; i++) {
802
t.error("end of file while reading list of fonts");
805
p = strtok(t.buf, WS);
807
char *temp = new char[strlen(p)+1];
809
font_name_table[i] = temp;
813
t.error("font count does not match number of fonts");
816
font_name_table[nfonts] = 0;
818
else if (strcmp("sizes", p) == 0) {
826
t.error("list of sizes must be terminated by `0'");
829
p = strtok(t.buf, WS);
832
switch (sscanf(p, "%d-%d", &lower, &upper)) {
837
if (lower <= upper && lower >= 0)
841
t.error("bad size range `%1'", p);
845
int *old_sizes = sizes;
846
sizes = new int[n*2];
847
memcpy(sizes, old_sizes, n*sizeof(int));
857
t.error("must have some sizes");
861
else if (strcmp("styles", p) == 0) {
862
int style_table_size = 5;
863
style_table = (const char **)new char *[style_table_size];
865
for (j = 0; j < style_table_size; j++)
872
// leave room for terminating 0
873
if (i + 1 >= style_table_size) {
874
const char **old_style_table = style_table;
875
style_table_size *= 2;
876
style_table = (const char **)new char*[style_table_size];
877
for (j = 0; j < i; j++)
878
style_table[j] = old_style_table[j];
879
for (; j < style_table_size; j++)
881
a_delete old_style_table;
883
char *tem = new char[strlen(p) + 1];
885
style_table[i++] = tem;
888
else if (strcmp("charset", p) == 0)
890
else if (unknown_desc_command_handler) {
893
(*unknown_desc_command_handler)(command, trim_arg(p), t.path, t.lineno);
897
t.error("missing `res' command");
900
if (unitwidth == 0) {
901
t.error("missing `unitwidth' command");
904
if (font_name_table == 0) {
905
t.error("missing `fonts' command");
909
t.error("missing `sizes' command");
913
t.error("bad `sizescale' value");
917
t.error("bad `hor' value");
921
t.error("bad `vert' value");
927
void font::handle_unknown_font_command(const char *, const char *,
933
font::set_unknown_desc_command_handler(FONT_COMMAND_HANDLER func)
935
FONT_COMMAND_HANDLER prev = unknown_desc_command_handler;
936
unknown_desc_command_handler = func;