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"
35 #include "ParagraphList_fwd.h"
40 #include "insets/insetbibitem.h"
41 #include "insets/insetoptarg.h"
43 #include "support/lstrings.h"
44 #include "support/textutils.h"
45 #include "support/convert.h"
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
56 using lyx::support::subst;
64 using std::ostringstream;
67 ParagraphList::ParagraphList()
71 Paragraph::Paragraph()
72 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
79 Paragraph::Paragraph(Paragraph const & par)
80 : itemdepth(par.itemdepth), insetlist(par.insetlist),
82 rows_(par.rows_), layout_(par.layout_),
83 text_(par.text_), begin_of_body_(par.begin_of_body_),
84 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
86 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
87 InsetList::iterator it = insetlist.begin();
88 InsetList::iterator end = insetlist.end();
89 for (; it != end; ++it)
90 it->inset = it->inset->clone().release();
94 Paragraph & Paragraph::operator=(Paragraph const & par)
96 // needed as we will destroy the pimpl_ before copying it
98 itemdepth = par.itemdepth;
100 insetlist = par.insetlist;
101 InsetList::iterator it = insetlist.begin();
102 InsetList::iterator end = insetlist.end();
103 for (; it != end; ++it)
104 it->inset = it->inset->clone().release();
108 layout_ = par.layout();
110 begin_of_body_ = par.begin_of_body_;
113 pimpl_ = new Pimpl(*par.pimpl_, this);
119 Paragraph::~Paragraph()
123 //lyxerr << "Paragraph::paragraph_id = "
124 // << Paragraph::paragraph_id << endl;
128 void Paragraph::write(Buffer const & buf, ostream & os,
129 BufferParams const & bparams,
130 depth_type & dth) const
132 // The beginning or end of a deeper (i.e. nested) area?
133 if (dth != params().depth()) {
134 if (params().depth() > dth) {
135 while (params().depth() > dth) {
136 os << "\n\\begin_deeper";
140 while (params().depth() < dth) {
141 os << "\n\\end_deeper";
147 // First write the layout
148 os << "\n\\begin_layout " << layout()->name() << '\n';
152 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
154 Change running_change = Change(Change::UNCHANGED);
155 lyx::time_type const curtime(lyx::current_time());
158 for (pos_type i = 0; i < size(); ++i) {
160 Change change = pimpl_->lookupChangeFull(i);
161 Changes::lyxMarkChange(os, column, curtime, running_change, change);
162 running_change = change;
164 // Write font changes
165 LyXFont font2 = getFontSettings(bparams, i);
166 if (font2 != font1) {
167 font2.lyxWriteChanges(font1, os);
172 value_type const c = getChar(i);
176 InsetBase const * inset = getInset(i);
178 if (inset->directWrite()) {
179 // international char, let it write
180 // code directly so it's shorter in
182 inset->write(buf, os);
186 os << "\\begin_inset ";
187 inset->write(buf, os);
188 os << "\n\\end_inset\n\n";
194 os << "\n\\backslash\n";
198 if (i + 1 < size() && getChar(i + 1) == ' ') {
205 if ((column > 70 && c == ' ')
210 // this check is to amend a bug. LyX sometimes
211 // inserts '\0' this could cause problems.
215 lyxerr << "ERROR (Paragraph::writeFile):"
216 " NULL char in structure." << endl;
222 // to make reading work properly
224 running_change = pimpl_->lookupChange(0);
225 Changes::lyxMarkChange(os, column, curtime,
226 Change(Change::UNCHANGED), running_change);
228 Changes::lyxMarkChange(os, column, curtime,
229 running_change, Change(Change::UNCHANGED));
231 os << "\n\\end_layout\n";
235 void Paragraph::validate(LaTeXFeatures & features) const
237 pimpl_->validate(features, *layout());
241 void Paragraph::eraseIntern(lyx::pos_type pos)
243 pimpl_->eraseIntern(pos);
247 bool Paragraph::erase(pos_type pos)
249 return pimpl_->erase(pos);
253 int Paragraph::erase(pos_type start, pos_type end)
255 return pimpl_->erase(start, end);
259 void Paragraph::insert(pos_type start, string const & str,
260 LyXFont const & font)
262 int size = str.size();
263 for (int i = 0 ; i < size ; ++i)
264 insertChar(start + i, str[i], font);
268 bool Paragraph::checkInsertChar(LyXFont &)
274 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
277 pimpl_->insertChar(pos, c, change);
281 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
282 LyXFont const & font, Change change)
284 pimpl_->insertChar(pos, c, change);
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
291 pimpl_->insertInset(pos, inset, change);
295 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
296 LyXFont const & font, Change change)
298 pimpl_->insertInset(pos, inset, change);
303 bool Paragraph::insetAllowed(InsetBase_code code)
305 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
309 InsetBase * Paragraph::getInset(pos_type pos)
311 BOOST_ASSERT(pos < size());
312 return insetlist.get(pos);
316 InsetBase const * Paragraph::getInset(pos_type pos) const
318 BOOST_ASSERT(pos < size());
319 return insetlist.get(pos);
323 // Gets uninstantiated font setting at position.
324 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
328 lyxerr << " pos: " << pos << " size: " << size() << endl;
329 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
405 (BufferParams const & bparams, 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
416 (BufferParams const & bparams, 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
427 LyXFont_size Paragraph::highestFontInRange
428 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
430 if (pimpl_->fontlist.empty())
433 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
434 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
435 for (; end_it != end; ++end_it) {
436 if (end_it->pos() >= endpos)
443 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
444 for (; cit != end; ++cit) {
445 if (cit->pos() >= startpos)
449 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
450 for (; cit != end_it; ++cit) {
451 LyXFont::FONT_SIZE size = cit->font().size();
452 if (size == LyXFont::INHERIT_SIZE)
454 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
461 Paragraph::value_type
462 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
464 value_type c = getChar(pos);
465 if (!lyxrc.rtl_support)
495 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
502 void Paragraph::setFont(pos_type pos, LyXFont const & font)
504 BOOST_ASSERT(pos <= size());
506 // First, reduce font against layout/label font
507 // Update: The setCharFont() routine in text2.C already
508 // reduces font, so we don't need to do that here. (Asger)
509 // No need to simplify this because it will disappear
510 // in a new kernel. (Asger)
511 // Next search font table
513 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
514 Pimpl::FontList::iterator it = beg;
515 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
516 for (; it != endit; ++it) {
517 if (it->pos() >= pos)
520 unsigned int i = distance(beg, it);
521 bool notfound = (it == endit);
523 if (!notfound && pimpl_->fontlist[i].font() == font)
526 bool begin = pos == 0 || notfound ||
527 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
528 // Is position pos is a beginning of a font block?
529 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
530 // Is position pos is the end of a font block?
531 if (begin && end) { // A single char block
532 if (i + 1 < pimpl_->fontlist.size() &&
533 pimpl_->fontlist[i + 1].font() == font) {
534 // Merge the singleton block with the next block
535 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
536 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
537 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
538 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
539 // Merge the singleton block with the previous block
540 pimpl_->fontlist[i - 1].pos(pos);
541 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
543 pimpl_->fontlist[i].font(font);
545 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
546 pimpl_->fontlist[i - 1].pos(pos);
548 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
549 Pimpl::FontTable(pos, font));
551 pimpl_->fontlist[i].pos(pos - 1);
552 if (!(i + 1 < pimpl_->fontlist.size() &&
553 pimpl_->fontlist[i + 1].font() == font))
554 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
555 Pimpl::FontTable(pos, font));
556 } else { // The general case. The block is splitted into 3 blocks
557 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
558 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
559 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
560 Pimpl::FontTable(pos, font));
565 void Paragraph::makeSameLayout(Paragraph const & par)
567 layout(par.layout());
569 params() = par.params();
573 int Paragraph::stripLeadingSpaces()
579 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
580 pimpl_->eraseIntern(0);
588 bool Paragraph::hasSameLayout(Paragraph const & par) const
590 return par.layout() == layout() && params().sameLayout(par.params());
594 Paragraph::depth_type Paragraph::getDepth() const
596 return params().depth();
600 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
602 if (layout()->isEnvironment())
603 return params().depth() + 1;
605 return params().depth();
609 char Paragraph::getAlign() const
611 return params().align();
615 string const & Paragraph::getLabelstring() const
617 return params().labelString();
621 // the next two functions are for the manual labels
622 string const Paragraph::getLabelWidthString() const
624 if (!params().labelWidthString().empty())
625 return params().labelWidthString();
627 return _("Senseless with this layout!");
631 void Paragraph::setLabelWidthString(string const & s)
633 params().labelWidthString(s);
637 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
640 params().labelWidthString(string());
641 params().align(LYX_ALIGN_LAYOUT);
642 params().spacing(Spacing(Spacing::Default));
646 pos_type Paragraph::beginOfBody() const
648 return begin_of_body_;
652 void Paragraph::setBeginOfBody()
654 if (layout()->labeltype != LABEL_MANUAL) {
659 // Unroll the first two cycles of the loop
660 // and remember the previous character to
661 // remove unnecessary getChar() calls
663 pos_type end = size();
664 if (i < end && !isNewline(i)) {
666 char previous_char = 0;
669 previous_char = text_[i];
672 while (i < end && previous_char != ' ') {
677 previous_char = temp;
687 // returns -1 if inset not found
688 int Paragraph::getPositionOfInset(InsetBase const * inset) const
691 InsetList::const_iterator it = insetlist.begin();
692 InsetList::const_iterator end = insetlist.end();
693 for (; it != end; ++it)
694 if (it->inset == inset)
700 InsetBibitem * Paragraph::bibitem() const
702 if (!insetlist.empty()) {
703 InsetBase * inset = insetlist.begin()->inset;
704 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
705 return static_cast<InsetBibitem *>(inset);
711 bool Paragraph::forceDefaultParagraphs() const
713 return inInset() && inInset()->forceDefaultParagraphs(inInset());
719 // paragraphs inside floats need different alignment tags to avoid
722 bool noTrivlistCentering(InsetBase::Code code)
724 return code == InsetBase::FLOAT_CODE || 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 InsetBase::Code code)
743 string output = suffix + "{";
744 if (noTrivlistCentering(code))
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 if (getParLanguage(bparams)->babel() != "hebrew")
790 output = corrected_env("\\begin", "flushleft", ownerCode());
792 output = corrected_env("\\begin", "flushright", ownerCode());
794 column += output.size();
796 } case LYX_ALIGN_RIGHT: {
798 if (getParLanguage(bparams)->babel() != "hebrew")
799 output = corrected_env("\\begin", "flushright", ownerCode());
801 output = corrected_env("\\begin", "flushleft", ownerCode());
803 column += output.size();
805 } case LYX_ALIGN_CENTER: {
807 output = corrected_env("\\begin", "center", ownerCode());
809 column += output.size();
818 // This could go to ParagraphParameters if we want to
819 int Paragraph::endTeXParParams(BufferParams const & bparams,
820 ostream & os, bool moving_arg) const
824 switch (params().align()) {
826 case LYX_ALIGN_BLOCK:
827 case LYX_ALIGN_LAYOUT:
828 case LYX_ALIGN_SPECIAL:
831 case LYX_ALIGN_RIGHT:
832 case LYX_ALIGN_CENTER:
840 switch (params().align()) {
842 case LYX_ALIGN_BLOCK:
843 case LYX_ALIGN_LAYOUT:
844 case LYX_ALIGN_SPECIAL:
846 case LYX_ALIGN_LEFT: {
848 if (getParLanguage(bparams)->babel() != "hebrew")
849 output = corrected_env("\\par\\end", "flushleft", ownerCode());
851 output = corrected_env("\\par\\end", "flushright", ownerCode());
853 column += output.size();
855 } case LYX_ALIGN_RIGHT: {
857 if (getParLanguage(bparams)->babel() != "hebrew")
858 output = corrected_env("\\par\\end", "flushright", ownerCode());
860 output = corrected_env("\\par\\end", "flushleft", ownerCode());
862 column += output.size();
864 } case LYX_ALIGN_CENTER: {
866 output = corrected_env("\\par\\end", "center", ownerCode());
868 column += output.size();
877 // This one spits out the text of the paragraph
878 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
879 BufferParams const & bparams,
880 LyXFont const & outerfont,
881 ostream & os, TexRow & texrow,
882 OutputParams const & runparams) const
884 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
886 bool return_value = false;
890 // well we have to check if we are in an inset with unlimited
891 // length (all in one row) if that is true then we don't allow
892 // any special options in the paragraph and also we don't allow
893 // any environment other then "Standard" to be valid!
894 bool asdefault = forceDefaultParagraphs();
897 style = bparams.getLyXTextClass().defaultLayout();
904 // Maybe we have to create a optional argument.
905 pos_type body_pos = beginOfBody();
906 unsigned int column = 0;
909 // the optional argument is kept in curly brackets in
910 // case it contains a ']'
913 basefont = getLabelFont(bparams, outerfont);
915 basefont = getLayoutFont(bparams, outerfont);
918 // Which font is currently active?
919 LyXFont running_font(basefont);
920 // Do we have an open font change?
921 bool open_font = false;
923 Change::Type running_change = Change::UNCHANGED;
925 texrow.start(id(), 0);
927 // if the paragraph is empty, the loop will not be entered at all
929 if (style->isCommand()) {
934 column += startTeXParParams(bparams, os,
935 runparams.moving_arg);
938 for (pos_type i = 0; i < size(); ++i) {
940 // First char in paragraph or after label?
944 column += running_font.latexWriteEndChanges(os, basefont, basefont);
947 basefont = getLayoutFont(bparams, outerfont);
948 running_font = basefont;
952 if (style->isCommand()) {
958 column += startTeXParParams(bparams, os,
959 runparams.moving_arg);
962 value_type c = getChar(i);
964 // Fully instantiated font
965 LyXFont font = getFont(bparams, i, outerfont);
967 LyXFont const last_font = running_font;
969 // Spaces at end of font change are simulated to be
970 // outside font change, i.e. we write "\textXX{text} "
971 // rather than "\textXX{text }". (Asger)
972 if (open_font && c == ' ' && i <= size() - 2) {
973 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
974 if (next_font != running_font && next_font != font) {
979 // We end font definition before blanks
981 (font != running_font ||
982 font.language() != running_font.language()))
984 column += running_font.latexWriteEndChanges(os,
986 (i == body_pos-1) ? basefont : font);
987 running_font = basefont;
991 // Blanks are printed before start of fontswitch
993 // Do not print the separation of the optional argument
994 if (i != body_pos - 1) {
995 pimpl_->simpleTeXBlanks(os, texrow, i,
996 column, font, *style);
1000 // Do we need to change font?
1001 if ((font != running_font ||
1002 font.language() != running_font.language()) &&
1005 column += font.latexWriteStartChanges(os, basefont,
1007 running_font = font;
1011 Change::Type change = pimpl_->lookupChange(i);
1013 column += Changes::latexMarkChange(os, running_change, change);
1014 running_change = change;
1016 OutputParams rp = runparams;
1017 rp.free_spacing = style->free_spacing;
1018 rp.local_language = font.language()->babel();
1019 rp.intitle = style->intitle;
1020 pimpl_->simpleTeXSpecialChars(buf, bparams,
1023 basefont, outerfont, open_font,
1025 *style, i, column, c);
1028 column += Changes::latexMarkChange(os,
1029 running_change, Change::UNCHANGED);
1031 // If we have an open font definition, we have to close it
1033 #ifdef FIXED_LANGUAGE_END_DETECTION
1036 .latexWriteEndChanges(os, basefont,
1037 next_->getFont(bparams,
1040 running_font.latexWriteEndChanges(os, basefont,
1044 #ifdef WITH_WARNINGS
1045 //#warning For now we ALWAYS have to close the foreign font settings if they are
1046 //#warning there as we start another \selectlanguage with the next paragraph if
1047 //#warning we are in need of this. This should be fixed sometime (Jug)
1049 running_font.latexWriteEndChanges(os, basefont, basefont);
1053 // Needed if there is an optional argument but no contents.
1054 if (body_pos > 0 && body_pos == size()) {
1056 return_value = false;
1060 column += endTeXParParams(bparams, os, runparams.moving_arg);
1063 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1064 return return_value;
1070 // checks, if newcol chars should be put into this line
1071 // writes newline, if necessary.
1072 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1073 string::size_type newcol)
1076 if (colcount > lyxrc.ascii_linelen) {
1078 colcount = newcol; // assume write after this call
1093 string tag_name(PAR_TAG const & pt) {
1095 case PAR_NONE: return "!-- --";
1096 case TT: return "tt";
1097 case SF: return "sf";
1098 case BF: return "bf";
1099 case IT: return "it";
1100 case SL: return "sl";
1101 case EM: return "em";
1108 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1110 p1 = static_cast<PAR_TAG>(p1 | p2);
1115 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1117 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1123 // Handle internal paragraph parsing -- layout already processed.
1124 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1126 LyXFont const & outerfont,
1127 OutputParams const & runparams,
1128 lyx::depth_type /*depth*/) const
1130 LyXLayout_ptr const & style = layout();
1132 string::size_type char_line_count = 5; // Heuristic choice ;-)
1134 // gets paragraph main font
1137 if (style->labeltype == LABEL_MANUAL) {
1138 font_old = style->labelfont;
1141 font_old = style->font;
1145 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1146 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1147 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1150 stack<PAR_TAG> tag_state;
1151 // parsing main loop
1152 for (pos_type i = 0; i < size(); ++i) {
1154 PAR_TAG tag_close = PAR_NONE;
1155 list < PAR_TAG > tag_open;
1157 LyXFont const font = getFont(buf.params(), i, outerfont);
1159 if (font_old.family() != font.family()) {
1160 switch (family_type) {
1161 case LyXFont::SANS_FAMILY:
1164 case LyXFont::TYPEWRITER_FAMILY:
1171 family_type = font.family();
1173 switch (family_type) {
1174 case LyXFont::SANS_FAMILY:
1175 tag_open.push_back(SF);
1177 case LyXFont::TYPEWRITER_FAMILY:
1178 tag_open.push_back(TT);
1185 if (font_old.series() != font.series()) {
1186 switch (series_type) {
1187 case LyXFont::BOLD_SERIES:
1194 series_type = font.series();
1196 switch (series_type) {
1197 case LyXFont::BOLD_SERIES:
1198 tag_open.push_back(BF);
1206 if (font_old.shape() != font.shape()) {
1207 switch (shape_type) {
1208 case LyXFont::ITALIC_SHAPE:
1211 case LyXFont::SLANTED_SHAPE:
1218 shape_type = font.shape();
1220 switch (shape_type) {
1221 case LyXFont::ITALIC_SHAPE:
1222 tag_open.push_back(IT);
1224 case LyXFont::SLANTED_SHAPE:
1225 tag_open.push_back(SL);
1232 if (font_old.emph() != font.emph()) {
1233 if (font.emph() == LyXFont::ON) {
1234 tag_open.push_back(EM);
1243 list < PAR_TAG > temp;
1244 while (!tag_state.empty() && tag_close) {
1245 PAR_TAG k = tag_state.top();
1247 os << "</" << tag_name(k) << '>';
1254 for(list< PAR_TAG >::const_iterator j = temp.begin();
1255 j != temp.end(); ++j) {
1257 os << '<' << tag_name(*j) << '>';
1260 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1261 j != tag_open.end(); ++j) {
1263 os << '<' << tag_name(*j) << '>';
1266 value_type c = getChar(i);
1269 if (c == Paragraph::META_INSET) {
1270 getInset(i)->linuxdoc(buf, os, runparams);
1275 if (style->latexparam() == "CDATA") {
1276 // "TeX"-Mode on == > SGML-Mode on.
1283 boost::tie(ws, str) = sgml::escapeChar(c);
1284 if (ws && !isFreeSpacing()) {
1285 // in freespacing mode, spaces are
1286 // non-breaking characters
1287 if (desc_on) { // if char is ' ' then...
1289 sgmlLineBreak(os, char_line_count, 6);
1293 sgmlLineBreak(os, char_line_count, 1);
1298 char_line_count += str.length();
1304 while (!tag_state.empty()) {
1305 os << "</" << tag_name(tag_state.top()) << '>';
1309 // resets description flag correctly
1311 // <tag> not closed...
1312 sgmlLineBreak(os, char_line_count, 6);
1318 bool Paragraph::emptyTag() const
1320 for (pos_type i = 0; i < size(); ++i) {
1322 InsetBase const * inset = getInset(i);
1323 InsetBase::Code lyx_code = inset->lyxCode();
1324 if (lyx_code != InsetBase::TOC_CODE &&
1325 lyx_code != InsetBase::INCLUDE_CODE &&
1326 lyx_code != InsetBase::GRAPHICS_CODE &&
1327 lyx_code != InsetBase::ERT_CODE &&
1328 lyx_code != InsetBase::FLOAT_CODE &&
1329 lyx_code != InsetBase::TABULAR_CODE) {
1333 value_type c = getChar(i);
1334 if (c != ' ' && c != '\t')
1342 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1344 for (pos_type i = 0; i < size(); ++i) {
1346 InsetBase const * inset = getInset(i);
1347 InsetBase::Code lyx_code = inset->lyxCode();
1348 if (lyx_code == InsetBase::LABEL_CODE) {
1349 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1350 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1359 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1362 for (i = 0; i < size(); ++i) {
1364 InsetBase const * inset = getInset(i);
1365 inset->docbook(buf, os, runparams);
1367 value_type c = getChar(i);
1372 boost::tie(ws, str) = sgml::escapeChar(c);
1381 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1385 for (pos_type i = initial; i < size(); ++i) {
1386 LyXFont font = getFont(buf.params(), i, outerfont);
1389 if (i != initial && font != font_old)
1398 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1400 OutputParams const & runparams,
1401 LyXFont const & outerfont,
1402 pos_type initial) const
1404 bool emph_flag = false;
1406 LyXLayout_ptr const & style = layout();
1408 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1410 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1413 // parsing main loop
1414 for (pos_type i = initial; i < size(); ++i) {
1415 LyXFont font = getFont(buf.params(), i, outerfont);
1417 // handle <emphasis> tag
1418 if (font_old.emph() != font.emph()) {
1419 if (font.emph() == LyXFont::ON) {
1422 } else if (i != initial) {
1423 os << "</emphasis>";
1429 InsetBase const * inset = getInset(i);
1430 inset->docbook(buf, os, runparams);
1432 value_type c = getChar(i);
1435 boost::tie(ws, str) = sgml::escapeChar(c);
1437 if (style->pass_thru)
1446 os << "</emphasis>";
1449 if (style->free_spacing)
1451 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1458 /// return true if the char is a meta-character for an inset
1460 bool IsInsetChar(char c)
1462 return (c == Paragraph::META_INSET);
1469 bool Paragraph::isHfill(pos_type pos) const
1472 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1476 bool Paragraph::isNewline(pos_type pos) const
1479 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1483 bool Paragraph::isSeparator(pos_type pos) const
1485 return IsSeparatorChar(getChar(pos));
1489 bool Paragraph::isLineSeparator(pos_type pos) const
1491 value_type const c = getChar(pos);
1492 return IsLineSeparatorChar(c)
1493 || (IsInsetChar(c) && getInset(pos) &&
1494 getInset(pos)->isLineSeparator());
1498 /// Used by the spellchecker
1499 bool Paragraph::isLetter(pos_type pos) const
1502 return getInset(pos)->isLetter();
1504 value_type const c = getChar(pos);
1505 return IsLetterChar(c) || IsDigit(c);
1511 Paragraph::getParLanguage(BufferParams const & bparams) const
1514 return getFirstFontSettings().language();
1515 #ifdef WITH_WARNINGS
1516 #warning FIXME we should check the prev par as well (Lgb)
1518 return bparams.language;
1522 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1524 return lyxrc.rtl_support
1525 && getParLanguage(bparams)->RightToLeft()
1526 && ownerCode() != InsetBase::ERT_CODE;
1530 void Paragraph::changeLanguage(BufferParams const & bparams,
1531 Language const * from, Language const * to)
1533 for (pos_type i = 0; i < size(); ++i) {
1534 LyXFont font = getFontSettings(bparams, i);
1535 if (font.language() == from) {
1536 font.setLanguage(to);
1543 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1545 Language const * doc_language = bparams.language;
1546 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1547 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1549 for (; cit != end; ++cit)
1550 if (cit->font().language() != ignore_language &&
1551 cit->font().language() != latex_language &&
1552 cit->font().language() != doc_language)
1558 // Convert the paragraph to a string.
1559 // Used for building the table of contents
1560 string const Paragraph::asString(Buffer const & buffer, bool label) const
1562 OutputParams runparams;
1563 return asString(buffer, runparams, label);
1567 string const Paragraph::asString(Buffer const & buffer,
1568 OutputParams const & runparams,
1573 if (label && !params().labelString().empty())
1574 s += params().labelString() + ' ';
1576 for (pos_type i = 0; i < size(); ++i) {
1577 value_type c = getChar(i);
1580 else if (c == META_INSET &&
1581 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1583 getInset(i)->plaintext(buffer, os, runparams);
1584 s += subst(STRCONV(os.str()),'\n',' ');
1590 // This should really be done by the caller and not here.
1591 string ret = asString(buffer, runparams, 0, size(), label);
1592 return subst(ret, '\n', ' ');
1597 string const Paragraph::asString(Buffer const & buffer,
1598 pos_type beg, pos_type end, bool label) const
1601 OutputParams const runparams;
1602 return asString(buffer, runparams, beg, end, label);
1606 string const Paragraph::asString(Buffer const & buffer,
1607 OutputParams const & runparams,
1608 pos_type beg, pos_type end, bool label) const
1612 if (beg == 0 && label && !params().labelString().empty())
1613 os << params().labelString() << ' ';
1615 for (pos_type i = beg; i < end; ++i) {
1616 value_type const c = getUChar(buffer.params(), i);
1619 else if (c == META_INSET)
1620 getInset(i)->plaintext(buffer, os, runparams);
1627 void Paragraph::setInsetOwner(UpdatableInset * inset)
1629 pimpl_->inset_owner = inset;
1633 void Paragraph::setContentsFromPar(Paragraph const & par)
1635 pimpl_->setContentsFromPar(par);
1639 void Paragraph::trackChanges(Change::Type type)
1641 pimpl_->trackChanges(type);
1645 void Paragraph::untrackChanges()
1647 pimpl_->untrackChanges();
1651 void Paragraph::cleanChanges()
1653 pimpl_->cleanChanges();
1657 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1659 BOOST_ASSERT(empty() || pos < size());
1660 return pimpl_->lookupChange(pos);
1664 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1666 BOOST_ASSERT(empty() || pos < size());
1667 return pimpl_->lookupChangeFull(pos);
1671 bool Paragraph::isChanged(pos_type start, pos_type end) const
1673 return pimpl_->isChanged(start, end);
1677 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1679 return pimpl_->isChangeEdited(start, end);
1683 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1685 pimpl_->setChange(pos, type);
1689 void Paragraph::markErased()
1691 pimpl_->markErased();
1695 void Paragraph::acceptChange(pos_type start, pos_type end)
1697 return pimpl_->acceptChange(start, end);
1701 void Paragraph::rejectChange(pos_type start, pos_type end)
1703 return pimpl_->rejectChange(start, end);
1707 int Paragraph::id() const
1713 LyXLayout_ptr const & Paragraph::layout() const
1719 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1721 layout_ = new_layout;
1725 UpdatableInset * Paragraph::inInset() const
1727 return pimpl_->inset_owner;
1731 InsetBase::Code Paragraph::ownerCode() const
1733 return pimpl_->inset_owner
1734 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1738 void Paragraph::clearContents()
1744 void Paragraph::setChar(pos_type pos, value_type c)
1750 ParagraphParameters & Paragraph::params()
1752 return pimpl_->params;
1756 ParagraphParameters const & Paragraph::params() const
1758 return pimpl_->params;
1762 bool Paragraph::isFreeSpacing() const
1764 if (layout()->free_spacing)
1767 // for now we just need this, later should we need this in some
1768 // other way we can always add a function to InsetBase too.
1769 return ownerCode() == InsetBase::ERT_CODE;
1773 bool Paragraph::allowEmpty() const
1775 if (layout()->keepempty)
1777 return ownerCode() == InsetBase::ERT_CODE;
1781 Row & Paragraph::getRow(pos_type pos)
1783 RowList::iterator rit = rows_.end();
1784 RowList::iterator const begin = rows_.begin();
1786 for (--rit; rit != begin && rit->pos() > pos; --rit)
1793 Row const & Paragraph::getRow(pos_type pos) const
1795 RowList::const_iterator rit = rows_.end();
1796 RowList::const_iterator const begin = rows_.begin();
1798 for (--rit; rit != begin && rit->pos() > pos; --rit)
1805 size_t Paragraph::pos2row(pos_type pos) const
1807 RowList::const_iterator rit = rows_.end();
1808 RowList::const_iterator const begin = rows_.begin();
1810 for (--rit; rit != begin && rit->pos() > pos; --rit)
1817 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1819 if (!Encodings::is_arabic(c))
1820 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1821 return c + (0xb0 - '0');
1825 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1826 unsigned char next_char = ' ';
1828 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1829 unsigned char const par_char = getChar(i);
1830 if (!Encodings::IsComposeChar_arabic(par_char)) {
1831 next_char = par_char;
1836 if (Encodings::is_arabic(next_char)) {
1837 if (Encodings::is_arabic(prev_char) &&
1838 !Encodings::is_arabic_special(prev_char))
1839 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1841 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1843 if (Encodings::is_arabic(prev_char) &&
1844 !Encodings::is_arabic_special(prev_char))
1845 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1847 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1852 void Paragraph::dump() const
1854 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1855 for (size_t i = 0; i != rows_.size(); ++i) {
1856 lyxerr << " row " << i << ": ";
1861 //void Paragraph::metrics(MetricsInfo & mi, Dimension & dim, LyXText & text)
1866 //void draw(PainterInfo & pi, int x, int y, LyXText & text) const