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"
30 extern int tex_code_break_column;
33 // Initialize static member.
34 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
35 // Initialization of the counter for the paragraph id's,
36 unsigned int Paragraph::Pimpl::paragraph_id = 0;
40 string special_phrases[][2] = {
43 { "LaTeX2e", "\\LaTeXe{}" },
44 { "LaTeX", "\\LaTeX{}" },
47 size_t phrases_nr = sizeof(special_phrases)/sizeof(special_phrases[0]);
52 Paragraph::Pimpl::Pimpl(Paragraph * owner)
60 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
61 : params(p.params), owner_(owner)
63 inset_owner = p.inset_owner;
65 fontlist = p.fontlist;
73 void Paragraph::Pimpl::clear()
79 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
82 text = par->pimpl_->text;
86 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
88 lyx::Assert(pos <= size());
89 // This is stronger, and I belive that this is the assertion
90 // that we should really use. (Lgb)
91 //Assert(pos < size());
93 // Then this has no meaning. (Lgb)
94 if (!size() || pos == size()) return '\0';
100 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
106 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
107 LyXFont const & font)
109 lyx::Assert(pos <= size());
111 text.insert(text.begin() + pos, c);
113 // Update the font table.
114 FontTable search_font(pos, LyXFont());
115 for (FontList::iterator it = std::lower_bound(fontlist.begin(),
117 search_font, matchFT());
118 it != fontlist.end(); ++it)
120 it->pos(it->pos() + 1);
123 // Update the inset table.
124 InsetTable search_inset(pos, 0);
125 for (InsetList::iterator it = std::lower_bound(owner_->insetlist.begin(),
126 owner_->insetlist.end(),
127 search_inset, matchIT());
128 it != owner_->insetlist.end(); ++it)
132 owner_->setFont(pos, font);
136 void Paragraph::Pimpl::insertInset(pos_type pos,
137 Inset * inset, LyXFont const & font)
140 lyx::Assert(pos <= size());
142 insertChar(pos, META_INSET, font);
143 lyx::Assert(text[pos] == META_INSET);
145 // Add a new entry in the inset table.
146 InsetTable search_inset(pos, 0);
147 InsetList::iterator it = std::lower_bound(owner_->insetlist.begin(),
148 owner_->insetlist.end(),
149 search_inset, matchIT());
150 if (it != owner_->insetlist.end() && it->pos == pos) {
151 lyxerr << "ERROR (Paragraph::InsertInset): "
152 "there is an inset in position: " << pos << std::endl;
154 owner_->insetlist.insert(it, InsetTable(pos, inset));
155 inset->parOwner(owner_);
159 inset->setOwner(inset_owner);
163 void Paragraph::Pimpl::erase(pos_type pos)
165 lyx::Assert(pos < size());
166 // if it is an inset, delete the inset entry
167 if (text[pos] == Paragraph::META_INSET) {
169 InsetTable search_inset(pos, 0);
170 InsetList::iterator it =
171 std::lower_bound(owner_->insetlist.begin(),
172 owner_->insetlist.end(),
173 search_inset, matchIT());
174 if (it != owner_->insetlist.end() && it->pos == pos) {
176 owner_->insetlist.erase(it);
180 text.erase(text.begin() + pos);
182 // Erase entries in the tables.
183 FontTable search_font(pos, LyXFont());
185 FontList::iterator it =
186 std::lower_bound(fontlist.begin(),
188 search_font, matchFT());
189 if (it != fontlist.end() && it->pos() == pos &&
191 (it != fontlist.begin()
192 && boost::prior(it)->pos() == pos - 1))) {
193 // If it is a multi-character font
194 // entry, we just make it smaller
195 // (see update below), otherwise we
197 unsigned int const i = it - fontlist.begin();
198 fontlist.erase(fontlist.begin() + i);
199 it = fontlist.begin() + i;
200 if (i > 0 && i < fontlist.size() &&
201 fontlist[i - 1].font() == fontlist[i].font()) {
202 fontlist.erase(fontlist.begin() + i - 1);
203 it = fontlist.begin() + i - 1;
207 // Update all other entries.
208 FontList::iterator fend = fontlist.end();
209 for (; it != fend; ++it)
210 it->pos(it->pos() - 1);
212 // Update the inset table.
213 InsetTable search_inset(pos, 0);
214 InsetList::iterator lend = owner_->insetlist.end();
215 for (InsetList::iterator it =
216 std::upper_bound(owner_->insetlist.begin(),
218 search_inset, matchIT());
224 void Paragraph::Pimpl::simpleTeXBlanks(std::ostream & os, TexRow & texrow,
226 int & column, LyXFont const & font,
227 LyXLayout const & style)
229 if (style.pass_thru) return;
230 if (column > tex_code_break_column
232 && getChar(i - 1) != ' '
234 // same in FreeSpacing mode
235 && !style.free_spacing
236 && !owner_->isFreeSpacing()
237 // In typewriter mode, we want to avoid
238 // ! . ? : at the end of a line
239 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
240 && (getChar(i - 1) == '.'
241 || getChar(i - 1) == '?'
242 || getChar(i - 1) == ':'
243 || getChar(i - 1) == '!'))) {
244 if (tex_code_break_column == 0) {
245 // in batchmode we need LaTeX to still
246 // see it as a space not as an extra '\n'
252 texrow.start(owner_, i + 1);
254 } else if (style.free_spacing) {
262 bool Paragraph::Pimpl::isTextAt(BufferParams const & bp,
263 string const & str, pos_type pos)
265 LyXFont const & font = owner_->getFont(bp, pos);
267 for (string::size_type i = 0; i < str.length(); ++i) {
268 if (pos + static_cast<pos_type>(i) >= size())
270 if (str[i] != getChar(pos + i))
272 if (owner_->getFont(bp, pos + i) != font)
279 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
280 BufferParams const & bparams,
285 LyXFont & running_font,
288 LyXLayout const & style,
293 if (style.pass_thru) {
294 if (c != '\0') os << c;
297 // Two major modes: LaTeX or plain
298 // Handle here those cases common to both modes
299 // and then split to handle the two modes separately.
301 case Paragraph::META_INSET: {
302 Inset * inset = owner_->getInset(i);
305 int const len = os.tellp();
306 //ostream::pos_type const len = os.tellp();
307 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
308 || inset->lyxCode() == Inset::MATH_CODE
309 || inset->lyxCode() == Inset::URL_CODE)
310 && running_font.isRightToLeft()) {
315 int tmp = inset->latex(buf, os, moving_arg,
322 for (int j = 0; j < tmp; ++j) {
325 texrow.start(owner_, i + 1);
328 column += int(os.tellp()) - len;
334 case Paragraph::META_NEWLINE:
336 column += running_font.latexWriteEndChanges(os,
341 basefont = owner_->getLayoutFont(bparams);
342 running_font = basefont;
345 case Paragraph::META_HFILL:
351 // And now for the special cases within each mode
355 os << "\\textbackslash{}";
359 case '°': case '±': case '²': case '³':
360 case '×': case '÷': case '¹': case 'ª':
361 case 'º': case '¬': case 'µ':
362 if ((bparams.inputenc == "latin1" ||
363 bparams.inputenc == "latin9") ||
364 (bparams.inputenc == "auto" &&
365 (font.language()->encoding()->LatexName()
367 font.language()->encoding()->LatexName()
369 os << "\\ensuremath{"
378 case '|': case '<': case '>':
379 // In T1 encoding, these characters exist
380 if (lyxrc.fontenc == "T1") {
382 //... but we should avoid ligatures
383 if ((c == '>' || c == '<')
385 && getChar(i + 1) == c) {
386 //os << "\\textcompwordmark{}";
387 // Jean-Marc, have a look at
388 // this. I think this works
396 // Typewriter font also has them
397 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
401 // Otherwise, we use what LaTeX
405 os << "\\textless{}";
409 os << "\\textgreater{}";
419 case '-': // "--" in Typewriter mode -> "-{}-"
421 && getChar(i + 1) == '-'
422 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
431 os << "\\char`\\\"{}";
436 if (bparams.inputenc == "default") {
445 case '%': case '#': case '{':
452 os << "\\textasciitilde{}";
457 os << "\\textasciicircum{}";
461 case '*': case '[': case ']':
462 // avoid being mistaken for optional arguments
463 os << '{' << c << '}';
468 // Blanks are printed before font switching.
469 // Sure? I am not! (try nice-latex)
470 // I am sure it's correct. LyX might be smarter
471 // in the future, but for now, nothing wrong is
477 // I assume this is hack treating typewriter as verbatim
478 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
487 // FIXME: if we have "LaTeX" with a font change in the middle (before
488 // the 'T', then the "TeX" part is still special cased. Really we
489 // should only operate this on "words" for some definition of word
493 for (; pnr < phrases_nr; ++pnr) {
494 if (isTextAt(bparams, special_phrases[pnr][0], i)) {
495 os << special_phrases[pnr][1];
496 i += special_phrases[pnr][0].length() - 1;
497 column += special_phrases[pnr][1].length() - 1;
502 if (pnr == phrases_nr && c != '\0') {
512 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
513 BufferParams const & bparams,
514 std::ostream & os, TexRow & texrow)
516 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << std::endl;
517 Paragraph * par = owner_;
519 while (par && par->params().depth() == owner_->params().depth()) {
520 if (textclasslist.Style(bparams.textclass,
521 par->layout).isEnvironment()) {
522 par = par->TeXEnvironment(buf, bparams,
525 par = par->TeXOnePar(buf, bparams,
529 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << std::endl;
535 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
537 InsetList::const_iterator cit = owner_->insetlist.begin();
538 InsetList::const_iterator lend = owner_->insetlist.end();
540 for (; cit != lend; ++cit) {
541 if ((result = cit->inset->getParFromID(id)))
548 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
549 BufferParams const & bparams) const
551 LyXFont tmpfont(font);
553 // check for environment font information
554 char par_depth = owner_->getDepth();
555 Paragraph const * par = owner_;
556 while (par && par->getDepth() && !tmpfont.resolved()) {
557 par = par->outerHook();
559 tmpfont.realize(textclasslist.
560 Style(bparams.textclass,
561 par->getLayout()).font
562 #ifdef INHERIT_LANGUAGE
566 par_depth = par->getDepth();
570 tmpfont.realize(textclasslist.TextClass(bparams.textclass)
572 #ifdef INHERIT_LANGUAGE