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"
24 extern int tex_code_break_column;
27 // Initialization of the counter for the paragraph id's,
28 unsigned int Paragraph::Pimpl::paragraph_id = 0;
30 // Initialize static member.
31 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
34 Paragraph::Pimpl::Pimpl(Paragraph * owner)
42 Paragraph::Pimpl::Pimpl(Paragraph::Pimpl const & p, Paragraph * owner)
45 inset_owner = p.inset_owner;
47 fontlist = p.fontlist;
51 void Paragraph::Pimpl::clear()
57 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
60 text = par->pimpl_->text;
65 Paragraph::Pimpl::getChar(Paragraph::size_type pos) const
67 lyx::Assert(pos <= size());
68 // This is stronger, and I belive that this is the assertion
69 // that we should really use. (Lgb)
70 //Assert(pos < size());
72 // Then this has no meaning. (Lgb)
73 if (!size() || pos == size()) return '\0';
79 void Paragraph::Pimpl::setChar(Paragraph::size_type pos,
80 Paragraph::value_type c)
86 void Paragraph::Pimpl::insertChar(Paragraph::size_type pos,
87 Paragraph::value_type c,
90 lyx::Assert(pos <= size());
92 text.insert(text.begin() + pos, c);
94 // Update the font table.
95 FontTable search_font(pos, LyXFont());
96 for (FontList::iterator it = lower_bound(fontlist.begin(),
98 search_font, matchFT());
99 it != fontlist.end(); ++it)
100 (*it).pos((*it).pos() + 1);
102 // Update the inset table.
103 InsetTable search_inset(pos, 0);
104 for (InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
105 owner_->insetlist.end(),
106 search_inset, matchIT());
107 it != owner_->insetlist.end(); ++it)
110 owner_->setFont(pos, font);
114 void Paragraph::Pimpl::insertInset(Paragraph::size_type pos,
115 Inset * inset, LyXFont const & font)
118 lyx::Assert(pos <= size());
120 insertChar(pos, META_INSET, font);
121 lyx::Assert(text[pos] == META_INSET);
123 // Add a new entry in the inset table.
124 InsetTable search_inset(pos, 0);
125 InsetList::iterator it = lower_bound(owner_->insetlist.begin(),
126 owner_->insetlist.end(),
127 search_inset, matchIT());
128 if (it != owner_->insetlist.end() && (*it).pos == pos) {
129 lyxerr << "ERROR (Paragraph::InsertInset): "
130 "there is an inset in position: " << pos << endl;
132 owner_->insetlist.insert(it, InsetTable(pos, inset));
136 inset->setOwner(inset_owner);
140 void Paragraph::Pimpl::erase(Paragraph::size_type pos)
142 lyx::Assert(pos < size());
143 // if it is an inset, delete the inset entry
144 if (text[pos] == Paragraph::META_INSET) {
146 InsetTable search_inset(pos, 0);
147 InsetList::iterator it =
148 lower_bound(owner_->insetlist.begin(),
149 owner_->insetlist.end(),
150 search_inset, matchIT());
151 if (it != owner_->insetlist.end() && (*it).pos == pos) {
153 owner_->insetlist.erase(it);
157 text.erase(text.begin() + pos);
159 // Erase entries in the tables.
160 FontTable search_font(pos, LyXFont());
162 FontList::iterator it =
163 lower_bound(fontlist.begin(),
165 search_font, matchFT());
166 if (it != fontlist.end() && (*it).pos() == pos &&
168 (it != fontlist.begin()
169 && (*(it - 1)).pos() == pos - 1))) {
170 // If it is a multi-character font
171 // entry, we just make it smaller
172 // (see update below), otherwise we
174 unsigned int const i = it - fontlist.begin();
175 fontlist.erase(fontlist.begin() + i);
176 it = fontlist.begin() + i;
177 if (i > 0 && i < fontlist.size() &&
178 fontlist[i - 1].font() == fontlist[i].font()) {
179 fontlist.erase(fontlist.begin() + i - 1);
180 it = fontlist.begin() + i - 1;
184 // Update all other entries.
185 FontList::iterator fend = fontlist.end();
186 for (; it != fend; ++it)
187 (*it).pos((*it).pos() - 1);
189 // Update the inset table.
190 InsetTable search_inset(pos, 0);
191 InsetList::iterator lend = owner_->insetlist.end();
192 for (InsetList::iterator it =
193 upper_bound(owner_->insetlist.begin(),
195 search_inset, matchIT());
201 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
202 Paragraph::size_type const i,
203 int & column, LyXFont const & font,
204 LyXLayout const & style)
206 if (column > tex_code_break_column
208 && owner_->getChar(i - 1) != ' '
209 && (i < owner_->size() - 1)
210 // In LaTeX mode, we don't want to
211 // break lines since some commands
213 && ! (font.latex() == LyXFont::ON)
214 // same in FreeSpacing mode
215 && !style.free_spacing
216 // In typewriter mode, we want to avoid
217 // ! . ? : at the end of a line
218 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
219 && (owner_->getChar(i-1) == '.'
220 || owner_->getChar(i-1) == '?'
221 || owner_->getChar(i-1) == ':'
222 || owner_->getChar(i-1) == '!'))) {
223 if (tex_code_break_column == 0) {
224 // in batchmode we need LaTeX to still
225 // see it as a space not as an extra '\n'
231 texrow.start(owner_, i + 1);
233 } else if (font.latex() == LyXFont::OFF) {
234 if (style.free_spacing) {
243 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
244 BufferParams const & bparams,
245 ostream & os, TexRow & texrow,
248 LyXFont & running_font,
251 LyXLayout const & style,
252 Paragraph::size_type & i,
254 Paragraph::value_type const c)
256 // Two major modes: LaTeX or plain
257 // Handle here those cases common to both modes
258 // and then split to handle the two modes separately.
260 case Paragraph::META_INSET: {
261 Inset * inset = owner_->getInset(i);
264 int const len = os.tellp();
265 //ostream::pos_type const len = os.tellp();
266 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
267 || inset->LyxCode() == Inset::MATH_CODE
268 || inset->LyxCode() == Inset::URL_CODE)
269 && running_font.isRightToLeft()) {
274 int tmp = inset->Latex(buf, os, moving_arg,
283 column += int(os.tellp()) - len;
292 case Paragraph::META_NEWLINE:
294 column += running_font.latexWriteEndChanges(os,
299 basefont = owner_->getFont(bparams, -1);
300 running_font = basefont;
303 case Paragraph::META_HFILL:
309 // And now for the special cases within each mode
310 // Are we in LaTeX mode?
311 if (font.latex() == LyXFont::ON) {
312 // at present we only have one option
313 // but I'll leave it as a switch statement
314 // so its simpler to extend. (ARRae)
317 // make sure that we will not print
318 // error generating chars to the tex
319 // file. This test would not be needed
320 // if it were done in the buffer
328 // Plain mode (i.e. not LaTeX)
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 == "auto" &&
340 font.language()->encoding()->LatexName()
342 os << "\\ensuremath{"
351 case '|': case '<': case '>':
352 // In T1 encoding, these characters exist
353 if (lyxrc.fontenc == "T1") {
355 //... but we should avoid ligatures
356 if ((c == '>' || c == '<')
357 && i <= owner_->size() - 2
358 && owner_->getChar(i + 1) == c) {
359 //os << "\\textcompwordmark{}";
360 // Jean-Marc, have a look at
361 // this. I think this works
369 // Typewriter font also has them
370 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
374 // Otherwise, we use what LaTeX
378 os << "\\textless{}";
382 os << "\\textgreater{}";
392 case '-': // "--" in Typewriter mode -> "-{}-"
393 if (i <= owner_->size() - 2
394 && owner_->getChar(i + 1) == '-'
395 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
404 os << "\\char`\\\"{}";
409 if (bparams.inputenc == "default") {
418 case '%': case '#': case '{':
425 os << "\\textasciitilde{}";
430 os << "\\textasciicircum{}";
434 case '*': case '[': case ']':
435 // avoid being mistaken for optional arguments
436 os << '{' << c << '}';
441 // Blanks are printed before font switching.
442 // Sure? I am not! (try nice-latex)
443 // I am sure it's correct. LyX might be smarter
444 // in the future, but for now, nothing wrong is
449 /* idea for labels --- begin*/
452 && i <= owner_->size() - 3
453 && font.family() != LyXFont::TYPEWRITER_FAMILY
454 && owner_->getChar(i + 1) == 'y'
455 && owner_->getChar(i + 2) == 'X') {
462 && i <= owner_->size() - 3
463 && font.family() != LyXFont::TYPEWRITER_FAMILY
464 && owner_->getChar(i + 1) == 'e'
465 && owner_->getChar(i + 2) == 'X') {
470 // Check for "LaTeX2e"
472 && i <= owner_->size() - 7
473 && font.family() != LyXFont::TYPEWRITER_FAMILY
474 && owner_->getChar(i + 1) == 'a'
475 && owner_->getChar(i + 2) == 'T'
476 && owner_->getChar(i + 3) == 'e'
477 && owner_->getChar(i + 4) == 'X'
478 && owner_->getChar(i + 5) == '2'
479 && owner_->getChar(i + 6) == 'e') {
486 && i <= owner_->size() - 5
487 && font.family() != LyXFont::TYPEWRITER_FAMILY
488 && owner_->getChar(i + 1) == 'a'
489 && owner_->getChar(i + 2) == 'T'
490 && owner_->getChar(i + 3) == 'e'
491 && owner_->getChar(i + 4) == 'X') {
495 /* idea for labels --- end*/
496 } else if (c != '\0') {
507 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
508 BufferParams const & bparams,
509 ostream & os, TexRow & texrow)
511 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
512 Paragraph * par = owner_;
514 while (par && par->params().depth() == owner_->params().depth()) {
515 if (textclasslist.Style(bparams.textclass,
516 par->layout).isEnvironment()) {
517 par = par->TeXEnvironment(buf, bparams,
520 par = par->TeXOnePar(buf, bparams,
524 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;