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 // This is in the critical path for loading!
256 pos_type const siz = size();
257 lyx::Assert(pos <= siz);
258 // This is stronger, and I belive that this is the assertion
259 // that we should really use. (Lgb)
260 //Assert(pos < size());
262 // Then this has no meaning. (Lgb)
263 if (!siz || pos == siz)
270 void Paragraph::Pimpl::setChar(pos_type pos, value_type c)
277 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c,
278 LyXFont const & font, Change change)
280 lyx::Assert(pos <= size());
283 changes_->record(change, pos);
286 // This is actually very common when parsing buffers (and
287 // maybe inserting ascii text)
289 // when appending characters, no need to update tables
291 owner_->setFont(pos, font);
295 text.insert(text.begin() + pos, c);
297 // Update the font table.
298 FontTable search_font(pos, LyXFont());
299 for (FontList::iterator it = lower_bound(fontlist.begin(),
301 search_font, matchFT());
302 it != fontlist.end(); ++it)
304 it->pos(it->pos() + 1);
308 owner_->insetlist.increasePosAfterPos(pos);
310 owner_->setFont(pos, font);
314 void Paragraph::Pimpl::insertInset(pos_type pos,
315 Inset * inset, LyXFont const & font, Change change)
318 lyx::Assert(pos <= size());
320 insertChar(pos, META_INSET, font, change);
321 lyx::Assert(text[pos] == META_INSET);
323 // Add a new entry in the insetlist.
324 owner_->insetlist.insert(inset, pos);
325 inset->parOwner(owner_);
328 inset->setOwner(inset_owner);
332 bool Paragraph::Pimpl::erasePos(pos_type pos)
334 lyx::Assert(pos < size());
337 Change::Type changetype(changes_->lookup(pos));
338 changes_->record(Change(Change::DELETED), pos);
340 // only allow the actual removal if it was /new/ text
341 if (changetype != Change::INSERTED) {
342 if (text[pos] == Paragraph::META_INSET) {
343 Inset * i(owner_->getInset(pos));
355 void Paragraph::Pimpl::eraseIntern(pos_type pos)
357 // if it is an inset, delete the inset entry
358 if (text[pos] == Paragraph::META_INSET) {
359 owner_->insetlist.erase(pos);
362 text.erase(text.begin() + pos);
364 // Erase entries in the tables.
365 FontTable search_font(pos, LyXFont());
367 FontList::iterator it =
368 lower_bound(fontlist.begin(),
370 search_font, matchFT());
371 if (it != fontlist.end() && it->pos() == pos &&
373 (it != fontlist.begin()
374 && boost::prior(it)->pos() == pos - 1))) {
375 // If it is a multi-character font
376 // entry, we just make it smaller
377 // (see update below), otherwise we
379 unsigned int const i = it - fontlist.begin();
380 fontlist.erase(fontlist.begin() + i);
381 it = fontlist.begin() + i;
382 if (i > 0 && i < fontlist.size() &&
383 fontlist[i - 1].font() == fontlist[i].font()) {
384 fontlist.erase(fontlist.begin() + i - 1);
385 it = fontlist.begin() + i - 1;
389 // Update all other entries.
390 FontList::iterator fend = fontlist.end();
391 for (; it != fend; ++it)
392 it->pos(it->pos() - 1);
394 // Update the insetlist.
395 owner_->insetlist.decreasePosAfterPos(pos);
399 void Paragraph::Pimpl::erase(pos_type pos)
405 bool Paragraph::Pimpl::erase(pos_type start, pos_type end)
408 pos_type count = end - start;
409 bool any_erased = false;
423 void Paragraph::Pimpl::simpleTeXBlanks(ostream & os, TexRow & texrow,
425 unsigned int & column,
426 LyXFont const & font,
427 LyXLayout const & style)
429 if (style.pass_thru) return;
430 if (column > lyxrc.ascii_linelen
432 && getChar(i - 1) != ' '
434 // same in FreeSpacing mode
435 && !style.free_spacing
436 && !owner_->isFreeSpacing()
437 // In typewriter mode, we want to avoid
438 // ! . ? : at the end of a line
439 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
440 && (getChar(i - 1) == '.'
441 || getChar(i - 1) == '?'
442 || getChar(i - 1) == ':'
443 || getChar(i - 1) == '!'))) {
446 texrow.start(owner_, i + 1);
448 } else if (style.free_spacing) {
456 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
458 pos_type const len = str.length();
460 // is the paragraph large enough?
461 if (pos + len > size())
464 // does the wanted text start at point?
465 for (string::size_type i = 0; i < str.length(); ++i) {
466 if (str[i] != text[pos + i])
470 // is there a font change in middle of the word?
471 FontList::const_iterator cit = fontlist.begin();
472 FontList::const_iterator end = fontlist.end();
473 for (; cit != end; ++cit) {
474 if (cit->pos() >= pos)
477 if (cit != end && pos + len - 1 > cit->pos())
484 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const * buf,
485 BufferParams const & bparams,
490 LyXFont & running_font,
493 Change::Type & running_change,
494 LyXLayout const & style,
496 unsigned int & column,
499 if (style.pass_thru) {
500 if (c != '\0') os << c;
503 // Two major modes: LaTeX or plain
504 // Handle here those cases common to both modes
505 // and then split to handle the two modes separately.
507 case Paragraph::META_INSET: {
508 Inset * inset = owner_->getInset(i);
510 // FIXME: remove this check
514 if (inset->isTextInset()) {
515 column += Changes::latexMarkChange(os, running_change,
517 running_change = Change::UNCHANGED;
521 int const len = os.tellp();
522 //ostream::pos_type const len = os.tellp();
523 if ((inset->lyxCode() == Inset::GRAPHICS_CODE
524 || inset->lyxCode() == Inset::MATH_CODE
525 || inset->lyxCode() == Inset::URL_CODE)
526 && running_font.isRightToLeft()) {
532 #warning Bug: we can have an empty font change here!
533 // if there has just been a font change, we are going to close it
534 // right now, which means stupid latex code like \textsf{}. AFAIK,
535 // this does not harm dvi output. A minor bug, thus (JMarc)
537 // some insets cannot be inside a font change command
538 if (open_font && inset->noFontChange()) {
539 column +=running_font.
540 latexWriteEndChanges(os,
544 basefont = owner_->getLayoutFont(bparams);
545 running_font = basefont;
548 int tmp = inset->latex(buf, os, moving_arg,
555 for (int j = 0; j < tmp; ++j) {
558 texrow.start(owner_, i + 1);
561 column += int(os.tellp()) - len;
566 case Paragraph::META_NEWLINE:
568 column += running_font.latexWriteEndChanges(os,
573 basefont = owner_->getLayoutFont(bparams);
574 running_font = basefont;
577 case Paragraph::META_HFILL:
583 // And now for the special cases within each mode
587 os << "\\textbackslash{}";
591 case '±': case '²': case '³':
592 case '×': case '÷': case '¹':
593 case '¬': case 'µ':
594 if ((bparams.inputenc == "latin1" ||
595 bparams.inputenc == "latin9") ||
596 (bparams.inputenc == "auto" &&
597 (font.language()->encoding()->LatexName()
599 font.language()->encoding()->LatexName()
601 os << "\\ensuremath{"
610 case '|': case '<': case '>':
611 // In T1 encoding, these characters exist
612 if (lyxrc.fontenc == "T1") {
614 //... but we should avoid ligatures
615 if ((c == '>' || c == '<')
617 && getChar(i + 1) == c) {
618 //os << "\\textcompwordmark{}";
619 // Jean-Marc, have a look at
620 // this. IÂ think this works
628 // Typewriter font also has them
629 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
633 // Otherwise, we use what LaTeX
637 os << "\\textless{}";
641 os << "\\textgreater{}";
651 case '-': // "--" in Typewriter mode -> "-{}-"
653 && getChar(i + 1) == '-'
654 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
663 os << "\\char`\\\"{}";
668 if (bparams.inputenc == "default") {
677 case '%': case '#': case '{':
684 os << "\\textasciitilde{}";
689 os << "\\textasciicircum{}";
693 case '*': case '[': case ']':
694 // avoid being mistaken for optional arguments
695 os << '{' << c << '}';
700 // Blanks are printed before font switching.
701 // Sure? I am not! (try nice-latex)
702 // I am sure it's correct. LyX might be smarter
703 // in the future, but for now, nothing wrong is
709 // I assume this is hack treating typewriter as verbatim
710 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
719 // FIXME: if we have "LaTeX" with a font
720 // change in the middle (before the 'T', then
721 // the "TeX" part is still special cased.
722 // Really we should only operate this on
723 // "words" for some definition of word
727 for (; pnr < phrases_nr; ++pnr) {
728 if (isTextAt(special_phrases[pnr].phrase, i)) {
729 os << special_phrases[pnr].macro;
730 i += special_phrases[pnr].phrase.length() - 1;
731 column += special_phrases[pnr].macro.length() - 1;
736 if (pnr == phrases_nr && c != '\0') {
746 Paragraph * Paragraph::Pimpl::TeXDeeper(Buffer const * buf,
747 BufferParams const & bparams,
748 ostream & os, TexRow & texrow)
750 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
751 Paragraph * par = owner_;
753 while (par && par->params().depth() == owner_->params().depth()) {
754 if (par->layout()->isEnvironment()) {
755 par = par->TeXEnvironment(buf, bparams,
758 par = par->TeXOnePar(buf, bparams,
762 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
768 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
769 LyXLayout const & layout) const
771 BufferParams const & bparams = features.bufferParams();
774 if (params.lineTop() || params.lineBottom())
775 features.require("lyxline");
776 if (!params.spacing().isDefault())
777 features.require("setspace");
780 features.useLayout(layout.name());
783 Language const * doc_language = bparams.language;
785 FontList::const_iterator fcit = fontlist.begin();
786 FontList::const_iterator fend = fontlist.end();
787 for (; fcit != fend; ++fcit) {
788 if (fcit->font().noun() == LyXFont::ON) {
789 lyxerr[Debug::LATEX] << "font.noun: "
790 << fcit->font().noun()
792 features.require("noun");
793 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
794 << fcit->font().stateText(0)
797 switch (fcit->font().color()) {
799 case LColor::inherit:
801 // probably we should put here all interface colors used for
802 // font displaying! For now I just add this ones I know of (Jug)
807 features.require("color");
808 lyxerr[Debug::LATEX] << "Color enabled. Font: "
809 << fcit->font().stateText(0)
813 Language const * language = fcit->font().language();
814 if (language->babel() != doc_language->babel() &&
815 language != ignore_language &&
816 language != latex_language)
818 features.useLanguage(language);
819 lyxerr[Debug::LATEX] << "Found language "
820 << language->babel() << endl;
824 if (!params.leftIndent().zero())
825 features.require("ParagraphLeftIndent");
828 InsetList::iterator icit = owner_->insetlist.begin();
829 InsetList::iterator iend = owner_->insetlist.end();
830 for (; icit != iend; ++icit) {
831 if (icit.getInset()) {
832 icit.getInset()->validate(features);
833 if (layout.needprotect &&
834 icit.getInset()->lyxCode() == Inset::FOOT_CODE)
835 features.require("NeedLyXFootnoteCode");
840 for (pos_type i = 0; i < size() ; ++i) {
841 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
842 if (!special_phrases[pnr].builtin
843 && isTextAt(special_phrases[pnr].phrase, i)) {
844 features.require(special_phrases[pnr].phrase);
852 LyXFont const Paragraph::Pimpl::realizeFont(LyXFont const & font,
853 BufferParams const & bparams) const
855 LyXFont tmpfont(font);
857 // check for environment font information
858 char par_depth = owner_->getDepth();
859 Paragraph const * par = owner_;
860 LyXTextClass const & tclass = bparams.getLyXTextClass();
862 while (par && par->getDepth() && !tmpfont.resolved()) {
863 par = par->outerHook();
865 tmpfont.realize(par->layout()->font);
866 par_depth = par->getDepth();
870 tmpfont.realize(tclass.defaultfont());