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 lyx::Assert(pos <= size());
93 // This is stronger, and I belive that this is the assertion
94 // that we should really use. (Lgb)
95 //Assert(pos < size());
97 // Then this has no meaning. (Lgb)
98 if (!size() || pos == size()) return '\0';
104 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
110 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
111 LyXFont const & font)
113 lyx::Assert(pos <= size());
115 text.insert(text.begin() + pos, c);
117 // Update the font table.
118 FontTable search_font(pos, LyXFont());
119 for (FontList::iterator it = lower_bound(fontlist.begin(),
121 search_font, matchFT());
122 it != fontlist.end(); ++it)
124 it->pos(it->pos() + 1);
127 // Update the inset table.
128 InsetTable search_inset(pos, 0);
129 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
130 owner_->insetlist.end(),
131 search_inset, matchIT());
132 it != owner_->insetlist.end(); ++it)
136 owner_->setFont(pos, font);
140 void Paragraph::Pimpl::insertInset(pos_type pos,
141 Inset * inset, LyXFont const & font)
144 lyx::Assert(pos <= size());
146 insertChar(pos, META_INSET, font);
147 lyx::Assert(text[pos] == META_INSET);
149 // Add a new entry in the inset table.
150 InsetTable search_inset(pos, 0);
151 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
152 owner_->insetlist.end(),
153 search_inset, matchIT());
154 if (it != owner_->insetlist.end() && it->pos == pos) {
155 lyxerr << "ERROR (Paragraph::InsertInset): "
156 "there is an inset in position: " << pos << endl;
158 owner_->insetlist.insert(it, InsetTable(pos, inset));
159 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) {
173 InsetTable search_inset(pos, 0);
174 InsetList::iterator it =
175 lower_bound(owner_->insetlist.begin(),
176 owner_->insetlist.end(),
177 search_inset, matchIT());
178 if (it != owner_->insetlist.end() && it->pos == pos) {
180 owner_->insetlist.erase(it);
184 text.erase(text.begin() + pos);
186 // Erase entries in the tables.
187 FontTable search_font(pos, LyXFont());
189 FontList::iterator it =
190 lower_bound(fontlist.begin(),
192 search_font, matchFT());
193 if (it != fontlist.end() && it->pos() == pos &&
195 (it != fontlist.begin()
196 && boost::prior(it)->pos() == pos - 1))) {
197 // If it is a multi-character font
198 // entry, we just make it smaller
199 // (see update below), otherwise we
201 unsigned int const i = it - fontlist.begin();
202 fontlist.erase(fontlist.begin() + i);
203 it = fontlist.begin() + i;
204 if (i > 0 && i < fontlist.size() &&
205 fontlist[i - 1].font() == fontlist[i].font()) {
206 fontlist.erase(fontlist.begin() + i - 1);
207 it = fontlist.begin() + i - 1;
211 // Update all other entries.
212 FontList::iterator fend = fontlist.end();
213 for (; it != fend; ++it)
214 it->pos(it->pos() - 1);
216 // Update the inset table.
217 InsetTable search_inset(pos, 0);
218 InsetList::iterator lend = owner_->insetlist.end();
219 for (InsetList::iterator it =
220 upper_bound(owner_->insetlist.begin(),
222 search_inset, matchIT());
228 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
230 int & column, LyXFont const & font,
231 LyXLayout const & style)
233 if (style.pass_thru) return;
234 if (column > tex_code_break_column
236 && getChar(i - 1) != ' '
238 // same in FreeSpacing mode
239 && !style.free_spacing
240 && !owner_->isFreeSpacing()
241 // In typewriter mode, we want to avoid
242 // ! . ? : at the end of a line
243 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
244 && (getChar(i - 1) == '.'
245 || getChar(i - 1) == '?'
246 || getChar(i - 1) == ':'
247 || getChar(i - 1) == '!'))) {
248 if (tex_code_break_column == 0) {
249 // in batchmode we need LaTeX to still
250 // see it as a space not as an extra '\n'
256 texrow.start(owner_, i + 1);
258 } else if (style.free_spacing) {
266 bool Paragraph::Pimpl::isTextAt(BufferParams const & bp,
267 string const & str, pos_type pos)
269 LyXFont const & font = owner_->getFont(bp, pos);
271 for (string::size_type i = 0; i < str.length(); ++i) {
272 if (pos + static_cast<pos_type>(i) >= size())
274 if (str[i] != getChar(pos + i))
276 if (owner_->getFont(bp, pos + i) != font)
283 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
284 BufferParams const & bparams,
289 LyXFont & running_font,
292 LyXLayout const & style,
297 if (style.pass_thru) {
298 if (c != '\0') os << c;
301 // Two major modes: LaTeX or plain
302 // Handle here those cases common to both modes
303 // and then split to handle the two modes separately.
305 case Paragraph::META_INSET: {
306 Inset * inset = owner_->getInset(i);
309 int const len = os.tellp();
310 //ostream::pos_type const len = os.tellp();
311 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
312 || inset->lyxCode() == Inset::MATH_CODE
313 || inset->lyxCode() == Inset::URL_CODE)
314 && running_font.isRightToLeft()) {
319 int tmp = inset->latex(buf, os, moving_arg,
326 for (int j = 0; j < tmp; ++j) {
329 texrow.start(owner_, i + 1);
332 column += int(os.tellp()) - len;
338 case Paragraph::META_NEWLINE:
340 column += running_font.latexWriteEndChanges(os,
345 basefont = owner_->getLayoutFont(bparams);
346 running_font = basefont;
349 case Paragraph::META_HFILL:
355 // And now for the special cases within each mode
359 os << "\\textbackslash{}";
363 case '±': case '²': case '³':
364 case '×': case '÷': case '¹':
366 if ((bparams.inputenc == "latin1" ||
367 bparams.inputenc == "latin9") ||
368 (bparams.inputenc == "auto" &&
369 (font.language()->encoding()->LatexName()
371 font.language()->encoding()->LatexName()
373 os << "\\ensuremath{"
382 case '|': case '<': case '>':
383 // In T1 encoding, these characters exist
384 if (lyxrc.fontenc == "T1") {
386 //... but we should avoid ligatures
387 if ((c == '>' || c == '<')
389 && getChar(i + 1) == c) {
390 //os << "\\textcompwordmark{}";
391 // Jean-Marc, have a look at
392 // this. I think this works
400 // Typewriter font also has them
401 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
405 // Otherwise, we use what LaTeX
409 os << "\\textless{}";
413 os << "\\textgreater{}";
423 case '-': // "--" in Typewriter mode -> "-{}-"
425 && getChar(i + 1) == '-'
426 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
435 os << "\\char`\\\"{}";
440 if (bparams.inputenc == "default") {
449 case '%': case '#': case '{':
456 os << "\\textasciitilde{}";
461 os << "\\textasciicircum{}";
465 case '*': case '[': case ']':
466 // avoid being mistaken for optional arguments
467 os << '{' << c << '}';
472 // Blanks are printed before font switching.
473 // Sure? I am not! (try nice-latex)
474 // I am sure it's correct. LyX might be smarter
475 // in the future, but for now, nothing wrong is
481 // I assume this is hack treating typewriter as verbatim
482 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
491 // FIXME: if we have "LaTeX" with a font change in the middle (before
492 // the 'T', then the "TeX" part is still special cased. Really we
493 // should only operate this on "words" for some definition of word
497 for (; pnr < phrases_nr; ++pnr) {
498 if (isTextAt(bparams, special_phrases[pnr][0], i)) {
499 os << special_phrases[pnr][1];
500 i += special_phrases[pnr][0].length() - 1;
501 column += special_phrases[pnr][1].length() - 1;
506 if (pnr == phrases_nr && c != '\0') {
516 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
517 BufferParams const & bparams,
518 ostream & os, TexRow & texrow)
520 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
521 Paragraph * par = owner_;
523 while (par && par->params().depth() == owner_->params().depth()) {
524 if (textclasslist[bparams.textclass][par->layout()].isEnvironment()) {
525 par = par->TeXEnvironment(buf, bparams,
528 par = par->TeXOnePar(buf, bparams,
532 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
538 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
540 InsetList::const_iterator cit = owner_->insetlist.begin();
541 InsetList::const_iterator lend = owner_->insetlist.end();
543 for (; cit != lend; ++cit) {
544 if ((result = cit->inset->getParFromID(id)))
551 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
552 BufferParams const & bparams) const
554 LyXFont tmpfont(font);
556 // check for environment font information
557 char par_depth = owner_->getDepth();
558 Paragraph const * par = owner_;
559 LyXTextClass const & tclass = textclasslist[bparams.textclass];
561 while (par && par->getDepth() && !tmpfont.resolved()) {
562 par = par->outerHook();
564 tmpfont.realize(tclass[par->layout()].font
565 #ifdef INHERIT_LANGUAGE
569 par_depth = par->getDepth();
573 tmpfont.realize(tclass.defaultfont()
574 #ifdef INHERIT_LANGUAGE