/contrib/groff/src/roff/troff/node.cpp
https://bitbucket.org/freebsd/freebsd-head/ · C++ · 6482 lines · 5629 code · 741 blank · 112 comment · 944 complexity · e517a17900a0b49bc72513e34d6fdf9d MD5 · raw file
Large files are truncated click here to view the full file
- // -*- C++ -*-
- /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
- Free Software Foundation, Inc.
- Written by James Clark (jjc@jclark.com)
- This file is part of groff.
- groff is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free
- Software Foundation; either version 2, or (at your option) any later
- version.
- groff is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- for more details.
- You should have received a copy of the GNU General Public License along
- with groff; see the file COPYING. If not, write to the Free Software
- Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
- extern int debug_state;
- #include "troff.h"
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include "dictionary.h"
- #include "hvunits.h"
- #include "stringclass.h"
- #include "mtsm.h"
- #include "env.h"
- #include "request.h"
- #include "node.h"
- #include "token.h"
- #include "div.h"
- #include "reg.h"
- #include "charinfo.h"
- #include "font.h"
- #include "input.h"
- #include "geometry.h"
- #include "nonposix.h"
- #ifdef _POSIX_VERSION
- #include <sys/wait.h>
- #else /* not _POSIX_VERSION */
- /* traditional Unix */
- #define WIFEXITED(s) (((s) & 0377) == 0)
- #define WEXITSTATUS(s) (((s) >> 8) & 0377)
- #define WTERMSIG(s) ((s) & 0177)
- #define WIFSTOPPED(s) (((s) & 0377) == 0177)
- #define WSTOPSIG(s) (((s) >> 8) & 0377)
- #define WIFSIGNALED(s) (((s) & 0377) != 0 && (((s) & 0377) != 0177))
- #endif /* not _POSIX_VERSION */
- // declarations to avoid friend name injections
- class tfont;
- class tfont_spec;
- tfont *make_tfont(tfont_spec &);
- /*
- * how many boundaries of images have been written? Useful for
- * debugging grohtml
- */
- int image_no = 0;
- static int suppress_start_page = 0;
- #define STORE_WIDTH 1
- symbol HYPHEN_SYMBOL("hy");
- // Character used when a hyphen is inserted at a line break.
- static charinfo *soft_hyphen_char;
- enum constant_space_type {
- CONSTANT_SPACE_NONE,
- CONSTANT_SPACE_RELATIVE,
- CONSTANT_SPACE_ABSOLUTE
- };
- struct special_font_list {
- int n;
- special_font_list *next;
- };
- special_font_list *global_special_fonts;
- static int global_ligature_mode = 1;
- static int global_kern_mode = 1;
- class track_kerning_function {
- int non_zero;
- units min_size;
- hunits min_amount;
- units max_size;
- hunits max_amount;
- public:
- track_kerning_function();
- track_kerning_function(units, hunits, units, hunits);
- int operator==(const track_kerning_function &);
- int operator!=(const track_kerning_function &);
- hunits compute(int point_size);
- };
- // embolden fontno when this is the current font
- struct conditional_bold {
- conditional_bold *next;
- int fontno;
- hunits offset;
- conditional_bold(int, hunits, conditional_bold * = 0);
- };
- class font_info {
- tfont *last_tfont;
- int number;
- font_size last_size;
- int last_height;
- int last_slant;
- symbol internal_name;
- symbol external_name;
- font *fm;
- char is_bold;
- hunits bold_offset;
- track_kerning_function track_kern;
- constant_space_type is_constant_spaced;
- units constant_space;
- int last_ligature_mode;
- int last_kern_mode;
- conditional_bold *cond_bold_list;
- void flush();
- public:
- special_font_list *sf;
- font_info(symbol, int, symbol, font *);
- int contains(charinfo *);
- void set_bold(hunits);
- void unbold();
- void set_conditional_bold(int, hunits);
- void conditional_unbold(int);
- void set_track_kern(track_kerning_function &);
- void set_constant_space(constant_space_type, units = 0);
- int is_named(symbol);
- symbol get_name();
- tfont *get_tfont(font_size, int, int, int);
- hunits get_space_width(font_size, int);
- hunits get_narrow_space_width(font_size);
- hunits get_half_narrow_space_width(font_size);
- int get_bold(hunits *);
- int is_special();
- int is_style();
- friend symbol get_font_name(int, environment *);
- friend symbol get_style_name(int);
- };
- class tfont_spec {
- protected:
- symbol name;
- int input_position;
- font *fm;
- font_size size;
- char is_bold;
- char is_constant_spaced;
- int ligature_mode;
- int kern_mode;
- hunits bold_offset;
- hunits track_kern; // add this to the width
- hunits constant_space_width;
- int height;
- int slant;
- public:
- tfont_spec(symbol, int, font *, font_size, int, int);
- tfont_spec(const tfont_spec &spec) { *this = spec; }
- tfont_spec plain();
- int operator==(const tfont_spec &);
- friend tfont *font_info::get_tfont(font_size fs, int, int, int);
- };
- class tfont : public tfont_spec {
- static tfont *tfont_list;
- tfont *next;
- tfont *plain_version;
- public:
- tfont(tfont_spec &);
- int contains(charinfo *);
- hunits get_width(charinfo *c);
- int get_bold(hunits *);
- int get_constant_space(hunits *);
- hunits get_track_kern();
- tfont *get_plain();
- font_size get_size();
- symbol get_name();
- charinfo *get_lig(charinfo *c1, charinfo *c2);
- int get_kern(charinfo *c1, charinfo *c2, hunits *res);
- int get_input_position();
- int get_character_type(charinfo *);
- int get_height();
- int get_slant();
- vunits get_char_height(charinfo *);
- vunits get_char_depth(charinfo *);
- hunits get_char_skew(charinfo *);
- hunits get_italic_correction(charinfo *);
- hunits get_left_italic_correction(charinfo *);
- hunits get_subscript_correction(charinfo *);
- friend tfont *make_tfont(tfont_spec &);
- };
- inline int env_definite_font(environment *env)
- {
- return env->get_family()->make_definite(env->get_font());
- }
- /* font_info functions */
- static font_info **font_table = 0;
- static int font_table_size = 0;
- font_info::font_info(symbol nm, int n, symbol enm, font *f)
- : last_tfont(0), number(n), last_size(0),
- internal_name(nm), external_name(enm), fm(f),
- is_bold(0), is_constant_spaced(CONSTANT_SPACE_NONE), last_ligature_mode(1),
- last_kern_mode(1), cond_bold_list(0), sf(0)
- {
- }
- inline int font_info::contains(charinfo *ci)
- {
- return fm != 0 && fm->contains(ci->get_index());
- }
- inline int font_info::is_special()
- {
- return fm != 0 && fm->is_special();
- }
- inline int font_info::is_style()
- {
- return fm == 0;
- }
- tfont *make_tfont(tfont_spec &spec)
- {
- for (tfont *p = tfont::tfont_list; p; p = p->next)
- if (*p == spec)
- return p;
- return new tfont(spec);
- }
- // this is the current_font, fontno is where we found the character,
- // presumably a special font
- tfont *font_info::get_tfont(font_size fs, int height, int slant, int fontno)
- {
- if (last_tfont == 0 || fs != last_size
- || height != last_height || slant != last_slant
- || global_ligature_mode != last_ligature_mode
- || global_kern_mode != last_kern_mode
- || fontno != number) {
- font_info *f = font_table[fontno];
- tfont_spec spec(f->external_name, f->number, f->fm, fs, height, slant);
- for (conditional_bold *p = cond_bold_list; p; p = p->next)
- if (p->fontno == fontno) {
- spec.is_bold = 1;
- spec.bold_offset = p->offset;
- break;
- }
- if (!spec.is_bold && is_bold) {
- spec.is_bold = 1;
- spec.bold_offset = bold_offset;
- }
- spec.track_kern = track_kern.compute(fs.to_scaled_points());
- spec.ligature_mode = global_ligature_mode;
- spec.kern_mode = global_kern_mode;
- switch (is_constant_spaced) {
- case CONSTANT_SPACE_NONE:
- break;
- case CONSTANT_SPACE_ABSOLUTE:
- spec.is_constant_spaced = 1;
- spec.constant_space_width = constant_space;
- break;
- case CONSTANT_SPACE_RELATIVE:
- spec.is_constant_spaced = 1;
- spec.constant_space_width
- = scale(constant_space*fs.to_scaled_points(),
- units_per_inch,
- 36*72*sizescale);
- break;
- default:
- assert(0);
- }
- if (fontno != number)
- return make_tfont(spec);
- last_tfont = make_tfont(spec);
- last_size = fs;
- last_height = height;
- last_slant = slant;
- last_ligature_mode = global_ligature_mode;
- last_kern_mode = global_kern_mode;
- }
- return last_tfont;
- }
- int font_info::get_bold(hunits *res)
- {
- if (is_bold) {
- *res = bold_offset;
- return 1;
- }
- else
- return 0;
- }
- void font_info::unbold()
- {
- if (is_bold) {
- is_bold = 0;
- flush();
- }
- }
- void font_info::set_bold(hunits offset)
- {
- if (!is_bold || offset != bold_offset) {
- is_bold = 1;
- bold_offset = offset;
- flush();
- }
- }
- void font_info::set_conditional_bold(int fontno, hunits offset)
- {
- for (conditional_bold *p = cond_bold_list; p; p = p->next)
- if (p->fontno == fontno) {
- if (offset != p->offset) {
- p->offset = offset;
- flush();
- }
- return;
- }
- cond_bold_list = new conditional_bold(fontno, offset, cond_bold_list);
- }
- conditional_bold::conditional_bold(int f, hunits h, conditional_bold *x)
- : next(x), fontno(f), offset(h)
- {
- }
- void font_info::conditional_unbold(int fontno)
- {
- for (conditional_bold **p = &cond_bold_list; *p; p = &(*p)->next)
- if ((*p)->fontno == fontno) {
- conditional_bold *tem = *p;
- *p = (*p)->next;
- delete tem;
- flush();
- return;
- }
- }
- void font_info::set_constant_space(constant_space_type type, units x)
- {
- if (type != is_constant_spaced
- || (type != CONSTANT_SPACE_NONE && x != constant_space)) {
- flush();
- is_constant_spaced = type;
- constant_space = x;
- }
- }
- void font_info::set_track_kern(track_kerning_function &tk)
- {
- if (track_kern != tk) {
- track_kern = tk;
- flush();
- }
- }
- void font_info::flush()
- {
- last_tfont = 0;
- }
- int font_info::is_named(symbol s)
- {
- return internal_name == s;
- }
- symbol font_info::get_name()
- {
- return internal_name;
- }
- symbol get_font_name(int fontno, environment *env)
- {
- symbol f = font_table[fontno]->get_name();
- if (font_table[fontno]->is_style()) {
- return concat(env->get_family()->nm, f);
- }
- return f;
- }
- symbol get_style_name(int fontno)
- {
- if (font_table[fontno]->is_style())
- return font_table[fontno]->get_name();
- else
- return EMPTY_SYMBOL;
- }
- hunits font_info::get_space_width(font_size fs, int space_sz)
- {
- if (is_constant_spaced == CONSTANT_SPACE_NONE)
- return scale(hunits(fm->get_space_width(fs.to_scaled_points())),
- space_sz, 12);
- else if (is_constant_spaced == CONSTANT_SPACE_ABSOLUTE)
- return constant_space;
- else
- return scale(constant_space*fs.to_scaled_points(),
- units_per_inch, 36*72*sizescale);
- }
- hunits font_info::get_narrow_space_width(font_size fs)
- {
- charinfo *ci = get_charinfo(symbol("|"));
- if (fm->contains(ci->get_index()))
- return hunits(fm->get_width(ci->get_index(), fs.to_scaled_points()));
- else
- return hunits(fs.to_units()/6);
- }
- hunits font_info::get_half_narrow_space_width(font_size fs)
- {
- charinfo *ci = get_charinfo(symbol("^"));
- if (fm->contains(ci->get_index()))
- return hunits(fm->get_width(ci->get_index(), fs.to_scaled_points()));
- else
- return hunits(fs.to_units()/12);
- }
- /* tfont */
- tfont_spec::tfont_spec(symbol nm, int n, font *f,
- font_size s, int h, int sl)
- : name(nm), input_position(n), fm(f), size(s),
- is_bold(0), is_constant_spaced(0), ligature_mode(1), kern_mode(1),
- height(h), slant(sl)
- {
- if (height == size.to_scaled_points())
- height = 0;
- }
- int tfont_spec::operator==(const tfont_spec &spec)
- {
- if (fm == spec.fm
- && size == spec.size
- && input_position == spec.input_position
- && name == spec.name
- && height == spec.height
- && slant == spec.slant
- && (is_bold
- ? (spec.is_bold && bold_offset == spec.bold_offset)
- : !spec.is_bold)
- && track_kern == spec.track_kern
- && (is_constant_spaced
- ? (spec.is_constant_spaced
- && constant_space_width == spec.constant_space_width)
- : !spec.is_constant_spaced)
- && ligature_mode == spec.ligature_mode
- && kern_mode == spec.kern_mode)
- return 1;
- else
- return 0;
- }
- tfont_spec tfont_spec::plain()
- {
- return tfont_spec(name, input_position, fm, size, height, slant);
- }
- hunits tfont::get_width(charinfo *c)
- {
- if (is_constant_spaced)
- return constant_space_width;
- else if (is_bold)
- return (hunits(fm->get_width(c->get_index(), size.to_scaled_points()))
- + track_kern + bold_offset);
- else
- return (hunits(fm->get_width(c->get_index(), size.to_scaled_points()))
- + track_kern);
- }
- vunits tfont::get_char_height(charinfo *c)
- {
- vunits v = fm->get_height(c->get_index(), size.to_scaled_points());
- if (height != 0 && height != size.to_scaled_points())
- return scale(v, height, size.to_scaled_points());
- else
- return v;
- }
- vunits tfont::get_char_depth(charinfo *c)
- {
- vunits v = fm->get_depth(c->get_index(), size.to_scaled_points());
- if (height != 0 && height != size.to_scaled_points())
- return scale(v, height, size.to_scaled_points());
- else
- return v;
- }
- hunits tfont::get_char_skew(charinfo *c)
- {
- return hunits(fm->get_skew(c->get_index(), size.to_scaled_points(), slant));
- }
- hunits tfont::get_italic_correction(charinfo *c)
- {
- return hunits(fm->get_italic_correction(c->get_index(), size.to_scaled_points()));
- }
- hunits tfont::get_left_italic_correction(charinfo *c)
- {
- return hunits(fm->get_left_italic_correction(c->get_index(),
- size.to_scaled_points()));
- }
- hunits tfont::get_subscript_correction(charinfo *c)
- {
- return hunits(fm->get_subscript_correction(c->get_index(),
- size.to_scaled_points()));
- }
- inline int tfont::get_input_position()
- {
- return input_position;
- }
- inline int tfont::contains(charinfo *ci)
- {
- return fm->contains(ci->get_index());
- }
- inline int tfont::get_character_type(charinfo *ci)
- {
- return fm->get_character_type(ci->get_index());
- }
- inline int tfont::get_bold(hunits *res)
- {
- if (is_bold) {
- *res = bold_offset;
- return 1;
- }
- else
- return 0;
- }
- inline int tfont::get_constant_space(hunits *res)
- {
- if (is_constant_spaced) {
- *res = constant_space_width;
- return 1;
- }
- else
- return 0;
- }
- inline hunits tfont::get_track_kern()
- {
- return track_kern;
- }
- inline tfont *tfont::get_plain()
- {
- return plain_version;
- }
- inline font_size tfont::get_size()
- {
- return size;
- }
- inline symbol tfont::get_name()
- {
- return name;
- }
- inline int tfont::get_height()
- {
- return height;
- }
- inline int tfont::get_slant()
- {
- return slant;
- }
- symbol SYMBOL_ff("ff");
- symbol SYMBOL_fi("fi");
- symbol SYMBOL_fl("fl");
- symbol SYMBOL_Fi("Fi");
- symbol SYMBOL_Fl("Fl");
- charinfo *tfont::get_lig(charinfo *c1, charinfo *c2)
- {
- if (ligature_mode == 0)
- return 0;
- charinfo *ci = 0;
- if (c1->get_ascii_code() == 'f') {
- switch (c2->get_ascii_code()) {
- case 'f':
- if (fm->has_ligature(font::LIG_ff))
- ci = get_charinfo(SYMBOL_ff);
- break;
- case 'i':
- if (fm->has_ligature(font::LIG_fi))
- ci = get_charinfo(SYMBOL_fi);
- break;
- case 'l':
- if (fm->has_ligature(font::LIG_fl))
- ci = get_charinfo(SYMBOL_fl);
- break;
- }
- }
- else if (ligature_mode != 2 && c1->nm == SYMBOL_ff) {
- switch (c2->get_ascii_code()) {
- case 'i':
- if (fm->has_ligature(font::LIG_ffi))
- ci = get_charinfo(SYMBOL_Fi);
- break;
- case 'l':
- if (fm->has_ligature(font::LIG_ffl))
- ci = get_charinfo(SYMBOL_Fl);
- break;
- }
- }
- if (ci != 0 && fm->contains(ci->get_index()))
- return ci;
- return 0;
- }
- inline int tfont::get_kern(charinfo *c1, charinfo *c2, hunits *res)
- {
- if (kern_mode == 0)
- return 0;
- else {
- int n = fm->get_kern(c1->get_index(),
- c2->get_index(),
- size.to_scaled_points());
- if (n) {
- *res = hunits(n);
- return 1;
- }
- else
- return 0;
- }
- }
- tfont *tfont::tfont_list = 0;
- tfont::tfont(tfont_spec &spec) : tfont_spec(spec)
- {
- next = tfont_list;
- tfont_list = this;
- tfont_spec plain_spec = plain();
- tfont *p;
- for (p = tfont_list; p; p = p->next)
- if (*p == plain_spec) {
- plain_version = p;
- break;
- }
- if (!p)
- plain_version = new tfont(plain_spec);
- }
- /* output_file */
- class real_output_file : public output_file {
- #ifndef POPEN_MISSING
- int piped;
- #endif
- int printing; // decision via optional page list
- int output_on; // \O[0] or \O[1] escape calls
- virtual void really_transparent_char(unsigned char) = 0;
- virtual void really_print_line(hunits x, vunits y, node *n,
- vunits before, vunits after, hunits width) = 0;
- virtual void really_begin_page(int pageno, vunits page_length) = 0;
- virtual void really_copy_file(hunits x, vunits y, const char *filename);
- virtual void really_put_filename(const char *filename);
- virtual void really_on();
- virtual void really_off();
- public:
- FILE *fp;
- real_output_file();
- ~real_output_file();
- void flush();
- void transparent_char(unsigned char);
- void print_line(hunits x, vunits y, node *n, vunits before, vunits after, hunits width);
- void begin_page(int pageno, vunits page_length);
- void put_filename(const char *filename);
- void on();
- void off();
- int is_on();
- int is_printing();
- void copy_file(hunits x, vunits y, const char *filename);
- };
- class suppress_output_file : public real_output_file {
- public:
- suppress_output_file();
- void really_transparent_char(unsigned char);
- void really_print_line(hunits x, vunits y, node *n, vunits, vunits, hunits width);
- void really_begin_page(int pageno, vunits page_length);
- };
- class ascii_output_file : public real_output_file {
- public:
- ascii_output_file();
- void really_transparent_char(unsigned char);
- void really_print_line(hunits x, vunits y, node *n, vunits, vunits, hunits width);
- void really_begin_page(int pageno, vunits page_length);
- void outc(unsigned char c);
- void outs(const char *s);
- };
- void ascii_output_file::outc(unsigned char c)
- {
- fputc(c, fp);
- }
- void ascii_output_file::outs(const char *s)
- {
- fputc('<', fp);
- if (s)
- fputs(s, fp);
- fputc('>', fp);
- }
- struct hvpair;
- class troff_output_file : public real_output_file {
- units hpos;
- units vpos;
- units output_vpos;
- units output_hpos;
- int force_motion;
- int current_size;
- int current_slant;
- int current_height;
- tfont *current_tfont;
- color *current_fill_color;
- color *current_glyph_color;
- int current_font_number;
- symbol *font_position;
- int nfont_positions;
- enum { TBUF_SIZE = 256 };
- char tbuf[TBUF_SIZE];
- int tbuf_len;
- int tbuf_kern;
- int begun_page;
- int cur_div_level;
- string tag_list;
- void do_motion();
- void put(char c);
- void put(unsigned char c);
- void put(int i);
- void put(unsigned int i);
- void put(const char *s);
- void set_font(tfont *tf);
- void flush_tbuf();
- public:
- troff_output_file();
- ~troff_output_file();
- void trailer(vunits page_length);
- void put_char(charinfo *, tfont *, color *, color *);
- void put_char_width(charinfo *, tfont *, color *, color *, hunits, hunits);
- void right(hunits);
- void down(vunits);
- void moveto(hunits, vunits);
- void start_special(tfont *, color *, color *, int = 0);
- void start_special();
- void special_char(unsigned char c);
- void end_special();
- void word_marker();
- void really_transparent_char(unsigned char c);
- void really_print_line(hunits x, vunits y, node *n, vunits before, vunits after, hunits width);
- void really_begin_page(int pageno, vunits page_length);
- void really_copy_file(hunits x, vunits y, const char *filename);
- void really_put_filename(const char *filename);
- void really_on();
- void really_off();
- void draw(char, hvpair *, int, font_size, color *, color *);
- void determine_line_limits (char code, hvpair *point, int npoints);
- void check_charinfo(tfont *tf, charinfo *ci);
- void glyph_color(color *c);
- void fill_color(color *c);
- int get_hpos() { return hpos; }
- int get_vpos() { return vpos; }
- void add_to_tag_list(string s);
- friend void space_char_hmotion_node::tprint(troff_output_file *);
- friend void unbreakable_space_node::tprint(troff_output_file *);
- };
- static void put_string(const char *s, FILE *fp)
- {
- for (; *s != '\0'; ++s)
- putc(*s, fp);
- }
- inline void troff_output_file::put(char c)
- {
- putc(c, fp);
- }
- inline void troff_output_file::put(unsigned char c)
- {
- putc(c, fp);
- }
- inline void troff_output_file::put(const char *s)
- {
- put_string(s, fp);
- }
- inline void troff_output_file::put(int i)
- {
- put_string(i_to_a(i), fp);
- }
- inline void troff_output_file::put(unsigned int i)
- {
- put_string(ui_to_a(i), fp);
- }
- void troff_output_file::start_special(tfont *tf, color *gcol, color *fcol,
- int no_init_string)
- {
- set_font(tf);
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- if (!no_init_string)
- put("x X ");
- }
- void troff_output_file::start_special()
- {
- flush_tbuf();
- do_motion();
- put("x X ");
- }
- void troff_output_file::special_char(unsigned char c)
- {
- put(c);
- if (c == '\n')
- put('+');
- }
- void troff_output_file::end_special()
- {
- put('\n');
- }
- inline void troff_output_file::moveto(hunits h, vunits v)
- {
- hpos = h.to_units();
- vpos = v.to_units();
- }
- void troff_output_file::really_print_line(hunits x, vunits y, node *n,
- vunits before, vunits after, hunits)
- {
- moveto(x, y);
- while (n != 0) {
- // Check whether we should push the current troff state and use
- // the state at the start of the invocation of this diversion.
- if (n->div_nest_level > cur_div_level && n->push_state) {
- state.push_state(n->push_state);
- cur_div_level = n->div_nest_level;
- }
- // Has the current diversion level decreased? Then we must pop the
- // troff state.
- while (n->div_nest_level < cur_div_level) {
- state.pop_state();
- cur_div_level = n->div_nest_level;
- }
- // Now check whether the state has changed.
- if ((is_on() || n->force_tprint())
- && (state.changed(n->state) || n->is_tag() || n->is_special)) {
- flush_tbuf();
- do_motion();
- force_motion = 1;
- flush();
- state.flush(fp, n->state, tag_list);
- tag_list = string("");
- flush();
- }
- n->tprint(this);
- n = n->next;
- }
- flush_tbuf();
- // This ensures that transparent throughput will have a more predictable
- // position.
- do_motion();
- force_motion = 1;
- hpos = 0;
- put('n');
- put(before.to_units());
- put(' ');
- put(after.to_units());
- put('\n');
- }
- inline void troff_output_file::word_marker()
- {
- flush_tbuf();
- if (is_on())
- put('w');
- }
- inline void troff_output_file::right(hunits n)
- {
- hpos += n.to_units();
- }
- inline void troff_output_file::down(vunits n)
- {
- vpos += n.to_units();
- }
- void troff_output_file::do_motion()
- {
- if (force_motion) {
- put('V');
- put(vpos);
- put('\n');
- put('H');
- put(hpos);
- put('\n');
- }
- else {
- if (hpos != output_hpos) {
- units n = hpos - output_hpos;
- if (n > 0 && n < hpos) {
- put('h');
- put(n);
- }
- else {
- put('H');
- put(hpos);
- }
- put('\n');
- }
- if (vpos != output_vpos) {
- units n = vpos - output_vpos;
- if (n > 0 && n < vpos) {
- put('v');
- put(n);
- }
- else {
- put('V');
- put(vpos);
- }
- put('\n');
- }
- }
- output_vpos = vpos;
- output_hpos = hpos;
- force_motion = 0;
- }
- void troff_output_file::flush_tbuf()
- {
- if (!is_on()) {
- tbuf_len = 0;
- return;
- }
- if (tbuf_len == 0)
- return;
- if (tbuf_kern == 0)
- put('t');
- else {
- put('u');
- put(tbuf_kern);
- put(' ');
- }
- check_output_limits(hpos, vpos);
- check_output_limits(hpos, vpos - current_size);
- for (int i = 0; i < tbuf_len; i++)
- put(tbuf[i]);
- put('\n');
- tbuf_len = 0;
- }
- void troff_output_file::check_charinfo(tfont *tf, charinfo *ci)
- {
- if (!is_on())
- return;
- int height = tf->get_char_height(ci).to_units();
- int width = tf->get_width(ci).to_units()
- + tf->get_italic_correction(ci).to_units();
- int depth = tf->get_char_depth(ci).to_units();
- check_output_limits(output_hpos, output_vpos - height);
- check_output_limits(output_hpos + width, output_vpos + depth);
- }
- void troff_output_file::put_char_width(charinfo *ci, tfont *tf,
- color *gcol, color *fcol,
- hunits w, hunits k)
- {
- int kk = k.to_units();
- if (!is_on()) {
- flush_tbuf();
- hpos += w.to_units() + kk;
- return;
- }
- set_font(tf);
- unsigned char c = ci->get_ascii_code();
- if (c == '\0') {
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- check_charinfo(tf, ci);
- if (ci->numbered()) {
- put('N');
- put(ci->get_number());
- }
- else {
- put('C');
- const char *s = ci->nm.contents();
- if (s[1] == 0) {
- put('\\');
- put(s[0]);
- }
- else
- put(s);
- }
- put('\n');
- hpos += w.to_units() + kk;
- }
- else if (tcommand_flag) {
- if (tbuf_len > 0 && hpos == output_hpos && vpos == output_vpos
- && (!gcol || gcol == current_glyph_color)
- && (!fcol || fcol == current_fill_color)
- && kk == tbuf_kern
- && tbuf_len < TBUF_SIZE) {
- check_charinfo(tf, ci);
- tbuf[tbuf_len++] = c;
- output_hpos += w.to_units() + kk;
- hpos = output_hpos;
- return;
- }
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- check_charinfo(tf, ci);
- tbuf[tbuf_len++] = c;
- output_hpos += w.to_units() + kk;
- tbuf_kern = kk;
- hpos = output_hpos;
- }
- else {
- // flush_tbuf();
- int n = hpos - output_hpos;
- check_charinfo(tf, ci);
- // check_output_limits(output_hpos, output_vpos);
- if (vpos == output_vpos
- && (!gcol || gcol == current_glyph_color)
- && (!fcol || fcol == current_fill_color)
- && n > 0 && n < 100 && !force_motion) {
- put(char(n/10 + '0'));
- put(char(n%10 + '0'));
- put(c);
- output_hpos = hpos;
- }
- else {
- glyph_color(gcol);
- fill_color(fcol);
- do_motion();
- put('c');
- put(c);
- }
- hpos += w.to_units() + kk;
- }
- }
- void troff_output_file::put_char(charinfo *ci, tfont *tf,
- color *gcol, color *fcol)
- {
- flush_tbuf();
- if (!is_on())
- return;
- set_font(tf);
- unsigned char c = ci->get_ascii_code();
- if (c == '\0') {
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- if (ci->numbered()) {
- put('N');
- put(ci->get_number());
- }
- else {
- put('C');
- const char *s = ci->nm.contents();
- if (s[1] == 0) {
- put('\\');
- put(s[0]);
- }
- else
- put(s);
- }
- put('\n');
- }
- else {
- int n = hpos - output_hpos;
- if (vpos == output_vpos
- && (!gcol || gcol == current_glyph_color)
- && (!fcol || fcol == current_fill_color)
- && n > 0 && n < 100) {
- put(char(n/10 + '0'));
- put(char(n%10 + '0'));
- put(c);
- output_hpos = hpos;
- }
- else {
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- put('c');
- put(c);
- }
- }
- }
- // set_font calls `flush_tbuf' if necessary.
- void troff_output_file::set_font(tfont *tf)
- {
- if (current_tfont == tf)
- return;
- flush_tbuf();
- int n = tf->get_input_position();
- symbol nm = tf->get_name();
- if (n >= nfont_positions || font_position[n] != nm) {
- put("x font ");
- put(n);
- put(' ');
- put(nm.contents());
- put('\n');
- if (n >= nfont_positions) {
- int old_nfont_positions = nfont_positions;
- symbol *old_font_position = font_position;
- nfont_positions *= 3;
- nfont_positions /= 2;
- if (nfont_positions <= n)
- nfont_positions = n + 10;
- font_position = new symbol[nfont_positions];
- memcpy(font_position, old_font_position,
- old_nfont_positions*sizeof(symbol));
- a_delete old_font_position;
- }
- font_position[n] = nm;
- }
- if (current_font_number != n) {
- put('f');
- put(n);
- put('\n');
- current_font_number = n;
- }
- int size = tf->get_size().to_scaled_points();
- if (current_size != size) {
- put('s');
- put(size);
- put('\n');
- current_size = size;
- }
- int slant = tf->get_slant();
- if (current_slant != slant) {
- put("x Slant ");
- put(slant);
- put('\n');
- current_slant = slant;
- }
- int height = tf->get_height();
- if (current_height != height) {
- put("x Height ");
- put(height == 0 ? current_size : height);
- put('\n');
- current_height = height;
- }
- current_tfont = tf;
- }
- // fill_color calls `flush_tbuf' and `do_motion' if necessary.
- void troff_output_file::fill_color(color *col)
- {
- if (!col || current_fill_color == col)
- return;
- current_fill_color = col;
- if (!color_flag)
- return;
- flush_tbuf();
- do_motion();
- put("DF");
- unsigned int components[4];
- color_scheme cs;
- cs = col->get_components(components);
- switch (cs) {
- case DEFAULT:
- put('d');
- break;
- case RGB:
- put("r ");
- put(Red);
- put(' ');
- put(Green);
- put(' ');
- put(Blue);
- break;
- case CMY:
- put("c ");
- put(Cyan);
- put(' ');
- put(Magenta);
- put(' ');
- put(Yellow);
- break;
- case CMYK:
- put("k ");
- put(Cyan);
- put(' ');
- put(Magenta);
- put(' ');
- put(Yellow);
- put(' ');
- put(Black);
- break;
- case GRAY:
- put("g ");
- put(Gray);
- break;
- }
- put('\n');
- }
- // glyph_color calls `flush_tbuf' and `do_motion' if necessary.
- void troff_output_file::glyph_color(color *col)
- {
- if (!col || current_glyph_color == col)
- return;
- current_glyph_color = col;
- if (!color_flag)
- return;
- flush_tbuf();
- // grotty doesn't like a color command if the vertical position is zero.
- do_motion();
- put("m");
- unsigned int components[4];
- color_scheme cs;
- cs = col->get_components(components);
- switch (cs) {
- case DEFAULT:
- put('d');
- break;
- case RGB:
- put("r ");
- put(Red);
- put(' ');
- put(Green);
- put(' ');
- put(Blue);
- break;
- case CMY:
- put("c ");
- put(Cyan);
- put(' ');
- put(Magenta);
- put(' ');
- put(Yellow);
- break;
- case CMYK:
- put("k ");
- put(Cyan);
- put(' ');
- put(Magenta);
- put(' ');
- put(Yellow);
- put(' ');
- put(Black);
- break;
- case GRAY:
- put("g ");
- put(Gray);
- break;
- }
- put('\n');
- }
- void troff_output_file::add_to_tag_list(string s)
- {
- if (tag_list == string(""))
- tag_list = s;
- else {
- tag_list += string("\n");
- tag_list += s;
- }
- }
- // determine_line_limits - works out the smallest box which will contain
- // the entity, code, built from the point array.
- void troff_output_file::determine_line_limits(char code, hvpair *point,
- int npoints)
- {
- int i, x, y;
- if (!is_on())
- return;
- switch (code) {
- case 'c':
- case 'C':
- // only the h field is used when defining a circle
- check_output_limits(output_hpos,
- output_vpos - point[0].h.to_units()/2);
- check_output_limits(output_hpos + point[0].h.to_units(),
- output_vpos + point[0].h.to_units()/2);
- break;
- case 'E':
- case 'e':
- check_output_limits(output_hpos,
- output_vpos - point[0].v.to_units()/2);
- check_output_limits(output_hpos + point[0].h.to_units(),
- output_vpos + point[0].v.to_units()/2);
- break;
- case 'P':
- case 'p':
- x = output_hpos;
- y = output_vpos;
- check_output_limits(x, y);
- for (i = 0; i < npoints; i++) {
- x += point[i].h.to_units();
- y += point[i].v.to_units();
- check_output_limits(x, y);
- }
- break;
- case 't':
- x = output_hpos;
- y = output_vpos;
- for (i = 0; i < npoints; i++) {
- x += point[i].h.to_units();
- y += point[i].v.to_units();
- check_output_limits(x, y);
- }
- break;
- case 'a':
- double c[2];
- int p[4];
- int minx, miny, maxx, maxy;
- x = output_hpos;
- y = output_vpos;
- p[0] = point[0].h.to_units();
- p[1] = point[0].v.to_units();
- p[2] = point[1].h.to_units();
- p[3] = point[1].v.to_units();
- if (adjust_arc_center(p, c)) {
- check_output_arc_limits(x, y,
- p[0], p[1], p[2], p[3],
- c[0], c[1],
- &minx, &maxx, &miny, &maxy);
- check_output_limits(minx, miny);
- check_output_limits(maxx, maxy);
- break;
- }
- // fall through
- case 'l':
- x = output_hpos;
- y = output_vpos;
- check_output_limits(x, y);
- for (i = 0; i < npoints; i++) {
- x += point[i].h.to_units();
- y += point[i].v.to_units();
- check_output_limits(x, y);
- }
- break;
- default:
- x = output_hpos;
- y = output_vpos;
- for (i = 0; i < npoints; i++) {
- x += point[i].h.to_units();
- y += point[i].v.to_units();
- check_output_limits(x, y);
- }
- }
- }
- void troff_output_file::draw(char code, hvpair *point, int npoints,
- font_size fsize, color *gcol, color *fcol)
- {
- int i;
- glyph_color(gcol);
- fill_color(fcol);
- flush_tbuf();
- do_motion();
- if (is_on()) {
- int size = fsize.to_scaled_points();
- if (current_size != size) {
- put('s');
- put(size);
- put('\n');
- current_size = size;
- current_tfont = 0;
- }
- put('D');
- put(code);
- if (code == 'c') {
- put(' ');
- put(point[0].h.to_units());
- }
- else
- for (i = 0; i < npoints; i++) {
- put(' ');
- put(point[i].h.to_units());
- put(' ');
- put(point[i].v.to_units());
- }
- determine_line_limits(code, point, npoints);
- }
- for (i = 0; i < npoints; i++)
- output_hpos += point[i].h.to_units();
- hpos = output_hpos;
- if (code != 'e') {
- for (i = 0; i < npoints; i++)
- output_vpos += point[i].v.to_units();
- vpos = output_vpos;
- }
- if (is_on())
- put('\n');
- }
- void troff_output_file::really_on()
- {
- flush_tbuf();
- force_motion = 1;
- do_motion();
- }
- void troff_output_file::really_off()
- {
- flush_tbuf();
- }
- void troff_output_file::really_put_filename(const char *filename)
- {
- flush_tbuf();
- put("F ");
- put(filename);
- put('\n');
- }
- void troff_output_file::really_begin_page(int pageno, vunits page_length)
- {
- flush_tbuf();
- if (begun_page) {
- if (page_length > V0) {
- put('V');
- put(page_length.to_units());
- put('\n');
- }
- }
- else
- begun_page = 1;
- current_tfont = 0;
- current_font_number = -1;
- current_size = 0;
- // current_height = 0;
- // current_slant = 0;
- hpos = 0;
- vpos = 0;
- output_hpos = 0;
- output_vpos = 0;
- force_motion = 1;
- for (int i = 0; i < nfont_positions; i++)
- font_position[i] = NULL_SYMBOL;
- put('p');
- put(pageno);
- put('\n');
- }
- void troff_output_file::really_copy_file(hunits x, vunits y,
- const char *filename)
- {
- moveto(x, y);
- flush_tbuf();
- do_motion();
- errno = 0;
- FILE *ifp = include_search_path.open_file_cautious(filename);
- if (ifp == 0)
- error("can't open `%1': %2", filename, strerror(errno));
- else {
- int c;
- while ((c = getc(ifp)) != EOF)
- put(char(c));
- fclose(ifp);
- }
- force_motion = 1;
- current_size = 0;
- current_tfont = 0;
- current_font_number = -1;
- for (int i = 0; i < nfont_positions; i++)
- font_position[i] = NULL_SYMBOL;
- }
- void troff_output_file::really_transparent_char(unsigned char c)
- {
- put(c);
- }
- troff_output_file::~troff_output_file()
- {
- a_delete font_position;
- }
- void troff_output_file::trailer(vunits page_length)
- {
- flush_tbuf();
- if (page_length > V0) {
- put("x trailer\n");
- put('V');
- put(page_length.to_units());
- put('\n');
- }
- put("x stop\n");
- }
- troff_output_file::troff_output_file()
- : current_slant(0), current_height(0), current_fill_color(0),
- current_glyph_color(0), nfont_positions(10), tbuf_len(0), begun_page(0),
- cur_div_level(0)
- {
- font_position = new symbol[nfont_positions];
- put("x T ");
- put(device);
- put('\n');
- put("x res ");
- put(units_per_inch);
- put(' ');
- put(hresolution);
- put(' ');
- put(vresolution);
- put('\n');
- put("x init\n");
- }
- /* output_file */
- output_file *the_output = 0;
- output_file::output_file()
- {
- }
- output_file::~output_file()
- {
- }
- void output_file::trailer(vunits)
- {
- }
- void output_file::put_filename(const char *)
- {
- }
- void output_file::on()
- {
- }
- void output_file::off()
- {
- }
- real_output_file::real_output_file()
- : printing(0), output_on(1)
- {
- #ifndef POPEN_MISSING
- if (pipe_command) {
- if ((fp = popen(pipe_command, POPEN_WT)) != 0) {
- piped = 1;
- return;
- }
- error("pipe open failed: %1", strerror(errno));
- }
- piped = 0;
- #endif /* not POPEN_MISSING */
- fp = stdout;
- }
- real_output_file::~real_output_file()
- {
- if (!fp)
- return;
- // To avoid looping, set fp to 0 before calling fatal().
- if (ferror(fp) || fflush(fp) < 0) {
- fp = 0;
- fatal("error writing output file");
- }
- #ifndef POPEN_MISSING
- if (piped) {
- int result = pclose(fp);
- fp = 0;
- if (result < 0)
- fatal("pclose failed");
- if (!WIFEXITED(result))
- error("output process `%1' got fatal signal %2",
- pipe_command,
- WIFSIGNALED(result) ? WTERMSIG(result) : WSTOPSIG(result));
- else {
- int exit_status = WEXITSTATUS(result);
- if (exit_status != 0)
- error("output process `%1' exited with status %2",
- pipe_command, exit_status);
- }
- }
- else
- #endif /* not POPEN MISSING */
- if (fclose(fp) < 0) {
- fp = 0;
- fatal("error closing output file");
- }
- }
- void real_output_file::flush()
- {
- if (fflush(fp) < 0)
- fatal("error writing output file");
- }
- int real_output_file::is_printing()
- {
- return printing;
- }
- void real_output_file::begin_page(int pageno, vunits page_length)
- {
- printing = in_output_page_list(pageno);
- if (printing)
- really_begin_page(pageno, page_length);
- }
- void real_output_file::copy_file(hunits x, vunits y, const char *filename)
- {
- if (printing && output_on)
- really_copy_file(x, y, filename);
- check_output_limits(x.to_units(), y.to_units());
- }
- void real_output_file::transparent_char(unsigned char c)
- {
- if (printing && output_on)
- really_transparent_char(c);
- }
- void real_output_file::print_line(hunits x, vunits y, node *n,
- vunits before, vunits after, hunits width)
- {
- if (printing)
- really_print_line(x, y, n, before, after, width);
- delete_node_list(n);
- }
- void real_output_file::really_copy_file(hunits, vunits, const char *)
- {
- // do nothing
- }
- void real_output_file::put_filename(const char *filename)
- {
- really_put_filename(filename);
- }
- void real_output_file::really_put_filename(const char *)
- {
- }
- void real_output_file::on()
- {
- really_on();
- if (output_on == 0)
- output_on = 1;
- }
- void real_output_file::off()
- {
- really_off();
- output_on = 0;
- }
- int real_output_file::is_on()
- {
- return output_on;
- }
- void real_output_file::really_on()
- {
- }
- void real_output_file::really_off()
- {
- }
- /* ascii_output_file */
- void ascii_output_file::really_transparent_char(unsigned char c)
- {
- putc(c, fp);
- }
- void ascii_output_file::really_print_line(hunits, vunits, node *n,
- vunits, vunits, hunits)
- {
- while (n != 0) {
- n->ascii_print(this);
- n = n->next;
- }
- fputc('\n', fp);
- }
- void ascii_output_file::really_begin_page(int /*pageno*/, vunits /*page_length*/)
- {
- fputs("<beginning of page>\n", fp);
- }
- ascii_output_file::ascii_output_file()
- {
- }
- /* suppress_output_file */
- suppress_output_file::suppress_output_file()
- {
- }
- void suppress_output_file::really_print_line(hunits, vunits, node *, vunits, vunits, hunits)
- {
- }
- void suppress_output_file::really_begin_page(int, vunits)
- {
- }
- void suppress_output_file::really_transparent_char(unsigned char)
- {
- }
- /* glyphs, ligatures, kerns, discretionary breaks */
- class charinfo_node : public node {
- protected:
- charinfo *ci;
- public:
- charinfo_node(charinfo *, statem *, int, node * = 0);
- int ends_sentence();
- int overlaps_vertically();
- int overlaps_horizontally();
- };
- charinfo_node::charinfo_node(charinfo *c, statem *s, int pop, node *x)
- : node(x, s, pop), ci(c)
- {
- }
- int charinfo_node::ends_sentence()
- {
- if (ci->ends_sentence())
- return 1;
- else if (ci->transparent())
- return 2;
- else
- return 0;
- }
- int charinfo_node::overlaps_horizontally()
- {
- return ci->overlaps_horizontally();
- }
- int charinfo_node::overlaps_vertically()
- {
- return ci->overlaps_vertically();
- }
- class glyph_node : public charinfo_node {
- static glyph_node *free_list;
- protected:
- tfont *tf;
- color *gcol;
- color *fcol; /* this is needed for grotty */
- #ifdef STORE_WIDTH
- hunits wid;
- glyph_node(charinfo *, tfont *, color *, color *, hunits,
- statem *, int, node * = 0);
- #endif
- public:
- void *operator new(size_t);
- void operator delete(void *);
- glyph_node(charinfo *, tfont *, color *, color *,
- statem *, int, node * = 0);
- ~glyph_node() {}
- node *copy();
- node *merge_glyph_node(glyph_node *);
- node *merge_self(node *);
- hunits width();
- node *last_char_node();
- units size();
- void vertical_extent(vunits *, vunits *);
- hunits subscript_correction();
- hunits italic_correction();
- hunits left_italic_correction();
- hunits skew();
- hyphenation_type get_hyphenation_type();
- tfont *get_tfont();
- color *get_glyph_color();
- color *get_fill_color();
- void tprint(troff_output_file *);
- void zero_width_tprint(troff_output_file *);
- hyphen_list *get_hyphen_list(hyphen_list *, int *);
- node *add_self(node *, hyphen_list **);
- void ascii_print(ascii_output_file *);
- void asciify(macro *);
- int character_type();
- int same(node *);
- const char *type();
- int force_tprint();
- int is_tag();
- void debug_node();
- };
- glyph_node *glyph_node::free_list = 0;
- class ligature_node : public glyph_node {
- node *n1;
- node *n2;
- #ifdef STORE_WIDTH
- ligature_node(charinfo *, tfont *, color *, color *, hunits,
- node *, node *, statem *, int, node * = 0);
- #endif
- public:
- void *operator new(size_t);
- void operator delete(void *);
- ligature_node(charinfo *, tfont *, color *, color *,
- node *, node *, statem *, int, node * = 0);
- ~ligature_node();
- node *copy();
- node *add_self(node *, hyphen_list **);
- hyphen_list *get_hyphen_list(hyphen_list *, int *);
- void ascii_print(ascii_output_file *);
- void asciify(macro *);
- int same(node *);
- const char *type();
- int force_tprint();
- int is_tag();
- };
- class kern_pair_node : public node {
- hunits amount;
- node *n1;
- node *n2;
- public:
- kern_pair_node(hunits, node *, node *, statem *, int, node * = 0);
- ~kern_pair_node();
- node *copy();
- node *merge_glyph_node(glyph_node *);
- node *add_self(node *, hyphen_list **);
- hyphen_list *get_hyphen_list(hyphen_list *, int *);
- node *add_discretionary_hyphen();
- hunits width();
- node *last_char_node();
- hunits italic_correction();
- hunits subscript_correction();
- void tprint(troff_output_file *);
- hyphenation_type get_hyphenation_type();
- int ends_sentence();
- void ascii_print(ascii_output_file *);
- void asciify(macro *);
- int same(node *);
- const char *type();
- int force_tprint();
- int is_tag();
- void vertical_extent(vunits *, vunits *);
- };
- class dbreak_node : public node {
- node *none;
- node *pre;
- node *post;
- public:
- dbreak_node(node *, node *, statem *, int, node * = 0);
- ~dbreak_node();
- node *copy();
- node *merge_glyph_node(glyph_node *);
- node *add_discretionary_hyphen();
- hunits width();
- node *last_char_node();
- hunits italic_correction();
- hunits subscript_correction();
- void tprint(troff_output_file *);
- breakpoint *get_breakpoints(hunits width, int ns, breakpoint *rest = 0,
- int is_inner = 0);
- int nbreaks();
- int ends_sentence();
- void split(int, node **, node **);
- hyphenation_type get_hyphenation_type();
- void ascii_print(ascii_output_file *);
- void asciify(macro *);
- int same(node *);
- const char *type();
- int force_tprint();
- int is_tag();
- };
- void *glyph_node::operator new(size_t n)
- {
- assert(n == sizeof(glyph_node));
- if (!free_list) {
- const int BLOCK = 1024;
- free_list = (glyph_node *)new char[sizeof(glyph_node)*BLOCK];
- for (int i = 0; i < BLOCK - 1; i++)
- free_list[i].next = free_list + i + 1;
- free_list[BLOCK-1].next = 0;
- }
- glyph_node *p = free_list;
- free_list = (glyph_node *)(free_list->next);
- p->next = 0;
- return p;
- }
- void *ligature_node::operator new(size_t n)
- {
- return new char[n];
- }
- void glyph_node::operator delete(void *p)
- {
- if (p) {
- ((glyph_node *)p)->next = free_list;
- free_list = (glyph_node *)p;
- }
- }
- void ligature_node::operator delete(void *p)
- {
- delete[] (char *)p;
- }
- glyph_node::glyph_node(charinfo *c, tfont *t, color *gc, color *fc,
- statem *s, int pop, node *x)
- : charinfo_node(c, s, pop, x), tf(t), gcol(gc), fcol(fc)
- {
- #ifdef STORE_WIDTH
- wid = tf->get_width(ci);
- #endif
- }
- #ifdef STORE_WIDTH
- glyph_node::glyph_node(charinfo *c, tfont *t,
- color *gc, color *fc, hunits w,
- statem *s, int pop, node *x)
- : charinfo_node(c, s, pop, x), tf(t), gcol(gc), fcol(fc), wid(w)
- {
- }
- #endif
- node *glyph_node::copy()
- {
- #ifdef STORE_WIDTH
- return new glyph_node(ci, tf, gcol, fcol, wid, state, div_nest_level);
- #else
- return new glyph_node(ci, tf, gcol, fcol, state, div_nest_level);
- #endif
- }
- node *glyph_node::merge_self(node *nd)
- {
- return nd->merge_glyph_node(this);
- }
- int glyph_node::character_type()
- {
- return tf->get_character_type(ci);
- }
- node *glyph_node::add_self(node *n, hyphen_list **p)
- {
- assert(ci->get_hyphenation_code() == (*p)->hyphenation_code);
- next = 0;
- node *nn;
- if (n == 0 || (nn = n->merge_glyph_node(this)) == 0) {
- next = n;
- nn = this;
- }
- if ((*p)->hyphen)
- nn = nn->add_discretionary_hyphen();
- hyphen_list *pp = *p;
- *p = (*p)->next;
- delete pp;
- return nn;
- }
- units glyph_node::size()
- {
- return tf->get_size().to_units();
- }
- hyphen_list *glyph_node::get_hyphen_list(hyphen_list *tail, int *count)
- {
- (*count)++;
- return new hyphen_list(ci->get_hyphenation_code(), tail);
- }
- tfont *node::get_tfont()
- {
- return 0;
- }
- tfont *glyph_node::get_tfont()
- {
- return tf;
- }
- color *node::get_glyph_color()
- {
- return 0;
- }
- color *glyph_node::get_glyph_color()
- {
- return gcol;
- }
- color *node::get_fill_color()
- {
- return 0;
- }
- color *glyph_node::get_fill_color()
- {
- return fcol;
- }
- node *node::merge_glyph_node(glyph_node *)
- {
- return 0;
- }
- node *glyph_node::merge_glyph_node(glyph_node *gn)
- {
- if (tf == gn->tf && gcol == gn->gcol && fcol == gn->fcol) {
- charinfo *lig;
- if ((lig = tf->get_lig(ci, gn->ci)) != 0) {
- node *next1 = next;
- next = 0;
- return new ligature_node(lig, tf, gcol, fcol, this, gn, state,
- gn->div_nest_level, next1);
- }
- hunits kern;
- if (tf->get_kern(ci, gn->ci, &kern)) {
- node *next1 = next;
- next = 0;
- return new kern_pair_node(kern, this, gn, state,
- gn->div_nest_level, next1);
- }
- }
- return 0;
- }
- #ifdef STORE_WIDTH
- inline
- #endif
- hunits glyph_node::width()
- {
- #ifdef STORE_WIDTH
- return wid;
- #else
- return tf->get_width(ci);
- #endif
- }
- node *glyph_node::last_char_node()
- {
- return this;
- }
- void glyph_node::vertical_extent(vunits *min, vunits *max)
- {
- *min = -tf->get_char_height(ci);
- *max = tf->get_char_depth(ci);
- }
- hunits glyph_node::skew()
- {
- return tf->get_char_skew(ci);
- }
- hunits glyph_node::subscript_correction()
- {
- return tf->get_subscript_correction(ci);
- }
- hunits glyph_node::italic_correction()
- {
- return tf->get_italic_correction(ci);
- }
- hunits glyph_node::left_italic_correction()
- {
- return tf->get_left_italic_correction(ci);
- }
- hyphenation_type glyph_node::get_hyphenation_type()
- {
- return HYPHEN_MIDDLE;
- }
- void glyph_node::ascii_print(ascii_output_file *ascii)
- {
- unsigned char c = ci->get_ascii_code();
- if (c != 0)
- ascii->outc(c);
- else
- ascii->outs(ci->nm.contents());
- }
- void glyph_node::debug_node()
- {
- unsigned char c = ci->get_ascii_code();
- fprintf(stderr, "{ %s [", type());
- if (c)
- fprintf(stderr, "%c", c);
- else
- fputs(ci->nm.contents(), stderr);
- if (push_state)
- fprintf(stderr, " <push_state>");
- if (state)
- state->display_state();
- fprintf(stderr, " nest level %d", div_nest_level);
- fprintf(stderr, "]}\n");
- fflush(stderr);
- }
- ligature_node::ligature_node(charinfo *c, tfont *t, color *gc, color *fc,
- node *gn1, node *gn2, statem *s,
- int pop, node *x)
- : glyph_node(c, t, gc, fc, s, pop, x), n1(gn1), n2(gn2)
- {
- }
- #ifdef STORE_WIDTH
- ligature_node::ligature_node(charinfo *c, tfont *t, color *gc, color *fc,
- hunits w, node *gn1, node *gn2, statem *s,
- int pop, node *x)
- : glyph_node(c, t, gc, fc, w, s, pop, x), n1(gn1), n2(gn2)
- {
- }
- #endif
- ligature_node::~ligature_node()
- {
- delete n1;
- delete n2;
- }
- node *ligature_node::copy()
- {
- #ifdef STORE_WIDTH
- return new ligature_node(ci, tf, gcol, fcol, wid, n1->copy(), n2->copy(),
- state, div_nest_level);
- #else
- return new ligature_node(ci, tf, gcol, fcol, n1->copy(), n2->copy(),
- state, div_nest_level);
- #endif
- }
- void ligature_node::ascii_print(ascii_output_file *ascii)
- {
- n1->ascii_print(ascii);
- n2->ascii_print(ascii);
- }
- hyphen_list *ligature_node::get_hyphen_list(hyphen_list *tail, int *count)
- {
- hyphen_list *hl = n2->get_hyphen_list(tail, count);
- return n1->get_hyphen_list(hl, count);
- }
- node *ligature_node::add_self(node *n, hyphen_list **p)
- {
- n = n1->add_self(n, p);
- n = n2->add_self(n, p);
- n1 = n2 = 0;
- delete this;
- return n;
- }
- kern_pair_node::kern_pair_node(hunits n, node *first, node *second,
- statem* s, int pop, node *x)
- : node(x, s, pop), amount(n), n1(first), n2(second)
- {
- }
- dbreak_node::dbreak_node(node *n, node *p, statem *s, int pop, node *x)
- : node(x, s, pop), none(n), pre(p), post(0)
- {
- }
- node *dbreak_node::merge_glyph_node(glyph_node *gn)
- {
- glyph_node *gn2 = (glyph_node *)gn->copy();
- node *new_none = none ? none->merge_glyph_node(gn) : 0;
- node *new_post = post ? post->merge_glyph_node(gn2) : 0;
- if (new_none == 0 && new_post == 0) {
- delete gn2;
- return 0;
- }
- if (new_none != 0)
- none = new_none;
- else {
- gn->next = none;
- none = gn;
- }
- if (new_post != 0)
- post = new_post;
- else {
- gn2->next = post;
- post = gn2;
- }
- return this;
- }
- node *kern_pair_node::merge_glyph_node(glyph_node *gn)
- {
- node *nd = n2->merge_glyph_node(gn);
- if (nd == 0)
- return 0;
- n2 = nd;
- nd = n2->merge_self(n1);
- if (nd) {
- nd->next = next;
- n1 = 0;
- n2 = 0;
- delete this;
- return nd;
- }
- return this;
- }
- hunits kern_pair_node::italic_correction()
- {
- return n2->italic_correction();
- }
- hunits kern_pair_node::subscript_correction()
- {
- return n2->subscript_correction();
- }
- void kern_pair_node::vertical_extent(vunits *min, vunits *max)
- {
- n1->vertical_extent(min, max);
- vunits min2, max2;
- n2->vertical_extent(&min2, &max2);
- if (min2 < *min)
- *min = min2;
- if (max2 > *max)
- *max = max2;
- }
- node *kern_pair_node::add_discretionary_hyphen()
- {
- tfont *tf = n2->get_tfo…