2 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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(glyph *, glyph *, 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);
76 text_file(FILE *fp, char *p);
79 void error(const char *format,
80 const errarg &arg1 = empty_errarg,
81 const errarg &arg2 = empty_errarg,
82 const errarg &arg3 = empty_errarg);
85 text_file::text_file(FILE *p, char *s)
86 : fp(p), path(s), lineno(0), size(0), skip_comments(1), silent(0), buf(0)
90 text_file::~text_file()
112 if (invalid_input_char(c))
113 error("invalid 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,
146 error_with_file_and_line(path, lineno, format, arg1, arg2, arg3);
149 int glyph_to_unicode(glyph *g)
151 const char *nm = glyph_to_name(g);
154 if (nm[0] == 'c' && nm[1] == 'h' && nm[2] == 'a' && nm[3] == 'r'
155 && (nm[4] >= '0' && nm[4] <= '9')) {
156 int n = (nm[4] - '0');
159 if (n > 0 && (nm[5] >= '0' && nm[5] <= '9')) {
160 n = 10*n + (nm[5] - '0');
163 if (nm[6] >= '0' && nm[6] <= '9') {
164 n = 10*n + (nm[6] - '0');
165 if (nm[7] == '\0' && n < 128)
170 // Unicode character?
171 if (check_unicode_name(nm)) {
173 return (int)strtol(nm + 1, &ignore, 16);
175 // If `nm' is a single letter `x', the glyph name is `\x'.
176 char buf[] = { '\\', '\0', '\0' };
181 // groff glyphs that map to Unicode?
182 const char *unicode = glyph_name_to_unicode(nm);
183 if (unicode != NULL && strchr(unicode, '_') == NULL) {
185 return (int)strtol(unicode, &ignore, 16);
193 font::font(const char *s)
194 : ligatures(0), kern_hash_table(0), space_width(0), special(0),
195 ch_index(0), nindices(0), ch(0), ch_used(0), ch_size(0), widths_cache(0)
197 name = new char[strlen(s) + 1];
202 // load(); // for testing
207 for (int i = 0; i < ch_used; i++)
208 if (ch[i].special_device_coding)
209 a_delete ch[i].special_device_coding;
212 if (kern_hash_table) {
213 for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++) {
214 font_kern_list *kerns = kern_hash_table[i];
216 font_kern_list *tem = kerns;
221 a_delete kern_hash_table;
224 a_delete internalname;
225 while (widths_cache) {
226 font_widths_cache *tem = widths_cache;
227 widths_cache = widths_cache->next;
232 static int scale_round(int n, int x, int y)
234 assert(x >= 0 && y > 0);
239 if (n <= (INT_MAX - y2) / x)
240 return (n * x + y2) / y;
241 return int(n * double(x) / double(y) + .5);
244 if (-(unsigned)n <= (-(unsigned)INT_MIN - y2) / x)
245 return (n * x - y2) / y;
246 return int(n * double(x) / double(y) - .5);
250 static int scale_round(int n, int x, int y, int z)
252 assert(x >= 0 && y > 0 && z > 0);
256 return int((n * double(x) / double(y)) * (double(z) / 1000.0) + .5);
258 return int((n * double(x) / double(y)) * (double(z) / 1000.0) - .5);
261 inline int font::scale(int w, int sz)
264 return scale_round(w, sz, unitwidth, zoom);
266 return sz == unitwidth ? w : scale_round(w, sz, unitwidth);
269 int font::unit_scale(double *value, char unit)
271 // we scale everything to inch
297 int font::get_skew(glyph *g, int point_size, int sl)
299 int h = get_height(g, point_size);
300 return int(h * tan((slant + sl) * PI / 180.0) + .5);
303 int font::contains(glyph *g)
305 int idx = glyph_to_index(g);
307 // Explicitly enumerated glyph?
308 if (idx < nindices && ch_index[idx] >= 0)
312 // ASCII or Unicode character, or groff glyph name that maps to Unicode?
313 if (glyph_to_unicode(g) >= 0)
315 // Numbered character?
316 if (glyph_to_number(g) >= 0)
322 int font::is_special()
327 font_widths_cache::font_widths_cache(int ps, int ch_size,
328 font_widths_cache *p)
329 : next(p), point_size(ps)
331 width = new int[ch_size];
332 for (int i = 0; i < ch_size; i++)
336 font_widths_cache::~font_widths_cache()
341 int font::get_width(glyph *g, int point_size)
343 int idx = glyph_to_index(g);
347 real_size = point_size;
350 if (point_size <= (INT_MAX - 500) / zoom)
351 real_size = (point_size * zoom + 500) / 1000;
353 real_size = int(point_size * double(zoom) / 1000.0 + .5);
355 if (idx < nindices && ch_index[idx] >= 0) {
356 // Explicitly enumerated glyph
357 int i = ch_index[idx];
358 if (real_size == unitwidth || font::unscaled_charwidths)
362 widths_cache = new font_widths_cache(real_size, ch_size);
363 else if (widths_cache->point_size != real_size) {
364 font_widths_cache **p;
365 for (p = &widths_cache; *p; p = &(*p)->next)
366 if ((*p)->point_size == real_size)
369 font_widths_cache *tem = *p;
371 tem->next = widths_cache;
375 widths_cache = new font_widths_cache(real_size, ch_size,
378 int &w = widths_cache->width[i];
380 w = scale(ch[i].width, point_size);
385 int width = 24; // XXX: Add a request to override this.
386 int w = wcwidth(get_code(g));
389 if (real_size == unitwidth || font::unscaled_charwidths)
392 return scale(width, point_size);
397 int font::get_height(glyph *g, int point_size)
399 int idx = glyph_to_index(g);
401 if (idx < nindices && ch_index[idx] >= 0) {
402 // Explicitly enumerated glyph
403 return scale(ch[ch_index[idx]].height, point_size);
412 int font::get_depth(glyph *g, int point_size)
414 int idx = glyph_to_index(g);
416 if (idx < nindices && ch_index[idx] >= 0) {
417 // Explicitly enumerated glyph
418 return scale(ch[ch_index[idx]].depth, point_size);
427 int font::get_italic_correction(glyph *g, int point_size)
429 int idx = glyph_to_index(g);
431 if (idx < nindices && ch_index[idx] >= 0) {
432 // Explicitly enumerated glyph
433 return scale(ch[ch_index[idx]].italic_correction, point_size);
442 int font::get_left_italic_correction(glyph *g, int point_size)
444 int idx = glyph_to_index(g);
446 if (idx < nindices && ch_index[idx] >= 0) {
447 // Explicitly enumerated glyph
448 return scale(ch[ch_index[idx]].pre_math_space, point_size);
457 int font::get_subscript_correction(glyph *g, int point_size)
459 int idx = glyph_to_index(g);
461 if (idx < nindices && ch_index[idx] >= 0) {
462 // Explicitly enumerated glyph
463 return scale(ch[ch_index[idx]].subscript_correction, point_size);
472 void font::set_zoom(int factor)
486 int font::get_space_width(int point_size)
488 return scale(space_width, point_size);
491 font_kern_list::font_kern_list(glyph *g1, glyph *g2, int n, font_kern_list *p)
492 : glyph1(g1), glyph2(g2), amount(n), next(p)
496 inline int font::hash_kern(glyph *g1, glyph *g2)
498 int n = ((glyph_to_index(g1) << 10) + glyph_to_index(g2))
499 % KERN_HASH_TABLE_SIZE;
500 return n < 0 ? -n : n;
503 void font::add_kern(glyph *g1, glyph *g2, int amount)
505 if (!kern_hash_table) {
506 kern_hash_table = new font_kern_list *[int(KERN_HASH_TABLE_SIZE)];
507 for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++)
508 kern_hash_table[i] = 0;
510 font_kern_list **p = kern_hash_table + hash_kern(g1, g2);
511 *p = new font_kern_list(g1, g2, amount, *p);
514 int font::get_kern(glyph *g1, glyph *g2, int point_size)
516 if (kern_hash_table) {
517 for (font_kern_list *p = kern_hash_table[hash_kern(g1, g2)]; p;
519 if (g1 == p->glyph1 && g2 == p->glyph2)
520 return scale(p->amount, point_size);
525 int font::has_ligature(int mask)
527 return mask & ligatures;
530 int font::get_character_type(glyph *g)
532 int idx = glyph_to_index(g);
534 if (idx < nindices && ch_index[idx] >= 0) {
535 // Explicitly enumerated glyph
536 return ch[ch_index[idx]].type;
545 int font::get_code(glyph *g)
547 int idx = glyph_to_index(g);
549 if (idx < nindices && ch_index[idx] >= 0) {
550 // Explicitly enumerated glyph
551 return ch[ch_index[idx]].code;
555 // ASCII or Unicode character, or groff glyph name that maps to Unicode?
556 int uni = glyph_to_unicode(g);
559 // Numbered character?
560 int n = glyph_to_number(g);
564 // The caller must check `contains(g)' before calling get_code(g).
568 const char *font::get_name()
573 const char *font::get_internal_name()
578 const char *font::get_special_device_encoding(glyph *g)
580 int idx = glyph_to_index(g);
582 if (idx < nindices && ch_index[idx] >= 0) {
583 // Explicitly enumerated glyph
584 return ch[ch_index[idx]].special_device_coding;
593 const char *font::get_image_generator()
595 return image_generator;
598 void font::alloc_ch_index(int idx)
604 ch_index = new int[nindices];
605 for (int i = 0; i < nindices; i++)
609 int old_nindices = nindices;
613 int *old_ch_index = ch_index;
614 ch_index = new int[nindices];
615 memcpy(ch_index, old_ch_index, sizeof(int)*old_nindices);
616 for (int i = old_nindices; i < nindices; i++)
618 a_delete old_ch_index;
622 void font::extend_ch()
625 ch = new font_char_metric[ch_size = 16];
627 int old_ch_size = ch_size;
629 font_char_metric *old_ch = ch;
630 ch = new font_char_metric[ch_size];
631 memcpy(ch, old_ch, old_ch_size*sizeof(font_char_metric));
639 for (i = nindices - 1; i >= 0; i--)
640 if (ch_index[i] >= 0)
644 int *old_ch_index = ch_index;
645 ch_index = new int[i];
646 memcpy(ch_index, old_ch_index, i*sizeof(int));
647 a_delete old_ch_index;
650 if (ch_used < ch_size) {
651 font_char_metric *old_ch = ch;
652 ch = new font_char_metric[ch_used];
653 memcpy(ch, old_ch, ch_used*sizeof(font_char_metric));
659 void font::add_entry(glyph *g, const font_char_metric &metric)
661 int idx = glyph_to_index(g);
665 assert(idx < nindices);
666 if (ch_used + 1 >= ch_size)
668 assert(ch_used + 1 < ch_size);
669 ch_index[idx] = ch_used;
670 ch[ch_used++] = metric;
673 void font::copy_entry(glyph *new_glyph, glyph *old_glyph)
675 int new_index = glyph_to_index(new_glyph);
676 int old_index = glyph_to_index(old_glyph);
677 assert(new_index >= 0 && old_index >= 0 && old_index < nindices);
678 if (new_index >= nindices)
679 alloc_ch_index(new_index);
680 ch_index[new_index] = ch_index[old_index];
683 font *font::load_font(const char *s, int *not_found, int head_only)
685 font *f = new font(s);
686 if (!f->load(not_found, head_only)) {
693 static char *trim_arg(char *p)
699 char *q = strchr(p, '\0');
700 while (q > p && csspace(q[-1]))
706 int font::scan_papersize(const char *p,
707 const char **size, double *length, double *width)
716 if (sscanf(pp, "%lf%1[ipPc],%lf%1[ipPc]", &l, lu, &w, wu) == 4
718 && unit_scale(&l, lu[0]) && unit_scale(&w, wu[0])) {
730 for (i = 0; i < NUM_PAPERSIZES; i++)
731 if (strcasecmp(papersizes[i].name, pp) == 0) {
733 *length = papersizes[i].length;
735 *width = papersizes[i].width;
737 *size = papersizes[i].name;
741 FILE *f = fopen(p, "r");
746 char *linep = strchr(line, '\0');
747 // skip final newline, if any
748 if (*(--linep) == '\n')
758 // If the font can't be found, then if not_found is non-NULL, it will be set
759 // to 1 otherwise a message will be printed.
761 int font::load(int *not_found, int head_only)
763 if (strcmp(name, "DESC") == 0) {
767 error("`DESC' is not a valid font file name");
772 if ((fp = open_file(name, &path)) == NULL) {
776 error("can't find font file `%1'", name);
779 text_file t(fp, path);
781 t.silent = head_only;
788 p = strtok(t.buf, WS);
789 if (strcmp(p, "name") == 0) {
791 else if (strcmp(p, "spacewidth") == 0) {
794 if (p == 0 || sscanf(p, "%d", &n) != 1 || n <= 0) {
795 t.error("bad argument for `spacewidth' command");
800 else if (strcmp(p, "slant") == 0) {
803 if (p == 0 || sscanf(p, "%lf", &n) != 1 || n >= 90.0 || n <= -90.0) {
804 t.error("bad argument for `slant' command", p);
809 else if (strcmp(p, "ligatures") == 0) {
812 if (p == 0 || strcmp(p, "0") == 0)
814 if (strcmp(p, "ff") == 0)
816 else if (strcmp(p, "fi") == 0)
818 else if (strcmp(p, "fl") == 0)
820 else if (strcmp(p, "ffi") == 0)
821 ligatures |= LIG_ffi;
822 else if (strcmp(p, "ffl") == 0)
823 ligatures |= LIG_ffl;
825 t.error("unrecognised ligature `%1'", p);
830 else if (strcmp(p, "internalname") == 0) {
833 t.error("`internalname' command requires argument");
836 internalname = new char[strlen(p) + 1];
837 strcpy(internalname, p);
839 else if (strcmp(p, "special") == 0) {
842 else if (strcmp(p, "kernpairs") != 0 && strcmp(p, "charset") != 0) {
845 handle_unknown_font_command(command, trim_arg(p), t.path, t.lineno);
853 t.error("missing charset command");
860 if (strcmp(command, "kernpairs") == 0) {
868 char *c1 = strtok(t.buf, WS);
871 char *c2 = strtok(0, WS);
878 t.error("missing kern amount");
882 if (sscanf(p, "%d", &n) != 1) {
883 t.error("bad kern amount `%1'", p);
886 glyph *g1 = name_to_glyph(c1);
887 glyph *g2 = name_to_glyph(c2);
891 else if (strcmp(command, "charset") == 0) {
895 glyph *last_glyph = NULL;
901 char *nm = strtok(t.buf, WS);
903 continue; // I dont think this should happen
910 if (last_glyph == NULL) {
911 t.error("first charset entry is duplicate");
914 if (strcmp(nm, "---") == 0) {
915 t.error("unnamed character cannot be duplicate");
918 glyph *g = name_to_glyph(nm);
919 copy_entry(g, last_glyph);
922 font_char_metric metric;
925 metric.pre_math_space = 0;
926 metric.italic_correction = 0;
927 metric.subscript_correction = 0;
928 int nparms = sscanf(p, "%d,%d,%d,%d,%d,%d",
929 &metric.width, &metric.height, &metric.depth,
930 &metric.italic_correction,
931 &metric.pre_math_space,
932 &metric.subscript_correction);
934 t.error("bad width for `%1'", nm);
939 t.error("missing character type for `%1'", nm);
943 if (sscanf(p, "%d", &type) != 1) {
944 t.error("bad character type for `%1'", nm);
947 if (type < 0 || type > 255) {
948 t.error("character type `%1' out of range", type);
954 t.error("missing code for `%1'", nm);
958 metric.code = (int)strtol(p, &ptr, 0);
959 if (metric.code == 0 && ptr == p) {
960 t.error("bad code `%1' for character `%2'", p, nm);
964 int w = wcwidth(metric.code);
969 if ((p == NULL) || (strcmp(p, "--") == 0)) {
970 metric.special_device_coding = NULL;
973 char *nam = new char[strlen(p) + 1];
975 metric.special_device_coding = nam;
977 if (strcmp(nm, "---") == 0) {
978 last_glyph = number_to_glyph(metric.code);
979 add_entry(last_glyph, metric);
982 last_glyph = name_to_glyph(nm);
983 add_entry(last_glyph, metric);
984 copy_entry(number_to_glyph(metric.code), last_glyph);
988 if (last_glyph == NULL) {
989 t.error("I didn't seem to find any characters");
994 t.error("unrecognised command `%1' "
995 "after `kernpairs' or `charset' command",
1002 if (!is_unicode && !had_charset) {
1003 t.error("missing `charset' command");
1006 if (space_width == 0) {
1008 space_width = scale_round(unitwidth, res, 72 * 3 * sizescale, zoom);
1010 space_width = scale_round(unitwidth, res, 72 * 3 * sizescale);
1016 const char *command;
1019 { "res", &font::res },
1020 { "hor", &font::hor },
1021 { "vert", &font::vert },
1022 { "unitwidth", &font::unitwidth },
1023 { "paperwidth", &font::paperwidth },
1024 { "paperlength", &font::paperlength },
1025 { "spare1", &font::biggestfont },
1026 { "biggestfont", &font::biggestfont },
1027 { "spare2", &font::spare2 },
1028 { "sizescale", &font::sizescale },
1031 int font::load_desc()
1036 if ((fp = open_file("DESC", &path)) == 0) {
1037 error("can't find `DESC' file");
1040 text_file t(fp, path);
1041 t.skip_comments = 1;
1044 char *p = strtok(t.buf, WS);
1047 for (idx = 0; !found && idx < sizeof(table)/sizeof(table[0]); idx++)
1048 if (strcmp(table[idx].command, p) == 0)
1051 char *q = strtok(0, WS);
1053 t.error("missing value for command `%1'", p);
1056 //int *ptr = &(this->*(table[idx-1].ptr));
1057 int *ptr = table[idx-1].ptr;
1058 if (sscanf(q, "%d", ptr) != 1) {
1059 t.error("bad number `%1'", q);
1063 else if (strcmp("family", p) == 0) {
1066 t.error("family command requires an argument");
1069 char *tem = new char[strlen(p)+1];
1073 else if (strcmp("fonts", p) == 0) {
1075 if (!p || sscanf(p, "%d", &nfonts) != 1 || nfonts <= 0) {
1076 t.error("bad number of fonts `%1'", p);
1079 font_name_table = (const char **)new char *[nfonts+1];
1080 for (int i = 0; i < nfonts; i++) {
1084 t.error("end of file while reading list of fonts");
1087 p = strtok(t.buf, WS);
1089 char *temp = new char[strlen(p)+1];
1091 font_name_table[i] = temp;
1095 t.error("font count does not match number of fonts");
1098 font_name_table[nfonts] = 0;
1100 else if (strcmp("papersize", p) == 0) {
1103 t.error("papersize command requires an argument");
1106 int found_paper = 0;
1108 double unscaled_paperwidth, unscaled_paperlength;
1109 if (scan_papersize(p, &papersize, &unscaled_paperlength,
1110 &unscaled_paperwidth)) {
1111 paperwidth = int(unscaled_paperwidth * res + 0.5);
1112 paperlength = int(unscaled_paperlength * res + 0.5);
1119 t.error("bad paper size");
1123 else if (strcmp("unscaled_charwidths", p) == 0)
1124 unscaled_charwidths = 1;
1125 else if (strcmp("pass_filenames", p) == 0)
1127 else if (strcmp("sizes", p) == 0) {
1135 t.error("list of sizes must be terminated by `0'");
1138 p = strtok(t.buf, WS);
1141 switch (sscanf(p, "%d-%d", &lower, &upper)) {
1146 if (lower <= upper && lower >= 0)
1150 t.error("bad size range `%1'", p);
1154 int *old_sizes = sizes;
1155 sizes = new int[n*2];
1156 memcpy(sizes, old_sizes, n*sizeof(int));
1166 t.error("must have some sizes");
1170 else if (strcmp("styles", p) == 0) {
1171 int style_table_size = 5;
1172 style_table = (const char **)new char *[style_table_size];
1174 for (j = 0; j < style_table_size; j++)
1181 // leave room for terminating 0
1182 if (i + 1 >= style_table_size) {
1183 const char **old_style_table = style_table;
1184 style_table_size *= 2;
1185 style_table = (const char **)new char*[style_table_size];
1186 for (j = 0; j < i; j++)
1187 style_table[j] = old_style_table[j];
1188 for (; j < style_table_size; j++)
1190 a_delete old_style_table;
1192 char *tem = new char[strlen(p) + 1];
1194 style_table[i++] = tem;
1197 else if (strcmp("tcommand", p) == 0)
1199 else if (strcmp("use_charnames_in_special", p) == 0)
1200 use_charnames_in_special = 1;
1201 else if (strcmp("unicode", p) == 0)
1203 else if (strcmp("image_generator", p) == 0) {
1206 t.error("image_generator command requires an argument");
1209 image_generator = strsave(p);
1211 else if (strcmp("charset", p) == 0)
1213 else if (unknown_desc_command_handler) {
1215 p = strtok(0, "\n");
1216 (*unknown_desc_command_handler)(command, trim_arg(p), t.path, t.lineno);
1220 t.error("missing `res' command");
1223 if (unitwidth == 0) {
1224 t.error("missing `unitwidth' command");
1227 if (font_name_table == 0) {
1228 t.error("missing `fonts' command");
1232 t.error("missing `sizes' command");
1235 if (sizescale < 1) {
1236 t.error("bad `sizescale' value");
1240 t.error("bad `hor' value");
1244 t.error("bad `vert' value");
1250 void font::handle_unknown_font_command(const char *, const char *,
1255 FONT_COMMAND_HANDLER
1256 font::set_unknown_desc_command_handler(FONT_COMMAND_HANDLER func)
1258 FONT_COMMAND_HANDLER prev = unknown_desc_command_handler;
1259 unknown_desc_command_handler = func;