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()) {
349 #warning Bug: we can have an empty font change here!
350 // if there has just been a font change, we are going to close it
351 // right now, which means stupid latex code like \textsf{}. AFAIK,
352 // this does not harm dvi output. A minor bug, thus (JMarc)
354 // some insets cannot be inside a font change command
355 if (open_font && inset->noFontChange()) {
356 column +=running_font.
357 latexWriteEndChanges(os,
361 basefont = owner_->getLayoutFont(bparams);
362 running_font = basefont;
365 int tmp = inset->latex(buf, os, moving_arg,
372 for (int j = 0; j < tmp; ++j) {
375 texrow.start(owner_, i + 1);
378 column += int(os.tellp()) - len;
384 case Paragraph::META_NEWLINE:
386 column += running_font.latexWriteEndChanges(os,
391 basefont = owner_->getLayoutFont(bparams);
392 running_font = basefont;
395 case Paragraph::META_HFILL:
401 // And now for the special cases within each mode
405 os << "\\textbackslash{}";
409 case '±': case '²': case '³':
410 case '×': case '÷': case '¹':
411 case '¬': case 'µ':
412 if ((bparams.inputenc == "latin1" ||
413 bparams.inputenc == "latin9") ||
414 (bparams.inputenc == "auto" &&
415 (font.language()->encoding()->LatexName()
417 font.language()->encoding()->LatexName()
419 os << "\\ensuremath{"
428 case '|': case '<': case '>':
429 // In T1 encoding, these characters exist
430 if (lyxrc.fontenc == "T1") {
432 //... but we should avoid ligatures
433 if ((c == '>' || c == '<')
435 && getChar(i + 1) == c) {
436 //os << "\\textcompwordmark{}";
437 // Jean-Marc, have a look at
438 // this. IÂ think this works
446 // Typewriter font also has them
447 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
451 // Otherwise, we use what LaTeX
455 os << "\\textless{}";
459 os << "\\textgreater{}";
469 case '-': // "--" in Typewriter mode -> "-{}-"
471 && getChar(i + 1) == '-'
472 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
481 os << "\\char`\\\"{}";
486 if (bparams.inputenc == "default") {
495 case '%': case '#': case '{':
502 os << "\\textasciitilde{}";
507 os << "\\textasciicircum{}";
511 case '*': case '[': case ']':
512 // avoid being mistaken for optional arguments
513 os << '{' << c << '}';
518 // Blanks are printed before font switching.
519 // Sure? I am not! (try nice-latex)
520 // I am sure it's correct. LyX might be smarter
521 // in the future, but for now, nothing wrong is
527 // I assume this is hack treating typewriter as verbatim
528 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
537 // FIXME: if we have "LaTeX" with a font
538 // change in the middle (before the 'T', then
539 // the "TeX" part is still special cased.
540 // Really we should only operate this on
541 // "words" for some definition of word
545 for (; pnr < phrases_nr; ++pnr) {
546 if (isTextAt(special_phrases[pnr].phrase, i)) {
547 os << special_phrases[pnr].macro;
548 i += special_phrases[pnr].phrase.length() - 1;
549 column += special_phrases[pnr].macro.length() - 1;
554 if (pnr == phrases_nr && c != '\0') {
564 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
565 BufferParams const & bparams,
566 ostream & os, TexRow & texrow)
568 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
569 Paragraph * par = owner_;
571 while (par && par->params().depth() == owner_->params().depth()) {
572 if (par->layout()->isEnvironment()) {
573 par = par->TeXEnvironment(buf, bparams,
576 par = par->TeXOnePar(buf, bparams,
580 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
586 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
587 LyXLayout const & layout) const
589 BufferParams const & bparams = features.bufferParams();
592 if (params.lineTop() || params.lineBottom())
593 features.require("lyxline");
594 if (!params.spacing().isDefault())
595 features.require("setspace");
598 features.useLayout(layout.name());
601 Language const * doc_language = bparams.language;
603 FontList::const_iterator fcit = fontlist.begin();
604 FontList::const_iterator fend = fontlist.end();
605 for (; fcit != fend; ++fcit) {
606 if (fcit->font().noun() == LyXFont::ON) {
607 lyxerr[Debug::LATEX] << "font.noun: "
608 << fcit->font().noun()
610 features.require("noun");
611 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
612 << fcit->font().stateText(0)
615 switch (fcit->font().color()) {
617 case LColor::inherit:
619 // probably we should put here all interface colors used for
620 // font displaying! For now I just add this ones I know of (Jug)
625 features.require("color");
626 lyxerr[Debug::LATEX] << "Color enabled. Font: "
627 << fcit->font().stateText(0)
631 Language const * language = fcit->font().language();
632 if (language->babel() != doc_language->babel() &&
633 language != ignore_language &&
634 #ifdef INHERIT_LANGUAGE
635 language != inherit_language &&
637 language != latex_language)
639 features.useLanguage(language);
640 lyxerr[Debug::LATEX] << "Found language "
641 << language->babel() << endl;
645 if (!params.leftIndent().zero())
646 features.require("ParagraphLeftIndent");
649 InsetList::const_iterator icit = owner_->insetlist.begin();
650 InsetList::const_iterator iend = owner_->insetlist.end();
651 for (; icit != iend; ++icit) {
653 icit->inset->validate(features);
654 if (layout.needprotect &&
655 icit->inset->lyxCode() == Inset::FOOT_CODE)
656 features.require("NeedLyXFootnoteCode");
661 for (pos_type i = 0; i < size() ; ++i) {
662 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
663 if (!special_phrases[pnr].builtin
664 && isTextAt(special_phrases[pnr].phrase, i)) {
665 features.require(special_phrases[pnr].phrase);
673 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
675 InsetList::const_iterator cit = owner_->insetlist.begin();
676 InsetList::const_iterator lend = owner_->insetlist.end();
678 for (; cit != lend; ++cit) {
679 if ((result = cit->inset->getParFromID(id)))
686 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
687 BufferParams const & bparams) const
689 LyXFont tmpfont(font);
691 // check for environment font information
692 char par_depth = owner_->getDepth();
693 Paragraph const * par = owner_;
694 LyXTextClass const & tclass = bparams.getLyXTextClass();
696 while (par && par->getDepth() && !tmpfont.resolved()) {
697 par = par->outerHook();
699 tmpfont.realize(par->layout()->font
700 #ifdef INHERIT_LANGUAGE
704 par_depth = par->getDepth();
708 tmpfont.realize(tclass.defaultfont()
709 #ifdef INHERIT_LANGUAGE