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,
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;
70 Paragraph::Pimpl::getChar(Paragraph::size_type pos) const
72 lyx::Assert(pos <= size());
73 // This is stronger, and I belive that this is the assertion
74 // that we should really use. (Lgb)
75 //Assert(pos < size());
77 // Then this has no meaning. (Lgb)
78 if (!size() || pos == size()) return '\0';
84 void Paragraph::Pimpl::setChar(Paragraph::size_type pos,
85 Paragraph::value_type c)
91 void Paragraph::Pimpl::insertChar(Paragraph::size_type pos,
92 Paragraph::value_type c,
95 lyx::Assert(pos <= size());
97 text.insert(text.begin() + pos, c);
99 // Update the font table.
100 FontTable search_font(pos, LyXFont());
101 for (FontList::iterator it = std::lower_bound(fontlist.begin(),
103 search_font, matchFT());
104 it != fontlist.end(); ++it)
105 it->pos(it->pos() + 1);
107 // Update the inset table.
108 InsetTable search_inset(pos, 0);
109 for (InsetList::iterator it = std::lower_bound(owner_->insetlist.begin(),
110 owner_->insetlist.end(),
111 search_inset, matchIT());
112 it != owner_->insetlist.end(); ++it)
115 owner_->setFont(pos, font);
119 void Paragraph::Pimpl::insertInset(Paragraph::size_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(Paragraph::size_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,
207 Paragraph::size_type const i,
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) != ' '
217 // In LaTeX mode, we don't want to
218 // break lines since some commands
220 && ! (font.latex() == LyXFont::ON)
222 // same in FreeSpacing mode
223 && !style.free_spacing
224 // In typewriter mode, we want to avoid
225 // ! . ? : at the end of a line
226 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
227 && (getChar(i - 1) == '.'
228 || getChar(i - 1) == '?'
229 || getChar(i - 1) == ':'
230 || getChar(i - 1) == '!'))) {
231 if (tex_code_break_column == 0) {
232 // in batchmode we need LaTeX to still
233 // see it as a space not as an extra '\n'
239 texrow.start(owner_, i + 1);
243 if (font.latex() == LyXFont::OFF) {
245 if (style.free_spacing) {
256 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
257 BufferParams const & bparams,
262 LyXFont & running_font,
265 LyXLayout const & style,
266 Paragraph::size_type & i,
268 Paragraph::value_type const c)
270 if (style.pass_thru) {
271 if (c != '\0') os << c;
274 // Two major modes: LaTeX or plain
275 // Handle here those cases common to both modes
276 // and then split to handle the two modes separately.
278 case Paragraph::META_INSET: {
279 Inset * inset = owner_->getInset(i);
282 int const len = os.tellp();
283 //ostream::pos_type const len = os.tellp();
284 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
285 || inset->lyxCode() == Inset::MATH_CODE
286 || inset->lyxCode() == Inset::URL_CODE)
287 && running_font.isRightToLeft()) {
292 int tmp = inset->latex(buf, os, moving_arg,
301 column += int(os.tellp()) - len;
310 case Paragraph::META_NEWLINE:
312 column += running_font.latexWriteEndChanges(os,
317 basefont = owner_->getFont(bparams, -1);
318 running_font = basefont;
321 case Paragraph::META_HFILL:
327 // And now for the special cases within each mode
329 // Are we in LaTeX mode?
330 if (font.latex() == LyXFont::ON) {
331 // at present we only have one option
332 // but I'll leave it as a switch statement
333 // so its simpler to extend. (ARRae)
336 // make sure that we will not print
337 // error generating chars to the tex
338 // file. This test would not be needed
339 // if it were done in the buffer
348 // Plain mode (i.e. not LaTeX)
351 os << "\\textbackslash{}";
355 case '°': case '±': case '²': case '³':
356 case '×': case '÷': case '¹': case 'ª':
357 case 'º': case '¬': case 'µ':
358 if (bparams.inputenc == "latin1" ||
359 (bparams.inputenc == "auto" &&
360 font.language()->encoding()->LatexName()
362 os << "\\ensuremath{"
371 case '|': case '<': case '>':
372 // In T1 encoding, these characters exist
373 if (lyxrc.fontenc == "T1") {
375 //... but we should avoid ligatures
376 if ((c == '>' || c == '<')
378 && getChar(i + 1) == c) {
379 //os << "\\textcompwordmark{}";
380 // Jean-Marc, have a look at
381 // this. I think this works
389 // Typewriter font also has them
390 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
394 // Otherwise, we use what LaTeX
398 os << "\\textless{}";
402 os << "\\textgreater{}";
412 case '-': // "--" in Typewriter mode -> "-{}-"
414 && getChar(i + 1) == '-'
415 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
424 os << "\\char`\\\"{}";
429 if (bparams.inputenc == "default") {
438 case '%': case '#': case '{':
445 os << "\\textasciitilde{}";
450 os << "\\textasciicircum{}";
454 case '*': case '[': case ']':
455 // avoid being mistaken for optional arguments
456 os << '{' << c << '}';
461 // Blanks are printed before font switching.
462 // Sure? I am not! (try nice-latex)
463 // I am sure it's correct. LyX might be smarter
464 // in the future, but for now, nothing wrong is
469 /* idea for labels --- begin*/
473 && font.family() != LyXFont::TYPEWRITER_FAMILY
474 && getChar(i + 1) == 'y'
475 && getChar(i + 2) == 'X') {
483 && font.family() != LyXFont::TYPEWRITER_FAMILY
484 && getChar(i + 1) == 'e'
485 && getChar(i + 2) == 'X') {
490 // Check for "LaTeX2e"
493 && font.family() != LyXFont::TYPEWRITER_FAMILY
494 && getChar(i + 1) == 'a'
495 && getChar(i + 2) == 'T'
496 && getChar(i + 3) == 'e'
497 && getChar(i + 4) == 'X'
498 && getChar(i + 5) == '2'
499 && getChar(i + 6) == 'e') {
507 && font.family() != LyXFont::TYPEWRITER_FAMILY
508 && getChar(i + 1) == 'a'
509 && getChar(i + 2) == 'T'
510 && getChar(i + 3) == 'e'
511 && getChar(i + 4) == 'X') {
515 /* idea for labels --- end*/
516 } else if (c != '\0') {
529 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
530 BufferParams const & bparams,
531 std::ostream & os, TexRow & texrow)
533 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << std::endl;
534 Paragraph * par = owner_;
536 while (par && par->params().depth() == owner_->params().depth()) {
537 if (textclasslist.Style(bparams.textclass,
538 par->layout).isEnvironment()) {
539 par = par->TeXEnvironment(buf, bparams,
542 par = par->TeXOnePar(buf, bparams,
546 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << std::endl;
552 Paragraph * Paragraph::Pimpl::getParFromID(int id) const
554 InsetList::const_iterator cit = owner_->insetlist.begin();
555 InsetList::const_iterator lend = owner_->insetlist.end();
557 for (; cit != lend; ++cit) {
558 if ((result = cit->inset->getParFromID(id)))