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 int & column, LyXFont const & font,
215 LyXLayout const & style)
217 if (style.pass_thru) return;
218 if (column > lyxrc.ascii_linelen
220 && getChar(i - 1) != ' '
222 // same in FreeSpacing mode
223 && !style.free_spacing
224 && !owner_->isFreeSpacing()
225 // In typewriter mode, we want to avoid
226 // ! . ? : at the end of a line
227 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
228 && (getChar(i - 1) == '.'
229 || getChar(i - 1) == '?'
230 || getChar(i - 1) == ':'
231 || getChar(i - 1) == '!'))) {
234 texrow.start(owner_, i + 1);
236 } else if (style.free_spacing) {
244 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
246 pos_type const len = str.length();
248 // is the paragraph large enough?
249 if (pos + len > size())
252 // does the wanted text start at point?
253 for (string::size_type i = 0; i < str.length(); ++i) {
254 if (str[i] != text[pos + i])
258 // is there a font change in middle of the word?
259 FontList::const_iterator cit = fontlist.begin();
260 FontList::const_iterator end = fontlist.end();
261 for (; cit != end; ++cit) {
262 if (cit->pos() >= pos)
265 if (cit != end && pos + len - 1 > cit->pos())
272 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
273 BufferParams const & bparams,
278 LyXFont & running_font,
281 LyXLayout const & style,
286 if (style.pass_thru) {
287 if (c != '\0') os << c;
290 // Two major modes: LaTeX or plain
291 // Handle here those cases common to both modes
292 // and then split to handle the two modes separately.
294 case Paragraph::META_INSET: {
295 Inset * inset = owner_->getInset(i);
298 int const len = os.tellp();
299 //ostream::pos_type const len = os.tellp();
300 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
301 || inset->lyxCode() == Inset::MATH_CODE
302 || inset->lyxCode() == Inset::URL_CODE)
303 && running_font.isRightToLeft()) {
309 #warning Bug: we can have an empty font change here!
310 // if there has just been a font change, we are going to close it
311 // right now, which means stupid latex code like \textsf{}. AFAIK,
312 // this does not harm dvi output. A minor bug, thus (JMarc)
314 // some insets cannot be inside a font change command
315 if (open_font && inset->noFontChange()) {
316 column +=running_font.
317 latexWriteEndChanges(os,
321 basefont = owner_->getLayoutFont(bparams);
322 running_font = basefont;
325 int tmp = inset->latex(buf, os, moving_arg,
332 for (int j = 0; j < tmp; ++j) {
335 texrow.start(owner_, i + 1);
338 column += int(os.tellp()) - len;
344 case Paragraph::META_NEWLINE:
346 column += running_font.latexWriteEndChanges(os,
351 basefont = owner_->getLayoutFont(bparams);
352 running_font = basefont;
355 case Paragraph::META_HFILL:
361 // And now for the special cases within each mode
365 os << "\\textbackslash{}";
369 case '±': case '²': case '³':
370 case '×': case '÷': case '¹':
371 case '¬': case 'µ':
372 if ((bparams.inputenc == "latin1" ||
373 bparams.inputenc == "latin9") ||
374 (bparams.inputenc == "auto" &&
375 (font.language()->encoding()->LatexName()
377 font.language()->encoding()->LatexName()
379 os << "\\ensuremath{"
388 case '|': case '<': case '>':
389 // In T1 encoding, these characters exist
390 if (lyxrc.fontenc == "T1") {
392 //... but we should avoid ligatures
393 if ((c == '>' || c == '<')
395 && getChar(i + 1) == c) {
396 //os << "\\textcompwordmark{}";
397 // Jean-Marc, have a look at
398 // this. IÂ think this works
406 // Typewriter font also has them
407 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
411 // Otherwise, we use what LaTeX
415 os << "\\textless{}";
419 os << "\\textgreater{}";
429 case '-': // "--" in Typewriter mode -> "-{}-"
431 && getChar(i + 1) == '-'
432 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
441 os << "\\char`\\\"{}";
446 if (bparams.inputenc == "default") {
455 case '%': case '#': case '{':
462 os << "\\textasciitilde{}";
467 os << "\\textasciicircum{}";
471 case '*': case '[': case ']':
472 // avoid being mistaken for optional arguments
473 os << '{' << c << '}';
478 // Blanks are printed before font switching.
479 // Sure? I am not! (try nice-latex)
480 // I am sure it's correct. LyX might be smarter
481 // in the future, but for now, nothing wrong is
487 // I assume this is hack treating typewriter as verbatim
488 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
497 // FIXME: if we have "LaTeX" with a font
498 // change in the middle (before the 'T', then
499 // the "TeX" part is still special cased.
500 // Really we should only operate this on
501 // "words" for some definition of word
505 for (; pnr < phrases_nr; ++pnr) {
506 if (isTextAt(special_phrases[pnr].phrase, i)) {
507 os << special_phrases[pnr].macro;
508 i += special_phrases[pnr].phrase.length() - 1;
509 column += special_phrases[pnr].macro.length() - 1;
514 if (pnr == phrases_nr && c != '\0') {
524 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
525 BufferParams const & bparams,
526 ostream & os, TexRow & texrow)
528 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
529 Paragraph * par = owner_;
531 while (par && par->params().depth() == owner_->params().depth()) {
532 if (par->layout()->isEnvironment()) {
533 par = par->TeXEnvironment(buf, bparams,
536 par = par->TeXOnePar(buf, bparams,
540 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
546 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
547 LyXLayout const & layout) const
549 BufferParams const & bparams = features.bufferParams();
552 if (params.lineTop() || params.lineBottom())
553 features.require("lyxline");
554 if (!params.spacing().isDefault())
555 features.require("setspace");
558 features.useLayout(layout.name());
561 Language const * doc_language = bparams.language;
563 FontList::const_iterator fcit = fontlist.begin();
564 FontList::const_iterator fend = fontlist.end();
565 for (; fcit != fend; ++fcit) {
566 if (fcit->font().noun() == LyXFont::ON) {
567 lyxerr[Debug::LATEX] << "font.noun: "
568 << fcit->font().noun()
570 features.require("noun");
571 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
572 << fcit->font().stateText(0)
575 switch (fcit->font().color()) {
577 case LColor::inherit:
579 // probably we should put here all interface colors used for
580 // font displaying! For now I just add this ones I know of (Jug)
585 features.require("color");
586 lyxerr[Debug::LATEX] << "Color enabled. Font: "
587 << fcit->font().stateText(0)
591 Language const * language = fcit->font().language();
592 if (language->babel() != doc_language->babel() &&
593 language != ignore_language &&
594 language != latex_language)
596 features.useLanguage(language);
597 lyxerr[Debug::LATEX] << "Found language "
598 << language->babel() << endl;
602 if (!params.leftIndent().zero())
603 features.require("ParagraphLeftIndent");
606 InsetList::iterator icit = owner_->insetlist.begin();
607 InsetList::iterator iend = owner_->insetlist.end();
608 for (; icit != iend; ++icit) {
609 if (icit.getInset()) {
610 icit.getInset()->validate(features);
611 if (layout.needprotect &&
612 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
613 features.require("NeedLyXFootnoteCode");
618 for (pos_type i = 0; i < size() ; ++i) {
619 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
620 if (!special_phrases[pnr].builtin
621 && isTextAt(special_phrases[pnr].phrase, i)) {
622 features.require(special_phrases[pnr].phrase);
630 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
632 InsetList::iterator cit = owner_->insetlist.begin();
633 InsetList::iterator lend = owner_->insetlist.end();
635 for (; cit != lend; ++cit) {
636 if ((result = cit.getInset()->getParFromID(id)))
643 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
644 BufferParams const & bparams) const
646 LyXFont tmpfont(font);
648 // check for environment font information
649 char par_depth = owner_->getDepth();
650 Paragraph const * par = owner_;
651 LyXTextClass const & tclass = bparams.getLyXTextClass();
653 while (par && par->getDepth() && !tmpfont.resolved()) {
654 par = par->outerHook();
656 tmpfont.realize(par->layout()->font);
657 par_depth = par->getDepth();
661 tmpfont.realize(tclass.defaultfont());