3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Richard Kimberly Heck (XHTML output)
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "Paragraph.h"
24 #include "BufferParams.h"
25 #include "BufferEncodings.h"
28 #include "InsetList.h"
30 #include "LaTeXFeatures.h"
35 #include "OutputParams.h"
36 #include "output_latex.h"
37 #include "output_xhtml.h"
38 #include "output_docbook.h"
39 #include "ParagraphParameters.h"
41 #include "SpellChecker.h"
42 #include "texstream.h"
45 #include "TextClass.h"
46 #include "WordLangTuple.h"
49 #include "frontends/alert.h"
51 #include "insets/InsetArgument.h"
52 #include "insets/InsetBibitem.h"
53 #include "insets/InsetLabel.h"
54 #include "insets/InsetSpecialChar.h"
55 #include "insets/InsetText.h"
57 #include "mathed/InsetMathHull.h"
59 #include "support/debug.h"
60 #include "support/docstring_list.h"
61 #include "support/ExceptionMessage.h"
62 #include "support/gettext.h"
63 #include "support/lassert.h"
64 #include "support/lstrings.h"
65 #include "support/textutils.h"
72 using namespace lyx::support;
74 // OSX clang, gcc < 4.8.0, and msvc < 2015 do not support C++11 thread_local
75 #if defined(__APPLE__) || (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 8)
76 #define THREAD_LOCAL_STATIC static __thread
77 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
78 #define THREAD_LOCAL_STATIC static __declspec(thread)
80 #define THREAD_LOCAL_STATIC thread_local static
85 /////////////////////////////////////////////////////////////////////
89 /////////////////////////////////////////////////////////////////////
91 class SpellResultRange {
93 SpellResultRange(FontSpan range, SpellChecker::Result result)
94 : range_(range), result_(result)
97 FontSpan const & range() const { return range_; }
99 void range(FontSpan const & r) { range_ = r; }
101 SpellChecker::Result result() const { return result_; }
103 void result(SpellChecker::Result r) { result_ = r; }
105 bool contains(pos_type pos) const { return range_.contains(pos); }
107 bool covered(FontSpan const & r) const
109 // 1. first of new range inside current range or
110 // 2. last of new range inside current range or
111 // 3. first of current range inside new range or
112 // 4. last of current range inside new range
113 //FIXME: is this the same as !range_.intersect(r).empty() ?
114 return range_.contains(r.first) || range_.contains(r.last) ||
115 r.contains(range_.first) || r.contains(range_.last);
118 void shift(pos_type pos, int offset)
120 if (range_.first > pos) {
121 range_.first += offset;
122 range_.last += offset;
123 } else if (range_.last >= pos) {
124 range_.last += offset;
129 SpellChecker::Result result_ ;
133 /////////////////////////////////////////////////////////////////////
137 /////////////////////////////////////////////////////////////////////
139 class SpellCheckerState {
143 needs_refresh_ = true;
144 current_change_number_ = 0;
147 void setRange(FontSpan const & fp, SpellChecker::Result state)
150 RangesIterator et = ranges_.end();
151 RangesIterator it = ranges_.begin();
152 for (; it != et; ++it) {
153 if (!it->covered(fp))
154 result.push_back(SpellResultRange(it->range(), it->result()));
155 else if (state == SpellChecker::WORD_OK) {
156 // trim or split the current misspelled range
157 // store misspelled ranges only
158 FontSpan range = it->range();
159 if (fp.first > range.first) {
160 // misspelled area in front of WORD_OK
161 range.last = fp.first - 1;
162 result.push_back(SpellResultRange(range, it->result()));
165 if (fp.last < range.last) {
166 // misspelled area after WORD_OK range
167 range.first = fp.last + 1;
168 result.push_back(SpellResultRange(range, it->result()));
173 if (state != SpellChecker::WORD_OK)
174 ranges_.push_back(SpellResultRange(fp, state));
177 void increasePosAfterPos(pos_type pos)
179 correctRangesAfterPos(pos, 1);
183 void decreasePosAfterPos(pos_type pos)
185 correctRangesAfterPos(pos, -1);
189 void refreshLast(pos_type pos)
191 if (pos < refresh_.last)
195 SpellChecker::Result getState(pos_type pos) const
197 SpellChecker::Result result = SpellChecker::WORD_OK;
198 RangesIterator et = ranges_.end();
199 RangesIterator it = ranges_.begin();
200 for (; it != et; ++it) {
201 if(it->contains(pos)) {
208 FontSpan const & getRange(pos_type pos) const
210 /// empty span to indicate mismatch
211 static FontSpan empty_;
212 RangesIterator et = ranges_.end();
213 RangesIterator it = ranges_.begin();
214 for (; it != et; ++it) {
215 if(it->contains(pos)) {
222 bool needsRefresh() const
224 return needs_refresh_;
227 SpellChecker::ChangeNumber currentChangeNumber() const
229 return current_change_number_;
232 void refreshRange(pos_type & first, pos_type & last) const
234 first = refresh_.first;
235 last = refresh_.last;
238 void needsRefresh(pos_type pos)
240 if (needs_refresh_ && pos != -1) {
241 if (pos < refresh_.first)
242 refresh_.first = pos;
243 if (pos > refresh_.last)
245 } else if (pos != -1) {
246 // init request check for neighbour positions too
247 refresh_.first = pos > 0 ? pos - 1 : 0;
248 // no need for special end of paragraph check
249 refresh_.last = pos + 1;
251 needs_refresh_ = pos != -1;
254 void needsCompleteRefresh(SpellChecker::ChangeNumber change_number)
256 needs_refresh_ = true;
259 current_change_number_ = change_number;
262 typedef vector<SpellResultRange> Ranges;
263 typedef Ranges::const_iterator RangesIterator;
265 /// the area of the paragraph with pending spell check
267 /// spell state cache version number
268 SpellChecker::ChangeNumber current_change_number_;
272 void correctRangesAfterPos(pos_type pos, int offset)
274 RangesIterator et = ranges_.end();
275 Ranges::iterator it = ranges_.begin();
276 for (; it != et; ++it) {
277 it->shift(pos, offset);
283 /////////////////////////////////////////////////////////////////////
285 // Paragraph::Private
287 /////////////////////////////////////////////////////////////////////
289 class Paragraph::Private
291 // Enforce our own "copy" constructor
292 Private(Private const &) = delete;
293 Private & operator=(Private const &) = delete;
294 // Unique ID generator
295 static int make_id();
298 Private(Paragraph * owner, Layout const & layout);
299 /// "Copy constructor"
300 Private(Private const &, Paragraph * owner);
301 /// Copy constructor from \p beg to \p end
302 Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
305 void insertChar(pos_type pos, char_type c, Change const & change);
307 /// Output the surrogate pair formed by \p c and \p next to \p os.
308 /// \return the number of characters written.
309 int latexSurrogatePair(BufferParams const &, otexstream & os,
310 char_type c, char_type next,
311 OutputParams const &) const;
313 /// Output a space in appropriate formatting (or a surrogate pair
314 /// if the next character is a combining character).
315 /// \return whether a surrogate pair was output.
316 bool simpleTeXBlanks(BufferParams const &,
317 OutputParams const &,
320 unsigned int & column,
322 Layout const & style) const;
324 /// This could go to ParagraphParameters if we want to.
325 int startTeXParParams(BufferParams const &, otexstream &,
326 OutputParams const &) const;
328 /// This could go to ParagraphParameters if we want to.
329 bool endTeXParParams(BufferParams const &, otexstream &,
330 OutputParams const &) const;
333 void latexInset(BufferParams const &,
338 Font const & outerfont,
340 Change & running_change,
341 Layout const & style,
343 unsigned int & column,
344 bool const fontswitch_inset,
345 bool const closeLanguage,
346 bool const lang_switched_at_inset) const;
349 void latexSpecialChar(
351 BufferParams const & bparams,
352 OutputParams const & runparams,
353 Font const & running_font,
354 string & alien_script,
355 Layout const & style,
358 unsigned int & column) const;
365 unsigned int & column) const;
371 unsigned int & column) const;
377 unsigned int & column) const;
380 void validate(LaTeXFeatures & features) const;
382 /// Checks if the paragraph contains only text and no inset or font change.
383 bool onlyText(Buffer const & buf, Font const & outerfont,
384 pos_type initial) const;
386 /// a vector of speller skip positions
387 typedef vector<FontSpan> SkipPositions;
388 typedef SkipPositions::const_iterator SkipPositionsIterator;
390 void appendSkipPosition(SkipPositions & skips, pos_type const pos) const;
392 Language * getSpellLanguage(pos_type const from) const;
394 Language * locateSpellRange(pos_type & from, pos_type & to,
395 SkipPositions & skips) const;
397 bool hasSpellerChange() const
399 SpellChecker::ChangeNumber speller_change_number = 0;
400 if (theSpellChecker())
401 speller_change_number = theSpellChecker()->changeNumber();
402 return speller_change_number > speller_state_.currentChangeNumber();
405 bool ignoreWord(docstring const & word) const;
407 void setMisspelled(pos_type from, pos_type to, SpellChecker::Result state)
409 pos_type textsize = owner_->size();
410 // check for sane arguments
411 if (to <= from || from >= textsize)
413 FontSpan fp = FontSpan(from, to - 1);
414 speller_state_.setRange(fp, state);
417 void requestSpellCheck(pos_type pos)
420 speller_state_.needsCompleteRefresh(speller_state_.currentChangeNumber());
422 speller_state_.needsRefresh(pos);
425 void readySpellCheck()
427 speller_state_.needsRefresh(-1);
430 bool needsSpellCheck() const
432 return speller_state_.needsRefresh();
435 void rangeOfSpellCheck(pos_type & first, pos_type & last) const
437 speller_state_.refreshRange(first, last);
439 last = owner_->size();
442 pos_type endpos = last;
443 owner_->locateWord(first, endpos, WHOLE_WORD, true);
446 owner_->locateWord(last, endpos, WHOLE_WORD, true);
451 int countSkips(SkipPositionsIterator & it, SkipPositionsIterator const et,
455 while (it != et && it->first < start) {
456 long skip = it->last - it->first + 1;
464 void markMisspelledWords(Language const * lang,
465 pos_type const & first, pos_type const & last,
466 SpellChecker::Result result,
467 docstring const & word,
468 SkipPositions const & skips);
470 InsetCode ownerCode() const
472 return inset_owner_ ? inset_owner_->lyxCode() : NO_CODE;
475 /// Which Paragraph owns us?
479 Inset const * inset_owner_;
485 ParagraphParameters params_;
487 /// for recording and looking up changes
491 InsetList insetlist_;
494 pos_type begin_of_body_;
496 typedef docstring TextContainer;
500 typedef set<docstring> Words;
501 typedef map<string, Words> LangWordsMap;
505 Layout const * layout_;
507 SpellCheckerState speller_state_;
513 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
514 : owner_(owner), inset_owner_(nullptr), begin_of_body_(0), layout_(&layout), id_(-1)
521 int Paragraph::Private::make_id()
523 // The id is unique per session across buffers because it is used in
524 // LFUN_PARAGRAPH_GOTO to switch to a different buffer, for instance in the
527 static int next_id(0);
532 Paragraph::Private::Private(Private const & p, Paragraph * owner)
533 : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_),
534 params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
535 begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
536 layout_(p.layout_), id_(make_id())
538 requestSpellCheck(p.text_.size());
542 Paragraph::Private::Private(Private const & p, Paragraph * owner,
543 pos_type beg, pos_type end)
544 : owner_(owner), inset_owner_(p.inset_owner_),
545 params_(p.params_), changes_(p.changes_),
546 insetlist_(p.insetlist_, beg, end),
547 begin_of_body_(p.begin_of_body_), words_(p.words_),
548 layout_(p.layout_), id_(make_id())
550 if (beg >= pos_type(p.text_.size()))
552 text_ = p.text_.substr(beg, end - beg);
554 FontList::const_iterator fcit = fontlist_.begin();
555 FontList::const_iterator fend = fontlist_.end();
556 for (; fcit != fend; ++fcit) {
557 if (fcit->pos() < beg)
559 if (fcit->pos() >= end) {
560 // Add last entry in the fontlist_.
561 fontlist_.set(text_.size() - 1, fcit->font());
564 // Add a new entry in the fontlist_.
565 fontlist_.set(fcit->pos() - beg, fcit->font());
567 requestSpellCheck(p.text_.size());
571 /////////////////////////////////////////////////////////////////////
575 /////////////////////////////////////////////////////////////////////
579 /** This helper class should be instantiated at the start of methods
580 * that can create or merge changes. If as a result the value of
581 * Paragraph::isChanged is modified, it makes sure that updateBuffer()
584 struct ChangesMonitor {
586 ChangesMonitor(Paragraph & par)
587 : par_(par), was_changed_(par.isChanged()) {}
591 /* We may need to run updateBuffer to check whether the buffer
592 * contains changes (and toggle the changes toolbar). We do it
594 * 1. the `changedness' of the paragraph has changed,
595 * 2. and we are not in the situation where the buffer has changes
596 * and new changes are added to the paragraph.
599 if (par_.isChanged() != was_changed_
600 && par_.inInset().isBufferValid()
601 && !(par_.inInset().buffer().areChangesPresent() && par_.isChanged()))
602 par_.inInset().buffer().forceUpdate();
603 } catch(support::ExceptionMessage const &) {}
608 Paragraph const & par_;
615 void Paragraph::addChangesToToc(DocIterator const & cdit, Buffer const & buf,
616 bool output_active, TocBackend & backend) const
618 d->changes_.addToToc(cdit, buf, output_active, backend);
622 bool Paragraph::isDeleted(pos_type start, pos_type end) const
624 LASSERT(start >= 0 && start <= size(), return false);
625 LASSERT(end > start && end <= size() + 1, return false);
627 return d->changes_.isDeleted(start, end);
631 bool Paragraph::isChanged(pos_type start, pos_type end) const
633 LASSERT(start >= 0 && start <= size(), return false);
634 LASSERT(end > start && end <= size() + 1, return false);
636 return d->changes_.isChanged(start, end);
639 // FIXME: Ideally the diverse isChanged() methods should account for that!
640 bool Paragraph::hasChangedInsets(pos_type start, pos_type end) const
642 LASSERT(start >= 0 && start <= size(), return false);
643 LASSERT(end > start && end <= size() + 1, return false);
645 for (auto const & icit : d->insetlist_) {
646 if (icit.pos < start)
650 if (icit.inset && icit.inset->isChanged())
656 bool Paragraph::isChanged() const
658 return d->changes_.isChanged();
662 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
664 // keep the logic here in sync with the logic of eraseChars()
668 Change const & change = d->changes_.lookup(size());
669 return change.inserted() && change.currentAuthor();
672 Change Paragraph::parEndChange() const
674 return d->changes_.lookup(size());
678 void Paragraph::setChange(Change const & change)
680 // Make sure that Buffer::hasChangesPresent is updated
681 ChangesMonitor cm(*this);
683 // beware of the imaginary end-of-par character!
684 d->changes_.set(change, 0, size() + 1);
687 * Propagate the change recursively - but not in case of DELETED!
689 * Imagine that your co-author makes changes in an existing inset. He
690 * sends your document to you and you come to the conclusion that the
691 * inset should go completely. If you erase it, LyX must not delete all
692 * text within the inset. Otherwise, the change tracked insertions of
693 * your co-author get lost and there is no way to restore them later.
695 * Conclusion: An inset's content should remain untouched if you delete it
698 if (!change.deleted()) {
699 for (pos_type pos = 0; pos < size(); ++pos) {
700 if (Inset * inset = getInset(pos))
701 inset->setChange(change);
707 void Paragraph::setChange(pos_type pos, Change const & change)
709 // Make sure that Buffer::hasChangesPresent is updated
710 ChangesMonitor cm(*this);
712 LASSERT(pos >= 0 && pos <= size(), return);
713 d->changes_.set(change, pos);
715 // see comment in setChange(Change const &) above
716 if (!change.deleted() && pos < size())
717 if (Inset * inset = getInset(pos))
718 inset->setChange(change);
722 Change const & Paragraph::lookupChange(pos_type pos) const
724 LBUFERR(pos >= 0 && pos <= size());
725 return d->changes_.lookup(pos);
729 void Paragraph::acceptChanges(pos_type start, pos_type end)
731 // Make sure that Buffer::hasChangesPresent is updated
732 ChangesMonitor cm(*this);
734 LASSERT(start >= 0 && start <= size(), return);
735 LASSERT(end > start && end <= size() + 1, return);
737 for (pos_type pos = start; pos < end; ++pos) {
738 switch (lookupChange(pos).type) {
739 case Change::UNCHANGED:
740 // accept changes in nested inset
741 if (Inset * inset = getInset(pos))
742 inset->acceptChanges();
745 case Change::INSERTED:
746 d->changes_.set(Change(Change::UNCHANGED), pos);
747 // also accept changes in nested inset
748 if (Inset * inset = getInset(pos))
749 inset->acceptChanges();
752 case Change::DELETED:
753 // Suppress access to non-existent
754 // "end-of-paragraph char"
756 eraseChar(pos, false);
767 void Paragraph::rejectChanges(pos_type start, pos_type end)
769 LASSERT(start >= 0 && start <= size(), return);
770 LASSERT(end > start && end <= size() + 1, return);
772 // Make sure that Buffer::hasChangesPresent is updated
773 ChangesMonitor cm(*this);
775 for (pos_type pos = start; pos < end; ++pos) {
776 switch (lookupChange(pos).type) {
777 case Change::UNCHANGED:
778 // reject changes in nested inset
779 if (Inset * inset = getInset(pos))
780 inset->rejectChanges();
783 case Change::INSERTED:
784 // Suppress access to non-existent
785 // "end-of-paragraph char"
787 eraseChar(pos, false);
793 case Change::DELETED:
794 d->changes_.set(Change(Change::UNCHANGED), pos);
796 // Do NOT reject changes within a deleted inset!
797 // There may be insertions of a co-author inside of it!
805 void Paragraph::Private::insertChar(pos_type pos, char_type c,
806 Change const & change)
808 LASSERT(pos >= 0 && pos <= int(text_.size()), return);
810 // Make sure that Buffer::hasChangesPresent is updated
811 ChangesMonitor cm(*owner_);
814 changes_.insert(change, pos);
816 // This is actually very common when parsing buffers (and
817 // maybe inserting ascii text)
818 if (pos == pos_type(text_.size())) {
819 // when appending characters, no need to update tables
821 // but we want spell checking
822 requestSpellCheck(pos);
826 text_.insert(text_.begin() + pos, c);
828 // Update the font table.
829 fontlist_.increasePosAfterPos(pos);
832 insetlist_.increasePosAfterPos(pos);
834 // Update list of misspelled positions
835 speller_state_.increasePosAfterPos(pos);
838 theSession().bookmarks().adjustPosAfterPos(inset_owner_->buffer().fileName(),
843 bool Paragraph::insertInset(pos_type pos, Inset * inset,
844 Font const & font, Change const & change)
846 LASSERT(inset, return false);
847 LASSERT(pos >= 0 && pos <= size(), return false);
849 // Make sure that Buffer::hasChangesPresent is updated
850 ChangesMonitor cm(*this);
852 // Paragraph::insertInset() can be used in cut/copy/paste operation where
853 // d->inset_owner_ is not set yet.
854 if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
857 d->insertChar(pos, META_INSET, change);
858 LASSERT(d->text_[pos] == META_INSET, return false);
860 // Add a new entry in the insetlist_.
861 d->insetlist_.insert(inset, pos);
863 // Some insets require run of spell checker
864 requestSpellCheck(pos);
870 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
872 LASSERT(pos >= 0 && pos <= size(), return false);
874 // Make sure that Buffer::hasChangesPresent is updated
875 ChangesMonitor cm(*this);
877 // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
880 Change change = d->changes_.lookup(pos);
882 // set the character to DELETED if
883 // a) it was previously unchanged or
884 // b) it was inserted by a co-author
886 if (!change.changed() ||
887 (change.inserted() && !change.currentAuthor())) {
888 setChange(pos, Change(Change::DELETED));
889 // request run of spell checker
890 requestSpellCheck(pos);
894 if (change.deleted())
898 // Don't physically access the imaginary end-of-paragraph character.
899 // eraseChar() can only mark it as DELETED. A physical deletion of
900 // end-of-par must be handled externally.
906 d->changes_.erase(pos);
908 // if it is an inset, delete the inset entry
909 if (d->text_[pos] == META_INSET)
910 d->insetlist_.erase(pos);
912 d->text_.erase(d->text_.begin() + pos);
914 // Update the fontlist_
915 d->fontlist_.erase(pos);
917 // Update the insetlist_
918 d->insetlist_.decreasePosAfterPos(pos);
920 // Update list of misspelled positions
921 d->speller_state_.decreasePosAfterPos(pos);
922 d->speller_state_.refreshLast(size());
925 theSession().bookmarks().adjustPosAfterPos(d->inset_owner_->buffer().fileName(),
932 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
934 LASSERT(start >= 0 && start <= size(), return 0);
935 LASSERT(end >= start && end <= size() + 1, return 0);
938 for (pos_type count = end - start; count; --count) {
939 if (!eraseChar(i, trackChanges))
946 // Handle combining characters
947 int Paragraph::Private::latexSurrogatePair(BufferParams const & bparams,
948 otexstream & os, char_type c, char_type next,
949 OutputParams const & runparams) const
951 // Writing next here may circumvent a possible font change between
952 // c and next. Since next is only output if it forms a surrogate pair
953 // with c we can ignore this:
954 // A font change inside a surrogate pair does not make sense and is
955 // hopefully impossible to input.
956 // FIXME: change tracking
957 // Is this correct WRT change tracking?
958 Encoding const & encoding = *(runparams.encoding);
959 docstring latex1 = encoding.latexChar(next).first;
960 if (runparams.inIPA) {
961 string const tipashortcut = Encodings::TIPAShortcut(next);
962 if (!tipashortcut.empty()) {
963 latex1 = from_ascii(tipashortcut);
966 docstring latex2 = encoding.latexChar(c).first;
968 if (bparams.useNonTeXFonts || docstring(1, next) == latex1) {
969 // Encoding supports the combination:
970 // output as is (combining char after base char).
971 os << latex2 << latex1;
972 return latex1.length() + latex2.length();
975 os << latex1 << "{" << latex2 << "}";
976 return latex1.length() + latex2.length() + 2;
980 bool Paragraph::Private::simpleTeXBlanks(BufferParams const & bparams,
981 OutputParams const & runparams,
984 unsigned int & column,
986 Layout const & style) const
988 if (style.pass_thru || runparams.pass_thru)
991 if (i + 1 < int(text_.size())) {
992 char_type next = text_[i + 1];
993 if (Encodings::isCombiningChar(next)) {
994 // This space has an accent, so we must always output it.
995 column += latexSurrogatePair(bparams, os, ' ', next, runparams) - 1;
1000 if (runparams.linelen > 0
1001 && column > runparams.linelen
1003 && text_[i - 1] != ' '
1004 && (i + 1 < int(text_.size()))
1005 // same in FreeSpacing mode
1006 && !owner_->isFreeSpacing()
1007 // In typewriter mode, we want to avoid
1008 // ! . ? : at the end of a line
1009 && !(font.fontInfo().family() == TYPEWRITER_FAMILY
1010 && (text_[i - 1] == '.'
1011 || text_[i - 1] == '?'
1012 || text_[i - 1] == ':'
1013 || text_[i - 1] == '!'))) {
1015 os.texrow().start(owner_->id(), i + 1);
1017 } else if (style.free_spacing) {
1026 void Paragraph::Private::latexInset(BufferParams const & bparams,
1028 OutputParams & runparams,
1029 Font & running_font,
1031 Font const & outerfont,
1033 Change & running_change,
1034 Layout const & style,
1036 unsigned int & column,
1037 bool const fontswitch_inset,
1038 bool const closeLanguage,
1039 bool const lang_switched_at_inset) const
1041 Inset * inset = owner_->getInset(i);
1044 if (style.pass_thru) {
1045 odocstringstream ods;
1046 inset->plaintext(ods, runparams);
1051 // FIXME: move this to InsetNewline::latex
1052 if (inset->lyxCode() == NEWLINE_CODE || inset->lyxCode() == SEPARATOR_CODE) {
1053 // newlines are handled differently here than
1054 // the default in simpleTeXSpecialChars().
1055 if (!style.newline_allowed) {
1059 bool needPar = false;
1060 column += running_font.latexWriteEndChanges(
1061 os, bparams, runparams,
1062 basefont, basefont, needPar);
1066 if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
1069 basefont = owner_->getLayoutFont(bparams, outerfont);
1070 running_font = basefont;
1072 if (runparams.moving_arg)
1076 os.texrow().start(owner_->id(), i + 1);
1080 if (owner_->isDeleted(i)) {
1081 if( ++runparams.inDeletedInset == 1)
1082 runparams.changeOfDeletedInset = owner_->lookupChange(i);
1085 if (inset->canTrackChanges()) {
1086 column += Changes::latexMarkChange(os, bparams, running_change,
1087 Change(Change::UNCHANGED), runparams);
1088 running_change = Change(Change::UNCHANGED);
1092 odocstream::pos_type const len = os.os().tellp();
1094 if (inset->forceLTR(runparams)
1095 && running_font.isRightToLeft()
1096 // ERT is an exception, it should be output with no
1097 // decorations at all
1098 && inset->lyxCode() != ERT_CODE) {
1099 if (runparams.use_polyglossia) {
1101 } else if (running_font.language()->lang() == "farsi"
1102 || running_font.language()->lang() == "arabic_arabi")
1103 os << "\\textLR{" << termcmd;
1109 if (open_font && fontswitch_inset) {
1110 bool lang_closed = false;
1111 // Close language if needed
1112 if (closeLanguage && !lang_switched_at_inset) {
1113 // We need prev_font here as language changes directly at inset
1114 // will only be started inside the inset.
1115 Font const prev_font = (i > 0) ?
1116 owner_->getFont(bparams, i - 1, outerfont)
1118 Font tmpfont(basefont);
1119 tmpfont.setLanguage(prev_font.language());
1120 bool needPar = false;
1121 unsigned int count = tmpfont.latexWriteEndChanges(os, bparams, runparams,
1123 needPar, closeLanguage);
1125 lang_closed = count > 0;
1127 // Update the running_font, making sure, however,
1128 // to leave the language as it was.
1129 // FIXME: probably a better way to keep track of the old
1130 // language, than copying the entire font?
1131 Font const copy_font(running_font);
1132 basefont = owner_->getLayoutFont(bparams, outerfont);
1133 running_font = basefont;
1135 running_font.setLanguage(copy_font.language());
1136 OutputParams rp = runparams;
1137 rp.encoding = basefont.language()->encoding();
1138 // For these, we use switches, so they should be taken as
1139 // base inside the inset.
1140 basefont.fontInfo().setSize(copy_font.fontInfo().size());
1141 basefont.fontInfo().setFamily(copy_font.fontInfo().family());
1142 basefont.fontInfo().setSeries(copy_font.fontInfo().series());
1143 // Now re-do font changes in a way needed here
1144 // (using switches with multi-par insets)
1145 InsetText const * textinset = inset->asInsetText();
1146 bool const cprotect = textinset
1147 ? textinset->hasCProtectContent(runparams.moving_arg)
1148 && !textinset->text().isMainText()
1150 unsigned int count2 = basefont.latexWriteStartChanges(os, bparams,
1156 if (count2 == 0 && (lang_closed || lang_switched_at_inset))
1160 runparams.local_font = &basefont;
1163 if (fontswitch_inset && !closeLanguage) {
1164 // The directionality has been switched at inset.
1165 // Force markup inside.
1166 runparams.local_font = &basefont;
1169 size_t const previous_row_count = os.texrow().rows();
1172 runparams.lastid = id_;
1173 runparams.lastpos = i;
1174 inset->latex(os, runparams);
1175 } catch (EncodingException & e) {
1176 // add location information and throw again.
1185 if (os.texrow().rows() > previous_row_count) {
1186 os.texrow().start(owner_->id(), i + 1);
1189 column += (unsigned int)(os.os().tellp() - len);
1192 if (owner_->isDeleted(i))
1193 --runparams.inDeletedInset;
1197 void Paragraph::Private::latexSpecialChar(otexstream & os,
1198 BufferParams const & bparams,
1199 OutputParams const & runparams,
1200 Font const & running_font,
1201 string & alien_script,
1202 Layout const & style,
1205 unsigned int & column) const
1207 char_type const c = owner_->getUChar(bparams, runparams, i);
1209 if (style.pass_thru || runparams.pass_thru || (runparams.for_searchAdv != OutputParams::NoSearch)
1210 || contains(style.pass_thru_chars, c)
1211 || contains(runparams.pass_thru_chars, c)) {
1212 if (runparams.for_searchAdv != OutputParams::NoSearch) {
1216 os << "\\braceleft ";
1218 os << "\\braceright ";
1222 else if (c != '\0') {
1223 Encoding const * const enc = runparams.encoding;
1224 if (enc && !enc->encodable(c))
1225 throw EncodingException(c);
1231 // TIPA uses its own T3 encoding
1232 if (runparams.inIPA && latexSpecialT3(c, os, i, column))
1234 // If T1 font encoding is used, use the special
1235 // characters it provides.
1236 // NOTE: Some languages reset the font encoding internally to a
1237 // non-standard font encoding. If we are using such a language,
1238 // we do not output special T1 chars.
1239 if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1240 && !runparams.isFullUnicode() && bparams.main_font_encoding() == "T1"
1241 && latexSpecialT1(c, os, i, column))
1243 // NOTE: "fontspec" (non-TeX fonts) sets the font encoding to "TU" (untill 2017 "EU1" or "EU2")
1244 else if (!runparams.inIPA && !running_font.language()->internalFontEncoding()
1245 && runparams.isFullUnicode() && latexSpecialTU(c, os, i, column))
1248 // Otherwise, we use what LaTeX provides us.
1251 os << "\\textbackslash" << termcmd;
1255 os << "\\textless" << termcmd;
1259 os << "\\textgreater" << termcmd;
1263 os << "\\textbar" << termcmd;
1268 if (i + 1 < static_cast<pos_type>(text_.size()) &&
1269 (end_pos == -1 || i + 1 < end_pos) &&
1270 text_[i+1] == '-') {
1271 // Prevent "--" becoming an en dash and "---" an em dash.
1272 // (Within \ttfamily, "---" is merged to en dash + hyphen.)
1278 os << "\\textquotedbl" << termcmd;
1283 case '%': case '#': case '{':
1291 os << "\\textasciitilde" << termcmd;
1296 os << "\\textasciicircum" << termcmd;
1303 // avoid being mistaken for optional arguments
1311 // Blanks are printed before font switching.
1312 // Sure? I am not! (try nice-latex)
1313 // I am sure it's correct. LyX might be smarter
1314 // in the future, but for now, nothing wrong is
1320 // XeTeX's dash behaviour is determined via a global setting
1321 if (bparams.use_dash_ligatures
1322 && owner_->getFontSettings(bparams, i).fontInfo().family() != TYPEWRITER_FAMILY
1324 // TODO #10961: && not in inset Flex Code
1325 // TODO #10961: && not in layout LyXCode
1326 && (!bparams.useNonTeXFonts || runparams.flavor != Flavor::XeTeX)) {
1343 Encoding const & encoding = *(runparams.encoding);
1344 char_type next = '\0';
1345 if (i + 1 < int(text_.size())) {
1346 next = text_[i + 1];
1347 if (Encodings::isCombiningChar(next)) {
1348 column += latexSurrogatePair(bparams, os, c, next, runparams) - 1;
1353 pair<docstring, bool> latex = encoding.latexChar(c);
1354 docstring nextlatex;
1355 bool nexttipas = false;
1356 string nexttipashortcut;
1357 if (next != '\0' && next != META_INSET && !encoding.encodable(next)) {
1358 nextlatex = encoding.latexChar(next).first;
1359 if (runparams.inIPA) {
1360 nexttipashortcut = Encodings::TIPAShortcut(next);
1361 nexttipas = !nexttipashortcut.empty();
1365 if (runparams.inIPA) {
1366 string const tipashortcut = Encodings::TIPAShortcut(c);
1367 if (!tipashortcut.empty()) {
1368 latex.first = from_ascii(tipashortcut);
1369 latex.second = false;
1373 // eventually close "script wrapper" command (see `Paragraph::latex`)
1374 if (!alien_script.empty()
1375 && alien_script != Encodings::isKnownScriptChar(next)) {
1376 column += latex.first.length();
1377 alien_script.clear();
1378 os << latex.first << "}";
1382 && ((!prefixIs(nextlatex, '\\')
1383 && !prefixIs(nextlatex, '{')
1384 && !prefixIs(nextlatex, '}'))
1386 && !prefixIs(from_ascii(nexttipashortcut), '\\')))
1388 // Prevent eating of a following space or command corruption by
1389 // following characters
1390 if (next == ' ' || next == '\0') {
1391 column += latex.first.length() + 1;
1392 os << latex.first << "{}";
1394 column += latex.first.length();
1395 os << latex.first << " ";
1398 column += latex.first.length() - 1;
1406 bool Paragraph::Private::latexSpecialT1(char_type const c, otexstream & os,
1407 pos_type i, unsigned int & column) const
1413 // In T1 encoding, these characters exist
1414 // but we should avoid ligatures
1415 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
1417 os << "\\textcompwordmark" << termcmd;
1424 // soul.sty breaks with \char`\"
1425 os << "\\textquotedbl" << termcmd;
1434 bool Paragraph::Private::latexSpecialTU(char_type const c, otexstream & os,
1435 pos_type i, unsigned int & column) const
1437 // TU encoding is currently on par with T1.
1438 return latexSpecialT1(c, os, i, column);
1442 bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
1443 pos_type /*i*/, unsigned int & column) const
1453 os << "\\textvertline" << termcmd;
1462 void Paragraph::Private::validate(LaTeXFeatures & features) const
1464 if (layout_->inpreamble && inset_owner_) {
1465 // FIXME: Using a string stream here circumvents the encoding
1466 // switching machinery of odocstream. Therefore the
1467 // output is wrong if this paragraph contains content
1468 // that needs to switch encoding.
1469 Buffer const & buf = inset_owner_->buffer();
1470 otexstringstream os;
1471 os << layout_->preamble();
1472 size_t const length = os.length();
1473 TeXOnePar(buf, *inset_owner_->getText(int(buf.getParFromID(owner_->id()).idx())),
1474 buf.getParFromID(owner_->id()).pit(), os,
1475 features.runparams(), string(), 0, -1, true);
1476 if (os.length() > length)
1477 features.addPreambleSnippet(os.release(), true);
1480 if (features.runparams().flavor == Flavor::Html
1481 && layout_->htmltitle()) {
1482 features.setHTMLTitle(owner_->asString(AS_STR_INSETS | AS_STR_SKIPDELETE));
1485 // check the params.
1486 if (!params_.spacing().isDefault())
1487 features.require("setspace");
1490 features.useLayout(layout_->name());
1493 fontlist_.validate(features);
1495 // then the indentation
1496 if (!params_.leftIndent().zero())
1497 features.require("ParagraphLeftIndent");
1500 InsetList::const_iterator icit = insetlist_.begin();
1501 InsetList::const_iterator iend = insetlist_.end();
1502 for (; icit != iend; ++icit) {
1504 features.inDeletedInset(owner_->isDeleted(icit->pos));
1505 if (icit->inset->lyxCode() == FOOT_CODE) {
1506 // FIXME: an item inset would make things much easier.
1507 if ((layout_->latextype == LATEX_LIST_ENVIRONMENT
1508 || (layout_->latextype == LATEX_ITEM_ENVIRONMENT
1509 && layout_->margintype == MARGIN_FIRST_DYNAMIC))
1510 && (icit->pos < begin_of_body_
1511 || (icit->pos == begin_of_body_
1512 && (icit->pos == 0 || text_[icit->pos - 1] != ' '))))
1513 features.saveNoteEnv("description");
1515 icit->inset->validate(features);
1516 features.inDeletedInset(false);
1517 if (layout_->needprotect &&
1518 icit->inset->lyxCode() == FOOT_CODE)
1519 features.require("NeedLyXFootnoteCode");
1523 // then the contents
1524 BufferParams const bp = features.runparams().is_child
1525 ? features.buffer().masterParams() : features.buffer().params();
1526 for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1527 char_type c = text_[i];
1528 CharInfo const & ci = Encodings::unicodeCharInfo(c);
1530 if (features.runparams().isFullUnicode() && bp.useNonTeXFonts)
1531 features.require("textquotedblp");
1532 else if (bp.main_font_encoding() != "T1"
1533 || ((&owner_->getFontSettings(bp, i))->language()->internalFontEncoding()))
1534 features.require("textquotedbl");
1535 } else if (ci.textfeature() && contains(ci.textpreamble(), '=')) {
1536 // features that depend on the font or input encoding
1537 string feats = ci.textpreamble();
1538 string fontenc = (&owner_->getFontSettings(bp, i))->language()->fontenc(bp);
1539 if (fontenc.empty())
1540 fontenc = features.runparams().main_fontenc;
1541 while (!feats.empty()) {
1543 feats = split(feats, feat, ',');
1544 if (contains(feat, "!=")) {
1545 // a feature that is required except for the spcified
1546 // font or input encodings
1548 string const contexts = ltrim(split(feat, realfeature, '!'), "=");
1549 // multiple encodings are separated by semicolon
1550 vector<string> context = getVectorFromString(contexts, ";");
1551 // require feature if the context matches neither current font
1552 // nor input encoding
1553 if (std::find(context.begin(), context.end(), fontenc) == context.end()
1554 && std::find(context.begin(), context.end(),
1555 features.runparams().encoding->name()) == context.end())
1556 features.require(realfeature);
1557 } else if (contains(feat, '=')) {
1558 // a feature that is required only for the spcified
1559 // font or input encodings
1561 string const contexts = split(feat, realfeature, '=');
1562 // multiple encodings are separated by semicolon
1563 vector<string> context = getVectorFromString(contexts, ";");
1564 // require feature if the context matches either current font
1565 // or input encoding
1566 if (std::find(context.begin(), context.end(), fontenc) != context.end()
1567 || std::find(context.begin(), context.end(),
1568 features.runparams().encoding->name()) != context.end())
1569 features.require(realfeature);
1572 } else if (!bp.use_dash_ligatures
1573 && (c == 0x2013 || c == 0x2014)
1574 && bp.useNonTeXFonts
1575 && features.runparams().flavor == Flavor::XeTeX)
1576 // XeTeX's dash behaviour is determined via a global setting
1577 features.require("xetexdashbreakstate");
1578 BufferEncodings::validate(c, features);
1582 /////////////////////////////////////////////////////////////////////
1586 /////////////////////////////////////////////////////////////////////
1589 Layout const emptyParagraphLayout;
1592 Paragraph::Paragraph()
1593 : d(new Paragraph::Private(this, emptyParagraphLayout))
1600 Paragraph::Paragraph(Paragraph const & par)
1601 : itemdepth(par.itemdepth),
1602 d(new Paragraph::Private(*par.d, this))
1608 Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
1609 : itemdepth(par.itemdepth),
1610 d(new Paragraph::Private(*par.d, this, beg, end))
1616 Paragraph & Paragraph::operator=(Paragraph const & par)
1618 // needed as we will destroy the private part before copying it
1620 itemdepth = par.itemdepth;
1624 d = new Private(*par.d, this);
1631 Paragraph::~Paragraph()
1640 // this shall be called just before every "os << ..." action.
1641 void flushString(ostream & os, docstring & s)
1650 void Paragraph::write(ostream & os, BufferParams const & bparams,
1651 depth_type & depth) const
1653 // The beginning or end of a deeper (i.e. nested) area?
1654 if (depth != d->params_.depth()) {
1655 if (d->params_.depth() > depth) {
1656 while (d->params_.depth() > depth) {
1657 os << "\n\\begin_deeper";
1661 while (d->params_.depth() < depth) {
1662 os << "\n\\end_deeper";
1668 // First write the layout
1669 os << "\n\\begin_layout " << to_utf8(d->layout_->name()) << '\n';
1671 d->params_.write(os);
1673 Font font1(inherit_font, bparams.language);
1675 Change running_change = Change(Change::UNCHANGED);
1677 // this string is used as a buffer to avoid repetitive calls
1678 // to to_utf8(), which turn out to be expensive (JMarc)
1679 docstring write_buffer;
1682 for (pos_type i = 0; i <= size(); ++i) {
1684 Change const & change = lookupChange(i);
1685 if (change != running_change)
1686 flushString(os, write_buffer);
1687 Changes::lyxMarkChange(os, bparams, column, running_change, change);
1688 running_change = change;
1693 // Write font changes
1694 Font font2 = getFontSettings(bparams, i);
1695 if (font2 != font1) {
1696 flushString(os, write_buffer);
1697 font2.lyxWriteChanges(font1, os);
1702 char_type const c = d->text_[i];
1705 if (Inset const * inset = getInset(i)) {
1706 flushString(os, write_buffer);
1707 if (inset->directWrite()) {
1708 // international char, let it write
1709 // code directly so it's shorter in
1715 os << "\\begin_inset ";
1717 os << "\n\\end_inset\n\n";
1720 // FIXME This can be removed again once the mystery
1721 // crash has been resolved.
1726 flushString(os, write_buffer);
1727 os << "\n\\backslash\n";
1731 flushString(os, write_buffer);
1732 if (i + 1 < size() && d->text_[i + 1] == ' ') {
1739 if ((column > 70 && c == ' ')
1741 flushString(os, write_buffer);
1745 // this check is to amend a bug. LyX sometimes
1746 // inserts '\0' this could cause problems.
1748 write_buffer.push_back(c);
1750 LYXERR0("NUL char in structure.");
1756 flushString(os, write_buffer);
1757 os << "\n\\end_layout\n";
1758 // FIXME This can be removed again once the mystery
1759 // crash has been resolved.
1764 void Paragraph::validate(LaTeXFeatures & features) const
1766 d->validate(features);
1767 bool fragile = features.runparams().moving_arg;
1768 fragile |= layout().needprotect;
1769 if (inInset().getLayout().isNeedProtect())
1771 if (needsCProtection(fragile))
1772 features.require("cprotect");
1776 void Paragraph::insert(pos_type pos, docstring const & str,
1777 Font const & font, Change const & change)
1779 for (size_t i = 0, n = str.size(); i != n ; ++i)
1780 insertChar(pos + i, str[i], font, change);
1784 void Paragraph::appendChar(char_type c, Font const & font,
1785 Change const & change)
1787 // Make sure that Buffer::hasChangesPresent is updated
1788 ChangesMonitor cm(*this);
1791 d->changes_.insert(change, d->text_.size());
1792 // when appending characters, no need to update tables
1793 d->text_.push_back(c);
1794 setFont(d->text_.size() - 1, font);
1795 d->requestSpellCheck(d->text_.size() - 1);
1799 void Paragraph::appendString(docstring const & s, Font const & font,
1800 Change const & change)
1802 // Make sure that Buffer::hasChangesPresent is updated
1803 ChangesMonitor cm(*this);
1805 pos_type end = s.size();
1806 size_t oldsize = d->text_.size();
1807 size_t newsize = oldsize + end;
1808 size_t capacity = d->text_.capacity();
1809 if (newsize >= capacity)
1810 d->text_.reserve(max(capacity + 100, newsize));
1812 // when appending characters, no need to update tables
1815 // FIXME: Optimize this!
1816 for (size_t i = oldsize; i != newsize; ++i) {
1818 d->changes_.insert(change, i);
1819 d->requestSpellCheck(i);
1821 d->fontlist_.set(oldsize, font);
1822 d->fontlist_.set(newsize - 1, font);
1826 void Paragraph::insertChar(pos_type pos, char_type c,
1829 d->insertChar(pos, c, Change(trackChanges ?
1830 Change::INSERTED : Change::UNCHANGED));
1834 void Paragraph::insertChar(pos_type pos, char_type c,
1835 Font const & font, bool trackChanges)
1837 d->insertChar(pos, c, Change(trackChanges ?
1838 Change::INSERTED : Change::UNCHANGED));
1843 void Paragraph::insertChar(pos_type pos, char_type c,
1844 Font const & font, Change const & change)
1846 d->insertChar(pos, c, change);
1851 void Paragraph::resetFonts(Font const & font)
1853 d->fontlist_.clear();
1854 d->fontlist_.set(0, font);
1855 d->fontlist_.set(d->text_.size() - 1, font);
1858 // Gets uninstantiated font setting at position.
1859 Font const & Paragraph::getFontSettings(BufferParams const & bparams,
1863 LYXERR0("pos: " << pos << " size: " << size());
1867 FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1868 if (cit != d->fontlist_.end())
1871 if (pos == size() && !empty())
1872 return getFontSettings(bparams, pos - 1);
1874 // Optimisation: avoid a full font instantiation if there is no
1875 // language change from previous call.
1876 static Font previous_font;
1877 static Language const * previous_lang = nullptr;
1878 Language const * lang = getParLanguage(bparams);
1879 if (lang != previous_lang) {
1880 previous_lang = lang;
1881 previous_font = Font(inherit_font, lang);
1883 return previous_font;
1887 FontSpan Paragraph::fontSpan(pos_type pos) const
1889 LBUFERR(pos <= size());
1892 return FontSpan(pos, pos);
1895 FontList::const_iterator cit = d->fontlist_.begin();
1896 FontList::const_iterator end = d->fontlist_.end();
1897 for (; cit != end; ++cit) {
1898 if (cit->pos() >= pos) {
1899 if (pos >= beginOfBody())
1900 return FontSpan(max(start, beginOfBody()),
1903 return FontSpan(start,
1904 min(beginOfBody() - 1,
1907 start = cit->pos() + 1;
1910 // This should not happen, but if so, we take no chances.
1911 LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
1912 LASSERT(false, return FontSpan(pos, pos));
1916 // Gets uninstantiated font setting at position 0
1917 Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1919 if (!empty() && !d->fontlist_.empty())
1920 return d->fontlist_.begin()->font();
1922 // Optimisation: avoid a full font instantiation if there is no
1923 // language change from previous call.
1924 static Font previous_font;
1925 static Language const * previous_lang = nullptr;
1926 if (bparams.language != previous_lang) {
1927 previous_lang = bparams.language;
1928 previous_font = Font(inherit_font, bparams.language);
1931 return previous_font;
1935 // Gets the fully instantiated font at a given position in a paragraph
1936 // This is basically the same function as Text::GetFont() in text2.cpp.
1937 // The difference is that this one is used for generating the LaTeX file,
1938 // and thus cosmetic "improvements" are disallowed: This has to deliver
1939 // the true picture of the buffer. (Asger)
1940 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1941 Font const & outerfont) const
1945 Font font = getFontSettings(bparams, pos);
1947 pos_type const body_pos = beginOfBody();
1948 FontInfo & fi = font.fontInfo();
1950 fi.realize(d->layout_->labelfont);
1952 fi.realize(d->layout_->font);
1954 fi.realize(outerfont.fontInfo());
1955 fi.realize(bparams.getFont().fontInfo());
1961 Font const Paragraph::getLabelFont
1962 (BufferParams const & bparams, Font const & outerfont) const
1964 FontInfo tmpfont = d->layout_->labelfont;
1965 tmpfont.realize(outerfont.fontInfo());
1966 tmpfont.realize(bparams.getFont().fontInfo());
1967 return Font(tmpfont, getParLanguage(bparams));
1971 Font const Paragraph::getLayoutFont
1972 (BufferParams const & bparams, Font const & outerfont) const
1974 FontInfo tmpfont = d->layout_->font;
1975 tmpfont.realize(outerfont.fontInfo());
1976 tmpfont.realize(bparams.getFont().fontInfo());
1977 return Font(tmpfont, getParLanguage(bparams));
1981 char_type Paragraph::getUChar(BufferParams const & bparams,
1982 OutputParams const & rp,
1985 char_type c = d->text_[pos];
1987 // Return unchanged character in LTR languages
1988 // or if we use poylglossia/bidi (XeTeX).
1989 if (rp.useBidiPackage()
1990 || !getFontSettings(bparams, pos).isRightToLeft())
1993 // Without polyglossia/bidi, we need to account for some special cases.
1994 // FIXME This needs to be audited!
1996 // * The input is as expected for all delimiters
1997 // => checked for Hebrew!
1998 // * The output matches the display in the LyX workarea
1999 // => checked for Hebrew!
2000 // * The special cases below are really necessary
2001 // => checked for Hebrew!
2002 // * In arabic_arabi, brackets are transformed to Arabic
2003 // Ornate Parentheses. Is this is really wanted?
2005 string const & lang = getFontSettings(bparams, pos).language()->lang();
2008 // 1. In the following languages, parentheses need to be reversed.
2009 // Also with polyglodia/luabidi
2010 bool const reverseparens = (lang == "hebrew" || rp.use_polyglossia);
2012 // 2. In the following languages, brackets don't need to be reversed.
2013 bool const reversebrackets = lang != "arabic_arabtex"
2014 && lang != "arabic_arabi"
2017 // Now swap delimiters if needed.
2028 if (reversebrackets)
2032 if (reversebrackets)
2053 void Paragraph::setFont(pos_type pos, Font const & font)
2055 LASSERT(pos <= size(), return);
2057 // First, reduce font against layout/label font
2058 // Update: The setCharFont() routine in text2.cpp already
2059 // reduces font, so we don't need to do that here. (Asger)
2061 d->fontlist_.set(pos, font);
2065 void Paragraph::makeSameLayout(Paragraph const & par)
2067 d->layout_ = par.d->layout_;
2068 d->params_ = par.d->params_;
2072 bool Paragraph::stripLeadingSpaces(bool trackChanges)
2074 if (isFreeSpacing())
2080 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
2081 if (eraseChar(pos, trackChanges))
2087 return count > 0 || pos > 0;
2091 bool Paragraph::hasSameLayout(Paragraph const & par) const
2093 return par.d->layout_ == d->layout_
2094 && d->params_.sameLayout(par.d->params_);
2098 depth_type Paragraph::getDepth() const
2100 return d->params_.depth();
2104 depth_type Paragraph::getMaxDepthAfter() const
2106 if (d->layout_->isEnvironment())
2107 return d->params_.depth() + 1;
2109 return d->params_.depth();
2113 LyXAlignment Paragraph::getAlign(BufferParams const & bparams) const
2115 if (d->params_.align() == LYX_ALIGN_LAYOUT)
2116 return getDefaultAlign(bparams);
2118 return d->params_.align();
2122 LyXAlignment Paragraph::getDefaultAlign(BufferParams const & bparams) const
2124 LyXAlignment res = layout().align;
2125 if (isRTL(bparams)) {
2127 if (res == LYX_ALIGN_LEFT)
2128 res = LYX_ALIGN_RIGHT;
2129 else if (res == LYX_ALIGN_RIGHT)
2130 res = LYX_ALIGN_LEFT;
2136 docstring const & Paragraph::labelString() const
2138 return d->params_.labelString();
2142 // the next two functions are for the manual labels
2143 docstring const Paragraph::getLabelWidthString() const
2145 if (d->layout_->margintype == MARGIN_MANUAL
2146 || d->layout_->latextype == LATEX_BIB_ENVIRONMENT)
2147 return d->params_.labelWidthString();
2149 return _("Senseless with this layout!");
2153 void Paragraph::setLabelWidthString(docstring const & s)
2155 d->params_.labelWidthString(s);
2159 docstring Paragraph::expandLabel(Layout const & layout,
2160 BufferParams const & bparams) const
2162 return expandParagraphLabel(layout, bparams, true);
2166 docstring Paragraph::expandParagraphLabel(Layout const & layout,
2167 BufferParams const & bparams, bool process_appendix) const
2169 DocumentClass const & tclass = bparams.documentClass();
2170 string const & lang = getParLanguage(bparams)->code();
2171 bool const in_appendix = process_appendix && d->params_.appendix();
2172 docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
2174 if (fmt.empty() && !layout.counter.empty())
2175 return tclass.counters().theCounter(layout.counter, lang);
2177 // handle 'inherited level parts' in 'fmt',
2178 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
2179 size_t const i = fmt.find('@', 0);
2180 if (i != docstring::npos) {
2181 size_t const j = fmt.find('@', i + 1);
2182 if (j != docstring::npos) {
2183 docstring parent(fmt, i + 1, j - i - 1);
2184 docstring label = from_ascii("??");
2185 if (tclass.hasLayout(parent))
2186 label = expandParagraphLabel(tclass[parent], bparams,
2188 fmt = docstring(fmt, 0, i) + label
2189 + docstring(fmt, j + 1, docstring::npos);
2193 return tclass.counters().counterLabel(fmt, lang);
2197 void Paragraph::applyLayout(Layout const & new_layout)
2199 d->layout_ = &new_layout;
2200 LyXAlignment const oldAlign = d->params_.align();
2202 if (!(oldAlign & d->layout_->alignpossible)) {
2203 frontend::Alert::warning(_("Alignment not permitted"),
2204 _("The new layout does not permit the alignment previously used.\nSetting to default."));
2205 d->params_.align(LYX_ALIGN_LAYOUT);
2210 pos_type Paragraph::beginOfBody() const
2212 return d->begin_of_body_;
2216 void Paragraph::setBeginOfBody()
2218 if (d->layout_->labeltype != LABEL_MANUAL) {
2219 d->begin_of_body_ = 0;
2223 // Unroll the first two cycles of the loop
2224 // and remember the previous character to
2225 // remove unnecessary getChar() calls
2227 pos_type end = size();
2228 bool prev_char_deleted = false;
2229 if (i < end && (!(isNewline(i) || isEnvSeparator(i)) || isDeleted(i))) {
2232 char_type previous_char = d->text_[i];
2233 if (!(isNewline(i) || isEnvSeparator(i))) {
2235 while (i < end && (previous_char != ' ' || prev_char_deleted)) {
2236 char_type temp = d->text_[i];
2237 prev_char_deleted = isDeleted(i);
2238 if (!isDeleted(i) && (isNewline(i) || isEnvSeparator(i)))
2241 previous_char = temp;
2247 d->begin_of_body_ = i;
2251 bool Paragraph::allowParagraphCustomization() const
2253 return inInset().allowParagraphCustomization();
2257 bool Paragraph::usePlainLayout() const
2259 return inInset().usePlainLayout();
2263 bool Paragraph::isPassThru() const
2265 return inInset().isPassThru() || d->layout_->pass_thru;
2270 // paragraphs inside floats need different alignment tags to avoid
2273 bool noTrivlistCentering(InsetCode code)
2275 return code == FLOAT_CODE
2276 || code == WRAP_CODE
2277 || code == CELL_CODE;
2281 string correction(string const & orig)
2283 if (orig == "flushleft")
2284 return "raggedright";
2285 if (orig == "flushright")
2286 return "raggedleft";
2287 if (orig == "center")
2293 bool corrected_env(otexstream & os, string const & suffix, string const & env,
2294 InsetCode code, bool const lastpar, int & col)
2296 string macro = suffix + "{";
2297 if (noTrivlistCentering(code)) {
2299 // the last paragraph in non-trivlist-aligned
2300 // context is special (to avoid unwanted whitespace)
2301 if (suffix == "\\begin") {
2302 macro = "\\" + correction(env) + "{}";
2303 os << from_ascii(macro);
2304 col += macro.size();
2309 macro += correction(env);
2313 if (suffix == "\\par\\end") {
2317 os << from_ascii(macro);
2318 col += macro.size();
2319 if (suffix == "\\begin") {
2329 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
2330 otexstream & os, OutputParams const & runparams) const
2335 (bparams.paragraph_separation == BufferParams::ParagraphIndentSeparation) ?
2336 (layout_->toggle_indent != ITOGGLE_NEVER) :
2337 (layout_->toggle_indent == ITOGGLE_ALWAYS);
2339 if (canindent && params_.noindent() && !layout_->pass_thru) {
2340 os << "\\noindent ";
2344 LyXAlignment const curAlign = params_.align();
2346 if (curAlign == layout_->align)
2350 case LYX_ALIGN_NONE:
2351 case LYX_ALIGN_BLOCK:
2352 case LYX_ALIGN_LAYOUT:
2353 case LYX_ALIGN_SPECIAL:
2354 case LYX_ALIGN_DECIMAL:
2356 case LYX_ALIGN_LEFT:
2357 case LYX_ALIGN_RIGHT:
2358 case LYX_ALIGN_CENTER:
2359 if (runparams.moving_arg) {
2366 string const begin_tag = "\\begin";
2367 InsetCode code = ownerCode();
2368 bool const lastpar = runparams.isLastPar;
2369 // RTL in classic (PDF)LaTeX (without the Bidi package)
2370 // Luabibdi (used by LuaTeX) behaves like classic
2371 bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
2372 && !runparams.useBidiPackage();
2375 case LYX_ALIGN_NONE:
2376 case LYX_ALIGN_BLOCK:
2377 case LYX_ALIGN_LAYOUT:
2378 case LYX_ALIGN_SPECIAL:
2379 case LYX_ALIGN_DECIMAL:
2381 case LYX_ALIGN_LEFT: {
2383 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2384 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2386 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2388 } case LYX_ALIGN_RIGHT: {
2390 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2391 corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
2393 corrected_env(os, begin_tag, "flushright", code, lastpar, column);
2395 } case LYX_ALIGN_CENTER: {
2396 corrected_env(os, begin_tag, "center", code, lastpar, column);
2405 bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
2406 otexstream & os, OutputParams const & runparams) const
2408 LyXAlignment const curAlign = params_.align();
2410 if (curAlign == layout_->align)
2414 case LYX_ALIGN_NONE:
2415 case LYX_ALIGN_BLOCK:
2416 case LYX_ALIGN_LAYOUT:
2417 case LYX_ALIGN_SPECIAL:
2418 case LYX_ALIGN_DECIMAL:
2420 case LYX_ALIGN_LEFT:
2421 case LYX_ALIGN_RIGHT:
2422 case LYX_ALIGN_CENTER:
2423 if (runparams.moving_arg)
2428 bool output = false;
2430 string const end_tag = "\\par\\end";
2431 InsetCode code = ownerCode();
2432 bool const lastpar = runparams.isLastPar;
2433 // RTL in classic (PDF)LaTeX (without the Bidi package)
2434 // Luabibdi (used by LuaTeX) behaves like classic
2435 bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
2436 && !runparams.useBidiPackage();
2439 case LYX_ALIGN_NONE:
2440 case LYX_ALIGN_BLOCK:
2441 case LYX_ALIGN_LAYOUT:
2442 case LYX_ALIGN_SPECIAL:
2443 case LYX_ALIGN_DECIMAL:
2445 case LYX_ALIGN_LEFT: {
2447 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2448 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2450 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2452 } case LYX_ALIGN_RIGHT: {
2454 // Classic (PDF)LaTeX switches the left/right logic in RTL mode
2455 output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
2457 output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
2459 } case LYX_ALIGN_CENTER: {
2460 corrected_env(os, end_tag, "center", code, lastpar, col);
2465 return output || lastpar;
2469 // This one spits out the text of the paragraph
2470 void Paragraph::latex(BufferParams const & bparams,
2471 Font const & outerfont,
2473 OutputParams const & runparams,
2474 int start_pos, int end_pos, bool force) const
2476 LYXERR(Debug::LATEX, "Paragraph::latex... " << this);
2478 // FIXME This check should not be needed. Perhaps issue an
2479 // error if it triggers.
2480 Layout const & style = inInset().forcePlainLayout() ?
2481 bparams.documentClass().plainLayout() : *d->layout_;
2483 if (!force && style.inpreamble)
2486 bool const allowcust = allowParagraphCustomization();
2488 // Current base font for all inherited font changes, without any
2489 // change caused by an individual character, except for the language:
2490 // It is set to the language of the first character.
2491 // As long as we are in the label, this font is the base font of the
2492 // label. Before the first body character it is set to the base font
2496 // If there is an open font-encoding changing command (script wrapper),
2497 // alien_script is set to its name
2498 string alien_script;
2501 // Maybe we have to create a optional argument.
2502 pos_type body_pos = beginOfBody();
2503 unsigned int column = 0;
2505 // If we are inside an non inheritFont() inset, the real outerfont is local_font
2506 Font const real_outerfont = (!inInset().inheritFont()
2507 && runparams.local_font != nullptr)
2508 ? Font(runparams.local_font->fontInfo()) : outerfont;
2511 // the optional argument is kept in curly brackets in
2512 // case it contains a ']'
2513 // This is not strictly needed, but if this is changed it
2514 // would be a file format change, and tex2lyx would need
2515 // to be adjusted, since it unconditionally removes the
2516 // braces when it parses \item.
2519 basefont = getLabelFont(bparams, real_outerfont);
2521 basefont = getLayoutFont(bparams, real_outerfont);
2524 // Which font is currently active?
2525 Font running_font(basefont);
2526 // Do we have an open font change?
2527 bool open_font = false;
2529 Change runningChange =
2530 runparams.inDeletedInset && !inInset().canTrackChanges()
2531 ? runparams.changeOfDeletedInset : Change(Change::UNCHANGED);
2533 Encoding const * const prev_encoding = runparams.encoding;
2535 os.texrow().start(id(), 0);
2537 // if the paragraph is empty, the loop will not be entered at all
2539 // For InTitle commands, we have already opened a group
2540 // in output_latex::TeXOnePar.
2541 if (style.isCommand() && !style.intitle) {
2545 if (!style.leftdelim().empty()) {
2546 os << style.leftdelim();
2547 column += style.leftdelim().size();
2550 column += d->startTeXParParams(bparams, os, runparams);
2553 // Whether a \par can be issued for insets typeset inline with text.
2554 // Yes if greater than 0. This has to be static.
2555 THREAD_LOCAL_STATIC int parInline = 0;
2557 for (pos_type i = 0; i < size(); ++i) {
2558 // First char in paragraph or after label?
2559 if (i == body_pos) {
2562 bool needPar = false;
2563 column += running_font.latexWriteEndChanges(
2564 os, bparams, runparams,
2565 basefont, basefont, needPar);
2568 basefont = getLayoutFont(bparams, real_outerfont);
2569 running_font = basefont;
2571 column += Changes::latexMarkChange(os, bparams,
2572 runningChange, Change(Change::UNCHANGED),
2574 runningChange = Change(Change::UNCHANGED);
2576 os << (isEnvSeparator(i) ? "}]~" : "}] ");
2579 // For InTitle commands, we have already opened a group
2580 // in output_latex::TeXOnePar.
2581 if (style.isCommand() && !style.intitle) {
2586 if (!style.leftdelim().empty()) {
2587 os << style.leftdelim();
2588 column += style.leftdelim().size();
2592 column += d->startTeXParParams(bparams, os,
2596 runparams.wasDisplayMath = runparams.inDisplayMath;
2597 runparams.inDisplayMath = false;
2598 bool deleted_display_math = false;
2599 Change const & change = runparams.inDeletedInset
2600 ? runparams.changeOfDeletedInset : lookupChange(i);
2602 char_type const c = d->text_[i];
2604 // Check whether a display math inset follows
2605 bool output_changes;
2606 if (runparams.for_searchAdv == OutputParams::NoSearch)
2607 output_changes = bparams.output_changes;
2609 output_changes = (runparams.for_searchAdv == OutputParams::SearchWithDeleted);
2611 && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2613 runparams.ctObject = getInset(i)->getCtObject(runparams);
2615 InsetMath const * im = getInset(i)->asInsetMath();
2616 if (im && im->asHullInset()
2617 && im->asHullInset()->outerDisplay()) {
2618 runparams.inDisplayMath = true;
2619 // runparams.inDeletedInset will be set by
2620 // latexInset later, but we need this info
2621 // before it is called. On the other hand, we
2622 // cannot set it here because it is a counter.
2623 deleted_display_math = isDeleted(i);
2625 if (output_changes && deleted_display_math
2626 && runningChange == change
2627 && change.type == Change::DELETED
2628 && !os.afterParbreak()) {
2629 // A display math in the same paragraph follows.
2630 // We have to close and then reopen \lyxdeleted,
2631 // otherwise the math will be shifted up.
2632 OutputParams rp = runparams;
2634 bool needPar = false;
2635 column += running_font.latexWriteEndChanges(
2636 os, bparams, rp, basefont,
2640 basefont = (body_pos > i) ? getLabelFont(bparams, real_outerfont)
2641 : getLayoutFont(bparams, real_outerfont);
2642 running_font = basefont;
2643 column += Changes::latexMarkChange(os, bparams,
2644 Change(Change::INSERTED), change, rp);
2648 if (output_changes && runningChange != change) {
2649 if (!alien_script.empty()) {
2652 alien_script.clear();
2655 bool needPar = false;
2656 column += running_font.latexWriteEndChanges(
2657 os, bparams, runparams,
2658 basefont, basefont, needPar);
2661 basefont = (body_pos > i) ? getLabelFont(bparams, real_outerfont)
2662 : getLayoutFont(bparams, real_outerfont);
2663 running_font = basefont;
2664 column += Changes::latexMarkChange(os, bparams, runningChange,
2666 runningChange = change;
2669 // do not output text which is marked deleted
2670 // if change tracking output is disabled
2671 if (!output_changes && change.deleted()) {
2677 // Fully instantiated font
2678 Font current_font = getFont(bparams, i, outerfont);
2680 Font const prev_font = (i > 0) ?
2681 getFont(bparams, i - 1, outerfont)
2684 Font const last_font = running_font;
2685 bool const in_ct_deletion = (output_changes
2686 && runningChange == change
2687 && change.type == Change::DELETED
2688 && !os.afterParbreak());
2689 // Insets where font switches are used (rather than font commands)
2690 bool const fontswitch_inset =
2693 && getInset(i)->allowMultiPar()
2694 && getInset(i)->lyxCode() != ERT_CODE
2695 && getInset(i)->producesOutput();
2697 bool closeLanguage = false;
2698 bool lang_switched_at_inset = false;
2699 if (fontswitch_inset) {
2700 // Some insets cannot be inside a font change command.
2701 // However, even such insets *can* be placed in \L or \R
2702 // or their equivalents (for RTL language switches),
2703 // so we don't close the language in those cases
2704 // (= differing isRightToLeft()).
2705 // ArabTeX, though, doesn't seem to handle this special behavior.
2706 closeLanguage = basefont.isRightToLeft() == current_font.isRightToLeft()
2707 || basefont.language()->lang() == "arabic_arabtex"
2708 || current_font.language()->lang() == "arabic_arabtex";
2709 // We need to check prev_font as language changes directly at inset
2710 // will only be started inside the inset.
2711 lang_switched_at_inset = prev_font.language() != current_font.language();
2714 // Do we need to close the previous font?
2715 bool langClosed = false;
2717 ((current_font != running_font
2718 || current_font.language() != running_font.language())
2719 || (fontswitch_inset
2720 && (current_font == prev_font))))
2722 // ensure there is no open script-wrapper
2723 if (!alien_script.empty()) {
2726 alien_script.clear();
2728 if (in_ct_deletion) {
2729 // We have to close and then reopen \lyxdeleted,
2730 // as strikeout needs to be on lowest level.
2735 // Force language closing
2736 current_font.setLanguage(basefont.language());
2737 Font const nextfont = (i == body_pos-1) ? basefont : current_font;
2738 bool needPar = false;
2739 column += running_font.latexWriteEndChanges(
2740 os, bparams, runparams, basefont,
2742 if (in_ct_deletion) {
2743 // We have to close and then reopen \lyxdeleted,
2744 // as strikeout needs to be on lowest level.
2745 OutputParams rp = runparams;
2746 column += Changes::latexMarkChange(os, bparams,
2747 Change(Change::UNCHANGED), Change(Change::DELETED), rp);
2750 // Has the language been closed in the latexWriteEndChanges() call above?
2751 langClosed = running_font.language() != basefont.language()
2752 && running_font.language() != nextfont.language()
2753 && (running_font.language()->encoding()->package() != Encoding::CJK);
2754 running_font = basefont;
2757 // if necessary, close language environment before opening CJK
2758 string const running_lang = running_font.language()->babel();
2759 string const lang_end_command = lyxrc.language_command_end;
2760 if (!lang_end_command.empty() && !bparams.useNonTeXFonts
2761 && !running_lang.empty()
2762 && running_lang == openLanguageName()
2763 && current_font.language()->encoding()->package() == Encoding::CJK) {
2764 string end_tag = subst(lang_end_command, "$$lang", running_lang);
2765 os << from_ascii(end_tag);
2766 column += end_tag.length();
2770 // Switch file encoding if necessary (and allowed)
2771 if ((!fontswitch_inset || closeLanguage)
2772 && !runparams.pass_thru && !style.pass_thru &&
2773 runparams.encoding->package() != Encoding::none &&
2774 current_font.language()->encoding()->package() != Encoding::none) {
2775 pair<bool, int> const enc_switch =
2776 switchEncoding(os.os(), bparams, runparams,
2777 *(current_font.language()->encoding()));
2778 if (enc_switch.first) {
2779 column += enc_switch.second;
2780 runparams.encoding = current_font.language()->encoding();
2784 // A display math inset inside an ulem command will be output
2785 // as a box of width \linewidth, so we have to either disable
2786 // indentation if the inset starts a paragraph, or start a new
2787 // line to accommodate such box. This has to be done before
2788 // writing any font changing commands.
2789 if (runparams.inDisplayMath && !deleted_display_math
2790 && runparams.inulemcmd) {
2791 if (os.afterParbreak())
2797 // Do we need to change font?
2798 if ((current_font != running_font ||
2799 current_font.language() != running_font.language())
2800 && i != body_pos - 1)
2802 if (!fontswitch_inset) {
2803 if (in_ct_deletion) {
2804 // We have to close and then reopen \lyxdeleted,
2805 // as strikeout needs to be on lowest level.
2806 OutputParams rp = runparams;
2807 bool needPar = false;
2808 column += running_font.latexWriteEndChanges(
2809 os, bparams, rp, basefont,
2814 otexstringstream ots;
2815 InsetText const * textinset = inInset().asInsetText();
2816 bool const cprotect = textinset
2817 ? textinset->hasCProtectContent(runparams.moving_arg)
2818 && !textinset->text().isMainText()
2820 column += current_font.latexWriteStartChanges(ots, bparams,
2821 runparams, basefont, last_font, false,
2823 // Check again for display math in ulem commands as a
2824 // font change may also occur just before a math inset.
2825 if (runparams.inDisplayMath && !deleted_display_math
2826 && runparams.inulemcmd) {
2827 if (os.afterParbreak())
2832 running_font = current_font;
2834 docstring fontchange = ots.str();
2836 // check whether the fontchange ends with a \\textcolor
2837 // modifier and the text starts with a space. If so we
2838 // need to add } in order to prevent \\textcolor from gobbling
2839 // the space (bug 4473).
2840 docstring const last_modifier = rsplit(fontchange, '\\');
2841 if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
2842 os << from_ascii("{}");
2843 else if (ots.terminateCommand())
2845 if (in_ct_deletion) {
2846 // We have to close and then reopen \lyxdeleted,
2847 // as strikeout needs to be on lowest level.
2848 OutputParams rp = runparams;
2849 column += Changes::latexMarkChange(os, bparams,
2850 Change(Change::UNCHANGED), change, rp);
2853 running_font = current_font;
2854 open_font = !langClosed;
2858 // FIXME: think about end_pos implementation...
2859 if (c == ' ' && i >= start_pos && (end_pos == -1 || i < end_pos)) {
2860 // FIXME: integrate this case in latexSpecialChar
2861 // Do not print the separation of the optional argument
2862 // if style.pass_thru is false. This works because
2863 // latexSpecialChar ignores spaces if
2864 // style.pass_thru is false.
2865 if (i != body_pos - 1) {
2866 if (d->simpleTeXBlanks(bparams, runparams, os,
2867 i, column, current_font, style)) {
2868 // A surrogate pair was output. We
2869 // must not call latexSpecialChar
2870 // in this iteration, since it would output
2871 // the combining character again.
2878 OutputParams rp = runparams;
2879 rp.free_spacing = style.free_spacing;
2880 rp.local_font = ¤t_font;
2881 rp.intitle = style.intitle;
2883 // Two major modes: LaTeX or plain
2884 // Handle here those cases common to both modes
2885 // and then split to handle the two modes separately.
2886 if (c == META_INSET) {
2887 if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2888 // Greyedout notes and, in general, all insets
2889 // with InsetLayout::isDisplay() == false,
2890 // are typeset inline with the text. So, we
2891 // can add a \par to the last paragraph of
2892 // such insets only if nothing else follows.
2893 bool incremented = false;
2894 Inset const * inset = getInset(i);
2895 InsetText const * textinset = inset
2896 ? inset->asInsetText()
2898 if (i + 1 == size() && textinset
2899 && !inset->getLayout().isDisplay()) {
2900 ParagraphList const & pars =
2901 textinset->text().paragraphs();
2902 pit_type const pit = pars.size() - 1;
2903 Font const lastfont =
2904 pit < 0 || pars[pit].empty()
2905 ? pars[pit].getLayoutFont(
2908 : pars[pit].getFont(bparams,
2909 pars[pit].size() - 1,
2911 if (lastfont.fontInfo().size() !=
2912 basefont.fontInfo().size()) {
2917 // We need to restore parts of this after insets with
2918 // allowMultiPar() true
2919 Font const save_basefont = basefont;
2920 d->latexInset(bparams, os, rp, running_font,
2921 basefont, real_outerfont, open_font,
2922 runningChange, style, i, column, fontswitch_inset,
2923 closeLanguage, lang_switched_at_inset);
2924 if (fontswitch_inset) {
2926 bool needPar = false;
2927 column += running_font.latexWriteEndChanges(
2928 os, bparams, runparams,
2929 basefont, basefont, needPar);
2932 basefont.fontInfo().setSize(save_basefont.fontInfo().size());
2933 basefont.fontInfo().setFamily(save_basefont.fontInfo().family());
2934 basefont.fontInfo().setSeries(save_basefont.fontInfo().series());
2939 if (runparams.ctObject == CtObject::DisplayObject
2940 || runparams.ctObject == CtObject::UDisplayObject) {
2941 // Close \lyx*deleted and force its
2942 // reopening (if needed)
2945 runningChange = Change(Change::UNCHANGED);
2946 runparams.ctObject = CtObject::Normal;
2949 } else if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
2950 if (!bparams.useNonTeXFonts)
2951 script = Encodings::isKnownScriptChar(c);
2952 if (script != alien_script) {
2953 if (!alien_script.empty()) {
2955 alien_script.clear();
2957 string fontenc = running_font.language()->fontenc(bparams);
2959 && !Encodings::fontencSupportsScript(fontenc, script)) {
2960 column += script.length() + 2;
2961 os << "\\" << script << "{";
2962 alien_script = script;
2966 d->latexSpecialChar(os, bparams, rp, running_font,
2967 alien_script, style, i, end_pos, column);
2968 } catch (EncodingException & e) {
2969 if (runparams.dryrun) {
2970 os << "<" << _("LyX Warning: ")
2971 << _("uncodable character") << " '";
2975 // add location information and throw again.
2983 // Set the encoding to that returned from latexSpecialChar (see
2984 // comment for encoding member in OutputParams.h)
2985 runparams.encoding = rp.encoding;
2987 // Also carry on the info on a closed ulem command for insets
2988 // such as Note that do not produce any output, so that no
2989 // command is ever executed but its opening was recorded.
2990 runparams.inulemcmd = rp.inulemcmd;
2992 // These need to be passed upstream as well
2993 runparams.need_maketitle = rp.need_maketitle;
2994 runparams.have_maketitle = rp.have_maketitle;
2996 // And finally, pass the post_macros upstream
2997 runparams.post_macro = rp.post_macro;
3000 // Close wrapper for alien script
3001 if (!alien_script.empty()) {
3003 alien_script.clear();
3006 Font const font = empty()
3007 ? getLayoutFont(bparams, real_outerfont)
3008 : getFont(bparams, size() - 1, real_outerfont);
3010 InsetText const * textinset = inInset().asInsetText();
3012 bool const maintext = textinset
3013 ? textinset->text().isMainText()
3016 size_t const numpars = textinset
3017 ? textinset->text().paragraphs().size()
3020 bool needPar = false;
3022 if (style.resfont.size() != font.fontInfo().size()
3023 && (!runparams.isLastPar || maintext
3024 || (numpars > 1 && d->ownerCode() != CELL_CODE
3025 && (inInset().getLayout().isDisplay()
3027 && !style.isCommand()) {
3031 // If we have an open font definition, we have to close it
3033 // Make sure that \\par is done with the font of the last
3034 // character if this has another size as the default.
3035 // This is necessary because LaTeX (and LyX on the screen)
3036 // calculates the space between the baselines according
3037 // to this font. (Matthias)
3039 // We must not change the font for the last paragraph
3040 // of non-multipar insets, tabular cells or commands,
3041 // since this produces unwanted whitespace.
3042 #ifdef FIXED_LANGUAGE_END_DETECTION
3044 running_font.latexWriteEndChanges(os, bparams,
3045 runparams, basefont,
3046 next_->getFont(bparams, 0, outerfont),
3049 running_font.latexWriteEndChanges(os, bparams,
3050 runparams, basefont, basefont, needPar);
3053 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
3054 //FIXME: there as we start another \selectlanguage with the next paragraph if
3055 //FIXME: we are in need of this. This should be fixed sometime (Jug)
3056 running_font.latexWriteEndChanges(os, bparams, runparams,
3057 basefont, basefont, needPar);
3061 // The \par could not be inserted at the same nesting
3062 // level of the font size change, so do it now.
3063 os << "{\\" << font.latexSize() << "\\par}";
3066 if (!runparams.inDeletedInset || inInset().canTrackChanges())
3067 column += Changes::latexMarkChange(os, bparams, runningChange,
3068 Change(Change::UNCHANGED), runparams);
3070 // Needed if there is an optional argument but no contents.
3071 if (body_pos > 0 && body_pos == size()) {
3075 if (!style.rightdelim().empty()) {
3076 os << style.rightdelim();
3077 column += style.rightdelim().size();
3080 if (allowcust && d->endTeXParParams(bparams, os, runparams)
3081 && runparams.encoding != prev_encoding) {
3082 runparams.encoding = prev_encoding;
3083 os << setEncoding(prev_encoding->iconvName());
3086 LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
3090 bool Paragraph::emptyTag() const
3092 for (pos_type i = 0; i < size(); ++i) {
3093 if (Inset const * inset = getInset(i)) {
3094 InsetCode lyx_code = inset->lyxCode();
3095 // FIXME testing like that is wrong. What is
3097 if (lyx_code != TOC_CODE &&
3098 lyx_code != INCLUDE_CODE &&
3099 lyx_code != GRAPHICS_CODE &&
3100 lyx_code != ERT_CODE &&
3101 lyx_code != LISTINGS_CODE &&
3102 lyx_code != FLOAT_CODE &&
3103 lyx_code != TABULAR_CODE) {
3107 char_type c = d->text_[i];
3108 if (c != ' ' && c != '\t')
3116 string Paragraph::getID(Buffer const &, OutputParams const &)
3119 for (pos_type i = 0; i < size(); ++i) {
3120 if (Inset const * inset = getInset(i)) {
3121 InsetCode lyx_code = inset->lyxCode();
3122 if (lyx_code == LABEL_CODE) {
3123 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
3124 docstring const & id = il->getParam("name");
3125 return "id='" + to_utf8(xml::cleanID(id)) + "'";
3133 pos_type Paragraph::firstWordDocBook(XMLStream & xs, OutputParams const & runparams) const
3136 for (i = 0; i < size(); ++i) {
3137 if (Inset const * inset = getInset(i)) {
3138 inset->docbook(xs, runparams);
3140 char_type c = d->text_[i];
3150 pos_type Paragraph::firstWordLyXHTML(XMLStream & xs, OutputParams const & runparams)
3154 for (i = 0; i < size(); ++i) {
3155 if (Inset const * inset = getInset(i)) {
3156 inset->xhtml(xs, runparams);
3158 char_type c = d->text_[i];
3168 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
3171 pos_type size = text_.size();
3172 for (pos_type i = initial; i < size; ++i) {
3173 Font font = owner_->getFont(buf.params(), i, outerfont);
3174 if (text_[i] == META_INSET)
3176 if (i != initial && font != font_old)
3187 void doFontSwitchDocBook(vector<xml::FontTag> & tagsToOpen,
3188 vector<xml::EndFontTag> & tagsToClose,
3189 bool & flag, FontState curstate, xml::FontTypes type)
3191 if (curstate == FONT_ON) {
3192 tagsToOpen.push_back(docbookStartFontTag(type));
3195 tagsToClose.push_back(docbookEndFontTag(type));
3200 class OptionalFontType {
3205 OptionalFontType(): ft(xml::FT_EMPH), has_value(false) {} // A possible value at random for ft.
3206 OptionalFontType(xml::FontTypes ft): ft(ft), has_value(true) {}
3209 OptionalFontType fontShapeToXml(FontShape fs)
3213 return {xml::FT_ITALIC};
3215 return {xml::FT_SLANTED};
3216 case SMALLCAPS_SHAPE:
3217 return {xml::FT_SMALLCAPS};
3222 // the other tags are for internal use
3228 OptionalFontType fontFamilyToXml(FontFamily fm)
3232 return {xml::FT_ROMAN};
3234 return {xml::FT_SANS};
3235 case TYPEWRITER_FAMILY:
3236 return {xml::FT_TYPE};
3237 case INHERIT_FAMILY:
3240 // the other tags are for internal use
3246 OptionalFontType fontSizeToXml(FontSize fs)
3250 return {xml::FT_SIZE_TINY};
3252 return {xml::FT_SIZE_SCRIPT};
3254 return {xml::FT_SIZE_FOOTNOTE};
3256 return {xml::FT_SIZE_SMALL};
3258 return {xml::FT_SIZE_LARGE};
3260 return {xml::FT_SIZE_LARGER};
3262 return {xml::FT_SIZE_LARGEST};
3264 return {xml::FT_SIZE_HUGE};
3266 return {xml::FT_SIZE_HUGER};
3268 return {xml::FT_SIZE_INCREASE};
3270 return {xml::FT_SIZE_DECREASE};
3275 // the other tags are for internal use
3281 struct DocBookFontState
3283 FontShape curr_fs = INHERIT_SHAPE;
3284 FontFamily curr_fam = INHERIT_FAMILY;
3285 FontSize curr_size = INHERIT_SIZE;
3287 // track whether we have opened these tags
3288 bool emph_flag = false;
3289 bool bold_flag = false;
3290 bool noun_flag = false;
3291 bool ubar_flag = false;
3292 bool dbar_flag = false;
3293 bool sout_flag = false;
3294 bool xout_flag = false;
3295 bool wave_flag = false;
3297 bool shap_flag = false;
3299 bool faml_flag = false;
3301 bool size_flag = false;
3304 std::tuple<vector<xml::FontTag>, vector<xml::EndFontTag>> computeDocBookFontSwitch(FontInfo const & font_old,
3306 std::string const & default_family,
3307 DocBookFontState & fs)
3309 vector<xml::FontTag> tagsToOpen;
3310 vector<xml::EndFontTag> tagsToClose;
3313 FontState curstate = font.fontInfo().emph();
3314 if (font_old.emph() != curstate)
3315 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.emph_flag, curstate, xml::FT_EMPH);
3318 curstate = font.fontInfo().noun();
3319 if (font_old.noun() != curstate)
3320 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.noun_flag, curstate, xml::FT_NOUN);
3323 curstate = font.fontInfo().underbar();
3324 if (font_old.underbar() != curstate)
3325 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.ubar_flag, curstate, xml::FT_UBAR);
3328 curstate = font.fontInfo().strikeout();
3329 if (font_old.strikeout() != curstate)
3330 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.sout_flag, curstate, xml::FT_SOUT);
3333 curstate = font.fontInfo().xout();
3334 if (font_old.xout() != curstate)
3335 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.xout_flag, curstate, xml::FT_XOUT);
3338 curstate = font.fontInfo().uuline();
3339 if (font_old.uuline() != curstate)
3340 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.dbar_flag, curstate, xml::FT_DBAR);
3343 curstate = font.fontInfo().uwave();
3344 if (font_old.uwave() != curstate)
3345 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.wave_flag, curstate, xml::FT_WAVE);
3348 // a little hackish, but allows us to reuse what we have.
3349 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
3350 if (font_old.series() != font.fontInfo().series())
3351 doFontSwitchDocBook(tagsToOpen, tagsToClose, fs.bold_flag, curstate, xml::FT_BOLD);
3354 fs.curr_fs = font.fontInfo().shape();
3355 FontShape old_fs = font_old.shape();
3356 if (old_fs != fs.curr_fs) {
3358 OptionalFontType tag = fontShapeToXml(old_fs);
3360 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3361 fs.shap_flag = false;
3364 OptionalFontType tag = fontShapeToXml(fs.curr_fs);
3366 tagsToOpen.push_back(docbookStartFontTag(tag.ft));
3370 fs.curr_fam = font.fontInfo().family();
3371 FontFamily old_fam = font_old.family();
3372 if (old_fam != fs.curr_fam) {
3374 OptionalFontType tag = fontFamilyToXml(old_fam);
3376 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3377 fs.faml_flag = false;
3379 switch (fs.curr_fam) {
3381 // we will treat a "default" font family as roman, since we have
3382 // no other idea what to do.
3383 if (default_family != "rmdefault" && default_family != "default") {
3384 tagsToOpen.push_back(docbookStartFontTag(xml::FT_ROMAN));
3385 fs.faml_flag = true;
3389 if (default_family != "sfdefault") {
3390 tagsToOpen.push_back(docbookStartFontTag(xml::FT_SANS));
3391 fs.faml_flag = true;
3394 case TYPEWRITER_FAMILY:
3395 if (default_family != "ttdefault") {
3396 tagsToOpen.push_back(docbookStartFontTag(xml::FT_TYPE));
3397 fs.faml_flag = true;
3400 case INHERIT_FAMILY:
3403 // the other tags are for internal use
3410 fs.curr_size = font.fontInfo().size();
3411 FontSize old_size = font_old.size();
3412 if (old_size != fs.curr_size) {
3414 OptionalFontType tag = fontSizeToXml(old_size);
3416 tagsToClose.push_back(docbookEndFontTag(tag.ft));
3417 fs.size_flag = false;
3420 OptionalFontType tag = fontSizeToXml(fs.curr_size);
3421 if (tag.has_value) {
3422 tagsToOpen.push_back(docbookStartFontTag(tag.ft));
3423 fs.size_flag = true;
3427 return std::tuple<vector<xml::FontTag>, vector<xml::EndFontTag>>(tagsToOpen, tagsToClose);
3430 } // anonymous namespace
3433 std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring>>
3434 Paragraph::simpleDocBookOnePar(Buffer const & buf,
3435 OutputParams const & runparams,
3436 Font const & outerfont,
3439 bool ignore_fonts) const
3441 std::vector<docstring> prependedParagraphs;
3442 std::vector<docstring> generatedParagraphs;
3443 std::vector<docstring> appendedParagraphs;
3444 odocstringstream os;
3446 // If there is an argument that must be output before the main tag, do it before handling the rest of the paragraph.
3447 // Also tag all arguments that shouldn't go in the main content right now, so that they are never generated at the
3449 OutputParams rp = runparams;
3450 for (pos_type i = initial; i < size(); ++i) {
3451 if (getInset(i) && getInset(i)->lyxCode() == ARG_CODE) {
3452 const InsetArgument * arg = getInset(i)->asInsetArgument();
3453 if (arg->docbookargumentbeforemaintag()) {
3454 auto xs_local = XMLStream(os);
3455 arg->docbook(xs_local, rp);
3457 prependedParagraphs.push_back(os.str());
3458 os.str(from_ascii(""));
3460 rp.docbook_prepended_arguments.insert(arg);
3461 } else if (arg->docbookargumentaftermaintag()) {
3462 rp.docbook_appended_arguments.insert(arg);
3467 // State variables for the main loop.
3468 auto xs = new XMLStream(os); // XMLStream has no copy constructor: to create a new object, the only solution
3469 // is to hold a pointer to the XMLStream (xs = XMLStream(os) is not allowed once the first object is built).
3470 std::vector<char_type> delayedChars; // When a font tag ends with a space, output it after the closing font tag.
3471 // This requires to store delayed characters at some point.
3473 DocBookFontState fs; // Track whether we have opened font tags
3474 DocBookFontState old_fs = fs;
3476 Layout const & style = *d->layout_;
3477 FontInfo font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3478 string const default_family = buf.masterBuffer()->params().fonts_default_family;
3480 vector<xml::FontTag> tagsToOpen;
3481 vector<xml::EndFontTag> tagsToClose;
3483 // Parsing main loop.
3484 for (pos_type i = initial; i < size(); ++i) {
3485 bool ignore_fonts_i = ignore_fonts
3486 || style.docbooknofontinside()
3487 || (getInset(i) && getInset(i)->getLayout().docbooknofontinside());
3489 // Don't show deleted material in the output.
3493 // If this is an InsetNewline, generate a new paragraph. Also reset the fonts, so that tags are closed in
3495 if (getInset(i) && getInset(i)->lyxCode() == NEWLINE_CODE) {
3496 if (!ignore_fonts_i)
3497 xs->closeFontTags();
3499 // Output one paragraph (i.e. one string entry in generatedParagraphs).
3500 generatedParagraphs.push_back(os.str());
3502 // Create a new XMLStream for the new paragraph, completely independent from the previous one. This implies
3503 // that the string stream must be reset.
3504 os.str(from_ascii(""));
3506 xs = new XMLStream(os);
3508 // Restore the fonts for the new paragraph, so that the right tags are opened for the new entry.
3509 if (!ignore_fonts_i) {
3510 font_old = outerfont.fontInfo();
3515 // Determine which tags should be opened or closed regarding fonts.
3516 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
3517 tie(tagsToOpen, tagsToClose) = computeDocBookFontSwitch(font_old, font, default_family, fs);
3519 if (!ignore_fonts_i) {
3520 vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
3521 vector<xml::EndFontTag>::const_iterator cen = tagsToClose.end();
3522 for (; cit != cen; ++cit)
3526 // Deal with the delayed characters *after* closing font tags.
3527 if (!delayedChars.empty()) {
3528 for (char_type c: delayedChars)
3530 delayedChars.clear();
3533 if (!ignore_fonts_i) {
3534 vector<xml::FontTag>::const_iterator sit = tagsToOpen.begin();
3535 vector<xml::FontTag>::const_iterator sen = tagsToOpen.end();
3536 for (; sit != sen; ++sit)
3539 tagsToClose.clear();
3543 // Finally, write the next character or inset.
3544 if (Inset const * inset = getInset(i)) {
3545 bool inset_is_argument_elsewhere = getInset(i)->asInsetArgument() &&
3546 rp.docbook_appended_arguments.find(inset->asInsetArgument()) != rp.docbook_appended_arguments.end() &&
3547 rp.docbook_prepended_arguments.find(inset->asInsetArgument()) != rp.docbook_prepended_arguments.end();
3549 if ((!rp.for_toc || inset->isInToc()) && !inset_is_argument_elsewhere) {
3550 // Arguments may need to be output
3551 OutputParams np = rp;
3552 np.local_font = &font;
3554 // TODO: special case will bite here.
3555 np.docbook_in_par = true;
3556 inset->docbook(*xs, np);
3559 char_type c = getUChar(buf.masterBuffer()->params(), rp, i);
3560 if (lyx::isSpace(c) && !ignore_fonts)
3561 delayedChars.push_back(c);
3565 font_old = font.fontInfo();
3568 // FIXME, this code is just imported from XHTML
3569 // I'm worried about what happens if a branch, say, is itself
3570 // wrapped in some font stuff. I think that will not work.
3572 xs->closeFontTags();
3574 // Deal with the delayed characters *after* closing font tags.
3575 if (!delayedChars.empty())
3576 for (char_type c: delayedChars)
3579 // In listings, new lines (i.e. \n characters in the output) are very important. Avoid generating one for the
3580 // last line to get a clean output.
3581 if (rp.docbook_in_listing && !is_last_par)
3584 // Finalise the last (and most likely only) paragraph.
3585 generatedParagraphs.push_back(os.str());
3586 os.str(from_ascii(""));
3589 // If there is an argument that must be output after the main tag, do it after handling the rest of the paragraph.
3590 for (pos_type i = initial; i < size(); ++i) {
3591 if (getInset(i) && getInset(i)->lyxCode() == ARG_CODE) {
3592 const InsetArgument * arg = getInset(i)->asInsetArgument();
3593 if (arg->docbookargumentaftermaintag()) {
3594 // Don't use rp, as this argument would not generate anything.
3595 auto xs_local = XMLStream(os);
3596 arg->docbook(xs_local, runparams);
3598 appendedParagraphs.push_back(os.str());
3599 os.str(from_ascii(""));
3604 return std::make_tuple(prependedParagraphs, generatedParagraphs, appendedParagraphs);
3610 void doFontSwitchXHTML(vector<xml::FontTag> & tagsToOpen,
3611 vector<xml::EndFontTag> & tagsToClose,
3612 bool & flag, FontState curstate, xml::FontTypes type)
3614 if (curstate == FONT_ON) {
3615 tagsToOpen.push_back(xhtmlStartFontTag(type));
3618 tagsToClose.push_back(xhtmlEndFontTag(type));
3623 } // anonymous namespace
3626 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
3628 OutputParams const & runparams,
3629 Font const & outerfont,
3630 bool start_paragraph, bool close_paragraph,
3631 pos_type initial) const
3635 // track whether we have opened these tags
3636 bool emph_flag = false;
3637 bool bold_flag = false;
3638 bool noun_flag = false;
3639 bool ubar_flag = false;
3640 bool dbar_flag = false;
3641 bool sout_flag = false;
3642 bool xout_flag = false;
3643 bool wave_flag = false;
3645 bool shap_flag = false;
3647 bool faml_flag = false;
3649 bool size_flag = false;
3651 Layout const & style = *d->layout_;
3653 if (start_paragraph)
3654 xs.startDivision(allowEmpty());
3657 style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3659 FontShape curr_fs = INHERIT_SHAPE;
3660 FontFamily curr_fam = INHERIT_FAMILY;
3661 FontSize curr_size = INHERIT_SIZE;
3663 string const default_family =
3664 buf.masterBuffer()->params().fonts_default_family;
3666 vector<xml::FontTag> tagsToOpen;
3667 vector<xml::EndFontTag> tagsToClose;
3669 // parsing main loop
3670 for (pos_type i = initial; i < size(); ++i) {
3671 // let's not show deleted material in the output
3675 Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
3678 FontState curstate = font.fontInfo().emph();
3679 if (font_old.emph() != curstate)
3680 doFontSwitchXHTML(tagsToOpen, tagsToClose, emph_flag, curstate, xml::FT_EMPH);
3683 curstate = font.fontInfo().noun();
3684 if (font_old.noun() != curstate)
3685 doFontSwitchXHTML(tagsToOpen, tagsToClose, noun_flag, curstate, xml::FT_NOUN);
3688 curstate = font.fontInfo().underbar();
3689 if (font_old.underbar() != curstate)
3690 doFontSwitchXHTML(tagsToOpen, tagsToClose, ubar_flag, curstate, xml::FT_UBAR);
3693 curstate = font.fontInfo().strikeout();
3694 if (font_old.strikeout() != curstate)
3695 doFontSwitchXHTML(tagsToOpen, tagsToClose, sout_flag, curstate, xml::FT_SOUT);
3698 curstate = font.fontInfo().xout();
3699 if (font_old.xout() != curstate)
3700 doFontSwitchXHTML(tagsToOpen, tagsToClose, xout_flag, curstate, xml::FT_XOUT);
3703 curstate = font.fontInfo().uuline();
3704 if (font_old.uuline() != curstate)
3705 doFontSwitchXHTML(tagsToOpen, tagsToClose, dbar_flag, curstate, xml::FT_DBAR);
3708 curstate = font.fontInfo().uwave();
3709 if (font_old.uwave() != curstate)
3710 doFontSwitchXHTML(tagsToOpen, tagsToClose, wave_flag, curstate, xml::FT_WAVE);
3713 // a little hackish, but allows us to reuse what we have.
3714 curstate = (font.fontInfo().series() == BOLD_SERIES ? FONT_ON : FONT_OFF);
3715 if (font_old.series() != font.fontInfo().series())
3716 doFontSwitchXHTML(tagsToOpen, tagsToClose, bold_flag, curstate, xml::FT_BOLD);
3719 curr_fs = font.fontInfo().shape();
3720 FontShape old_fs = font_old.shape();
3721 if (old_fs != curr_fs) {
3725 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_ITALIC));
3728 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SLANTED));
3730 case SMALLCAPS_SHAPE:
3731 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SMALLCAPS));
3737 // the other tags are for internal use
3745 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_ITALIC));
3749 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SLANTED));
3752 case SMALLCAPS_SHAPE:
3753 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SMALLCAPS));
3760 // the other tags are for internal use
3767 curr_fam = font.fontInfo().family();
3768 FontFamily old_fam = font_old.family();
3769 if (old_fam != curr_fam) {
3773 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_ROMAN));
3776 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SANS));
3778 case TYPEWRITER_FAMILY:
3779 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_TYPE));
3781 case INHERIT_FAMILY:
3784 // the other tags are for internal use
3792 // we will treat a "default" font family as roman, since we have
3793 // no other idea what to do.
3794 if (default_family != "rmdefault" && default_family != "default") {
3795 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_ROMAN));
3800 if (default_family != "sfdefault") {
3801 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SANS));
3805 case TYPEWRITER_FAMILY:
3806 if (default_family != "ttdefault") {
3807 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_TYPE));
3811 case INHERIT_FAMILY:
3814 // the other tags are for internal use
3821 curr_size = font.fontInfo().size();
3822 FontSize old_size = font_old.size();
3823 if (old_size != curr_size) {
3827 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_TINY));
3830 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_SCRIPT));
3833 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_FOOTNOTE));
3836 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_SMALL));
3839 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGE));
3842 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGER));
3845 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_LARGEST));
3848 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_HUGE));
3851 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_HUGER));
3854 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_INCREASE));
3857 tagsToClose.emplace_back(xhtmlEndFontTag(xml::FT_SIZE_DECREASE));
3863 // the other tags are for internal use
3869 switch (curr_size) {
3871 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_TINY));
3875 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_SCRIPT));
3879 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_FOOTNOTE));
3883 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_SMALL));
3887 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGE));
3891 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGER));
3895 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_LARGEST));
3899 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_HUGE));
3903 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_HUGER));
3907 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_INCREASE));
3911 tagsToOpen.emplace_back(xhtmlStartFontTag(xml::FT_SIZE_DECREASE));
3918 // the other tags are for internal use
3925 // Other such tags? What about the other text ranges?
3927 vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
3928 vector<xml::EndFontTag>::const_iterator cen = tagsToClose.end();
3929 for (; cit != cen; ++cit)
3932 vector<xml::FontTag>::const_iterator sit = tagsToOpen.begin();
3933 vector<xml::FontTag>::const_iterator sen = tagsToOpen.end();
3934 for (; sit != sen; ++sit)
3937 tagsToClose.clear();
3940 Inset const * inset = getInset(i);
3942 if (!runparams.for_toc || inset->isInToc()) {
3943 OutputParams np = runparams;
3944 np.local_font = &font;
3945 // If the paragraph has size 1, then we are in the "special
3946 // case" where we do not output the containing paragraph info
3947 if (!inset->getLayout().htmlisblock() && size() != 1)
3948 np.html_in_par = true;
3949 retval += inset->xhtml(xs, np);
3952 char_type c = getUChar(buf.masterBuffer()->params(),
3954 if (c == ' ' && (style.free_spacing || runparams.free_spacing))
3955 xs << XMLStream::ESCAPE_NONE << " ";
3959 font_old = font.fontInfo();
3963 // I'm worried about what happens if a branch, say, is itself
3964 // wrapped in some font stuff. I think that will not work.
3966 if (close_paragraph)
3973 bool Paragraph::isHfill(pos_type pos) const
3975 Inset const * inset = getInset(pos);
3976 return inset && inset->isHfill();
3980 bool Paragraph::isNewline(pos_type pos) const
3982 // U+2028 LINE SEPARATOR
3983 // U+2029 PARAGRAPH SEPARATOR
3984 char_type const c = d->text_[pos];
3985 if (c == 0x2028 || c == 0x2029)
3987 Inset const * inset = getInset(pos);
3988 return inset && inset->lyxCode() == NEWLINE_CODE;
3992 bool Paragraph::isEnvSeparator(pos_type pos) const
3994 Inset const * inset = getInset(pos);
3995 return inset && inset->lyxCode() == SEPARATOR_CODE;
3999 bool Paragraph::isLineSeparator(pos_type pos) const
4001 char_type const c = d->text_[pos];
4002 if (isLineSeparatorChar(c))
4004 Inset const * inset = getInset(pos);
4005 return inset && inset->isLineSeparator();
4009 bool Paragraph::isWordSeparator(pos_type pos, bool const ignore_deleted) const
4013 if (ignore_deleted && isDeleted(pos))
4015 if (Inset const * inset = getInset(pos))
4016 return !inset->isLetter();
4017 // if we have a hard hyphen (no en- or emdash) or apostrophe
4018 // we pass this to the spell checker
4019 // FIXME: this method is subject to change, visit
4020 // https://bugzilla.mozilla.org/show_bug.cgi?id=355178
4021 // to get an impression how complex this is.
4022 if (isHardHyphenOrApostrophe(pos))
4024 char_type const c = d->text_[pos];
4025 // We want to pass the escape chars to the spellchecker
4026 docstring const escape_chars = from_utf8(lyxrc.spellchecker_esc_chars);
4027 return !isLetterChar(c) && !isDigitASCII(c) && !contains(escape_chars, c);
4031 bool Paragraph::isHardHyphenOrApostrophe(pos_type pos) const
4033 pos_type const psize = size();
4036 char_type const c = d->text_[pos];
4037 if (c != '-' && c != '\'')
4039 pos_type nextpos = pos + 1;
4040 pos_type prevpos = pos > 0 ? pos - 1 : 0;
4041 if ((nextpos == psize || isSpace(nextpos))
4042 && (pos == 0 || isSpace(prevpos)))
4048 bool Paragraph::needsCProtection(bool const fragile) const
4050 // first check the layout of the paragraph, but only in insets
4051 InsetText const * textinset = inInset().asInsetText();
4052 bool const maintext = textinset
4053 ? textinset->text().isMainText()
4056 if (!maintext && layout().needcprotect) {
4057 // Environments need cprotection regardless the content
4058 if (layout().latextype == LATEX_ENVIRONMENT)
4061 // Commands need cprotection if they contain specific chars
4062 int const nchars_escape = 9;
4063 static char_type const chars_escape[nchars_escape] = {
4064 '&', '_', '$', '%', '#', '^', '{', '}', '\\'};
4066 docstring const pars = asString();
4067 for (int k = 0; k < nchars_escape; k++) {
4068 if (contains(pars, chars_escape[k]))
4073 // now check whether we have insets that need cprotection
4074 pos_type size = pos_type(d->text_.size());
4075 for (pos_type i = 0; i < size; ++i) {
4078 Inset const * ins = getInset(i);
4079 if (ins->needsCProtection(maintext, fragile))
4081 // Now check math environments
4082 InsetMath const * im = getInset(i)->asInsetMath();
4083 if (!im || im->cell(0).empty())
4085 switch(im->cell(0)[0]->lyxCode()) {
4086 case MATH_AMSARRAY_CODE:
4087 case MATH_SUBSTACK_CODE:
4089 case MATH_XYMATRIX_CODE:
4090 // these need cprotection
4101 FontSpan const & Paragraph::getSpellRange(pos_type pos) const
4103 return d->speller_state_.getRange(pos);
4107 bool Paragraph::isChar(pos_type pos) const
4109 if (Inset const * inset = getInset(pos))
4110 return inset->isChar();
4111 char_type const c = d->text_[pos];
4112 return !isLetterChar(c) && !isDigitASCII(c) && !lyx::isSpace(c);
4116 bool Paragraph::isSpace(pos_type pos) const
4118 if (Inset const * inset = getInset(pos))
4119 return inset->isSpace();
4120 char_type const c = d->text_[pos];
4121 return lyx::isSpace(c);
4126 Paragraph::getParLanguage(BufferParams const & bparams) const
4129 return getFirstFontSettings(bparams).language();
4130 // FIXME: we should check the prev par as well (Lgb)
4131 return bparams.language;
4135 bool Paragraph::isRTL(BufferParams const & bparams) const
4137 return getParLanguage(bparams)->rightToLeft()
4138 && !inInset().getLayout().forceLTR();
4142 void Paragraph::changeLanguage(BufferParams const & bparams,
4143 Language const * from, Language const * to)
4145 // change language including dummy font change at the end
4146 for (pos_type i = 0; i <= size(); ++i) {
4147 Font font = getFontSettings(bparams, i);
4148 if (font.language() == from) {
4149 font.setLanguage(to);
4151 d->requestSpellCheck(i);
4157 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
4159 Language const * doc_language = bparams.language;
4160 for (auto const & f : d->fontlist_)
4161 if (f.font().language() != ignore_language &&
4162 f.font().language() != latex_language &&
4163 f.font().language() != doc_language)
4169 void Paragraph::getLanguages(std::set<Language const *> & langs) const
4171 for (auto const & f : d->fontlist_) {
4172 Language const * lang = f.font().language();
4173 if (lang != ignore_language &&
4174 lang != latex_language)
4180 docstring Paragraph::asString(int options) const
4182 return asString(0, size(), options);
4186 docstring Paragraph::asString(pos_type beg, pos_type end, int options, const OutputParams *runparams) const
4188 odocstringstream os;
4191 && options & AS_STR_LABEL
4192 && !d->params_.labelString().empty())
4193 os << d->params_.labelString() << ' ';
4195 for (pos_type i = beg; i < end; ++i) {
4196 if ((options & AS_STR_SKIPDELETE) && isDeleted(i))
4198 char_type const c = d->text_[i];
4199 if (isPrintable(c) || c == '\t'
4200 || (c == '\n' && (options & AS_STR_NEWLINES)))
4202 else if (c == META_INSET && (options & AS_STR_INSETS)) {
4203 if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
4204 LASSERT(runparams != nullptr, return docstring());
4205 getInset(i)->plaintext(os, *runparams);
4206 } else if (c == META_INSET && (options & AS_STR_MATHED)
4207 && getInset(i)->lyxCode() == REF_CODE) {
4208 Buffer const & buf = getInset(i)->buffer();
4209 OutputParams rp(&buf.params().encoding());
4210 Font const font(inherit_font, buf.params().language);
4211 rp.local_font = &font;
4213 getInset(i)->latex(ots, rp);
4215 getInset(i)->toString(os);
4224 void Paragraph::forOutliner(docstring & os, size_t const maxlen,
4225 bool const shorten, bool const label) const
4227 size_t tmplen = shorten ? maxlen + 1 : maxlen;
4228 if (label && !labelString().empty())
4229 os += labelString() + ' ';
4230 if (!layout().isTocCaption())
4232 for (pos_type i = 0; i < size() && os.length() < tmplen; ++i) {
4235 char_type const c = d->text_[i];
4238 else if (c == META_INSET)
4239 getInset(i)->forOutliner(os, tmplen, false);
4242 Text::shortenForOutliner(os, maxlen);
4246 void Paragraph::setInsetOwner(Inset const * inset)
4248 d->inset_owner_ = inset;
4252 int Paragraph::id() const
4258 void Paragraph::setId(int id)
4264 Layout const & Paragraph::layout() const
4270 void Paragraph::setLayout(Layout const & layout)
4272 d->layout_ = &layout;
4276 void Paragraph::setDefaultLayout(DocumentClass const & tc)
4278 setLayout(tc.defaultLayout());
4282 void Paragraph::setPlainLayout(DocumentClass const & tc)
4284 setLayout(tc.plainLayout());
4288 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tc)
4290 if (usePlainLayout())
4293 setDefaultLayout(tc);
4297 Inset const & Paragraph::inInset() const
4299 LBUFERR(d->inset_owner_);
4300 return *d->inset_owner_;
4304 ParagraphParameters & Paragraph::params()
4310 ParagraphParameters const & Paragraph::params() const
4316 bool Paragraph::isFreeSpacing() const
4318 if (d->layout_->free_spacing)
4320 return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
4324 bool Paragraph::allowEmpty() const
4326 if (d->layout_->keepempty)
4328 return d->inset_owner_ && d->inset_owner_->allowEmpty();
4332 bool Paragraph::brokenBiblio() const
4334 // There is a problem if there is no bibitem at position 0 in
4335 // paragraphs that need one, if there is another bibitem in the
4336 // paragraph or if this paragraph is not supposed to have
4337 // a bibitem inset at all.
4338 return ((d->layout_->labeltype == LABEL_BIBLIO
4339 && (d->insetlist_.find(BIBITEM_CODE) != 0
4340 || d->insetlist_.find(BIBITEM_CODE, 1) > 0))
4341 || (d->layout_->labeltype != LABEL_BIBLIO
4342 && d->insetlist_.find(BIBITEM_CODE) != -1));
4346 int Paragraph::fixBiblio(Buffer const & buffer)
4348 // FIXME: when there was already an inset at 0, the return value is 1,
4349 // which does not tell whether another inset has been removed; the
4350 // cursor cannot be correctly updated.
4352 bool const track_changes = buffer.params().track_changes;
4353 int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
4355 // The case where paragraph is not BIBLIO
4356 if (d->layout_->labeltype != LABEL_BIBLIO) {
4357 if (bibitem_pos == -1)
4358 // No InsetBibitem => OK
4360 // There is an InsetBibitem: remove it!
4361 d->insetlist_.release(bibitem_pos);
4362 eraseChar(bibitem_pos, track_changes);
4363 return (bibitem_pos == 0) ? -1 : -bibitem_pos;
4366 bool const hasbibitem0 = bibitem_pos == 0;
4368 bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
4369 // There was an InsetBibitem at pos 0,
4370 // and no other one => OK
4371 if (bibitem_pos == -1)
4373 // there is a bibitem at the 0 position, but since
4374 // there is a second one, we copy the second on the
4375 // first. We're assuming there are at most two of
4376 // these, which there should be.
4377 // FIXME: why does it make sense to do that rather
4378 // than keep the first? (JMarc)
4379 Inset * inset = releaseInset(bibitem_pos);
4380 d->insetlist_.begin()->inset = inset;
4381 // This needs to be done to update the counter (#8499)
4382 buffer.updateBuffer();
4383 return -bibitem_pos;
4386 // We need to create an inset at the beginning
4387 Inset * inset = nullptr;
4388 if (bibitem_pos > 0) {
4389 // there was one somewhere in the paragraph, let's move it
4390 inset = d->insetlist_.release(bibitem_pos);
4391 eraseChar(bibitem_pos, track_changes);
4394 inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
4395 InsetCommandParams(BIBITEM_CODE));
4397 Font font(inherit_font, buffer.params().language);
4398 insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
4399 : Change::UNCHANGED));
4401 // This is needed to get the counters right
4402 buffer.updateBuffer();
4407 void Paragraph::checkAuthors(AuthorList const & authorList)
4409 d->changes_.checkAuthors(authorList);
4413 bool Paragraph::isChanged(pos_type pos) const
4415 return lookupChange(pos).changed();
4419 bool Paragraph::isInserted(pos_type pos) const
4421 return lookupChange(pos).inserted();
4425 bool Paragraph::isDeleted(pos_type pos) const
4427 return lookupChange(pos).deleted();
4431 InsetList const & Paragraph::insetList() const
4433 return d->insetlist_;
4437 void Paragraph::setInsetBuffers(Buffer & b)
4439 d->insetlist_.setBuffer(b);
4443 void Paragraph::resetBuffer()
4445 d->insetlist_.resetBuffer();
4449 Inset * Paragraph::releaseInset(pos_type pos)
4451 Inset * inset = d->insetlist_.release(pos);
4452 /// does not honour change tracking!
4453 eraseChar(pos, false);
4458 Inset * Paragraph::getInset(pos_type pos)
4460 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
4461 ? d->insetlist_.get(pos) : nullptr;
4465 Inset const * Paragraph::getInset(pos_type pos) const
4467 return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
4468 ? d->insetlist_.get(pos) : nullptr;
4472 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
4473 pos_type & right, TextCase action)
4475 // process sequences of modified characters; in change
4476 // tracking mode, this approach results in much better
4477 // usability than changing case on a char-by-char basis
4478 // We also need to track the current font, since font
4479 // changes within sequences can occur.
4480 vector<pair<char_type, Font> > changes;
4482 bool const trackChanges = bparams.track_changes;
4484 bool capitalize = true;
4486 for (; pos < right; ++pos) {
4487 char_type oldChar = d->text_[pos];
4488 char_type newChar = oldChar;
4490 // ignore insets and don't play with deleted text!
4491 if (oldChar != META_INSET && !isDeleted(pos)) {
4493 case text_lowercase:
4494 newChar = lowercase(oldChar);
4496 case text_capitalization:
4498 newChar = uppercase(oldChar);
4502 case text_uppercase:
4503 newChar = uppercase(oldChar);
4508 if (isWordSeparator(pos) || isDeleted(pos)) {
4509 // permit capitalization again
4513 if (oldChar != newChar) {
4514 changes.push_back(make_pair(newChar, getFontSettings(bparams, pos)));
4515 if (pos != right - 1)
4517 // step behind the changing area
4521 int erasePos = pos - changes.size();
4522 for (auto const & change : changes) {
4523 insertChar(pos, change.first, change.second, trackChanges);
4524 if (!eraseChar(erasePos, trackChanges)) {
4527 ++right; // expand selection
4535 int Paragraph::find(docstring const & str, bool cs, bool mw,
4536 pos_type start_pos, bool del) const
4538 pos_type pos = start_pos;
4539 int const strsize = str.length();
4541 pos_type const parsize = d->text_.size();
4542 for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
4543 // ignore deleted matter
4544 if (!del && isDeleted(pos)) {
4545 if (pos == parsize - 1)
4551 // Ignore "invisible" letters such as ligature breaks
4552 // and hyphenation chars while searching
4553 bool nonmatch = false;
4554 while (pos < parsize && isInset(pos)) {
4555 Inset const * inset = getInset(pos);
4556 if (!inset->isLetter() && !inset->isChar())
4558 odocstringstream os;
4559 inset->toString(os);
4560 docstring const insetstring = os.str();
4561 if (!insetstring.empty()) {
4562 int const insetstringsize = insetstring.length();
4563 for (int j = 0; j < insetstringsize && pos < parsize; ++i, ++j) {
4564 if ((cs && str[i] != insetstring[j])
4565 || (!cs && uppercase(str[i]) != uppercase(insetstring[j]))) {
4573 if (nonmatch || i == strsize)
4575 if (cs && str[i] != d->text_[pos])
4577 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
4584 // if necessary, check whether string matches word
4586 if (start_pos > 0 && !isWordSeparator(start_pos - 1))
4589 && !isWordSeparator(pos))
4593 return pos - start_pos;
4597 char_type Paragraph::getChar(pos_type pos) const
4599 return d->text_[pos];
4603 pos_type Paragraph::size() const
4605 return d->text_.size();
4609 bool Paragraph::empty() const
4611 return d->text_.empty();
4615 bool Paragraph::isInset(pos_type pos) const
4617 return d->text_[pos] == META_INSET;
4621 bool Paragraph::isSeparator(pos_type pos) const
4623 //FIXME: Are we sure this can be the only separator?
4624 return d->text_[pos] == ' ';
4628 void Paragraph::deregisterWords()
4630 Private::LangWordsMap::const_iterator itl = d->words_.begin();
4631 Private::LangWordsMap::const_iterator ite = d->words_.end();
4632 for (; itl != ite; ++itl) {
4633 WordList & wl = theWordList(itl->first);
4634 Private::Words::const_iterator it = (itl->second).begin();
4635 Private::Words::const_iterator et = (itl->second).end();
4636 for (; it != et; ++it)
4643 void Paragraph::locateWord(pos_type & from, pos_type & to,
4644 word_location const loc, bool const ignore_deleted) const
4647 case WHOLE_WORD_STRICT:
4648 if (from == 0 || from == size()
4649 || isWordSeparator(from, ignore_deleted)
4650 || isWordSeparator(from - 1, ignore_deleted)) {
4657 // If we are already at the beginning of a word, do nothing
4658 if (!from || isWordSeparator(from - 1, ignore_deleted))
4663 // always move the cursor to the beginning of previous word
4664 while (from && !isWordSeparator(from - 1, ignore_deleted))
4668 LYXERR0("Paragraph::locateWord: NEXT_WORD not implemented yet");
4671 // no need to move the 'from' cursor
4675 while (to < size() && !isWordSeparator(to, ignore_deleted))
4680 void Paragraph::collectWords()
4682 for (pos_type pos = 0; pos < size(); ++pos) {
4683 if (isWordSeparator(pos))
4685 pos_type from = pos;
4686 locateWord(from, pos, WHOLE_WORD);
4687 // Work around MSVC warning: The statement
4688 // if (pos < from + lyxrc.completion_minlength)
4689 // triggers a signed vs. unsigned warning.
4690 // I don't know why this happens, it could be a MSVC bug, or
4691 // related to LLP64 (windows) vs. LP64 (unix) programming
4692 // model, or the C++ standard might be ambigous in the section
4693 // defining the "usual arithmetic conversions". However, using
4694 // a temporary variable is safe and works on all compilers.
4695 pos_type const endpos = from + lyxrc.completion_minlength;
4698 FontList::const_iterator cit = d->fontlist_.fontIterator(from);
4699 if (cit == d->fontlist_.end())
4701 Language const * lang = cit->font().language();
4702 docstring const word = asString(from, pos, AS_STR_NONE);
4703 d->words_[lang->lang()].insert(word);
4708 void Paragraph::registerWords()
4710 Private::LangWordsMap::const_iterator itl = d->words_.begin();
4711 Private::LangWordsMap::const_iterator ite = d->words_.end();
4712 for (; itl != ite; ++itl) {
4713 WordList & wl = theWordList(itl->first);
4714 Private::Words::const_iterator it = (itl->second).begin();
4715 Private::Words::const_iterator et = (itl->second).end();
4716 for (; it != et; ++it)
4722 void Paragraph::updateWords()
4730 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
4732 SkipPositionsIterator begin = skips.begin();
4733 SkipPositions::iterator end = skips.end();
4734 if (pos > 0 && begin < end) {
4736 if (end->last == pos - 1) {
4741 skips.insert(end, FontSpan(pos, pos));
4745 Language * Paragraph::Private::locateSpellRange(
4746 pos_type & from, pos_type & to,
4747 SkipPositions & skips) const
4749 // skip leading white space
4750 while (from < to && owner_->isWordSeparator(from))
4752 // don't check empty range
4755 // get current language
4756 Language * lang = getSpellLanguage(from);
4757 pos_type last = from;
4758 bool samelang = true;
4759 bool sameinset = true;
4760 while (last < to && samelang && sameinset) {
4761 // hop to end of word
4762 while (last < to && !owner_->isWordSeparator(last)) {
4763 Inset const * inset = owner_->getInset(last);
4764 if (inset && dynamic_cast<const InsetSpecialChar *>(inset)) {
4765 // check for "invisible" letters such as ligature breaks
4766 odocstringstream os;
4767 inset->toString(os);
4768 if (os.str().length() != 0) {
4769 // avoid spell check of visible special char insets
4770 // stop the loop in front of the special char inset
4775 appendSkipPosition(skips, last);
4776 } else if (owner_->isDeleted(last)) {
4777 appendSkipPosition(skips, last);
4781 // hop to next word while checking for insets
4782 while (sameinset && last < to && owner_->isWordSeparator(last)) {
4783 if (Inset const * inset = owner_->getInset(last))
4784 sameinset = inset->isChar() && inset->isLetter();
4785 if (sameinset && owner_->isDeleted(last)) {
4786 appendSkipPosition(skips, last);
4791 if (sameinset && last < to) {
4792 // now check for language change
4793 samelang = lang == getSpellLanguage(last);
4796 // if language change detected backstep is needed
4804 Language * Paragraph::Private::getSpellLanguage(pos_type const from) const
4807 const_cast<Language *>(owner_->getFontSettings(
4808 inset_owner_->buffer().params(), from).language());
4809 if (lang == inset_owner_->buffer().params().language
4810 && !lyxrc.spellchecker_alt_lang.empty()) {
4812 string const lang_variety =
4813 split(lyxrc.spellchecker_alt_lang, lang_code, '-');
4814 lang->setCode(lang_code);
4815 lang->setVariety(lang_variety);
4821 void Paragraph::requestSpellCheck(pos_type pos)
4823 d->requestSpellCheck(pos);
4827 bool Paragraph::needsSpellCheck() const
4829 SpellChecker::ChangeNumber speller_change_number = 0;
4830 if (theSpellChecker())
4831 speller_change_number = theSpellChecker()->changeNumber();
4832 if (speller_change_number > d->speller_state_.currentChangeNumber()) {
4833 d->speller_state_.needsCompleteRefresh(speller_change_number);
4835 return d->needsSpellCheck();
4839 bool Paragraph::Private::ignoreWord(docstring const & word) const
4841 // Ignore words with digits
4842 // FIXME: make this customizable
4843 // (note that some checkers ignore words with digits by default)
4844 docstring::const_iterator cit = word.begin();
4845 docstring::const_iterator const end = word.end();
4846 for (; cit != end; ++cit) {
4847 if (isNumber((*cit)))
4854 SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
4855 WordLangTuple & wl, docstring_list & suggestions,
4856 bool do_suggestion, bool check_learned) const
4858 SpellChecker::Result result = SpellChecker::WORD_OK;
4859 SpellChecker * speller = theSpellChecker();
4863 if (!d->layout_->spellcheck || !inInset().allowSpellCheck())
4866 locateWord(from, to, WHOLE_WORD, true);
4867 if (from == to || from >= size())
4870 docstring word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
4871 Language * lang = d->getSpellLanguage(from);
4873 BufferParams const & bparams = d->inset_owner_->buffer().params();
4875 if (getFontSettings(bparams, from).fontInfo().nospellcheck() == FONT_ON)
4878 wl = WordLangTuple(word, lang);
4883 if (needsSpellCheck() || check_learned) {
4885 if (!d->ignoreWord(word)) {
4886 bool const trailing_dot = to < size() && d->text_[to] == '.';
4887 result = speller->check(wl, bparams.spellignore());
4888 if (SpellChecker::misspelled(result) && trailing_dot) {
4889 wl = WordLangTuple(word.append(from_ascii(".")), lang);
4890 result = speller->check(wl, bparams.spellignore());
4891 if (!SpellChecker::misspelled(result)) {
4892 LYXERR(Debug::GUI, "misspelled word is correct with dot: \"" <<
4894 from << ".." << to << "]");
4896 // spell check with dot appended failed too
4897 // restore original word/lang value
4898 word = asString(from, to, AS_STR_INSETS | AS_STR_SKIPDELETE);
4899 wl = WordLangTuple(word, lang);
4903 if (!SpellChecker::misspelled(result)) {
4904 // area up to the begin of the next word is not misspelled
4905 while (end < size() && isWordSeparator(end))
4908 d->setMisspelled(from, end, result);
4910 result = d->speller_state_.getState(from);
4914 suggestions.clear();
4916 if (SpellChecker::misspelled(result)) {
4917 LYXERR(Debug::GUI, "misspelled word: \"" <<
4919 from << ".." << to << "]");
4921 speller->suggest(wl, suggestions);
4927 void Paragraph::anonymize()
4929 // This is a very crude anonymization for now
4930 for (char_type & c : d->text_)
4931 if (isLetterChar(c) || isNumber(c))
4936 void Paragraph::Private::markMisspelledWords(
4937 Language const * lang,
4938 pos_type const & first, pos_type const & last,
4939 SpellChecker::Result result,
4940 docstring const & word,
4941 SkipPositions const & skips)
4943 if (!SpellChecker::misspelled(result)) {
4944 setMisspelled(first, last, SpellChecker::WORD_OK);
4948 SpellChecker * speller = theSpellChecker();
4949 // locate and enumerate the error positions
4950 int nerrors = speller->numMisspelledWords();
4952 SkipPositionsIterator it = skips.begin();
4953 SkipPositionsIterator et = skips.end();
4954 for (int index = 0; index < nerrors; ++index) {
4957 speller->misspelledWord(index, wstart, wlen);
4958 /// should not happen if speller supports range checks
4961 WordLangTuple const candidate(word.substr(wstart, wlen), lang);
4962 wstart += first + numskipped;
4963 if (snext < wstart) {
4964 /// mark the range of correct spelling
4965 numskipped += countSkips(it, et, wstart);
4966 setMisspelled(snext,
4967 wstart - 1, SpellChecker::WORD_OK);
4969 snext = wstart + wlen;
4970 // Check whether the candidate is in the document's local dict
4971 SpellChecker::Result actresult = result;
4972 if (inset_owner_->buffer().params().spellignored(candidate))
4973 actresult = SpellChecker::DOCUMENT_LEARNED_WORD;
4974 numskipped += countSkips(it, et, snext);
4975 /// mark the range of misspelling
4976 setMisspelled(wstart, snext, actresult);
4977 if (actresult == SpellChecker::DOCUMENT_LEARNED_WORD)
4978 LYXERR(Debug::GUI, "local dictionary word: \"" <<
4979 candidate.word() << "\" [" <<
4980 wstart << ".." << (snext-1) << "]");
4982 LYXERR(Debug::GUI, "misspelled word: \"" <<
4983 candidate.word() << "\" [" <<
4984 wstart << ".." << (snext-1) << "]");
4987 if (snext <= last) {
4988 /// mark the range of correct spelling at end
4989 setMisspelled(snext, last, SpellChecker::WORD_OK);
4994 void Paragraph::spellCheck() const
4996 SpellChecker * speller = theSpellChecker();
4997 if (!speller || empty() ||!needsSpellCheck())
5001 d->rangeOfSpellCheck(start, endpos);
5002 if (speller->canCheckParagraph()) {
5003 // loop until we leave the range
5004 for (pos_type first = start; first < endpos; ) {
5005 pos_type last = endpos;
5006 Private::SkipPositions skips;
5007 Language * lang = d->locateSpellRange(first, last, skips);
5008 if (first >= endpos)
5010 // start the spell checker on the unit of meaning
5011 docstring word = asString(first, last, AS_STR_INSETS + AS_STR_SKIPDELETE);
5012 WordLangTuple wl = WordLangTuple(word, lang);
5013 BufferParams const & bparams = d->inset_owner_->buffer().params();
5014 SpellChecker::Result result = !word.empty() ?
5015 speller->check(wl, bparams.spellignore()) : SpellChecker::WORD_OK;
5016 d->markMisspelledWords(lang, first, last, result, word, skips);
5020 static docstring_list suggestions;
5021 pos_type to = endpos;
5022 while (start < endpos) {
5024 spellCheck(start, to, wl, suggestions, false);
5028 d->readySpellCheck();
5032 bool Paragraph::isMisspelled(pos_type pos, bool check_boundary) const
5034 bool result = SpellChecker::misspelled(d->speller_state_.getState(pos));
5035 if (result || pos <= 0 || pos > size())
5037 if (check_boundary && (pos == size() || isWordSeparator(pos)))
5038 result = SpellChecker::misspelled(d->speller_state_.getState(pos - 1));
5043 string Paragraph::magicLabel() const
5046 ss << "magicparlabel-" << id();