2 /* Copyright (C) 1989-2018 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/>. */
20 extern int debug_state;
28 #include "dictionary.h"
30 #include "stringclass.h"
49 #else /* not _POSIX_VERSION */
51 /* traditional Unix */
53 #define WIFEXITED(s) (((s) & 0377) == 0)
54 #define WEXITSTATUS(s) (((s) >> 8) & 0377)
55 #define WTERMSIG(s) ((s) & 0177)
56 #define WIFSTOPPED(s) (((s) & 0377) == 0177)
57 #define WSTOPSIG(s) (((s) >> 8) & 0377)
58 #define WIFSIGNALED(s) (((s) & 0377) != 0 && (((s) & 0377) != 0177))
60 #endif /* not _POSIX_VERSION */
62 // declarations to avoid friend name injections
65 tfont *make_tfont(tfont_spec &);
69 * how many boundaries of images have been written? Useful for
74 static int suppress_start_page = 0;
78 symbol HYPHEN_SYMBOL("hy");
80 // Character used when a hyphen is inserted at a line break.
81 static charinfo *soft_hyphen_char;
83 enum constant_space_type {
85 CONSTANT_SPACE_RELATIVE,
86 CONSTANT_SPACE_ABSOLUTE
89 struct special_font_list {
91 special_font_list *next;
94 special_font_list *global_special_fonts;
95 static int global_ligature_mode = 1;
96 static int global_kern_mode = 1;
98 class track_kerning_function {
105 track_kerning_function();
106 track_kerning_function(units, hunits, units, hunits);
107 int operator==(const track_kerning_function &);
108 int operator!=(const track_kerning_function &);
109 hunits compute(int point_size);
112 // embolden fontno when this is the current font
114 struct conditional_bold {
115 conditional_bold *next;
118 conditional_bold(int, hunits, conditional_bold * = 0);
127 symbol internal_name;
128 symbol external_name;
132 track_kerning_function track_kern;
133 constant_space_type is_constant_spaced;
134 units constant_space;
135 int last_ligature_mode;
137 conditional_bold *cond_bold_list;
140 special_font_list *sf;
141 font_info(symbol, int, symbol, font *);
142 int contains(charinfo *);
143 void set_bold(hunits);
145 void set_conditional_bold(int, hunits);
146 void conditional_unbold(int);
147 void set_track_kern(track_kerning_function &);
148 void set_constant_space(constant_space_type, units = 0);
149 int is_named(symbol);
151 tfont *get_tfont(font_size, int, int, int);
152 hunits get_space_width(font_size, int);
153 hunits get_narrow_space_width(font_size);
154 hunits get_half_narrow_space_width(font_size);
155 int get_bold(hunits *);
160 friend symbol get_font_name(int, environment *);
161 friend symbol get_style_name(int);
171 char is_constant_spaced;
175 hunits track_kern; // add this to the width
176 hunits constant_space_width;
180 tfont_spec(symbol, int, font *, font_size, int, int);
181 tfont_spec(const tfont_spec &spec) { *this = spec; }
183 int operator==(const tfont_spec &);
184 friend tfont *font_info::get_tfont(font_size fs, int, int, int);
187 class tfont : public tfont_spec {
188 static tfont *tfont_list;
190 tfont *plain_version;
193 int contains(charinfo *);
194 hunits get_width(charinfo *c);
195 int get_bold(hunits *);
196 int get_constant_space(hunits *);
197 hunits get_track_kern();
199 font_size get_size();
202 charinfo *get_lig(charinfo *c1, charinfo *c2);
203 int get_kern(charinfo *c1, charinfo *c2, hunits *res);
204 int get_input_position();
205 int get_character_type(charinfo *);
208 vunits get_char_height(charinfo *);
209 vunits get_char_depth(charinfo *);
210 hunits get_char_skew(charinfo *);
211 hunits get_italic_correction(charinfo *);
212 hunits get_left_italic_correction(charinfo *);
213 hunits get_subscript_correction(charinfo *);
214 friend tfont *make_tfont(tfont_spec &);
217 inline int env_definite_font(environment *env)
219 return env->get_family()->make_definite(env->get_font());
222 /* font_info functions */
224 static font_info **font_table = 0;
225 static int font_table_size = 0;
227 font_info::font_info(symbol nm, int n, symbol enm, font *f)
228 : last_tfont(0), number(n), last_size(0),
229 internal_name(nm), external_name(enm), fm(f),
230 is_bold(0), is_constant_spaced(CONSTANT_SPACE_NONE), last_ligature_mode(1),
231 last_kern_mode(1), cond_bold_list(0), sf(0)
235 inline int font_info::contains(charinfo *ci)
237 return fm != 0 && fm->contains(ci->as_glyph());
240 inline int font_info::is_special()
242 return fm != 0 && fm->is_special();
245 inline int font_info::is_style()
250 void font_info::set_zoom(int zoom)
256 inline int font_info::get_zoom()
260 return fm->get_zoom();
263 tfont *make_tfont(tfont_spec &spec)
265 for (tfont *p = tfont::tfont_list; p; p = p->next)
268 return new tfont(spec);
271 int env_get_zoom(environment *env)
273 int fontno = env->get_family()->make_definite(env->get_font());
274 return font_table[fontno]->get_zoom();
277 // this is the current_font, fontno is where we found the character,
278 // presumably a special font
280 tfont *font_info::get_tfont(font_size fs, int height, int slant, int fontno)
282 if (last_tfont == 0 || fs != last_size
283 || height != last_height || slant != last_slant
284 || global_ligature_mode != last_ligature_mode
285 || global_kern_mode != last_kern_mode
286 || fontno != number) {
287 font_info *f = font_table[fontno];
288 tfont_spec spec(f->external_name, f->number, f->fm, fs, height, slant);
289 for (conditional_bold *p = cond_bold_list; p; p = p->next)
290 if (p->fontno == fontno) {
292 spec.bold_offset = p->offset;
295 if (!spec.is_bold && is_bold) {
297 spec.bold_offset = bold_offset;
299 spec.track_kern = track_kern.compute(fs.to_scaled_points());
300 spec.ligature_mode = global_ligature_mode;
301 spec.kern_mode = global_kern_mode;
302 switch (is_constant_spaced) {
303 case CONSTANT_SPACE_NONE:
305 case CONSTANT_SPACE_ABSOLUTE:
306 spec.is_constant_spaced = 1;
307 spec.constant_space_width = constant_space;
309 case CONSTANT_SPACE_RELATIVE:
310 spec.is_constant_spaced = 1;
311 spec.constant_space_width
312 = scale(constant_space*fs.to_scaled_points(),
319 if (fontno != number)
320 return make_tfont(spec);
321 // save font for comparison purposes
322 last_tfont = make_tfont(spec);
323 // save font related values not contained in tfont
325 last_height = height;
327 last_ligature_mode = global_ligature_mode;
328 last_kern_mode = global_kern_mode;
333 int font_info::get_bold(hunits *res)
343 void font_info::unbold()
351 void font_info::set_bold(hunits offset)
353 if (!is_bold || offset != bold_offset) {
355 bold_offset = offset;
360 void font_info::set_conditional_bold(int fontno, hunits offset)
362 for (conditional_bold *p = cond_bold_list; p; p = p->next)
363 if (p->fontno == fontno) {
364 if (offset != p->offset) {
370 cond_bold_list = new conditional_bold(fontno, offset, cond_bold_list);
373 conditional_bold::conditional_bold(int f, hunits h, conditional_bold *x)
374 : next(x), fontno(f), offset(h)
378 void font_info::conditional_unbold(int fontno)
380 for (conditional_bold **p = &cond_bold_list; *p; p = &(*p)->next)
381 if ((*p)->fontno == fontno) {
382 conditional_bold *tem = *p;
390 void font_info::set_constant_space(constant_space_type type, units x)
392 if (type != is_constant_spaced
393 || (type != CONSTANT_SPACE_NONE && x != constant_space)) {
395 is_constant_spaced = type;
400 void font_info::set_track_kern(track_kerning_function &tk)
402 if (track_kern != tk) {
408 void font_info::flush()
413 int font_info::is_named(symbol s)
415 return internal_name == s;
418 symbol font_info::get_name()
420 return internal_name;
423 symbol get_font_name(int fontno, environment *env)
425 symbol f = font_table[fontno]->get_name();
426 if (font_table[fontno]->is_style()) {
427 return concat(env->get_family()->nm, f);
432 symbol get_style_name(int fontno)
434 if (font_table[fontno]->is_style())
435 return font_table[fontno]->get_name();
440 hunits font_info::get_space_width(font_size fs, int space_sz)
442 if (is_constant_spaced == CONSTANT_SPACE_NONE)
443 return scale(hunits(fm->get_space_width(fs.to_scaled_points())),
445 else if (is_constant_spaced == CONSTANT_SPACE_ABSOLUTE)
446 return constant_space;
448 return scale(constant_space*fs.to_scaled_points(),
449 units_per_inch, 36*72*sizescale);
452 hunits font_info::get_narrow_space_width(font_size fs)
454 charinfo *ci = get_charinfo(symbol("|"));
455 if (fm->contains(ci->as_glyph()))
456 return hunits(fm->get_width(ci->as_glyph(), fs.to_scaled_points()));
458 return hunits(fs.to_units()/6);
461 hunits font_info::get_half_narrow_space_width(font_size fs)
463 charinfo *ci = get_charinfo(symbol("^"));
464 if (fm->contains(ci->as_glyph()))
465 return hunits(fm->get_width(ci->as_glyph(), fs.to_scaled_points()));
467 return hunits(fs.to_units()/12);
472 tfont_spec::tfont_spec(symbol nm, int n, font *f,
473 font_size s, int h, int sl)
474 : name(nm), input_position(n), fm(f), size(s),
475 is_bold(0), is_constant_spaced(0), ligature_mode(1), kern_mode(1),
478 if (height == size.to_scaled_points())
482 int tfont_spec::operator==(const tfont_spec &spec)
486 && input_position == spec.input_position
488 && height == spec.height
489 && slant == spec.slant
491 ? (spec.is_bold && bold_offset == spec.bold_offset)
493 && track_kern == spec.track_kern
494 && (is_constant_spaced
495 ? (spec.is_constant_spaced
496 && constant_space_width == spec.constant_space_width)
497 : !spec.is_constant_spaced)
498 && ligature_mode == spec.ligature_mode
499 && kern_mode == spec.kern_mode)
505 tfont_spec tfont_spec::plain()
507 return tfont_spec(name, input_position, fm, size, height, slant);
510 hunits tfont::get_width(charinfo *c)
512 if (is_constant_spaced)
513 return constant_space_width;
515 return (hunits(fm->get_width(c->as_glyph(), size.to_scaled_points()))
516 + track_kern + bold_offset);
518 return (hunits(fm->get_width(c->as_glyph(), size.to_scaled_points()))
522 vunits tfont::get_char_height(charinfo *c)
524 vunits v = fm->get_height(c->as_glyph(), size.to_scaled_points());
525 if (height != 0 && height != size.to_scaled_points())
526 return scale(v, height, size.to_scaled_points());
531 vunits tfont::get_char_depth(charinfo *c)
533 vunits v = fm->get_depth(c->as_glyph(), size.to_scaled_points());
534 if (height != 0 && height != size.to_scaled_points())
535 return scale(v, height, size.to_scaled_points());
540 hunits tfont::get_char_skew(charinfo *c)
542 return hunits(fm->get_skew(c->as_glyph(), size.to_scaled_points(), slant));
545 hunits tfont::get_italic_correction(charinfo *c)
547 return hunits(fm->get_italic_correction(c->as_glyph(), size.to_scaled_points()));
550 hunits tfont::get_left_italic_correction(charinfo *c)
552 return hunits(fm->get_left_italic_correction(c->as_glyph(),
553 size.to_scaled_points()));
556 hunits tfont::get_subscript_correction(charinfo *c)
558 return hunits(fm->get_subscript_correction(c->as_glyph(),
559 size.to_scaled_points()));
562 inline int tfont::get_input_position()
564 return input_position;
567 inline int tfont::contains(charinfo *ci)
569 return fm->contains(ci->as_glyph());
572 inline int tfont::get_character_type(charinfo *ci)
574 return fm->get_character_type(ci->as_glyph());
577 inline int tfont::get_bold(hunits *res)
587 inline int tfont::get_constant_space(hunits *res)
589 if (is_constant_spaced) {
590 *res = constant_space_width;
597 inline hunits tfont::get_track_kern()
602 inline tfont *tfont::get_plain()
604 return plain_version;
607 inline font_size tfont::get_size()
612 inline int tfont::get_zoom()
614 return fm->get_zoom();
617 inline symbol tfont::get_name()
622 inline int tfont::get_height()
627 inline int tfont::get_slant()
632 symbol SYMBOL_ff("ff");
633 symbol SYMBOL_fi("fi");
634 symbol SYMBOL_fl("fl");
635 symbol SYMBOL_Fi("Fi");
636 symbol SYMBOL_Fl("Fl");
638 charinfo *tfont::get_lig(charinfo *c1, charinfo *c2)
640 if (ligature_mode == 0)
643 if (c1->get_ascii_code() == 'f') {
644 switch (c2->get_ascii_code()) {
646 if (fm->has_ligature(font::LIG_ff))
647 ci = get_charinfo(SYMBOL_ff);
650 if (fm->has_ligature(font::LIG_fi))
651 ci = get_charinfo(SYMBOL_fi);
654 if (fm->has_ligature(font::LIG_fl))
655 ci = get_charinfo(SYMBOL_fl);
659 else if (ligature_mode != 2 && c1->nm == SYMBOL_ff) {
660 switch (c2->get_ascii_code()) {
662 if (fm->has_ligature(font::LIG_ffi))
663 ci = get_charinfo(SYMBOL_Fi);
666 if (fm->has_ligature(font::LIG_ffl))
667 ci = get_charinfo(SYMBOL_Fl);
671 if (ci != 0 && fm->contains(ci->as_glyph()))
676 inline int tfont::get_kern(charinfo *c1, charinfo *c2, hunits *res)
681 int n = fm->get_kern(c1->as_glyph(),
683 size.to_scaled_points());
693 tfont *tfont::tfont_list = 0;
695 tfont::tfont(tfont_spec &spec) : tfont_spec(spec)
699 tfont_spec plain_spec = plain();
701 for (p = tfont_list; p; p = p->next)
702 if (*p == plain_spec) {
707 plain_version = new tfont(plain_spec);
712 class real_output_file : public output_file {
713 #ifndef POPEN_MISSING
716 int printing; // decision via optional page list
717 int output_on; // \O[0] or \O[1] escape calls
718 virtual void really_transparent_char(unsigned char) = 0;
719 virtual void really_print_line(hunits x, vunits y, node *n,
720 vunits before, vunits after, hunits width) = 0;
721 virtual void really_begin_page(int pageno, vunits page_length) = 0;
722 virtual void really_copy_file(hunits x, vunits y, const char *filename);
723 virtual void really_put_filename(const char *, int);
724 virtual void really_on();
725 virtual void really_off();
731 void transparent_char(unsigned char);
732 void print_line(hunits x, vunits y, node *n, vunits before, vunits after, hunits width);
733 void begin_page(int pageno, vunits page_length);
734 void put_filename(const char *, int);
739 void copy_file(hunits x, vunits y, const char *filename);
742 class suppress_output_file : public real_output_file {
744 suppress_output_file();
745 void really_transparent_char(unsigned char);
746 void really_print_line(hunits x, vunits y, node *n, vunits, vunits, hunits width);
747 void really_begin_page(int pageno, vunits page_length);
750 class ascii_output_file : public real_output_file {
753 void really_transparent_char(unsigned char);
754 void really_print_line(hunits x, vunits y, node *n, vunits, vunits, hunits width);
755 void really_begin_page(int pageno, vunits page_length);
756 void outc(unsigned char c);
757 void outs(const char *s);
760 void ascii_output_file::outc(unsigned char c)
765 void ascii_output_file::outs(const char *s)
775 class troff_output_file : public real_output_file {
784 tfont *current_tfont;
785 color *current_fill_color;
786 color *current_glyph_color;
787 int current_font_number;
788 symbol *font_position;
790 enum { TBUF_SIZE = 256 };
791 char tbuf[TBUF_SIZE];
799 void put(unsigned char c);
801 void put(unsigned int i);
802 void put(const char *s);
803 void set_font(tfont *tf);
807 ~troff_output_file();
808 void trailer(vunits page_length);
809 void put_char(charinfo *, tfont *, color *, color *);
810 void put_char_width(charinfo *, tfont *, color *, color *, hunits, hunits);
813 void moveto(hunits, vunits);
814 void start_special(tfont *, color *, color *, int = 0);
815 void start_special();
816 void special_char(unsigned char c);
819 void really_transparent_char(unsigned char c);
820 void really_print_line(hunits x, vunits y, node *n, vunits before, vunits after, hunits width);
821 void really_begin_page(int pageno, vunits page_length);
822 void really_copy_file(hunits x, vunits y, const char *filename);
823 void really_put_filename(const char *, int);
826 void draw(char, hvpair *, int, font_size, color *, color *);
827 void determine_line_limits (char code, hvpair *point, int npoints);
828 void check_charinfo(tfont *tf, charinfo *ci);
829 void glyph_color(color *c);
830 void fill_color(color *c);
831 int get_hpos() { return hpos; }
832 int get_vpos() { return vpos; }
833 void add_to_tag_list(string s);
834 friend void space_char_hmotion_node::tprint(troff_output_file *);
835 friend void unbreakable_space_node::tprint(troff_output_file *);
838 static void put_string(const char *s, FILE *fp)
840 for (; *s != '\0'; ++s)
844 inline void troff_output_file::put(char c)
849 inline void troff_output_file::put(unsigned char c)
854 inline void troff_output_file::put(const char *s)
859 inline void troff_output_file::put(int i)
861 put_string(i_to_a(i), fp);
864 inline void troff_output_file::put(unsigned int i)
866 put_string(ui_to_a(i), fp);
869 void troff_output_file::start_special(tfont *tf, color *gcol, color *fcol,
881 void troff_output_file::start_special()
888 void troff_output_file::special_char(unsigned char c)
895 void troff_output_file::end_special()
900 inline void troff_output_file::moveto(hunits h, vunits v)
906 void troff_output_file::really_print_line(hunits x, vunits y, node *n,
907 vunits before, vunits after, hunits)
911 // Check whether we should push the current troff state and use
912 // the state at the start of the invocation of this diversion.
913 if (n->div_nest_level > cur_div_level && n->push_state) {
914 state.push_state(n->push_state);
915 cur_div_level = n->div_nest_level;
917 // Has the current diversion level decreased? Then we must pop the
919 while (n->div_nest_level < cur_div_level) {
921 cur_div_level = n->div_nest_level;
923 // Now check whether the state has changed.
924 if ((is_on() || n->force_tprint())
925 && (state.changed(n->state) || n->is_tag() || n->is_special)) {
930 state.flush(fp, n->state, tag_list);
931 tag_list = string("");
938 // This ensures that transparent throughput will have a more predictable
944 put(before.to_units());
946 put(after.to_units());
950 inline void troff_output_file::word_marker()
957 inline void troff_output_file::right(hunits n)
959 hpos += n.to_units();
962 inline void troff_output_file::down(vunits n)
964 vpos += n.to_units();
967 void troff_output_file::do_motion()
978 if (hpos != output_hpos) {
979 units n = hpos - output_hpos;
980 if (n > 0 && n < hpos) {
990 if (vpos != output_vpos) {
991 units n = vpos - output_vpos;
992 if (n > 0 && n < vpos) {
1008 void troff_output_file::flush_tbuf()
1024 check_output_limits(hpos, vpos);
1025 check_output_limits(hpos, vpos - current_size);
1027 for (int i = 0; i < tbuf_len; i++)
1033 void troff_output_file::check_charinfo(tfont *tf, charinfo *ci)
1038 int height = tf->get_char_height(ci).to_units();
1039 int width = tf->get_width(ci).to_units()
1040 + tf->get_italic_correction(ci).to_units();
1041 int depth = tf->get_char_depth(ci).to_units();
1042 check_output_limits(output_hpos, output_vpos - height);
1043 check_output_limits(output_hpos + width, output_vpos + depth);
1046 void troff_output_file::put_char_width(charinfo *ci, tfont *tf,
1047 color *gcol, color *fcol,
1050 int kk = k.to_units();
1053 hpos += w.to_units() + kk;
1057 unsigned char c = ci->get_ascii_code();
1063 check_charinfo(tf, ci);
1064 if (ci->numbered()) {
1066 put(ci->get_number());
1070 const char *s = ci->nm.contents();
1079 hpos += w.to_units() + kk;
1081 else if (tcommand_flag) {
1082 if (tbuf_len > 0 && hpos == output_hpos && vpos == output_vpos
1083 && (!gcol || gcol == current_glyph_color)
1084 && (!fcol || fcol == current_fill_color)
1086 && tbuf_len < TBUF_SIZE) {
1087 check_charinfo(tf, ci);
1088 tbuf[tbuf_len++] = c;
1089 output_hpos += w.to_units() + kk;
1097 check_charinfo(tf, ci);
1098 tbuf[tbuf_len++] = c;
1099 output_hpos += w.to_units() + kk;
1105 int n = hpos - output_hpos;
1106 check_charinfo(tf, ci);
1107 // check_output_limits(output_hpos, output_vpos);
1108 if (vpos == output_vpos
1109 && (!gcol || gcol == current_glyph_color)
1110 && (!fcol || fcol == current_fill_color)
1111 && n > 0 && n < 100 && !force_motion) {
1112 put(char(n/10 + '0'));
1113 put(char(n%10 + '0'));
1124 hpos += w.to_units() + kk;
1128 void troff_output_file::put_char(charinfo *ci, tfont *tf,
1129 color *gcol, color *fcol)
1135 unsigned char c = ci->get_ascii_code();
1141 if (ci->numbered()) {
1143 put(ci->get_number());
1147 const char *s = ci->nm.contents();
1158 int n = hpos - output_hpos;
1159 if (vpos == output_vpos
1160 && (!gcol || gcol == current_glyph_color)
1161 && (!fcol || fcol == current_fill_color)
1162 && n > 0 && n < 100) {
1163 put(char(n/10 + '0'));
1164 put(char(n%10 + '0'));
1179 // set_font calls 'flush_tbuf' if necessary.
1181 void troff_output_file::set_font(tfont *tf)
1183 if (current_tfont == tf)
1186 int n = tf->get_input_position();
1187 symbol nm = tf->get_name();
1188 if (n >= nfont_positions || font_position[n] != nm) {
1194 if (n >= nfont_positions) {
1195 int old_nfont_positions = nfont_positions;
1196 symbol *old_font_position = font_position;
1197 nfont_positions *= 3;
1198 nfont_positions /= 2;
1199 if (nfont_positions <= n)
1200 nfont_positions = n + 10;
1201 font_position = new symbol[nfont_positions];
1202 memcpy(font_position, old_font_position,
1203 old_nfont_positions*sizeof(symbol));
1204 a_delete old_font_position;
1206 font_position[n] = nm;
1208 if (current_font_number != n) {
1212 current_font_number = n;
1214 int zoom = tf->get_zoom();
1217 size = scale(tf->get_size().to_scaled_points(),
1220 size = tf->get_size().to_scaled_points();
1221 if (current_size != size) {
1225 current_size = size;
1227 int slant = tf->get_slant();
1228 if (current_slant != slant) {
1232 current_slant = slant;
1234 int height = tf->get_height();
1235 if (current_height != height) {
1237 put(height == 0 ? current_size : height);
1239 current_height = height;
1244 // fill_color calls 'flush_tbuf' and 'do_motion' if necessary.
1246 void troff_output_file::fill_color(color *col)
1248 if (!col || current_fill_color == col)
1250 current_fill_color = col;
1256 unsigned int components[4];
1258 cs = col->get_components(components);
1297 // glyph_color calls 'flush_tbuf' and 'do_motion' if necessary.
1299 void troff_output_file::glyph_color(color *col)
1301 if (!col || current_glyph_color == col)
1303 current_glyph_color = col;
1307 // grotty doesn't like a color command if the vertical position is zero.
1310 unsigned int components[4];
1312 cs = col->get_components(components);
1351 void troff_output_file::add_to_tag_list(string s)
1353 if (tag_list == string(""))
1356 tag_list += string("\n");
1361 // determine_line_limits - works out the smallest box which will contain
1362 // the entity, code, built from the point array.
1363 void troff_output_file::determine_line_limits(char code, hvpair *point,
1374 // only the h field is used when defining a circle
1375 check_output_limits(output_hpos,
1376 output_vpos - point[0].h.to_units()/2);
1377 check_output_limits(output_hpos + point[0].h.to_units(),
1378 output_vpos + point[0].h.to_units()/2);
1382 check_output_limits(output_hpos,
1383 output_vpos - point[0].v.to_units()/2);
1384 check_output_limits(output_hpos + point[0].h.to_units(),
1385 output_vpos + point[0].v.to_units()/2);
1391 check_output_limits(x, y);
1392 for (i = 0; i < npoints; i++) {
1393 x += point[i].h.to_units();
1394 y += point[i].v.to_units();
1395 check_output_limits(x, y);
1401 for (i = 0; i < npoints; i++) {
1402 x += point[i].h.to_units();
1403 y += point[i].v.to_units();
1404 check_output_limits(x, y);
1410 int minx, miny, maxx, maxy;
1413 p[0] = point[0].h.to_units();
1414 p[1] = point[0].v.to_units();
1415 p[2] = point[1].h.to_units();
1416 p[3] = point[1].v.to_units();
1417 if (adjust_arc_center(p, c)) {
1418 check_output_arc_limits(x, y,
1419 p[0], p[1], p[2], p[3],
1421 &minx, &maxx, &miny, &maxy);
1422 check_output_limits(minx, miny);
1423 check_output_limits(maxx, maxy);
1430 check_output_limits(x, y);
1431 for (i = 0; i < npoints; i++) {
1432 x += point[i].h.to_units();
1433 y += point[i].v.to_units();
1434 check_output_limits(x, y);
1440 for (i = 0; i < npoints; i++) {
1441 x += point[i].h.to_units();
1442 y += point[i].v.to_units();
1443 check_output_limits(x, y);
1448 void troff_output_file::draw(char code, hvpair *point, int npoints,
1449 font_size fsize, color *gcol, color *fcol)
1457 int size = fsize.to_scaled_points();
1458 if (current_size != size) {
1462 current_size = size;
1469 put(point[0].h.to_units());
1472 for (i = 0; i < npoints; i++) {
1474 put(point[i].h.to_units());
1476 put(point[i].v.to_units());
1478 determine_line_limits(code, point, npoints);
1481 for (i = 0; i < npoints; i++)
1482 output_hpos += point[i].h.to_units();
1485 for (i = 0; i < npoints; i++)
1486 output_vpos += point[i].v.to_units();
1493 void troff_output_file::really_on()
1500 void troff_output_file::really_off()
1505 void troff_output_file::really_put_filename(const char *filename, int po)
1517 void troff_output_file::really_begin_page(int pageno, vunits page_length)
1521 if (page_length > V0) {
1523 put(page_length.to_units());
1530 current_font_number = -1;
1532 // current_height = 0;
1533 // current_slant = 0;
1539 for (int i = 0; i < nfont_positions; i++)
1540 font_position[i] = NULL_SYMBOL;
1546 void troff_output_file::really_copy_file(hunits x, vunits y,
1547 const char *filename)
1553 FILE *ifp = include_search_path.open_file_cautious(filename);
1555 error("can't open '%1': %2", filename, strerror(errno));
1558 while ((c = getc(ifp)) != EOF)
1565 current_font_number = -1;
1566 for (int i = 0; i < nfont_positions; i++)
1567 font_position[i] = NULL_SYMBOL;
1570 void troff_output_file::really_transparent_char(unsigned char c)
1575 troff_output_file::~troff_output_file()
1577 a_delete font_position;
1580 void troff_output_file::trailer(vunits page_length)
1583 if (page_length > V0) {
1586 put(page_length.to_units());
1592 troff_output_file::troff_output_file()
1593 : current_slant(0), current_height(0), current_fill_color(0),
1594 current_glyph_color(0), nfont_positions(10), tbuf_len(0), begun_page(0),
1597 font_position = new symbol[nfont_positions];
1602 put(units_per_inch);
1613 output_file *the_output = 0;
1615 output_file::output_file()
1619 output_file::~output_file()
1623 void output_file::trailer(vunits)
1627 void output_file::put_filename(const char *, int)
1631 void output_file::on()
1635 void output_file::off()
1639 real_output_file::real_output_file()
1640 : printing(0), output_on(1)
1642 #ifndef POPEN_MISSING
1644 if ((fp = popen(pipe_command, POPEN_WT)) != 0) {
1648 error("pipe open failed: %1", strerror(errno));
1651 #endif /* not POPEN_MISSING */
1655 real_output_file::~real_output_file()
1659 // To avoid looping, set fp to 0 before calling fatal().
1660 if (ferror(fp) || fflush(fp) < 0) {
1662 fatal("error writing output file");
1664 #ifndef POPEN_MISSING
1666 int result = pclose(fp);
1669 fatal("pclose failed");
1670 if (!WIFEXITED(result))
1671 error("output process '%1' got fatal signal %2",
1673 WIFSIGNALED(result) ? WTERMSIG(result) : WSTOPSIG(result));
1675 int exit_status = WEXITSTATUS(result);
1676 if (exit_status != 0)
1677 error("output process '%1' exited with status %2",
1678 pipe_command, exit_status);
1682 #endif /* not POPEN MISSING */
1683 if (fclose(fp) < 0) {
1685 fatal("error closing output file");
1689 void real_output_file::flush()
1692 fatal("error writing output file");
1695 int real_output_file::is_printing()
1700 void real_output_file::begin_page(int pageno, vunits page_length)
1702 printing = in_output_page_list(pageno);
1704 really_begin_page(pageno, page_length);
1707 void real_output_file::copy_file(hunits x, vunits y, const char *filename)
1709 if (printing && output_on)
1710 really_copy_file(x, y, filename);
1711 check_output_limits(x.to_units(), y.to_units());
1714 void real_output_file::transparent_char(unsigned char c)
1716 if (printing && output_on)
1717 really_transparent_char(c);
1720 void real_output_file::print_line(hunits x, vunits y, node *n,
1721 vunits before, vunits after, hunits width)
1724 really_print_line(x, y, n, before, after, width);
1725 delete_node_list(n);
1728 void real_output_file::really_copy_file(hunits, vunits, const char *)
1733 void real_output_file::put_filename(const char *filename, int po)
1735 really_put_filename(filename, po);
1738 void real_output_file::really_put_filename(const char *, int)
1742 void real_output_file::on()
1749 void real_output_file::off()
1755 int real_output_file::is_on()
1760 void real_output_file::really_on()
1764 void real_output_file::really_off()
1768 /* ascii_output_file */
1770 void ascii_output_file::really_transparent_char(unsigned char c)
1775 void ascii_output_file::really_print_line(hunits, vunits, node *n,
1776 vunits, vunits, hunits)
1779 n->ascii_print(this);
1785 void ascii_output_file::really_begin_page(int /*pageno*/, vunits /*page_length*/)
1787 fputs("<beginning of page>\n", fp);
1790 ascii_output_file::ascii_output_file()
1794 /* suppress_output_file */
1796 suppress_output_file::suppress_output_file()
1800 void suppress_output_file::really_print_line(hunits, vunits, node *, vunits, vunits, hunits)
1804 void suppress_output_file::really_begin_page(int, vunits)
1808 void suppress_output_file::really_transparent_char(unsigned char)
1812 /* glyphs, ligatures, kerns, discretionary breaks */
1814 class charinfo_node : public node {
1818 charinfo_node(charinfo *, statem *, int, node * = 0);
1819 int ends_sentence();
1820 int overlaps_vertically();
1821 int overlaps_horizontally();
1824 charinfo_node::charinfo_node(charinfo *c, statem *s, int pop, node *x)
1825 : node(x, s, pop), ci(c)
1829 int charinfo_node::ends_sentence()
1831 if (ci->ends_sentence())
1833 else if (ci->transparent())
1839 int charinfo_node::overlaps_horizontally()
1841 return ci->overlaps_horizontally();
1844 int charinfo_node::overlaps_vertically()
1846 return ci->overlaps_vertically();
1849 class glyph_node : public charinfo_node {
1853 color *fcol; /* this is needed for grotty */
1856 glyph_node(charinfo *, tfont *, color *, color *, hunits,
1857 statem *, int, node * = 0);
1860 glyph_node(charinfo *, tfont *, color *, color *,
1861 statem *, int, node * = 0);
1864 node *merge_glyph_node(glyph_node *);
1865 node *merge_self(node *);
1867 node *last_char_node();
1869 void vertical_extent(vunits *, vunits *);
1870 hunits subscript_correction();
1871 hunits italic_correction();
1872 hunits left_italic_correction();
1874 hyphenation_type get_hyphenation_type();
1876 color *get_glyph_color();
1877 color *get_fill_color();
1878 void tprint(troff_output_file *);
1879 void zero_width_tprint(troff_output_file *);
1880 hyphen_list *get_hyphen_list(hyphen_list *, int *);
1881 node *add_self(node *, hyphen_list **);
1882 void ascii_print(ascii_output_file *);
1883 void asciify(macro *);
1884 int character_type();
1892 class ligature_node : public glyph_node {
1896 ligature_node(charinfo *, tfont *, color *, color *, hunits,
1897 node *, node *, statem *, int, node * = 0);
1900 void *operator new(size_t);
1901 void operator delete(void *);
1902 ligature_node(charinfo *, tfont *, color *, color *,
1903 node *, node *, statem *, int, node * = 0);
1906 node *add_self(node *, hyphen_list **);
1907 hyphen_list *get_hyphen_list(hyphen_list *, int *);
1908 void ascii_print(ascii_output_file *);
1909 void asciify(macro *);
1916 class kern_pair_node : public node {
1921 kern_pair_node(hunits, node *, node *, statem *, int, node * = 0);
1924 node *merge_glyph_node(glyph_node *);
1925 node *add_self(node *, hyphen_list **);
1926 hyphen_list *get_hyphen_list(hyphen_list *, int *);
1927 node *add_discretionary_hyphen();
1929 node *last_char_node();
1930 hunits italic_correction();
1931 hunits subscript_correction();
1932 void tprint(troff_output_file *);
1933 hyphenation_type get_hyphenation_type();
1934 int ends_sentence();
1935 void ascii_print(ascii_output_file *);
1936 void asciify(macro *);
1941 void vertical_extent(vunits *, vunits *);
1944 class dbreak_node : public node {
1949 dbreak_node(node *, node *, statem *, int, node * = 0);
1952 node *merge_glyph_node(glyph_node *);
1953 node *add_discretionary_hyphen();
1955 node *last_char_node();
1956 hunits italic_correction();
1957 hunits subscript_correction();
1958 void tprint(troff_output_file *);
1959 breakpoint *get_breakpoints(hunits width, int ns, breakpoint *rest = 0,
1962 int ends_sentence();
1963 void split(int, node **, node **);
1964 hyphenation_type get_hyphenation_type();
1965 void ascii_print(ascii_output_file *);
1966 void asciify(macro *);
1973 void *ligature_node::operator new(size_t n)
1978 void ligature_node::operator delete(void *p)
1983 glyph_node::glyph_node(charinfo *c, tfont *t, color *gc, color *fc,
1984 statem *s, int pop, node *x)
1985 : charinfo_node(c, s, pop, x), tf(t), gcol(gc), fcol(fc)
1988 wid = tf->get_width(ci);
1993 glyph_node::glyph_node(charinfo *c, tfont *t,
1994 color *gc, color *fc, hunits w,
1995 statem *s, int pop, node *x)
1996 : charinfo_node(c, s, pop, x), tf(t), gcol(gc), fcol(fc), wid(w)
2001 node *glyph_node::copy()
2004 return new glyph_node(ci, tf, gcol, fcol, wid, state, div_nest_level);
2006 return new glyph_node(ci, tf, gcol, fcol, state, div_nest_level);
2010 node *glyph_node::merge_self(node *nd)
2012 return nd->merge_glyph_node(this);
2015 int glyph_node::character_type()
2017 return tf->get_character_type(ci);
2020 node *glyph_node::add_self(node *n, hyphen_list **p)
2022 assert(ci->get_hyphenation_code() == (*p)->hyphenation_code);
2025 if (n == 0 || (nn = n->merge_glyph_node(this)) == 0) {
2030 nn = nn->add_discretionary_hyphen();
2031 hyphen_list *pp = *p;
2037 units glyph_node::size()
2039 return tf->get_size().to_units();
2042 hyphen_list *glyph_node::get_hyphen_list(hyphen_list *tail, int *count)
2045 return new hyphen_list(ci->get_hyphenation_code(), tail);
2048 tfont *node::get_tfont()
2053 tfont *glyph_node::get_tfont()
2058 color *node::get_glyph_color()
2063 color *glyph_node::get_glyph_color()
2068 color *node::get_fill_color()
2073 color *glyph_node::get_fill_color()
2078 node *node::merge_glyph_node(glyph_node *)
2083 node *glyph_node::merge_glyph_node(glyph_node *gn)
2085 if (tf == gn->tf && gcol == gn->gcol && fcol == gn->fcol) {
2087 if ((lig = tf->get_lig(ci, gn->ci)) != 0) {
2090 return new ligature_node(lig, tf, gcol, fcol, this, gn, state,
2091 gn->div_nest_level, next1);
2094 if (tf->get_kern(ci, gn->ci, &kern)) {
2097 return new kern_pair_node(kern, this, gn, state,
2098 gn->div_nest_level, next1);
2107 hunits glyph_node::width()
2112 return tf->get_width(ci);
2116 node *glyph_node::last_char_node()
2121 void glyph_node::vertical_extent(vunits *min, vunits *max)
2123 *min = -tf->get_char_height(ci);
2124 *max = tf->get_char_depth(ci);
2127 hunits glyph_node::skew()
2129 return tf->get_char_skew(ci);
2132 hunits glyph_node::subscript_correction()
2134 return tf->get_subscript_correction(ci);
2137 hunits glyph_node::italic_correction()
2139 return tf->get_italic_correction(ci);
2142 hunits glyph_node::left_italic_correction()
2144 return tf->get_left_italic_correction(ci);
2147 hyphenation_type glyph_node::get_hyphenation_type()
2149 return HYPHEN_MIDDLE;
2152 void glyph_node::ascii_print(ascii_output_file *ascii)
2154 unsigned char c = ci->get_ascii_code();
2158 ascii->outs(ci->nm.contents());
2161 void glyph_node::debug_node()
2163 unsigned char c = ci->get_ascii_code();
2164 fprintf(stderr, "{ %s [", type());
2166 fprintf(stderr, "%c", c);
2168 fprintf(stderr, "%s", ci->nm.contents());
2170 fprintf(stderr, " <push_state>");
2172 state->display_state();
2173 fprintf(stderr, " nest level %d", div_nest_level);
2174 fprintf(stderr, "]}\n");
2178 ligature_node::ligature_node(charinfo *c, tfont *t, color *gc, color *fc,
2179 node *gn1, node *gn2, statem *s,
2181 : glyph_node(c, t, gc, fc, s, pop, x), n1(gn1), n2(gn2)
2186 ligature_node::ligature_node(charinfo *c, tfont *t, color *gc, color *fc,
2187 hunits w, node *gn1, node *gn2, statem *s,
2189 : glyph_node(c, t, gc, fc, w, s, pop, x), n1(gn1), n2(gn2)
2194 ligature_node::~ligature_node()
2200 node *ligature_node::copy()
2203 return new ligature_node(ci, tf, gcol, fcol, wid, n1->copy(), n2->copy(),
2204 state, div_nest_level);
2206 return new ligature_node(ci, tf, gcol, fcol, n1->copy(), n2->copy(),
2207 state, div_nest_level);
2211 void ligature_node::ascii_print(ascii_output_file *ascii)
2213 n1->ascii_print(ascii);
2214 n2->ascii_print(ascii);
2217 hyphen_list *ligature_node::get_hyphen_list(hyphen_list *tail, int *count)
2219 hyphen_list *hl = n2->get_hyphen_list(tail, count);
2220 return n1->get_hyphen_list(hl, count);
2223 node *ligature_node::add_self(node *n, hyphen_list **p)
2225 n = n1->add_self(n, p);
2226 n = n2->add_self(n, p);
2232 kern_pair_node::kern_pair_node(hunits n, node *first, node *second,
2233 statem* s, int pop, node *x)
2234 : node(x, s, pop), amount(n), n1(first), n2(second)
2238 dbreak_node::dbreak_node(node *n, node *p, statem *s, int pop, node *x)
2239 : node(x, s, pop), none(n), pre(p), post(0)
2243 node *dbreak_node::merge_glyph_node(glyph_node *gn)
2245 glyph_node *gn2 = (glyph_node *)gn->copy();
2246 node *new_none = none ? none->merge_glyph_node(gn) : 0;
2247 node *new_post = post ? post->merge_glyph_node(gn2) : 0;
2248 if (new_none == 0 && new_post == 0) {
2267 node *kern_pair_node::merge_glyph_node(glyph_node *gn)
2269 node *nd = n2->merge_glyph_node(gn);
2273 nd = n2->merge_self(n1);
2284 hunits kern_pair_node::italic_correction()
2286 return n2->italic_correction();
2289 hunits kern_pair_node::subscript_correction()
2291 return n2->subscript_correction();
2294 void kern_pair_node::vertical_extent(vunits *min, vunits *max)
2296 n1->vertical_extent(min, max);
2298 n2->vertical_extent(&min2, &max2);
2305 node *kern_pair_node::add_discretionary_hyphen()
2307 tfont *tf = n1->get_tfont();
2309 if (tf->contains(soft_hyphen_char)) {
2310 color *gcol = n2->get_glyph_color();
2311 color *fcol = n2->get_fill_color();
2315 glyph_node *gn = new glyph_node(soft_hyphen_char, tf, gcol, fcol,
2316 state, div_nest_level);
2317 node *nn = n->merge_glyph_node(gn);
2322 return new dbreak_node(this, nn, state, div_nest_level, next1);
2328 kern_pair_node::~kern_pair_node()
2336 dbreak_node::~dbreak_node()
2338 delete_node_list(pre);
2339 delete_node_list(post);
2340 delete_node_list(none);
2343 node *kern_pair_node::copy()
2345 return new kern_pair_node(amount, n1->copy(), n2->copy(), state,
2349 node *copy_node_list(node *n)
2353 node *nn = n->copy();
2367 void delete_node_list(node *n)
2376 node *dbreak_node::copy()
2378 dbreak_node *p = new dbreak_node(copy_node_list(none), copy_node_list(pre),
2379 state, div_nest_level);
2380 p->post = copy_node_list(post);
2384 hyphen_list *node::get_hyphen_list(hyphen_list *tail, int *)
2389 hyphen_list *kern_pair_node::get_hyphen_list(hyphen_list *tail, int *count)
2391 hyphen_list *hl = n2->get_hyphen_list(tail, count);
2392 return n1->get_hyphen_list(hl, count);
2395 class hyphen_inhibitor_node : public node {
2397 hyphen_inhibitor_node(node * = 0);
2403 hyphenation_type get_hyphenation_type();
2406 hyphen_inhibitor_node::hyphen_inhibitor_node(node *nd) : node(nd)
2410 node *hyphen_inhibitor_node::copy()
2412 return new hyphen_inhibitor_node;
2415 int hyphen_inhibitor_node::same(node *)
2420 const char *hyphen_inhibitor_node::type()
2422 return "hyphen_inhibitor_node";
2425 int hyphen_inhibitor_node::force_tprint()
2430 int hyphen_inhibitor_node::is_tag()
2435 hyphenation_type hyphen_inhibitor_node::get_hyphenation_type()
2437 return HYPHEN_INHIBIT;
2440 /* add_discretionary_hyphen methods */
2442 node *dbreak_node::add_discretionary_hyphen()
2445 post = post->add_discretionary_hyphen();
2447 none = none->add_discretionary_hyphen();
2451 node *node::add_discretionary_hyphen()
2453 tfont *tf = get_tfont();
2455 return new hyphen_inhibitor_node(this);
2456 if (tf->contains(soft_hyphen_char)) {
2457 color *gcol = get_glyph_color();
2458 color *fcol = get_fill_color();
2462 glyph_node *gn = new glyph_node(soft_hyphen_char, tf, gcol, fcol,
2463 state, div_nest_level);
2464 node *n1 = n->merge_glyph_node(gn);
2469 return new dbreak_node(this, n1, state, div_nest_level, next1);
2474 node *node::merge_self(node *)
2479 node *node::add_self(node *n, hyphen_list ** /*p*/)
2485 node *kern_pair_node::add_self(node *n, hyphen_list **p)
2487 n = n1->add_self(n, p);
2488 n = n2->add_self(n, p);
2494 hunits node::width()
2499 node *node::last_char_node()
2504 int node::force_tprint()
2514 int node::get_break_code()
2519 hunits hmotion_node::width()
2526 return points_to_units(10);
2529 void node::debug_node()
2531 fprintf(stderr, "{ %s ", type());
2533 fprintf(stderr, " <push_state>");
2535 fprintf(stderr, " <state>");
2536 fprintf(stderr, " nest level %d", div_nest_level);
2537 fprintf(stderr, " }\n");
2541 void node::debug_node_list()
2552 hunits kern_pair_node::width()
2554 return n1->width() + n2->width() + amount;
2557 node *kern_pair_node::last_char_node()
2559 node *nd = n2->last_char_node();
2562 return n1->last_char_node();
2565 hunits dbreak_node::width()
2568 for (node *n = none; n != 0; n = n->next)
2573 node *dbreak_node::last_char_node()
2575 for (node *n = none; n; n = n->next) {
2576 node *last_node = n->last_char_node();
2583 hunits dbreak_node::italic_correction()
2585 return none ? none->italic_correction() : H0;
2588 hunits dbreak_node::subscript_correction()
2590 return none ? none->subscript_correction() : H0;
2593 class italic_corrected_node : public node {
2597 italic_corrected_node(node *, hunits, statem *, int, node * = 0);
2598 ~italic_corrected_node();
2600 void ascii_print(ascii_output_file *);
2601 void asciify(macro *);
2603 node *last_char_node();
2604 void vertical_extent(vunits *, vunits *);
2605 int ends_sentence();
2606 int overlaps_horizontally();
2607 int overlaps_vertically();
2609 hyphenation_type get_hyphenation_type();
2611 hyphen_list *get_hyphen_list(hyphen_list *, int *);
2612 int character_type();
2613 void tprint(troff_output_file *);
2614 hunits subscript_correction();
2616 node *add_self(node *, hyphen_list **);
2622 node *node::add_italic_correction(hunits *wd)
2624 hunits ic = italic_correction();
2631 return new italic_corrected_node(this, ic, state, div_nest_level, next1);
2635 italic_corrected_node::italic_corrected_node(node *nn, hunits xx, statem *s,
2637 : node(p, s, pop), n(nn), x(xx)
2642 italic_corrected_node::~italic_corrected_node()
2647 node *italic_corrected_node::copy()
2649 return new italic_corrected_node(n->copy(), x, state, div_nest_level);
2652 hunits italic_corrected_node::width()
2654 return n->width() + x;
2657 void italic_corrected_node::vertical_extent(vunits *min, vunits *max)
2659 n->vertical_extent(min, max);
2662 void italic_corrected_node::tprint(troff_output_file *out)
2668 hunits italic_corrected_node::skew()
2670 return n->skew() - x/2;
2673 hunits italic_corrected_node::subscript_correction()
2675 return n->subscript_correction() - x;
2678 void italic_corrected_node::ascii_print(ascii_output_file *out)
2680 n->ascii_print(out);
2683 int italic_corrected_node::ends_sentence()
2685 return n->ends_sentence();
2688 int italic_corrected_node::overlaps_horizontally()
2690 return n->overlaps_horizontally();
2693 int italic_corrected_node::overlaps_vertically()
2695 return n->overlaps_vertically();
2698 node *italic_corrected_node::last_char_node()
2700 return n->last_char_node();
2703 tfont *italic_corrected_node::get_tfont()
2705 return n->get_tfont();
2708 hyphenation_type italic_corrected_node::get_hyphenation_type()
2710 return n->get_hyphenation_type();
2713 node *italic_corrected_node::add_self(node *nd, hyphen_list **p)
2715 nd = n->add_self(nd, p);
2716 hunits not_interested;
2717 nd = nd->add_italic_correction(¬_interested);
2723 hyphen_list *italic_corrected_node::get_hyphen_list(hyphen_list *tail,
2726 return n->get_hyphen_list(tail, count);
2729 int italic_corrected_node::character_type()
2731 return n->character_type();
2734 class break_char_node : public node {
2737 char prev_break_code;
2740 break_char_node(node *, int, int, color *, node * = 0);
2741 break_char_node(node *, int, int, color *, statem *, int, node * = 0);
2745 vunits vertical_width();
2746 node *last_char_node();
2747 int character_type();
2748 int ends_sentence();
2749 node *add_self(node *, hyphen_list **);
2750 hyphen_list *get_hyphen_list(hyphen_list *, int *);
2751 void tprint(troff_output_file *);
2752 void zero_width_tprint(troff_output_file *);
2753 void ascii_print(ascii_output_file *);
2754 void asciify(macro *);
2755 hyphenation_type get_hyphenation_type();
2756 int overlaps_vertically();
2757 int overlaps_horizontally();
2764 int get_break_code();
2767 break_char_node::break_char_node(node *n, int bc, int pbc, color *c, node *x)
2768 : node(x), ch(n), break_code(bc), prev_break_code(pbc), col(c)
2772 break_char_node::break_char_node(node *n, int bc, int pbc, color *c,
2773 statem *s, int pop, node *x)
2774 : node(x, s, pop), ch(n), break_code(bc), prev_break_code(pbc), col(c)
2778 break_char_node::~break_char_node()
2783 node *break_char_node::copy()
2785 return new break_char_node(ch->copy(), break_code, prev_break_code,
2786 col, state, div_nest_level);
2789 hunits break_char_node::width()
2794 vunits break_char_node::vertical_width()
2796 return ch->vertical_width();
2799 node *break_char_node::last_char_node()
2801 return ch->last_char_node();
2804 int break_char_node::character_type()
2806 return ch->character_type();
2809 int break_char_node::ends_sentence()
2811 return ch->ends_sentence();
2814 enum break_char_type {
2815 CAN_BREAK_BEFORE = 0x01,
2816 CAN_BREAK_AFTER = 0x02,
2817 IGNORE_HCODES = 0x04,
2818 PROHIBIT_BREAK_BEFORE = 0x08,
2819 PROHIBIT_BREAK_AFTER = 0x10,
2820 INTER_CHAR_SPACE = 0x20
2823 node *break_char_node::add_self(node *n, hyphen_list **p)
2825 int have_space_node = 0;
2826 assert((*p)->hyphenation_code == 0);
2827 if (break_code & CAN_BREAK_BEFORE) {
2828 if ((*p)->breakable || break_code & IGNORE_HCODES) {
2829 n = new space_node(H0, col, n);
2831 have_space_node = 1;
2834 if (!have_space_node) {
2835 if (prev_break_code & INTER_CHAR_SPACE
2836 || prev_break_code & PROHIBIT_BREAK_AFTER) {
2837 if (break_code & PROHIBIT_BREAK_BEFORE)
2838 // stretchable zero-width space not implemented yet
2841 // breakable, stretchable zero-width space not implemented yet
2842 n = new space_node(H0, col, n);
2849 if (break_code & CAN_BREAK_AFTER) {
2850 if ((*p)->breakable || break_code & IGNORE_HCODES) {
2851 n = new space_node(H0, col, n);
2855 hyphen_list *pp = *p;
2861 hyphen_list *break_char_node::get_hyphen_list(hyphen_list *tail, int *)
2863 return new hyphen_list(0, tail);
2866 hyphenation_type break_char_node::get_hyphenation_type()
2868 return HYPHEN_MIDDLE;
2871 void break_char_node::ascii_print(ascii_output_file *ascii)
2873 ch->ascii_print(ascii);
2876 int break_char_node::overlaps_vertically()
2878 return ch->overlaps_vertically();
2881 int break_char_node::overlaps_horizontally()
2883 return ch->overlaps_horizontally();
2886 units break_char_node::size()
2891 tfont *break_char_node::get_tfont()
2893 return ch->get_tfont();
2896 node *extra_size_node::copy()
2898 return new extra_size_node(n, state, div_nest_level);
2901 extra_size_node::extra_size_node(vunits i, statem *s, int pop)
2902 : node(0, s, pop), n(i)
2906 extra_size_node::extra_size_node(vunits i)
2911 node *vertical_size_node::copy()
2913 return new vertical_size_node(n, state, div_nest_level);
2916 vertical_size_node::vertical_size_node(vunits i, statem *s, int pop)
2917 : node(0, s, pop), n(i)
2921 vertical_size_node::vertical_size_node(vunits i)
2926 node *hmotion_node::copy()
2928 return new hmotion_node(n, was_tab, unformat, col, state, div_nest_level);
2931 node *space_char_hmotion_node::copy()
2933 return new space_char_hmotion_node(n, col, state, div_nest_level);
2936 vmotion_node::vmotion_node(vunits i, color *c)
2941 vmotion_node::vmotion_node(vunits i, color *c, statem *s, int pop)
2942 : node(0, s, pop), n(i), col(c)
2946 node *vmotion_node::copy()
2948 return new vmotion_node(n, col, state, div_nest_level);
2951 node *dummy_node::copy()
2953 return new dummy_node;
2956 node *transparent_dummy_node::copy()
2958 return new transparent_dummy_node;
2961 hline_node::~hline_node()
2967 hline_node::hline_node(hunits i, node *c, node *nxt)
2968 : node(nxt), x(i), n(c)
2972 hline_node::hline_node(hunits i, node *c, statem *s, int pop, node *nxt)
2973 : node(nxt, s, pop), x(i), n(c)
2977 node *hline_node::copy()
2979 return new hline_node(x, n ? n->copy() : 0, state, div_nest_level);
2982 hunits hline_node::width()
2984 return x < H0 ? H0 : x;
2987 vline_node::vline_node(vunits i, node *c, node *nxt)
2988 : node(nxt), x(i), n(c)
2992 vline_node::vline_node(vunits i, node *c, statem *s, int pop, node *nxt)
2993 : node(nxt, s, pop), x(i), n(c)
2997 vline_node::~vline_node()
3003 node *vline_node::copy()
3005 return new vline_node(x, n ? n->copy() : 0, state, div_nest_level);
3008 hunits vline_node::width()
3010 return n == 0 ? H0 : n->width();
3013 zero_width_node::zero_width_node(node *nd, statem *s, int pop)
3014 : node(0, s, pop), n(nd)
3018 zero_width_node::zero_width_node(node *nd)
3023 zero_width_node::~zero_width_node()
3025 delete_node_list(n);
3028 node *zero_width_node::copy()
3030 return new zero_width_node(copy_node_list(n), state, div_nest_level);
3033 int node_list_character_type(node *p)
3036 for (; p; p = p->next)
3037 t |= p->character_type();
3041 int zero_width_node::character_type()
3043 return node_list_character_type(n);
3046 void node_list_vertical_extent(node *p, vunits *min, vunits *max)
3050 vunits cur_vpos = V0;
3052 for (; p; p = p->next) {
3053 p->vertical_extent(&v1, &v2);
3060 cur_vpos += p->vertical_width();
3064 void zero_width_node::vertical_extent(vunits *min, vunits *max)
3066 node_list_vertical_extent(n, min, max);
3069 overstrike_node::overstrike_node()
3070 : list(0), max_width(H0)
3074 overstrike_node::overstrike_node(statem *s, int pop)
3075 : node(0, s, pop), list(0), max_width(H0)
3079 overstrike_node::~overstrike_node()
3081 delete_node_list(list);
3084 node *overstrike_node::copy()
3086 overstrike_node *on = new overstrike_node(state, div_nest_level);
3087 for (node *tem = list; tem; tem = tem->next)
3088 on->overstrike(tem->copy());
3092 void overstrike_node::overstrike(node *n)
3096 hunits w = n->width();
3100 for (p = &list; *p; p = &(*p)->next)
3106 hunits overstrike_node::width()
3111 bracket_node::bracket_node()
3112 : list(0), max_width(H0)
3116 bracket_node::bracket_node(statem *s, int pop)
3117 : node(0, s, pop), list(0), max_width(H0)
3121 bracket_node::~bracket_node()
3123 delete_node_list(list);
3126 node *bracket_node::copy()
3128 bracket_node *on = new bracket_node(state, div_nest_level);
3129 node *last_node = 0;
3133 for (tem = list; tem; tem = tem->next) {
3135 tem->next->last = tem;
3138 for (tem = last_node; tem; tem = tem->last)
3139 on->bracket(tem->copy());
3143 void bracket_node::bracket(node *n)
3147 hunits w = n->width();
3154 hunits bracket_node::width()
3164 int node::merge_space(hunits, hunits, hunits)
3170 space_node::space_node(hunits nn, color *c, node *p)
3171 : node(p, 0, 0), n(nn), set(0), was_escape_colon(0), col(c)
3175 space_node::space_node(hunits nn, int s, int flag, color *c, statem *st,
3177 : node(p, st, pop), n(nn), set(s), was_escape_colon(flag), col(c)
3182 space_node::~space_node()
3187 node *space_node::copy()
3189 return new space_node(n, set, was_escape_colon, col, state, div_nest_level);
3192 int space_node::force_tprint()
3197 int space_node::is_tag()
3202 int space_node::nspaces()
3207 int space_node::merge_space(hunits h, hunits, hunits)
3213 hunits space_node::width()
3218 void node::spread_space(int*, hunits*)
3222 void space_node::spread_space(int *n_spaces, hunits *desired_space)
3225 assert(*n_spaces > 0);
3226 if (*n_spaces == 1) {
3227 n += *desired_space;
3228 *desired_space = H0;
3231 hunits extra = *desired_space / *n_spaces;
3232 *desired_space -= extra;
3240 void node::freeze_space()
3244 void space_node::freeze_space()
3249 void node::is_escape_colon()
3253 void space_node::is_escape_colon()
3255 was_escape_colon = 1;
3258 diverted_space_node::diverted_space_node(vunits d, statem *s, int pop,
3260 : node(p, s, pop), n(d)
3264 diverted_space_node::diverted_space_node(vunits d, node *p)
3269 node *diverted_space_node::copy()
3271 return new diverted_space_node(n, state, div_nest_level);
3274 diverted_copy_file_node::diverted_copy_file_node(symbol s, statem *st,
3276 : node(p, st, pop), filename(s)
3280 diverted_copy_file_node::diverted_copy_file_node(symbol s, node *p)
3281 : node(p), filename(s)
3285 node *diverted_copy_file_node::copy()
3287 return new diverted_copy_file_node(filename, state, div_nest_level);
3290 int node::ends_sentence()
3295 int kern_pair_node::ends_sentence()
3297 switch (n2->ends_sentence()) {
3307 return n1->ends_sentence();
3310 int node_list_ends_sentence(node *n)
3312 for (; n != 0; n = n->next)
3313 switch (n->ends_sentence()) {
3326 int dbreak_node::ends_sentence()
3328 return node_list_ends_sentence(none);
3331 int node::overlaps_horizontally()
3336 int node::overlaps_vertically()
3341 int node::discardable()
3346 int space_node::discardable()
3351 vunits node::vertical_width()
3356 vunits vline_node::vertical_width()
3361 vunits vmotion_node::vertical_width()
3366 int node::set_unformat_flag()
3371 int node::character_type()
3376 hunits node::subscript_correction()
3381 hunits node::italic_correction()
3386 hunits node::left_italic_correction()
3396 /* vertical_extent methods */
3398 void node::vertical_extent(vunits *min, vunits *max)
3400 vunits v = vertical_width();
3411 void vline_node::vertical_extent(vunits *min, vunits *max)
3414 node::vertical_extent(min, max);
3417 n->vertical_extent(&cmin, &cmax);
3418 vunits h = n->size();
3425 // we print the first character and then move up, so
3427 // we print the last character and then move up h
3440 // we move down by h and then print the first character, so
3450 /* ascii_print methods */
3452 static void ascii_print_reverse_node_list(ascii_output_file *ascii, node *n)
3456 ascii_print_reverse_node_list(ascii, n->next);
3457 n->ascii_print(ascii);
3460 void dbreak_node::ascii_print(ascii_output_file *ascii)
3462 ascii_print_reverse_node_list(ascii, none);
3465 void kern_pair_node::ascii_print(ascii_output_file *ascii)
3467 n1->ascii_print(ascii);
3468 n2->ascii_print(ascii);
3471 void node::ascii_print(ascii_output_file *)
3475 void space_node::ascii_print(ascii_output_file *ascii)
3481 void hmotion_node::ascii_print(ascii_output_file *ascii)
3483 // this is pretty arbitrary
3484 if (n >= points_to_units(2))
3488 void space_char_hmotion_node::ascii_print(ascii_output_file *ascii)
3493 /* asciify methods */
3495 void node::asciify(macro *m)
3500 void glyph_node::asciify(macro *m)
3502 unsigned char c = ci->get_asciify_code();
3504 c = ci->get_ascii_code();
3513 void kern_pair_node::asciify(macro *m)
3521 static void asciify_reverse_node_list(macro *m, node *n)
3525 asciify_reverse_node_list(m, n->next);
3529 void dbreak_node::asciify(macro *m)
3531 asciify_reverse_node_list(m, none);
3536 void ligature_node::asciify(macro *m)
3544 void break_char_node::asciify(macro *m)
3551 void italic_corrected_node::asciify(macro *m)
3558 void left_italic_corrected_node::asciify(macro *m)
3567 void hmotion_node::asciify(macro *m)
3577 space_char_hmotion_node::space_char_hmotion_node(hunits i, color *c,
3580 : hmotion_node(i, c, s, pop, nxt)
3584 space_char_hmotion_node::space_char_hmotion_node(hunits i, color *c,
3586 : hmotion_node(i, c, 0, 0, nxt)
3590 void space_char_hmotion_node::asciify(macro *m)
3592 m->append(ESCAPE_SPACE);
3596 void space_node::asciify(macro *m)
3598 if (was_escape_colon) {
3599 m->append(ESCAPE_COLON);
3606 void word_space_node::asciify(macro *m)
3608 for (width_list *w = orig_width; w; w = w->next)
3613 void unbreakable_space_node::asciify(macro *m)
3615 m->append(ESCAPE_TILDE);
3619 void line_start_node::asciify(macro *)
3624 void vertical_size_node::asciify(macro *)
3629 breakpoint *node::get_breakpoints(hunits /*width*/, int /*nspaces*/,
3630 breakpoint *rest, int /*is_inner*/)
3640 breakpoint *space_node::get_breakpoints(hunits wd, int ns,
3641 breakpoint *rest, int is_inner)
3643 if (next && next->discardable())
3645 breakpoint *bp = new breakpoint;
3652 bp->index = rest->index + 1;
3662 int space_node::nbreaks()
3664 if (next && next->discardable())
3670 static breakpoint *node_list_get_breakpoints(node *p, hunits *widthp,
3671 int ns, breakpoint *rest)
3674 rest = p->get_breakpoints(*widthp,
3676 node_list_get_breakpoints(p->next, widthp, ns,
3679 *widthp += p->width();
3684 breakpoint *dbreak_node::get_breakpoints(hunits wd, int ns,
3685 breakpoint *rest, int is_inner)
3687 breakpoint *bp = new breakpoint;
3690 for (node *tem = pre; tem != 0; tem = tem->next)
3691 bp->width += tem->width();
3696 bp->index = rest->index + 1;
3703 return node_list_get_breakpoints(none, &wd, ns, bp);
3706 int dbreak_node::nbreaks()
3709 for (node *tem = none; tem != 0; tem = tem->next)
3710 i += tem->nbreaks();
3714 void node::split(int /*where*/, node ** /*prep*/, node ** /*postp*/)
3719 void space_node::split(int where, node **pre, node **post)
3727 static void node_list_split(node *p, int *wherep, node **prep, node **postp)
3731 int nb = p->nbreaks();
3732 node_list_split(p->next, wherep, prep, postp);
3737 else if (*wherep < nb) {
3739 p->split(*wherep, prep, postp);
3748 void dbreak_node::split(int where, node **prep, node **postp)
3758 for (tem = pre; tem->next != 0; tem = tem->next)
3769 node_list_split(none, &where, prep, postp);
3775 hyphenation_type node::get_hyphenation_type()
3777 return HYPHEN_BOUNDARY;
3780 hyphenation_type dbreak_node::get_hyphenation_type()
3782 return HYPHEN_INHIBIT;
3785 hyphenation_type kern_pair_node::get_hyphenation_type()
3787 return HYPHEN_MIDDLE;
3790 hyphenation_type dummy_node::get_hyphenation_type()
3792 return HYPHEN_MIDDLE;
3795 hyphenation_type transparent_dummy_node::get_hyphenation_type()
3797 return HYPHEN_MIDDLE;
3800 hyphenation_type hmotion_node::get_hyphenation_type()
3802 return HYPHEN_MIDDLE;
3805 hyphenation_type space_char_hmotion_node::get_hyphenation_type()
3807 return HYPHEN_MIDDLE;
3810 hyphenation_type overstrike_node::get_hyphenation_type()
3812 return HYPHEN_MIDDLE;
3815 hyphenation_type space_node::get_hyphenation_type()
3817 if (was_escape_colon)
3818 return HYPHEN_MIDDLE;
3819 return HYPHEN_BOUNDARY;
3822 hyphenation_type unbreakable_space_node::get_hyphenation_type()
3824 return HYPHEN_MIDDLE;
3827 int node::interpret(macro *)
3832 special_node::special_node(const macro &m, int n)
3833 : mac(m), no_init_string(n)
3835 font_size fs = curenv->get_font_size();
3836 int char_height = curenv->get_char_height();
3837 int char_slant = curenv->get_char_slant();
3838 int fontno = env_definite_font(curenv);
3839 tf = font_table[fontno]->get_tfont(fs, char_height, char_slant, fontno);
3840 if (curenv->is_composite())
3841 tf = tf->get_plain();
3842 gcol = curenv->get_glyph_color();
3843 fcol = curenv->get_fill_color();
3847 special_node::special_node(const macro &m, tfont *t,
3848 color *gc, color *fc,
3851 : node(0, s, pop), mac(m), tf(t), gcol(gc), fcol(fc), no_init_string(n)
3856 int special_node::same(node *n)
3858 return mac == ((special_node *)n)->mac
3859 && tf == ((special_node *)n)->tf
3860 && gcol == ((special_node *)n)->gcol
3861 && fcol == ((special_node *)n)->fcol
3862 && no_init_string == ((special_node *)n)->no_init_string;
3865 const char *special_node::type()
3867 return "special_node";
3870 int special_node::ends_sentence()
3875 int special_node::force_tprint()
3880 int special_node::is_tag()
3885 node *special_node::copy()
3887 return new special_node(mac, tf, gcol, fcol, state, div_nest_level,
3891 void special_node::tprint_start(troff_output_file *out)
3893 out->start_special(tf, gcol, fcol, no_init_string);
3896 void special_node::tprint_char(troff_output_file *out, unsigned char c)
3898 out->special_char(c);
3901 void special_node::tprint_end(troff_output_file *out)
3906 tfont *special_node::get_tfont()
3913 suppress_node::suppress_node(int on_or_off, int issue_limits)
3914 : is_on(on_or_off), emit_limits(issue_limits), filename(0), position(0),
3919 suppress_node::suppress_node(symbol f, char p, int id)
3920 : is_on(2), emit_limits(0), filename(f), position(p), image_id(id)
3925 suppress_node::suppress_node(int issue_limits, int on_or_off,
3926 symbol f, char p, int id,
3928 : node(0, s, pop), is_on(on_or_off), emit_limits(issue_limits), filename(f),
3929 position(p), image_id(id)
3933 int suppress_node::same(node *n)
3935 return ((is_on == ((suppress_node *)n)->is_on)
3936 && (emit_limits == ((suppress_node *)n)->emit_limits)
3937 && (filename == ((suppress_node *)n)->filename)
3938 && (position == ((suppress_node *)n)->position)
3939 && (image_id == ((suppress_node *)n)->image_id));
3942 const char *suppress_node::type()
3944 return "suppress_node";
3947 node *suppress_node::copy()
3949 return new suppress_node(emit_limits, is_on, filename, position, image_id,
3950 state, div_nest_level);
3955 tag_node::tag_node()
3961 tag_node::tag_node(string s, int delay)
3962 : tag_string(s), delayed(delay)
3964 is_special = !delay;
3967 tag_node::tag_node(string s, statem *st, int pop, int delay)
3968 : node(0, st, pop), tag_string(s), delayed(delay)
3970 is_special = !delay;
3973 node *tag_node::copy()
3975 return new tag_node(tag_string, state, div_nest_level, delayed);
3978 void tag_node::tprint(troff_output_file *out)
3981 out->add_to_tag_list(tag_string);
3983 out->state.add_tag(out->fp, tag_string);
3986 int tag_node::same(node *nd)
3988 return tag_string == ((tag_node *)nd)->tag_string
3989 && delayed == ((tag_node *)nd)->delayed;
3992 const char *tag_node::type()
3997 int tag_node::force_tprint()
4002 int tag_node::is_tag()
4007 int tag_node::ends_sentence()
4012 int get_reg_int(const char *p)
4014 reg *r = (reg *)number_reg_dictionary.lookup(p);
4016 if (r && (r->get_value(&prev_value)))
4017 return (int)prev_value;
4019 warning(WARN_REG, "number register '%1' not defined", p);
4023 const char *get_reg_str(const char *p)
4025 reg *r = (reg *)number_reg_dictionary.lookup(p);
4027 return r->get_string();
4029 warning(WARN_REG, "register '%1' not defined", p);
4033 void suppress_node::put(troff_output_file *out, const char *s)
4036 while (s[i] != (char)0) {
4037 out->special_char(s[i]);
4043 * We need to remember the start of the image and its name.
4046 static char last_position = 0;
4047 static const char *last_image_filename = 0;
4048 static int last_image_id = 0;
4050 inline int min(int a, int b)
4052 return a < b ? a : b;
4056 * tprint - if (is_on == 2)
4057 * remember current position (l, r, c, i) and filename
4063 * emit postscript bounds for image
4065 * if (suppress boolean differs from current state)
4068 * record current page
4069 * set low water mark.
4072 void suppress_node::tprint(troff_output_file *out)
4074 int current_page = topdiv->get_page_number();
4075 // firstly check to see whether this suppress node contains
4076 // an image filename & position.
4078 // remember position and filename
4079 last_position = position;
4080 char *tem = (char *)last_image_filename;
4081 last_image_filename = strsave(filename.contents());
4084 last_image_id = image_id;
4085 // printf("start of image and page = %d\n", current_page);
4088 // now check whether the suppress node requires us to issue limits.
4091 // remember that the filename will contain a %d in which the
4092 // last_image_id is placed
4093 if (last_image_filename == (char *) 0)
4096 sprintf(name, last_image_filename, last_image_id);
4098 switch (last_position) {
4100 out->start_special();
4101 put(out, "devtag:.centered-image");
4104 out->start_special();
4105 put(out, "devtag:.right-image");
4108 out->start_special();
4109 put(out, "devtag:.left-image");
4117 out->start_special();
4118 put(out, "devtag:.auto-image ");
4123 // postscript (or other device)
4124 if (suppress_start_page > 0 && current_page != suppress_start_page)
4125 error("suppression limit registers span more than one page;\n"
4126 "image description %1 will be wrong", image_no);
4127 // if (topdiv->get_page_number() != suppress_start_page)
4128 // fprintf(stderr, "end of image and topdiv page = %d and suppress_start_page = %d\n",
4129 // topdiv->get_page_number(), suppress_start_page);
4131 // remember that the filename will contain a %d in which the
4132 // image_no is placed
4134 "grohtml-info:page %d %d %d %d %d %d %s %d %d %s\n",
4135 topdiv->get_page_number(),
4136 get_reg_int("opminx"), get_reg_int("opminy"),
4137 get_reg_int("opmaxx"), get_reg_int("opmaxy"),
4138 // page offset + line length
4139 get_reg_int(".o") + get_reg_int(".l"),
4140 name, hresolution, vresolution, get_reg_str(".F"));
4147 // lastly we reset the output registers
4148 reset_output_registers();
4152 suppress_start_page = current_page;
4157 int suppress_node::force_tprint()
4162 int suppress_node::is_tag()
4167 hunits suppress_node::width()
4172 /* composite_node */
4174 class composite_node : public charinfo_node {
4178 composite_node(node *, charinfo *, tfont *, statem *, int, node * = 0);
4182 node *last_char_node();
4184 void tprint(troff_output_file *);
4185 hyphenation_type get_hyphenation_type();
4186 void ascii_print(ascii_output_file *);
4187 void asciify(macro *);
4188 hyphen_list *get_hyphen_list(hyphen_list *, int *);
4189 node *add_self(node *, hyphen_list **);
4195 void vertical_extent(vunits *, vunits *);
4196 vunits vertical_width();
4199 composite_node::composite_node(node *p, charinfo *c, tfont *t, statem *s,
4201 : charinfo_node(c, s, pop, x), n(p), tf(t)
4205 composite_node::~composite_node()
4207 delete_node_list(n);
4210 node *composite_node::copy()
4212 return new composite_node(copy_node_list(n), ci, tf, state, div_nest_level);
4215 hunits composite_node::width()
4218 if (tf->get_constant_space(&x))
4221 for (node *tem = n; tem; tem = tem->next)
4224 if (tf->get_bold(&offset))
4226 x += tf->get_track_kern();
4230 node *composite_node::last_char_node()
4235 vunits composite_node::vertical_width()
4238 for (node *tem = n; tem; tem = tem->next)
4239 v += tem->vertical_width();
4243 units composite_node::size()
4245 return tf->get_size().to_units();
4248 hyphenation_type composite_node::get_hyphenation_type()
4250 return HYPHEN_MIDDLE;
4253 void composite_node::asciify(macro *m)
4255 unsigned char c = ci->get_asciify_code();
4257 c = ci->get_ascii_code();
4266 void composite_node::ascii_print(ascii_output_file *ascii)
4268 unsigned char c = ci->get_ascii_code();
4272 ascii->outs(ci->nm.contents());
4276 hyphen_list *composite_node::get_hyphen_list(hyphen_list *tail, int *count)
4279 return new hyphen_list(ci->get_hyphenation_code(), tail);
4282 node *composite_node::add_self(node *nn, hyphen_list **p)
4284 assert(ci->get_hyphenation_code() == (*p)->hyphenation_code);
4288 nn = nn->add_discretionary_hyphen();
4289 hyphen_list *pp = *p;
4295 tfont *composite_node::get_tfont()
4300 node *reverse_node_list(node *n)
4312 void composite_node::vertical_extent(vunits *minimum, vunits *maximum)
4314 n = reverse_node_list(n);
4315 node_list_vertical_extent(n, minimum, maximum);
4316 n = reverse_node_list(n);
4319 width_list::width_list(hunits w, hunits s)
4320 : width(w), sentence_width(s), next(0)
4324 width_list::width_list(width_list *w)
4325 : width(w->width), sentence_width(w->sentence_width), next(0)
4329 word_space_node::word_space_node(hunits d, color *c, width_list *w, node *x)
4330 : space_node(d, c, x), orig_width(w), unformat(0)
4334 word_space_node::word_space_node(hunits d, int s, color *c, width_list *w,
4335 int flag, statem *st, int pop, node *x)
4336 : space_node(d, s, 0, c, st, pop, x), orig_width(w), unformat(flag)
4340 word_space_node::~word_space_node()
4342 width_list *w = orig_width;
4344 width_list *tmp = w;
4350 node *word_space_node::copy()
4352 assert(orig_width != 0);
4353 width_list *w_old_curr = orig_width;
4354 width_list *w_new_curr = new width_list(w_old_curr);
4355 width_list *w_new = w_new_curr;
4356 w_old_curr = w_old_curr->next;
4357 while (w_old_curr != 0) {
4358 w_new_curr->next = new width_list(w_old_curr);
4359 w_new_curr = w_new_curr->next;
4360 w_old_curr = w_old_curr->next;
4362 return new word_space_node(n, set, col, w_new, unformat, state,
4366 int word_space_node::set_unformat_flag()
4372 void word_space_node::tprint(troff_output_file *out)
4374 out->fill_color(col);
4379 int word_space_node::merge_space(hunits h, hunits sw, hunits ssw)
4382 assert(orig_width != 0);
4383 width_list *w = orig_width;
4384 for (; w->next; w = w->next)
4386 w->next = new width_list(sw, ssw);
4390 unbreakable_space_node::unbreakable_space_node(hunits d, color *c, node *x)
4391 : word_space_node(d, c, 0, x)
4395 unbreakable_space_node::unbreakable_space_node(hunits d, int s,
4396 color *c, statem *st, int pop,
4398 : word_space_node(d, s, c, 0, 0, st, pop, x)
4402 node *unbreakable_space_node::copy()
4404 return new unbreakable_space_node(n, set, col, state, div_nest_level);
4407 int unbreakable_space_node::force_tprint()
4412 int unbreakable_space_node::is_tag()
4417 breakpoint *unbreakable_space_node::get_breakpoints(hunits, int,
4418 breakpoint *rest, int)
4423 int unbreakable_space_node::nbreaks()
4428 void unbreakable_space_node::split(int, node **, node **)
4433 int unbreakable_space_node::merge_space(hunits, hunits, hunits)
4442 draw_node::draw_node(char c, hvpair *p, int np, font_size s,
4443 color *gc, color *fc)
4444 : npoints(np), sz(s), gcol(gc), fcol(fc), code(c)
4446 point = new hvpair[npoints];
4447 for (int i = 0; i < npoints; i++)
4451 draw_node::draw_node(char c, hvpair *p, int np, font_size s,
4452 color *gc, color *fc, statem *st, int pop)
4453 : node(0, st, pop), npoints(np), sz(s), gcol(gc), fcol(fc), code(c)
4455 point = new hvpair[npoints];
4456 for (int i = 0; i < npoints; i++)
4460 int draw_node::same(node *n)
4462 draw_node *nd = (draw_node *)n;
4463 if (code != nd->code || npoints != nd->npoints || sz != nd->sz
4464 || gcol != nd->gcol || fcol != nd->fcol)
4466 for (int i = 0; i < npoints; i++)
4467 if (point[i].h != nd->point[i].h || point[i].v != nd->point[i].v)
4472 const char *draw_node::type()
4477 int draw_node::force_tprint()
4482 int draw_node::is_tag()
4487 draw_node::~draw_node()
4493 hunits draw_node::width()
4496 for (int i = 0; i < npoints; i++)
4501 vunits draw_node::vertical_width()
4506 for (int i = 0; i < npoints; i++)
4511 node *draw_node::copy()
4513 return new draw_node(code, point, npoints, sz, gcol, fcol, state,
4517 void draw_node::tprint(troff_output_file *out)
4519 out->draw(code, point, npoints, sz, gcol, fcol);
4522 /* tprint methods */
4524 void glyph_node::tprint(troff_output_file *out)
4526 tfont *ptf = tf->get_plain();
4528 out->put_char_width(ci, ptf, gcol, fcol, width(), H0);
4531 int bold = tf->get_bold(&offset);
4532 hunits w = ptf->get_width(ci);
4535 int cs = tf->get_constant_space(&x);
4545 k = tf->get_track_kern();
4547 out->put_char(ci, ptf, gcol, fcol);
4550 out->put_char_width(ci, ptf, gcol, fcol, w, k);
4554 void glyph_node::zero_width_tprint(troff_output_file *out)
4556 tfont *ptf = tf->get_plain();
4558 int bold = tf->get_bold(&offset);
4560 int cs = tf->get_constant_space(&x);
4562 x -= ptf->get_width(ci);
4568 out->put_char(ci, ptf, gcol, fcol);
4571 out->put_char(ci, ptf, gcol, fcol);
4572 out->right(-offset);
4578 void break_char_node::tprint(troff_output_file *t)
4583 void break_char_node::zero_width_tprint(troff_output_file *t)
4585 ch->zero_width_tprint(t);
4588 void hline_node::tprint(troff_output_file *out)
4598 hunits w = n->width();
4600 error("horizontal line drawing character must have positive width");
4611 out->right(xx - xx2);
4614 hunits rem = x - w*i;
4616 if (n->overlaps_horizontally()) {
4619 out->right(rem - w);
4630 void vline_node::tprint(troff_output_file *out)
4636 vunits h = n->size();
4637 int overlaps = n->overlaps_vertically();
4642 vunits rem = y - i*h;
4644 out->right(n->width());
4649 n->zero_width_tprint(out);
4653 n->zero_width_tprint(out);
4662 out->down(-h - rem);
4668 vunits rem = y - i*h;
4671 out->right(n->width());
4676 n->zero_width_tprint(out);
4679 n->zero_width_tprint(out);
4688 void zero_width_node::tprint(troff_output_file *out)
4693 n->zero_width_tprint(out);
4696 int hpos = out->get_hpos();
4697 int vpos = out->get_vpos();
4703 out->moveto(hpos, vpos);
4706 void overstrike_node::tprint(troff_output_file *out)
4709 for (node *tem = list; tem; tem = tem->next) {
4710 hunits x = (max_width - tem->width())/2;
4711 out->right(x - pos);
4713 tem->zero_width_tprint(out);
4715 out->right(max_width - pos);
4718 void bracket_node::tprint(troff_output_file *out)
4724 for (tem = list; tem; tem = tem->next)
4726 vunits h = list->size();
4727 vunits totalh = h*npieces;
4728 vunits y = (totalh - h)/2;
4730 for (tem = list; tem; tem = tem->next) {
4731 tem->zero_width_tprint(out);
4734 out->right(max_width);
4735 out->down(totalh - y);
4738 void node::tprint(troff_output_file *)
4742 void node::zero_width_tprint(troff_output_file *out)
4744 int hpos = out->get_hpos();
4745 int vpos = out->get_vpos();
4747 out->moveto(hpos, vpos);
4750 void space_node::tprint(troff_output_file *out)
4752 out->fill_color(col);
4756 void hmotion_node::tprint(troff_output_file *out)
4758 out->fill_color(col);
4762 void space_char_hmotion_node::tprint(troff_output_file *out)
4764 out->fill_color(col);
4766 // we emit the space width as a negative glyph index
4770 out->put(-n.to_units());
4776 void vmotion_node::tprint(troff_output_file *out)
4778 out->fill_color(col);
4782 void kern_pair_node::tprint(troff_output_file *out)
4789 static void tprint_reverse_node_list(troff_output_file *out, node *n)
4793 tprint_reverse_node_list(out, n->next);
4797 void dbreak_node::tprint(troff_output_file *out)
4799 tprint_reverse_node_list(out, none);
4802 void composite_node::tprint(troff_output_file *out)
4805 int is_bold = tf->get_bold(&bold_offset);
4806 hunits track_kern = tf->get_track_kern();
4807 hunits constant_space;
4808 int is_constant_spaced = tf->get_constant_space(&constant_space);
4810 if (is_constant_spaced) {
4812 for (node *tem = n; tem; tem = tem->next)
4821 int hpos = out->get_hpos();
4822 int vpos = out->get_vpos();
4823 tprint_reverse_node_list(out, n);
4824 out->moveto(hpos, vpos);
4825 out->right(bold_offset);
4827 tprint_reverse_node_list(out, n);
4828 if (is_constant_spaced)
4831 out->right(track_kern);
4834 node *make_composite_node(charinfo *s, environment *env)
4836 int fontno = env_definite_font(env);
4838 error("no current font");
4841 assert(fontno < font_table_size && font_table[fontno] != 0);
4842 node *n = charinfo_to_node_list(s, env);
4843 font_size fs = env->get_font_size();
4844 int char_height = env->get_char_height();
4845 int char_slant = env->get_char_slant();
4846 tfont *tf = font_table[fontno]->get_tfont(fs, char_height, char_slant,
4848 if (env->is_composite())
4849 tf = tf->get_plain();
4850 return new composite_node(n, s, tf, 0, 0, 0);
4853 node *make_glyph_node(charinfo *s, environment *env, int no_error_message = 0)
4855 int fontno = env_definite_font(env);
4857 error("no current font");
4860 assert(fontno < font_table_size && font_table[fontno] != 0);
4862 int found = font_table[fontno]->contains(s);
4864 macro *mac = s->get_macro();
4865 if (mac && s->is_fallback())
4866 return make_composite_node(s, env);
4867 if (s->numbered()) {
4868 if (!no_error_message)
4869 warning(WARN_CHAR, "can't find numbered character %1",
4873 special_font_list *sf = font_table[fontno]->sf;
4874 while (sf != 0 && !found) {
4877 found = font_table[fn]->contains(s);
4881 symbol f = font_table[fontno]->get_name();
4882 string gl(f.contents());
4884 gl += s->nm.contents();
4886 charinfo *ci = get_charinfo(symbol(gl.contents()));
4887 if (ci && ci->get_macro())
4888 return make_composite_node(ci, env);
4891 sf = global_special_fonts;
4892 while (sf != 0 && !found) {
4895 found = font_table[fn]->contains(s);
4900 if (mac && s->is_special())
4901 return make_composite_node(s, env);
4903 for (fn = 0; fn < font_table_size; fn++)
4905 && font_table[fn]->is_special()
4906 && font_table[fn]->contains(s)) {
4912 if (!no_error_message && s->first_time_not_found()) {
4913 unsigned char input_code = s->get_ascii_code();
4914 if (input_code != 0) {
4915 if (csgraph(input_code))
4916 warning(WARN_CHAR, "can't find character '%1'", input_code);
4918 warning(WARN_CHAR, "can't find character with input code %1",
4921 else if (s->nm.contents()) {
4922 const char *nm = s->nm.contents();
4923 const char *backslash = (nm[1] == 0) ? "\\" : "";
4924 warning(WARN_CHAR, "can't find special character '%1%2'",
4931 font_size fs = env->get_font_size();
4932 int char_height = env->get_char_height();
4933 int char_slant = env->get_char_slant();
4934 tfont *tf = font_table[fontno]->get_tfont(fs, char_height, char_slant, fn);
4935 if (env->is_composite())
4936 tf = tf->get_plain();
4937 color *gcol = env->get_glyph_color();
4938 color *fcol = env->get_fill_color();
4939 return new glyph_node(s, tf, gcol, fcol, 0, 0);
4942 node *make_node(charinfo *ci, environment *env)
4944 switch (ci->get_special_translation()) {
4945 case charinfo::TRANSLATE_SPACE:
4946 return new space_char_hmotion_node(env->get_space_width(),
4947 env->get_fill_color());
4948 case charinfo::TRANSLATE_STRETCHABLE_SPACE:
4949 return new unbreakable_space_node(env->get_space_width(),
4950 env->get_fill_color());
4951 case charinfo::TRANSLATE_DUMMY:
4952 return new dummy_node;
4953 case charinfo::TRANSLATE_HYPHEN_INDICATOR:
4954 error("translation to \\%% ignored in this context");
4957 charinfo *tem = ci->get_translation();
4960 macro *mac = ci->get_macro();
4961 if (mac && ci->is_normal())
4962 return make_composite_node(ci, env);
4964 return make_glyph_node(ci, env);
4967 int character_exists(charinfo *ci, environment *env)
4969 if (ci->get_special_translation() != charinfo::TRANSLATE_NONE)
4971 charinfo *tem = ci->get_translation();
4974 if (ci->get_macro())
4976 node *nd = make_glyph_node(ci, env, 1);
4984 node *node::add_char(charinfo *ci, environment *env,
4985 hunits *widthp, int *spacep, node **glyph_comp_np)
4988 switch (ci->get_special_translation()) {
4989 case charinfo::TRANSLATE_SPACE:
4990 res = new space_char_hmotion_node(env->get_space_width(),
4991 env->get_fill_color(), this);
4992 *widthp += res->width();
4994 case charinfo::TRANSLATE_STRETCHABLE_SPACE:
4995 res = new unbreakable_space_node(env->get_space_width(),
4996 env->get_fill_color(), this);
4997 res->freeze_space();
4998 *widthp += res->width();
4999 *spacep += res->nspaces();
5001 case charinfo::TRANSLATE_DUMMY:
5002 return new dummy_node(this);
5003 case charinfo::TRANSLATE_HYPHEN_INDICATOR:
5004 return add_discretionary_hyphen();
5006 charinfo *tem = ci->get_translation();
5009 macro *mac = ci->get_macro();
5010 if (mac && ci->is_normal()) {
5011 res = make_composite_node(ci, env);
5014 *widthp += res->width();
5016 *glyph_comp_np = res;
5020 *glyph_comp_np = res;
5025 node *gn = make_glyph_node(ci, env);
5029 hunits old_width = width();
5030 node *p = gn->merge_self(this);
5032 *widthp += gn->width();
5037 *widthp += p->width() - old_width;
5041 *glyph_comp_np = res;
5045 if (ci->can_break_before())
5046 break_code = CAN_BREAK_BEFORE;
5047 if (ci->can_break_after())
5048 break_code |= CAN_BREAK_AFTER;
5049 if (ci->ignore_hcodes())
5050 break_code |= IGNORE_HCODES;
5051 if (ci->prohibit_break_before())
5052 break_code = PROHIBIT_BREAK_BEFORE;
5053 if (ci->prohibit_break_after())
5054 break_code |= PROHIBIT_BREAK_AFTER;
5055 if (ci->inter_char_space())
5056 break_code |= INTER_CHAR_SPACE;
5058 node *next1 = res->next;
5060 res = new break_char_node(res, break_code, get_break_code(),
5061 env->get_fill_color(), next1);
5069 int same_node(node *n1, node *n2)
5073 return n1->type() == n2->type() && n1->same(n2);
5081 int same_node_list(node *n1, node *n2)
5084 if (n1->type() != n2->type() || !n1->same(n2))
5092 int extra_size_node::same(node *nd)
5094 return n == ((extra_size_node *)nd)->n;
5097 const char *extra_size_node::type()
5099 return "extra_size_node";
5102 int extra_size_node::force_tprint()
5107 int extra_size_node::is_tag()
5112 int vertical_size_node::same(node *nd)
5114 return n == ((vertical_size_node *)nd)->n;
5117 const char *vertical_size_node::type()
5119 return "vertical_size_node";
5122 int vertical_size_node::set_unformat_flag()
5127 int vertical_size_node::force_tprint()
5132 int vertical_size_node::is_tag()
5137 int hmotion_node::same(node *nd)
5139 return n == ((hmotion_node *)nd)->n
5140 && col == ((hmotion_node *)nd)->col;
5143 const char *hmotion_node::type()
5145 return "hmotion_node";
5148 int hmotion_node::set_unformat_flag()
5154 int hmotion_node::force_tprint()
5159 int hmotion_node::is_tag()
5164 node *hmotion_node::add_self(node *nd, hyphen_list **p)
5167 hyphen_list *pp = *p;
5173 hyphen_list *hmotion_node::get_hyphen_list(hyphen_list *tail, int *)
5175 return new hyphen_list(0, tail);
5178 int space_char_hmotion_node::same(node *nd)
5180 return n == ((space_char_hmotion_node *)nd)->n
5181 && col == ((space_char_hmotion_node *)nd)->col;
5184 const char *space_char_hmotion_node::type()
5186 return "space_char_hmotion_node";
5189 int space_char_hmotion_node::force_tprint()
5194 int space_char_hmotion_node::is_tag()
5199 node *space_char_hmotion_node::add_self(node *nd, hyphen_list **p)
5202 hyphen_list *pp = *p;
5208 hyphen_list *space_char_hmotion_node::get_hyphen_list(hyphen_list *tail,
5211 return new hyphen_list(0, tail);
5214 int vmotion_node::same(node *nd)
5216 return n == ((vmotion_node *)nd)->n
5217 && col == ((vmotion_node *)nd)->col;
5220 const char *vmotion_node::type()
5222 return "vmotion_node";
5225 int vmotion_node::force_tprint()
5230 int vmotion_node::is_tag()
5235 int hline_node::same(node *nd)
5237 return x == ((hline_node *)nd)->x && same_node(n, ((hline_node *)nd)->n);
5240 const char *hline_node::type()
5242 return "hline_node";
5245 int hline_node::force_tprint()
5250 int hline_node::is_tag()
5255 int vline_node::same(node *nd)
5257 return x == ((vline_node *)nd)->x && same_node(n, ((vline_node *)nd)->n);
5260 const char *vline_node::type()
5262 return "vline_node";
5265 int vline_node::force_tprint()
5270 int vline_node::is_tag()
5275 int dummy_node::same(node * /*nd*/)
5280 const char *dummy_node::type()
5282 return "dummy_node";
5285 int dummy_node::force_tprint()
5290 int dummy_node::is_tag()
5295 int transparent_dummy_node::same(node * /*nd*/)
5300 const char *transparent_dummy_node::type()
5302 return "transparent_dummy_node";
5305 int transparent_dummy_node::force_tprint()
5310 int transparent_dummy_node::is_tag()
5315 int transparent_dummy_node::ends_sentence()
5320 int zero_width_node::same(node *nd)
5322 return same_node_list(n, ((zero_width_node *)nd)->n);
5325 const char *zero_width_node::type()
5327 return "zero_width_node";
5330 int zero_width_node::force_tprint()
5335 int zero_width_node::is_tag()
5340 int italic_corrected_node::same(node *nd)
5342 return (x == ((italic_corrected_node *)nd)->x
5343 && same_node(n, ((italic_corrected_node *)nd)->n));
5346 const char *italic_corrected_node::type()
5348 return "italic_corrected_node";
5351 int italic_corrected_node::force_tprint()
5356 int italic_corrected_node::is_tag()
5361 left_italic_corrected_node::left_italic_corrected_node(node *xx)
5366 left_italic_corrected_node::left_italic_corrected_node(statem *s, int pop,
5368 : node(xx, s, pop), n(0)
5372 left_italic_corrected_node::~left_italic_corrected_node()
5377 node *left_italic_corrected_node::merge_glyph_node(glyph_node *gn)
5380 hunits lic = gn->left_italic_correction();
5381 if (!lic.is_zero()) {
5388 node *nd = n->merge_glyph_node(gn);
5391 x = n->left_italic_correction();
5398 node *left_italic_corrected_node::copy()
5400 left_italic_corrected_node *nd =
5401 new left_italic_corrected_node(state, div_nest_level);
5409 void left_italic_corrected_node::tprint(troff_output_file *out)
5417 const char *left_italic_corrected_node::type()
5419 return "left_italic_corrected_node";
5422 int left_italic_corrected_node::force_tprint()
5427 int left_italic_corrected_node::is_tag()
5432 int left_italic_corrected_node::same(node *nd)
5434 return (x == ((left_italic_corrected_node *)nd)->x
5435 && same_node(n, ((left_italic_corrected_node *)nd)->n));
5438 void left_italic_corrected_node::ascii_print(ascii_output_file *out)
5441 n->ascii_print(out);
5444 hunits left_italic_corrected_node::width()
5446 return n ? n->width() + x : H0;
5449 void left_italic_corrected_node::vertical_extent(vunits *minimum,
5453 n->vertical_extent(minimum, maximum);
5455 node::vertical_extent(minimum, maximum);
5458 hunits left_italic_corrected_node::skew()
5460 return n ? n->skew() + x/2 : H0;
5463 hunits left_italic_corrected_node::subscript_correction()
5465 return n ? n->subscript_correction() : H0;
5468 hunits left_italic_corrected_node::italic_correction()
5470 return n ? n->italic_correction() : H0;
5473 int left_italic_corrected_node::ends_sentence()
5475 return n ? n->ends_sentence() : 0;
5478 int left_italic_corrected_node::overlaps_horizontally()
5480 return n ? n->overlaps_horizontally() : 0;
5483 int left_italic_corrected_node::overlaps_vertically()
5485 return n ? n->overlaps_vertically() : 0;
5488 node *left_italic_corrected_node::last_char_node()
5490 return n ? n->last_char_node() : 0;
5493 tfont *left_italic_corrected_node::get_tfont()
5495 return n ? n->get_tfont() : 0;
5498 hyphenation_type left_italic_corrected_node::get_hyphenation_type()
5501 return n->get_hyphenation_type();
5503 return HYPHEN_MIDDLE;
5506 hyphen_list *left_italic_corrected_node::get_hyphen_list(hyphen_list *tail,
5509 return n ? n->get_hyphen_list(tail, count) : tail;
5512 node *left_italic_corrected_node::add_self(node *nd, hyphen_list **p)
5515 nd = new left_italic_corrected_node(state, div_nest_level, nd);
5516 nd = n->add_self(nd, p);
5527 int left_italic_corrected_node::character_type()
5529 return n ? n->character_type() : 0;
5532 int overstrike_node::same(node *nd)
5534 return same_node_list(list, ((overstrike_node *)nd)->list);
5537 const char *overstrike_node::type()
5539 return "overstrike_node";
5542 int overstrike_node::force_tprint()
5547 int overstrike_node::is_tag()
5552 node *overstrike_node::add_self(node *n, hyphen_list **p)
5555 hyphen_list *pp = *p;
5561 hyphen_list *overstrike_node::get_hyphen_list(hyphen_list *tail, int *)
5563 return new hyphen_list(0, tail);
5566 int bracket_node::same(node *nd)
5568 return same_node_list(list, ((bracket_node *)nd)->list);
5571 const char *bracket_node::type()
5573 return "bracket_node";
5576 int bracket_node::force_tprint()
5581 int bracket_node::is_tag()
5586 int composite_node::same(node *nd)
5588 return ci == ((composite_node *)nd)->ci
5589 && same_node_list(n, ((composite_node *)nd)->n);
5592 const char *composite_node::type()
5594 return "composite_node";
5597 int composite_node::force_tprint()
5602 int composite_node::is_tag()
5607 int glyph_node::same(node *nd)
5609 return ci == ((glyph_node *)nd)->ci
5610 && tf == ((glyph_node *)nd)->tf
5611 && gcol == ((glyph_node *)nd)->gcol
5612 && fcol == ((glyph_node *)nd)->fcol;
5615 const char *glyph_node::type()
5617 return "glyph_node";
5620 int glyph_node::force_tprint()
5625 int glyph_node::is_tag()
5630 int ligature_node::same(node *nd)
5632 return (same_node(n1, ((ligature_node *)nd)->n1)
5633 && same_node(n2, ((ligature_node *)nd)->n2)
5634 && glyph_node::same(nd));
5637 const char *ligature_node::type()
5639 return "ligature_node";
5642 int ligature_node::force_tprint()
5647 int ligature_node::is_tag()
5652 int kern_pair_node::same(node *nd)
5654 return (amount == ((kern_pair_node *)nd)->amount
5655 && same_node(n1, ((kern_pair_node *)nd)->n1)
5656 && same_node(n2, ((kern_pair_node *)nd)->n2));
5659 const char *kern_pair_node::type()
5661 return "kern_pair_node";
5664 int kern_pair_node::force_tprint()
5669 int kern_pair_node::is_tag()
5674 int dbreak_node::same(node *nd)
5676 return (same_node_list(none, ((dbreak_node *)nd)->none)
5677 && same_node_list(pre, ((dbreak_node *)nd)->pre)
5678 && same_node_list(post, ((dbreak_node *)nd)->post));
5681 const char *dbreak_node::type()
5683 return "dbreak_node";
5686 int dbreak_node::force_tprint()
5691 int dbreak_node::is_tag()
5696 int break_char_node::same(node *nd)
5698 return break_code == ((break_char_node *)nd)->break_code
5699 && col == ((break_char_node *)nd)->col
5700 && same_node(ch, ((break_char_node *)nd)->ch);
5703 const char *break_char_node::type()
5705 return "break_char_node";
5708 int break_char_node::force_tprint()
5713 int break_char_node::is_tag()
5718 int break_char_node::get_break_code()
5723 int line_start_node::same(node * /*nd*/)
5728 const char *line_start_node::type()
5730 return "line_start_node";
5733 int line_start_node::force_tprint()
5738 int line_start_node::is_tag()
5743 int space_node::same(node *nd)
5745 return n == ((space_node *)nd)->n
5746 && set == ((space_node *)nd)->set
5747 && col == ((space_node *)nd)->col;
5750 const char *space_node::type()
5752 return "space_node";
5755 int word_space_node::same(node *nd)
5757 return n == ((word_space_node *)nd)->n
5758 && set == ((word_space_node *)nd)->set
5759 && col == ((word_space_node *)nd)->col;
5762 const char *word_space_node::type()
5764 return "word_space_node";
5767 int word_space_node::force_tprint()
5772 int word_space_node::is_tag()
5777 void unbreakable_space_node::tprint(troff_output_file *out)
5779 out->fill_color(col);
5781 // we emit the space width as a negative glyph index
5785 out->put(-n.to_units());
5791 int unbreakable_space_node::same(node *nd)
5793 return n == ((unbreakable_space_node *)nd)->n
5794 && set == ((unbreakable_space_node *)nd)->set
5795 && col == ((unbreakable_space_node *)nd)->col;
5798 const char *unbreakable_space_node::type()
5800 return "unbreakable_space_node";
5803 node *unbreakable_space_node::add_self(node *nd, hyphen_list **p)
5806 hyphen_list *pp = *p;
5812 hyphen_list *unbreakable_space_node::get_hyphen_list(hyphen_list *tail, int *)
5814 return new hyphen_list(0, tail);
5817 int diverted_space_node::same(node *nd)
5819 return n == ((diverted_space_node *)nd)->n;
5822 const char *diverted_space_node::type()
5824 return "diverted_space_node";
5827 int diverted_space_node::force_tprint()
5832 int diverted_space_node::is_tag()
5837 int diverted_copy_file_node::same(node *nd)
5839 return filename == ((diverted_copy_file_node *)nd)->filename;
5842 const char *diverted_copy_file_node::type()
5844 return "diverted_copy_file_node";
5847 int diverted_copy_file_node::force_tprint()
5852 int diverted_copy_file_node::is_tag()
5857 // Grow the font_table so that its size is > n.
5859 static void grow_font_table(int n)
5861 assert(n >= font_table_size);
5862 font_info **old_font_table = font_table;
5863 int old_font_table_size = font_table_size;
5864 font_table_size = font_table_size ? (font_table_size*3)/2 : 10;
5865 if (font_table_size <= n)
5866 font_table_size = n + 10;
5867 font_table = new font_info *[font_table_size];
5868 if (old_font_table_size)
5869 memcpy(font_table, old_font_table,
5870 old_font_table_size*sizeof(font_info *));
5871 a_delete old_font_table;
5872 for (int i = old_font_table_size; i < font_table_size; i++)
5876 dictionary font_translation_dictionary(17);
5878 static symbol get_font_translation(symbol nm)
5880 void *p = font_translation_dictionary.lookup(nm);
5881 return p ? symbol((char *)p) : nm;
5884 dictionary font_dictionary(50);
5886 static int mount_font_no_translate(int n, symbol name, symbol external_name,
5890 // We store the address of this char in font_dictionary to indicate
5891 // that we've previously tried to mount the font and failed.
5894 void *p = font_dictionary.lookup(external_name);
5897 fm = font::load_font(external_name.contents(), ¬_found, check_only);
5902 warning(WARN_FONT, "can't find font '%1'", external_name.contents());
5903 (void)font_dictionary.lookup(external_name, &a_char);
5906 (void)font_dictionary.lookup(name, fm);
5908 else if (p == &a_char) {
5910 error("invalid font '%1'", external_name.contents());
5918 if (n >= font_table_size) {
5919 if (n - font_table_size > 1000) {
5920 error("font position too much larger than first unused position");
5925 else if (font_table[n] != 0)
5926 delete font_table[n];
5927 font_table[n] = new font_info(name, n, external_name, fm);
5928 font_family::invalidate_fontno(n);
5932 int mount_font(int n, symbol name, symbol external_name)
5935 name = get_font_translation(name);
5936 if (external_name.is_null())
5937 external_name = name;
5939 external_name = get_font_translation(external_name);
5940 return mount_font_no_translate(n, name, external_name);
5943 int check_font(symbol fam, symbol name)
5945 if (check_style(name))
5946 name = concat(fam, name);
5947 return mount_font_no_translate(0, name, name, 1);
5950 int check_style(symbol s)
5952 int i = symbol_fontno(s);
5953 return i < 0 ? 0 : font_table[i]->is_style();
5956 void mount_style(int n, symbol name)
5959 if (n >= font_table_size) {
5960 if (n - font_table_size > 1000) {
5961 error("font position too much larger than first unused position");
5966 else if (font_table[n] != 0)
5967 delete font_table[n];
5968 font_table[n] = new font_info(get_font_translation(name), n, NULL_SYMBOL, 0);
5969 font_family::invalidate_fontno(n);
5972 /* global functions */
5974 void font_translate()
5976 symbol from = get_name(1);
5977 if (!from.is_null()) {
5978 symbol to = get_name();
5979 if (to.is_null() || from == to)
5980 font_translation_dictionary.remove(from);
5982 (void)font_translation_dictionary.lookup(from, (void *)to.contents());
5987 void font_position()
5990 if (get_integer(&n)) {
5992 error("negative font position");
5994 symbol internal_name = get_name(1);
5995 if (!internal_name.is_null()) {
5996 symbol external_name = get_long_name();
5997 mount_font(n, internal_name, external_name); // ignore error
6004 font_family::font_family(symbol s)
6005 : map_size(10), nm(s)
6007 map = new int[map_size];
6008 for (int i = 0; i < map_size; i++)
6012 font_family::~font_family()
6017 int font_family::make_definite(int i)
6020 if (i < map_size && map[i] >= 0)
6023 if (i < font_table_size && font_table[i] != 0) {
6024 if (i >= map_size) {
6025 int old_map_size = map_size;
6031 map = new int[map_size];
6032 memcpy(map, old_map, old_map_size*sizeof(int));
6034 for (int j = old_map_size; j < map_size; j++)
6037 if (font_table[i]->is_style()) {
6038 symbol sty = font_table[i]->get_name();
6039 symbol f = concat(nm, sty);
6041 // don't use symbol_fontno, because that might return a style
6042 // and because we don't want to translate the name
6043 for (n = 0; n < font_table_size; n++)
6044 if (font_table[n] != 0 && font_table[n]->is_named(f)
6045 && !font_table[n]->is_style())
6047 if (n >= font_table_size) {
6048 n = next_available_font_position();
6049 if (!mount_font_no_translate(n, f, f))
6065 dictionary family_dictionary(5);
6067 font_family *lookup_family(symbol nm)
6069 font_family *f = (font_family *)family_dictionary.lookup(nm);
6071 f = new font_family(nm);
6072 (void)family_dictionary.lookup(nm, f);
6077 void font_family::invalidate_fontno(int n)
6079 assert(n >= 0 && n < font_table_size);
6080 dictionary_iterator iter(family_dictionary);
6083 while (iter.get(&nam, (void **)&fam)) {
6084 int mapsize = fam->map_size;
6087 for (int i = 0; i < mapsize; i++)
6088 if (fam->map[i] == n)
6096 if (get_integer(&n)) {
6098 error("negative font position");
6100 symbol internal_name = get_name(1);
6101 if (!internal_name.is_null())
6102 mount_style(n, internal_name);
6108 static int get_fontno()
6112 if (tok.delimiter()) {
6113 symbol s = get_name(1);
6115 n = symbol_fontno(s);
6117 n = next_available_font_position();
6118 if (!mount_font(n, s))
6121 return curenv->get_family()->make_definite(n);
6124 else if (get_integer(&n)) {
6125 if (n < 0 || n >= font_table_size || font_table[n] == 0)
6126 error("bad font number");
6128 return curenv->get_family()->make_definite(n);
6133 static int underline_fontno = 2;
6135 void underline_font()
6137 int n = get_fontno();
6139 underline_fontno = n;
6143 int get_underline_fontno()
6145 return underline_fontno;
6148 void define_font_special_character()
6150 int n = get_fontno();
6155 symbol f = font_table[n]->get_name();
6156 do_define_character(CHAR_FONT_SPECIAL, f.contents());
6159 void remove_font_special_character()
6161 int n = get_fontno();
6166 symbol f = font_table[n]->get_name();
6167 while (!tok.newline() && !tok.eof()) {
6168 if (!tok.space() && !tok.tab()) {
6169 charinfo *s = tok.get_char(1);
6170 string gl(f.contents());
6172 gl += s->nm.contents();
6174 charinfo *ci = get_charinfo(symbol(gl.contents()));
6177 macro *m = ci->set_macro(0);
6186 static void read_special_fonts(special_font_list **sp)
6188 special_font_list *s = *sp;
6191 special_font_list *tem = s;
6195 special_font_list **p = sp;
6197 int i = get_fontno();
6199 special_font_list *tem = new special_font_list;
6208 void font_special_request()
6210 int n = get_fontno();
6212 read_special_fonts(&font_table[n]->sf);
6216 void special_request()
6218 read_special_fonts(&global_special_fonts);
6222 void font_zoom_request()
6224 int n = get_fontno();
6226 if (font_table[n]->is_style())
6227 warning(WARN_FONT, "can't set zoom factor for a style");
6230 if (has_arg() && get_integer(&zoom)) {
6232 warning(WARN_FONT, "can't use negative zoom factor");
6234 font_table[n]->set_zoom(zoom);
6237 font_table[n]->set_zoom(0);
6243 int next_available_font_position()
6246 for (i = 1; i < font_table_size && font_table[i] != 0; i++)
6251 int symbol_fontno(symbol s)
6253 s = get_font_translation(s);
6254 for (int i = 0; i < font_table_size; i++)
6255 if (font_table[i] != 0 && font_table[i]->is_named(s))
6260 int is_good_fontno(int n)
6262 return n >= 0 && n < font_table_size && font_table[n] != 0;
6265 int get_bold_fontno(int n)
6267 if (n >= 0 && n < font_table_size && font_table[n] != 0) {
6269 if (font_table[n]->get_bold(&offset))
6270 return offset.to_units() + 1;
6278 hunits env_digit_width(environment *env)
6280 node *n = make_glyph_node(charset_table['0'], env);
6282 hunits x = n->width();
6290 hunits env_space_width(environment *env)
6292 int fn = env_definite_font(env);
6293 font_size fs = env->get_font_size();
6294 if (fn < 0 || fn >= font_table_size || font_table[fn] == 0)
6295 return scale(fs.to_units()/3, env->get_space_size(), 12);
6297 return font_table[fn]->get_space_width(fs, env->get_space_size());
6300 hunits env_sentence_space_width(environment *env)
6302 int fn = env_definite_font(env);
6303 font_size fs = env->get_font_size();
6304 if (fn < 0 || fn >= font_table_size || font_table[fn] == 0)
6305 return scale(fs.to_units()/3, env->get_sentence_space_size(), 12);
6307 return font_table[fn]->get_space_width(fs, env->get_sentence_space_size());
6310 hunits env_half_narrow_space_width(environment *env)
6312 int fn = env_definite_font(env);
6313 font_size fs = env->get_font_size();
6314 if (fn < 0 || fn >= font_table_size || font_table[fn] == 0)
6317 return font_table[fn]->get_half_narrow_space_width(fs);
6320 hunits env_narrow_space_width(environment *env)
6322 int fn = env_definite_font(env);
6323 font_size fs = env->get_font_size();
6324 if (fn < 0 || fn >= font_table_size || font_table[fn] == 0)
6327 return font_table[fn]->get_narrow_space_width(fs);
6332 int n = get_fontno();
6335 if (tok.delimiter()) {
6336 int f = get_fontno();
6339 if (has_arg() && get_number(&offset, 'u') && offset >= 1)
6340 font_table[f]->set_conditional_bold(n, hunits(offset - 1));
6342 font_table[f]->conditional_unbold(n);
6347 if (get_number(&offset, 'u') && offset >= 1)
6348 font_table[n]->set_bold(hunits(offset - 1));
6350 font_table[n]->unbold();
6354 font_table[n]->unbold();
6359 track_kerning_function::track_kerning_function() : non_zero(0)
6363 track_kerning_function::track_kerning_function(int min_s, hunits min_a,
6364 int max_s, hunits max_a)
6365 : non_zero(1), min_size(min_s), min_amount(min_a), max_size(max_s),
6370 int track_kerning_function::operator==(const track_kerning_function &tk)
6374 && min_size == tk.min_size
6375 && min_amount == tk.min_amount
6376 && max_size == tk.max_size
6377 && max_amount == tk.max_amount);
6379 return !tk.non_zero;
6382 int track_kerning_function::operator!=(const track_kerning_function &tk)
6385 return (!tk.non_zero
6386 || min_size != tk.min_size
6387 || min_amount != tk.min_amount
6388 || max_size != tk.max_size
6389 || max_amount != tk.max_amount);
6394 hunits track_kerning_function::compute(int size)
6397 if (max_size <= min_size)
6399 else if (size <= min_size)
6401 else if (size >= max_size)
6404 return (scale(max_amount, size - min_size, max_size - min_size)
6405 + scale(min_amount, max_size - size, max_size - min_size));
6413 int n = get_fontno();
6416 hunits min_a, max_a;
6418 && get_number(&min_s, 'z')
6419 && get_hunits(&min_a, 'p')
6420 && get_number(&max_s, 'z')
6421 && get_hunits(&max_a, 'p')) {
6422 track_kerning_function tk(min_s, min_a, max_s, max_a);
6423 font_table[n]->set_track_kern(tk);
6426 track_kerning_function tk;
6427 font_table[n]->set_track_kern(tk);
6433 void constant_space()
6435 int n = get_fontno();
6438 if (!has_arg() || !get_integer(&x))
6439 font_table[n]->set_constant_space(CONSTANT_SPACE_NONE);
6441 if (!has_arg() || !get_number(&y, 'z'))
6442 font_table[n]->set_constant_space(CONSTANT_SPACE_RELATIVE, x);
6444 font_table[n]->set_constant_space(CONSTANT_SPACE_ABSOLUTE,
6456 if (has_arg() && get_integer(&lig) && lig >= 0 && lig <= 2)
6457 global_ligature_mode = lig;
6459 global_ligature_mode = 1;
6466 if (has_arg() && get_integer(&k))
6467 global_kern_mode = k != 0;
6469 global_kern_mode = 1;
6473 void set_soft_hyphen_char()
6475 soft_hyphen_char = get_optional_char();
6476 if (!soft_hyphen_char)
6477 soft_hyphen_char = get_charinfo(HYPHEN_SYMBOL);
6483 if (suppress_output_flag)
6484 the_output = new suppress_output_file;
6485 else if (ascii_output_flag)
6486 the_output = new ascii_output_file;
6488 the_output = new troff_output_file;
6491 class next_available_font_position_reg : public reg {
6493 const char *get_string();
6496 const char *next_available_font_position_reg::get_string()
6498 return i_to_a(next_available_font_position());
6501 class printing_reg : public reg {
6503 const char *get_string();
6506 const char *printing_reg::get_string()
6509 return the_output->is_printing() ? "1" : "0";
6514 void init_node_requests()
6516 init_request("bd", bold_font);
6517 init_request("cs", constant_space);
6518 init_request("fp", font_position);
6519 init_request("fschar", define_font_special_character);
6520 init_request("fspecial", font_special_request);
6521 init_request("fzoom", font_zoom_request);
6522 init_request("ftr", font_translate);
6523 init_request("kern", kern_request);
6524 init_request("lg", ligature);
6525 init_request("rfschar", remove_font_special_character);
6526 init_request("shc", set_soft_hyphen_char);
6527 init_request("special", special_request);
6528 init_request("sty", style);
6529 init_request("tkf", track_kern);
6530 init_request("uf", underline_font);
6531 number_reg_dictionary.define(".fp", new next_available_font_position_reg);
6532 number_reg_dictionary.define(".kern",
6533 new constant_int_reg(&global_kern_mode));
6534 number_reg_dictionary.define(".lg",
6535 new constant_int_reg(&global_ligature_mode));
6536 number_reg_dictionary.define(".P", new printing_reg);
6537 soft_hyphen_char = get_charinfo(HYPHEN_SYMBOL);