]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
Revert part of 21965 which was debugging code.
[lyx.git] / src / Text2.cpp
index 14afb1a49b933991d4bf1c422391a626205a4feb..d03d71d1073b198f4804aaeec066f3c5381a17da 100644 (file)
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "Bullet.h"
-#include "Color.h"
+#include "Changes.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
-#include "debug.h"
 #include "DispatchResult.h"
 #include "ErrorList.h"
 #include "FuncRequest.h"
-#include "gettext.h"
 #include "Language.h"
+#include "Layout.h"
 #include "Lexer.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
@@ -44,9 +43,8 @@
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
-#include "Server.h"
-#include "ServerSocket.h"
-#include "Undo.h"
+#include "TextClass.h"
+#include "TextMetrics.h"
 #include "VSpace.h"
 
 #include "frontends/FontMetrics.h"
 
 #include "mathed/InsetMathHull.h"
 
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "support/textutils.h"
 
-#include <boost/current_function.hpp>
+#include <boost/next_prior.hpp>
 
 #include <sstream>
 
@@ -71,8 +71,7 @@ using std::istringstream;
 namespace lyx {
 
 Text::Text()
-       : background_color_(Color::background),
-         autoBreakRows_(false)
+       : autoBreakRows_(false)
 {}
 
 
@@ -82,141 +81,55 @@ bool Text::isMainText(Buffer const & buffer) const
 }
 
 
-// Gets the fully instantiated font at a given position in a paragraph
-// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
-// The difference is that this one is used for displaying, and thus we
-// are allowed to make cosmetic improvements. For instance make footnotes
-// smaller. (Asger)
-Font Text::getFont(Buffer const & buffer, Paragraph const & par,
-               pos_type const pos) const
-{
-       BOOST_ASSERT(pos >= 0);
-
-       LayoutPtr const & layout = par.layout();
-       // FIXME: broken?
-       BufferParams const & params = buffer.params();
-       pos_type const body_pos = par.beginOfBody();
-
-       // We specialize the 95% common case:
-       if (!par.getDepth()) {
-               Font f = par.getFontSettings(params, pos);
-               if (!isMainText(buffer))
-                       applyOuterFont(buffer, f);
-               Font lf;
-               Font rlf;
-               if (layout->labeltype == LABEL_MANUAL && pos < body_pos) {
-                       lf = layout->labelfont;
-                       rlf = layout->reslabelfont;
-               } else {
-                       lf = layout->font;
-                       rlf = layout->resfont;
-               }
-               // In case the default family has been customized
-               if (lf.family() == Font::INHERIT_FAMILY)
-                       rlf.setFamily(params.getFont().family());
-               return f.realize(rlf);
-       }
-
-       // The uncommon case need not be optimized as much
-       Font layoutfont;
-       if (pos < body_pos)
-               layoutfont = layout->labelfont;
-       else
-               layoutfont = layout->font;
-
-       Font font = par.getFontSettings(params, pos);
-       font.realize(layoutfont);
-
-       if (!isMainText(buffer))
-               applyOuterFont(buffer, font);
-
-       // Find the pit value belonging to paragraph. This will not break
-       // even if pars_ would not be a vector anymore.
-       // Performance appears acceptable.
-
-       pit_type pit = pars_.size();
-       for (pit_type it = 0; it < pit; ++it)
-               if (&pars_[it] == &par) {
-                       pit = it;
-                       break;
-               }
-       // Realize against environment font information
-       // NOTE: the cast to pit_type should be removed when pit_type
-       // changes to a unsigned integer.
-       if (pit < pit_type(pars_.size()))
-               font.realize(outerFont(pit, pars_));
-
-       // Realize with the fonts of lesser depth.
-       font.realize(params.getFont());
-
-       return font;
-}
-
-// There are currently two font mechanisms in LyX:
-// 1. The font attributes in a lyxtext, and
-// 2. The inset-specific font properties, defined in an inset's
-// metrics() and draw() methods and handed down the inset chain through
-// the pi/mi parameters, and stored locally in a lyxtext in font_.
-// This is where the two are integrated in the final fully realized
-// font.
-void Text::applyOuterFont(Buffer const & buffer, Font & font) const {
-       Font lf(font_);
-       lf.reduce(buffer.params().getFont());
-       lf.realize(font);
-       lf.setLanguage(font.language());
-       font = lf;
-}
-
-
-Font Text::getLayoutFont(Buffer const & buffer, pit_type const pit) const
+FontInfo Text::getLayoutFont(Buffer const & buffer, pit_type const pit) const
 {
        LayoutPtr const & layout = pars_[pit].layout();
 
        if (!pars_[pit].getDepth())  {
-               Font lf = layout->resfont;
+               FontInfo lf = layout->resfont;
                // In case the default family has been customized
-               if (layout->font.family() == Font::INHERIT_FAMILY)
-                       lf.setFamily(buffer.params().getFont().family());
+               if (layout->font.family() == INHERIT_FAMILY)
+                       lf.setFamily(buffer.params().getFont().fontInfo().family());
                return lf;
        }
 
-       Font font = layout->font;
+       FontInfo font = layout->font;
        // Realize with the fonts of lesser depth.
        //font.realize(outerFont(pit, paragraphs()));
-       font.realize(buffer.params().getFont());
+       font.realize(buffer.params().getFont().fontInfo());
 
        return font;
 }
 
 
