1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "paragraph_pimpl.h"
20 #include "bufferparams.h"
24 #include "lyxtextclasslist.h"
26 #include "support/LAssert.h"
31 using std::upper_bound;
32 using std::lower_bound;
34 extern int tex_code_break_column;
37 // Initialize static member.
38 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
39 // Initialization of the counter for the paragraph id's,
40 unsigned int Paragraph::Pimpl::paragraph_id = 0;
44 string special_phrases[][2] = {
47 { "LaTeX2e", "\\LaTeXe{}" },
48 { "LaTeX", "\\LaTeX{}" },
51 size_t phrases_nr = sizeof(special_phrases)/sizeof(special_phrases[0]);
56 Paragraph::Pimpl::Pimpl(Paragraph * owner)
64 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
65 : params(p.params), owner_(owner)
67 inset_owner = p.inset_owner;
69 fontlist = p.fontlist;
77 void Paragraph::Pimpl::clear()
83 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
86 text = par->pimpl_->text;
90 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
92 lyx::Assert(pos <= size());
93 // This is stronger, and I belive that this is the assertion
94 // that we should really use. (Lgb)
95 //Assert(pos < size());
97 // Then this has no meaning. (Lgb)
98 if (!size() || pos == size()) return '\0';
104 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
110 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
111 LyXFont const & font)
113 lyx::Assert(pos <= size());
115 // This is actually very common when parsing buffers (and
116 // maybe inserting ascii text)
118 // when appending characters, no need to update tables
120 owner_->setFont(pos, font);
124 text.insert(text.begin() + pos, c);
126 // Update the font table.
127 FontTable search_font(pos, LyXFont());
128 for (FontList::iterator it = lower_bound(fontlist.begin(),
130 search_font, matchFT());
131 it != fontlist.end(); ++it)
133 it->pos(it->pos() + 1);
136 // Update the inset table.
137 InsetTable search_inset(pos, 0);
138 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
139 owner_->insetlist.end(),
140 search_inset, matchIT());
141 it != owner_->insetlist.end(); ++it)
145 owner_->setFont(pos, font);
149 void Paragraph::Pimpl::insertInset(pos_type pos,
150 Inset * inset, LyXFont const & font)
153 lyx::Assert(pos <= size());
155 insertChar(pos, META_INSET, font);
156 lyx::Assert(text[pos] == META_INSET);
158 // Add a new entry in the inset table.
159 InsetTable search_inset(pos, 0);
160 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
161 owner_->insetlist.end(),
162 search_inset, matchIT());
163 if (it != owner_->insetlist.end() && it->pos == pos) {
164 lyxerr << "ERROR (Paragraph::InsertInset): "
165 "there is an inset in position: " << pos << endl;
167 owner_->insetlist.insert(it, InsetTable(pos, inset));
168 inset->parOwner(owner_);
172 inset->setOwner(inset_owner);
176 void Paragraph::Pimpl::erase(pos_type pos)
178 lyx::Assert(pos < size());
179 // if it is an inset, delete the inset entry
180 if (text[pos] == Paragraph::META_INSET) {
182 InsetTable search_inset(pos, 0);
183 InsetList::iterator it =
184 lower_bound(owner_->insetlist.begin(),
185 owner_->insetlist.end(),
186 search_inset, matchIT());
187 if (it != owner_->insetlist.end() && it->pos == pos) {
189 owner_->insetlist.erase(it);
193 text.erase(text.begin() + pos);
195 // Erase entries in the tables.
196 FontTable search_font(pos, LyXFont());
198 FontList::iterator it =
199 lower_bound(fontlist.begin(),
201 search_font, matchFT());
202 if (it != fontlist.end() && it->pos() == pos &&
204 (it != fontlist.begin()
205 && boost::prior(it)->pos() == pos - 1))) {
206 // If it is a multi-character font
207 // entry, we just make it smaller
208 // (see update below), otherwise we
210 unsigned int const i = it - fontlist.begin();
211 fontlist.erase(fontlist.begin() + i);
212 it = fontlist.begin() + i;
213 if (i > 0 && i < fontlist.size() &&
214 fontlist[i - 1].font() == fontlist[i].font()) {
215 fontlist.erase(fontlist.begin() + i - 1);
216 it = fontlist.begin() + i - 1;
220 // Update all other entries.
221 FontList::iterator fend = fontlist.end();
222 for (; it != fend; ++it)
223 it->pos(it->pos() - 1);
225 // Update the inset table.
226 InsetTable search_inset(pos, 0);
227 InsetList::iterator lend = owner_->insetlist.end();
228 for (InsetList::iterator it =
229 upper_bound(owner_->insetlist.begin(),
231 search_inset, matchIT());
237 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
239 int & column, LyXFont const & font,
240 LyXLayout const & style)
242 if (style.pass_thru) return;
243 if (column > tex_code_break_column
245 && getChar(i - 1) != ' '
247 // same in FreeSpacing mode
248 && !style.free_spacing
249 && !owner_->isFreeSpacing()
250 // In typewriter mode, we want to avoid
251 // ! . ? : at the end of a line
252 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
253 && (getChar(i - 1) == '.'
254 || getChar(i - 1) == '?'
255 || getChar(i - 1) == ':'
256 || getChar(i - 1) == '!'))) {
257 if (tex_code_break_column == 0) {
258 // in batchmode we need LaTeX to still
259 // see it as a space not as an extra '\n'
265 texrow.start(owner_, i + 1);
267 } else if (style.free_spacing) {
275 bool Paragraph::Pimpl::isTextAt(BufferParams const & bp,
276 string const & str, pos_type pos)
278 LyXFont const & font = owner_->getFont(bp, pos);
280 for (string::size_type i = 0; i < str.length(); ++i) {
281 if (pos + static_cast<pos_type>(i) >= size())
283 if (str[i] != getChar(pos + i))
285 if (owner_->getFont(bp, pos + i) != font)
292 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
293 BufferParams const & bparams,
298 LyXFont & running_font,
301 LyXLayout const & style,
306 if (style.pass_thru) {
307 if (c != '\0') os << c;
310 // Two major modes: LaTeX or plain
311 // Handle here those cases common to both modes
312 // and then split to handle the two modes separately.
314 case Paragraph::META_INSET: {
315 Inset * inset = owner_->getInset(i);
318 int const len = os.tellp();
319 //ostream::pos_type const len = os.tellp();
320 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
321 || inset->lyxCode() == Inset::MATH_CODE
322 || inset->lyxCode() == Inset::URL_CODE)
323 && running_font.isRightToLeft()) {
328 int tmp = inset->latex(buf, os, moving_arg,
335 for (int j = 0; j < tmp; ++j) {
338 texrow.start(owner_, i + 1);
341 column += int(os.tellp()) - len;
347 case Paragraph::META_NEWLINE:
349 column += running_font.latexWriteEndChanges(os,
354 basefont = owner_->getLayoutFont(bparams);
355 running_font = basefont;
358 case Paragraph::META_HFILL:
364 // And now for the special cases within each mode
368 os << "\\textbackslash{}";
372 case '±': case '²': case '³':
373 case '×': case '÷': case '¹':
375 if ((bparams.inputenc == "latin1" ||
376 bparams.inputenc == "latin9") ||
377 (bparams.inputenc == "auto" &&
378 (font.language()->encoding()->LatexName()
380 font.language()->encoding()->LatexName()
382 os << "\\ensuremath{"
391 case '|': case '<': case '>':
392 // In T1 encoding, these characters exist
393 if (lyxrc.fontenc == "T1") {
395 //... but we should avoid ligatures
396 if ((c == '>' || c == '<')
398 && getChar(i + 1) == c) {
399 //os << "\\textcompwordmark{}";
400 // Jean-Marc, have a look at
401 // this. I think this works
409 // Typewriter font also has them
410 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
414 // Otherwise, we use what LaTeX
418 os << "\\textless{}";
422 os << "\\textgreater{}";
432 case '-': // "--" in Typewriter mode -> "-{}-"
434 && getChar(i + 1) == '-'
435 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
444 os << "\\char`\\\"{}";
449 if (bparams.inputenc == "default") {
458 case '%': case '#': case '{':
465 os << "\\textasciitilde{}";
470 os << "\\textasciicircum{}";
474 case '*': case '[': case ']':
475 // avoid being mistaken for optional arguments
476 os << '{' << c << '}';
481 // Blanks are printed before font switching.
482 // Sure? I am not! (try nice-latex)
483 // I am sure it's correct. LyX might be smarter
484 // in the future, but for now, nothing wrong is
490 // I assume this is hack treating typewriter as verbatim
491 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
500 // FIXME: if we have "LaTeX" with a font change in the middle (before
501 // the 'T', then the "TeX" part is still special cased. Really we
502 // should only operate this on "words" for some definition of word
506 for (; pnr < phrases_nr; ++pnr) {
507 if (isTextAt(bparams, special_phrases[pnr][0], i)) {
508 os << special_phrases[pnr][1];
509 i += special_phrases[pnr][0].length() - 1;
510 column += special_phrases[pnr][1].length() - 1;
515 if (pnr == phrases_nr && c != '\0') {
525 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
526 BufferParams const & bparams,
527 ostream & os, TexRow & texrow)
529 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
530 Paragraph * par = owner_;
532 while (par && par->params().depth() == owner_->params().depth()) {
533 if (textclasslist[bparams.textclass][par->layout()].isEnvironment()) {
534 par = par->TeXEnvironment(buf, bparams,
537 par = par->TeXOnePar(buf, bparams,
541 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
547 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
549 InsetList::const_iterator cit = owner_->insetlist.begin();
550 InsetList::const_iterator lend = owner_->insetlist.end();
552 for (; cit != lend; ++cit) {
553 if ((result = cit->inset->getParFromID(id)))
560 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
561 BufferParams const & bparams) const
563 LyXFont tmpfont(font);
565 // check for environment font information
566 char par_depth = owner_->getDepth();
567 Paragraph const * par = owner_;
568 LyXTextClass const & tclass = textclasslist[bparams.textclass];
570 while (par && par->getDepth() && !tmpfont.resolved()) {
571 par = par->outerHook();
573 tmpfont.realize(tclass[par->layout()].font
574 #ifdef INHERIT_LANGUAGE
578 par_depth = par->getDepth();
582 tmpfont.realize(tclass.defaultfont()
583 #ifdef INHERIT_LANGUAGE