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"
19 #include "bufferparams.h"
23 #include "support/LAssert.h"
25 extern int tex_code_break_column;
28 // Initialization of the counter for the paragraph id's,
29 unsigned int Paragraph::Pimpl::paragraph_id = 0;
31 // Initialize static member.
32 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
35 Paragraph::Pimpl::Pimpl(Paragraph * owner)
43 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
44 : params(p.params), owner_(owner)
46 inset_owner = p.inset_owner;
48 fontlist = p.fontlist;
56 void Paragraph::Pimpl::clear()
62 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
65 text = par->pimpl_->text;
69 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
71 lyx::Assert(pos <= size());
72 // This is stronger, and I belive that this is the assertion
73 // that we should really use. (Lgb)
74 //Assert(pos < size());
76 // Then this has no meaning. (Lgb)
77 if (!size() || pos == size()) return '\0';
83 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
89 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
92 lyx::Assert(pos <= size());
94 text.insert(text.begin() + pos, c);
96 // Update the font table.
97 FontTable search_font(pos, LyXFont());
98 for (FontList::iterator it = std::lower_bound(fontlist.begin(),
100 search_font, matchFT());
101 it != fontlist.end(); ++it)
103 it->pos(it->pos() + 1);
106 // Update the inset table.
107 InsetTable search_inset(pos, 0);
108 for (InsetList::iterator it = std::lower_bound(owner_->insetlist.begin(),
109 owner_->insetlist.end(),
110 search_inset, matchIT());
111 it != owner_->insetlist.end(); ++it)
115 owner_->setFont(pos, font);
119 void Paragraph::Pimpl::insertInset(pos_type pos,
120 Inset * inset, LyXFont const & font)
123 lyx::Assert(pos <= size());
125 insertChar(pos, META_INSET, font);
126 lyx::Assert(text[pos] == META_INSET);
128 // Add a new entry in the inset table.
129 InsetTable search_inset(pos, 0);
130 InsetList::iterator it = std::lower_bound(owner_->insetlist.begin(),
131 owner_->insetlist.end(),
132 search_inset, matchIT());
133 if (it != owner_->insetlist.end() && it->pos == pos) {
134 lyxerr << "ERROR (Paragraph::InsertInset): "
135 "there is an inset in position: " << pos << std::endl;
137 owner_->insetlist.insert(it, InsetTable(pos, inset));
141 inset->setOwner(inset_owner);
145 void Paragraph::Pimpl::erase(pos_type pos)
147 lyx::Assert(pos < size());
148 // if it is an inset, delete the inset entry
149 if (text[pos] == Paragraph::META_INSET) {
151 InsetTable search_inset(pos, 0);
152 InsetList::iterator it =
153 std::lower_bound(owner_->insetlist.begin(),
154 owner_->insetlist.end(),
155 search_inset, matchIT());
156 if (it != owner_->insetlist.end() && it->pos == pos) {
158 owner_->insetlist.erase(it);
162 text.erase(text.begin() + pos);
164 // Erase entries in the tables.
165 FontTable search_font(pos, LyXFont());
167 FontList::iterator it =
168 std::lower_bound(fontlist.begin(),
170 search_font, matchFT());
171 if (it != fontlist.end() && it->pos() == pos &&
173 (it != fontlist.begin()
174 && boost::prior(it)->pos() == pos - 1))) {
175 // If it is a multi-character font
176 // entry, we just make it smaller
177 // (see update below), otherwise we
179 unsigned int const i = it - fontlist.begin();
180 fontlist.erase(fontlist.begin() + i);
181 it = fontlist.begin() + i;
182 if (i > 0 && i < fontlist.size() &&
183 fontlist[i - 1].font() == fontlist[i].font()) {
184 fontlist.erase(fontlist.begin() + i - 1);
185 it = fontlist.begin() + i - 1;
189 // Update all other entries.
190 FontList::iterator fend = fontlist.end();
191 for (; it != fend; ++it)
192 it->pos(it->pos() - 1);
194 // Update the inset table.
195 InsetTable search_inset(pos, 0);
196 InsetList::iterator lend = owner_->insetlist.end();
197 for (InsetList::iterator it =
198 std::upper_bound(owner_->insetlist.begin(),
200 search_inset, matchIT());
206 void Paragraph::Pimpl::simpleTeXBlanks(std::ostream & os, TexRow & texrow,
208 int & column, LyXFont const & font,
209 LyXLayout const & style)
211 if (style.pass_thru) return;
212 if (column > tex_code_break_column
214 && getChar(i - 1) != ' '
216 // same in FreeSpacing mode
217 && !style.free_spacing
218 // In typewriter mode, we want to avoid
219 // ! . ? : at the end of a line
220 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
221 && (getChar(i - 1) == '.'
222 || getChar(i - 1) == '?'
223 || getChar(i - 1) == ':'
224 || getChar(i - 1) == '!'))) {
225 if (tex_code_break_column == 0) {
226 // in batchmode we need LaTeX to still
227 // see it as a space not as an extra '\n'
233 texrow.start(owner_, i + 1);
235 } else if (style.free_spacing) {
243 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos)
245 for (int i=0; i < str.length(); ++i) {
246 if (pos + i >= size())
248 if (str[i] != getChar(pos + i))
255 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
256 BufferParams const & bparams,
261 LyXFont & running_font,
264 LyXLayout const & style,
269 if (style.pass_thru) {
270 if (c != '\0') os << c;
273 // Two major modes: LaTeX or plain
274 // Handle here those cases common to both modes
275 // and then split to handle the two modes separately.
277 case Paragraph::META_INSET: {
278 Inset * inset = owner_->getInset(i);
281 int const len = os.tellp();
282 //ostream::pos_type const len = os.tellp();
283 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
284 || inset->lyxCode() == Inset::MATH_CODE
285 || inset->lyxCode() == Inset::URL_CODE)
286 && running_font.isRightToLeft()) {
291 int tmp = inset->latex(buf, os, moving_arg,
298 for (int j = 0; j < tmp; ++j) {
301 texrow.start(owner_, i + 1);
304 column += int(os.tellp()) - len;
310 case Paragraph::META_NEWLINE:
312 column += running_font.latexWriteEndChanges(os,
317 basefont = owner_->getLayoutFont(bparams);
318 running_font = basefont;
321 case Paragraph::META_HFILL:
327 // And now for the special cases within each mode
331 os << "\\textbackslash{}";
335 case '°': case '±': case '²': case '³':
336 case '×': case '÷': case '¹': case 'ª':
337 case 'º': case '¬': case 'µ':
338 if ((bparams.inputenc == "latin1" ||
339 bparams.inputenc == "latin9") ||
340 (bparams.inputenc == "auto" &&
341 (font.language()->encoding()->LatexName()
343 font.language()->encoding()->LatexName()
345 os << "\\ensuremath{"
354 case '|': case '<': case '>':
355 // In T1 encoding, these characters exist
356 if (lyxrc.fontenc == "T1") {
358 //... but we should avoid ligatures
359 if ((c == '>' || c == '<')
361 && getChar(i + 1) == c) {
362 //os << "\\textcompwordmark{}";
363 // Jean-Marc, have a look at
364 // this. I think this works
372 // Typewriter font also has them
373 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
377 // Otherwise, we use what LaTeX
381 os << "\\textless{}";
385 os << "\\textgreater{}";
395 case '-': // "--" in Typewriter mode -> "-{}-"
397 && getChar(i + 1) == '-'
398 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
407 os << "\\char`\\\"{}";
412 if (bparams.inputenc == "default") {
421 case '%': case '#': case '{':
428 os << "\\textasciitilde{}";
433 os << "\\textasciicircum{}";
437 case '*': case '[': case ']':
438 // avoid being mistaken for optional arguments
439 os << '{' << c << '}';
444 // Blanks are printed before font switching.
445 // Sure? I am not! (try nice-latex)
446 // I am sure it's correct. LyX might be smarter
447 // in the future, but for now, nothing wrong is
453 // I assume this is hack treating typewriter as verbatim
454 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
461 if (isTextAt("LyX", i)) {
465 } else if (isTextAt("TeX", i)) {
469 } else if (isTextAt("LaTeX2e", i)) {
473 } else if (isTextAt("LaTeX", i)) {
477 // do we really try to print out '\0' ?
478 } else if (c != '\0') {
488 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
489 BufferParams const & bparams,
490 std::ostream & os, TexRow & texrow)
492 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << std::endl;
493 Paragraph * par = owner_;
495 while (par && par->params().depth() == owner_->params().depth()) {
496 if (textclasslist.Style(bparams.textclass,
497 par->layout).isEnvironment()) {
498 par = par->TeXEnvironment(buf, bparams,
501 par = par->TeXOnePar(buf, bparams,
505 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << std::endl;
511 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
513 InsetList::const_iterator cit = owner_->insetlist.begin();
514 InsetList::const_iterator lend = owner_->insetlist.end();
516 for (; cit != lend; ++cit) {
517 if ((result = cit->inset->getParFromID(id)))
524 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
525 BufferParams const & bparams) const
527 LyXFont tmpfont(font);
529 // check for environment font information
530 char par_depth = owner_->getDepth();
531 Paragraph const * par = owner_;
532 while (par && par->getDepth() && !tmpfont.resolved()) {
533 par = par->outerHook();
535 tmpfont.realize(textclasslist.
536 Style(bparams.textclass,
537 par->getLayout()).font
538 #ifdef INHERIT_LANGUAGE
542 par_depth = par->getDepth();
546 tmpfont.realize(textclasslist.TextClass(bparams.textclass)
548 #ifdef INHERIT_LANGUAGE