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>
58 using lyx::support::subst;
66 using std::ostringstream;
69 Paragraph::Paragraph()
70 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
77 Paragraph::Paragraph(Paragraph const & par)
78 : itemdepth(par.itemdepth), insetlist(par.insetlist),
80 rows_(par.rows_), rowSignature_(par.rowSignature_),
82 text_(par.text_), begin_of_body_(par.begin_of_body_),
83 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
85 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
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 Paragraph & Paragraph::operator=(Paragraph const & par)
95 // needed as we will destroy the pimpl_ before copying it
97 itemdepth = par.itemdepth;
99 insetlist = par.insetlist;
100 InsetList::iterator it = insetlist.begin();
101 InsetList::iterator end = insetlist.end();
102 for (; it != end; ++it)
103 it->inset = it->inset->clone().release();
107 rowSignature_ = par.rowSignature_;
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;
167 // Write font changes
168 LyXFont font2 = getFontSettings(bparams, i);
169 if (font2 != font1) {
170 font2.lyxWriteChanges(font1, os);
175 value_type const c = getChar(i);
179 InsetBase const * inset = getInset(i);
181 if (inset->directWrite()) {
182 // international char, let it write
183 // code directly so it's shorter in
185 inset->write(buf, os);
189 os << "\\begin_inset ";
190 inset->write(buf, os);
191 os << "\n\\end_inset\n\n";
197 os << "\n\\backslash\n";
201 if (i + 1 < size() && getChar(i + 1) == ' ') {
208 if ((column > 70 && c == ' ')
213 // this check is to amend a bug. LyX sometimes
214 // inserts '\0' this could cause problems.
218 lyxerr << "ERROR (Paragraph::writeFile):"
219 " NULL char in structure." << endl;
225 os << "\n\\end_layout\n";
229 void Paragraph::validate(LaTeXFeatures & features) const
231 pimpl_->validate(features, *layout());
235 void Paragraph::eraseIntern(lyx::pos_type pos)
237 pimpl_->eraseIntern(pos);
241 bool Paragraph::erase(pos_type pos)
243 return pimpl_->erase(pos);
247 int Paragraph::erase(pos_type start, pos_type end)
249 return pimpl_->erase(start, end);
253 void Paragraph::insert(pos_type start, string const & str,
254 LyXFont const & font)
256 for (size_t i = 0, n = str.size(); i != n ; ++i)
257 insertChar(start + i, str[i], font);
261 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
264 pimpl_->insertChar(pos, c, change);
268 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
269 LyXFont const & font, Change change)
271 pimpl_->insertChar(pos, c, change);
276 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
278 pimpl_->insertInset(pos, inset, change);
282 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
283 LyXFont const & font, Change change)
285 pimpl_->insertInset(pos, inset, change);
290 bool Paragraph::insetAllowed(InsetBase_code code)
292 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
296 // Gets uninstantiated font setting at position.
297 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
301 lyxerr << " pos: " << pos << " size: " << size() << endl;
302 BOOST_ASSERT(pos <= size());
305 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
306 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
307 for (; cit != end; ++cit)
308 if (cit->pos() >= pos)
314 if (pos == size() && !empty())
315 return getFontSettings(bparams, pos - 1);
317 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
321 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
323 BOOST_ASSERT(pos <= size());
324 lyx::pos_type start = 0;
326 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
327 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
328 for (; cit != end; ++cit) {
329 if (cit->pos() >= pos) {
330 if (pos >= beginOfBody())
331 return FontSpan(std::max(start, beginOfBody()),
334 return FontSpan(start,
335 std::min(beginOfBody() - 1,
338 start = cit->pos() + 1;
341 // This should not happen, but if so, we take no chances.
342 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
344 return FontSpan(pos, pos);
348 // Gets uninstantiated font setting at position 0
349 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
351 if (!empty() && !pimpl_->fontlist.empty())
352 return pimpl_->fontlist[0].font();
354 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
358 // Gets the fully instantiated font at a given position in a paragraph
359 // This is basically the same function as LyXText::GetFont() in text2.C.
360 // The difference is that this one is used for generating the LaTeX file,
361 // and thus cosmetic "improvements" are disallowed: This has to deliver
362 // the true picture of the buffer. (Asger)
363 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
364 LyXFont const & outerfont) const
366 BOOST_ASSERT(pos >= 0);
368 LyXLayout_ptr const & lout = layout();
370 pos_type const body_pos = beginOfBody();
374 layoutfont = lout->labelfont;
376 layoutfont = lout->font;
378 LyXFont font = getFontSettings(bparams, pos);
379 font.realize(layoutfont);
380 font.realize(outerfont);
381 font.realize(bparams.getLyXTextClass().defaultfont());
387 LyXFont const Paragraph::getLabelFont
388 (BufferParams const & bparams, LyXFont const & outerfont) const
390 LyXFont tmpfont = layout()->labelfont;
391 tmpfont.setLanguage(getParLanguage(bparams));
392 tmpfont.realize(outerfont);
393 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
398 LyXFont const Paragraph::getLayoutFont
399 (BufferParams const & bparams, LyXFont const & outerfont) const
401 LyXFont tmpfont = layout()->font;
402 tmpfont.setLanguage(getParLanguage(bparams));
403 tmpfont.realize(outerfont);
404 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
409 /// Returns the height of the highest font in range
410 LyXFont_size Paragraph::highestFontInRange
411 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
413 if (pimpl_->fontlist.empty())
416 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
417 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
418 for (; end_it != end; ++end_it) {
419 if (end_it->pos() >= endpos)
426 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
427 for (; cit != end; ++cit) {
428 if (cit->pos() >= startpos)
432 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
433 for (; cit != end_it; ++cit) {
434 LyXFont::FONT_SIZE size = cit->font().size();
435 if (size == LyXFont::INHERIT_SIZE)
437 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
444 Paragraph::value_type
445 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
447 value_type c = getChar(pos);
448 if (!lyxrc.rtl_support)
478 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
485 void Paragraph::setFont(pos_type pos, LyXFont const & font)
487 BOOST_ASSERT(pos <= size());
489 // First, reduce font against layout/label font
490 // Update: The setCharFont() routine in text2.C already
491 // reduces font, so we don't need to do that here. (Asger)
492 // No need to simplify this because it will disappear
493 // in a new kernel. (Asger)
494 // Next search font table
496 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
497 Pimpl::FontList::iterator it = beg;
498 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
499 for (; it != endit; ++it) {
500 if (it->pos() >= pos)
503 size_t const i = distance(beg, it);
504 bool notfound = (it == endit);
506 if (!notfound && pimpl_->fontlist[i].font() == font)
509 bool begin = pos == 0 || notfound ||
510 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
511 // Is position pos is a beginning of a font block?
512 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
513 // Is position pos is the end of a font block?
514 if (begin && end) { // A single char block
515 if (i + 1 < pimpl_->fontlist.size() &&
516 pimpl_->fontlist[i + 1].font() == font) {
517 // Merge the singleton block with the next block
518 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
519 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
520 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
521 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
522 // Merge the singleton block with the previous block
523 pimpl_->fontlist[i - 1].pos(pos);
524 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 pimpl_->fontlist[i].font(font);
528 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
529 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
532 Pimpl::FontTable(pos, font));
534 pimpl_->fontlist[i].pos(pos - 1);
535 if (!(i + 1 < pimpl_->fontlist.size() &&
536 pimpl_->fontlist[i + 1].font() == font))
537 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
538 Pimpl::FontTable(pos, font));
539 } else { // The general case. The block is splitted into 3 blocks
540 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
541 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
542 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
543 Pimpl::FontTable(pos, font));
548 void Paragraph::makeSameLayout(Paragraph const & par)
550 layout(par.layout());
552 params() = par.params();
556 int Paragraph::stripLeadingSpaces()
562 while (!empty() && (isNewline(0) || isLineSeparator(0))
563 && (lookupChange(0) != Change::DELETED)) {
572 bool Paragraph::hasSameLayout(Paragraph const & par) const
574 return par.layout() == layout() && params().sameLayout(par.params());
578 Paragraph::depth_type Paragraph::getDepth() const
580 return params().depth();
584 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
586 if (layout()->isEnvironment())
587 return params().depth() + 1;
589 return params().depth();
593 char Paragraph::getAlign() const
595 return params().align();
599 string const & Paragraph::getLabelstring() const
601 return params().labelString();
605 // the next two functions are for the manual labels
606 string const Paragraph::getLabelWidthString() const
608 if (!params().labelWidthString().empty())
609 return params().labelWidthString();
611 return _("Senseless with this layout!");
615 void Paragraph::setLabelWidthString(string const & s)
617 params().labelWidthString(s);
621 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
624 params().labelWidthString(string());
625 params().align(LYX_ALIGN_LAYOUT);
626 params().spacing(Spacing(Spacing::Default));
630 pos_type Paragraph::beginOfBody() const
632 return begin_of_body_;
636 void Paragraph::setBeginOfBody()
638 if (layout()->labeltype != LABEL_MANUAL) {
643 // Unroll the first two cycles of the loop
644 // and remember the previous character to
645 // remove unnecessary getChar() calls
647 pos_type end = size();
648 if (i < end && !isNewline(i)) {
650 char previous_char = 0;
653 previous_char = text_[i];
656 while (i < end && previous_char != ' ') {
661 previous_char = temp;
671 // returns -1 if inset not found
672 int Paragraph::getPositionOfInset(InsetBase const * inset) const
675 InsetList::const_iterator it = insetlist.begin();
676 InsetList::const_iterator end = insetlist.end();
677 for (; it != end; ++it)
678 if (it->inset == inset)
684 InsetBibitem * Paragraph::bibitem() const
686 if (!insetlist.empty()) {
687 InsetBase * inset = insetlist.begin()->inset;
688 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
689 return static_cast<InsetBibitem *>(inset);
695 bool Paragraph::forceDefaultParagraphs() const
697 return inInset() && inInset()->forceDefaultParagraphs(0);
703 // paragraphs inside floats need different alignment tags to avoid
706 bool noTrivlistCentering(InsetBase::Code code)
708 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
712 string correction(string const & orig)
714 if (orig == "flushleft")
715 return "raggedright";
716 if (orig == "flushright")
718 if (orig == "center")
724 string const corrected_env(string const & suffix, string const & env,
725 InsetBase::Code code)
727 string output = suffix + "{";
728 if (noTrivlistCentering(code))
729 output += correction(env);
738 // This could go to ParagraphParameters if we want to
739 int Paragraph::startTeXParParams(BufferParams const & bparams,
740 ostream & os, bool moving_arg) const
744 if (params().noindent()) {
749 switch (params().align()) {
751 case LYX_ALIGN_BLOCK:
752 case LYX_ALIGN_LAYOUT:
753 case LYX_ALIGN_SPECIAL:
756 case LYX_ALIGN_RIGHT:
757 case LYX_ALIGN_CENTER:
765 switch (params().align()) {
767 case LYX_ALIGN_BLOCK:
768 case LYX_ALIGN_LAYOUT:
769 case LYX_ALIGN_SPECIAL:
771 case LYX_ALIGN_LEFT: {
773 if (getParLanguage(bparams)->babel() != "hebrew")
774 output = corrected_env("\\begin", "flushleft", ownerCode());
776 output = corrected_env("\\begin", "flushright", ownerCode());
778 column += output.size();
780 } case LYX_ALIGN_RIGHT: {
782 if (getParLanguage(bparams)->babel() != "hebrew")
783 output = corrected_env("\\begin", "flushright", ownerCode());
785 output = corrected_env("\\begin", "flushleft", ownerCode());
787 column += output.size();
789 } case LYX_ALIGN_CENTER: {
791 output = corrected_env("\\begin", "center", ownerCode());
793 column += output.size();
802 // This could go to ParagraphParameters if we want to
803 int Paragraph::endTeXParParams(BufferParams const & bparams,
804 ostream & os, bool moving_arg) const
808 switch (params().align()) {
810 case LYX_ALIGN_BLOCK:
811 case LYX_ALIGN_LAYOUT:
812 case LYX_ALIGN_SPECIAL:
815 case LYX_ALIGN_RIGHT:
816 case LYX_ALIGN_CENTER:
824 switch (params().align()) {
826 case LYX_ALIGN_BLOCK:
827 case LYX_ALIGN_LAYOUT:
828 case LYX_ALIGN_SPECIAL:
830 case LYX_ALIGN_LEFT: {
832 if (getParLanguage(bparams)->babel() != "hebrew")
833 output = corrected_env("\\par\\end", "flushleft", ownerCode());
835 output = corrected_env("\\par\\end", "flushright", ownerCode());
837 column += output.size();
839 } case LYX_ALIGN_RIGHT: {
841 if (getParLanguage(bparams)->babel() != "hebrew")
842 output = corrected_env("\\par\\end", "flushright", ownerCode());
844 output = corrected_env("\\par\\end", "flushleft", ownerCode());
846 column += output.size();
848 } case LYX_ALIGN_CENTER: {
850 output = corrected_env("\\par\\end", "center", ownerCode());
852 column += output.size();
861 // This one spits out the text of the paragraph
862 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
863 BufferParams const & bparams,
864 LyXFont const & outerfont,
865 ostream & os, TexRow & texrow,
866 OutputParams const & runparams) const
868 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
870 bool return_value = false;
874 // well we have to check if we are in an inset with unlimited
875 // length (all in one row) if that is true then we don't allow
876 // any special options in the paragraph and also we don't allow
877 // any environment other then "Standard" to be valid!
878 bool asdefault = forceDefaultParagraphs();
881 style = bparams.getLyXTextClass().defaultLayout();
888 LaTeXFeatures features(buf, bparams, runparams);
890 // output change tracking marks only if desired,
891 // if dvipost is installed,
892 // and with dvi/ps (other formats don't work)
893 bool const output = bparams.output_changes
894 && runparams.flavor == OutputParams::LATEX
895 && features.isAvailable("dvipost");
897 // Maybe we have to create a optional argument.
898 pos_type body_pos = beginOfBody();
899 unsigned int column = 0;
902 // the optional argument is kept in curly brackets in
903 // case it contains a ']'
906 basefont = getLabelFont(bparams, outerfont);
908 basefont = getLayoutFont(bparams, outerfont);
911 // Which font is currently active?
912 LyXFont running_font(basefont);
913 // Do we have an open font change?
914 bool open_font = false;
916 Change::Type running_change = Change::UNCHANGED;
918 texrow.start(id(), 0);
920 // if the paragraph is empty, the loop will not be entered at all
922 if (style->isCommand()) {
927 column += startTeXParParams(bparams, os,
928 runparams.moving_arg);
931 for (pos_type i = 0; i < size(); ++i) {
933 // First char in paragraph or after label?
937 column += running_font.latexWriteEndChanges(os, basefont, basefont);
940 basefont = getLayoutFont(bparams, outerfont);
941 running_font = basefont;
945 if (style->isCommand()) {
951 column += startTeXParParams(bparams, os,
952 runparams.moving_arg);
955 value_type c = getChar(i);
957 // Fully instantiated font
958 LyXFont font = getFont(bparams, i, outerfont);
960 LyXFont const last_font = running_font;
962 // Spaces at end of font change are simulated to be
963 // outside font change, i.e. we write "\textXX{text} "
964 // rather than "\textXX{text }". (Asger)
965 if (open_font && c == ' ' && i <= size() - 2) {
966 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
967 if (next_font != running_font && next_font != font) {
972 // We end font definition before blanks
974 (font != running_font ||
975 font.language() != running_font.language()))
977 column += running_font.latexWriteEndChanges(os,
979 (i == body_pos-1) ? basefont : font);
980 running_font = basefont;
984 // Blanks are printed before start of fontswitch
986 // Do not print the separation of the optional argument
987 if (i != body_pos - 1) {
988 pimpl_->simpleTeXBlanks(os, texrow, i,
989 column, font, *style);
993 // Do we need to change font?
994 if ((font != running_font ||
995 font.language() != running_font.language()) &&
998 column += font.latexWriteStartChanges(os, basefont,
1000 running_font = font;
1004 Change::Type change = pimpl_->lookupChange(i);
1006 column += Changes::latexMarkChange(os, running_change,
1008 running_change = change;
1010 // do not output text which is marked deleted
1011 // if change tracking output is not desired
1012 if (output || running_change != Change::DELETED) {
1013 OutputParams rp = runparams;
1014 rp.free_spacing = style->free_spacing;
1015 rp.local_font = &font;
1016 rp.intitle = style->intitle;
1017 pimpl_->simpleTeXSpecialChars(buf, bparams,
1020 basefont, outerfont, open_font,
1022 *style, i, column, c);
1026 column += Changes::latexMarkChange(os,
1027 running_change, Change::UNCHANGED, output);
1029 // If we have an open font definition, we have to close it
1031 #ifdef FIXED_LANGUAGE_END_DETECTION
1034 .latexWriteEndChanges(os, basefont,
1035 next_->getFont(bparams,
1038 running_font.latexWriteEndChanges(os, basefont,
1042 #ifdef WITH_WARNINGS
1043 //#warning For now we ALWAYS have to close the foreign font settings if they are
1044 //#warning there as we start another \selectlanguage with the next paragraph if
1045 //#warning we are in need of this. This should be fixed sometime (Jug)
1047 running_font.latexWriteEndChanges(os, basefont, basefont);
1051 // Needed if there is an optional argument but no contents.
1052 if (body_pos > 0 && body_pos == size()) {
1054 return_value = false;
1058 column += endTeXParParams(bparams, os, runparams.moving_arg);
1061 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1062 return return_value;
1068 // checks, if newcol chars should be put into this line
1069 // writes newline, if necessary.
1070 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1071 string::size_type newcol)
1074 if (colcount > lyxrc.ascii_linelen) {
1076 colcount = newcol; // assume write after this call
1091 string tag_name(PAR_TAG const & pt) {
1093 case PAR_NONE: return "!-- --";
1094 case TT: return "tt";
1095 case SF: return "sf";
1096 case BF: return "bf";
1097 case IT: return "it";
1098 case SL: return "sl";
1099 case EM: return "em";
1106 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1108 p1 = static_cast<PAR_TAG>(p1 | p2);
1113 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1115 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1121 // Handle internal paragraph parsing -- layout already processed.
1122 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1124 LyXFont const & outerfont,
1125 OutputParams const & runparams,
1126 lyx::depth_type /*depth*/) const
1128 LyXLayout_ptr const & style = layout();
1130 string::size_type char_line_count = 5; // Heuristic choice ;-)
1132 // gets paragraph main font
1135 if (style->labeltype == LABEL_MANUAL) {
1136 font_old = style->labelfont;
1139 font_old = style->font;
1143 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1144 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1145 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1148 stack<PAR_TAG> tag_state;
1149 // parsing main loop
1150 for (pos_type i = 0; i < size(); ++i) {
1152 PAR_TAG tag_close = PAR_NONE;
1153 list < PAR_TAG > tag_open;
1155 LyXFont const font = getFont(buf.params(), i, outerfont);
1157 if (font_old.family() != font.family()) {
1158 switch (family_type) {
1159 case LyXFont::SANS_FAMILY:
1162 case LyXFont::TYPEWRITER_FAMILY:
1169 family_type = font.family();
1171 switch (family_type) {
1172 case LyXFont::SANS_FAMILY:
1173 tag_open.push_back(SF);
1175 case LyXFont::TYPEWRITER_FAMILY:
1176 tag_open.push_back(TT);
1183 if (font_old.series() != font.series()) {
1184 switch (series_type) {
1185 case LyXFont::BOLD_SERIES:
1192 series_type = font.series();
1194 switch (series_type) {
1195 case LyXFont::BOLD_SERIES:
1196 tag_open.push_back(BF);
1204 if (font_old.shape() != font.shape()) {
1205 switch (shape_type) {
1206 case LyXFont::ITALIC_SHAPE:
1209 case LyXFont::SLANTED_SHAPE:
1216 shape_type = font.shape();
1218 switch (shape_type) {
1219 case LyXFont::ITALIC_SHAPE:
1220 tag_open.push_back(IT);
1222 case LyXFont::SLANTED_SHAPE:
1223 tag_open.push_back(SL);
1230 if (font_old.emph() != font.emph()) {
1231 if (font.emph() == LyXFont::ON) {
1232 tag_open.push_back(EM);
1241 list < PAR_TAG > temp;
1242 while (!tag_state.empty() && tag_close) {
1243 PAR_TAG k = tag_state.top();
1245 os << "</" << tag_name(k) << '>';
1252 for(list< PAR_TAG >::const_iterator j = temp.begin();
1253 j != temp.end(); ++j) {
1255 os << '<' << tag_name(*j) << '>';
1258 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1259 j != tag_open.end(); ++j) {
1261 os << '<' << tag_name(*j) << '>';
1264 value_type c = getChar(i);
1267 if (c == Paragraph::META_INSET) {
1268 getInset(i)->linuxdoc(buf, os, runparams);
1273 if (style->latexparam() == "CDATA") {
1274 // "TeX"-Mode on == > SGML-Mode on.
1281 boost::tie(ws, str) = sgml::escapeChar(c);
1282 if (ws && !isFreeSpacing()) {
1283 // in freespacing mode, spaces are
1284 // non-breaking characters
1285 if (desc_on) { // if char is ' ' then...
1287 sgmlLineBreak(os, char_line_count, 6);
1291 sgmlLineBreak(os, char_line_count, 1);
1296 char_line_count += str.length();
1302 while (!tag_state.empty()) {
1303 os << "</" << tag_name(tag_state.top()) << '>';
1307 // resets description flag correctly
1309 // <tag> not closed...
1310 sgmlLineBreak(os, char_line_count, 6);
1316 bool Paragraph::emptyTag() const
1318 for (pos_type i = 0; i < size(); ++i) {
1320 InsetBase const * inset = getInset(i);
1321 InsetBase::Code lyx_code = inset->lyxCode();
1322 if (lyx_code != InsetBase::TOC_CODE &&
1323 lyx_code != InsetBase::INCLUDE_CODE &&
1324 lyx_code != InsetBase::GRAPHICS_CODE &&
1325 lyx_code != InsetBase::ERT_CODE &&
1326 lyx_code != InsetBase::FLOAT_CODE &&
1327 lyx_code != InsetBase::TABULAR_CODE) {
1331 value_type c = getChar(i);
1332 if (c != ' ' && c != '\t')
1340 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1342 for (pos_type i = 0; i < size(); ++i) {
1344 InsetBase const * inset = getInset(i);
1345 InsetBase::Code lyx_code = inset->lyxCode();
1346 if (lyx_code == InsetBase::LABEL_CODE) {
1347 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1348 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1357 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1360 for (i = 0; i < size(); ++i) {
1362 InsetBase const * inset = getInset(i);
1363 inset->docbook(buf, os, runparams);
1365 value_type c = getChar(i);
1370 boost::tie(ws, str) = sgml::escapeChar(c);
1379 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1383 for (pos_type i = initial; i < size(); ++i) {
1384 LyXFont font = getFont(buf.params(), i, outerfont);
1387 if (i != initial && font != font_old)
1396 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1398 OutputParams const & runparams,
1399 LyXFont const & outerfont,
1400 pos_type initial) const
1402 bool emph_flag = false;
1404 LyXLayout_ptr const & style = layout();
1406 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1408 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1411 // parsing main loop
1412 for (pos_type i = initial; i < size(); ++i) {
1413 LyXFont font = getFont(buf.params(), i, outerfont);
1415 // handle <emphasis> tag
1416 if (font_old.emph() != font.emph()) {
1417 if (font.emph() == LyXFont::ON) {
1420 } else if (i != initial) {
1421 os << "</emphasis>";
1427 InsetBase const * inset = getInset(i);
1428 inset->docbook(buf, os, runparams);
1430 value_type c = getChar(i);
1433 boost::tie(ws, str) = sgml::escapeChar(c);
1435 if (style->pass_thru)
1444 os << "</emphasis>";
1447 if (style->free_spacing)
1449 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1454 bool Paragraph::isNewline(pos_type pos) const
1457 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1461 bool Paragraph::isLineSeparator(pos_type pos) const
1463 value_type const c = getChar(pos);
1464 return isLineSeparatorChar(c)
1465 || (c == Paragraph::META_INSET && getInset(pos) &&
1466 getInset(pos)->isLineSeparator());
1470 /// Used by the spellchecker
1471 bool Paragraph::isLetter(pos_type pos) const
1474 return getInset(pos)->isLetter();
1476 value_type const c = getChar(pos);
1477 return isLetterChar(c) || isDigit(c);
1483 Paragraph::getParLanguage(BufferParams const & bparams) const
1486 return getFirstFontSettings(bparams).language();
1487 #ifdef WITH_WARNINGS
1488 #warning FIXME we should check the prev par as well (Lgb)
1490 return bparams.language;
1494 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1496 return lyxrc.rtl_support
1497 && getParLanguage(bparams)->rightToLeft()
1498 && ownerCode() != InsetBase::ERT_CODE;
1502 void Paragraph::changeLanguage(BufferParams const & bparams,
1503 Language const * from, Language const * to)
1505 for (pos_type i = 0; i < size(); ++i) {
1506 LyXFont font = getFontSettings(bparams, i);
1507 if (font.language() == from) {
1508 font.setLanguage(to);
1515 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1517 Language const * doc_language = bparams.language;
1518 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1519 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1521 for (; cit != end; ++cit)
1522 if (cit->font().language() != ignore_language &&
1523 cit->font().language() != latex_language &&
1524 cit->font().language() != doc_language)
1530 // Convert the paragraph to a string.
1531 // Used for building the table of contents
1532 string const Paragraph::asString(Buffer const & buffer, bool label) const
1534 OutputParams runparams;
1535 return asString(buffer, runparams, label);
1539 string const Paragraph::asString(Buffer const & buffer,
1540 OutputParams const & runparams,
1545 if (label && !params().labelString().empty())
1546 s += params().labelString() + ' ';
1548 for (pos_type i = 0; i < size(); ++i) {
1549 value_type c = getChar(i);
1552 else if (c == META_INSET &&
1553 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1555 getInset(i)->plaintext(buffer, os, runparams);
1556 s += subst(STRCONV(os.str()),'\n',' ');
1562 // This should really be done by the caller and not here.
1563 string ret = asString(buffer, runparams, 0, size(), label);
1564 return subst(ret, '\n', ' ');
1569 string const Paragraph::asString(Buffer const & buffer,
1570 pos_type beg, pos_type end, bool label) const
1573 OutputParams const runparams;
1574 return asString(buffer, runparams, beg, end, label);
1578 string const Paragraph::asString(Buffer const & buffer,
1579 OutputParams const & runparams,
1580 pos_type beg, pos_type end, bool label) const
1584 if (beg == 0 && label && !params().labelString().empty())
1585 os << params().labelString() << ' ';
1587 for (pos_type i = beg; i < end; ++i) {
1588 value_type const c = getUChar(buffer.params(), i);
1591 else if (c == META_INSET)
1592 getInset(i)->textString(buffer, os, runparams);
1599 void Paragraph::setInsetOwner(InsetBase * inset)
1601 pimpl_->inset_owner = inset;
1605 void Paragraph::setContentsFromPar(Paragraph const & par)
1607 pimpl_->setContentsFromPar(par);
1611 void Paragraph::trackChanges(Change::Type type)
1613 pimpl_->trackChanges(type);
1617 void Paragraph::untrackChanges()
1619 pimpl_->untrackChanges();
1623 void Paragraph::cleanChanges()
1625 pimpl_->cleanChanges();
1629 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1631 BOOST_ASSERT(pos <= size());
1632 return pimpl_->lookupChange(pos);
1636 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1638 BOOST_ASSERT(pos <= size());
1639 return pimpl_->lookupChangeFull(pos);
1643 bool Paragraph::isChanged(pos_type start, pos_type end) const
1645 return pimpl_->isChanged(start, end);
1649 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1651 return pimpl_->isChangeEdited(start, end);
1655 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1657 pimpl_->setChange(pos, type);
1661 void Paragraph::setChangeFull(lyx::pos_type pos, Change change)
1663 pimpl_->setChangeFull(pos, change);
1667 void Paragraph::markErased(bool erased)
1669 pimpl_->markErased(erased);
1673 void Paragraph::acceptChange(pos_type start, pos_type end)
1675 return pimpl_->acceptChange(start, end);
1679 void Paragraph::rejectChange(pos_type start, pos_type end)
1681 return pimpl_->rejectChange(start, end);
1685 int Paragraph::id() const
1691 LyXLayout_ptr const & Paragraph::layout() const
1697 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1699 layout_ = new_layout;
1703 InsetBase * Paragraph::inInset() const
1705 return pimpl_->inset_owner;
1709 InsetBase::Code Paragraph::ownerCode() const
1711 return pimpl_->inset_owner
1712 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1716 void Paragraph::clearContents()
1722 void Paragraph::setChar(pos_type pos, value_type c)
1728 ParagraphParameters & Paragraph::params()
1730 return pimpl_->params;
1734 ParagraphParameters const & Paragraph::params() const
1736 return pimpl_->params;
1740 bool Paragraph::isFreeSpacing() const
1742 if (layout()->free_spacing)
1745 // for now we just need this, later should we need this in some
1746 // other way we can always add a function to InsetBase too.
1747 return ownerCode() == InsetBase::ERT_CODE;
1751 bool Paragraph::allowEmpty() const
1753 if (layout()->keepempty)
1755 return ownerCode() == InsetBase::ERT_CODE;
1759 Row & Paragraph::getRow(pos_type pos, bool boundary)
1761 BOOST_ASSERT(!rows().empty());
1763 // If boundary is set we should return the row on which
1764 // the character before is inside.
1765 if (pos > 0 && boundary)
1768 RowList::iterator rit = rows_.end();
1769 RowList::iterator const begin = rows_.begin();
1771 for (--rit; rit != begin && rit->pos() > pos; --rit)
1778 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1780 BOOST_ASSERT(!rows().empty());
1782 // If boundary is set we should return the row on which
1783 // the character before is inside.
1784 if (pos > 0 && boundary)
1787 RowList::const_iterator rit = rows_.end();
1788 RowList::const_iterator const begin = rows_.begin();
1790 for (--rit; rit != begin && rit->pos() > pos; --rit)
1797 size_t Paragraph::pos2row(pos_type pos) const
1799 BOOST_ASSERT(!rows().empty());
1801 RowList::const_iterator rit = rows_.end();
1802 RowList::const_iterator const begin = rows_.begin();
1804 for (--rit; rit != begin && rit->pos() > pos; --rit)
1811 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1813 if (!Encodings::is_arabic(c))
1814 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1815 return c + (0xb0 - '0');
1819 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1820 unsigned char next_char = ' ';
1822 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1823 unsigned char const par_char = getChar(i);
1824 if (!Encodings::isComposeChar_arabic(par_char)) {
1825 next_char = par_char;
1830 if (Encodings::is_arabic(next_char)) {
1831 if (Encodings::is_arabic(prev_char) &&
1832 !Encodings::is_arabic_special(prev_char))
1833 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1835 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1837 if (Encodings::is_arabic(prev_char) &&
1838 !Encodings::is_arabic_special(prev_char))
1839 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1841 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1846 void Paragraph::dump() const
1848 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1849 for (size_t i = 0; i != rows_.size(); ++i) {
1850 lyxerr << " row " << i << ": ";