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 // Initialize static member.
35 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
36 // Initialization of the counter for the paragraph id's,
37 unsigned int Paragraph::Pimpl::paragraph_id = 0;
41 struct special_phrase {
47 special_phrase special_phrases[] = {
48 { "LyX", "\\LyX{}", false },
49 { "TeX", "\\TeX{}", true },
50 { "LaTeX2e", "\\LaTeXe{}", true },
51 { "LaTeX", "\\LaTeX{}", true },
54 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
59 Paragraph::Pimpl::Pimpl(Paragraph * owner)
67 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
68 : params(p.params), owner_(owner)
70 inset_owner = p.inset_owner;
72 fontlist = p.fontlist;
80 void Paragraph::Pimpl::clear()
86 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
89 text = par->pimpl_->text;
93 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
95 // This is in the critical path for loading!
96 pos_type const siz = size();
97 lyx::Assert(pos <= siz);
98 // This is stronger, and I belive that this is the assertion
99 // that we should really use. (Lgb)
100 //Assert(pos < size());
102 // Then this has no meaning. (Lgb)
103 if (!siz || pos == siz)
110 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
116 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
117 LyXFont const & font)
119 lyx::Assert(pos <= size());
121 // This is actually very common when parsing buffers (and
122 // maybe inserting ascii text)
124 // when appending characters, no need to update tables
126 owner_->setFont(pos, font);
130 text.insert(text.begin() + pos, c);
132 // Update the font table.
133 FontTable search_font(pos, LyXFont());
134 for (FontList::iterator it = lower_bound(fontlist.begin(),
136 search_font, matchFT());
137 it != fontlist.end(); ++it)
139 it->pos(it->pos() + 1);
143 owner_->insetlist.increasePosAfterPos(pos);
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 insetlist.
159 owner_->insetlist.insert(inset, pos);
160 inset->parOwner(owner_);
163 inset->setOwner(inset_owner);
167 void Paragraph::Pimpl::erase(pos_type pos)
169 lyx::Assert(pos < size());
170 // if it is an inset, delete the inset entry
171 if (text[pos] == Paragraph::META_INSET) {
172 owner_->insetlist.erase(pos);
175 text.erase(text.begin() + pos);
177 // Erase entries in the tables.
178 FontTable search_font(pos, LyXFont());
180 FontList::iterator it =
181 lower_bound(fontlist.begin(),
183 search_font, matchFT());
184 if (it != fontlist.end() && it->pos() == pos &&
186 (it != fontlist.begin()
187 && boost::prior(it)->pos() == pos - 1))) {
188 // If it is a multi-character font
189 // entry, we just make it smaller
190 // (see update below), otherwise we
192 unsigned int const i = it - fontlist.begin();
193 fontlist.erase(fontlist.begin() + i);
194 it = fontlist.begin() + i;
195 if (i > 0 && i < fontlist.size() &&
196 fontlist[i - 1].font() == fontlist[i].font()) {
197 fontlist.erase(fontlist.begin() + i - 1);
198 it = fontlist.begin() + i - 1;
202 // Update all other entries.
203 FontList::iterator fend = fontlist.end();
204 for (; it != fend; ++it)
205 it->pos(it->pos() - 1);
207 // Update the insetlist.
208 owner_->insetlist.decreasePosAfterPos(pos);
212 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
214 unsigned int & column,
215 LyXFont const & font,
216 LyXLayout const & style)
218 if (style.pass_thru) return;
219 if (column > lyxrc.ascii_linelen
221 && getChar(i - 1) != ' '
223 // same in FreeSpacing mode
224 && !style.free_spacing
225 && !owner_->isFreeSpacing()
226 // In typewriter mode, we want to avoid
227 // ! . ? : at the end of a line
228 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
229 && (getChar(i - 1) == '.'
230 || getChar(i - 1) == '?'
231 || getChar(i - 1) == ':'
232 || getChar(i - 1) == '!'))) {
235 texrow.start(owner_, i + 1);
237 } else if (style.free_spacing) {
245 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
247 pos_type const len = str.length();
249 // is the paragraph large enough?
250 if (pos + len > size())
253 // does the wanted text start at point?
254 for (string::size_type i = 0; i < str.length(); ++i) {
255 if (str[i] != text[pos + i])
259 // is there a font change in middle of the word?
260 FontList::const_iterator cit = fontlist.begin();
261 FontList::const_iterator end = fontlist.end();
262 for (; cit != end; ++cit) {
263 if (cit->pos() >= pos)
266 if (cit != end && pos + len - 1 > cit->pos())
273 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
274 BufferParams const & bparams,
279 LyXFont & running_font,
282 LyXLayout const & style,
284 unsigned int & column,
287 if (style.pass_thru) {
288 if (c != '\0') os << c;
291 // Two major modes: LaTeX or plain
292 // Handle here those cases common to both modes
293 // and then split to handle the two modes separately.
295 case Paragraph::META_INSET: {
296 Inset * inset = owner_->getInset(i);
299 int const len = os.tellp();
300 //ostream::pos_type const len = os.tellp();
301 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
302 || inset->lyxCode() == Inset::MATH_CODE
303 || inset->lyxCode() == Inset::URL_CODE)
304 && running_font.isRightToLeft()) {
310 #warning Bug: we can have an empty font change here!
311 // if there has just been a font change, we are going to close it
312 // right now, which means stupid latex code like \textsf{}. AFAIK,
313 // this does not harm dvi output. A minor bug, thus (JMarc)
315 // some insets cannot be inside a font change command
316 if (open_font && inset->noFontChange()) {
317 column +=running_font.
318 latexWriteEndChanges(os,
322 basefont = owner_->getLayoutFont(bparams);
323 running_font = basefont;
326 int tmp = inset->latex(buf, os, moving_arg,
333 for (int j = 0; j < tmp; ++j) {
336 texrow.start(owner_, i + 1);
339 column += int(os.tellp()) - len;
345 case Paragraph::META_NEWLINE:
347 column += running_font.latexWriteEndChanges(os,
352 basefont = owner_->getLayoutFont(bparams);
353 running_font = basefont;
356 case Paragraph::META_HFILL:
362 // And now for the special cases within each mode
366 os << "\\textbackslash{}";
370 case '±': case '²': case '³':
371 case '×': case '÷': case '¹':
372 case '¬': case 'µ':
373 if ((bparams.inputenc == "latin1" ||
374 bparams.inputenc == "latin9") ||
375 (bparams.inputenc == "auto" &&
376 (font.language()->encoding()->LatexName()
378 font.language()->encoding()->LatexName()
380 os << "\\ensuremath{"
389 case '|': case '<': case '>':
390 // In T1 encoding, these characters exist
391 if (lyxrc.fontenc == "T1") {
393 //... but we should avoid ligatures
394 if ((c == '>' || c == '<')
396 && getChar(i + 1) == c) {
397 //os << "\\textcompwordmark{}";
398 // Jean-Marc, have a look at
399 // this. IÂ think this works
407 // Typewriter font also has them
408 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
412 // Otherwise, we use what LaTeX
416 os << "\\textless{}";
420 os << "\\textgreater{}";
430 case '-': // "--" in Typewriter mode -> "-{}-"
432 && getChar(i + 1) == '-'
433 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
442 os << "\\char`\\\"{}";
447 if (bparams.inputenc == "default") {
456 case '%': case '#': case '{':
463 os << "\\textasciitilde{}";
468 os << "\\textasciicircum{}";
472 case '*': case '[': case ']':
473 // avoid being mistaken for optional arguments
474 os << '{' << c << '}';
479 // Blanks are printed before font switching.
480 // Sure? I am not! (try nice-latex)
481 // I am sure it's correct. LyX might be smarter
482 // in the future, but for now, nothing wrong is
488 // I assume this is hack treating typewriter as verbatim
489 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
498 // FIXME: if we have "LaTeX" with a font
499 // change in the middle (before the 'T', then
500 // the "TeX" part is still special cased.
501 // Really we should only operate this on
502 // "words" for some definition of word
506 for (; pnr < phrases_nr; ++pnr) {
507 if (isTextAt(special_phrases[pnr].phrase, i)) {
508 os << special_phrases[pnr].macro;
509 i += special_phrases[pnr].phrase.length() - 1;
510 column += special_phrases[pnr].macro.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 (par->layout()->isEnvironment()) {
534 par = par->TeXEnvironment(buf, bparams,
537 par = par->TeXOnePar(buf, bparams,
541 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
547 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
548 LyXLayout const & layout) const
550 BufferParams const & bparams = features.bufferParams();
553 if (params.lineTop() || params.lineBottom())
554 features.require("lyxline");
555 if (!params.spacing().isDefault())
556 features.require("setspace");
559 features.useLayout(layout.name());
562 Language const * doc_language = bparams.language;
564 FontList::const_iterator fcit = fontlist.begin();
565 FontList::const_iterator fend = fontlist.end();
566 for (; fcit != fend; ++fcit) {
567 if (fcit->font().noun() == LyXFont::ON) {
568 lyxerr[Debug::LATEX] << "font.noun: "
569 << fcit->font().noun()
571 features.require("noun");
572 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
573 << fcit->font().stateText(0)
576 switch (fcit->font().color()) {
578 case LColor::inherit:
580 // probably we should put here all interface colors used for
581 // font displaying! For now I just add this ones I know of (Jug)
586 features.require("color");
587 lyxerr[Debug::LATEX] << "Color enabled. Font: "
588 << fcit->font().stateText(0)
592 Language const * language = fcit->font().language();
593 if (language->babel() != doc_language->babel() &&
594 language != ignore_language &&
595 language != latex_language)
597 features.useLanguage(language);
598 lyxerr[Debug::LATEX] << "Found language "
599 << language->babel() << endl;
603 if (!params.leftIndent().zero())
604 features.require("ParagraphLeftIndent");
607 InsetList::iterator icit = owner_->insetlist.begin();
608 InsetList::iterator iend = owner_->insetlist.end();
609 for (; icit != iend; ++icit) {
610 if (icit.getInset()) {
611 icit.getInset()->validate(features);
612 if (layout.needprotect &&
613 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
614 features.require("NeedLyXFootnoteCode");
619 for (pos_type i = 0; i < size() ; ++i) {
620 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
621 if (!special_phrases[pnr].builtin
622 && isTextAt(special_phrases[pnr].phrase, i)) {
623 features.require(special_phrases[pnr].phrase);
631 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
633 InsetList::iterator cit = owner_->insetlist.begin();
634 InsetList::iterator lend = owner_->insetlist.end();
636 for (; cit != lend; ++cit) {
637 if ((result = cit.getInset()->getParFromID(id)))
644 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
645 BufferParams const & bparams) const
647 LyXFont tmpfont(font);
649 // check for environment font information
650 char par_depth = owner_->getDepth();
651 Paragraph const * par = owner_;
652 LyXTextClass const & tclass = bparams.getLyXTextClass();
654 while (par && par->getDepth() && !tmpfont.resolved()) {
655 par = par->outerHook();
657 tmpfont.realize(par->layout()->font);
658 par_depth = par->getDepth();
662 tmpfont.realize(tclass.defaultfont());