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"
25 #include "lyxtextclasslist.h"
27 #include "support/LAssert.h"
32 using std::upper_bound;
33 using std::lower_bound;
35 extern int tex_code_break_column;
38 // Initialize static member.
39 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
40 // Initialization of the counter for the paragraph id's,
41 unsigned int Paragraph::Pimpl::paragraph_id = 0;
45 struct special_phrase {
51 special_phrase special_phrases[] = {
52 { "LyX", "\\LyX{}", false },
53 { "TeX", "\\TeX{}", true },
54 { "LaTeX2e", "\\LaTeXe{}", true },
55 { "LaTeX", "\\LaTeX{}", true },
58 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
63 Paragraph::Pimpl::Pimpl(Paragraph * owner)
71 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
72 : params(p.params), owner_(owner)
74 inset_owner = p.inset_owner;
76 fontlist = p.fontlist;
84 void Paragraph::Pimpl::clear()
90 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
93 text = par->pimpl_->text;
97 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
99 // This is in the critical path for loading!
100 pos_type const siz = size();
101 lyx::Assert(pos <= siz);
102 // This is stronger, and I belive that this is the assertion
103 // that we should really use. (Lgb)
104 //Assert(pos < size());
106 // Then this has no meaning. (Lgb)
107 if (!siz || pos == siz)
114 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
120 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
121 LyXFont const & font)
123 lyx::Assert(pos <= size());
125 // This is actually very common when parsing buffers (and
126 // maybe inserting ascii text)
128 // when appending characters, no need to update tables
130 owner_->setFont(pos, font);
134 text.insert(text.begin() + pos, c);
136 // Update the font table.
137 FontTable search_font(pos, LyXFont());
138 for (FontList::iterator it = lower_bound(fontlist.begin(),
140 search_font, matchFT());
141 it != fontlist.end(); ++it)
143 it->pos(it->pos() + 1);
146 // Update the inset table.
147 InsetTable search_inset(pos, 0);
148 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
149 owner_->insetlist.end(),
150 search_inset, matchIT());
151 it != owner_->insetlist.end(); ++it)
155 owner_->setFont(pos, font);
159 void Paragraph::Pimpl::insertInset(pos_type pos,
160 Inset * inset, LyXFont const & font)
163 lyx::Assert(pos <= size());
165 insertChar(pos, META_INSET, font);
166 lyx::Assert(text[pos] == META_INSET);
168 // Add a new entry in the inset table.
169 InsetTable search_inset(pos, 0);
170 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
171 owner_->insetlist.end(),
172 search_inset, matchIT());
173 if (it != owner_->insetlist.end() && it->pos == pos) {
174 lyxerr << "ERROR (Paragraph::InsertInset): "
175 "there is an inset in position: " << pos << endl;
177 owner_->insetlist.insert(it, InsetTable(pos, inset));
178 inset->parOwner(owner_);
182 inset->setOwner(inset_owner);
186 void Paragraph::Pimpl::erase(pos_type pos)
188 lyx::Assert(pos < size());
189 // if it is an inset, delete the inset entry
190 if (text[pos] == Paragraph::META_INSET) {
192 InsetTable search_inset(pos, 0);
193 InsetList::iterator it =
194 lower_bound(owner_->insetlist.begin(),
195 owner_->insetlist.end(),
196 search_inset, matchIT());
197 if (it != owner_->insetlist.end() && it->pos == pos) {
199 owner_->insetlist.erase(it);
203 text.erase(text.begin() + pos);
205 // Erase entries in the tables.
206 FontTable search_font(pos, LyXFont());
208 FontList::iterator it =
209 lower_bound(fontlist.begin(),
211 search_font, matchFT());
212 if (it != fontlist.end() && it->pos() == pos &&
214 (it != fontlist.begin()
215 && boost::prior(it)->pos() == pos - 1))) {
216 // If it is a multi-character font
217 // entry, we just make it smaller
218 // (see update below), otherwise we
220 unsigned int const i = it - fontlist.begin();
221 fontlist.erase(fontlist.begin() + i);
222 it = fontlist.begin() + i;
223 if (i > 0 && i < fontlist.size() &&
224 fontlist[i - 1].font() == fontlist[i].font()) {
225 fontlist.erase(fontlist.begin() + i - 1);
226 it = fontlist.begin() + i - 1;
230 // Update all other entries.
231 FontList::iterator fend = fontlist.end();
232 for (; it != fend; ++it)
233 it->pos(it->pos() - 1);
235 // Update the inset table.
236 InsetTable search_inset(pos, 0);
237 InsetList::iterator lend = owner_->insetlist.end();
238 for (InsetList::iterator it =
239 upper_bound(owner_->insetlist.begin(),
241 search_inset, matchIT());
247 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
249 int & column, LyXFont const & font,
250 LyXLayout const & style)
252 if (style.pass_thru) return;
253 if (column > tex_code_break_column
255 && getChar(i - 1) != ' '
257 // same in FreeSpacing mode
258 && !style.free_spacing
259 && !owner_->isFreeSpacing()
260 // In typewriter mode, we want to avoid
261 // ! . ? : at the end of a line
262 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
263 && (getChar(i - 1) == '.'
264 || getChar(i - 1) == '?'
265 || getChar(i - 1) == ':'
266 || getChar(i - 1) == '!'))) {
267 if (tex_code_break_column == 0) {
268 // in batchmode we need LaTeX to still
269 // see it as a space not as an extra '\n'
275 texrow.start(owner_, i + 1);
277 } else if (style.free_spacing) {
285 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
287 pos_type const len = str.length();
289 // is the paragraph large enough?
290 if (pos + len > size())
293 // does the wanted text start at point?
294 for (string::size_type i = 0; i < str.length(); ++i) {
295 if (str[i] != text[pos + i])
299 // is there a font change in middle of the word?
300 FontList::const_iterator cit = fontlist.begin();
301 FontList::const_iterator end = fontlist.end();
302 for (; cit != end; ++cit) {
303 if (cit->pos() >= pos)
306 if (cit != end && pos + len - 1 > cit->pos())
313 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
314 BufferParams const & bparams,
319 LyXFont & running_font,
322 LyXLayout const & style,
327 if (style.pass_thru) {
328 if (c != '\0') os << c;
331 // Two major modes: LaTeX or plain
332 // Handle here those cases common to both modes
333 // and then split to handle the two modes separately.
335 case Paragraph::META_INSET: {
336 Inset * inset = owner_->getInset(i);
339 int const len = os.tellp();
340 //ostream::pos_type const len = os.tellp();
341 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
342 || inset->lyxCode() == Inset::MATH_CODE
343 || inset->lyxCode() == Inset::URL_CODE)
344 && running_font.isRightToLeft()) {
349 int tmp = inset->latex(buf, os, moving_arg,
356 for (int j = 0; j < tmp; ++j) {
359 texrow.start(owner_, i + 1);
362 column += int(os.tellp()) - len;
368 case Paragraph::META_NEWLINE:
370 column += running_font.latexWriteEndChanges(os,
375 basefont = owner_->getLayoutFont(bparams);
376 running_font = basefont;
379 case Paragraph::META_HFILL:
385 // And now for the special cases within each mode
389 os << "\\textbackslash{}";
393 case '±': case '²': case '³':
394 case '×': case '÷': case '¹':
395 case '¬': case 'µ':
396 if ((bparams.inputenc == "latin1" ||
397 bparams.inputenc == "latin9") ||
398 (bparams.inputenc == "auto" &&
399 (font.language()->encoding()->LatexName()
401 font.language()->encoding()->LatexName()
403 os << "\\ensuremath{"
412 case '|': case '<': case '>':
413 // In T1 encoding, these characters exist
414 if (lyxrc.fontenc == "T1") {
416 //... but we should avoid ligatures
417 if ((c == '>' || c == '<')
419 && getChar(i + 1) == c) {
420 //os << "\\textcompwordmark{}";
421 // Jean-Marc, have a look at
422 // this. IÂ think this works
430 // Typewriter font also has them
431 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
435 // Otherwise, we use what LaTeX
439 os << "\\textless{}";
443 os << "\\textgreater{}";
453 case '-': // "--" in Typewriter mode -> "-{}-"
455 && getChar(i + 1) == '-'
456 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
465 os << "\\char`\\\"{}";
470 if (bparams.inputenc == "default") {
479 case '%': case '#': case '{':
486 os << "\\textasciitilde{}";
491 os << "\\textasciicircum{}";
495 case '*': case '[': case ']':
496 // avoid being mistaken for optional arguments
497 os << '{' << c << '}';
502 // Blanks are printed before font switching.
503 // Sure? I am not! (try nice-latex)
504 // I am sure it's correct. LyX might be smarter
505 // in the future, but for now, nothing wrong is
511 // I assume this is hack treating typewriter as verbatim
512 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
521 // FIXME: if we have "LaTeX" with a font
522 // change in the middle (before the 'T', then
523 // the "TeX" part is still special cased.
524 // Really we should only operate this on
525 // "words" for some definition of word
529 for (; pnr < phrases_nr; ++pnr) {
530 if (isTextAt(special_phrases[pnr].phrase, i)) {
531 os << special_phrases[pnr].macro;
532 i += special_phrases[pnr].phrase.length() - 1;
533 column += special_phrases[pnr].macro.length() - 1;
538 if (pnr == phrases_nr && c != '\0') {
548 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
549 BufferParams const & bparams,
550 ostream & os, TexRow & texrow)
552 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
553 Paragraph * par = owner_;
555 while (par && par->params().depth() == owner_->params().depth()) {
556 if (par->layout()->isEnvironment()) {
557 par = par->TeXEnvironment(buf, bparams,
560 par = par->TeXOnePar(buf, bparams,
564 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
570 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
571 LyXLayout const & layout) const
573 BufferParams const & bparams = features.bufferParams();
576 if (params.lineTop() || params.lineBottom())
577 features.require("lyxline");
578 if (!params.spacing().isDefault())
579 features.require("setspace");
582 features.useLayout(layout.name());
585 Language const * doc_language = bparams.language;
587 FontList::const_iterator fcit = fontlist.begin();
588 FontList::const_iterator fend = fontlist.end();
589 for (; fcit != fend; ++fcit) {
590 if (fcit->font().noun() == LyXFont::ON) {
591 lyxerr[Debug::LATEX] << "font.noun: "
592 << fcit->font().noun()
594 features.require("noun");
595 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
596 << fcit->font().stateText(0)
599 switch (fcit->font().color()) {
601 case LColor::inherit:
603 // probably we should put here all interface colors used for
604 // font displaying! For now I just add this ones I know of (Jug)
609 features.require("color");
610 lyxerr[Debug::LATEX] << "Color enabled. Font: "
611 << fcit->font().stateText(0)
615 Language const * language = fcit->font().language();
616 if (language->babel() != doc_language->babel() &&
617 language != ignore_language &&
618 #ifdef INHERIT_LANGUAGE
619 language != inherit_language &&
621 language != latex_language)
623 features.useLanguage(language);
624 lyxerr[Debug::LATEX] << "Found language "
625 << language->babel() << endl;
629 if (!params.leftIndent().zero())
630 features.require("ParagraphLeftIndent");
633 InsetList::const_iterator icit = owner_->insetlist.begin();
634 InsetList::const_iterator iend = owner_->insetlist.end();
635 for (; icit != iend; ++icit) {
637 icit->inset->validate(features);
638 if (layout.needprotect &&
639 icit->inset->lyxCode() == Inset::FOOT_CODE)
640 features.require("NeedLyXFootnoteCode");
645 for (pos_type i = 0; i < size() ; ++i) {
646 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
647 if (!special_phrases[pnr].builtin
648 && isTextAt(special_phrases[pnr].phrase, i)) {
649 features.require(special_phrases[pnr].phrase);
657 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
659 InsetList::const_iterator cit = owner_->insetlist.begin();
660 InsetList::const_iterator lend = owner_->insetlist.end();
662 for (; cit != lend; ++cit) {
663 if ((result = cit->inset->getParFromID(id)))
670 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
671 BufferParams const & bparams) const
673 LyXFont tmpfont(font);
675 // check for environment font information
676 char par_depth = owner_->getDepth();
677 Paragraph const * par = owner_;
678 LyXTextClass const & tclass = textclasslist[bparams.textclass];
680 while (par && par->getDepth() && !tmpfont.resolved()) {
681 par = par->outerHook();
683 tmpfont.realize(par->layout()->font
684 #ifdef INHERIT_LANGUAGE
688 par_depth = par->getDepth();
692 tmpfont.realize(tclass.defaultfont()
693 #ifdef INHERIT_LANGUAGE