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"
20 #include "bufferparams.h"
24 #include "lyxtextclasslist.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 string special_phrases[][2] = {
47 { "LaTeX2e", "\\LaTeXe{}" },
48 { "LaTeX", "\\LaTeX{}" },
51 size_t phrases_nr = sizeof(special_phrases)/sizeof(special_phrases[0]);
56 Paragraph::Pimpl::Pimpl(Paragraph * owner)
64 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
65 : params(p.params), owner_(owner)
67 inset_owner = p.inset_owner;
69 fontlist = p.fontlist;
77 void Paragraph::Pimpl::clear()
83 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
86 text = par->pimpl_->text;
90 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
92 // This is in the critical path for loading!
93 pos_type const siz = size();
94 lyx::Assert(pos <= siz);
95 // This is stronger, and I belive that this is the assertion
96 // that we should really use. (Lgb)
97 //Assert(pos < size());
99 // Then this has no meaning. (Lgb)
100 if (!siz || pos == siz)
107 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
113 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
114 LyXFont const & font)
116 lyx::Assert(pos <= size());
118 // This is actually very common when parsing buffers (and
119 // maybe inserting ascii text)
121 // when appending characters, no need to update tables
123 owner_->setFont(pos, font);
127 text.insert(text.begin() + pos, c);
129 // Update the font table.
130 FontTable search_font(pos, LyXFont());
131 for (FontList::iterator it = lower_bound(fontlist.begin(),
133 search_font, matchFT());
134 it != fontlist.end(); ++it)
136 it->pos(it->pos() + 1);
139 // Update the inset table.
140 InsetTable search_inset(pos, 0);
141 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
142 owner_->insetlist.end(),
143 search_inset, matchIT());
144 it != owner_->insetlist.end(); ++it)
148 owner_->setFont(pos, font);
152 void Paragraph::Pimpl::insertInset(pos_type pos,
153 Inset * inset, LyXFont const & font)
156 lyx::Assert(pos <= size());
158 insertChar(pos, META_INSET, font);
159 lyx::Assert(text[pos] == META_INSET);
161 // Add a new entry in the inset table.
162 InsetTable search_inset(pos, 0);
163 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
164 owner_->insetlist.end(),
165 search_inset, matchIT());
166 if (it != owner_->insetlist.end() && it->pos == pos) {
167 lyxerr << "ERROR (Paragraph::InsertInset): "
168 "there is an inset in position: " << pos << endl;
170 owner_->insetlist.insert(it, InsetTable(pos, inset));
171 inset->parOwner(owner_);
175 inset->setOwner(inset_owner);
179 void Paragraph::Pimpl::erase(pos_type pos)
181 lyx::Assert(pos < size());
182 // if it is an inset, delete the inset entry
183 if (text[pos] == Paragraph::META_INSET) {
185 InsetTable search_inset(pos, 0);
186 InsetList::iterator it =
187 lower_bound(owner_->insetlist.begin(),
188 owner_->insetlist.end(),
189 search_inset, matchIT());
190 if (it != owner_->insetlist.end() && it->pos == pos) {
192 owner_->insetlist.erase(it);
196 text.erase(text.begin() + pos);
198 // Erase entries in the tables.
199 FontTable search_font(pos, LyXFont());
201 FontList::iterator it =
202 lower_bound(fontlist.begin(),
204 search_font, matchFT());
205 if (it != fontlist.end() && it->pos() == pos &&
207 (it != fontlist.begin()
208 && boost::prior(it)->pos() == pos - 1))) {
209 // If it is a multi-character font
210 // entry, we just make it smaller
211 // (see update below), otherwise we
213 unsigned int const i = it - fontlist.begin();
214 fontlist.erase(fontlist.begin() + i);
215 it = fontlist.begin() + i;
216 if (i > 0 && i < fontlist.size() &&
217 fontlist[i - 1].font() == fontlist[i].font()) {
218 fontlist.erase(fontlist.begin() + i - 1);
219 it = fontlist.begin() + i - 1;
223 // Update all other entries.
224 FontList::iterator fend = fontlist.end();
225 for (; it != fend; ++it)
226 it->pos(it->pos() - 1);
228 // Update the inset table.
229 InsetTable search_inset(pos, 0);
230 InsetList::iterator lend = owner_->insetlist.end();
231 for (InsetList::iterator it =
232 upper_bound(owner_->insetlist.begin(),
234 search_inset, matchIT());
240 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
242 int & column, LyXFont const & font,
243 LyXLayout const & style)
245 if (style.pass_thru) return;
246 if (column > tex_code_break_column
248 && getChar(i - 1) != ' '
250 // same in FreeSpacing mode
251 && !style.free_spacing
252 && !owner_->isFreeSpacing()
253 // In typewriter mode, we want to avoid
254 // ! . ? : at the end of a line
255 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
256 && (getChar(i - 1) == '.'
257 || getChar(i - 1) == '?'
258 || getChar(i - 1) == ':'
259 || getChar(i - 1) == '!'))) {
260 if (tex_code_break_column == 0) {
261 // in batchmode we need LaTeX to still
262 // see it as a space not as an extra '\n'
268 texrow.start(owner_, i + 1);
270 } else if (style.free_spacing) {
278 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos)
280 pos_type const len = str.length();
282 // is the paragraph large enough?
283 if (pos + len > size())
286 // does the wanted text start at point?
287 for (string::size_type i = 0; i < str.length(); ++i) {
288 if (str[i] != text[pos + i])
292 // is there a font change in middle of the word?
293 FontList::const_iterator cit = fontlist.begin();
294 FontList::const_iterator end = fontlist.end();
295 for (; cit != end; ++cit) {
296 if (cit->pos() >= pos)
299 if (cit != end && pos + len - 1 > cit->pos())
306 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
307 BufferParams const & bparams,
312 LyXFont & running_font,
315 LyXLayout const & style,
320 if (style.pass_thru) {
321 if (c != '\0') os << c;
324 // Two major modes: LaTeX or plain
325 // Handle here those cases common to both modes
326 // and then split to handle the two modes separately.
328 case Paragraph::META_INSET: {
329 Inset * inset = owner_->getInset(i);
332 int const len = os.tellp();
333 //ostream::pos_type const len = os.tellp();
334 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
335 || inset->lyxCode() == Inset::MATH_CODE
336 || inset->lyxCode() == Inset::URL_CODE)
337 && running_font.isRightToLeft()) {
342 int tmp = inset->latex(buf, os, moving_arg,
349 for (int j = 0; j < tmp; ++j) {
352 texrow.start(owner_, i + 1);
355 column += int(os.tellp()) - len;
361 case Paragraph::META_NEWLINE:
363 column += running_font.latexWriteEndChanges(os,
368 basefont = owner_->getLayoutFont(bparams);
369 running_font = basefont;
372 case Paragraph::META_HFILL:
378 // And now for the special cases within each mode
382 os << "\\textbackslash{}";
386 case '±': case '²': case '³':
387 case '×': case '÷': case '¹':
389 if ((bparams.inputenc == "latin1" ||
390 bparams.inputenc == "latin9") ||
391 (bparams.inputenc == "auto" &&
392 (font.language()->encoding()->LatexName()
394 font.language()->encoding()->LatexName()
396 os << "\\ensuremath{"
405 case '|': case '<': case '>':
406 // In T1 encoding, these characters exist
407 if (lyxrc.fontenc == "T1") {
409 //... but we should avoid ligatures
410 if ((c == '>' || c == '<')
412 && getChar(i + 1) == c) {
413 //os << "\\textcompwordmark{}";
414 // Jean-Marc, have a look at
415 // this. I think this works
423 // Typewriter font also has them
424 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
428 // Otherwise, we use what LaTeX
432 os << "\\textless{}";
436 os << "\\textgreater{}";
446 case '-': // "--" in Typewriter mode -> "-{}-"
448 && getChar(i + 1) == '-'
449 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
458 os << "\\char`\\\"{}";
463 if (bparams.inputenc == "default") {
472 case '%': case '#': case '{':
479 os << "\\textasciitilde{}";
484 os << "\\textasciicircum{}";
488 case '*': case '[': case ']':
489 // avoid being mistaken for optional arguments
490 os << '{' << c << '}';
495 // Blanks are printed before font switching.
496 // Sure? I am not! (try nice-latex)
497 // I am sure it's correct. LyX might be smarter
498 // in the future, but for now, nothing wrong is
504 // I assume this is hack treating typewriter as verbatim
505 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
514 // FIXME: if we have "LaTeX" with a font change in the middle (before
515 // the 'T', then the "TeX" part is still special cased. Really we
516 // should only operate this on "words" for some definition of word
520 for (; pnr < phrases_nr; ++pnr) {
521 if (isTextAt(special_phrases[pnr][0], i)) {
522 os << special_phrases[pnr][1];
523 i += special_phrases[pnr][0].length() - 1;
524 column += special_phrases[pnr][1].length() - 1;
529 if (pnr == phrases_nr && c != '\0') {
539 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
540 BufferParams const & bparams,
541 ostream & os, TexRow & texrow)
543 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
544 Paragraph * par = owner_;
546 while (par && par->params().depth() == owner_->params().depth()) {
547 if (textclasslist[bparams.textclass][par->layout()].isEnvironment()) {
548 par = par->TeXEnvironment(buf, bparams,
551 par = par->TeXOnePar(buf, bparams,
555 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
561 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
563 InsetList::const_iterator cit = owner_->insetlist.begin();
564 InsetList::const_iterator lend = owner_->insetlist.end();
566 for (; cit != lend; ++cit) {
567 if ((result = cit->inset->getParFromID(id)))
574 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
575 BufferParams const & bparams) const
577 LyXFont tmpfont(font);
579 // check for environment font information
580 char par_depth = owner_->getDepth();
581 Paragraph const * par = owner_;
582 LyXTextClass const & tclass = textclasslist[bparams.textclass];
584 while (par && par->getDepth() && !tmpfont.resolved()) {
585 par = par->outerHook();
587 tmpfont.realize(tclass[par->layout()].font
588 #ifdef INHERIT_LANGUAGE
592 par_depth = par->getDepth();
596 tmpfont.realize(tclass.defaultfont()
597 #ifdef INHERIT_LANGUAGE