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 Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
24 #include "bufferparams.h"
30 #include "LaTeXFeatures.h"
35 #include "outputparams.h"
36 #include "paragraph_funcs.h"
38 #include "rowpainter.h"
44 #include "frontends/FontMetrics.h"
46 #include "insets/insetbibitem.h"
47 #include "insets/insetoptarg.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
51 #include "support/convert.h"
52 #include "support/unicode.h"
54 #include <boost/bind.hpp>
67 using std::ostringstream;
71 using support::contains;
72 using support::rsplit;
75 Paragraph::Paragraph()
76 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
83 Paragraph::Paragraph(Paragraph const & par)
84 : itemdepth(par.itemdepth), insetlist(par.insetlist),
86 text_(par.text_), begin_of_body_(par.begin_of_body_),
87 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
89 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
90 InsetList::iterator it = insetlist.begin();
91 InsetList::iterator end = insetlist.end();
92 for (; it != end; ++it)
93 it->inset = it->inset->clone().release();
97 Paragraph & Paragraph::operator=(Paragraph const & par)
99 // needed as we will destroy the pimpl_ before copying it
101 itemdepth = par.itemdepth;
103 insetlist = par.insetlist;
104 InsetList::iterator it = insetlist.begin();
105 InsetList::iterator end = insetlist.end();
106 for (; it != end; ++it)
107 it->inset = it->inset->clone().release();
109 layout_ = par.layout();
111 begin_of_body_ = par.begin_of_body_;
114 pimpl_ = new Pimpl(*par.pimpl_, this);
120 Paragraph::~Paragraph()
124 //lyxerr << "Paragraph::paragraph_id = "
125 // << Paragraph::paragraph_id << endl;
129 void Paragraph::write(Buffer const & buf, ostream & os,
130 BufferParams const & bparams,
131 depth_type & dth) const
133 // The beginning or end of a deeper (i.e. nested) area?
134 if (dth != params().depth()) {
135 if (params().depth() > dth) {
136 while (params().depth() > dth) {
137 os << "\n\\begin_deeper";
141 while (params().depth() < dth) {
142 os << "\n\\end_deeper";
148 // First write the layout
149 os << "\n\\begin_layout " << layout()->name() << '\n';
153 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155 Change running_change = Change(Change::UNCHANGED);
158 for (pos_type i = 0; i <= size(); ++i) {
160 Change change = pimpl_->lookupChange(i);
161 Changes::lyxMarkChange(os, column, running_change, change);
162 running_change = change;
167 // Write font changes
168 LyXFont font2 = getFontSettings(bparams, i);
169 if (font2 != font1) {
170 font2.lyxWriteChanges(font1, os);
175 value_type const c = getChar(i);
179 InsetBase const * inset = getInset(i);
181 if (inset->directWrite()) {
182 // international char, let it write
183 // code directly so it's shorter in
185 inset->write(buf, os);
189 os << "\\begin_inset ";
190 inset->write(buf, os);
191 os << "\n\\end_inset\n\n";
197 os << "\n\\backslash\n";
201 if (i + 1 < size() && getChar(i + 1) == ' ') {
208 if ((column > 70 && c == ' ')
213 // this check is to amend a bug. LyX sometimes
214 // inserts '\0' this could cause problems.
216 std::vector<char> tmp = ucs4_to_utf8(c);
220 lyxerr << "ERROR (Paragraph::writeFile):"
221 " NULL char in structure." << endl;
227 os << "\n\\end_layout\n";
231 void Paragraph::validate(LaTeXFeatures & features) const
233 pimpl_->validate(features, *layout());
237 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
239 return pimpl_->eraseChar(pos, trackChanges);
243 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
245 return pimpl_->eraseChars(start, end, trackChanges);
249 void Paragraph::insert(pos_type start, docstring const & str,
250 LyXFont const & font, Change const & change)
252 for (size_t i = 0, n = str.size(); i != n ; ++i)
253 insertChar(start + i, str[i], font, change);
257 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
260 pimpl_->insertChar(pos, c, Change(trackChanges ?
261 Change::INSERTED : Change::UNCHANGED));
265 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
266 LyXFont const & font, bool trackChanges)
268 pimpl_->insertChar(pos, c, Change(trackChanges ?
269 Change::INSERTED : Change::UNCHANGED));
274 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
275 LyXFont const & font, Change const & change)
277 pimpl_->insertChar(pos, c, change);
282 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
283 Change const & change)
285 pimpl_->insertInset(pos, inset, change);
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
290 LyXFont const & font, Change const & change)
292 pimpl_->insertInset(pos, inset, change);
297 bool Paragraph::insetAllowed(InsetBase_code code)
299 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
303 // Gets uninstantiated font setting at position.
304 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
308 lyxerr << " pos: " << pos << " size: " << size() << endl;
309 BOOST_ASSERT(pos <= size());
312 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314 for (; cit != end; ++cit)
315 if (cit->pos() >= pos)
321 if (pos == size() && !empty())
322 return getFontSettings(bparams, pos - 1);
324 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
328 FontSpan Paragraph::fontSpan(pos_type pos) const
330 BOOST_ASSERT(pos <= size());
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit) {
336 if (cit->pos() >= pos) {
337 if (pos >= beginOfBody())
338 return FontSpan(std::max(start, beginOfBody()),
341 return FontSpan(start,
342 std::min(beginOfBody() - 1,
345 start = cit->pos() + 1;
348 // This should not happen, but if so, we take no chances.
349 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
351 return FontSpan(pos, pos);
355 // Gets uninstantiated font setting at position 0
356 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
358 if (!empty() && !pimpl_->fontlist.empty())
359 return pimpl_->fontlist[0].font();
361 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
365 // Gets the fully instantiated font at a given position in a paragraph
366 // This is basically the same function as LyXText::GetFont() in text2.C.
367 // The difference is that this one is used for generating the LaTeX file,
368 // and thus cosmetic "improvements" are disallowed: This has to deliver
369 // the true picture of the buffer. (Asger)
370 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
371 LyXFont const & outerfont) const
373 BOOST_ASSERT(pos >= 0);
375 LyXLayout_ptr const & lout = layout();
377 pos_type const body_pos = beginOfBody();
381 layoutfont = lout->labelfont;
383 layoutfont = lout->font;
385 LyXFont font = getFontSettings(bparams, pos);
386 font.realize(layoutfont);
387 font.realize(outerfont);
388 font.realize(bparams.getFont());
394 LyXFont const Paragraph::getLabelFont
395 (BufferParams const & bparams, LyXFont const & outerfont) const
397 LyXFont tmpfont = layout()->labelfont;
398 tmpfont.setLanguage(getParLanguage(bparams));
399 tmpfont.realize(outerfont);
400 tmpfont.realize(bparams.getFont());
405 LyXFont const Paragraph::getLayoutFont
406 (BufferParams const & bparams, LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->font;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getFont());
416 /// Returns the height of the highest font in range
417 LyXFont_size Paragraph::highestFontInRange
418 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
420 if (pimpl_->fontlist.empty())
423 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
424 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
425 for (; end_it != end; ++end_it) {
426 if (end_it->pos() >= endpos)
433 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
434 for (; cit != end; ++cit) {
435 if (cit->pos() >= startpos)
439 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
440 for (; cit != end_it; ++cit) {
441 LyXFont::FONT_SIZE size = cit->font().size();
442 if (size == LyXFont::INHERIT_SIZE)
444 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
451 Paragraph::value_type
452 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
454 value_type c = getChar(pos);
455 if (!lyxrc.rtl_support)
485 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
492 void Paragraph::setFont(pos_type pos, LyXFont const & font)
494 BOOST_ASSERT(pos <= size());
496 // First, reduce font against layout/label font
497 // Update: The setCharFont() routine in text2.C already
498 // reduces font, so we don't need to do that here. (Asger)
499 // No need to simplify this because it will disappear
500 // in a new kernel. (Asger)
501 // Next search font table
503 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
504 Pimpl::FontList::iterator it = beg;
505 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
506 for (; it != endit; ++it) {
507 if (it->pos() >= pos)
510 size_t const i = distance(beg, it);
511 bool notfound = (it == endit);
513 if (!notfound && pimpl_->fontlist[i].font() == font)
516 bool begin = pos == 0 || notfound ||
517 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
518 // Is position pos is a beginning of a font block?
519 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
520 // Is position pos is the end of a font block?
521 if (begin && end) { // A single char block
522 if (i + 1 < pimpl_->fontlist.size() &&
523 pimpl_->fontlist[i + 1].font() == font) {
524 // Merge the singleton block with the next block
525 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
527 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
528 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
529 // Merge the singleton block with the previous block
530 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
533 pimpl_->fontlist[i].font(font);
535 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
536 pimpl_->fontlist[i - 1].pos(pos);
538 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
539 Pimpl::FontTable(pos, font));
541 pimpl_->fontlist[i].pos(pos - 1);
542 if (!(i + 1 < pimpl_->fontlist.size() &&
543 pimpl_->fontlist[i + 1].font() == font))
544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
545 Pimpl::FontTable(pos, font));
546 } else { // The general case. The block is splitted into 3 blocks
547 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
548 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
550 Pimpl::FontTable(pos, font));
555 void Paragraph::makeSameLayout(Paragraph const & par)
557 layout(par.layout());
559 params() = par.params();
563 bool Paragraph::stripLeadingSpaces(bool trackChanges)
571 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
572 if (eraseChar(pos, trackChanges))
578 return count > 0 || pos > 0;
582 bool Paragraph::hasSameLayout(Paragraph const & par) const
584 return par.layout() == layout() && params().sameLayout(par.params());
588 depth_type Paragraph::getDepth() const
590 return params().depth();
594 depth_type Paragraph::getMaxDepthAfter() const
596 if (layout()->isEnvironment())
597 return params().depth() + 1;
599 return params().depth();
603 char Paragraph::getAlign() const
605 if (params().align() == LYX_ALIGN_LAYOUT)
606 return layout()->align;
608 return params().align();
612 docstring const & Paragraph::getLabelstring() const
614 return params().labelString();
618 // the next two functions are for the manual labels
619 docstring const Paragraph::getLabelWidthString() const
621 if (!params().labelWidthString().empty())
622 return params().labelWidthString();
624 return _("Senseless with this layout!");
628 void Paragraph::setLabelWidthString(docstring const & s)
630 params().labelWidthString(s);
634 docstring const Paragraph::translateIfPossible(docstring const & s,
635 BufferParams const & bparams) const
637 if (!support::isAscii(s) || s.empty()) {
638 // This must be a user defined layout. We cannot translate
639 // this, since gettext accepts only ascii keys.
642 // Probably standard layout, try to translate
643 Messages & m = getMessages(getParLanguage(bparams)->code());
644 return m.get(to_ascii(s));
648 docstring Paragraph::expandLabel(LyXLayout_ptr const & layout,
649 BufferParams const & bparams, bool process_appendix) const
651 LyXTextClass const & tclass = bparams.getLyXTextClass();
654 if (process_appendix && params().appendix())
655 fmt = translateIfPossible(layout->labelstring_appendix(),
658 fmt = translateIfPossible(layout->labelstring(), bparams);
660 // handle 'inherited level parts' in 'fmt',
661 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
662 size_t const i = fmt.find('@', 0);
663 if (i != docstring::npos) {
664 size_t const j = fmt.find('@', i + 1);
665 if (j != docstring::npos) {
666 docstring parent(fmt, i + 1, j - i - 1);
668 docstring label = expandLabel(tclass[to_utf8(parent)], bparams);
669 fmt = docstring(fmt, 0, i) + label + docstring(fmt, j + 1, docstring::npos);
673 return tclass.counters().counterLabel(fmt);
677 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
680 params().labelWidthString(docstring());
681 params().align(LYX_ALIGN_LAYOUT);
682 params().spacing(Spacing(Spacing::Default));
686 pos_type Paragraph::beginOfBody() const
688 return begin_of_body_;
692 void Paragraph::setBeginOfBody()
694 if (layout()->labeltype != LABEL_MANUAL) {
699 // Unroll the first two cycles of the loop
700 // and remember the previous character to
701 // remove unnecessary getChar() calls
703 pos_type end = size();
704 if (i < end && !isNewline(i)) {
706 char_type previous_char = 0;
709 previous_char = text_[i];
712 while (i < end && previous_char != ' ') {
717 previous_char = temp;
727 // returns -1 if inset not found
728 int Paragraph::getPositionOfInset(InsetBase const * inset) const
731 InsetList::const_iterator it = insetlist.begin();
732 InsetList::const_iterator end = insetlist.end();
733 for (; it != end; ++it)
734 if (it->inset == inset)
740 InsetBibitem * Paragraph::bibitem() const
742 if (!insetlist.empty()) {
743 InsetBase * inset = insetlist.begin()->inset;
744 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
745 return static_cast<InsetBibitem *>(inset);
751 bool Paragraph::forceDefaultParagraphs() const
753 return inInset() && inInset()->forceDefaultParagraphs(0);
759 // paragraphs inside floats need different alignment tags to avoid
762 bool noTrivlistCentering(InsetBase::Code code)
764 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
768 string correction(string const & orig)
770 if (orig == "flushleft")
771 return "raggedright";
772 if (orig == "flushright")
774 if (orig == "center")
780 string const corrected_env(string const & suffix, string const & env,
781 InsetBase::Code code)
783 string output = suffix + "{";
784 if (noTrivlistCentering(code))
785 output += correction(env);
789 if (suffix == "\\begin")
795 void adjust_row_column(string const & str, TexRow & texrow, int & column)
797 if (!contains(str, "\n"))
798 column += str.size();
802 column = rsplit(str, tmp, '\n').size();
809 // This could go to ParagraphParameters if we want to
810 int Paragraph::startTeXParParams(BufferParams const & bparams,
811 odocstream & os, TexRow & texrow,
812 bool moving_arg) const
816 if (params().noindent()) {
821 switch (params().align()) {
823 case LYX_ALIGN_BLOCK:
824 case LYX_ALIGN_LAYOUT:
825 case LYX_ALIGN_SPECIAL:
828 case LYX_ALIGN_RIGHT:
829 case LYX_ALIGN_CENTER:
837 switch (params().align()) {
839 case LYX_ALIGN_BLOCK:
840 case LYX_ALIGN_LAYOUT:
841 case LYX_ALIGN_SPECIAL:
843 case LYX_ALIGN_LEFT: {
845 if (getParLanguage(bparams)->babel() != "hebrew")
846 output = corrected_env("\\begin", "flushleft", ownerCode());
848 output = corrected_env("\\begin", "flushright", ownerCode());
849 os << from_ascii(output);
850 adjust_row_column(output, texrow, column);
852 } case LYX_ALIGN_RIGHT: {
854 if (getParLanguage(bparams)->babel() != "hebrew")
855 output = corrected_env("\\begin", "flushright", ownerCode());
857 output = corrected_env("\\begin", "flushleft", ownerCode());
858 os << from_ascii(output);
859 adjust_row_column(output, texrow, column);
861 } case LYX_ALIGN_CENTER: {
863 output = corrected_env("\\begin", "center", ownerCode());
864 os << from_ascii(output);
865 adjust_row_column(output, texrow, column);
874 // This could go to ParagraphParameters if we want to
875 int Paragraph::endTeXParParams(BufferParams const & bparams,
876 odocstream & os, TexRow & texrow,
877 bool moving_arg) const
881 switch (params().align()) {
883 case LYX_ALIGN_BLOCK:
884 case LYX_ALIGN_LAYOUT:
885 case LYX_ALIGN_SPECIAL:
888 case LYX_ALIGN_RIGHT:
889 case LYX_ALIGN_CENTER:
897 switch (params().align()) {
899 case LYX_ALIGN_BLOCK:
900 case LYX_ALIGN_LAYOUT:
901 case LYX_ALIGN_SPECIAL:
903 case LYX_ALIGN_LEFT: {
905 if (getParLanguage(bparams)->babel() != "hebrew")
906 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
908 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
909 os << from_ascii(output);
910 adjust_row_column(output, texrow, column);
912 } case LYX_ALIGN_RIGHT: {
914 if (getParLanguage(bparams)->babel() != "hebrew")
915 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
917 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
918 os << from_ascii(output);
919 adjust_row_column(output, texrow, column);
921 } case LYX_ALIGN_CENTER: {
923 output = corrected_env("\n\\par\\end", "center", ownerCode());
924 os << from_ascii(output);
925 adjust_row_column(output, texrow, column);
934 // This one spits out the text of the paragraph
935 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
936 BufferParams const & bparams,
937 LyXFont const & outerfont,
938 odocstream & os, TexRow & texrow,
939 OutputParams const & runparams) const
941 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
943 bool return_value = false;
947 // well we have to check if we are in an inset with unlimited
948 // length (all in one row) if that is true then we don't allow
949 // any special options in the paragraph and also we don't allow
950 // any environment other than the default layout of the text class
952 bool asdefault = forceDefaultParagraphs();
955 style = bparams.getLyXTextClass().defaultLayout();
960 // Current base font for all inherited font changes, without any
961 // change caused by an individual character, except for the language:
962 // It is set to the language of the first character.
963 // As long as we are in the label, this font is the base font of the
964 // label. Before the first body character it is set to the base font
966 // This must be identical to basefont in TeXOnePar().
969 // output change tracking marks only if desired,
970 // if dvipost is installed,
971 // and with dvi/ps (other formats don't work)
972 bool const output = bparams.outputChanges
973 && runparams.flavor == OutputParams::LATEX
974 && LaTeXFeatures::isAvailable("dvipost");
976 // Maybe we have to create a optional argument.
977 pos_type body_pos = beginOfBody();
978 unsigned int column = 0;
981 // the optional argument is kept in curly brackets in
982 // case it contains a ']'
985 basefont = getLabelFont(bparams, outerfont);
987 basefont = getLayoutFont(bparams, outerfont);
990 // Which font is currently active?
991 LyXFont running_font(basefont);
992 // Do we have an open font change?
993 bool open_font = false;
995 Change::Type runningChangeType = Change::UNCHANGED;
997 texrow.start(id(), 0);
999 // if the paragraph is empty, the loop will not be entered at all
1001 if (style->isCommand()) {
1006 column += startTeXParParams(bparams, os, texrow,
1007 runparams.moving_arg);
1010 // Computed only once per paragraph since bparams.encoding() is expensive
1011 Encoding const & doc_encoding = bparams.encoding();
1012 for (pos_type i = 0; i < size(); ++i) {
1013 // First char in paragraph or after label?
1014 if (i == body_pos) {
1017 column += running_font.latexWriteEndChanges(
1018 os, basefont, basefont, bparams);
1021 basefont = getLayoutFont(bparams, outerfont);
1022 running_font = basefont;
1024 column += Changes::latexMarkChange(os,
1025 runningChangeType, Change::UNCHANGED, output);
1026 runningChangeType = Change::UNCHANGED;
1031 if (style->isCommand()) {
1037 column += startTeXParParams(bparams, os,
1039 runparams.moving_arg);
1042 Change::Type changeType = pimpl_->lookupChange(i).type;
1044 // do not output text which is marked deleted
1045 // if change tracking output is disabled
1046 if (!output && changeType == Change::DELETED) {
1047 runningChangeType = changeType;
1053 column += Changes::latexMarkChange(os, runningChangeType,
1054 changeType, output);
1055 runningChangeType = changeType;
1057 value_type c = getChar(i);
1059 // Fully instantiated font
1060 LyXFont font = getFont(bparams, i, outerfont);
1062 LyXFont const last_font = running_font;
1064 // Do we need to close the previous font?
1066 (font != running_font ||
1067 font.language() != running_font.language()))
1069 column += running_font.latexWriteEndChanges(
1071 (i == body_pos-1) ? basefont : font,
1073 running_font = basefont;
1077 // Do we need to change font?
1078 if ((font != running_font ||
1079 font.language() != running_font.language()) &&
1082 column += font.latexWriteStartChanges(
1083 os, basefont, last_font, bparams);
1084 running_font = font;
1089 // Do not print the separation of the optional argument
1090 // if style->pass_thru is false. This works because
1091 // simpleTeXSpecialChars ignores spaces if
1092 // style->pass_thru is false.
1093 if (i != body_pos - 1) {
1094 if (pimpl_->simpleTeXBlanks(bparams,
1095 doc_encoding, os, texrow,
1096 i, column, font, *style))
1097 // A surrogate pair was output. We
1098 // must not call simpleTeXSpecialChars
1099 // in this iteration, since
1100 // simpleTeXBlanks incremented i, and
1101 // simpleTeXSpecialChars would output
1102 // the combining character again.
1107 OutputParams rp = runparams;
1108 rp.free_spacing = style->free_spacing;
1109 rp.local_font = &font;
1110 rp.intitle = style->intitle;
1111 pimpl_->simpleTeXSpecialChars(buf, bparams, doc_encoding, os,
1112 texrow, rp, running_font,
1113 basefont, outerfont, open_font,
1114 runningChangeType, *style, i, column, c);
1117 // If we have an open font definition, we have to close it
1119 #ifdef FIXED_LANGUAGE_END_DETECTION
1122 .latexWriteEndChanges(os, basefont,
1123 next_->getFont(bparams, 0, outerfont),
1126 running_font.latexWriteEndChanges(os, basefont,
1130 #ifdef WITH_WARNINGS
1131 //#warning For now we ALWAYS have to close the foreign font settings if they are
1132 //#warning there as we start another \selectlanguage with the next paragraph if
1133 //#warning we are in need of this. This should be fixed sometime (Jug)
1135 running_font.latexWriteEndChanges(os, basefont, basefont,
1140 column += Changes::latexMarkChange(os,
1141 runningChangeType, Change::UNCHANGED, output);
1143 // Needed if there is an optional argument but no contents.
1144 if (body_pos > 0 && body_pos == size()) {
1146 return_value = false;
1150 column += endTeXParParams(bparams, os, texrow,
1151 runparams.moving_arg);
1154 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1155 return return_value;
1172 string tag_name(PAR_TAG const & pt) {
1174 case PAR_NONE: return "!-- --";
1175 case TT: return "tt";
1176 case SF: return "sf";
1177 case BF: return "bf";
1178 case IT: return "it";
1179 case SL: return "sl";
1180 case EM: return "em";
1187 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1189 p1 = static_cast<PAR_TAG>(p1 | p2);
1194 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1196 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1202 bool Paragraph::emptyTag() const
1204 for (pos_type i = 0; i < size(); ++i) {
1206 InsetBase const * inset = getInset(i);
1207 InsetBase::Code lyx_code = inset->lyxCode();
1208 if (lyx_code != InsetBase::TOC_CODE &&
1209 lyx_code != InsetBase::INCLUDE_CODE &&
1210 lyx_code != InsetBase::GRAPHICS_CODE &&
1211 lyx_code != InsetBase::ERT_CODE &&
1212 lyx_code != InsetBase::FLOAT_CODE &&
1213 lyx_code != InsetBase::TABULAR_CODE) {
1217 value_type c = getChar(i);
1218 if (c != ' ' && c != '\t')
1226 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1228 for (pos_type i = 0; i < size(); ++i) {
1230 InsetBase const * inset = getInset(i);
1231 InsetBase::Code lyx_code = inset->lyxCode();
1232 if (lyx_code == InsetBase::LABEL_CODE) {
1233 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1234 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1243 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1246 for (i = 0; i < size(); ++i) {
1248 InsetBase const * inset = getInset(i);
1249 inset->docbook(buf, os, runparams);
1251 value_type c = getChar(i);
1254 os << sgml::escapeChar(c);
1261 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1265 for (pos_type i = initial; i < size(); ++i) {
1266 LyXFont font = getFont(buf.params(), i, outerfont);
1269 if (i != initial && font != font_old)
1278 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1280 OutputParams const & runparams,
1281 LyXFont const & outerfont,
1282 pos_type initial) const
1284 bool emph_flag = false;
1286 LyXLayout_ptr const & style = layout();
1288 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1290 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1293 // parsing main loop
1294 for (pos_type i = initial; i < size(); ++i) {
1295 LyXFont font = getFont(buf.params(), i, outerfont);
1297 // handle <emphasis> tag
1298 if (font_old.emph() != font.emph()) {
1299 if (font.emph() == LyXFont::ON) {
1302 } else if (i != initial) {
1303 os << "</emphasis>";
1309 InsetBase const * inset = getInset(i);
1310 inset->docbook(buf, os, runparams);
1312 value_type c = getChar(i);
1314 if (style->pass_thru)
1317 os << sgml::escapeChar(c);
1323 os << "</emphasis>";
1326 if (style->free_spacing)
1328 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1333 bool Paragraph::isNewline(pos_type pos) const
1336 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1340 bool Paragraph::isLineSeparator(pos_type pos) const
1342 value_type const c = getChar(pos);
1343 return isLineSeparatorChar(c)
1344 || (c == Paragraph::META_INSET && getInset(pos) &&
1345 getInset(pos)->isLineSeparator());
1349 /// Used by the spellchecker
1350 bool Paragraph::isLetter(pos_type pos) const
1353 return getInset(pos)->isLetter();
1355 value_type const c = getChar(pos);
1356 return isLetterChar(c) || isDigit(c);
1362 Paragraph::getParLanguage(BufferParams const & bparams) const
1365 return getFirstFontSettings(bparams).language();
1366 #ifdef WITH_WARNINGS
1367 #warning FIXME we should check the prev par as well (Lgb)
1369 return bparams.language;
1373 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1375 return lyxrc.rtl_support
1376 && getParLanguage(bparams)->rightToLeft()
1377 && ownerCode() != InsetBase::ERT_CODE;
1381 void Paragraph::changeLanguage(BufferParams const & bparams,
1382 Language const * from, Language const * to)
1384 // change language including dummy font change at the end
1385 for (pos_type i = 0; i <= size(); ++i) {
1386 LyXFont font = getFontSettings(bparams, i);
1387 if (font.language() == from) {
1388 font.setLanguage(to);
1395 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1397 Language const * doc_language = bparams.language;
1398 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1399 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1401 for (; cit != end; ++cit)
1402 if (cit->font().language() != ignore_language &&
1403 cit->font().language() != latex_language &&
1404 cit->font().language() != doc_language)
1410 // Convert the paragraph to a string.
1411 // Used for building the table of contents
1412 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1414 return asString(buffer, 0, size(), label);
1418 docstring const Paragraph::asString(Buffer const & buffer,
1419 pos_type beg, pos_type end, bool label) const
1422 odocstringstream os;
1424 if (beg == 0 && label && !params().labelString().empty())
1425 os << params().labelString() << ' ';
1427 for (pos_type i = beg; i < end; ++i) {
1428 value_type const c = getUChar(buffer.params(), i);
1431 else if (c == META_INSET)
1432 getInset(i)->textString(buffer, os);
1439 void Paragraph::setInsetOwner(InsetBase * inset)
1441 pimpl_->inset_owner = inset;
1445 Change const & Paragraph::lookupChange(pos_type pos) const
1447 BOOST_ASSERT(pos <= size());
1448 return pimpl_->lookupChange(pos);
1452 bool Paragraph::isChanged(pos_type start, pos_type end) const
1454 return pimpl_->isChanged(start, end);
1458 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1460 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1464 void Paragraph::setChange(Change const & change)
1466 pimpl_->setChange(change);
1470 void Paragraph::setChange(pos_type pos, Change const & change)
1472 pimpl_->setChange(pos, change);
1476 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start, pos_type end)
1478 return pimpl_->acceptChanges(bparams, start, end);
1482 void Paragraph::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
1484 return pimpl_->rejectChanges(bparams, start, end);
1488 int Paragraph::id() const
1494 LyXLayout_ptr const & Paragraph::layout() const
1500 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1502 layout_ = new_layout;
1506 InsetBase * Paragraph::inInset() const
1508 return pimpl_->inset_owner;
1512 InsetBase::Code Paragraph::ownerCode() const
1514 return pimpl_->inset_owner
1515 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1519 ParagraphParameters & Paragraph::params()
1521 return pimpl_->params;
1525 ParagraphParameters const & Paragraph::params() const
1527 return pimpl_->params;
1531 bool Paragraph::isFreeSpacing() const
1533 if (layout()->free_spacing)
1536 // for now we just need this, later should we need this in some
1537 // other way we can always add a function to InsetBase too.
1538 return ownerCode() == InsetBase::ERT_CODE;
1542 bool Paragraph::allowEmpty() const
1544 if (layout()->keepempty)
1546 return ownerCode() == InsetBase::ERT_CODE;
1550 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1552 if (!Encodings::is_arabic(c))
1553 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1554 // FIXME UNICODE What does this do?
1555 return c + (0xb0 - '0');
1559 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1560 value_type next_char = ' ';
1562 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1563 value_type const par_char = getChar(i);
1564 if (!Encodings::isComposeChar_arabic(par_char)) {
1565 next_char = par_char;
1570 if (Encodings::is_arabic(next_char)) {
1571 if (Encodings::is_arabic(prev_char) &&
1572 !Encodings::is_arabic_special(prev_char))
1573 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1575 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1577 if (Encodings::is_arabic(prev_char) &&
1578 !Encodings::is_arabic_special(prev_char))
1579 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1581 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1586 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1591 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
1593 // expand at the end of a row only if there is another hfill on the same row
1594 if (pos == row.endpos() - 1) {
1595 for (pos_type i = row.pos(); i < pos; i++) {
1602 // expand at the beginning of a row only if it is the first row of a paragraph
1603 if (pos == row.pos()) {
1607 // do not expand in some labels
1608 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1611 // if there is anything between the first char of the row and
1612 // the specified position that is neither a newline nor an hfill,
1613 // the hfill will be expanded, otherwise it won't
1614 for (pos_type i = row.pos(); i < pos; i++) {
1615 if (!isNewline(i) && !isHfill(i))
1622 bool Paragraph::checkBiblio(bool track_changes)
1624 // Add bibitem insets if necessary
1625 if (layout()->labeltype != LABEL_BIBLIO)
1628 bool hasbibitem = !insetlist.empty()
1629 // Insist on it being in pos 0
1630 && getChar(0) == Paragraph::META_INSET
1631 && insetlist.begin()->inset->lyxCode() == InsetBase::BIBITEM_CODE;
1636 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
1637 insertInset(0, static_cast<InsetBase *>(inset),
1638 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));