/contrib/groff/src/roff/troff/env.cpp
https://bitbucket.org/freebsd/freebsd-head/ · C++ · 3893 lines · 3496 code · 263 blank · 134 comment · 898 complexity · c07b344fa412b4f06e5daa000c8d6c87 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. */
- #include "troff.h"
- #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 "macropath.h"
- #include "input.h"
- #include <math.h>
- symbol default_family("T");
- enum { ADJUST_LEFT = 0, ADJUST_BOTH = 1, ADJUST_CENTER = 3, ADJUST_RIGHT = 5 };
- enum { HYPHEN_LAST_LINE = 2, HYPHEN_LAST_CHARS = 4, HYPHEN_FIRST_CHARS = 8 };
- struct env_list {
- environment *env;
- env_list *next;
- env_list(environment *e, env_list *p) : env(e), next(p) {}
- };
- env_list *env_stack;
- const int NENVIRONMENTS = 10;
- environment *env_table[NENVIRONMENTS];
- dictionary env_dictionary(10);
- environment *curenv;
- static int next_line_number = 0;
- extern int suppress_push;
- extern statem *get_diversion_state();
- charinfo *field_delimiter_char;
- charinfo *padding_indicator_char;
- int translate_space_to_dummy = 0;
- class pending_output_line {
- node *nd;
- int no_fill;
- int was_centered;
- vunits vs;
- vunits post_vs;
- hunits width;
- #ifdef WIDOW_CONTROL
- int last_line; // Is it the last line of the paragraph?
- #endif /* WIDOW_CONTROL */
- public:
- pending_output_line *next;
- pending_output_line(node *, int, vunits, vunits, hunits, int,
- pending_output_line * = 0);
- ~pending_output_line();
- int output();
- #ifdef WIDOW_CONTROL
- friend void environment::mark_last_line();
- friend void environment::output(node *, int, vunits, vunits, hunits, int);
- #endif /* WIDOW_CONTROL */
- };
- pending_output_line::pending_output_line(node *n, int nf, vunits v, vunits pv,
- hunits w, int ce,
- pending_output_line *p)
- : nd(n), no_fill(nf), was_centered(ce), vs(v), post_vs(pv), width(w),
- #ifdef WIDOW_CONTROL
- last_line(0),
- #endif /* WIDOW_CONTROL */
- next(p)
- {
- }
- pending_output_line::~pending_output_line()
- {
- delete_node_list(nd);
- }
- int pending_output_line::output()
- {
- if (trap_sprung_flag)
- return 0;
- #ifdef WIDOW_CONTROL
- if (next && next->last_line && !no_fill) {
- curdiv->need(vs + post_vs + vunits(vresolution));
- if (trap_sprung_flag) {
- next->last_line = 0; // Try to avoid infinite loops.
- return 0;
- }
- }
- #endif
- curenv->construct_format_state(nd, was_centered, !no_fill);
- curdiv->output(nd, no_fill, vs, post_vs, width);
- nd = 0;
- return 1;
- }
- void environment::output(node *nd, int no_fill_flag,
- vunits vs, vunits post_vs,
- hunits width, int was_centered)
- {
- #ifdef WIDOW_CONTROL
- while (pending_lines) {
- if (widow_control && !pending_lines->no_fill && !pending_lines->next)
- break;
- if (!pending_lines->output())
- break;
- pending_output_line *tem = pending_lines;
- pending_lines = pending_lines->next;
- delete tem;
- }
- #else /* WIDOW_CONTROL */
- output_pending_lines();
- #endif /* WIDOW_CONTROL */
- if (!trap_sprung_flag && !pending_lines
- #ifdef WIDOW_CONTROL
- && (!widow_control || no_fill_flag)
- #endif /* WIDOW_CONTROL */
- ) {
- curenv->construct_format_state(nd, was_centered, !no_fill_flag);
- curdiv->output(nd, no_fill_flag, vs, post_vs, width);
- } else {
- pending_output_line **p;
- for (p = &pending_lines; *p; p = &(*p)->next)
- ;
- *p = new pending_output_line(nd, no_fill_flag, vs, post_vs, width,
- was_centered);
- }
- }
- // a line from .tl goes at the head of the queue
- void environment::output_title(node *nd, int no_fill_flag,
- vunits vs, vunits post_vs,
- hunits width)
- {
- if (!trap_sprung_flag)
- curdiv->output(nd, no_fill_flag, vs, post_vs, width);
- else
- pending_lines = new pending_output_line(nd, no_fill_flag, vs, post_vs,
- width, 0, pending_lines);
- }
- void environment::output_pending_lines()
- {
- while (pending_lines && pending_lines->output()) {
- pending_output_line *tem = pending_lines;
- pending_lines = pending_lines->next;
- delete tem;
- }
- }
- #ifdef WIDOW_CONTROL
- void environment::mark_last_line()
- {
- if (!widow_control || !pending_lines)
- return;
- pending_output_line *p;
- for (p = pending_lines; p->next; p = p->next)
- ;
- if (!p->no_fill)
- p->last_line = 1;
- }
- void widow_control_request()
- {
- int n;
- if (has_arg() && get_integer(&n))
- curenv->widow_control = n != 0;
- else
- curenv->widow_control = 1;
- skip_line();
- }
- #endif /* WIDOW_CONTROL */
- /* font_size functions */
- size_range *font_size::size_table = 0;
- int font_size::nranges = 0;
- extern "C" {
- int compare_ranges(const void *p1, const void *p2)
- {
- return ((size_range *)p1)->min - ((size_range *)p2)->min;
- }
- }
- void font_size::init_size_table(int *sizes)
- {
- nranges = 0;
- while (sizes[nranges*2] != 0)
- nranges++;
- assert(nranges > 0);
- size_table = new size_range[nranges];
- for (int i = 0; i < nranges; i++) {
- size_table[i].min = sizes[i*2];
- size_table[i].max = sizes[i*2 + 1];
- }
- qsort(size_table, nranges, sizeof(size_range), compare_ranges);
- }
- font_size::font_size(int sp)
- {
- for (int i = 0; i < nranges; i++) {
- if (sp < size_table[i].min) {
- if (i > 0 && size_table[i].min - sp >= sp - size_table[i - 1].max)
- p = size_table[i - 1].max;
- else
- p = size_table[i].min;
- return;
- }
- if (sp <= size_table[i].max) {
- p = sp;
- return;
- }
- }
- p = size_table[nranges - 1].max;
- }
- int font_size::to_units()
- {
- return scale(p, units_per_inch, sizescale*72);
- }
- // we can't do this in a static constructor because various dictionaries
- // have to get initialized first
- void init_environments()
- {
- curenv = env_table[0] = new environment("0");
- }
- void tab_character()
- {
- curenv->tab_char = get_optional_char();
- skip_line();
- }
- void leader_character()
- {
- curenv->leader_char = get_optional_char();
- skip_line();
- }
- void environment::add_char(charinfo *ci)
- {
- int s;
- node *gc_np = 0;
- if (interrupted)
- ;
- // don't allow fields in dummy environments
- else if (ci == field_delimiter_char && !dummy) {
- if (current_field)
- wrap_up_field();
- else
- start_field();
- }
- else if (current_field && ci == padding_indicator_char)
- add_padding();
- else if (current_tab) {
- if (tab_contents == 0)
- tab_contents = new line_start_node;
- if (ci != hyphen_indicator_char)
- tab_contents = tab_contents->add_char(ci, this, &tab_width, &s, &gc_np);
- else
- tab_contents = tab_contents->add_discretionary_hyphen();
- }
- else {
- if (line == 0)
- start_line();
- #if 0
- fprintf(stderr, "current line is\n");
- line->debug_node_list();
- #endif
- if (ci != hyphen_indicator_char)
- line = line->add_char(ci, this, &width_total, &space_total, &gc_np);
- else
- line = line->add_discretionary_hyphen();
- }
- #if 0
- fprintf(stderr, "now after we have added character the line is\n");
- line->debug_node_list();
- #endif
- if ((!suppress_push) && gc_np) {
- if (gc_np && (gc_np->state == 0)) {
- gc_np->state = construct_state(0);
- gc_np->push_state = get_diversion_state();
- }
- else if (line && (line->state == 0)) {
- line->state = construct_state(0);
- line->push_state = get_diversion_state();
- }
- }
- #if 0
- fprintf(stderr, "now we have possibly added the state the line is\n");
- line->debug_node_list();
- #endif
- }
- node *environment::make_char_node(charinfo *ci)
- {
- return make_node(ci, this);
- }
- void environment::add_node(node *n)
- {
- if (n == 0)
- return;
- if (!suppress_push) {
- if (n->is_special && n->state == NULL)
- n->state = construct_state(0);
- n->push_state = get_diversion_state();
- }
- if (current_tab || current_field)
- n->freeze_space();
- if (interrupted) {
- delete n;
- }
- else if (current_tab) {
- n->next = tab_contents;
- tab_contents = n;
- tab_width += n->width();
- }
- else {
- if (line == 0) {
- if (discarding && n->discardable()) {
- // XXX possibly: input_line_start -= n->width();
- delete n;
- return;
- }
- start_line();
- }
- width_total += n->width();
- space_total += n->nspaces();
- n->next = line;
- line = n;
- construct_new_line_state(line);
- }
- }
- void environment::add_hyphen_indicator()
- {
- if (current_tab || interrupted || current_field
- || hyphen_indicator_char != 0)
- return;
- if (line == 0)
- start_line();
- line = line->add_discretionary_hyphen();
- }
- int environment::get_hyphenation_flags()
- {
- return hyphenation_flags;
- }
- int environment::get_hyphen_line_max()
- {
- return hyphen_line_max;
- }
- int environment::get_hyphen_line_count()
- {
- return hyphen_line_count;
- }
- int environment::get_center_lines()
- {
- return center_lines;
- }
- int environment::get_right_justify_lines()
- {
- return right_justify_lines;
- }
- void environment::add_italic_correction()
- {
- if (current_tab) {
- if (tab_contents)
- tab_contents = tab_contents->add_italic_correction(&tab_width);
- }
- else if (line)
- line = line->add_italic_correction(&width_total);
- }
- void environment::space_newline()
- {
- assert(!current_tab && !current_field);
- if (interrupted)
- return;
- hunits x = H0;
- hunits sw = env_space_width(this);
- hunits ssw = env_sentence_space_width(this);
- if (!translate_space_to_dummy) {
- x = sw;
- if (node_list_ends_sentence(line) == 1)
- x += ssw;
- }
- width_list *w = new width_list(sw, ssw);
- if (node_list_ends_sentence(line) == 1)
- w->next = new width_list(sw, ssw);
- if (line != 0 && line->merge_space(x, sw, ssw)) {
- width_total += x;
- return;
- }
- add_node(new word_space_node(x, get_fill_color(), w));
- possibly_break_line(0, spread_flag);
- spread_flag = 0;
- }
- void environment::space()
- {
- space(env_space_width(this), env_sentence_space_width(this));
- }
- void environment::space(hunits space_width, hunits sentence_space_width)
- {
- if (interrupted)
- return;
- if (current_field && padding_indicator_char == 0) {
- add_padding();
- return;
- }
- hunits x = translate_space_to_dummy ? H0 : space_width;
- node *p = current_tab ? tab_contents : line;
- hunits *tp = current_tab ? &tab_width : &width_total;
- if (p && p->nspaces() == 1 && p->width() == x
- && node_list_ends_sentence(p->next) == 1) {
- hunits xx = translate_space_to_dummy ? H0 : sentence_space_width;
- if (p->merge_space(xx, space_width, sentence_space_width)) {
- *tp += xx;
- return;
- }
- }
- if (p && p->merge_space(x, space_width, sentence_space_width)) {
- *tp += x;
- return;
- }
- add_node(new word_space_node(x,
- get_fill_color(),
- new width_list(space_width,
- sentence_space_width)));
- possibly_break_line(0, spread_flag);
- spread_flag = 0;
- }
- node *do_underline_special(int);
- void environment::set_font(symbol nm)
- {
- if (interrupted)
- return;
- if (nm == symbol("P") || nm.is_empty()) {
- if (family->make_definite(prev_fontno) < 0)
- return;
- int tem = fontno;
- fontno = prev_fontno;
- prev_fontno = tem;
- }
- else {
- prev_fontno = fontno;
- int n = symbol_fontno(nm);
- if (n < 0) {
- n = next_available_font_position();
- if (!mount_font(n, nm))
- return;
- }
- if (family->make_definite(n) < 0)
- return;
- fontno = n;
- }
- if (underline_spaces && fontno != prev_fontno) {
- if (fontno == get_underline_fontno())
- add_node(do_underline_special(1));
- if (prev_fontno == get_underline_fontno())
- add_node(do_underline_special(0));
- }
- }
- void environment::set_font(int n)
- {
- if (interrupted)
- return;
- if (is_good_fontno(n)) {
- prev_fontno = fontno;
- fontno = n;
- }
- else
- warning(WARN_FONT, "bad font number");
- }
- void environment::set_family(symbol fam)
- {
- if (interrupted)
- return;
- if (fam.is_null() || fam.is_empty()) {
- if (prev_family->make_definite(fontno) < 0)
- return;
- font_family *tem = family;
- family = prev_family;
- prev_family = tem;
- }
- else {
- font_family *f = lookup_family(fam);
- if (f->make_definite(fontno) < 0)
- return;
- prev_family = family;
- family = f;
- }
- }
- void environment::set_size(int n)
- {
- if (interrupted)
- return;
- if (n == 0) {
- font_size temp = prev_size;
- prev_size = size;
- size = temp;
- int temp2 = prev_requested_size;
- prev_requested_size = requested_size;
- requested_size = temp2;
- }
- else {
- prev_size = size;
- size = font_size(n);
- prev_requested_size = requested_size;
- requested_size = n;
- }
- }
- void environment::set_char_height(int n)
- {
- if (interrupted)
- return;
- if (n == requested_size || n <= 0)
- char_height = 0;
- else
- char_height = n;
- }
- void environment::set_char_slant(int n)
- {
- if (interrupted)
- return;
- char_slant = n;
- }
- color *environment::get_prev_glyph_color()
- {
- return prev_glyph_color;
- }
- color *environment::get_glyph_color()
- {
- return glyph_color;
- }
- color *environment::get_prev_fill_color()
- {
- return prev_fill_color;
- }
- color *environment::get_fill_color()
- {
- return fill_color;
- }
- void environment::set_glyph_color(color *c)
- {
- if (interrupted)
- return;
- curenv->prev_glyph_color = curenv->glyph_color;
- curenv->glyph_color = c;
- }
- void environment::set_fill_color(color *c)
- {
- if (interrupted)
- return;
- curenv->prev_fill_color = curenv->fill_color;
- curenv->fill_color = c;
- }
- environment::environment(symbol nm)
- : dummy(0),
- prev_line_length((units_per_inch*13)/2),
- line_length((units_per_inch*13)/2),
- prev_title_length((units_per_inch*13)/2),
- title_length((units_per_inch*13)/2),
- prev_size(sizescale*10),
- size(sizescale*10),
- requested_size(sizescale*10),
- prev_requested_size(sizescale*10),
- char_height(0),
- char_slant(0),
- space_size(12),
- sentence_space_size(12),
- adjust_mode(ADJUST_BOTH),
- fill(1),
- interrupted(0),
- prev_line_interrupted(0),
- center_lines(0),
- right_justify_lines(0),
- prev_vertical_spacing(points_to_units(12)),
- vertical_spacing(points_to_units(12)),
- prev_post_vertical_spacing(0),
- post_vertical_spacing(0),
- prev_line_spacing(1),
- line_spacing(1),
- prev_indent(0),
- indent(0),
- temporary_indent(0),
- have_temporary_indent(0),
- underline_lines(0),
- underline_spaces(0),
- input_trap_count(0),
- continued_input_trap(0),
- line(0),
- prev_text_length(0),
- width_total(0),
- space_total(0),
- input_line_start(0),
- line_tabs(0),
- current_tab(TAB_NONE),
- leader_node(0),
- tab_char(0),
- leader_char(charset_table['.']),
- current_field(0),
- discarding(0),
- spread_flag(0),
- margin_character_flags(0),
- margin_character_node(0),
- margin_character_distance(points_to_units(10)),
- numbering_nodes(0),
- number_text_separation(1),
- line_number_indent(0),
- line_number_multiple(1),
- no_number_count(0),
- hyphenation_flags(1),
- hyphen_line_count(0),
- hyphen_line_max(-1),
- hyphenation_space(H0),
- hyphenation_margin(H0),
- composite(0),
- pending_lines(0),
- #ifdef WIDOW_CONTROL
- widow_control(0),
- #endif /* WIDOW_CONTROL */
- glyph_color(&default_color),
- prev_glyph_color(&default_color),
- fill_color(&default_color),
- prev_fill_color(&default_color),
- seen_space(0),
- seen_eol(0),
- suppress_next_eol(0),
- seen_break(0),
- tabs(units_per_inch/2, TAB_LEFT),
- name(nm),
- control_char('.'),
- no_break_control_char('\''),
- hyphen_indicator_char(0)
- {
- prev_family = family = lookup_family(default_family);
- prev_fontno = fontno = 1;
- if (!is_good_fontno(1))
- fatal("font number 1 not a valid font");
- if (family->make_definite(1) < 0)
- fatal("invalid default family `%1'", default_family.contents());
- prev_fontno = fontno;
- }
- environment::environment(const environment *e)
- : dummy(1),
- prev_line_length(e->prev_line_length),
- line_length(e->line_length),
- prev_title_length(e->prev_title_length),
- title_length(e->title_length),
- prev_size(e->prev_size),
- size(e->size),
- requested_size(e->requested_size),
- prev_requested_size(e->prev_requested_size),
- char_height(e->char_height),
- char_slant(e->char_slant),
- prev_fontno(e->prev_fontno),
- fontno(e->fontno),
- prev_family(e->prev_family),
- family(e->family),
- space_size(e->space_size),
- sentence_space_size(e->sentence_space_size),
- adjust_mode(e->adjust_mode),
- fill(e->fill),
- interrupted(0),
- prev_line_interrupted(0),
- center_lines(0),
- right_justify_lines(0),
- prev_vertical_spacing(e->prev_vertical_spacing),
- vertical_spacing(e->vertical_spacing),
- prev_post_vertical_spacing(e->prev_post_vertical_spacing),
- post_vertical_spacing(e->post_vertical_spacing),
- prev_line_spacing(e->prev_line_spacing),
- line_spacing(e->line_spacing),
- prev_indent(e->prev_indent),
- indent(e->indent),
- temporary_indent(0),
- have_temporary_indent(0),
- underline_lines(0),
- underline_spaces(0),
- input_trap_count(0),
- continued_input_trap(0),
- line(0),
- prev_text_length(e->prev_text_length),
- width_total(0),
- space_total(0),
- input_line_start(0),
- line_tabs(e->line_tabs),
- current_tab(TAB_NONE),
- leader_node(0),
- tab_char(e->tab_char),
- leader_char(e->leader_char),
- current_field(0),
- discarding(0),
- spread_flag(0),
- margin_character_flags(e->margin_character_flags),
- margin_character_node(e->margin_character_node),
- margin_character_distance(e->margin_character_distance),
- numbering_nodes(0),
- number_text_separation(e->number_text_separation),
- line_number_indent(e->line_number_indent),
- line_number_multiple(e->line_number_multiple),
- no_number_count(e->no_number_count),
- hyphenation_flags(e->hyphenation_flags),
- hyphen_line_count(0),
- hyphen_line_max(e->hyphen_line_max),
- hyphenation_space(e->hyphenation_space),
- hyphenation_margin(e->hyphenation_margin),
- composite(0),
- pending_lines(0),
- #ifdef WIDOW_CONTROL
- widow_control(e->widow_control),
- #endif /* WIDOW_CONTROL */
- glyph_color(e->glyph_color),
- prev_glyph_color(e->prev_glyph_color),
- fill_color(e->fill_color),
- prev_fill_color(e->prev_fill_color),
- seen_space(e->seen_space),
- seen_eol(e->seen_eol),
- suppress_next_eol(e->suppress_next_eol),
- seen_break(e->seen_break),
- tabs(e->tabs),
- name(e->name), // so that eg `.if "\n[.ev]"0"' works
- control_char(e->control_char),
- no_break_control_char(e->no_break_control_char),
- hyphen_indicator_char(e->hyphen_indicator_char)
- {
- }
- void environment::copy(const environment *e)
- {
- prev_line_length = e->prev_line_length;
- line_length = e->line_length;
- prev_title_length = e->prev_title_length;
- title_length = e->title_length;
- prev_size = e->prev_size;
- size = e->size;
- prev_requested_size = e->prev_requested_size;
- requested_size = e->requested_size;
- char_height = e->char_height;
- char_slant = e->char_slant;
- space_size = e->space_size;
- sentence_space_size = e->sentence_space_size;
- adjust_mode = e->adjust_mode;
- fill = e->fill;
- interrupted = 0;
- prev_line_interrupted = 0;
- center_lines = 0;
- right_justify_lines = 0;
- prev_vertical_spacing = e->prev_vertical_spacing;
- vertical_spacing = e->vertical_spacing;
- prev_post_vertical_spacing = e->prev_post_vertical_spacing,
- post_vertical_spacing = e->post_vertical_spacing,
- prev_line_spacing = e->prev_line_spacing;
- line_spacing = e->line_spacing;
- prev_indent = e->prev_indent;
- indent = e->indent;
- have_temporary_indent = 0;
- temporary_indent = 0;
- underline_lines = 0;
- underline_spaces = 0;
- input_trap_count = 0;
- continued_input_trap = 0;
- prev_text_length = e->prev_text_length;
- width_total = 0;
- space_total = 0;
- input_line_start = 0;
- control_char = e->control_char;
- no_break_control_char = e->no_break_control_char;
- hyphen_indicator_char = e->hyphen_indicator_char;
- spread_flag = 0;
- line = 0;
- pending_lines = 0;
- discarding = 0;
- tabs = e->tabs;
- line_tabs = e->line_tabs;
- current_tab = TAB_NONE;
- current_field = 0;
- margin_character_flags = e->margin_character_flags;
- margin_character_node = e->margin_character_node;
- margin_character_distance = e->margin_character_distance;
- numbering_nodes = 0;
- number_text_separation = e->number_text_separation;
- line_number_multiple = e->line_number_multiple;
- line_number_indent = e->line_number_indent;
- no_number_count = e->no_number_count;
- tab_char = e->tab_char;
- leader_char = e->leader_char;
- hyphenation_flags = e->hyphenation_flags;
- fontno = e->fontno;
- prev_fontno = e->prev_fontno;
- dummy = e->dummy;
- family = e->family;
- prev_family = e->prev_family;
- leader_node = 0;
- #ifdef WIDOW_CONTROL
- widow_control = e->widow_control;
- #endif /* WIDOW_CONTROL */
- hyphen_line_max = e->hyphen_line_max;
- hyphen_line_count = 0;
- hyphenation_space = e->hyphenation_space;
- hyphenation_margin = e->hyphenation_margin;
- composite = 0;
- glyph_color= e->glyph_color;
- prev_glyph_color = e->prev_glyph_color;
- fill_color = e->fill_color;
- prev_fill_color = e->prev_fill_color;
- }
- environment::~environment()
- {
- delete leader_node;
- delete_node_list(line);
- delete_node_list(numbering_nodes);
- }
- hunits environment::get_input_line_position()
- {
- hunits n;
- if (line == 0)
- n = -input_line_start;
- else
- n = width_total - input_line_start;
- if (current_tab)
- n += tab_width;
- return n;
- }
- void environment::set_input_line_position(hunits n)
- {
- input_line_start = line == 0 ? -n : width_total - n;
- if (current_tab)
- input_line_start += tab_width;
- }
- hunits environment::get_line_length()
- {
- return line_length;
- }
- hunits environment::get_saved_line_length()
- {
- if (line)
- return target_text_length + saved_indent;
- else
- return line_length;
- }
- vunits environment::get_vertical_spacing()
- {
- return vertical_spacing;
- }
- vunits environment::get_post_vertical_spacing()
- {
- return post_vertical_spacing;
- }
- int environment::get_line_spacing()
- {
- return line_spacing;
- }
- vunits environment::total_post_vertical_spacing()
- {
- vunits tem(post_vertical_spacing);
- if (line_spacing > 1)
- tem += (line_spacing - 1)*vertical_spacing;
- return tem;
- }
- int environment::get_bold()
- {
- return get_bold_fontno(fontno);
- }
- hunits environment::get_digit_width()
- {
- return env_digit_width(this);
- }
- int environment::get_adjust_mode()
- {
- return adjust_mode;
- }
- int environment::get_fill()
- {
- return fill;
- }
- hunits environment::get_indent()
- {
- return indent;
- }
- hunits environment::get_saved_indent()
- {
- if (line)
- return saved_indent;
- else if (have_temporary_indent)
- return temporary_indent;
- else
- return indent;
- }
- hunits environment::get_temporary_indent()
- {
- return temporary_indent;
- }
- hunits environment::get_title_length()
- {
- return title_length;
- }
- node *environment::get_prev_char()
- {
- for (node *n = current_tab ? tab_contents : line; n; n = n->next) {
- node *last = n->last_char_node();
- if (last)
- return last;
- }
- return 0;
- }
- hunits environment::get_prev_char_width()
- {
- node *last = get_prev_char();
- if (!last)
- return H0;
- return last->width();
- }
- hunits environment::get_prev_char_skew()
- {
- node *last = get_prev_char();
- if (!last)
- return H0;
- return last->skew();
- }
- vunits environment::get_prev_char_height()
- {
- node *last = get_prev_char();
- if (!last)
- return V0;
- vunits min, max;
- last->vertical_extent(&min, &max);
- return -min;
- }
- vunits environment::get_prev_char_depth()
- {
- node *last = get_prev_char();
- if (!last)
- return V0;
- vunits min, max;
- last->vertical_extent(&min, &max);
- return max;
- }
- hunits environment::get_text_length()
- {
- hunits n = line == 0 ? H0 : width_total;
- if (current_tab)
- n += tab_width;
- return n;
- }
- hunits environment::get_prev_text_length()
- {
- return prev_text_length;
- }
- static int sb_reg_contents = 0;
- static int st_reg_contents = 0;
- static int ct_reg_contents = 0;
- static int rsb_reg_contents = 0;
- static int rst_reg_contents = 0;
- static int skw_reg_contents = 0;
- static int ssc_reg_contents = 0;
- void environment::width_registers()
- {
- // this is used to implement \w; it sets the st, sb, ct registers
- vunits min = 0, max = 0, cur = 0;
- int character_type = 0;
- ssc_reg_contents = line ? line->subscript_correction().to_units() : 0;
- skw_reg_contents = line ? line->skew().to_units() : 0;
- line = reverse_node_list(line);
- vunits real_min = V0;
- vunits real_max = V0;
- vunits v1, v2;
- for (node *tem = line; tem; tem = tem->next) {
- tem->vertical_extent(&v1, &v2);
- v1 += cur;
- if (v1 < real_min)
- real_min = v1;
- v2 += cur;
- if (v2 > real_max)
- real_max = v2;
- if ((cur += tem->vertical_width()) < min)
- min = cur;
- else if (cur > max)
- max = cur;
- character_type |= tem->character_type();
- }
- line = reverse_node_list(line);
- st_reg_contents = -min.to_units();
- sb_reg_contents = -max.to_units();
- rst_reg_contents = -real_min.to_units();
- rsb_reg_contents = -real_max.to_units();
- ct_reg_contents = character_type;
- }
- node *environment::extract_output_line()
- {
- if (current_tab)
- wrap_up_tab();
- node *n = line;
- line = 0;
- return n;
- }
- /* environment related requests */
- void environment_switch()
- {
- int pop = 0; // 1 means pop, 2 means pop but no error message on underflow
- if (curenv->is_dummy())
- error("can't switch environments when current environment is dummy");
- else if (!has_arg())
- pop = 1;
- else {
- symbol nm;
- if (!tok.delimiter()) {
- // It looks like a number.
- int n;
- if (get_integer(&n)) {
- if (n >= 0 && n < NENVIRONMENTS) {
- env_stack = new env_list(curenv, env_stack);
- if (env_table[n] == 0)
- env_table[n] = new environment(i_to_a(n));
- curenv = env_table[n];
- }
- else
- nm = i_to_a(n);
- }
- else
- pop = 2;
- }
- else {
- nm = get_long_name(1);
- if (nm.is_null())
- pop = 2;
- }
- if (!nm.is_null()) {
- environment *e = (environment *)env_dictionary.lookup(nm);
- if (!e) {
- e = new environment(nm);
- (void)env_dictionary.lookup(nm, e);
- }
- env_stack = new env_list(curenv, env_stack);
- curenv = e;
- }
- }
- if (pop) {
- if (env_stack == 0) {
- if (pop == 1)
- error("environment stack underflow");
- }
- else {
- int seen_space = curenv->seen_space;
- int seen_eol = curenv->seen_eol;
- int suppress_next_eol = curenv->suppress_next_eol;
- curenv = env_stack->env;
- curenv->seen_space = seen_space;
- curenv->seen_eol = seen_eol;
- curenv->suppress_next_eol = suppress_next_eol;
- env_list *tem = env_stack;
- env_stack = env_stack->next;
- delete tem;
- }
- }
- skip_line();
- }
- void environment_copy()
- {
- symbol nm;
- environment *e=0;
- tok.skip();
- if (!tok.delimiter()) {
- // It looks like a number.
- int n;
- if (get_integer(&n)) {
- if (n >= 0 && n < NENVIRONMENTS)
- e = env_table[n];
- else
- nm = i_to_a(n);
- }
- }
- else
- nm = get_long_name(1);
- if (!e && !nm.is_null())
- e = (environment *)env_dictionary.lookup(nm);
- if (e == 0) {
- error("No environment to copy from");
- return;
- }
- else
- curenv->copy(e);
- skip_line();
- }
- void fill_color_change()
- {
- symbol s = get_name();
- if (s.is_null())
- curenv->set_fill_color(curenv->get_prev_fill_color());
- else
- do_fill_color(s);
- skip_line();
- }
- void glyph_color_change()
- {
- symbol s = get_name();
- if (s.is_null())
- curenv->set_glyph_color(curenv->get_prev_glyph_color());
- else
- do_glyph_color(s);
- skip_line();
- }
- static symbol P_symbol("P");
- void font_change()
- {
- symbol s = get_name();
- int is_number = 1;
- if (s.is_null() || s == P_symbol) {
- s = P_symbol;
- is_number = 0;
- }
- else {
- for (const char *p = s.contents(); p != 0 && *p != 0; p++)
- if (!csdigit(*p)) {
- is_number = 0;
- break;
- }
- }
- if (is_number)
- curenv->set_font(atoi(s.contents()));
- else
- curenv->set_font(s);
- skip_line();
- }
- void family_change()
- {
- symbol s = get_name();
- curenv->set_family(s);
- skip_line();
- }
- void point_size()
- {
- int n;
- if (has_arg() && get_number(&n, 'z', curenv->get_requested_point_size())) {
- if (n <= 0)
- n = 1;
- curenv->set_size(n);
- }
- else
- curenv->set_size(0);
- skip_line();
- }
- void override_sizes()
- {
- int n = 16;
- int *sizes = new int[n];
- int i = 0;
- char *buf = read_string();
- if (!buf)
- return;
- char *p = strtok(buf, " \t");
- for (;;) {
- if (!p)
- break;
- int lower, upper;
- switch (sscanf(p, "%d-%d", &lower, &upper)) {
- case 1:
- upper = lower;
- // fall through
- case 2:
- if (lower <= upper && lower >= 0)
- break;
- // fall through
- default:
- warning(WARN_RANGE, "bad size range `%1'", p);
- return;
- }
- if (i + 2 > n) {
- int *old_sizes = sizes;
- sizes = new int[n*2];
- memcpy(sizes, old_sizes, n*sizeof(int));
- n *= 2;
- a_delete old_sizes;
- }
- sizes[i++] = lower;
- if (lower == 0)
- break;
- sizes[i++] = upper;
- p = strtok(0, " \t");
- }
- font_size::init_size_table(sizes);
- }
- void space_size()
- {
- int n;
- if (get_integer(&n)) {
- curenv->space_size = n;
- if (has_arg() && get_integer(&n))
- curenv->sentence_space_size = n;
- else
- curenv->sentence_space_size = curenv->space_size;
- }
- skip_line();
- }
- void fill()
- {
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- curenv->fill = 1;
- tok.next();
- }
- void no_fill()
- {
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- curenv->fill = 0;
- curenv->suppress_next_eol = 1;
- tok.next();
- }
- void center()
- {
- int n;
- if (!has_arg() || !get_integer(&n))
- n = 1;
- else if (n < 0)
- n = 0;
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- curenv->right_justify_lines = 0;
- curenv->center_lines = n;
- curdiv->modified_tag.incl(MTSM_CE);
- tok.next();
- }
- void right_justify()
- {
- int n;
- if (!has_arg() || !get_integer(&n))
- n = 1;
- else if (n < 0)
- n = 0;
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- curenv->center_lines = 0;
- curenv->right_justify_lines = n;
- curdiv->modified_tag.incl(MTSM_RJ);
- tok.next();
- }
- void line_length()
- {
- hunits temp;
- if (has_arg() && get_hunits(&temp, 'm', curenv->line_length)) {
- if (temp < H0) {
- warning(WARN_RANGE, "bad line length %1u", temp.to_units());
- temp = H0;
- }
- }
- else
- temp = curenv->prev_line_length;
- curenv->prev_line_length = curenv->line_length;
- curenv->line_length = temp;
- curdiv->modified_tag.incl(MTSM_LL);
- skip_line();
- }
- void title_length()
- {
- hunits temp;
- if (has_arg() && get_hunits(&temp, 'm', curenv->title_length)) {
- if (temp < H0) {
- warning(WARN_RANGE, "bad title length %1u", temp.to_units());
- temp = H0;
- }
- }
- else
- temp = curenv->prev_title_length;
- curenv->prev_title_length = curenv->title_length;
- curenv->title_length = temp;
- skip_line();
- }
- void vertical_spacing()
- {
- vunits temp;
- if (has_arg() && get_vunits(&temp, 'p', curenv->vertical_spacing)) {
- if (temp < V0) {
- warning(WARN_RANGE, "vertical spacing must not be negative");
- temp = vresolution;
- }
- }
- else
- temp = curenv->prev_vertical_spacing;
- curenv->prev_vertical_spacing = curenv->vertical_spacing;
- curenv->vertical_spacing = temp;
- skip_line();
- }
- void post_vertical_spacing()
- {
- vunits temp;
- if (has_arg() && get_vunits(&temp, 'p', curenv->post_vertical_spacing)) {
- if (temp < V0) {
- warning(WARN_RANGE,
- "post vertical spacing must be greater than or equal to 0");
- temp = V0;
- }
- }
- else
- temp = curenv->prev_post_vertical_spacing;
- curenv->prev_post_vertical_spacing = curenv->post_vertical_spacing;
- curenv->post_vertical_spacing = temp;
- skip_line();
- }
- void line_spacing()
- {
- int temp;
- if (has_arg() && get_integer(&temp)) {
- if (temp < 1) {
- warning(WARN_RANGE, "value %1 out of range: interpreted as 1", temp);
- temp = 1;
- }
- }
- else
- temp = curenv->prev_line_spacing;
- curenv->prev_line_spacing = curenv->line_spacing;
- curenv->line_spacing = temp;
- skip_line();
- }
- void indent()
- {
- hunits temp;
- if (has_arg() && get_hunits(&temp, 'm', curenv->indent)) {
- if (temp < H0) {
- warning(WARN_RANGE, "indent cannot be negative");
- temp = H0;
- }
- }
- else
- temp = curenv->prev_indent;
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- curenv->have_temporary_indent = 0;
- curenv->prev_indent = curenv->indent;
- curenv->indent = temp;
- curdiv->modified_tag.incl(MTSM_IN);
- tok.next();
- }
- void temporary_indent()
- {
- int err = 0;
- hunits temp;
- if (!get_hunits(&temp, 'm', curenv->get_indent()))
- err = 1;
- while (!tok.newline() && !tok.eof())
- tok.next();
- if (break_flag)
- curenv->do_break();
- if (temp < H0) {
- warning(WARN_RANGE, "total indent cannot be negative");
- temp = H0;
- }
- if (!err) {
- curenv->temporary_indent = temp;
- curenv->have_temporary_indent = 1;
- curdiv->modified_tag.incl(MTSM_TI);
- }
- tok.next();
- }
- node *do_underline_special(int underline_spaces)
- {
- macro m;
- m.append_str("x u ");
- m.append(underline_spaces + '0');
- return new special_node(m, 1);
- }
- void do_underline(int underline_spaces)
- {
- int n;
- if (!has_arg() || !get_integer(&n))
- n = 1;
- if (n <= 0) {
- if (curenv->underline_lines > 0) {
- curenv->prev_fontno = curenv->fontno;
- curenv->fontno = curenv->pre_underline_fontno;
- if (underline_spaces) {
- curenv->underline_spaces = 0;
- curenv->add_node(do_underline_special(0));
- }
- }
- curenv->underline_lines = 0;
- }
- else {
- curenv->underline_lines = n;
- curenv->pre_underline_fontno = curenv->fontno;
- curenv->fontno = get_underline_fontno();
- if (underline_spaces) {
- curenv->underline_spaces = 1;
- curenv->add_node(do_underline_special(1));
- }
- }
- skip_line();
- }
- void continuous_underline()
- {
- do_underline(1);
- }
- void underline()
- {
- do_underline(0);
- }
- void control_char()
- {
- curenv->control_char = '.';
- if (has_arg()) {
- if (tok.ch() == 0)
- error("bad control character");
- else
- curenv->control_char = tok.ch();
- }
- skip_line();
- }
- void no_break_control_char()
- {
- curenv->no_break_control_char = '\'';
- if (has_arg()) {
- if (tok.ch() == 0)
- error("bad control character");
- else
- curenv->no_break_control_char = tok.ch();
- }
- skip_line();
- }
- void margin_character()
- {
- while (tok.space())
- tok.next();
- charinfo *ci = tok.get_char();
- if (ci) {
- // Call tok.next() only after making the node so that
- // .mc \s+9\(br\s0 works.
- node *nd = curenv->make_char_node(ci);
- tok.next();
- if (nd) {
- delete curenv->margin_character_node;
- curenv->margin_character_node = nd;
- curenv->margin_character_flags = (MARGIN_CHARACTER_ON
- |MARGIN_CHARACTER_NEXT);
- hunits d;
- if (has_arg() && get_hunits(&d, 'm'))
- curenv->margin_character_distance = d;
- }
- }
- else {
- check_missing_character();
- curenv->margin_character_flags &= ~MARGIN_CHARACTER_ON;
- if (curenv->margin_character_flags == 0) {
- delete curenv->margin_character_node;
- curenv->margin_character_node = 0;
- }
- }
- skip_line();
- }
- void number_lines()
- {
- delete_node_list(curenv->numbering_nodes);
- curenv->numbering_nodes = 0;
- if (has_arg()) {
- node *nd = 0;
- for (int i = '9'; i >= '0'; i--) {
- node *tem = make_node(charset_table[i], curenv);
- if (!tem) {
- skip_line();
- return;
- }
- tem->next = nd;
- nd = tem;
- }
- curenv->numbering_nodes = nd;
- curenv->line_number_digit_width = env_digit_width(curenv);
- int n;
- if (!tok.delimiter()) {
- if (get_integer(&n, next_line_number)) {
- next_line_number = n;
- if (next_line_number < 0) {
- warning(WARN_RANGE, "negative line number");
- next_line_number = 0;
- }
- }
- }
- else
- while (!tok.space() && !tok.newline() && !tok.eof())
- tok.next();
- if (has_arg()) {
- if (!tok.delimiter()) {
- if (get_integer(&n)) {
- if (n <= 0) {
- warning(WARN_RANGE, "negative or zero line number multiple");
- }
- else
- curenv->line_number_multiple = n;
- }
- }
- else
- while (!tok.space() && !tok.newline() && !tok.eof())
- tok.next();
- if (has_arg()) {
- if (!tok.delimiter()) {
- if (get_integer(&n))
- curenv->number_text_separation = n;
- }
- else
- while (!tok.space() && !tok.newline() && !tok.eof())
- tok.next();
- if (has_arg() && !tok.delimiter() && get_integer(&n))
- curenv->line_number_indent = n;
- }
- }
- }
- skip_line();
- }
- void no_number()
- {
- int n;
- if (has_arg() && get_integer(&n))
- curenv->no_number_count = n > 0 ? n : 0;
- else
- curenv->no_number_count = 1;
- skip_line();
- }
- void no_hyphenate()
- {
- curenv->hyphenation_flags = 0;
- skip_line();
- }
- void hyphenate_request()
- {
- int n;
- if (has_arg() && get_integer(&n))
- curenv->hyphenation_flags = n;
- else
- curenv->hyphenation_flags = 1;
- skip_line();
- }
- void hyphen_char()
- {
- curenv->hyphen_indicator_char = get_optional_char();
- skip_line();
- }
- void hyphen_line_max_request()
- {
- int n;
- if (has_arg() && get_integer(&n))
- curenv->hyphen_line_max = n;
- else
- curenv->hyphen_line_max = -1;
- skip_line();
- }
- void environment::interrupt()
- {
- if (!dummy) {
- add_node(new transparent_dummy_node);
- interrupted = 1;
- }
- }
- void environment::newline()
- {
- int was_centered = 0;
- if (underline_lines > 0) {
- if (--underline_lines == 0) {
- prev_fontno = fontno;
- fontno = pre_underline_fontno;
- if (underline_spaces) {
- underline_spaces = 0;
- add_node(do_underline_special(0));
- }
- }
- }
- if (current_field)
- wrap_up_field();
- if (current_tab)
- wrap_up_tab();
- // strip trailing spaces
- while (line != 0 && line->discardable()) {
- width_total -= line->width();
- space_total -= line->nspaces();
- node *tem = line;
- line = line->next;
- delete tem;
- }
- node *to_be_output = 0;
- hunits to_be_output_width;
- prev_line_interrupted = 0;
- if (dummy)
- space_newline();
- else if (interrupted) {
- interrupted = 0;
- // see environment::final_break
- prev_line_interrupted = exit_started ? 2 : 1;
- }
- else if (center_lines > 0) {
- --center_lines;
- hunits x = target_text_length - width_total;
- if (x > H0)
- saved_indent += x/2;
- to_be_output = line;
- was_centered = 1;
- to_be_output_width = width_total;
- line = 0;
- }
- else if (right_justify_lines > 0) {
- --right_justify_lines;
- hunits x = target_text_length - width_total;
- if (x > H0)
- saved_indent += x;
- to_be_output = line;
- to_be_output_width = width_total;
- line = 0;
- }
- else if (fill)
- space_newline();
- else {
- to_be_output = line;
- to_be_output_width = width_total;
- line = 0;
- }
- input_line_start = line == 0 ? H0 : width_total;
- if (to_be_output) {
- if (is_html && !fill) {
- curdiv->modified_tag.incl(MTSM_EOL);
- if (suppress_next_eol)
- suppress_next_eol = 0;
- else
- seen_eol = 1;
- }
- output_line(to_be_output, to_be_output_width, was_centered);
- hyphen_line_count = 0;
- }
- if (input_trap_count > 0) {
- if (!(continued_input_trap && prev_line_interrupted))
- if (--input_trap_count == 0)
- spring_trap(input_trap);
- }
- }
- void environment::output_line(node *n, hunits width, int was_centered)
- {
- prev_text_length = width;
- if (margin_character_flags) {
- hunits d = line_length + margin_character_distance - saved_indent - width;
- if (d > 0) {
- n = new hmotion_node(d, get_fill_color(), n);
- width += d;
- }
- margin_character_flags &= ~MARGIN_CHARACTER_NEXT;
- node *tem;
- if (!margin_character_flags) {
- tem = margin_character_node;
- margin_character_node = 0;
- }
- else
- tem = margin_character_node->copy();
- tem->next = n;
- n = tem;
- width += tem->width();
- }
- node *nn = 0;
- while (n != 0) {
- node *tem = n->next;
- n->next = nn;
- nn = n;
- n = tem;
- }
- if (!saved_indent.is_zero())
- nn = new hmotion_node(saved_indent, get_fill_color(), nn);
- width += saved_indent;
- if (no_number_count > 0)
- --no_number_count;
- else if (numbering_nodes) {
- hunits w = (line_number_digit_width
- *(3+line_number_indent+number_text_separation));
- if (next_line_number % line_number_multiple != 0)
- nn = new hmotion_node(w, get_fill_color(), nn);
- else {
- hunits x = w;
- nn = new hmotion_node(number_text_separation * line_number_digit_width,
- get_fill_color(), nn);
- x -= number_text_separation*line_number_digit_width;
- char buf[30];
- sprintf(buf, "%3d", next_line_number);
- for (char *p = strchr(buf, '\0') - 1; p >= buf && *p != ' '; --p) {
- node *gn = numbering_nodes;
- for (int count = *p - '0'; count > 0; count--)
- gn = gn->next;
- gn = gn->copy();
- x -= gn->width();
- gn->next = nn;
- nn = gn;
- }
- nn = new hmotion_node(x, get_fill_color(), nn);
- }
- width += w;
- ++next_line_number;
- }
- output(nn, !fill, vertical_spacing, total_post_vertical_spacing(), width,
- was_centered);
- }
- void environment::start_line()
- {
- assert(line == 0);
- discarding = 0;
- line = new line_start_node;
- if (have_temporary_indent) {
- saved_indent = temporary_indent;
- have_temporary_indent = 0;
- }
- else
- saved_indent = indent;
- target_text_length = line_length - saved_indent;
- width_total = H0;
- space_total = 0;
- }
- hunits environment::get_hyphenation_space()
- {
- return hyphenation_space;
- }
- void hyphenation_space_request()
- {
- hunits n;
- if (get_hunits(&n, 'm')) {
- if (n < H0) {
- warning(WARN_RANGE, "hyphenation space cannot be negative");
- n = H0;
- }
- curenv->hyphenation_space = n;
- }
- skip_line();
- }
- hunits environment::get_hyphenation_margin()
- {
- return hyphenation_margin;
- }
- void hyphenation_margin_request()
- {
- hunits n;
- if (get_hunits(&n, 'm')) {
- if (n < H0) {
- warning(WARN_RANGE, "hyphenation margin cannot be negative");
- n = H0;
- }
- curenv->hyphenation_margin = n;
- }
- skip_line();
- }
- breakpoint *environment::choose_breakpoint()
- {
- hunits x = width_total;
- int s = space_total;
- node *n = line;
- breakpoint *best_bp = 0; // the best breakpoint so far
- int best_bp_fits = 0;
- while (n != 0) {
- x -= n->width();
- s -= n->nspaces();
- breakpoint *bp = n->get_breakpoints(x, s);
- while (bp != 0) {
- if (bp->width <= target_text_length) {
- if (!bp->hyphenated) {
- breakpoint *tem = bp->next;
- bp->next = 0;
- while (tem != 0) {
- breakpoint *tem1 = tem;
- tem = tem->next;
- delete tem1;
- }
- if (best_bp_fits
- // Decide whether to use the hyphenated breakpoint.
- && (hyphen_line_max < 0
- // Only choose the hyphenated breakpoint if it would not
- // exceed the maximum number of consecutive hyphenated
- // lines.
- || hyphen_line_count + 1 <= hyphen_line_max)
- && !(adjust_mode == ADJUST_BOTH
- // Don't choose the hyphenated breakpoint if the line
- // can be justified by adding no more than
- // hyphenation_space to any word space.
- ? (bp->nspaces > 0
- && (((target_text_length - bp->width
- + (bp->nspaces - 1)*hresolution)/bp->nspaces)
- <= hyphenation_space))
- // Don't choose the hyphenated breakpoint if the line
- // is no more than hyphenation_margin short.
- : target_text_length - bp->width <= hyphenation_margin)) {
- delete bp;
- return best_bp;
- }
- if (best_bp)
- delete best_bp;
- return bp;
- }
- else {
- if ((adjust_mode == ADJUST_BOTH
- ? hyphenation_space == H0
- : hyphenation_margin == H0)
- && (hyphen_line_max < 0
- || hyphen_line_count + 1 <= hyphen_line_max)) {
- // No need to consider a non-hyphenated breakpoint.
- if (best_bp)
- delete best_bp;
- breakpoint *tem = bp->next;
- bp->next = 0;
- while (tem != 0) {
- breakpoint *tem1 = tem;
- tem = tem->next;
- delete tem1;
- }
- return bp;
- }
- // It fits but it's hyphenated.
- if (!best_bp_fits) {
- if (best_bp)
- delete best_bp;
- best_bp = bp;
- bp = bp->next;
- best_bp_fits = 1;
- }
- else {
- breakpoint *tem = bp;
- bp = bp->next;
- delete tem;
- }
- }
- }
- else {
- if (best_bp)
- delete best_bp;
- best_bp = bp;
- bp = bp->next;
- }
- }
- n = n->next;
- }
- if (best_bp) {
- if (!best_bp_fits)
- output_warning(WARN_BREAK, "can't break line");
- return best_bp;
- }
- return 0;
- }
- void environment::hyphenate_line(int start_here)
- {
- assert(line != 0);
- hyphenation_type prev_type = line->get_hyphenation_type();
- node **startp;
- if (start_here)
- startp = &line;
- else
- for (startp = &line->next; *startp != 0; startp = &(*startp)->next) {
- hyphenation_type this_type = (*startp)->get_hyphenation_type();
- if (prev_type == HYPHEN_BOUNDARY && this_type == HYPHEN_MIDDLE)
- break;
- prev_type = this_type;
- }
- if (*startp == 0)
- return;
- node *tem = *startp;
- do {
- tem = tem->next;
- } while (tem != 0 && tem->get_hyphenation_type() == HYPHEN_MIDDLE);
- int inhibit = (tem != 0 && tem->get_hyphenation_type() == HYPHEN_INHIBIT);
- node *end = tem;
- hyphen_list *sl = 0;
- tem = *startp;
- node *forward = 0;
- int i = 0;
- while (tem != end) {
- sl = tem->get_hyphen_list(sl, &i);
- node *tem1 = tem;
- tem = tem->next;
- tem1->next = forward;
- forward = tem1;
- }
- if (!inhibit) {
- // this is for characters like hyphen and emdash
- int prev_code = 0;
- for (hyphen_list *h = sl; h; h = h->next) {
- h->breakable = (prev_code != 0
- && h->next != 0
- && h->next->hyphenation_code != 0);
- prev_code = h->hyphenation_code;
- }
- }
- if (hyphenation_flags != 0
- && !inhibit
- // this may not be right if we have extra space on this line
- && !((hyphenation_flags & HYPHEN_LAST_LINE)
- && (curdiv->distance_to_next_trap()
- <= vertical_spacing + total_post_vertical_spacing()))
- && i >= 4)
- hyphenate(sl, hyphenation_flags);
- while (forward != 0) {
- node *tem1 = forward;
- forward = forward->next;
- tem1->next = 0;
- tem = tem1->add_self(tem, &sl);
- }
- *startp = tem;
- }
- static node *node_list_reverse(node *n)
- {
- node *res = 0;
- while (n) {
- node *tem = n;
- n = n->next;
- tem->next = res;
- res = tem;
- }
- return res;
- }
- static void distribute_space(node *n, int nspaces, hunits desired_space,
- int force_reverse = 0)
- {
- static int reverse = 0;
- if (force_reverse || reverse)
- n = node_list_reverse(n);
- if (!force_reverse && nspaces > 0 && spread_limit >= 0
- && desired_space.to_units() > 0) {
- hunits em = curenv->get_size();
- double Ems = (double)desired_space.to_units() / nspaces
- / (em.is_zero() ? hresolution : em.to_units());
- if (Ems > spread_limit)
- output_warning(WARN_BREAK, "spreading %1m per space", Ems);
- }
- for (node *tem = n; tem; tem = tem->next)
- tem->spread_space(&nspaces, &desired_space);
- if (force_reverse || reverse)
- (void)node_list_reverse(n);
- if (!force_reverse)
- reverse = !reverse;
- assert(desired_space.is_zero() && nspaces == 0);
- }
- void environment::possibly_break_line(int start_here, int forced)
- {
- int was_centered = center_lines > 0;
- if (!fill || current_tab || current_field || dummy)
- return;
- while (line != 0
- && (forced
- // When a macro follows a paragraph in fill mode, the
- // current line should not be empty.
- || (width_total - line->width()) > target_text_length)) {
- hyphenate_line(start_here);
- breakpoint *bp = choose_breakpoint();
- if (bp == 0)
- // we'll find one eventually
- return;
- node *pre, *post;
- node **ndp = &line;
- while (*ndp != bp->nd)
- ndp = &(*ndp)->next;
- bp->nd->split(bp->index, &pre, &post);
- *ndp = post;
- hunits extra_space_width = H0;
- switch(adjust_mode) {
- case ADJUST_BOTH:
- if (bp->nspaces != 0)
- extra_space_width = target_text_length - bp->width;
- else if (bp->width > 0 && target_text_length > 0
- && target_text_length > bp->width)
- output_warning(WARN_BREAK, "cannot adjust line");
- break;
- case ADJUST_CENTER:
- saved_indent += (target_text_length - bp->width)/2;
- was_centered = 1;
- break;
- case ADJUST_RIGHT:
- saved_indent += target_text_length - bp->width;
- break;
- }
- distribute_space(pre, bp->nspaces, extra_space_width);
- hunits output_width = bp->width + extra_space_width;
- input_line_start -= output_width;
- if (bp->hyphenated)
- hyphen_line_count++;
- else
- hyphen_line_count = 0;
- delete bp;
- space_total = 0;
- width_total = 0;
- node *first_non_discardable = 0;
- node *tem;
- for (tem = line; tem != 0; tem = tem->next)
- if (!tem->discardable())
- first_non_discardable = tem;
- node *to_be_discarded;
- if (first_non_discardable) {
- to_be_discarded = first_non_discardable->next;
- first_non_discardable->next = 0;
- for (tem = line; tem != 0; tem = tem->next) {
- width_total += tem->width();
- space_total += tem->nspaces();
- }
- discarding = 0;
- }
- else {
- discarding = 1;
- to_be_discarded = line;
- line = 0;
- }
- // Do output_line() here so that line will be 0 iff the
- // the environment will be empty.
- output_line(pre, output_width, was_centered);
- while (to_be_discarded != 0) {
- tem = to_be_discarded;
- to_be_discarded = to_be_discarded->next;
- input_line_start -= tem->width();…