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"
34 #include "outputparams.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphList_fwd.h"
41 #include "insets/insetbibitem.h"
42 #include "insets/insetoptarg.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
46 #include "support/convert.h"
47 #include "support/unicode.h"
49 #include <boost/bind.hpp>
59 using lyx::support::contains;
60 using lyx::support::rsplit;
69 using std::ostringstream;
72 Paragraph::Paragraph()
73 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
80 Paragraph::Paragraph(Paragraph const & par)
81 : itemdepth(par.itemdepth), insetlist(par.insetlist),
83 rows_(par.rows_), rowSignature_(par.rowSignature_),
85 text_(par.text_), begin_of_body_(par.begin_of_body_),
86 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
88 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
89 InsetList::iterator it = insetlist.begin();
90 InsetList::iterator end = insetlist.end();
91 for (; it != end; ++it)
92 it->inset = it->inset->clone().release();
96 Paragraph & Paragraph::operator=(Paragraph const & par)
98 // needed as we will destroy the pimpl_ before copying it
100 itemdepth = par.itemdepth;
102 insetlist = par.insetlist;
103 InsetList::iterator it = insetlist.begin();
104 InsetList::iterator end = insetlist.end();
105 for (; it != end; ++it)
106 it->inset = it->inset->clone().release();
110 rowSignature_ = par.rowSignature_;
111 layout_ = par.layout();
113 begin_of_body_ = par.begin_of_body_;
116 pimpl_ = new Pimpl(*par.pimpl_, this);
122 Paragraph::~Paragraph()
126 //lyxerr << "Paragraph::paragraph_id = "
127 // << Paragraph::paragraph_id << endl;
131 void Paragraph::write(Buffer const & buf, ostream & os,
132 BufferParams const & bparams,
133 depth_type & dth) const
135 // The beginning or end of a deeper (i.e. nested) area?
136 if (dth != params().depth()) {
137 if (params().depth() > dth) {
138 while (params().depth() > dth) {
139 os << "\n\\begin_deeper";
143 while (params().depth() < dth) {
144 os << "\n\\end_deeper";
150 // First write the layout
151 os << "\n\\begin_layout " << layout()->name() << '\n';
155 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
157 Change running_change = Change(Change::UNCHANGED);
160 for (pos_type i = 0; i <= size(); ++i) {
162 Change change = pimpl_->lookupChange(i);
163 Changes::lyxMarkChange(os, column, running_change, change);
164 running_change = change;
169 // Write font changes
170 LyXFont font2 = getFontSettings(bparams, i);
171 if (font2 != font1) {
172 font2.lyxWriteChanges(font1, os);
177 value_type const c = getChar(i);
181 InsetBase const * inset = getInset(i);
183 if (inset->directWrite()) {
184 // international char, let it write
185 // code directly so it's shorter in
187 inset->write(buf, os);
191 os << "\\begin_inset ";
192 inset->write(buf, os);
193 os << "\n\\end_inset\n\n";
199 os << "\n\\backslash\n";
203 if (i + 1 < size() && getChar(i + 1) == ' ') {
210 if ((column > 70 && c == ' ')
215 // this check is to amend a bug. LyX sometimes
216 // inserts '\0' this could cause problems.
218 std::vector<char> tmp = ucs4_to_utf8(c);
222 lyxerr << "ERROR (Paragraph::writeFile):"
223 " NULL char in structure." << endl;
229 os << "\n\\end_layout\n";
233 void Paragraph::validate(LaTeXFeatures & features) const
235 pimpl_->validate(features, *layout());
239 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
241 return pimpl_->eraseChar(pos, trackChanges);
245 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
247 return pimpl_->eraseChars(start, end, trackChanges);
251 void Paragraph::insert(pos_type start, docstring const & str,
252 LyXFont const & font, Change const & change)
254 for (size_t i = 0, n = str.size(); i != n ; ++i)
255 insertChar(start + i, str[i], font, change);
259 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
262 pimpl_->insertChar(pos, c, Change(trackChanges ?
263 Change::INSERTED : Change::UNCHANGED));
267 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
268 LyXFont const & font, bool trackChanges)
270 pimpl_->insertChar(pos, c, Change(trackChanges ?
271 Change::INSERTED : Change::UNCHANGED));
276 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
277 LyXFont const & font, Change const & change)
279 pimpl_->insertChar(pos, c, change);
284 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
285 Change const & change)
287 pimpl_->insertInset(pos, inset, change);
291 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
292 LyXFont const & font, Change const & change)
294 pimpl_->insertInset(pos, inset, change);
299 bool Paragraph::insetAllowed(InsetBase_code code)
301 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
305 // Gets uninstantiated font setting at position.
306 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
310 lyxerr << " pos: " << pos << " size: " << size() << endl;
311 BOOST_ASSERT(pos <= size());
314 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
315 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
316 for (; cit != end; ++cit)
317 if (cit->pos() >= pos)
323 if (pos == size() && !empty())
324 return getFontSettings(bparams, pos - 1);
326 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
330 FontSpan Paragraph::fontSpan(pos_type pos) const
332 BOOST_ASSERT(pos <= size());
335 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
336 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
337 for (; cit != end; ++cit) {
338 if (cit->pos() >= pos) {
339 if (pos >= beginOfBody())
340 return FontSpan(std::max(start, beginOfBody()),
343 return FontSpan(start,
344 std::min(beginOfBody() - 1,
347 start = cit->pos() + 1;
350 // This should not happen, but if so, we take no chances.
351 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
353 return FontSpan(pos, pos);
357 // Gets uninstantiated font setting at position 0
358 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
360 if (!empty() && !pimpl_->fontlist.empty())
361 return pimpl_->fontlist[0].font();
363 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
367 // Gets the fully instantiated font at a given position in a paragraph
368 // This is basically the same function as LyXText::GetFont() in text2.C.
369 // The difference is that this one is used for generating the LaTeX file,
370 // and thus cosmetic "improvements" are disallowed: This has to deliver
371 // the true picture of the buffer. (Asger)
372 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
373 LyXFont const & outerfont) const
375 BOOST_ASSERT(pos >= 0);
377 LyXLayout_ptr const & lout = layout();
379 pos_type const body_pos = beginOfBody();
383 layoutfont = lout->labelfont;
385 layoutfont = lout->font;
387 LyXFont font = getFontSettings(bparams, pos);
388 font.realize(layoutfont);
389 font.realize(outerfont);
390 font.realize(bparams.getFont());
396 LyXFont const Paragraph::getLabelFont
397 (BufferParams const & bparams, LyXFont const & outerfont) const
399 LyXFont tmpfont = layout()->labelfont;
400 tmpfont.setLanguage(getParLanguage(bparams));
401 tmpfont.realize(outerfont);
402 tmpfont.realize(bparams.getFont());
407 LyXFont const Paragraph::getLayoutFont
408 (BufferParams const & bparams, LyXFont const & outerfont) const
410 LyXFont tmpfont = layout()->font;
411 tmpfont.setLanguage(getParLanguage(bparams));
412 tmpfont.realize(outerfont);
413 tmpfont.realize(bparams.getFont());
418 /// Returns the height of the highest font in range
419 LyXFont_size Paragraph::highestFontInRange
420 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
422 if (pimpl_->fontlist.empty())
425 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
426 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
427 for (; end_it != end; ++end_it) {
428 if (end_it->pos() >= endpos)
435 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
436 for (; cit != end; ++cit) {
437 if (cit->pos() >= startpos)
441 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
442 for (; cit != end_it; ++cit) {
443 LyXFont::FONT_SIZE size = cit->font().size();
444 if (size == LyXFont::INHERIT_SIZE)
446 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
453 Paragraph::value_type
454 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
456 value_type c = getChar(pos);
457 if (!lyxrc.rtl_support)
487 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
494 void Paragraph::setFont(pos_type pos, LyXFont const & font)
496 BOOST_ASSERT(pos <= size());
498 // First, reduce font against layout/label font
499 // Update: The setCharFont() routine in text2.C already
500 // reduces font, so we don't need to do that here. (Asger)
501 // No need to simplify this because it will disappear
502 // in a new kernel. (Asger)
503 // Next search font table
505 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
506 Pimpl::FontList::iterator it = beg;
507 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
508 for (; it != endit; ++it) {
509 if (it->pos() >= pos)
512 size_t const i = distance(beg, it);
513 bool notfound = (it == endit);
515 if (!notfound && pimpl_->fontlist[i].font() == font)
518 bool begin = pos == 0 || notfound ||
519 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
520 // Is position pos is a beginning of a font block?
521 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
522 // Is position pos is the end of a font block?
523 if (begin && end) { // A single char block
524 if (i + 1 < pimpl_->fontlist.size() &&
525 pimpl_->fontlist[i + 1].font() == font) {
526 // Merge the singleton block with the next block
527 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
528 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
529 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
530 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
531 // Merge the singleton block with the previous block
532 pimpl_->fontlist[i - 1].pos(pos);
533 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
535 pimpl_->fontlist[i].font(font);
537 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
538 pimpl_->fontlist[i - 1].pos(pos);
540 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
541 Pimpl::FontTable(pos, font));
543 pimpl_->fontlist[i].pos(pos - 1);
544 if (!(i + 1 < pimpl_->fontlist.size() &&
545 pimpl_->fontlist[i + 1].font() == font))
546 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
547 Pimpl::FontTable(pos, font));
548 } else { // The general case. The block is splitted into 3 blocks
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
550 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
551 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
552 Pimpl::FontTable(pos, font));
557 void Paragraph::makeSameLayout(Paragraph const & par)
559 layout(par.layout());
561 params() = par.params();
565 int Paragraph::stripLeadingSpaces()
571 while (!empty() && (isNewline(0) || isLineSeparator(0))
573 eraseChar(0, false); // no change tracking here
581 bool Paragraph::hasSameLayout(Paragraph const & par) const
583 return par.layout() == layout() && params().sameLayout(par.params());
587 depth_type Paragraph::getDepth() const
589 return params().depth();
593 depth_type Paragraph::getMaxDepthAfter() const
595 if (layout()->isEnvironment())
596 return params().depth() + 1;
598 return params().depth();
602 char Paragraph::getAlign() const
604 return params().align();
608 docstring const & Paragraph::getLabelstring() const
610 return params().labelString();
614 // the next two functions are for the manual labels
615 docstring const Paragraph::getLabelWidthString() const
617 if (!params().labelWidthString().empty())
618 return params().labelWidthString();
620 return _("Senseless with this layout!");
624 void Paragraph::setLabelWidthString(docstring const & s)
626 params().labelWidthString(s);
630 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
633 params().labelWidthString(docstring());
634 params().align(LYX_ALIGN_LAYOUT);
635 params().spacing(Spacing(Spacing::Default));
639 pos_type Paragraph::beginOfBody() const
641 return begin_of_body_;
645 void Paragraph::setBeginOfBody()
647 if (layout()->labeltype != LABEL_MANUAL) {
652 // Unroll the first two cycles of the loop
653 // and remember the previous character to
654 // remove unnecessary getChar() calls
656 pos_type end = size();
657 if (i < end && !isNewline(i)) {
659 char_type previous_char = 0;
662 previous_char = text_[i];
665 while (i < end && previous_char != ' ') {
670 previous_char = temp;
680 // returns -1 if inset not found
681 int Paragraph::getPositionOfInset(InsetBase const * inset) const
684 InsetList::const_iterator it = insetlist.begin();
685 InsetList::const_iterator end = insetlist.end();
686 for (; it != end; ++it)
687 if (it->inset == inset)
693 InsetBibitem * Paragraph::bibitem() const
695 if (!insetlist.empty()) {
696 InsetBase * inset = insetlist.begin()->inset;
697 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
698 return static_cast<InsetBibitem *>(inset);
704 bool Paragraph::forceDefaultParagraphs() const
706 return inInset() && inInset()->forceDefaultParagraphs(0);
712 // paragraphs inside floats need different alignment tags to avoid
715 bool noTrivlistCentering(InsetBase::Code code)
717 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
721 string correction(string const & orig)
723 if (orig == "flushleft")
724 return "raggedright";
725 if (orig == "flushright")
727 if (orig == "center")
733 string const corrected_env(string const & suffix, string const & env,
734 InsetBase::Code code)
736 string output = suffix + "{";
737 if (noTrivlistCentering(code))
738 output += correction(env);
742 if (suffix == "\\begin")
748 int adjust_column_count(string const & str, int oldcol)
750 if (!contains(str, "\n"))
751 return oldcol + str.size();
754 return rsplit(str, tmp, '\n').size();
761 // This could go to ParagraphParameters if we want to
762 int Paragraph::startTeXParParams(BufferParams const & bparams,
763 odocstream & os, bool moving_arg) const
767 if (params().noindent()) {
772 switch (params().align()) {
774 case LYX_ALIGN_BLOCK:
775 case LYX_ALIGN_LAYOUT:
776 case LYX_ALIGN_SPECIAL:
779 case LYX_ALIGN_RIGHT:
780 case LYX_ALIGN_CENTER:
788 switch (params().align()) {
790 case LYX_ALIGN_BLOCK:
791 case LYX_ALIGN_LAYOUT:
792 case LYX_ALIGN_SPECIAL:
794 case LYX_ALIGN_LEFT: {
796 if (getParLanguage(bparams)->babel() != "hebrew")
797 output = corrected_env("\\begin", "flushleft", ownerCode());
799 output = corrected_env("\\begin", "flushright", ownerCode());
800 os << from_ascii(output);
801 column = adjust_column_count(output, column);
803 } case LYX_ALIGN_RIGHT: {
805 if (getParLanguage(bparams)->babel() != "hebrew")
806 output = corrected_env("\\begin", "flushright", ownerCode());
808 output = corrected_env("\\begin", "flushleft", ownerCode());
809 os << from_ascii(output);
810 column = adjust_column_count(output, column);
812 } case LYX_ALIGN_CENTER: {
814 output = corrected_env("\\begin", "center", ownerCode());
815 os << from_ascii(output);
816 column = adjust_column_count(output, column);
825 // This could go to ParagraphParameters if we want to
826 int Paragraph::endTeXParParams(BufferParams const & bparams,
827 odocstream & os, bool moving_arg) const
831 switch (params().align()) {
833 case LYX_ALIGN_BLOCK:
834 case LYX_ALIGN_LAYOUT:
835 case LYX_ALIGN_SPECIAL:
838 case LYX_ALIGN_RIGHT:
839 case LYX_ALIGN_CENTER:
847 switch (params().align()) {
849 case LYX_ALIGN_BLOCK:
850 case LYX_ALIGN_LAYOUT:
851 case LYX_ALIGN_SPECIAL:
853 case LYX_ALIGN_LEFT: {
855 if (getParLanguage(bparams)->babel() != "hebrew")
856 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
858 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
859 os << from_ascii(output);
860 column = adjust_column_count(output, column);
862 } case LYX_ALIGN_RIGHT: {
864 if (getParLanguage(bparams)->babel() != "hebrew")
865 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
867 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
868 os << from_ascii(output);
869 column = adjust_column_count(output, column);
871 } case LYX_ALIGN_CENTER: {
873 output = corrected_env("\n\\par\\end", "center", ownerCode());
874 os << from_ascii(output);
875 column = adjust_column_count(output, column);
884 // This one spits out the text of the paragraph
885 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
886 BufferParams const & bparams,
887 LyXFont const & outerfont,
888 odocstream & os, TexRow & texrow,
889 OutputParams const & runparams) const
891 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
893 bool return_value = false;
897 // well we have to check if we are in an inset with unlimited
898 // length (all in one row) if that is true then we don't allow
899 // any special options in the paragraph and also we don't allow
900 // any environment other then "Standard" to be valid!
901 bool asdefault = forceDefaultParagraphs();
904 style = bparams.getLyXTextClass().defaultLayout();
911 LaTeXFeatures features(buf, bparams, runparams);
913 // output change tracking marks only if desired,
914 // if dvipost is installed,
915 // and with dvi/ps (other formats don't work)
916 bool const output = bparams.outputChanges
917 && runparams.flavor == OutputParams::LATEX
918 && features.isAvailable("dvipost");
920 // Maybe we have to create a optional argument.
921 pos_type body_pos = beginOfBody();
922 unsigned int column = 0;
925 // the optional argument is kept in curly brackets in
926 // case it contains a ']'
929 basefont = getLabelFont(bparams, outerfont);
931 basefont = getLayoutFont(bparams, outerfont);
934 // Which font is currently active?
935 LyXFont running_font(basefont);
936 // Do we have an open font change?
937 bool open_font = false;
939 Change::Type runningChangeType = Change::UNCHANGED;
941 texrow.start(id(), 0);
943 // if the paragraph is empty, the loop will not be entered at all
945 if (style->isCommand()) {
950 column += startTeXParParams(bparams, os,
951 runparams.moving_arg);
954 for (pos_type i = 0; i < size(); ++i) {
956 // First char in paragraph or after label?
960 column += running_font.latexWriteEndChanges(os, basefont, basefont);
963 basefont = getLayoutFont(bparams, outerfont);
964 running_font = basefont;
968 if (style->isCommand()) {
974 column += startTeXParParams(bparams, os,
975 runparams.moving_arg);
978 value_type c = getChar(i);
980 // Fully instantiated font
981 LyXFont font = getFont(bparams, i, outerfont);
983 LyXFont const last_font = running_font;
985 // Spaces at end of font change are simulated to be
986 // outside font change, i.e. we write "\textXX{text} "
987 // rather than "\textXX{text }". (Asger)
988 if (open_font && c == ' ' && i <= size() - 2) {
989 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
990 if (next_font != running_font && next_font != font) {
995 // We end font definition before blanks
997 (font != running_font ||
998 font.language() != running_font.language()))
1000 column += running_font.latexWriteEndChanges(os,
1002 (i == body_pos-1) ? basefont : font);
1003 running_font = basefont;
1007 // Blanks are printed before start of fontswitch
1009 // Do not print the separation of the optional argument
1010 if (i != body_pos - 1) {
1011 pimpl_->simpleTeXBlanks(os, texrow, i,
1012 column, font, *style);
1016 // Do we need to change font?
1017 if ((font != running_font ||
1018 font.language() != running_font.language()) &&
1021 column += font.latexWriteStartChanges(os, basefont,
1023 running_font = font;
1027 Change::Type changeType = pimpl_->lookupChange(i).type;
1029 column += Changes::latexMarkChange(os, runningChangeType,
1030 changeType, output);
1031 runningChangeType = changeType;
1033 // do not output text which is marked deleted
1034 // if change tracking output is not desired
1035 if (output || runningChangeType != Change::DELETED) {
1036 OutputParams rp = runparams;
1037 rp.free_spacing = style->free_spacing;
1038 rp.local_font = &font;
1039 rp.intitle = style->intitle;
1040 pimpl_->simpleTeXSpecialChars(buf, bparams,
1043 basefont, outerfont, open_font,
1045 *style, i, column, c);
1049 column += Changes::latexMarkChange(os,
1050 runningChangeType, Change::UNCHANGED, output);
1052 // If we have an open font definition, we have to close it
1054 #ifdef FIXED_LANGUAGE_END_DETECTION
1057 .latexWriteEndChanges(os, basefont,
1058 next_->getFont(bparams,
1061 running_font.latexWriteEndChanges(os, basefont,
1065 #ifdef WITH_WARNINGS
1066 //#warning For now we ALWAYS have to close the foreign font settings if they are
1067 //#warning there as we start another \selectlanguage with the next paragraph if
1068 //#warning we are in need of this. This should be fixed sometime (Jug)
1070 running_font.latexWriteEndChanges(os, basefont, basefont);
1074 // Needed if there is an optional argument but no contents.
1075 if (body_pos > 0 && body_pos == size()) {
1077 return_value = false;
1081 column += endTeXParParams(bparams, os, runparams.moving_arg);
1084 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1085 return return_value;
1091 // checks, if newcol chars should be put into this line
1092 // writes newline, if necessary.
1093 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1094 string::size_type newcol)
1097 if (colcount > lyxrc.ascii_linelen) {
1099 colcount = newcol; // assume write after this call
1114 string tag_name(PAR_TAG const & pt) {
1116 case PAR_NONE: return "!-- --";
1117 case TT: return "tt";
1118 case SF: return "sf";
1119 case BF: return "bf";
1120 case IT: return "it";
1121 case SL: return "sl";
1122 case EM: return "em";
1129 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1131 p1 = static_cast<PAR_TAG>(p1 | p2);
1136 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1138 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1144 bool Paragraph::emptyTag() const
1146 for (pos_type i = 0; i < size(); ++i) {
1148 InsetBase const * inset = getInset(i);
1149 InsetBase::Code lyx_code = inset->lyxCode();
1150 if (lyx_code != InsetBase::TOC_CODE &&
1151 lyx_code != InsetBase::INCLUDE_CODE &&
1152 lyx_code != InsetBase::GRAPHICS_CODE &&
1153 lyx_code != InsetBase::ERT_CODE &&
1154 lyx_code != InsetBase::FLOAT_CODE &&
1155 lyx_code != InsetBase::TABULAR_CODE) {
1159 value_type c = getChar(i);
1160 if (c != ' ' && c != '\t')
1168 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1170 for (pos_type i = 0; i < size(); ++i) {
1172 InsetBase const * inset = getInset(i);
1173 InsetBase::Code lyx_code = inset->lyxCode();
1174 if (lyx_code == InsetBase::LABEL_CODE) {
1175 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1176 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1185 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1188 for (i = 0; i < size(); ++i) {
1190 InsetBase const * inset = getInset(i);
1191 inset->docbook(buf, os, runparams);
1193 value_type c = getChar(i);
1196 os << sgml::escapeChar(c);
1203 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1207 for (pos_type i = initial; i < size(); ++i) {
1208 LyXFont font = getFont(buf.params(), i, outerfont);
1211 if (i != initial && font != font_old)
1220 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1222 OutputParams const & runparams,
1223 LyXFont const & outerfont,
1224 pos_type initial) const
1226 bool emph_flag = false;
1228 LyXLayout_ptr const & style = layout();
1230 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1232 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1235 // parsing main loop
1236 for (pos_type i = initial; i < size(); ++i) {
1237 LyXFont font = getFont(buf.params(), i, outerfont);
1239 // handle <emphasis> tag
1240 if (font_old.emph() != font.emph()) {
1241 if (font.emph() == LyXFont::ON) {
1244 } else if (i != initial) {
1245 os << "</emphasis>";
1251 InsetBase const * inset = getInset(i);
1252 inset->docbook(buf, os, runparams);
1254 value_type c = getChar(i);
1256 if (style->pass_thru)
1259 os << sgml::escapeChar(c);
1265 os << "</emphasis>";
1268 if (style->free_spacing)
1270 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1275 bool Paragraph::isNewline(pos_type pos) const
1278 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1282 bool Paragraph::isLineSeparator(pos_type pos) const
1284 value_type const c = getChar(pos);
1285 return isLineSeparatorChar(c)
1286 || (c == Paragraph::META_INSET && getInset(pos) &&
1287 getInset(pos)->isLineSeparator());
1291 /// Used by the spellchecker
1292 bool Paragraph::isLetter(pos_type pos) const
1295 return getInset(pos)->isLetter();
1297 value_type const c = getChar(pos);
1298 return isLetterChar(c) || isDigit(c);
1304 Paragraph::getParLanguage(BufferParams const & bparams) const
1307 return getFirstFontSettings(bparams).language();
1308 #ifdef WITH_WARNINGS
1309 #warning FIXME we should check the prev par as well (Lgb)
1311 return bparams.language;
1315 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1317 return lyxrc.rtl_support
1318 && getParLanguage(bparams)->rightToLeft()
1319 && ownerCode() != InsetBase::ERT_CODE;
1323 void Paragraph::changeLanguage(BufferParams const & bparams,
1324 Language const * from, Language const * to)
1326 // change language including dummy font change at the end
1327 for (pos_type i = 0; i <= size(); ++i) {
1328 LyXFont font = getFontSettings(bparams, i);
1329 if (font.language() == from) {
1330 font.setLanguage(to);
1337 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1339 Language const * doc_language = bparams.language;
1340 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1341 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1343 for (; cit != end; ++cit)
1344 if (cit->font().language() != ignore_language &&
1345 cit->font().language() != latex_language &&
1346 cit->font().language() != doc_language)
1352 // Convert the paragraph to a string.
1353 // Used for building the table of contents
1354 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1356 OutputParams runparams;
1357 return asString(buffer, runparams, label);
1361 docstring const Paragraph::asString(Buffer const & buffer,
1362 OutputParams const & runparams,
1367 if (label && !params().labelString().empty())
1368 s += params().labelString() + ' ';
1370 for (pos_type i = 0; i < size(); ++i) {
1371 value_type c = getChar(i);
1374 else if (c == META_INSET &&
1375 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1377 getInset(i)->plaintext(buffer, os, runparams);
1378 s += subst(STRCONV(os.str()),'\n',' ');
1384 // This should really be done by the caller and not here.
1385 docstring ret = asString(buffer, runparams, 0, size(), label);
1386 return subst(ret, '\n', ' ');
1391 docstring const Paragraph::asString(Buffer const & buffer,
1392 pos_type beg, pos_type end, bool label) const
1395 OutputParams const runparams;
1396 return asString(buffer, runparams, beg, end, label);
1400 docstring const Paragraph::asString(Buffer const & buffer,
1401 OutputParams const & runparams,
1402 pos_type beg, pos_type end, bool label) const
1404 lyx::odocstringstream os;
1406 if (beg == 0 && label && !params().labelString().empty())
1407 os << params().labelString() << ' ';
1409 for (pos_type i = beg; i < end; ++i) {
1410 value_type const c = getUChar(buffer.params(), i);
1413 else if (c == META_INSET)
1414 getInset(i)->textString(buffer, os, runparams);
1421 void Paragraph::setInsetOwner(InsetBase * inset)
1423 pimpl_->inset_owner = inset;
1427 Change const Paragraph::lookupChange(pos_type pos) const
1429 BOOST_ASSERT(pos <= size());
1430 return pimpl_->lookupChange(pos);
1434 bool Paragraph::isChanged(pos_type start, pos_type end) const
1436 return pimpl_->isChanged(start, end);
1440 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1442 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1446 void Paragraph::setChange(Change const & change)
1448 pimpl_->setChange(change);
1452 void Paragraph::setChange(pos_type pos, Change const & change)
1454 pimpl_->setChange(pos, change);
1458 void Paragraph::acceptChanges(pos_type start, pos_type end)
1460 return pimpl_->acceptChanges(start, end);
1464 void Paragraph::rejectChanges(pos_type start, pos_type end)
1466 return pimpl_->rejectChanges(start, end);
1470 int Paragraph::id() const
1476 LyXLayout_ptr const & Paragraph::layout() const
1482 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1484 layout_ = new_layout;
1488 InsetBase * Paragraph::inInset() const
1490 return pimpl_->inset_owner;
1494 InsetBase::Code Paragraph::ownerCode() const
1496 return pimpl_->inset_owner
1497 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1501 void Paragraph::clearContents()
1507 ParagraphParameters & Paragraph::params()
1509 return pimpl_->params;
1513 ParagraphParameters const & Paragraph::params() const
1515 return pimpl_->params;
1519 bool Paragraph::isFreeSpacing() const
1521 if (layout()->free_spacing)
1524 // for now we just need this, later should we need this in some
1525 // other way we can always add a function to InsetBase too.
1526 return ownerCode() == InsetBase::ERT_CODE;
1530 bool Paragraph::allowEmpty() const
1532 if (layout()->keepempty)
1534 return ownerCode() == InsetBase::ERT_CODE;
1538 Row & Paragraph::getRow(pos_type pos, bool boundary)
1540 BOOST_ASSERT(!rows().empty());
1542 // If boundary is set we should return the row on which
1543 // the character before is inside.
1544 if (pos > 0 && boundary)
1547 RowList::iterator rit = rows_.end();
1548 RowList::iterator const begin = rows_.begin();
1550 for (--rit; rit != begin && rit->pos() > pos; --rit)
1557 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1559 BOOST_ASSERT(!rows().empty());
1561 // If boundary is set we should return the row on which
1562 // the character before is inside.
1563 if (pos > 0 && boundary)
1566 RowList::const_iterator rit = rows_.end();
1567 RowList::const_iterator const begin = rows_.begin();
1569 for (--rit; rit != begin && rit->pos() > pos; --rit)
1576 size_t Paragraph::pos2row(pos_type pos) const
1578 BOOST_ASSERT(!rows().empty());
1580 RowList::const_iterator rit = rows_.end();
1581 RowList::const_iterator const begin = rows_.begin();
1583 for (--rit; rit != begin && rit->pos() > pos; --rit)
1590 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1592 if (!Encodings::is_arabic(c))
1593 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1594 // FIXME UNICODE What does this do?
1595 return c + (0xb0 - '0');
1599 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1600 value_type next_char = ' ';
1602 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1603 value_type const par_char = getChar(i);
1604 if (!Encodings::isComposeChar_arabic(par_char)) {
1605 next_char = par_char;
1610 if (Encodings::is_arabic(next_char)) {
1611 if (Encodings::is_arabic(prev_char) &&
1612 !Encodings::is_arabic_special(prev_char))
1613 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1615 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1617 if (Encodings::is_arabic(prev_char) &&
1618 !Encodings::is_arabic_special(prev_char))
1619 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1621 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1626 void Paragraph::dump() const
1628 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1629 for (size_t i = 0; i != rows_.size(); ++i) {
1630 lyxerr << " row " << i << ": ";
1636 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1641 // at the end of a row it does not count
1642 // unless another hfill exists on the line
1643 if (pos >= row.endpos()) {
1644 for (pos_type i = row.pos(); i < pos && !isHfill(i); ++i)
1648 // at the beginning of a row it does not count, if it is not
1649 // the first row of a paragaph
1653 // in some labels it does not count
1654 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1657 // if there is anything between the first char of the row and
1658 // the specified position that is not a newline and not a hfill,
1659 // the hfill will count, otherwise not
1660 pos_type i = row.pos();
1661 while (i < pos && (isNewline(i) || isHfill(i)))