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 // This is in the critical path for loading!
93 pos_type const siz = size();
94 lyx::Assert(pos <= siz);
95 // This is stronger, and I belive that this is the assertion
96 // that we should really use. (Lgb)
97 //Assert(pos < size());
99 // Then this has no meaning. (Lgb)
100 if (!siz || pos == siz)
107 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
113 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
114 LyXFont const & font)
116 lyx::Assert(pos <= size());
118 // This is actually very common when parsing buffers (and
119 // maybe inserting ascii text)
121 // when appending characters, no need to update tables
123 owner_->setFont(pos, font);
127 text.insert(text.begin() + pos, c);
129 // Update the font table.
130 FontTable search_font(pos, LyXFont());
131 for (FontList::iterator it = lower_bound(fontlist.begin(),
133 search_font, matchFT());
134 it != fontlist.end(); ++it)
136 it->pos(it->pos() + 1);
139 // Update the inset table.
140 InsetTable search_inset(pos, 0);
141 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
142 owner_->insetlist.end(),
143 search_inset, matchIT());
144 it != owner_->insetlist.end(); ++it)
148 owner_->setFont(pos, font);
152 void Paragraph::Pimpl::insertInset(pos_type pos,
153 Inset * inset, LyXFont const & font)
156 lyx::Assert(pos <= size());
158 insertChar(pos, META_INSET, font);
159 lyx::Assert(text[pos] == META_INSET);
161 // Add a new entry in the inset table.
162 InsetTable search_inset(pos, 0);
163 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
164 owner_->insetlist.end(),
165 search_inset, matchIT());
166 if (it != owner_->insetlist.end() && it->pos == pos) {
167 lyxerr << "ERROR (Paragraph::InsertInset): "
168 "there is an inset in position: " << pos << endl;
170 owner_->insetlist.insert(it, InsetTable(pos, inset));
171 inset->parOwner(owner_);
175 inset->setOwner(inset_owner);
179 void Paragraph::Pimpl::erase(pos_type pos)
181 lyx::Assert(pos < size());
182 // if it is an inset, delete the inset entry
183 if (text[pos] == Paragraph::META_INSET) {
185 InsetTable search_inset(pos, 0);
186 InsetList::iterator it =
187 lower_bound(owner_->insetlist.begin(),
188 owner_->insetlist.end(),
189 search_inset, matchIT());
190 if (it != owner_->insetlist.end() && it->pos == pos) {
192 owner_->insetlist.erase(it);
196 text.erase(text.begin() + pos);
198 // Erase entries in the tables.
199 FontTable search_font(pos, LyXFont());
201 FontList::iterator it =
202 lower_bound(fontlist.begin(),
204 search_font, matchFT());
205 if (it != fontlist.end() && it->pos() == pos &&
207 (it != fontlist.begin()
208 && boost::prior(it)->pos() == pos - 1))) {
209 // If it is a multi-character font
210 // entry, we just make it smaller
211 // (see update below), otherwise we
213 unsigned int const i = it - fontlist.begin();
214 fontlist.erase(fontlist.begin() + i);
215 it = fontlist.begin() + i;
216 if (i > 0 && i < fontlist.size() &&
217 fontlist[i - 1].font() == fontlist[i].font()) {
218 fontlist.erase(fontlist.begin() + i - 1);
219 it = fontlist.begin() + i - 1;
223 // Update all other entries.
224 FontList::iterator fend = fontlist.end();
225 for (; it != fend; ++it)
226 it->pos(it->pos() - 1);
228 // Update the inset table.
229 InsetTable search_inset(pos, 0);
230 InsetList::iterator lend = owner_->insetlist.end();
231 for (InsetList::iterator it =
232 upper_bound(owner_->insetlist.begin(),
234 search_inset, matchIT());
240 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
242 int & column, LyXFont const & font,
243 LyXLayout const & style)
245 if (style.pass_thru) return;
246 if (column > tex_code_break_column
248 && getChar(i - 1) != ' '
250 // same in FreeSpacing mode
251 && !style.free_spacing
252 && !owner_->isFreeSpacing()
253 // In typewriter mode, we want to avoid
254 // ! . ? : at the end of a line
255 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
256 && (getChar(i - 1) == '.'
257 || getChar(i - 1) == '?'
258 || getChar(i - 1) == ':'
259 || getChar(i - 1) == '!'))) {
260 if (tex_code_break_column == 0) {
261 // in batchmode we need LaTeX to still
262 // see it as a space not as an extra '\n'
268 texrow.start(owner_, i + 1);
270 } else if (style.free_spacing) {
278 bool Paragraph::Pimpl::isTextAt(BufferParams const & bp,
279 string const & str, pos_type pos)
281 LyXFont const & font = owner_->getFont(bp, pos);
283 for (string::size_type i = 0; i < str.length(); ++i) {
284 if (pos + static_cast<pos_type>(i) >= size())
286 if (str[i] != getChar(pos + i))
288 if (owner_->getFont(bp, pos + i) != font)
295 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
296 BufferParams const & bparams,
301 LyXFont & running_font,
304 LyXLayout const & style,
309 if (style.pass_thru) {
310 if (c != '\0') os << c;
313 // Two major modes: LaTeX or plain
314 // Handle here those cases common to both modes
315 // and then split to handle the two modes separately.
317 case Paragraph::META_INSET: {
318 Inset * inset = owner_->getInset(i);
321 int const len = os.tellp();
322 //ostream::pos_type const len = os.tellp();
323 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
324 || inset->lyxCode() == Inset::MATH_CODE
325 || inset->lyxCode() == Inset::URL_CODE)
326 && running_font.isRightToLeft()) {
331 int tmp = inset->latex(buf, os, moving_arg,
338 for (int j = 0; j < tmp; ++j) {
341 texrow.start(owner_, i + 1);
344 column += int(os.tellp()) - len;
350 case Paragraph::META_NEWLINE:
352 column += running_font.latexWriteEndChanges(os,
357 basefont = owner_->getLayoutFont(bparams);
358 running_font = basefont;
361 case Paragraph::META_HFILL:
367 // And now for the special cases within each mode
371 os << "\\textbackslash{}";
375 case '±': case '²': case '³':
376 case '×': case '÷': case '¹':
378 if ((bparams.inputenc == "latin1" ||
379 bparams.inputenc == "latin9") ||
380 (bparams.inputenc == "auto" &&
381 (font.language()->encoding()->LatexName()
383 font.language()->encoding()->LatexName()
385 os << "\\ensuremath{"
394 case '|': case '<': case '>':
395 // In T1 encoding, these characters exist
396 if (lyxrc.fontenc == "T1") {
398 //... but we should avoid ligatures
399 if ((c == '>' || c == '<')
401 && getChar(i + 1) == c) {
402 //os << "\\textcompwordmark{}";
403 // Jean-Marc, have a look at
404 // this. I think this works
412 // Typewriter font also has them
413 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
417 // Otherwise, we use what LaTeX
421 os << "\\textless{}";
425 os << "\\textgreater{}";
435 case '-': // "--" in Typewriter mode -> "-{}-"
437 && getChar(i + 1) == '-'
438 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
447 os << "\\char`\\\"{}";
452 if (bparams.inputenc == "default") {
461 case '%': case '#': case '{':
468 os << "\\textasciitilde{}";
473 os << "\\textasciicircum{}";
477 case '*': case '[': case ']':
478 // avoid being mistaken for optional arguments
479 os << '{' << c << '}';
484 // Blanks are printed before font switching.
485 // Sure? I am not! (try nice-latex)
486 // I am sure it's correct. LyX might be smarter
487 // in the future, but for now, nothing wrong is
493 // I assume this is hack treating typewriter as verbatim
494 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
503 // FIXME: if we have "LaTeX" with a font change in the middle (before
504 // the 'T', then the "TeX" part is still special cased. Really we
505 // should only operate this on "words" for some definition of word
509 for (; pnr < phrases_nr; ++pnr) {
510 if (isTextAt(bparams, special_phrases[pnr][0], i)) {
511 os << special_phrases[pnr][1];
512 i += special_phrases[pnr][0].length() - 1;
513 column += special_phrases[pnr][1].length() - 1;
518 if (pnr == phrases_nr && c != '\0') {
528 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
529 BufferParams const & bparams,
530 ostream & os, TexRow & texrow)
532 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
533 Paragraph * par = owner_;
535 while (par && par->params().depth() == owner_->params().depth()) {
536 if (textclasslist[bparams.textclass][par->layout()].isEnvironment()) {
537 par = par->TeXEnvironment(buf, bparams,
540 par = par->TeXOnePar(buf, bparams,
544 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
550 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
552 InsetList::const_iterator cit = owner_->insetlist.begin();
553 InsetList::const_iterator lend = owner_->insetlist.end();
555 for (; cit != lend; ++cit) {
556 if ((result = cit->inset->getParFromID(id)))
563 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
564 BufferParams const & bparams) const
566 LyXFont tmpfont(font);
568 // check for environment font information
569 char par_depth = owner_->getDepth();
570 Paragraph const * par = owner_;
571 LyXTextClass const & tclass = textclasslist[bparams.textclass];
573 while (par && par->getDepth() && !tmpfont.resolved()) {
574 par = par->outerHook();
576 tmpfont.realize(tclass[par->layout()].font
577 #ifdef INHERIT_LANGUAGE
581 par_depth = par->getDepth();
585 tmpfont.realize(tclass.defaultfont()
586 #ifdef INHERIT_LANGUAGE