X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FParagraph.cpp;h=e46faab4b922848e6487ea7077d4ab64f138aa4b;hb=97e5041a4abfec0820b8c8662577e83ce0e6c3cd;hp=2baa9b922e9ae4703fd3524f86c776835c5c56a8;hpb=1199f092d4463d84b5b752ca2df0f61354fedb85;p=lyx.git diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 2baa9b922e..e46faab4b9 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -109,15 +109,15 @@ public: /// specified by the latex macro \p ltx, to \p os starting from \p i. /// \return the number of characters written. int writeScriptChars(odocstream & os, docstring const & ltx, - Change &, Encoding const &, pos_type & i); + Change const &, Encoding const &, pos_type & i); /// This could go to ParagraphParameters if we want to. int startTeXParParams(BufferParams const &, odocstream &, TexRow &, - bool) const; + OutputParams const &) const; /// This could go to ParagraphParameters if we want to. int endTeXParParams(BufferParams const &, odocstream &, TexRow &, - bool) const; + OutputParams const &) const; /// void latexInset(BufferParams const &, @@ -135,9 +135,9 @@ public: /// void latexSpecialChar( odocstream & os, - OutputParams & runparams, - Font & running_font, - Change & running_change, + OutputParams const & runparams, + Font const & running_font, + Change const & running_change, Layout const & style, pos_type & i, unsigned int & column); @@ -146,20 +146,20 @@ public: bool latexSpecialT1( char_type const c, odocstream & os, - pos_type & i, + pos_type i, unsigned int & column); /// bool latexSpecialTypewriter( char_type const c, odocstream & os, - pos_type & i, + pos_type i, unsigned int & column); /// bool latexSpecialPhrase( odocstream & os, pos_type & i, unsigned int & column, - OutputParams & runparams); + OutputParams const & runparams); /// void validate(LaTeXFeatures & features, @@ -279,6 +279,13 @@ Paragraph::Private::Private(Private const & p, Paragraph * owner, } +void Paragraph::addChangesToToc(DocIterator const & cdit, + Buffer const & buf) const +{ + d->changes_.addToToc(cdit, buf); +} + + bool Paragraph::isChanged(pos_type start, pos_type end) const { LASSERT(start >= 0 && start <= size(), /**/); @@ -442,17 +449,23 @@ void Paragraph::Private::insertChar(pos_type pos, char_type c, } -void Paragraph::insertInset(pos_type pos, Inset * inset, +bool Paragraph::insertInset(pos_type pos, Inset * inset, Change const & change) { LASSERT(inset, /**/); LASSERT(pos >= 0 && pos <= size(), /**/); + // Paragraph::insertInset() can be used in cut/copy/paste operation where + // d->inset_owner_ is not set yet. + if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode())) + return false; + d->insertChar(pos, META_INSET, change); LASSERT(d->text_[pos] == META_INSET, /**/); // Add a new entry in the insetlist_. d->insetlist_.insert(inset, pos); + return true; } @@ -590,7 +603,7 @@ bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams, int Paragraph::Private::writeScriptChars(odocstream & os, docstring const & ltx, - Change & runningChange, + Change const & runningChange, Encoding const & encoding, pos_type & i) { @@ -827,9 +840,9 @@ void Paragraph::Private::latexInset( void Paragraph::Private::latexSpecialChar( odocstream & os, - OutputParams & runparams, - Font & running_font, - Change & running_change, + OutputParams const & runparams, + Font const & running_font, + Change const & running_change, Layout const & style, pos_type & i, unsigned int & column) @@ -957,7 +970,7 @@ void Paragraph::Private::latexSpecialChar( bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os, - pos_type & i, unsigned int & column) + pos_type i, unsigned int & column) { switch (c) { case '>': @@ -985,7 +998,7 @@ bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os, bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os, - pos_type & i, unsigned int & column) + pos_type i, unsigned int & column) { switch (c) { case '-': @@ -1007,7 +1020,7 @@ bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i, - unsigned int & column, OutputParams & runparams) + unsigned int & column, OutputParams const & runparams) { // FIXME: if we have "LaTeX" with a font // change in the middle (before the 'T', then @@ -1165,7 +1178,7 @@ void Paragraph::write(ostream & os, BufferParams const & bparams, break; // Write font changes - Font font2 = getFontSettings(bparams, i); + Font const & font2 = getFontSettings(bparams, i); if (font2 != font1) { font2.lyxWriteChanges(font1, os); column = 0; @@ -1296,12 +1309,13 @@ void Paragraph::insertChar(pos_type pos, char_type c, } -void Paragraph::insertInset(pos_type pos, Inset * inset, +bool Paragraph::insertInset(pos_type pos, Inset * inset, Font const & font, Change const & change) { - insertInset(pos, inset, change); + bool const success = insertInset(pos, inset, change); // Set the font/language of the inset... setFont(pos, font); + return success; } @@ -1313,7 +1327,7 @@ void Paragraph::resetFonts(Font const & font) } // Gets uninstantiated font setting at position. -Font const Paragraph::getFontSettings(BufferParams const & bparams, +Font const & Paragraph::getFontSettings(BufferParams const & bparams, pos_type pos) const { if (pos > size()) { @@ -1328,7 +1342,16 @@ Font const Paragraph::getFontSettings(BufferParams const & bparams, if (pos == size() && !empty()) return getFontSettings(bparams, pos - 1); - return Font(inherit_font, getParLanguage(bparams)); + // Optimisation: avoid a full font instantiation if there is no + // language change from previous call. + static Font previous_font; + static Language const * previous_lang = 0; + Language const * lang = getParLanguage(bparams); + if (lang != previous_lang) { + previous_lang = lang; + previous_font = Font(inherit_font, lang); + } + return previous_font; } @@ -1359,12 +1382,21 @@ FontSpan Paragraph::fontSpan(pos_type pos) const // Gets uninstantiated font setting at position 0 -Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const +Font const & Paragraph::getFirstFontSettings(BufferParams const & bparams) const { if (!empty() && !d->fontlist_.empty()) return d->fontlist_.begin()->font(); - return Font(inherit_font, bparams.language); + // Optimisation: avoid a full font instantiation if there is no + // language change from previous call. + static Font previous_font; + static Language const * previous_lang = 0; + if (bparams.language != previous_lang) { + previous_lang = bparams.language; + previous_font = Font(inherit_font, bparams.language); + } + + return previous_font; } @@ -1381,13 +1413,14 @@ Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos, Font font = getFontSettings(bparams, pos); pos_type const body_pos = beginOfBody(); + FontInfo & fi = font.fontInfo(); if (pos < body_pos) - font.fontInfo().realize(d->layout_->labelfont); + fi.realize(d->layout_->labelfont); else - font.fontInfo().realize(d->layout_->font); + fi.realize(d->layout_->font); - font.fontInfo().realize(outerfont.fontInfo()); - font.fontInfo().realize(bparams.getFont().fontInfo()); + fi.realize(outerfont.fontInfo()); + fi.realize(bparams.getFont().fontInfo()); return font; } @@ -1680,7 +1713,9 @@ namespace { bool noTrivlistCentering(InsetCode code) { - return code == FLOAT_CODE || code == WRAP_CODE; + return code == FLOAT_CODE + || code == WRAP_CODE + || code == CELL_CODE; } @@ -1697,12 +1732,19 @@ string correction(string const & orig) string const corrected_env(string const & suffix, string const & env, - InsetCode code) + InsetCode code, bool const lastpar) { string output = suffix + "{"; - if (noTrivlistCentering(code)) + if (noTrivlistCentering(code)) { + if (lastpar) { + // the last paragraph in non-trivlist-aligned + // context is special (to avoid unwanted whitespace) + if (suffix == "\\begin") + return "\\" + correction(env) + "{}"; + return string(); + } output += correction(env); - else + } else output += env; output += "}"; if (suffix == "\\begin") @@ -1727,7 +1769,7 @@ void adjust_row_column(string const & str, TexRow & texrow, int & column) int Paragraph::Private::startTeXParParams(BufferParams const & bparams, odocstream & os, TexRow & texrow, - bool moving_arg) const + OutputParams const & runparams) const { int column = 0; @@ -1750,13 +1792,17 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams, case LYX_ALIGN_LEFT: case LYX_ALIGN_RIGHT: case LYX_ALIGN_CENTER: - if (moving_arg) { + if (runparams.moving_arg) { os << "\\protect"; column += 8; } break; } + string const begin_tag = "\\begin"; + InsetCode code = owner_->ownerCode(); + bool const lastpar = runparams.isLastPar; + switch (curAlign) { case LYX_ALIGN_NONE: case LYX_ALIGN_BLOCK: @@ -1766,24 +1812,24 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams, case LYX_ALIGN_LEFT: { string output; if (owner_->getParLanguage(bparams)->babel() != "hebrew") - output = corrected_env("\\begin", "flushleft", owner_->ownerCode()); + output = corrected_env(begin_tag, "flushleft", code, lastpar); else - output = corrected_env("\\begin", "flushright", owner_->ownerCode()); + output = corrected_env(begin_tag, "flushright", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; } case LYX_ALIGN_RIGHT: { string output; if (owner_->getParLanguage(bparams)->babel() != "hebrew") - output = corrected_env("\\begin", "flushright", owner_->ownerCode()); + output = corrected_env(begin_tag, "flushright", code, lastpar); else - output = corrected_env("\\begin", "flushleft", owner_->ownerCode()); + output = corrected_env(begin_tag, "flushleft", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; } case LYX_ALIGN_CENTER: { string output; - output = corrected_env("\\begin", "center", owner_->ownerCode()); + output = corrected_env(begin_tag, "center", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; @@ -1796,7 +1842,7 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams, int Paragraph::Private::endTeXParParams(BufferParams const & bparams, odocstream & os, TexRow & texrow, - bool moving_arg) const + OutputParams const & runparams) const { int column = 0; @@ -1809,13 +1855,17 @@ int Paragraph::Private::endTeXParParams(BufferParams const & bparams, case LYX_ALIGN_LEFT: case LYX_ALIGN_RIGHT: case LYX_ALIGN_CENTER: - if (moving_arg) { + if (runparams.moving_arg) { os << "\\protect"; column = 8; } break; } + string const end_tag = "\n\\par\\end"; + InsetCode code = owner_->ownerCode(); + bool const lastpar = runparams.isLastPar; + switch (params_.align()) { case LYX_ALIGN_NONE: case LYX_ALIGN_BLOCK: @@ -1825,24 +1875,24 @@ int Paragraph::Private::endTeXParParams(BufferParams const & bparams, case LYX_ALIGN_LEFT: { string output; if (owner_->getParLanguage(bparams)->babel() != "hebrew") - output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode()); + output = corrected_env(end_tag, "flushleft", code, lastpar); else - output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode()); + output = corrected_env(end_tag, "flushright", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; } case LYX_ALIGN_RIGHT: { string output; if (owner_->getParLanguage(bparams)->babel() != "hebrew") - output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode()); + output = corrected_env(end_tag, "flushright", code, lastpar); else - output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode()); + output = corrected_env(end_tag, "flushleft", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; } case LYX_ALIGN_CENTER: { string output; - output = corrected_env("\n\\par\\end", "center", owner_->ownerCode()); + output = corrected_env(end_tag, "center", code, lastpar); os << from_ascii(output); adjust_row_column(output, texrow, column); break; @@ -1859,7 +1909,7 @@ bool Paragraph::latex(BufferParams const & bparams, odocstream & os, TexRow & texrow, OutputParams const & runparams) const { - LYXERR(Debug::LATEX, "SimpleTeXOnePar... " << this); + LYXERR(Debug::LATEX, "Paragraph::latex... " << this); bool return_value = false; @@ -1908,7 +1958,7 @@ bool Paragraph::latex(BufferParams const & bparams, } if (!asdefault) column += d->startTeXParParams(bparams, os, texrow, - runparams.moving_arg); + runparams); } for (pos_type i = 0; i < size(); ++i) { @@ -1939,7 +1989,7 @@ bool Paragraph::latex(BufferParams const & bparams, if (!asdefault) column += d->startTeXParParams(bparams, os, texrow, - runparams.moving_arg); + runparams); } Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset @@ -2116,10 +2166,10 @@ bool Paragraph::latex(BufferParams const & bparams, if (!asdefault) { column += d->endTeXParParams(bparams, os, texrow, - runparams.moving_arg); + runparams); } - LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this); + LYXERR(Debug::LATEX, "Paragraph::latex... done " << this); return return_value; } @@ -2372,7 +2422,7 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options) const for (pos_type i = beg; i < end; ++i) { char_type const c = d->text_[i]; - if (isPrintable(c)) + if (isPrintable(c) || c == '\t') os.put(c); else if (c == META_INSET && options & AS_STR_INSETS) getInset(i)->textString(os);