]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[lyx.git] / src / Text2.cpp
index b55827be5fbb026213c60d1e3454f939e75088a8..4b04512a102e4c5b9c48c5f3beeb35cc1f6480b8 100644 (file)
@@ -71,9 +71,7 @@ using std::istringstream;
 namespace lyx {
 
 Text::Text()
-       : current_font(Font::ALL_INHERIT),
-         background_color_(Color::background),
-         autoBreakRows_(false)
+       : autoBreakRows_(false)
 {}
 
 
@@ -83,92 +81,6 @@ 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
 {
        LayoutPtr const & layout = pars_[pit].layout();
@@ -211,7 +123,7 @@ Font Text::getLabelFont(Buffer const & buffer, Paragraph const & par) const
 
 
 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();
@@ -239,7 +151,7 @@ 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);
 
        layoutfont.realize(buffer.params().getFont());
 
@@ -250,7 +162,7 @@ void Text::setCharFont(Buffer const & buffer, pit_type pit,
 }
 
 
-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) &&
@@ -265,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);
                }
        }
 }
@@ -410,15 +322,15 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
                layoutfont = getLayoutFont(cur.buffer(), pit);
 
        // Update current font
-       real_current_font.update(font,
+       cur.real_current_font.update(font,
                                        cur.buffer().params().language,
                                        toggleall);
 
        // Reduce to implicit settings
-       current_font = real_current_font;
-       current_font.reduce(layoutfont);
+       cur.current_font = cur.real_current_font;
+       cur.current_font.reduce(layoutfont);
        // And resolve it completely
-       real_current_font.realize(layoutfont);
+       cur.real_current_font.realize(layoutfont);
 
        // if there is no selection that's all we need to do
        if (!cur.selection())
@@ -427,15 +339,17 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
        // Ok, we have a selection.
        recordUndoSelection(cur);
 
-       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
@@ -451,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_);
                }
        }
 }
@@ -574,7 +489,7 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 {
        BOOST_ASSERT(this == cur.text());
        BOOST_ASSERT(inset);
-       cur.paragraph().insertInset(cur.pos(), inset, current_font,
+       cur.paragraph().insertInset(cur.pos(), inset, cur.current_font,
                                    Change(cur.buffer().params().trackChanges ?
                                           Change::INSERTED : Change::UNCHANGED));
 }
@@ -584,7 +499,7 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 void Text::insertStringAsLines(Cursor & cur, docstring const & str)
 {
        cur.buffer().insertStringAsLines(pars_, cur.pit(), cur.pos(),
-                                        current_font, str, autoBreakRows_);
+                                        cur.current_font, str, autoBreakRows_);
 }
 
 
@@ -655,50 +570,7 @@ void Text::setCursorIntern(Cursor & cur,
        cur.boundary(boundary);
        setCursor(cur.top(), par, pos);
        if (setfont)
-               setCurrentFont(cur);
-}
-
-
-void Text::setCurrentFont(Cursor & cur)
-{
-       BOOST_ASSERT(this == cur.text());
-       pos_type pos = cur.pos();
-       Paragraph & par = cur.paragraph();
-
-       // are we behind previous char in fact? -> go to that char
-       if (pos > 0 && cur.boundary())
-               --pos;
-
-       // find position to take the font from
-       if (pos != 0) {
-               // paragraph end? -> font of last char
-               if (pos == cur.lastpos())
-                       --pos;
-               // on space? -> look at the words in front of space
-               else if (pos > 0 && par.isSeparator(pos))       {
-                       // abc| def -> font of c
-                       // abc |[WERBEH], i.e. boundary==true -> font of c
-                       // abc [WERBEH]| def, font of the space
-                       if (!isRTLBoundary(cur.buffer(), par, pos))
-                               --pos;
-               }
-       }
-
-       // get font
-       BufferParams const & bufparams = cur.buffer().params();
-       current_font = par.getFontSettings(bufparams, pos);
-       real_current_font = getFont(cur.buffer(), par, pos);
-
-       // special case for paragraph end
-       if (cur.pos() == cur.lastpos()
-           && isRTLBoundary(cur.buffer(), par, cur.pos())
-           && !cur.boundary()) {
-               Language const * lang = par.getParLanguage(bufparams);
-               current_font.setLanguage(lang);
-               current_font.setNumber(Font::OFF);
-               real_current_font.setLanguage(lang);
-               real_current_font.setNumber(Font::OFF);
-       }
+               cur.setCurrentFont();
 }
 
 
@@ -780,10 +652,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, 
@@ -812,7 +684,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
@@ -941,7 +813,11 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                           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)
+                       plist[old.pit()].params().startOfAppendix(true);
 
                // see #warning (FIXME?) above 
                if (cur.depth() >= old.depth()) {