string uni_upcase_all (string s) { string r; int i=0, n=N(s); while (i<n) { int start= i; tm_char_forwards (s, i); r << uni_upcase_char (s (start, i)); } return r; }
void poor_bbb_font_rep::adjust_xpositions (string s, SI* xpos) { SI dx= 0; int i=0; while (i < N(s)) { int start= i; tm_char_forwards (s, i); dx += fat; for (int k=start+1; k<=i; k++) xpos[k] += dx; } }
array<int> ad_hoc_language_rep::get_hyphens (string s) { if (hyphens->contains (s)) { string h= hyphens[s]; array<int> penalty; for (int i=0; i<N(h); tm_char_forwards (h, i)) if (h[i] != '-') penalty << HYPH_INVALID; else if (N(penalty)>0) penalty[N(penalty)-1]= 0; return penalty; } else return base->get_hyphens (s); }
static string rewrite_math (string s) { string r; int i= 0, n= N(s); while (i < n) { int start= i; tm_char_forwards (s, i); if (s[start] == '<' && start+1 < n && s[start+1] == '#' && s[i-1] == '>') r << utf8_to_cork (cork_to_utf8 (s (start, i))); else r << s (start, i); } return r; }
static string rewrite_letters (string s) { init_unicode_substitution (); string r; int i= 0, n= N(s); while (i < n) { int start= i; tm_char_forwards (s, i); string ss= s (start, i); if (substitution_char->contains (ss)) r << substitution_char[ss]; else r << ss; } return r; }
string uni_translit (string s) { translit_init (); string r; int i=0, n=N(s); while (i<n) { int start= i; tm_char_forwards (s, i); string ss= s (start, i); if (translit_table->contains (ss)) r << translit_table [ss]; else r << ss; } return r; }
void poor_bbb_font_rep::draw_fixed (renderer ren, string s, SI x, SI y, SI* xpos) { int i=0; while (i < N(s)) { int start= i; tm_char_forwards (s, i); string ss= s (start, i); font_metric fnm; font_glyphs fng; int c= index_glyph (ss, fnm, fng); if (c >= 0) ren->draw (c, fng, start==0? x: x + xpos[start], y); } }
text_property oriental_language_rep::advance (tree t, int& pos) { string s= t->label; if (pos == N(s)) return &tp_normal_rep; if (s[pos] == ' ') { pos++; return &tp_space_rep; } if (pos < N(s) && !test (s, pos, "<#")) { while (pos < N(s) && s[pos] != ' ' && !test (s, pos, "<#")) tm_char_forwards (s, pos); return &tp_cjk_no_break_rep; } int start= pos; tm_char_forwards (s, pos); string c= s (start, pos); int next= pos; tm_char_forwards (s, next); string x= s (pos, next); if (punct->contains (c)) { if (punct->contains (x) || pos == N(s)) return &tp_cjk_no_break_period_rep; else if (wide_punct->contains (c)) return &tp_cjk_wide_period_rep; else return &tp_cjk_period_rep; } else { if (punct->contains (x) || pos == N(s)) return &tp_cjk_no_break_rep; else return &tp_cjk_normal_rep; } }
void poor_smallcaps_font_rep::advance (string s, int& pos, string& r, int& nr) { int start= pos; nr= -1; while (pos < N(s)) { int end= pos; tm_char_forwards (s, end); string c1= s (pos, end); string c2= uni_upcase_char (c1); int next= ((c1 != c2)? 1: 0); if (next == nr) pos= end; else if (nr == -1) { pos= end; nr= next; } else break; } r= s (start, pos); if (nr == 1) r= uni_upcase_all (r); }
void edit_select_rep::select_enlarge_text () { path p= common (start_p, end_p); if (start_p == end_p) p= path_up (p); tree st= subtree (et, p); ASSERT (is_atomic (st), "non textual tree"); string s= st->label; string mode= get_env_string (MODE); int i1= last_item (start_p), j1= i1; int i2= last_item (end_p), j2= i2; path q= path_up (p); if (mode == "text" || mode == "src") { int i, f= 4; if (i1 > 0) { i= i1; tm_char_backwards (s, i); f= min (f, breaking_force (s[i])); } if (i2 < N(s)) f= min (f, breaking_force (s[i2])); while (i1 > 0) { i= i1; tm_char_backwards (s, i); if (breaking_force (s[i]) > f) break; i1= i; } while (i2 < N(s)) { if (breaking_force (s[i2]) > f) break; tm_char_forwards (s, i2); } if (i1 < i2 && (i1 != j1 || i2 != j2)) { if (is_concat (subtree (et, q)) && i1 == 0 && i2 == N(s)) select (q * 0, q * 1); else select (p * i1, p * i2); return; } } if (is_concat (subtree (et, q)) || (i1 == 0 && i2 == N(s))) select (q * 0, q * 1); else select (p * 0, p * N(s)); }
void smart_font_rep::advance (string s, int& pos, string& r, int& nr) { int* chv= sm->chv; hashmap<string,int>& cht (sm->cht); int start= pos; nr= -1; while (pos < N(s)) { if (s[pos] != '<') { int c= (int) (unsigned char) s[pos]; int next= chv[c]; if (math_kind != 0 && math_kind != 2 && is_letter (c) && (pos == 0 || !is_letter (s[pos-1])) && (pos+1 == N(s) || !is_letter (s[pos+1]))) next= italic_nr; else if (chv[c] == -1) next= resolve (s (pos, pos+1)); if (next == nr) pos++; else if (nr == -1) { pos++; nr= next; } else break; } else { int end= pos; tm_char_forwards (s, end); int next= cht[s (pos, end)]; if (next == -1) next= resolve (s (pos, end)); if (next == nr) pos= end; else if (nr == -1) { pos= end; nr= next; } else break; } } r= s (start, pos); if (nr < 0) return; if (N(fn) <= nr || is_nil (fn[nr])) initialize_font (nr); if (sm->fn_rewr[nr] != REWRITE_NONE) r= rewrite (r, sm->fn_rewr[nr]); //cout << "Got " << r << " in " << fn[nr]->res_name << "\n"; }
void packrat_parser_rep::serialize (tree t, path p) { if (is_nil (p) || p->item != -1) current_start (p)= N(current_string); if (is_atomic (t)) { int begin= N(current_string); int pos=0; string s= t->label; while (true) { if (N(current_string) != begin + pos) if (is_nil (p) || p->item != -1) { //cout << p * pos << " <-> " << N(current_string) << LF; //cout << " " << s (0, pos) << LF; //cout << " " << current_string (begin, N(current_string)) << LF; current_path_pos (p * pos)= N(current_string); current_pos_path (N(current_string))= p * pos; } if (pos >= N(s)) break; int start= pos; tm_char_forwards (s, pos); if (pos == start+1) current_string << s[start]; else { // FIXME: where did we use this kind of syntactical substitutions? tree r (UNINIT); string ss= s (start, pos); if (N(ss) != 1 && existing_tree_label (ss)) r= the_drd->get_syntax (as_tree_label (ss)); //if (r != UNINIT) cout << "Rewrite " << ss << " -> " << r << "\n"; if (r == UNINIT) current_string << ss; else serialize (r, path (-1)); } } } else switch (L(t)) { case RAW_DATA: current_string << "<\\rawdata></>"; break; case DOCUMENT: case PARA: for (int i=0; i<N(t); i++) { serialize (t[i], p * i); current_string << "\n"; } break; case SURROUND: serialize (t[0], p * 0); serialize (t[2], p * 2); serialize (t[1], p * 1); break; case CONCAT: for (int i=0; i<N(t); i++) serialize (t[i], p * i); break; case RIGID: serialize (t[0], p * 0); break; case HIDDEN: break; case FREEZE: case UNFREEZE: serialize (t[0], p * 0); break; case HSPACE: case VAR_VSPACE: case VSPACE: case SPACE: case HTAB: break; case MOVE: case SHIFT: case RESIZE: case CLIPPED: serialize (t[0], p * 0); break; case WITH_LIMITS: case LINE_BREAK: case NEW_LINE: case NEXT_LINE: case NO_BREAK: case YES_INDENT: case NO_INDENT: case VAR_YES_INDENT: case VAR_NO_INDENT: case VAR_PAGE_BREAK: case PAGE_BREAK: case VAR_NO_PAGE_BREAK: case NO_PAGE_BREAK: case VAR_NO_BREAK_HERE: case NO_BREAK_HERE: case NO_BREAK_START: case NO_BREAK_END: case VAR_NEW_PAGE: case NEW_PAGE: case VAR_NEW_DPAGE: case NEW_DPAGE: break; case WIDE: case NEG: serialize (t[0], p * 0); break; case SYNTAX: serialize (t[1], path (-1)); break; case TFORMAT: case TWITH: case CWITH: case CELL: serialize (t[N(t)-1], p * (N(t)-1)); break; case WITH: if (t[0] == "mode" && t[1] != "math"); else serialize (t[N(t)-1], p * (N(t)-1)); break; case VALUE: { tree r= the_drd->get_syntax (t); //if (r != UNINIT) cout << "Rewrite " << t << " -> " << r << "\n"; if (r != UNINIT) serialize (r, path (-1)); else serialize_compound (t, p); break; } case STYLE_WITH: case VAR_STYLE_WITH: case LOCUS: serialize (t[N(t)-1], p * (N(t)-1)); break; case ID: case HARD_ID: case LINK: case URL: case SCRIPT: case OBSERVER: case FIND_ACCESSIBLE: break; case HLINK: case ACTION: serialize (t[0], p * 0); break; case SET_BINDING: case GET_BINDING: case LABEL: case REFERENCE: case PAGEREF: case GET_ATTACHMENT: case WRITE: case TOC_NOTIFY: break; case SPECIFIC: serialize (t[1], p * 1); break; case FLAG: break; case HYPHENATE_AS: serialize (t[1], p * 1); break; default: serialize_compound (t, p); break; } if (is_nil (p) || p->item != -1) current_end (p)= N(current_string); }
static tree move_brackets_sub (tree t, bool in) { //cout << t << INDENT << LF; if (is_compound (t)) { int i, n= N(t); tree r= tree (t, n); for (i=0; i<n; i++) r[i]= move_brackets_sub (t[i], in); t= r; } while (true) { tree r= t; bool search= true; if (is_concat (t)) for (int i=0; i<N(t) && search; i++) if (is_compound (t[i], "math")) { array<tree> a= concat_tokenize (t[i][0]); for (int j=0; j<N(a) && search; j++) if (is_atomic (a[j]) && is_simple_opening (a[j]->label)) for (int k= i+1; k<N(t) && search; k++) if (is_atomic (t[k])) { string s= t[k]->label; for (int l=0; l<N(s) && search; tm_char_forwards (s, l)) if (is_simple_matching (a[j]->label, s (l, l+1))) { if (k == i+1 && l == 0 && in) { array<tree> c= concat_decompose (t); a << tree (s (0, 1)); c[i]= compound ("math", concat_recompose (a)); c[i]= upgrade_brackets (c[i]); c[i+1]= s (1, N(s)); r= move_brackets_sub (concat_recompose (c), in); search= false; } else if (j == 0 && !in) { tree x= a[0]; array<tree> c= concat_decompose (t); a= range (a, 1, N(a)); c[i]= compound ("math", concat_recompose (a)); c= append (range (c, 0, i), append (x, range (c, i, N(c)))); r= move_brackets_sub (concat_recompose (c), in); search= false; } } } for (int j=N(a)-1; j>=0 && search; j--) if (is_atomic (a[j]) && is_simple_closing (a[j]->label)) for (int k= i-1; k>=0 && search; k--) if (is_atomic (t[k])) { string s= t[k]->label; for (int l=N(s); l>0 && search; tm_char_backwards (s, l)) if (is_simple_matching (s (l-1, l), a[j]->label)) { if (k == i-1 && l == N(s) && in) { array<tree> c= concat_decompose (t); a= append (tree (s (l-1, l)), a); c[i]= compound ("math", concat_recompose (a)); c[i]= upgrade_brackets (c[i]); c[i-1]= s (0, l-1); r= move_brackets_sub (concat_recompose (c), in); search= false; } else if (j == N(a)-1 && !in) { tree x= a[j]; array<tree> c= concat_decompose (t); a= range (a, 0, j); c[i]= compound ("math", concat_recompose (a)); c= append (range (c, 0, i+1), append (x, range (c, i+1, N(c)))); r= move_brackets_sub (concat_recompose (c), in); search= false; } } } } if (search) break; else { //cout << "< " << t << LF; //cout << "> " << r << LF; t= r; } } //cout << UNINDENT << "Done" << LF; return t; }