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"
18 #include "LaTeXFeatures.h"
21 #include "bufferparams.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 struct special_phrase {
50 special_phrase special_phrases[] = {
51 { "LyX", "\\LyX{}", false },
52 { "TeX", "\\TeX{}", true },
53 { "LaTeX2e", "\\LaTeXe{}", true },
54 { "LaTeX", "\\LaTeX{}", true },
57 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
62 Paragraph::Pimpl::Pimpl(Paragraph * owner)
70 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
71 : params(p.params), owner_(owner)
73 inset_owner = p.inset_owner;
75 fontlist = p.fontlist;
83 void Paragraph::Pimpl::clear()
89 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
92 text = par->pimpl_->text;
96 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
98 // This is in the critical path for loading!
99 pos_type const siz = size();
100 lyx::Assert(pos <= siz);
101 // This is stronger, and I belive that this is the assertion
102 // that we should really use. (Lgb)
103 //Assert(pos < size());
105 // Then this has no meaning. (Lgb)
106 if (!siz || pos == siz)
113 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
119 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
120 LyXFont const & font)
122 lyx::Assert(pos <= size());
124 // This is actually very common when parsing buffers (and
125 // maybe inserting ascii text)
127 // when appending characters, no need to update tables
129 owner_->setFont(pos, font);
133 text.insert(text.begin() + pos, c);
135 // Update the font table.
136 FontTable search_font(pos, LyXFont());
137 for (FontList::iterator it = lower_bound(fontlist.begin(),
139 search_font, matchFT());
140 it != fontlist.end(); ++it)
142 it->pos(it->pos() + 1);
145 // Update the inset table.
146 InsetTable search_inset(pos, 0);
147 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
148 owner_->insetlist.end(),
149 search_inset, matchIT());
150 it != owner_->insetlist.end(); ++it)
154 owner_->setFont(pos, font);
158 void Paragraph::Pimpl::insertInset(pos_type pos,
159 Inset * inset, LyXFont const & font)
162 lyx::Assert(pos <= size());
164 insertChar(pos, META_INSET, font);
165 lyx::Assert(text[pos] == META_INSET);
167 // Add a new entry in the inset table.
168 InsetTable search_inset(pos, 0);
169 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
170 owner_->insetlist.end(),
171 search_inset, matchIT());
172 if (it != owner_->insetlist.end() && it->pos == pos) {
173 lyxerr << "ERROR (Paragraph::InsertInset): "
174 "there is an inset in position: " << pos << endl;
176 owner_->insetlist.insert(it, InsetTable(pos, inset));
177 inset->parOwner(owner_);
181 inset->setOwner(inset_owner);
185 void Paragraph::Pimpl::erase(pos_type pos)
187 lyx::Assert(pos < size());
188 // if it is an inset, delete the inset entry
189 if (text[pos] == Paragraph::META_INSET) {
191 InsetTable search_inset(pos, 0);
192 InsetList::iterator it =
193 lower_bound(owner_->insetlist.begin(),
194 owner_->insetlist.end(),
195 search_inset, matchIT());
196 if (it != owner_->insetlist.end() && it->pos == pos) {
198 owner_->insetlist.erase(it);
202 text.erase(text.begin() + pos);
204 // Erase entries in the tables.
205 FontTable search_font(pos, LyXFont());
207 FontList::iterator it =
208 lower_bound(fontlist.begin(),
210 search_font, matchFT());
211 if (it != fontlist.end() && it->pos() == pos &&
213 (it != fontlist.begin()
214 && boost::prior(it)->pos() == pos - 1))) {
215 // If it is a multi-character font
216 // entry, we just make it smaller
217 // (see update below), otherwise we
219 unsigned int const i = it - fontlist.begin();
220 fontlist.erase(fontlist.begin() + i);
221 it = fontlist.begin() + i;
222 if (i > 0 && i < fontlist.size() &&
223 fontlist[i - 1].font() == fontlist[i].font()) {
224 fontlist.erase(fontlist.begin() + i - 1);
225 it = fontlist.begin() + i - 1;
229 // Update all other entries.
230 FontList::iterator fend = fontlist.end();
231 for (; it != fend; ++it)
232 it->pos(it->pos() - 1);
234 // Update the inset table.
235 InsetTable search_inset(pos, 0);
236 InsetList::iterator lend = owner_->insetlist.end();
237 for (InsetList::iterator it =
238 upper_bound(owner_->insetlist.begin(),
240 search_inset, matchIT());
246 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
248 int & column, LyXFont const & font,
249 LyXLayout const & style)
251 if (style.pass_thru) return;
252 if (column > tex_code_break_column
254 && getChar(i - 1) != ' '
256 // same in FreeSpacing mode
257 && !style.free_spacing
258 && !owner_->isFreeSpacing()
259 // In typewriter mode, we want to avoid
260 // ! . ? : at the end of a line
261 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
262 && (getChar(i - 1) == '.'
263 || getChar(i - 1) == '?'
264 || getChar(i - 1) == ':'
265 || getChar(i - 1) == '!'))) {
266 if (tex_code_break_column == 0) {
267 // in batchmode we need LaTeX to still
268 // see it as a space not as an extra '\n'
274 texrow.start(owner_, i + 1);
276 } else if (style.free_spacing) {
284 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
286 pos_type const len = str.length();
288 // is the paragraph large enough?
289 if (pos + len > size())
292 // does the wanted text start at point?
293 for (string::size_type i = 0; i < str.length(); ++i) {
294 if (str[i] != text[pos + i])
298 // is there a font change in middle of the word?
299 FontList::const_iterator cit = fontlist.begin();
300 FontList::const_iterator end = fontlist.end();
301 for (; cit != end; ++cit) {
302 if (cit->pos() >= pos)
305 if (cit != end && pos + len - 1 > cit->pos())
312 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
313 BufferParams const & bparams,
318 LyXFont & running_font,
321 LyXLayout const & style,
326 if (style.pass_thru) {
327 if (c != '\0') os << c;
330 // Two major modes: LaTeX or plain
331 // Handle here those cases common to both modes
332 // and then split to handle the two modes separately.
334 case Paragraph::META_INSET: {
335 Inset * inset = owner_->getInset(i);
338 int const len = os.tellp();
339 //ostream::pos_type const len = os.tellp();
340 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
341 || inset->lyxCode() == Inset::MATH_CODE
342 || inset->lyxCode() == Inset::URL_CODE)
343 && running_font.isRightToLeft()) {
348 int tmp = inset->latex(buf, os, moving_arg,
355 for (int j = 0; j < tmp; ++j) {
358 texrow.start(owner_, i + 1);
361 column += int(os.tellp()) - len;
367 case Paragraph::META_NEWLINE:
369 column += running_font.latexWriteEndChanges(os,
374 basefont = owner_->getLayoutFont(bparams);
375 running_font = basefont;
378 case Paragraph::META_HFILL:
384 // And now for the special cases within each mode
388 os << "\\textbackslash{}";
392 case '±': case '²': case '³':
393 case '×': case '÷': case '¹':
394 case '¬': case 'µ':
395 if ((bparams.inputenc == "latin1" ||
396 bparams.inputenc == "latin9") ||
397 (bparams.inputenc == "auto" &&
398 (font.language()->encoding()->LatexName()
400 font.language()->encoding()->LatexName()
402 os << "\\ensuremath{"
411 case '|': case '<': case '>':
412 // In T1 encoding, these characters exist
413 if (lyxrc.fontenc == "T1") {
415 //... but we should avoid ligatures
416 if ((c == '>' || c == '<')
418 && getChar(i + 1) == c) {
419 //os << "\\textcompwordmark{}";
420 // Jean-Marc, have a look at
421 // this. IÂ think this works
429 // Typewriter font also has them
430 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
434 // Otherwise, we use what LaTeX
438 os << "\\textless{}";
442 os << "\\textgreater{}";
452 case '-': // "--" in Typewriter mode -> "-{}-"
454 && getChar(i + 1) == '-'
455 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
464 os << "\\char`\\\"{}";
469 if (bparams.inputenc == "default") {
478 case '%': case '#': case '{':
485 os << "\\textasciitilde{}";
490 os << "\\textasciicircum{}";
494 case '*': case '[': case ']':
495 // avoid being mistaken for optional arguments
496 os << '{' << c << '}';
501 // Blanks are printed before font switching.
502 // Sure? I am not! (try nice-latex)
503 // I am sure it's correct. LyX might be smarter
504 // in the future, but for now, nothing wrong is
510 // I assume this is hack treating typewriter as verbatim
511 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
520 // FIXME: if we have "LaTeX" with a font
521 // change in the middle (before the 'T', then
522 // the "TeX" part is still special cased.
523 // Really we should only operate this on
524 // "words" for some definition of word
528 for (; pnr < phrases_nr; ++pnr) {
529 if (isTextAt(special_phrases[pnr].phrase, i)) {
530 os << special_phrases[pnr].macro;
531 i += special_phrases[pnr].phrase.length() - 1;
532 column += special_phrases[pnr].macro.length() - 1;
537 if (pnr == phrases_nr && c != '\0') {
547 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
548 BufferParams const & bparams,
549 ostream & os, TexRow & texrow)
551 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
552 Paragraph * par = owner_;
554 while (par && par->params().depth() == owner_->params().depth()) {
555 if (par->layout()->isEnvironment()) {
556 par = par->TeXEnvironment(buf, bparams,
559 par = par->TeXOnePar(buf, bparams,
563 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
569 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
570 LyXLayout const & layout) const
572 BufferParams const & bparams = features.bufferParams();
575 if (params.lineTop() || params.lineBottom())
576 features.require("lyxline");
577 if (!params.spacing().isDefault())
578 features.require("setspace");
581 features.useLayout(layout.name());
584 Language const * doc_language = bparams.language;
586 FontList::const_iterator fcit = fontlist.begin();
587 FontList::const_iterator fend = fontlist.end();
588 for (; fcit != fend; ++fcit) {
589 if (fcit->font().noun() == LyXFont::ON) {
590 lyxerr[Debug::LATEX] << "font.noun: "
591 << fcit->font().noun()
593 features.require("noun");
594 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
595 << fcit->font().stateText(0)
598 switch (fcit->font().color()) {
600 case LColor::inherit:
602 // probably we should put here all interface colors used for
603 // font displaying! For now I just add this ones I know of (Jug)
608 features.require("color");
609 lyxerr[Debug::LATEX] << "Color enabled. Font: "
610 << fcit->font().stateText(0)
614 Language const * language = fcit->font().language();
615 if (language->babel() != doc_language->babel() &&
616 language != ignore_language &&
617 #ifdef INHERIT_LANGUAGE
618 language != inherit_language &&
620 language != latex_language)
622 features.useLanguage(language);
623 lyxerr[Debug::LATEX] << "Found language "
624 << language->babel() << endl;
628 if (!params.leftIndent().zero())
629 features.require("ParagraphLeftIndent");
632 InsetList::const_iterator icit = owner_->insetlist.begin();
633 InsetList::const_iterator iend = owner_->insetlist.end();
634 for (; icit != iend; ++icit) {
636 icit->inset->validate(features);
637 if (layout.needprotect &&
638 icit->inset->lyxCode() == Inset::FOOT_CODE)
639 features.require("NeedLyXFootnoteCode");
644 for (pos_type i = 0; i < size() ; ++i) {
645 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
646 if (!special_phrases[pnr].builtin
647 && isTextAt(special_phrases[pnr].phrase, i)) {
648 features.require(special_phrases[pnr].phrase);
656 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
658 InsetList::const_iterator cit = owner_->insetlist.begin();
659 InsetList::const_iterator lend = owner_->insetlist.end();
661 for (; cit != lend; ++cit) {
662 if ((result = cit->inset->getParFromID(id)))
669 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
670 BufferParams const & bparams) const
672 LyXFont tmpfont(font);
674 // check for environment font information
675 char par_depth = owner_->getDepth();
676 Paragraph const * par = owner_;
677 LyXTextClass const & tclass = bparams.getLyXTextClass();
679 while (par && par->getDepth() && !tmpfont.resolved()) {
680 par = par->outerHook();
682 tmpfont.realize(par->layout()->font
683 #ifdef INHERIT_LANGUAGE
687 par_depth = par->getDepth();
691 tmpfont.realize(tclass.defaultfont()
692 #ifdef INHERIT_LANGUAGE