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);
142 // Update the inset table.
143 InsetTable search_inset(pos, 0);
144 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
145 owner_->insetlist.end(),
146 search_inset, matchIT());
147 it != owner_->insetlist.end(); ++it)
151 owner_->setFont(pos, font);
155 void Paragraph::Pimpl::insertInset(pos_type pos,
156 Inset * inset, LyXFont const & font)
159 lyx::Assert(pos <= size());
161 insertChar(pos, META_INSET, font);
162 lyx::Assert(text[pos] == META_INSET);
164 // Add a new entry in the inset table.
165 InsetTable search_inset(pos, 0);
166 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
167 owner_->insetlist.end(),
168 search_inset, matchIT());
169 if (it != owner_->insetlist.end() && it->pos == pos) {
170 lyxerr << "ERROR (Paragraph::InsertInset): "
171 "there is an inset in position: " << pos << endl;
173 owner_->insetlist.insert(it, InsetTable(pos, inset));
174 inset->parOwner(owner_);
178 inset->setOwner(inset_owner);
182 void Paragraph::Pimpl::erase(pos_type pos)
184 lyx::Assert(pos < size());
185 // if it is an inset, delete the inset entry
186 if (text[pos] == Paragraph::META_INSET) {
188 InsetTable search_inset(pos, 0);
189 InsetList::iterator it =
190 lower_bound(owner_->insetlist.begin(),
191 owner_->insetlist.end(),
192 search_inset, matchIT());
193 if (it != owner_->insetlist.end() && it->pos == pos) {
195 owner_->insetlist.erase(it);
199 text.erase(text.begin() + pos);
201 // Erase entries in the tables.
202 FontTable search_font(pos, LyXFont());
204 FontList::iterator it =
205 lower_bound(fontlist.begin(),
207 search_font, matchFT());
208 if (it != fontlist.end() && it->pos() == pos &&
210 (it != fontlist.begin()
211 && boost::prior(it)->pos() == pos - 1))) {
212 // If it is a multi-character font
213 // entry, we just make it smaller
214 // (see update below), otherwise we
216 unsigned int const i = it - fontlist.begin();
217 fontlist.erase(fontlist.begin() + i);
218 it = fontlist.begin() + i;
219 if (i > 0 && i < fontlist.size() &&
220 fontlist[i - 1].font() == fontlist[i].font()) {
221 fontlist.erase(fontlist.begin() + i - 1);
222 it = fontlist.begin() + i - 1;
226 // Update all other entries.
227 FontList::iterator fend = fontlist.end();
228 for (; it != fend; ++it)
229 it->pos(it->pos() - 1);
231 // Update the inset table.
232 InsetTable search_inset(pos, 0);
233 InsetList::iterator lend = owner_->insetlist.end();
234 for (InsetList::iterator it =
235 upper_bound(owner_->insetlist.begin(),
237 search_inset, matchIT());
243 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
245 int & column, LyXFont const & font,
246 LyXLayout const & style)
248 if (style.pass_thru) return;
249 if (column > lyxrc.ascii_linelen
251 && getChar(i - 1) != ' '
253 // same in FreeSpacing mode
254 && !style.free_spacing
255 && !owner_->isFreeSpacing()
256 // In typewriter mode, we want to avoid
257 // ! . ? : at the end of a line
258 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
259 && (getChar(i - 1) == '.'
260 || getChar(i - 1) == '?'
261 || getChar(i - 1) == ':'
262 || getChar(i - 1) == '!'))) {
265 texrow.start(owner_, i + 1);
267 } else if (style.free_spacing) {
275 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
277 pos_type const len = str.length();
279 // is the paragraph large enough?
280 if (pos + len > size())
283 // does the wanted text start at point?
284 for (string::size_type i = 0; i < str.length(); ++i) {
285 if (str[i] != text[pos + i])
289 // is there a font change in middle of the word?
290 FontList::const_iterator cit = fontlist.begin();
291 FontList::const_iterator end = fontlist.end();
292 for (; cit != end; ++cit) {
293 if (cit->pos() >= pos)
296 if (cit != end && pos + len - 1 > cit->pos())
303 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
304 BufferParams const & bparams,
309 LyXFont & running_font,
312 LyXLayout const & style,
317 if (style.pass_thru) {
318 if (c != '\0') os << c;
321 // Two major modes: LaTeX or plain
322 // Handle here those cases common to both modes
323 // and then split to handle the two modes separately.
325 case Paragraph::META_INSET: {
326 Inset * inset = owner_->getInset(i);
329 int const len = os.tellp();
330 //ostream::pos_type const len = os.tellp();
331 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
332 || inset->lyxCode() == Inset::MATH_CODE
333 || inset->lyxCode() == Inset::URL_CODE)
334 && running_font.isRightToLeft()) {
340 #warning Bug: we can have an empty font change here!
341 // if there has just been a font change, we are going to close it
342 // right now, which means stupid latex code like \textsf{}. AFAIK,
343 // this does not harm dvi output. A minor bug, thus (JMarc)
345 // some insets cannot be inside a font change command
346 if (open_font && inset->noFontChange()) {
347 column +=running_font.
348 latexWriteEndChanges(os,
352 basefont = owner_->getLayoutFont(bparams);
353 running_font = basefont;
356 int tmp = inset->latex(buf, os, moving_arg,
363 for (int j = 0; j < tmp; ++j) {
366 texrow.start(owner_, i + 1);
369 column += int(os.tellp()) - len;
375 case Paragraph::META_NEWLINE:
377 column += running_font.latexWriteEndChanges(os,
382 basefont = owner_->getLayoutFont(bparams);
383 running_font = basefont;
386 case Paragraph::META_HFILL:
392 // And now for the special cases within each mode
396 os << "\\textbackslash{}";
400 case '±': case '²': case '³':
401 case '×': case '÷': case '¹':
402 case '¬': case 'µ':
403 if ((bparams.inputenc == "latin1" ||
404 bparams.inputenc == "latin9") ||
405 (bparams.inputenc == "auto" &&
406 (font.language()->encoding()->LatexName()
408 font.language()->encoding()->LatexName()
410 os << "\\ensuremath{"
419 case '|': case '<': case '>':
420 // In T1 encoding, these characters exist
421 if (lyxrc.fontenc == "T1") {
423 //... but we should avoid ligatures
424 if ((c == '>' || c == '<')
426 && getChar(i + 1) == c) {
427 //os << "\\textcompwordmark{}";
428 // Jean-Marc, have a look at
429 // this. IÂ think this works
437 // Typewriter font also has them
438 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
442 // Otherwise, we use what LaTeX
446 os << "\\textless{}";
450 os << "\\textgreater{}";
460 case '-': // "--" in Typewriter mode -> "-{}-"
462 && getChar(i + 1) == '-'
463 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
472 os << "\\char`\\\"{}";
477 if (bparams.inputenc == "default") {
486 case '%': case '#': case '{':
493 os << "\\textasciitilde{}";
498 os << "\\textasciicircum{}";
502 case '*': case '[': case ']':
503 // avoid being mistaken for optional arguments
504 os << '{' << c << '}';
509 // Blanks are printed before font switching.
510 // Sure? I am not! (try nice-latex)
511 // I am sure it's correct. LyX might be smarter
512 // in the future, but for now, nothing wrong is
518 // I assume this is hack treating typewriter as verbatim
519 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
528 // FIXME: if we have "LaTeX" with a font
529 // change in the middle (before the 'T', then
530 // the "TeX" part is still special cased.
531 // Really we should only operate this on
532 // "words" for some definition of word
536 for (; pnr < phrases_nr; ++pnr) {
537 if (isTextAt(special_phrases[pnr].phrase, i)) {
538 os << special_phrases[pnr].macro;
539 i += special_phrases[pnr].phrase.length() - 1;
540 column += special_phrases[pnr].macro.length() - 1;
545 if (pnr == phrases_nr && c != '\0') {
555 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
556 BufferParams const & bparams,
557 ostream & os, TexRow & texrow)
559 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
560 Paragraph * par = owner_;
562 while (par && par->params().depth() == owner_->params().depth()) {
563 if (par->layout()->isEnvironment()) {
564 par = par->TeXEnvironment(buf, bparams,
567 par = par->TeXOnePar(buf, bparams,
571 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
577 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
578 LyXLayout const & layout) const
580 BufferParams const & bparams = features.bufferParams();
583 if (params.lineTop() || params.lineBottom())
584 features.require("lyxline");
585 if (!params.spacing().isDefault())
586 features.require("setspace");
589 features.useLayout(layout.name());
592 Language const * doc_language = bparams.language;
594 FontList::const_iterator fcit = fontlist.begin();
595 FontList::const_iterator fend = fontlist.end();
596 for (; fcit != fend; ++fcit) {
597 if (fcit->font().noun() == LyXFont::ON) {
598 lyxerr[Debug::LATEX] << "font.noun: "
599 << fcit->font().noun()
601 features.require("noun");
602 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
603 << fcit->font().stateText(0)
606 switch (fcit->font().color()) {
608 case LColor::inherit:
610 // probably we should put here all interface colors used for
611 // font displaying! For now I just add this ones I know of (Jug)
616 features.require("color");
617 lyxerr[Debug::LATEX] << "Color enabled. Font: "
618 << fcit->font().stateText(0)
622 Language const * language = fcit->font().language();
623 if (language->babel() != doc_language->babel() &&
624 language != ignore_language &&
625 #ifdef INHERIT_LANGUAGE
626 language != inherit_language &&
628 language != latex_language)
630 features.useLanguage(language);
631 lyxerr[Debug::LATEX] << "Found language "
632 << language->babel() << endl;
636 if (!params.leftIndent().zero())
637 features.require("ParagraphLeftIndent");
640 InsetList::const_iterator icit = owner_->insetlist.begin();
641 InsetList::const_iterator iend = owner_->insetlist.end();
642 for (; icit != iend; ++icit) {
644 icit->inset->validate(features);
645 if (layout.needprotect &&
646 icit->inset->lyxCode() == Inset::FOOT_CODE)
647 features.require("NeedLyXFootnoteCode");
652 for (pos_type i = 0; i < size() ; ++i) {
653 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
654 if (!special_phrases[pnr].builtin
655 && isTextAt(special_phrases[pnr].phrase, i)) {
656 features.require(special_phrases[pnr].phrase);
664 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
666 InsetList::const_iterator cit = owner_->insetlist.begin();
667 InsetList::const_iterator lend = owner_->insetlist.end();
669 for (; cit != lend; ++cit) {
670 if ((result = cit->inset->getParFromID(id)))
677 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
678 BufferParams const & bparams) const
680 LyXFont tmpfont(font);
682 // check for environment font information
683 char par_depth = owner_->getDepth();
684 Paragraph const * par = owner_;
685 LyXTextClass const & tclass = bparams.getLyXTextClass();
687 while (par && par->getDepth() && !tmpfont.resolved()) {
688 par = par->outerHook();
690 tmpfont.realize(par->layout()->font
691 #ifdef INHERIT_LANGUAGE
695 par_depth = par->getDepth();
699 tmpfont.realize(tclass.defaultfont()
700 #ifdef INHERIT_LANGUAGE