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"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetoptarg.h"
42 #include "support/lstrings.h"
43 #include "support/std_sstream.h"
44 #include "support/textutils.h"
45 #include "support/tostr.h"
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
55 using lyx::support::contains;
56 using lyx::support::subst;
63 using std::ostringstream;
66 Paragraph::Paragraph()
67 : y(0), height(0), begin_of_body_(0),
68 pimpl_(new Paragraph::Pimpl(this))
75 Paragraph::Paragraph(Paragraph const & par)
76 : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
77 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
80 // this is because of the dummy layout of the paragraphs that
82 layout_ = par.layout();
84 // copy everything behind the break-position to the new paragraph
85 insetlist = par.insetlist;
86 InsetList::iterator it = insetlist.begin();
87 InsetList::iterator end = insetlist.end();
88 for (; it != end; ++it)
89 it->inset = it->inset->clone().release();
93 void Paragraph::operator=(Paragraph const & par)
95 // needed as we will destroy the pimpl_ before copying it
99 lyxerr << "Paragraph::operator=()" << endl;
104 pimpl_ = new Pimpl(*par.pimpl_, this);
106 itemdepth = par.itemdepth;
107 // this is because of the dummy layout of the paragraphs that
109 layout_ = par.layout();
111 // copy everything behind the break-position to the new paragraph
112 insetlist = par.insetlist;
113 InsetList::iterator it = insetlist.begin();
114 InsetList::iterator end = insetlist.end();
115 for (; it != end; ++it)
116 it->inset = it->inset->clone().release();
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);
156 lyx::time_type const curtime(lyx::current_time());
159 for (pos_type i = 0; i < size(); ++i) {
165 Change change = pimpl_->lookupChangeFull(i);
166 Changes::lyxMarkChange(os, column, curtime, running_change, change);
167 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);
189 os << "\n\\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.
218 lyxerr << "ERROR (Paragraph::writeFile):"
219 " NULL char in structure." << endl;
225 // to make reading work properly
227 running_change = pimpl_->lookupChange(0);
228 Changes::lyxMarkChange(os, column, curtime,
229 Change(Change::UNCHANGED), running_change);
231 Changes::lyxMarkChange(os, column, curtime,
232 running_change, Change(Change::UNCHANGED));
234 os << "\n\\end_layout\n";
238 void Paragraph::validate(LaTeXFeatures & features) const
240 pimpl_->validate(features, *layout());
244 void Paragraph::eraseIntern(lyx::pos_type pos)
246 pimpl_->eraseIntern(pos);
250 bool Paragraph::erase(pos_type pos)
252 return pimpl_->erase(pos);
256 int Paragraph::erase(pos_type start, pos_type end)
258 return pimpl_->erase(start, end);
262 void Paragraph::insert(pos_type start, string const & str,
263 LyXFont const & font)
265 int size = str.size();
266 for (int i = 0 ; i < size ; ++i)
267 insertChar(start + i, str[i], font);
271 bool Paragraph::checkInsertChar(LyXFont &)
277 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
279 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
283 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
284 LyXFont const & font, Change change)
286 pimpl_->insertChar(pos, c, font, change);
290 void Paragraph::insertInset(pos_type pos, InsetBase * inset)
292 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
296 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
297 LyXFont const & font, Change change)
299 pimpl_->insertInset(pos, inset, font, change);
303 bool Paragraph::insetAllowed(InsetOld_code code)
305 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
306 if (pimpl_->inset_owner)
307 return pimpl_->inset_owner->insetAllowed(code);
312 InsetBase * Paragraph::getInset(pos_type pos)
314 BOOST_ASSERT(pos < size());
315 return insetlist.get(pos);
319 InsetBase const * Paragraph::getInset(pos_type pos) const
321 BOOST_ASSERT(pos < size());
322 return insetlist.get(pos);
326 // Gets uninstantiated font setting at position.
327 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
330 BOOST_ASSERT(pos <= size());
332 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
333 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
334 for (; cit != end; ++cit)
335 if (cit->pos() >= pos)
341 if (pos == size() && !empty())
342 return getFontSettings(bparams, pos - 1);
344 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
348 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
350 BOOST_ASSERT(pos <= size());
352 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
353 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
354 for (; cit != end; ++cit)
355 if (cit->pos() >= pos)
358 // This should not happen, but if so, we take no chances.
359 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
365 // Gets uninstantiated font setting at position 0
366 LyXFont const Paragraph::getFirstFontSettings() const
368 if (!empty() && !pimpl_->fontlist.empty())
369 return pimpl_->fontlist[0].font();
371 return LyXFont(LyXFont::ALL_INHERIT);
375 // Gets the fully instantiated font at a given position in a paragraph
376 // This is basically the same function as LyXText::GetFont() in text2.C.
377 // The difference is that this one is used for generating the LaTeX file,
378 // and thus cosmetic "improvements" are disallowed: This has to deliver
379 // the true picture of the buffer. (Asger)
380 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
381 LyXFont const & outerfont) const
383 BOOST_ASSERT(pos >= 0);
385 LyXLayout_ptr const & lout = layout();
387 pos_type const body_pos = beginOfBody();
391 layoutfont = lout->labelfont;
393 layoutfont = lout->font;
395 LyXFont font = getFontSettings(bparams, pos);
396 font.realize(layoutfont);
397 font.realize(outerfont);
398 font.realize(bparams.getLyXTextClass().defaultfont());
404 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
405 LyXFont const & outerfont) const
407 LyXFont tmpfont = layout()->labelfont;
408 tmpfont.setLanguage(getParLanguage(bparams));
409 tmpfont.realize(outerfont);
410 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
415 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
416 LyXFont const & outerfont) const
418 LyXFont tmpfont = layout()->font;
419 tmpfont.setLanguage(getParLanguage(bparams));
420 tmpfont.realize(outerfont);
421 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
426 /// Returns the height of the highest font in range
428 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
429 LyXFont_size def_size) const
431 if (pimpl_->fontlist.empty())
434 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
435 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
436 for (; end_it != end; ++end_it) {
437 if (end_it->pos() >= endpos)
444 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
445 for (; cit != end; ++cit) {
446 if (cit->pos() >= startpos)
450 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
451 for (; cit != end_it; ++cit) {
452 LyXFont::FONT_SIZE size = cit->font().size();
453 if (size == LyXFont::INHERIT_SIZE)
455 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
462 Paragraph::value_type
463 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
465 value_type c = getChar(pos);
466 if (!lyxrc.rtl_support)
496 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
503 void Paragraph::setFont(pos_type pos, LyXFont const & font)
505 BOOST_ASSERT(pos <= size());
507 // First, reduce font against layout/label font
508 // Update: The setCharFont() routine in text2.C already
509 // reduces font, so we don't need to do that here. (Asger)
510 // No need to simplify this because it will disappear
511 // in a new kernel. (Asger)
512 // Next search font table
514 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
515 Pimpl::FontList::iterator it = beg;
516 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
517 for (; it != endit; ++it) {
518 if (it->pos() >= pos)
521 unsigned int i = std::distance(beg, it);
522 bool notfound = (it == endit);
524 if (!notfound && pimpl_->fontlist[i].font() == font)
527 bool begin = pos == 0 || notfound ||
528 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
529 // Is position pos is a beginning of a font block?
530 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
531 // Is position pos is the end of a font block?
532 if (begin && end) { // A single char block
533 if (i + 1 < pimpl_->fontlist.size() &&
534 pimpl_->fontlist[i + 1].font() == font) {
535 // Merge the singleton block with the next block
536 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
537 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
538 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
539 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
540 // Merge the singleton block with the previous block
541 pimpl_->fontlist[i - 1].pos(pos);
542 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
544 pimpl_->fontlist[i].font(font);
546 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
547 pimpl_->fontlist[i - 1].pos(pos);
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
550 Pimpl::FontTable(pos, font));
552 pimpl_->fontlist[i].pos(pos - 1);
553 if (!(i + 1 < pimpl_->fontlist.size() &&
554 pimpl_->fontlist[i + 1].font() == font))
555 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
556 Pimpl::FontTable(pos, font));
557 } else { // The general case. The block is splitted into 3 blocks
558 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
559 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
560 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
561 Pimpl::FontTable(pos, font));
566 void Paragraph::makeSameLayout(Paragraph const & par)
568 layout(par.layout());
570 params() = par.params();
574 int Paragraph::stripLeadingSpaces()
580 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
581 pimpl_->eraseIntern(0);
589 bool Paragraph::hasSameLayout(Paragraph const & par) const
591 return par.layout() == layout() && params().sameLayout(par.params());
595 Paragraph::depth_type Paragraph::getDepth() const
597 return params().depth();
601 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
603 if (layout()->isEnvironment())
604 return params().depth() + 1;
606 return params().depth();
610 char Paragraph::getAlign() const
612 return params().align();
616 string const & Paragraph::getLabelstring() const
618 return params().labelString();
622 // the next two functions are for the manual labels
623 string const Paragraph::getLabelWidthString() const
625 if (!params().labelWidthString().empty())
626 return params().labelWidthString();
628 return _("Senseless with this layout!");
632 void Paragraph::setLabelWidthString(string const & s)
634 params().labelWidthString(s);
638 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
641 params().labelWidthString(string());
642 params().align(LYX_ALIGN_LAYOUT);
643 params().spacing(Spacing(Spacing::Default));
647 int Paragraph::beginOfBody() const
649 return begin_of_body_;
653 void Paragraph::setBeginOfBody()
655 if (layout()->labeltype != LABEL_MANUAL) {
660 // Unroll the first two cycles of the loop
661 // and remember the previous character to
662 // remove unnecessary getChar() calls
664 pos_type end = size();
665 if (i < end && !isNewline(i)) {
667 char previous_char = 0;
670 previous_char = text_[i];
673 while (i < end && previous_char != ' ') {
678 previous_char = temp;
688 // returns -1 if inset not found
689 int Paragraph::getPositionOfInset(InsetBase const * inset) const
692 InsetList::const_iterator it = insetlist.begin();
693 InsetList::const_iterator end = insetlist.end();
694 for (; it != end; ++it)
695 if (it->inset == inset)
701 InsetBibitem * Paragraph::bibitem() const
703 if (!insetlist.empty()) {
704 InsetBase * inset = insetlist.begin()->inset;
705 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
706 return static_cast<InsetBibitem *>(inset);
714 // paragraphs inside floats need different alignment tags to avoid
717 bool noTrivlistCentering(UpdatableInset const * inset)
719 if (inset && inset->owner()) {
720 InsetBase::Code const code = inset->owner()->lyxCode();
721 return code == InsetBase::FLOAT_CODE ||
722 code == InsetBase::WRAP_CODE;
728 string correction(string const & orig)
730 if (orig == "flushleft")
731 return "raggedright";
732 if (orig == "flushright")
734 if (orig == "center")
740 string const corrected_env(string const & suffix, string const & env,
741 UpdatableInset const * inset)
743 string output = suffix + "{";
744 if (noTrivlistCentering(inset))
745 output += correction(env);
754 // This could go to ParagraphParameters if we want to
755 int Paragraph::startTeXParParams(BufferParams const & bparams,
756 ostream & os, bool moving_arg) const
760 if (params().noindent()) {
765 switch (params().align()) {
767 case LYX_ALIGN_BLOCK:
768 case LYX_ALIGN_LAYOUT:
769 case LYX_ALIGN_SPECIAL:
772 case LYX_ALIGN_RIGHT:
773 case LYX_ALIGN_CENTER:
781 switch (params().align()) {
783 case LYX_ALIGN_BLOCK:
784 case LYX_ALIGN_LAYOUT:
785 case LYX_ALIGN_SPECIAL:
787 case LYX_ALIGN_LEFT: {
789 UpdatableInset const * const inset = pimpl_->inset_owner;
790 if (getParLanguage(bparams)->babel() != "hebrew")
791 output = corrected_env("\\begin", "flushleft", inset);
793 output = corrected_env("\\begin", "flushright", inset);
795 column += output.size();
797 } case LYX_ALIGN_RIGHT: {
799 UpdatableInset const * const inset = pimpl_->inset_owner;
800 if (getParLanguage(bparams)->babel() != "hebrew")
801 output = corrected_env("\\begin", "flushright", inset);
803 output = corrected_env("\\begin", "flushleft", inset);
805 column += output.size();
807 } case LYX_ALIGN_CENTER: {
809 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
811 column += output.size();
820 // This could go to ParagraphParameters if we want to
821 int Paragraph::endTeXParParams(BufferParams const & bparams,
822 ostream & os, bool moving_arg) const
826 switch (params().align()) {
828 case LYX_ALIGN_BLOCK:
829 case LYX_ALIGN_LAYOUT:
830 case LYX_ALIGN_SPECIAL:
833 case LYX_ALIGN_RIGHT:
834 case LYX_ALIGN_CENTER:
842 switch (params().align()) {
844 case LYX_ALIGN_BLOCK:
845 case LYX_ALIGN_LAYOUT:
846 case LYX_ALIGN_SPECIAL:
848 case LYX_ALIGN_LEFT: {
850 UpdatableInset const * const inset = pimpl_->inset_owner;
851 if (getParLanguage(bparams)->babel() != "hebrew")
852 output = corrected_env("\\par\\end", "flushleft", inset);
854 output = corrected_env("\\par\\end", "flushright", inset);
856 column += output.size();
858 } case LYX_ALIGN_RIGHT: {
860 UpdatableInset const * const inset = pimpl_->inset_owner;
861 if (getParLanguage(bparams)->babel() != "hebrew")
862 output = corrected_env("\\par\\end", "flushright", inset);
864 output = corrected_env("\\par\\end", "flushleft", inset);
866 column += output.size();
868 } case LYX_ALIGN_CENTER: {
870 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
872 column += output.size();
881 // This one spits out the text of the paragraph
882 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
883 BufferParams const & bparams,
884 LyXFont const & outerfont,
885 ostream & os, TexRow & texrow,
886 OutputParams const & runparams)
888 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
890 bool return_value = false;
894 // well we have to check if we are in an inset with unlimited
895 // length (all in one row) if that is true then we don't allow
896 // any special options in the paragraph and also we don't allow
897 // any environment other then "Standard" to be valid!
899 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
902 style = bparams.getLyXTextClass().defaultLayout();
909 // Maybe we have to create a optional argument.
910 pos_type body_pos = beginOfBody();
911 unsigned int column = 0;
916 basefont = getLabelFont(bparams, outerfont);
918 basefont = getLayoutFont(bparams, outerfont);
921 bool moving_arg = runparams.moving_arg;
922 moving_arg |= style->needprotect;
924 // Which font is currently active?
925 LyXFont running_font(basefont);
926 // Do we have an open font change?
927 bool open_font = false;
929 Change::Type running_change = Change::UNCHANGED;
931 texrow.start(id(), 0);
933 // if the paragraph is empty, the loop will not be entered at all
935 if (style->isCommand()) {
940 column += startTeXParParams(bparams, os, moving_arg);
943 for (pos_type i = 0; i < size(); ++i) {
945 // First char in paragraph or after label?
949 column += running_font.latexWriteEndChanges(os, basefont, basefont);
952 basefont = getLayoutFont(bparams, outerfont);
953 running_font = basefont;
957 if (style->isCommand()) {
963 column += startTeXParParams(bparams, os,
967 value_type c = getChar(i);
969 // Fully instantiated font
970 LyXFont font = getFont(bparams, i, outerfont);
972 LyXFont const last_font = running_font;
974 // Spaces at end of font change are simulated to be
975 // outside font change, i.e. we write "\textXX{text} "
976 // rather than "\textXX{text }". (Asger)
977 if (open_font && c == ' ' && i <= size() - 2) {
978 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
979 if (next_font != running_font && next_font != font) {
984 // We end font definition before blanks
986 (font != running_font ||
987 font.language() != running_font.language()))
989 column += running_font.latexWriteEndChanges(os,
991 (i == body_pos-1) ? basefont : font);
992 running_font = basefont;
996 // Blanks are printed before start of fontswitch
998 // Do not print the separation of the optional argument
999 if (i != body_pos - 1) {
1000 pimpl_->simpleTeXBlanks(os, texrow, i,
1001 column, font, *style);
1005 // Do we need to change font?
1006 if ((font != running_font ||
1007 font.language() != running_font.language()) &&
1010 column += font.latexWriteStartChanges(os, basefont,
1012 running_font = font;
1016 Change::Type change = pimpl_->lookupChange(i);
1018 column += Changes::latexMarkChange(os, running_change, change);
1019 running_change = change;
1021 OutputParams rp = runparams;
1022 rp.moving_arg = moving_arg;
1023 rp.free_spacing = style->free_spacing;
1024 pimpl_->simpleTeXSpecialChars(buf, bparams,
1025 os, texrow, runparams,
1027 basefont, outerfont, open_font,
1029 *style, i, column, c);
1032 column += Changes::latexMarkChange(os,
1033 running_change, Change::UNCHANGED);
1035 // If we have an open font definition, we have to close it
1037 #ifdef FIXED_LANGUAGE_END_DETECTION
1040 .latexWriteEndChanges(os, basefont,
1041 next_->getFont(bparams,
1044 running_font.latexWriteEndChanges(os, basefont,
1048 #ifdef WITH_WARNINGS
1049 //#warning For now we ALWAYS have to close the foreign font settings if they are
1050 //#warning there as we start another \selectlanguage with the next paragraph if
1051 //#warning we are in need of this. This should be fixed sometime (Jug)
1053 running_font.latexWriteEndChanges(os, basefont, basefont);
1057 // Needed if there is an optional argument but no contents.
1058 if (body_pos > 0 && body_pos == size()) {
1060 return_value = false;
1064 column += endTeXParParams(bparams, os, moving_arg);
1067 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1068 return return_value;
1074 // checks, if newcol chars should be put into this line
1075 // writes newline, if necessary.
1076 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1077 string::size_type newcol)
1080 if (colcount > lyxrc.ascii_linelen) {
1082 colcount = newcol; // assume write after this call
1097 string tag_name(PAR_TAG const & pt) {
1099 case PAR_NONE: return "!-- --";
1100 case TT: return "tt";
1101 case SF: return "sf";
1102 case BF: return "bf";
1103 case IT: return "it";
1104 case SL: return "sl";
1105 case EM: return "em";
1112 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1114 p1 = static_cast<PAR_TAG>(p1 | p2);
1119 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1121 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1127 // Handle internal paragraph parsing -- layout already processed.
1128 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1130 LyXFont const & outerfont,
1131 OutputParams const & runparams,
1132 lyx::depth_type /*depth*/) const
1134 LyXLayout_ptr const & style = layout();
1136 string::size_type char_line_count = 5; // Heuristic choice ;-)
1138 // gets paragraph main font
1141 if (style->labeltype == LABEL_MANUAL) {
1142 font_old = style->labelfont;
1145 font_old = style->font;
1149 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1150 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1151 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1154 stack<PAR_TAG> tag_state;
1155 // parsing main loop
1156 for (pos_type i = 0; i < size(); ++i) {
1158 PAR_TAG tag_close = PAR_NONE;
1159 list < PAR_TAG > tag_open;
1161 LyXFont const font = getFont(buf.params(), i, outerfont);
1163 if (font_old.family() != font.family()) {
1164 switch (family_type) {
1165 case LyXFont::SANS_FAMILY:
1168 case LyXFont::TYPEWRITER_FAMILY:
1175 family_type = font.family();
1177 switch (family_type) {
1178 case LyXFont::SANS_FAMILY:
1179 tag_open.push_back(SF);
1181 case LyXFont::TYPEWRITER_FAMILY:
1182 tag_open.push_back(TT);
1189 if (font_old.series() != font.series()) {
1190 switch (series_type) {
1191 case LyXFont::BOLD_SERIES:
1198 series_type = font.series();
1200 switch (series_type) {
1201 case LyXFont::BOLD_SERIES:
1202 tag_open.push_back(BF);
1210 if (font_old.shape() != font.shape()) {
1211 switch (shape_type) {
1212 case LyXFont::ITALIC_SHAPE:
1215 case LyXFont::SLANTED_SHAPE:
1222 shape_type = font.shape();
1224 switch (shape_type) {
1225 case LyXFont::ITALIC_SHAPE:
1226 tag_open.push_back(IT);
1228 case LyXFont::SLANTED_SHAPE:
1229 tag_open.push_back(SL);
1236 if (font_old.emph() != font.emph()) {
1237 if (font.emph() == LyXFont::ON) {
1238 tag_open.push_back(EM);
1247 list < PAR_TAG > temp;
1248 while (!tag_state.empty() && tag_close) {
1249 PAR_TAG k = tag_state.top();
1251 os << "</" << tag_name(k) << '>';
1258 for(list< PAR_TAG >::const_iterator j = temp.begin();
1259 j != temp.end(); ++j) {
1261 os << '<' << tag_name(*j) << '>';
1264 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1265 j != tag_open.end(); ++j) {
1267 os << '<' << tag_name(*j) << '>';
1270 char c = getChar(i);
1273 if (c == Paragraph::META_INSET) {
1274 getInset(i)->linuxdoc(buf, os, runparams);
1279 if (style->latexparam() == "CDATA") {
1280 // "TeX"-Mode on == > SGML-Mode on.
1287 boost::tie(ws, str) = sgml::escapeChar(c);
1288 if (ws && !isFreeSpacing()) {
1289 // in freespacing mode, spaces are
1290 // non-breaking characters
1291 if (desc_on) { // if char is ' ' then...
1293 sgmlLineBreak(os, char_line_count, 6);
1297 sgmlLineBreak(os, char_line_count, 1);
1302 char_line_count += str.length();
1308 while (!tag_state.empty()) {
1309 os << "</" << tag_name(tag_state.top()) << '>';
1313 // resets description flag correctly
1315 // <tag> not closed...
1316 sgmlLineBreak(os, char_line_count, 6);
1322 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1324 LyXFont const & outerfont,
1325 OutputParams const & runparams,
1326 lyx::depth_type depth,
1329 bool emph_flag = false;
1331 LyXLayout_ptr const & style = layout();
1332 LyXLayout_ptr const & defaultstyle =
1333 buf.params().getLyXTextClass().defaultLayout();
1336 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1338 int char_line_count = depth;
1339 bool label_closed = true;
1340 bool para_closed = true;
1342 if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1344 Counters & counters = buf.params().getLyXTextClass().counters();
1345 if (!style->free_spacing)
1346 os << string(depth,' ');
1347 if (!style->labeltag().empty()) {
1348 os << "<" << style->labeltag() << ">\n";
1349 label_closed = false;
1351 if (!defaultstyle->latexparam().empty()) {
1352 counters.step("para");
1353 ls = tostr(counters.value("para"));
1355 + subst(defaultstyle->latexparam(), "#", ls) + '"';
1357 os << "<" << style->itemtag() << ">\n"
1358 << string(depth, ' ') << "<"
1359 << defaultstyle->latexname() << ls << ">\n";
1360 para_closed = false;
1364 // parsing main loop
1365 for (pos_type i = 0; i < size(); ++i) {
1366 LyXFont font = getFont(buf.params(), i, outerfont);
1368 // handle <emphasis> tag
1369 if (font_old.emph() != font.emph()) {
1370 if (font.emph() == LyXFont::ON) {
1371 if (style->latexparam() == "CDATA")
1374 if (style->latexparam() == "CDATA")
1378 if (style->latexparam() == "CDATA")
1380 os << "</emphasis>";
1381 if (style->latexparam() == "CDATA")
1389 InsetBase const * inset = getInset(i);
1390 // don't print the inset in position 0 if desc_on == 3 (label)
1391 //if (i || desc_on != 3) {
1393 if (style->latexparam() == "CDATA")
1395 inset->docbook(buf, os, runparams);
1396 if (style->latexparam() == "CDATA")
1400 char c = getChar(i);
1403 boost::tie(ws, str) = sgml::escapeChar(c);
1405 if (style->pass_thru) {
1407 } else if (isFreeSpacing() || c != ' ') {
1409 } else if (!style->labeltag().empty() && !label_closed) {
1411 os << "\n</" << style->labeltag() << "><"
1412 << style->itemtag() << "><"
1413 << defaultstyle->latexname() << ">";
1414 label_closed = true;
1415 para_closed = false;
1424 if (style->latexparam() == "CDATA")
1426 os << "</emphasis>";
1427 if (style->latexparam() == "CDATA")
1431 // resets description flag correctly
1432 if (!label_closed) {
1433 // <term> not closed...
1434 os << "</" << style->labeltag() << ">\n<"
1435 << style->itemtag() << "><"
1436 << defaultstyle->latexname() << "> ";
1439 os << "\n" << string(depth, ' ') << "</"
1440 << defaultstyle->latexname() << ">\n";
1442 if (style->free_spacing)
1449 /// return true if the char is a meta-character for an inset
1451 bool IsInsetChar(char c)
1453 return (c == Paragraph::META_INSET);
1460 bool Paragraph::isHfill(pos_type pos) const
1462 return IsInsetChar(getChar(pos))
1463 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1467 bool Paragraph::isInset(pos_type pos) const
1469 return IsInsetChar(getChar(pos));
1473 bool Paragraph::isNewline(pos_type pos) const
1475 return IsInsetChar(getChar(pos))
1476 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1480 bool Paragraph::isSeparator(pos_type pos) const
1482 return IsSeparatorChar(getChar(pos));
1486 bool Paragraph::isLineSeparator(pos_type pos) const
1488 value_type const c = getChar(pos);
1489 return IsLineSeparatorChar(c)
1490 || (IsInsetChar(c) && getInset(pos) &&
1491 getInset(pos)->isLineSeparator());
1495 bool Paragraph::isKomma(pos_type pos) const
1497 return IsKommaChar(getChar(pos));
1501 /// Used by the spellchecker
1502 bool Paragraph::isLetter(pos_type pos) const
1504 value_type const c = getChar(pos);
1505 if (IsLetterChar(c))
1508 return getInset(pos)->isLetter();
1509 // We want to pass the ' and escape chars to ispell
1510 string const extra = lyxrc.isp_esc_chars + '\'';
1511 return contains(extra, c);
1515 bool Paragraph::isWord(pos_type pos) const
1517 unsigned char const c = getChar(pos);
1518 return !(IsSeparatorChar(c)
1525 Paragraph::getParLanguage(BufferParams const & bparams) const
1528 return getFirstFontSettings().language();
1529 #warning FIXME we should check the prev par as well (Lgb)
1530 return bparams.language;
1534 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1536 return lyxrc.rtl_support
1537 && getParLanguage(bparams)->RightToLeft()
1538 && !(inInset() && inInset()->owner() &&
1539 inInset()->owner()->lyxCode() == InsetBase::ERT_CODE);
1543 void Paragraph::changeLanguage(BufferParams const & bparams,
1544 Language const * from, Language const * to)
1546 for (pos_type i = 0; i < size(); ++i) {
1547 LyXFont font = getFontSettings(bparams, i);
1548 if (font.language() == from) {
1549 font.setLanguage(to);
1556 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1558 Language const * doc_language = bparams.language;
1559 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1560 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1562 for (; cit != end; ++cit)
1563 if (cit->font().language() != ignore_language &&
1564 cit->font().language() != latex_language &&
1565 cit->font().language() != doc_language)
1571 // Convert the paragraph to a string.
1572 // Used for building the table of contents
1573 string const Paragraph::asString(Buffer const & buffer,
1576 OutputParams runparams;
1577 return asString(buffer, runparams, label);
1581 string const Paragraph::asString(Buffer const & buffer,
1582 OutputParams const & runparams,
1587 if (label && !params().labelString().empty())
1588 s += params().labelString() + ' ';
1590 for (pos_type i = 0; i < size(); ++i) {
1591 value_type c = getChar(i);
1594 else if (c == META_INSET &&
1595 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1597 getInset(i)->plaintext(buffer, os, runparams);
1598 s += subst(STRCONV(os.str()),'\n',' ');
1604 // This should really be done by the caller and not here.
1605 string ret = asString(buffer, runparams, 0, size(), label);
1606 return subst(ret, '\n', ' ');
1611 string const Paragraph::asString(Buffer const & buffer,
1612 pos_type beg, pos_type end, bool label) const
1615 OutputParams const runparams;
1616 return asString(buffer, runparams, beg, end, label);
1620 string const Paragraph::asString(Buffer const & buffer,
1621 OutputParams const & runparams,
1622 pos_type beg, pos_type end, bool label) const
1626 if (beg == 0 && label && !params().labelString().empty())
1627 os << params().labelString() << ' ';
1629 for (pos_type i = beg; i < end; ++i) {
1630 value_type const c = getUChar(buffer.params(), i);
1633 else if (c == META_INSET)
1634 getInset(i)->plaintext(buffer, os, runparams);
1641 void Paragraph::setInsetOwner(UpdatableInset * inset)
1643 pimpl_->inset_owner = inset;
1644 InsetList::iterator it = insetlist.begin();
1645 InsetList::iterator end = insetlist.end();
1646 for (; it != end; ++it)
1648 it->inset->setOwner(inset);
1652 void Paragraph::setContentsFromPar(Paragraph const & par)
1654 pimpl_->setContentsFromPar(par);
1658 void Paragraph::trackChanges(Change::Type type)
1660 pimpl_->trackChanges(type);
1664 void Paragraph::untrackChanges()
1666 pimpl_->untrackChanges();
1670 void Paragraph::cleanChanges()
1672 pimpl_->cleanChanges();
1676 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1678 BOOST_ASSERT(!size() || pos < size());
1679 return pimpl_->lookupChange(pos);
1683 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1685 BOOST_ASSERT(!size() || pos < size());
1686 return pimpl_->lookupChangeFull(pos);
1690 bool Paragraph::isChanged(pos_type start, pos_type end) const
1692 return pimpl_->isChanged(start, end);
1696 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1698 return pimpl_->isChangeEdited(start, end);
1702 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1704 pimpl_->setChange(pos, type);
1709 void Paragraph::markErased()
1711 pimpl_->markErased();
1715 void Paragraph::acceptChange(pos_type start, pos_type end)
1717 return pimpl_->acceptChange(start, end);
1721 void Paragraph::rejectChange(pos_type start, pos_type end)
1723 return pimpl_->rejectChange(start, end);
1727 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1729 // This is in the critical path!
1730 pos_type const siz = text_.size();
1732 BOOST_ASSERT(0 <= pos);
1733 BOOST_ASSERT(pos <= siz);
1736 lyxerr << "getChar() on pos " << pos << " in par id "
1737 << id() << " of size " << siz
1738 << " is a bit silly !" << endl;
1739 BOOST_ASSERT(false);
1747 int Paragraph::id() const
1753 LyXLayout_ptr const & Paragraph::layout() const
1756 InsetBase * inset = inInset();
1757 if (inset && inset->lyxCode() == InsetBase::ENVIRONMENT_CODE)
1758 return static_cast<InsetEnvironment*>(inset)->layout();
1764 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1766 layout_ = new_layout;
1770 UpdatableInset * Paragraph::inInset() const
1772 return pimpl_->inset_owner;
1776 void Paragraph::clearContents()
1782 void Paragraph::setChar(pos_type pos, value_type c)
1788 ParagraphParameters & Paragraph::params()
1790 return pimpl_->params;
1794 ParagraphParameters const & Paragraph::params() const
1796 return pimpl_->params;
1800 bool Paragraph::isFreeSpacing() const
1802 if (layout()->free_spacing)
1805 // for now we just need this, later should we need this in some
1806 // other way we can always add a function to InsetBase too.
1807 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1808 return pimpl_->inset_owner->owner()->lyxCode() == InsetBase::ERT_CODE;
1813 bool Paragraph::allowEmpty() const
1815 if (layout()->keepempty)
1817 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1818 return pimpl_->inset_owner->owner()->lyxCode() == InsetBase::ERT_CODE;
1823 RowList::iterator Paragraph::getRow(pos_type pos)
1825 RowList::iterator rit = rows.end();
1826 RowList::iterator const begin = rows.begin();
1828 for (--rit; rit != begin && rit->pos() > pos; --rit)
1835 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1837 if (!Encodings::is_arabic(c))
1838 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1839 return c + (0xb0 - '0');
1843 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1844 unsigned char next_char = ' ';
1846 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1847 unsigned char const par_char = getChar(i);
1848 if (!Encodings::IsComposeChar_arabic(par_char)) {
1849 next_char = par_char;
1854 if (Encodings::is_arabic(next_char)) {
1855 if (Encodings::is_arabic(prev_char) &&
1856 !Encodings::is_arabic_special(prev_char))
1857 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1859 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1861 if (Encodings::is_arabic(prev_char) &&
1862 !Encodings::is_arabic_special(prev_char))
1863 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1865 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);