-Font Text::getLabelFont(Buffer const & buffer, Paragraph const & par) const
+FontInfo Text::getLabelFont(Buffer const & buffer, Paragraph const & par) const
 {
        LayoutPtr const & layout = par.layout();
 
        if (!par.getDepth()) {
-               Font lf = layout->reslabelfont;
+               FontInfo lf = layout->reslabelfont;
                // In case the default family has been customized
-               if (layout->labelfont.family() == Font::INHERIT_FAMILY)
-                       lf.setFamily(buffer.params().getFont().family());
+               if (layout->labelfont.family() == INHERIT_FAMILY)
+                       lf.setFamily(buffer.params().getFont().fontInfo().family());
                return lf;
        }
 
-       Font font = layout->labelfont;
+       FontInfo font = layout->labelfont;
        // Realize with the fonts of lesser depth.
-       font.realize(buffer.params().getFont());
+       font.realize(buffer.params().getFont().fontInfo());
 
        return font;
 }
 
 
 void Text::setCharFont(Buffer const & buffer, pit_type pit,
-               pos_type pos, Font const & fnt)
+               pos_type pos, Font const & fnt, Font const & display_font)
 {
        Font font = fnt;
        LayoutPtr const & layout = pars_[pit].layout();
 
        // Get concrete layout font to reduce against
-       Font layoutfont;
+       FontInfo layoutfont;
 
        if (pos < pars_[pit].beginOfBody())
                layoutfont = layout->labelfont;
@@ -238,18 +151,18 @@ void Text::setCharFont(Buffer const & buffer, pit_type pit,
        // Inside inset, apply the inset's font attributes if any
        // (charstyle!)
        if (!isMainText(buffer))
-               layoutfont.realize(font_);
+               layoutfont.realize(display_font.fontInfo());
 
-       layoutfont.realize(buffer.params().getFont());
+       layoutfont.realize(buffer.params().getFont().fontInfo());
 
        // Now, reduce font against full layout font
-       font.reduce(layoutfont);
+       font.fontInfo().reduce(layoutfont);
 
        pars_[pit].setFont(pos, font);
 }
 
 
-void Text::setInsetFont(Buffer const & buffer, pit_type pit,
+void Text::setInsetFont(BufferView const & bv, pit_type pit,
                pos_type pos, Font const & font, bool toggleall)
 {
        BOOST_ASSERT(pars_[pit].isInset(pos) &&
@@ -264,7 +177,7 @@ void Text::setInsetFont(Buffer const & buffer, pit_type pit,
                        CursorSlice cellend = cs;
                        cellend.pit() = cellend.lastpit();
                        cellend.pos() = cellend.lastpos();
-                       text->setFont(buffer, cs, cellend, font, toggleall);
+                       text->setFont(bv, cs, cellend, font, toggleall);
                }
        }
 }
@@ -317,7 +230,7 @@ void Text::setLayout(Cursor & cur, docstring const & layout)
        LayoutPtr const & lyxlayout = params.getTextClass()[layout];
        if (lyxlayout->is_environment) {
                // move everything in a new environment inset
-               LYXERR(Debug::DEBUG) << "setting layout " << to_utf8(layout) << endl;
+               LYXERR(Debug::DEBUG, "setting layout " << to_utf8(layout));
                lyx::dispatch(FuncRequest(LFUN_LINE_BEGIN));
                lyx::dispatch(FuncRequest(LFUN_LINE_END_SELECT));
                lyx::dispatch(FuncRequest(LFUN_CUT));
@@ -376,7 +289,7 @@ void Text::changeDepth(Cursor & cur, DEPTH_CHANGE type)
        BOOST_ASSERT(this == cur.text());
        pit_type const beg = cur.selBegin().pit();
        pit_type const end = cur.selEnd().pit() + 1;
-       recordUndoSelection(cur);
+       cur.recordUndoSelection();
        int max_depth = (beg != 0 ? pars_[beg - 1].getMaxDepthAfter() : 0);
 
        for (pit_type pit = beg; pit != end; ++pit) {
@@ -401,7 +314,7 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
        BOOST_ASSERT(this == cur.text());
        // Set the current_font
        // Determine basis font
-       Font layoutfont;
+       FontInfo layoutfont;
        pit_type pit = cur.pit();
        if (cur.pos() < pars_[pit].beginOfBody())
                layoutfont = getLabelFont(cur.buffer(), pars_[pit]);
@@ -415,26 +328,28 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
 
        // Reduce to implicit settings
        cur.current_font = cur.real_current_font;
-       cur.current_font.reduce(layoutfont);
+       cur.current_font.fontInfo().reduce(layoutfont);
        // And resolve it completely
-       cur.real_current_font.realize(layoutfont);
+       cur.real_current_font.fontInfo().realize(layoutfont);
 
        // if there is no selection that's all we need to do
        if (!cur.selection())
                return;
 
        // Ok, we have a selection.
-       recordUndoSelection(cur);
+       cur.recordUndoSelection();
 
-       setFont(cur.buffer(), cur.selectionBegin().top(), 
+       setFont(cur.bv(), cur.selectionBegin().top(), 
                cur.selectionEnd().top(), font, toggleall);
 }
 
 
-void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
+void Text::setFont(BufferView const & bv, CursorSlice const & begin,
                CursorSlice const & end, Font const & font,
                bool toggleall)
 {
+       Buffer const & buffer = bv.buffer();
+
        // Don't use forwardChar here as ditend might have
        // pos() == lastpos() and forwardChar would miss it.
        // Can't use forwardPos either as this descends into
@@ -450,10 +365,11 @@ void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
                                // text cells of the inset (bug 1973).
                                // FIXME: This should change, see documentation
                                // of noFontChange in Inset.h
-                               setInsetFont(buffer, pit, pos, font, toggleall);
-                       Font f = getFont(buffer, dit.paragraph(), pos);
+                               setInsetFont(bv, pit, pos, font, toggleall);
+                       TextMetrics const & tm = bv.textMetrics(this);
+                       Font f = tm.getDisplayFont(pit, pos);
                        f.update(font, language, toggleall);
-                       setCharFont(buffer, pit, pos, f);
+                       setCharFont(buffer, pit, pos, f, tm.font_);
                }
        }
 }
@@ -477,7 +393,8 @@ void Text::toggleFree(Cursor & cur, Font const & font, bool toggleall)
 {
        BOOST_ASSERT(this == cur.text());
        // If the mask is completely neutral, tell user
-       if (font == Font(Font::ALL_IGNORE)) {
+       if (font.fontInfo() == ignore_font && 
+               (font.language() == 0 || font.language() == ignore_language)) {
                // Could only happen with user style
                cur.message(_("No font change defined."));
                return;
@@ -489,7 +406,7 @@ void Text::toggleFree(Cursor & cur, Font const & font, bool toggleall)
        CursorSlice resetCursor = cur.top();
        bool implicitSelection =
                font.language() == ignore_language
-               && font.number() == Font::IGNORE
+               && font.fontInfo().number() == FONT_IGNORE
                && selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
 
        // Set font
@@ -667,7 +584,7 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front)
        if (!front && cur.pos() == 0)
                return false;
        Inset * inset = front ? cur.nextInset() : cur.prevInset();
-       if (!isHighlyEditableInset(inset))
+       if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
                return false;
        /*
         * Apparently, when entering an inset we are expected to be positioned
@@ -683,7 +600,7 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front)
 }
 
 
-bool Text::cursorLeft(Cursor & cur)
+bool Text::cursorBackward(Cursor & cur)
 {
        // Tell BufferView to test for FitCursor in any case!
        cur.updateFlags(Update::FitCursor);
@@ -720,12 +637,12 @@ bool Text::cursorLeft(Cursor & cur)
 
        // move to the previous paragraph or do nothing
        if (cur.pit() > 0)
-               return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
+               return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size(), true, false);
        return false;
 }
 
 
-bool Text::cursorRight(Cursor & cur)
+bool Text::cursorForward(Cursor & cur)
 {
        // Tell BufferView to test for FitCursor in any case!
        cur.updateFlags(Update::FitCursor);
@@ -736,10 +653,10 @@ bool Text::cursorRight(Cursor & cur)
                if (checkAndActivateInset(cur, true))
                        return false;
 
+               TextMetrics const & tm = cur.bv().textMetrics(this);
                // if left of boundary -> just jump to right side
-         // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
-         if (cur.boundary() && 
-                               !isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos()))
+               // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
+               if (cur.boundary() && !tm.isRTLBoundary(cur.pit(), cur.pos()))
                        return setCursor(cur, cur.pit(), cur.pos(), true, false);
 
                // next position is left of boundary, 
@@ -768,7 +685,7 @@ bool Text::cursorRight(Cursor & cur)
                
                // in front of RTL boundary? Stay on this side of the boundary because:
                //   ab|cDDEEFFghi -> abc|DDEEFFghi
-               if (isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
+               if (tm.isRTLBoundary(cur.pit(), cur.pos() + 1))
                        return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
                
                // move right
@@ -777,7 +694,7 @@ bool Text::cursorRight(Cursor & cur)
 
        // move to next paragraph
        if (cur.pit() != cur.lastpit())
-               return setCursor(cur, cur.pit() + 1, 0);
+               return setCursor(cur, cur.pit() + 1, 0, true, false);
        return false;
 }
 
@@ -827,7 +744,7 @@ void Text::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
 bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                Cursor & old, bool & need_anchor_change)
 {
-       //LYXERR(Debug::DEBUG) << "DEPM: cur:\n" << cur << "old:\n" << old << endl;
+       //LYXERR(Debug::DEBUG, "DEPM: cur:\n" << cur << "old:\n" << old);
 
        Paragraph & oldpar = old.paragraph();
 
@@ -893,11 +810,15 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
 
        if (oldpar.empty() || (oldpar.size() == 1 && oldpar.isLineSeparator(0))) {
                // Delete old par.
-               recordUndo(old, Undo::ATOMIC,
+               old.recordUndo(ATOMIC_UNDO,
                           max(old.pit() - 1, pit_type(0)),
                           min(old.pit() + 1, old.lastpit()));
                ParagraphList & plist = old.text()->paragraphs();
+               bool const soa = oldpar.params().startOfAppendix();
                plist.erase(boost::next(plist.begin(), old.pit()));
+               // do not lose start of appendix marker (bug 4212)
+               if (soa && old.pit() < pit_type(plist.size()))
+                       plist[old.pit()].params().startOfAppendix(true);
 
                // see #warning (FIXME?) above 
                if (cur.depth() >= old.depth()) {
@@ -970,13 +891,13 @@ void Text::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool tra
 
 void Text::recUndo(Cursor & cur, pit_type first, pit_type last) const
 {
-       recordUndo(cur, Undo::ATOMIC, first, last);
+       cur.recordUndo(ATOMIC_UNDO, first, last);
 }
 
 
 void Text::recUndo(Cursor & cur, pit_type par) const
 {
-       recordUndo(cur, Undo::ATOMIC, par, par);
+       cur.recordUndo(ATOMIC_UNDO, par, par);
 }
 
 } // namespace lyx