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"
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/bind.hpp>
57 using lyx::support::subst;
65 using std::ostringstream;
68 ParagraphList::ParagraphList()
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_), layout_(par.layout_),
84 text_(par.text_), begin_of_body_(par.begin_of_body_),
85 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
87 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
88 InsetList::iterator it = insetlist.begin();
89 InsetList::iterator end = insetlist.end();
90 for (; it != end; ++it)
91 it->inset = it->inset->clone().release();
95 Paragraph & Paragraph::operator=(Paragraph const & par)
97 // needed as we will destroy the pimpl_ before copying it
99 itemdepth = par.itemdepth;
101 insetlist = par.insetlist;
102 InsetList::iterator it = insetlist.begin();
103 InsetList::iterator end = insetlist.end();
104 for (; it != end; ++it)
105 it->inset = it->inset->clone().release();
109 layout_ = par.layout();
111 begin_of_body_ = par.begin_of_body_;
114 pimpl_ = new Pimpl(*par.pimpl_, this);
120 Paragraph::~Paragraph()
124 //lyxerr << "Paragraph::paragraph_id = "
125 // << Paragraph::paragraph_id << endl;
129 void Paragraph::write(Buffer const & buf, ostream & os,
130 BufferParams const & bparams,
131 depth_type & dth) const
133 // The beginning or end of a deeper (i.e. nested) area?
134 if (dth != params().depth()) {
135 if (params().depth() > dth) {
136 while (params().depth() > dth) {
137 os << "\n\\begin_deeper";
141 while (params().depth() < dth) {
142 os << "\n\\end_deeper";
148 // First write the layout
149 os << "\n\\begin_layout " << layout()->name() << '\n';
153 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155 Change running_change = Change(Change::UNCHANGED);
156 lyx::time_type const curtime(lyx::current_time());
159 for (pos_type i = 0; i < size(); ++i) {
161 Change change = pimpl_->lookupChangeFull(i);
162 Changes::lyxMarkChange(os, column, curtime, running_change, change);
163 running_change = change;
165 // Write font changes
166 LyXFont font2 = getFontSettings(bparams, i);
167 if (font2 != font1) {
168 font2.lyxWriteChanges(font1, os);
173 value_type const c = getChar(i);
177 InsetBase const * inset = getInset(i);
179 if (inset->directWrite()) {
180 // international char, let it write
181 // code directly so it's shorter in
183 inset->write(buf, os);
187 os << "\\begin_inset ";
188 inset->write(buf, os);
189 os << "\n\\end_inset\n\n";
195 os << "\n\\backslash\n";
199 if (i + 1 < size() && getChar(i + 1) == ' ') {
206 if ((column > 70 && c == ' ')
211 // this check is to amend a bug. LyX sometimes
212 // inserts '\0' this could cause problems.
216 lyxerr << "ERROR (Paragraph::writeFile):"
217 " NULL char in structure." << endl;
223 // to make reading work properly
225 running_change = pimpl_->lookupChange(0);
226 Changes::lyxMarkChange(os, column, curtime,
227 Change(Change::UNCHANGED), running_change);
229 Changes::lyxMarkChange(os, column, curtime,
230 running_change, Change(Change::UNCHANGED));
232 os << "\n\\end_layout\n";
236 void Paragraph::validate(LaTeXFeatures & features) const
238 pimpl_->validate(features, *layout());
242 void Paragraph::eraseIntern(lyx::pos_type pos)
244 pimpl_->eraseIntern(pos);
248 bool Paragraph::erase(pos_type pos)
250 return pimpl_->erase(pos);
254 int Paragraph::erase(pos_type start, pos_type end)
256 return pimpl_->erase(start, end);
260 void Paragraph::insert(pos_type start, string const & str,
261 LyXFont const & font)
263 int size = str.size();
264 for (int i = 0 ; i < size ; ++i)
265 insertChar(start + i, str[i], font);
269 bool Paragraph::checkInsertChar(LyXFont &)
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
278 pimpl_->insertChar(pos, c, change);
282 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
283 LyXFont const & font, Change change)
285 pimpl_->insertChar(pos, c, change);
290 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
292 pimpl_->insertInset(pos, inset, change);
296 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
297 LyXFont const & font, Change change)
299 pimpl_->insertInset(pos, inset, change);
304 bool Paragraph::insetAllowed(InsetBase_code code)
306 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
310 InsetBase * Paragraph::getInset(pos_type pos)
312 BOOST_ASSERT(pos < size());
313 return insetlist.get(pos);
317 InsetBase const * Paragraph::getInset(pos_type pos) const
319 BOOST_ASSERT(pos < size());
320 return insetlist.get(pos);
324 // Gets uninstantiated font setting at position.
325 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
329 lyxerr << " pos: " << pos << " size: " << size() << endl;
330 BOOST_ASSERT(pos <= size());
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit)
336 if (cit->pos() >= pos)
342 if (pos == size() && !empty())
343 return getFontSettings(bparams, pos - 1);
345 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
349 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
351 BOOST_ASSERT(pos <= size());
353 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
354 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
355 for (; cit != end; ++cit)
356 if (cit->pos() >= pos)
359 // This should not happen, but if so, we take no chances.
360 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
366 // Gets uninstantiated font setting at position 0
367 LyXFont const Paragraph::getFirstFontSettings() const
369 if (!empty() && !pimpl_->fontlist.empty())
370 return pimpl_->fontlist[0].font();
372 return LyXFont(LyXFont::ALL_INHERIT);
376 // Gets the fully instantiated font at a given position in a paragraph
377 // This is basically the same function as LyXText::GetFont() in text2.C.
378 // The difference is that this one is used for generating the LaTeX file,
379 // and thus cosmetic "improvements" are disallowed: This has to deliver
380 // the true picture of the buffer. (Asger)
381 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
382 LyXFont const & outerfont) const
384 BOOST_ASSERT(pos >= 0);
386 LyXLayout_ptr const & lout = layout();
388 pos_type const body_pos = beginOfBody();
392 layoutfont = lout->labelfont;
394 layoutfont = lout->font;
396 LyXFont font = getFontSettings(bparams, pos);
397 font.realize(layoutfont);
398 font.realize(outerfont);
399 font.realize(bparams.getLyXTextClass().defaultfont());
405 LyXFont const Paragraph::getLabelFont
406 (BufferParams const & bparams, LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->labelfont;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
416 LyXFont const Paragraph::getLayoutFont
417 (BufferParams const & bparams, LyXFont const & outerfont) const
419 LyXFont tmpfont = layout()->font;
420 tmpfont.setLanguage(getParLanguage(bparams));
421 tmpfont.realize(outerfont);
422 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
427 /// Returns the height of the highest font in range
428 LyXFont_size Paragraph::highestFontInRange
429 (pos_type startpos, pos_type endpos, 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 = 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 pos_type 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);
712 bool Paragraph::forceDefaultParagraphs() const
714 return inInset() && inInset()->forceDefaultParagraphs(inInset());
720 // paragraphs inside floats need different alignment tags to avoid
723 bool noTrivlistCentering(InsetBase::Code code)
725 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
729 string correction(string const & orig)
731 if (orig == "flushleft")
732 return "raggedright";
733 if (orig == "flushright")
735 if (orig == "center")
741 string const corrected_env(string const & suffix, string const & env,
742 InsetBase::Code code)
744 string output = suffix + "{";
745 if (noTrivlistCentering(code))
746 output += correction(env);
755 // This could go to ParagraphParameters if we want to
756 int Paragraph::startTeXParParams(BufferParams const & bparams,
757 ostream & os, bool moving_arg) const
761 if (params().noindent()) {
766 switch (params().align()) {
768 case LYX_ALIGN_BLOCK:
769 case LYX_ALIGN_LAYOUT:
770 case LYX_ALIGN_SPECIAL:
773 case LYX_ALIGN_RIGHT:
774 case LYX_ALIGN_CENTER:
782 switch (params().align()) {
784 case LYX_ALIGN_BLOCK:
785 case LYX_ALIGN_LAYOUT:
786 case LYX_ALIGN_SPECIAL:
788 case LYX_ALIGN_LEFT: {
790 if (getParLanguage(bparams)->babel() != "hebrew")
791 output = corrected_env("\\begin", "flushleft", ownerCode());
793 output = corrected_env("\\begin", "flushright", ownerCode());
795 column += output.size();
797 } case LYX_ALIGN_RIGHT: {
799 if (getParLanguage(bparams)->babel() != "hebrew")
800 output = corrected_env("\\begin", "flushright", ownerCode());
802 output = corrected_env("\\begin", "flushleft", ownerCode());
804 column += output.size();
806 } case LYX_ALIGN_CENTER: {
808 output = corrected_env("\\begin", "center", ownerCode());
810 column += output.size();
819 // This could go to ParagraphParameters if we want to
820 int Paragraph::endTeXParParams(BufferParams const & bparams,
821 ostream & os, bool moving_arg) const
825 switch (params().align()) {
827 case LYX_ALIGN_BLOCK:
828 case LYX_ALIGN_LAYOUT:
829 case LYX_ALIGN_SPECIAL:
832 case LYX_ALIGN_RIGHT:
833 case LYX_ALIGN_CENTER:
841 switch (params().align()) {
843 case LYX_ALIGN_BLOCK:
844 case LYX_ALIGN_LAYOUT:
845 case LYX_ALIGN_SPECIAL:
847 case LYX_ALIGN_LEFT: {
849 if (getParLanguage(bparams)->babel() != "hebrew")
850 output = corrected_env("\\par\\end", "flushleft", ownerCode());
852 output = corrected_env("\\par\\end", "flushright", ownerCode());
854 column += output.size();
856 } case LYX_ALIGN_RIGHT: {
858 if (getParLanguage(bparams)->babel() != "hebrew")
859 output = corrected_env("\\par\\end", "flushright", ownerCode());
861 output = corrected_env("\\par\\end", "flushleft", ownerCode());
863 column += output.size();
865 } case LYX_ALIGN_CENTER: {
867 output = corrected_env("\\par\\end", "center", ownerCode());
869 column += output.size();
878 // This one spits out the text of the paragraph
879 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
880 BufferParams const & bparams,
881 LyXFont const & outerfont,
882 ostream & os, TexRow & texrow,
883 OutputParams const & runparams) const
885 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
887 bool return_value = false;
891 // well we have to check if we are in an inset with unlimited
892 // length (all in one row) if that is true then we don't allow
893 // any special options in the paragraph and also we don't allow
894 // any environment other then "Standard" to be valid!
895 bool asdefault = forceDefaultParagraphs();
898 style = bparams.getLyXTextClass().defaultLayout();
905 LaTeXFeatures features(buf, bparams, runparams.nice);
907 // output change tracking marks only if desired,
908 // if dvipost is installed,
909 // and with dvi/ps (other formats don't work)
910 bool const output = bparams.output_changes
911 && runparams.flavor == OutputParams::LATEX
912 && features.isAvailable("dvipost");
914 // Maybe we have to create a optional argument.
915 pos_type body_pos = beginOfBody();
916 unsigned int column = 0;
919 // the optional argument is kept in curly brackets in
920 // case it contains a ']'
923 basefont = getLabelFont(bparams, outerfont);
925 basefont = getLayoutFont(bparams, outerfont);
928 // Which font is currently active?
929 LyXFont running_font(basefont);
930 // Do we have an open font change?
931 bool open_font = false;
933 Change::Type running_change = Change::UNCHANGED;
935 texrow.start(id(), 0);
937 // if the paragraph is empty, the loop will not be entered at all
939 if (style->isCommand()) {
944 column += startTeXParParams(bparams, os,
945 runparams.moving_arg);
948 for (pos_type i = 0; i < size(); ++i) {
950 // First char in paragraph or after label?
954 column += running_font.latexWriteEndChanges(os, basefont, basefont);
957 basefont = getLayoutFont(bparams, outerfont);
958 running_font = basefont;
962 if (style->isCommand()) {
968 column += startTeXParParams(bparams, os,
969 runparams.moving_arg);
972 value_type c = getChar(i);
974 // Fully instantiated font
975 LyXFont font = getFont(bparams, i, outerfont);
977 LyXFont const last_font = running_font;
979 // Spaces at end of font change are simulated to be
980 // outside font change, i.e. we write "\textXX{text} "
981 // rather than "\textXX{text }". (Asger)
982 if (open_font && c == ' ' && i <= size() - 2) {
983 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
984 if (next_font != running_font && next_font != font) {
989 // We end font definition before blanks
991 (font != running_font ||
992 font.language() != running_font.language()))
994 column += running_font.latexWriteEndChanges(os,
996 (i == body_pos-1) ? basefont : font);
997 running_font = basefont;
1001 // Blanks are printed before start of fontswitch
1003 // Do not print the separation of the optional argument
1004 if (i != body_pos - 1) {
1005 pimpl_->simpleTeXBlanks(os, texrow, i,
1006 column, font, *style);
1010 // Do we need to change font?
1011 if ((font != running_font ||
1012 font.language() != running_font.language()) &&
1015 column += font.latexWriteStartChanges(os, basefont,
1017 running_font = font;
1021 Change::Type change = pimpl_->lookupChange(i);
1023 column += Changes::latexMarkChange(os, running_change,
1025 running_change = change;
1027 // do not output text which is marked deleted
1028 // if change tracking output is not desired
1029 if (output || running_change != Change::DELETED) {
1030 OutputParams rp = runparams;
1031 rp.free_spacing = style->free_spacing;
1032 rp.local_language = font.language()->babel();
1033 rp.intitle = style->intitle;
1034 pimpl_->simpleTeXSpecialChars(buf, bparams,
1037 basefont, outerfont, open_font,
1039 *style, i, column, c);
1043 column += Changes::latexMarkChange(os,
1044 running_change, Change::UNCHANGED, output);
1046 // If we have an open font definition, we have to close it
1048 #ifdef FIXED_LANGUAGE_END_DETECTION
1051 .latexWriteEndChanges(os, basefont,
1052 next_->getFont(bparams,
1055 running_font.latexWriteEndChanges(os, basefont,
1059 #ifdef WITH_WARNINGS
1060 //#warning For now we ALWAYS have to close the foreign font settings if they are
1061 //#warning there as we start another \selectlanguage with the next paragraph if
1062 //#warning we are in need of this. This should be fixed sometime (Jug)
1064 running_font.latexWriteEndChanges(os, basefont, basefont);
1068 // Needed if there is an optional argument but no contents.
1069 if (body_pos > 0 && body_pos == size()) {
1071 return_value = false;
1075 column += endTeXParParams(bparams, os, runparams.moving_arg);
1078 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1079 return return_value;
1085 // checks, if newcol chars should be put into this line
1086 // writes newline, if necessary.
1087 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1088 string::size_type newcol)
1091 if (colcount > lyxrc.ascii_linelen) {
1093 colcount = newcol; // assume write after this call
1108 string tag_name(PAR_TAG const & pt) {
1110 case PAR_NONE: return "!-- --";
1111 case TT: return "tt";
1112 case SF: return "sf";
1113 case BF: return "bf";
1114 case IT: return "it";
1115 case SL: return "sl";
1116 case EM: return "em";
1123 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1125 p1 = static_cast<PAR_TAG>(p1 | p2);
1130 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1132 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1138 // Handle internal paragraph parsing -- layout already processed.
1139 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1141 LyXFont const & outerfont,
1142 OutputParams const & runparams,
1143 lyx::depth_type /*depth*/) const
1145 LyXLayout_ptr const & style = layout();
1147 string::size_type char_line_count = 5; // Heuristic choice ;-)
1149 // gets paragraph main font
1152 if (style->labeltype == LABEL_MANUAL) {
1153 font_old = style->labelfont;
1156 font_old = style->font;
1160 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1161 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1162 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1165 stack<PAR_TAG> tag_state;
1166 // parsing main loop
1167 for (pos_type i = 0; i < size(); ++i) {
1169 PAR_TAG tag_close = PAR_NONE;
1170 list < PAR_TAG > tag_open;
1172 LyXFont const font = getFont(buf.params(), i, outerfont);
1174 if (font_old.family() != font.family()) {
1175 switch (family_type) {
1176 case LyXFont::SANS_FAMILY:
1179 case LyXFont::TYPEWRITER_FAMILY:
1186 family_type = font.family();
1188 switch (family_type) {
1189 case LyXFont::SANS_FAMILY:
1190 tag_open.push_back(SF);
1192 case LyXFont::TYPEWRITER_FAMILY:
1193 tag_open.push_back(TT);
1200 if (font_old.series() != font.series()) {
1201 switch (series_type) {
1202 case LyXFont::BOLD_SERIES:
1209 series_type = font.series();
1211 switch (series_type) {
1212 case LyXFont::BOLD_SERIES:
1213 tag_open.push_back(BF);
1221 if (font_old.shape() != font.shape()) {
1222 switch (shape_type) {
1223 case LyXFont::ITALIC_SHAPE:
1226 case LyXFont::SLANTED_SHAPE:
1233 shape_type = font.shape();
1235 switch (shape_type) {
1236 case LyXFont::ITALIC_SHAPE:
1237 tag_open.push_back(IT);
1239 case LyXFont::SLANTED_SHAPE:
1240 tag_open.push_back(SL);
1247 if (font_old.emph() != font.emph()) {
1248 if (font.emph() == LyXFont::ON) {
1249 tag_open.push_back(EM);
1258 list < PAR_TAG > temp;
1259 while (!tag_state.empty() && tag_close) {
1260 PAR_TAG k = tag_state.top();
1262 os << "</" << tag_name(k) << '>';
1269 for(list< PAR_TAG >::const_iterator j = temp.begin();
1270 j != temp.end(); ++j) {
1272 os << '<' << tag_name(*j) << '>';
1275 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1276 j != tag_open.end(); ++j) {
1278 os << '<' << tag_name(*j) << '>';
1281 value_type c = getChar(i);
1284 if (c == Paragraph::META_INSET) {
1285 getInset(i)->linuxdoc(buf, os, runparams);
1290 if (style->latexparam() == "CDATA") {
1291 // "TeX"-Mode on == > SGML-Mode on.
1298 boost::tie(ws, str) = sgml::escapeChar(c);
1299 if (ws && !isFreeSpacing()) {
1300 // in freespacing mode, spaces are
1301 // non-breaking characters
1302 if (desc_on) { // if char is ' ' then...
1304 sgmlLineBreak(os, char_line_count, 6);
1308 sgmlLineBreak(os, char_line_count, 1);
1313 char_line_count += str.length();
1319 while (!tag_state.empty()) {
1320 os << "</" << tag_name(tag_state.top()) << '>';
1324 // resets description flag correctly
1326 // <tag> not closed...
1327 sgmlLineBreak(os, char_line_count, 6);
1333 bool Paragraph::emptyTag() const
1335 for (pos_type i = 0; i < size(); ++i) {
1337 InsetBase const * inset = getInset(i);
1338 InsetBase::Code lyx_code = inset->lyxCode();
1339 if (lyx_code != InsetBase::TOC_CODE &&
1340 lyx_code != InsetBase::INCLUDE_CODE &&
1341 lyx_code != InsetBase::GRAPHICS_CODE &&
1342 lyx_code != InsetBase::ERT_CODE &&
1343 lyx_code != InsetBase::FLOAT_CODE &&
1344 lyx_code != InsetBase::TABULAR_CODE) {
1348 value_type c = getChar(i);
1349 if (c != ' ' && c != '\t')
1357 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1359 for (pos_type i = 0; i < size(); ++i) {
1361 InsetBase const * inset = getInset(i);
1362 InsetBase::Code lyx_code = inset->lyxCode();
1363 if (lyx_code == InsetBase::LABEL_CODE) {
1364 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1365 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1374 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1377 for (i = 0; i < size(); ++i) {
1379 InsetBase const * inset = getInset(i);
1380 inset->docbook(buf, os, runparams);
1382 value_type c = getChar(i);
1387 boost::tie(ws, str) = sgml::escapeChar(c);
1396 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1400 for (pos_type i = initial; i < size(); ++i) {
1401 LyXFont font = getFont(buf.params(), i, outerfont);
1404 if (i != initial && font != font_old)
1413 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1415 OutputParams const & runparams,
1416 LyXFont const & outerfont,
1417 pos_type initial) const
1419 bool emph_flag = false;
1421 LyXLayout_ptr const & style = layout();
1423 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1425 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1428 // parsing main loop
1429 for (pos_type i = initial; i < size(); ++i) {
1430 LyXFont font = getFont(buf.params(), i, outerfont);
1432 // handle <emphasis> tag
1433 if (font_old.emph() != font.emph()) {
1434 if (font.emph() == LyXFont::ON) {
1437 } else if (i != initial) {
1438 os << "</emphasis>";
1444 InsetBase const * inset = getInset(i);
1445 inset->docbook(buf, os, runparams);
1447 value_type c = getChar(i);
1450 boost::tie(ws, str) = sgml::escapeChar(c);
1452 if (style->pass_thru)
1461 os << "</emphasis>";
1464 if (style->free_spacing)
1466 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1473 /// return true if the char is a meta-character for an inset
1475 bool IsInsetChar(char c)
1477 return (c == Paragraph::META_INSET);
1484 bool Paragraph::isHfill(pos_type pos) const
1487 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1491 bool Paragraph::isNewline(pos_type pos) const
1494 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1498 bool Paragraph::isSeparator(pos_type pos) const
1500 return IsSeparatorChar(getChar(pos));
1504 bool Paragraph::isLineSeparator(pos_type pos) const
1506 value_type const c = getChar(pos);
1507 return IsLineSeparatorChar(c)
1508 || (IsInsetChar(c) && getInset(pos) &&
1509 getInset(pos)->isLineSeparator());
1513 /// Used by the spellchecker
1514 bool Paragraph::isLetter(pos_type pos) const
1517 return getInset(pos)->isLetter();
1519 value_type const c = getChar(pos);
1520 return IsLetterChar(c) || IsDigit(c);
1526 Paragraph::getParLanguage(BufferParams const & bparams) const
1529 return getFirstFontSettings().language();
1530 #ifdef WITH_WARNINGS
1531 #warning FIXME we should check the prev par as well (Lgb)
1533 return bparams.language;
1537 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1539 return lyxrc.rtl_support
1540 && getParLanguage(bparams)->RightToLeft()
1541 && ownerCode() != InsetBase::ERT_CODE;
1545 void Paragraph::changeLanguage(BufferParams const & bparams,
1546 Language const * from, Language const * to)
1548 for (pos_type i = 0; i < size(); ++i) {
1549 LyXFont font = getFontSettings(bparams, i);
1550 if (font.language() == from) {
1551 font.setLanguage(to);
1558 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1560 Language const * doc_language = bparams.language;
1561 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1562 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1564 for (; cit != end; ++cit)
1565 if (cit->font().language() != ignore_language &&
1566 cit->font().language() != latex_language &&
1567 cit->font().language() != doc_language)
1573 // Convert the paragraph to a string.
1574 // Used for building the table of contents
1575 string const Paragraph::asString(Buffer const & buffer, bool label) const
1577 OutputParams runparams;
1578 return asString(buffer, runparams, label);
1582 string const Paragraph::asString(Buffer const & buffer,
1583 OutputParams const & runparams,
1588 if (label && !params().labelString().empty())
1589 s += params().labelString() + ' ';
1591 for (pos_type i = 0; i < size(); ++i) {
1592 value_type c = getChar(i);
1595 else if (c == META_INSET &&
1596 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1598 getInset(i)->plaintext(buffer, os, runparams);
1599 s += subst(STRCONV(os.str()),'\n',' ');
1605 // This should really be done by the caller and not here.
1606 string ret = asString(buffer, runparams, 0, size(), label);
1607 return subst(ret, '\n', ' ');
1612 string const Paragraph::asString(Buffer const & buffer,
1613 pos_type beg, pos_type end, bool label) const
1616 OutputParams const runparams;
1617 return asString(buffer, runparams, beg, end, label);
1621 string const Paragraph::asString(Buffer const & buffer,
1622 OutputParams const & runparams,
1623 pos_type beg, pos_type end, bool label) const
1627 if (beg == 0 && label && !params().labelString().empty())
1628 os << params().labelString() << ' ';
1630 for (pos_type i = beg; i < end; ++i) {
1631 value_type const c = getUChar(buffer.params(), i);
1634 else if (c == META_INSET)
1635 getInset(i)->plaintext(buffer, os, runparams);
1642 void Paragraph::setInsetOwner(UpdatableInset * inset)
1644 pimpl_->inset_owner = inset;
1648 void Paragraph::setContentsFromPar(Paragraph const & par)
1650 pimpl_->setContentsFromPar(par);
1654 void Paragraph::trackChanges(Change::Type type)
1656 pimpl_->trackChanges(type);
1660 void Paragraph::untrackChanges()
1662 pimpl_->untrackChanges();
1666 void Paragraph::cleanChanges()
1668 pimpl_->cleanChanges();
1672 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1674 BOOST_ASSERT(empty() || pos < size());
1675 return pimpl_->lookupChange(pos);
1679 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1681 BOOST_ASSERT(empty() || pos < size());
1682 return pimpl_->lookupChangeFull(pos);
1686 bool Paragraph::isChanged(pos_type start, pos_type end) const
1688 return pimpl_->isChanged(start, end);
1692 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1694 return pimpl_->isChangeEdited(start, end);
1698 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1700 pimpl_->setChange(pos, type);
1704 void Paragraph::markErased()
1706 pimpl_->markErased();
1710 void Paragraph::acceptChange(pos_type start, pos_type end)
1712 return pimpl_->acceptChange(start, end);
1716 void Paragraph::rejectChange(pos_type start, pos_type end)
1718 return pimpl_->rejectChange(start, end);
1722 int Paragraph::id() const
1728 LyXLayout_ptr const & Paragraph::layout() const
1734 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1736 layout_ = new_layout;
1740 UpdatableInset * Paragraph::inInset() const
1742 return pimpl_->inset_owner;
1746 InsetBase::Code Paragraph::ownerCode() const
1748 return pimpl_->inset_owner
1749 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1753 void Paragraph::clearContents()
1759 void Paragraph::setChar(pos_type pos, value_type c)
1765 ParagraphParameters & Paragraph::params()
1767 return pimpl_->params;
1771 ParagraphParameters const & Paragraph::params() const
1773 return pimpl_->params;
1777 bool Paragraph::isFreeSpacing() const
1779 if (layout()->free_spacing)
1782 // for now we just need this, later should we need this in some
1783 // other way we can always add a function to InsetBase too.
1784 return ownerCode() == InsetBase::ERT_CODE;
1788 bool Paragraph::allowEmpty() const
1790 if (layout()->keepempty)
1792 return ownerCode() == InsetBase::ERT_CODE;
1796 Row & Paragraph::getRow(pos_type pos)
1798 RowList::iterator rit = rows_.end();
1799 RowList::iterator const begin = rows_.begin();
1801 for (--rit; rit != begin && rit->pos() > pos; --rit)
1808 Row const & Paragraph::getRow(pos_type pos) const
1810 RowList::const_iterator rit = rows_.end();
1811 RowList::const_iterator const begin = rows_.begin();
1813 for (--rit; rit != begin && rit->pos() > pos; --rit)
1820 size_t Paragraph::pos2row(pos_type pos) const
1822 RowList::const_iterator rit = rows_.end();
1823 RowList::const_iterator const begin = rows_.begin();
1825 for (--rit; rit != begin && rit->pos() > pos; --rit)
1832 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1834 if (!Encodings::is_arabic(c))
1835 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1836 return c + (0xb0 - '0');
1840 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1841 unsigned char next_char = ' ';
1843 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1844 unsigned char const par_char = getChar(i);
1845 if (!Encodings::IsComposeChar_arabic(par_char)) {
1846 next_char = par_char;
1851 if (Encodings::is_arabic(next_char)) {
1852 if (Encodings::is_arabic(prev_char) &&
1853 !Encodings::is_arabic_special(prev_char))
1854 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1856 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1858 if (Encodings::is_arabic(prev_char) &&
1859 !Encodings::is_arabic_special(prev_char))
1860 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1862 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1867 void Paragraph::dump() const
1869 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1870 for (size_t i = 0; i != rows_.size(); ++i) {
1871 lyxerr << " row " << i << ": ";
1876 //void Paragraph::metrics(MetricsInfo & mi, Dimension & dim, LyXText & text)
1881 //void draw(PainterInfo & pi, int x, int y, LyXText & text) const