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 * ====================================================== */
13 #include "paragraph_pimpl.h"
14 #include "LaTeXFeatures.h"
17 #include "bufferparams.h"
22 #include "support/LAssert.h"
27 using std::upper_bound;
28 using std::lower_bound;
30 // Initialize static member.
31 ShareContainer<LyXFont> Paragraph::Pimpl::FontTable::container;
32 // Initialization of the counter for the paragraph id's,
33 unsigned int Paragraph::Pimpl::paragraph_id = 0;
37 struct special_phrase {
43 special_phrase special_phrases[] = {
44 { "LyX", "\\LyX{}", false },
45 { "TeX", "\\TeX{}", true },
46 { "LaTeX2e", "\\LaTeXe{}", true },
47 { "LaTeX", "\\LaTeX{}", true },
50 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
55 Paragraph::Pimpl::Pimpl(Paragraph * owner)
63 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner, bool same_ids)
64 : params(p.params), owner_(owner)
66 inset_owner = p.inset_owner;
68 fontlist = p.fontlist;
75 changes_.reset(new Changes(*p.changes_.get()));
79 void Paragraph::Pimpl::clear()
86 void Paragraph::Pimpl::setContentsFromPar(Paragraph const * par)
89 text = par->pimpl_->text;
90 if (par->pimpl_->tracking()) {
91 changes_.reset(new Changes(*(par->pimpl_->changes_.get())));
96 void Paragraph::Pimpl::trackChanges(Change::Type type)
99 lyxerr[Debug::CHANGES] << "already tracking for par " << id_ << endl;
103 lyxerr[Debug::CHANGES] << "track changes for par "
104 << id_ << " type " << type << endl;
105 changes_.reset(new Changes(type));
106 changes_->set(type, 0, size());
110 void Paragraph::Pimpl::untrackChanges()
116 void Paragraph::Pimpl::cleanChanges()
118 // if we're not tracking, we don't want to reset...
122 changes_.reset(new Changes(Change::INSERTED));
123 changes_->set(Change::INSERTED, 0, size());
127 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
132 return changes_->isChange(start, end);
136 bool Paragraph::Pimpl::isChangeEdited(pos_type start, pos_type end) const
141 return changes_->isChangeEdited(start, end);
145 void Paragraph::Pimpl::setChange(pos_type pos, Change::Type type)
150 changes_->set(type, pos);
154 Change::Type Paragraph::Pimpl::lookupChange(pos_type pos) const
157 return Change::UNCHANGED;
159 return changes_->lookup(pos);
163 Change const Paragraph::Pimpl::lookupChangeFull(pos_type pos) const
166 return Change(Change::UNCHANGED);
168 return changes_->lookupFull(pos);
172 void Paragraph::Pimpl::markErased()
174 lyx::Assert(tracking());
176 // FIXME: we should actually remove INSERTED chars.
177 // difficult because owning insettexts/tabulars need
178 // to update themselves when rows etc. change
179 changes_->set(Change::DELETED, 0, size());
180 changes_->reset(Change::DELETED);
184 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
190 changes_.reset(new Changes(Change::UNCHANGED));
194 lyxerr << "acceptchange" << endl;
197 for (; i < end; ++i) {
198 switch (lookupChange(i)) {
199 case Change::UNCHANGED:
202 case Change::INSERTED:
203 changes_->set(Change::UNCHANGED, i);
206 case Change::DELETED:
215 lyxerr << "endacceptchange" << endl;
216 changes_->reset(Change::UNCHANGED);
220 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
226 changes_.reset(new Changes(Change::UNCHANGED));
232 for (; i < end; ++i) {
233 switch (lookupChange(i)) {
234 case Change::UNCHANGED:
237 case Change::INSERTED:
244 case Change::DELETED:
245 changes_->set(Change::UNCHANGED, i);
249 changes_->reset(Change::UNCHANGED);
253 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
255 //lyx::Assert(pos <= siz);
256 // This is stronger, and I belive that this is the assertion
257 // that we should really use. (Lgb)
258 lyx::Assert(pos < size());
261 // This is in the critical path for loading!
262 pos_type const siz = size();
264 // Then this has no meaning. (Lgb)
265 if (!siz || pos == siz) {
266 lyxerr << "getChar() on pos " << pos << " in par id "
267 << owner_->id() << " of size " << siz
268 << " is a bit silly !" << endl;
276 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
283 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
284 LyXFont const & font, Change change)
286 lyx::Assert(pos <= size());
289 changes_->record(change, pos);
292 // This is actually very common when parsing buffers (and
293 // maybe inserting ascii text)
295 // when appending characters, no need to update tables
297 owner_->setFont(pos, font);
301 text.insert(text.begin() + pos, c);
303 // Update the font table.
304 FontTable search_font(pos, LyXFont());
305 for (FontList::iterator it = lower_bound(fontlist.begin(),
307 search_font, matchFT());
308 it != fontlist.end(); ++it)
310 it->pos(it->pos() + 1);
314 owner_->insetlist.increasePosAfterPos(pos);
316 owner_->setFont(pos, font);
320 void Paragraph::Pimpl::insertInset(pos_type pos,
321 Inset * inset, LyXFont const & font, Change change)
324 lyx::Assert(pos <= size());
326 insertChar(pos, META_INSET, font, change);
327 lyx::Assert(text[pos] == META_INSET);
329 // Add a new entry in the insetlist.
330 owner_->insetlist.insert(inset, pos);
331 inset->parOwner(owner_);
334 inset->setOwner(inset_owner);
338 bool Paragraph::Pimpl::erasePos(pos_type pos)
340 lyx::Assert(pos < size());
343 Change::Type changetype(changes_->lookup(pos));
344 changes_->record(Change(Change::DELETED), pos);
346 // only allow the actual removal if it was /new/ text
347 if (changetype != Change::INSERTED) {
348 if (text[pos] == Paragraph::META_INSET) {
349 Inset * i(owner_->getInset(pos));
361 void Paragraph::Pimpl::eraseIntern(pos_type pos)
363 // if it is an inset, delete the inset entry
364 if (text[pos] == Paragraph::META_INSET) {
365 owner_->insetlist.erase(pos);
368 text.erase(text.begin() + pos);
370 // Erase entries in the tables.
371 FontTable search_font(pos, LyXFont());
373 FontList::iterator it =
374 lower_bound(fontlist.begin(),
376 search_font, matchFT());
377 if (it != fontlist.end() && it->pos() == pos &&
379 (it != fontlist.begin()
380 && boost::prior(it)->pos() == pos - 1))) {
381 // If it is a multi-character font
382 // entry, we just make it smaller
383 // (see update below), otherwise we
385 unsigned int const i = it - fontlist.begin();
386 fontlist.erase(fontlist.begin() + i);
387 it = fontlist.begin() + i;
388 if (i > 0 && i < fontlist.size() &&
389 fontlist[i - 1].font() == fontlist[i].font()) {
390 fontlist.erase(fontlist.begin() + i - 1);
391 it = fontlist.begin() + i - 1;
395 // Update all other entries.
396 FontList::iterator fend = fontlist.end();
397 for (; it != fend; ++it)
398 it->pos(it->pos() - 1);
400 // Update the insetlist.
401 owner_->insetlist.decreasePosAfterPos(pos);
405 void Paragraph::Pimpl::erase(pos_type pos)
411 bool Paragraph::Pimpl::erase(pos_type start, pos_type end)
414 pos_type count = end - start;
415 bool any_erased = false;
429 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
431 unsigned int & column,
432 LyXFont const & font,
433 LyXLayout const & style)
435 if (style.pass_thru) return;
436 if (column > lyxrc.ascii_linelen
438 && getChar(i - 1) != ' '
440 // same in FreeSpacing mode
441 && !style.free_spacing
442 && !owner_->isFreeSpacing()
443 // In typewriter mode, we want to avoid
444 // ! . ? : at the end of a line
445 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
446 && (getChar(i - 1) == '.'
447 || getChar(i - 1) == '?'
448 || getChar(i - 1) == ':'
449 || getChar(i - 1) == '!'))) {
452 texrow.start(owner_, i + 1);
454 } else if (style.free_spacing) {
462 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
464 pos_type const len = str.length();
466 // is the paragraph large enough?
467 if (pos + len > size())
470 // does the wanted text start at point?
471 for (string::size_type i = 0; i < str.length(); ++i) {
472 if (str[i] != text[pos + i])
476 // is there a font change in middle of the word?
477 FontList::const_iterator cit = fontlist.begin();
478 FontList::const_iterator end = fontlist.end();
479 for (; cit != end; ++cit) {
480 if (cit->pos() >= pos)
483 if (cit != end && pos + len - 1 > cit->pos())
490 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
491 BufferParams const & bparams,
496 LyXFont & running_font,
499 Change::Type & running_change,
500 LyXLayout const & style,
502 unsigned int & column,
505 if (style.pass_thru) {
511 // Two major modes: LaTeX or plain
512 // Handle here those cases common to both modes
513 // and then split to handle the two modes separately.
515 case Paragraph::META_INSET: {
516 Inset * inset = owner_->getInset(i);
518 // FIXME: remove this check
522 // FIXME: move this to InsetNewline::latex
523 if (inset->lyxCode() == Inset::NEWLINE_CODE) {
524 // newlines are handled differently here than
525 // the default in simpleTeXSpecialChars().
526 if (!style.newline_allowed) {
530 column += running_font.latexWriteEndChanges(os, basefont, basefont);
533 basefont = owner_->getLayoutFont(bparams);
534 running_font = basefont;
536 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
545 texrow.start(owner_, i + 1);
550 if (inset->isTextInset()) {
551 column += Changes::latexMarkChange(os, running_change,
553 running_change = Change::UNCHANGED;
557 int const len = os.tellp();
558 //ostream::pos_type const len = os.tellp();
559 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
560 || inset->lyxCode() == Inset::MATH_CODE
561 || inset->lyxCode() == Inset::URL_CODE)
562 && running_font.isRightToLeft()) {
568 #warning Bug: we can have an empty font change here!
569 // if there has just been a font change, we are going to close it
570 // right now, which means stupid latex code like \textsf{}. AFAIK,
571 // this does not harm dvi output. A minor bug, thus (JMarc)
573 // some insets cannot be inside a font change command
574 if (open_font && inset->noFontChange()) {
575 column +=running_font.
576 latexWriteEndChanges(os,
580 basefont = owner_->getLayoutFont(bparams);
581 running_font = basefont;
584 int tmp = inset->latex(buf, os, moving_arg,
591 for (int j = 0; j < tmp; ++j) {
594 texrow.start(owner_, i + 1);
597 column += int(os.tellp()) - len;
603 // And now for the special cases within each mode
607 os << "\\textbackslash{}";
611 case '±': case '²': case '³':
612 case '×': case '÷': case '¹':
614 if ((bparams.inputenc == "latin1" ||
615 bparams.inputenc == "latin9") ||
616 (bparams.inputenc == "auto" &&
617 (font.language()->encoding()->LatexName()
619 font.language()->encoding()->LatexName()
621 os << "\\ensuremath{"
630 case '|': case '<': case '>':
631 // In T1 encoding, these characters exist
632 if (lyxrc.fontenc == "T1") {
634 //... but we should avoid ligatures
635 if ((c == '>' || c == '<')
637 && getChar(i + 1) == c) {
638 //os << "\\textcompwordmark{}";
639 // Jean-Marc, have a look at
640 // this. I think this works
648 // Typewriter font also has them
649 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
653 // Otherwise, we use what LaTeX
657 os << "\\textless{}";
661 os << "\\textgreater{}";
671 case '-': // "--" in Typewriter mode -> "-{}-"
673 && getChar(i + 1) == '-'
674 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
683 os << "\\char`\\\"{}";
688 if (bparams.inputenc == "default") {
697 case '%': case '#': case '{':
704 os << "\\textasciitilde{}";
709 os << "\\textasciicircum{}";
713 case '*': case '[': case ']':
714 // avoid being mistaken for optional arguments
715 os << '{' << c << '}';
720 // Blanks are printed before font switching.
721 // Sure? I am not! (try nice-latex)
722 // I am sure it's correct. LyX might be smarter
723 // in the future, but for now, nothing wrong is
729 // I assume this is hack treating typewriter as verbatim
730 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
739 // FIXME: if we have "LaTeX" with a font
740 // change in the middle (before the 'T', then
741 // the "TeX" part is still special cased.
742 // Really we should only operate this on
743 // "words" for some definition of word
747 for (; pnr < phrases_nr; ++pnr) {
748 if (isTextAt(special_phrases[pnr].phrase, i)) {
749 os << special_phrases[pnr].macro;
750 i += special_phrases[pnr].phrase.length() - 1;
751 column += special_phrases[pnr].macro.length() - 1;
756 if (pnr == phrases_nr && c != '\0') {
765 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
766 LyXLayout const & layout) const
768 BufferParams const & bparams = features.bufferParams();
771 if (params.lineTop() || params.lineBottom())
772 features.require("lyxline");
773 if (!params.spacing().isDefault())
774 features.require("setspace");
777 features.useLayout(layout.name());
780 Language const * doc_language = bparams.language;
782 FontList::const_iterator fcit = fontlist.begin();
783 FontList::const_iterator fend = fontlist.end();
784 for (; fcit != fend; ++fcit) {
785 if (fcit->font().noun() == LyXFont::ON) {
786 lyxerr[Debug::LATEX] << "font.noun: "
787 << fcit->font().noun()
789 features.require("noun");
790 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
791 << fcit->font().stateText(0)
794 switch (fcit->font().color()) {
796 case LColor::inherit:
798 // probably we should put here all interface colors used for
799 // font displaying! For now I just add this ones I know of (Jug)
804 features.require("color");
805 lyxerr[Debug::LATEX] << "Color enabled. Font: "
806 << fcit->font().stateText(0)
810 Language const * language = fcit->font().language();
811 if (language->babel() != doc_language->babel() &&
812 language != ignore_language &&
813 language != latex_language)
815 features.useLanguage(language);
816 lyxerr[Debug::LATEX] << "Found language "
817 << language->babel() << endl;
821 if (!params.leftIndent().zero())
822 features.require("ParagraphLeftIndent");
825 InsetList::iterator icit = owner_->insetlist.begin();
826 InsetList::iterator iend = owner_->insetlist.end();
827 for (; icit != iend; ++icit) {
828 if (icit.getInset()) {
829 icit.getInset()->validate(features);
830 if (layout.needprotect &&
831 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
832 features.require("NeedLyXFootnoteCode");
837 for (pos_type i = 0; i < size() ; ++i) {
838 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
839 if (!special_phrases[pnr].builtin
840 && isTextAt(special_phrases[pnr].phrase, i)) {
841 features.require(special_phrases[pnr].phrase);
849 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
850 BufferParams const & bparams) const
852 LyXFont tmpfont(font);
854 // check for environment font information
855 char par_depth = owner_->getDepth();
856 Paragraph const * par = owner_;
857 LyXTextClass const & tclass = bparams.getLyXTextClass();
859 while (par && par->getDepth() && !tmpfont.resolved()) {
860 par = par->outerHook();
862 tmpfont.realize(par->layout()->font);
863 par_depth = par->getDepth();
867 tmpfont.realize(tclass.defaultfont());