commit 65b03c7c72a9871e3f603f07204705e644dbc96c
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Wed Jun 21 16:58:19 2023 +0200

    Improve label font computation in insets
    
    This patch reuses the code of TextMetrics::displayFont() that handles
    the label part of LABEL_MANUAL paragraphs to create a new
    labelDisplayFont() method usable for things like Itemize labels.
    
    To this end, and new magic value is used as position to force the
    label case in displayFont(). The code is also factored a bit and
    cleaned up.
    
    Fixes bug #12810.
---
 src/RowPainter.cpp  |    2 +-
 src/TextMetrics.cpp |   45 +++++++++++++++++++++++++++------------------
 src/TextMetrics.h   |    8 +++++++-
 3 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/src/RowPainter.cpp b/src/RowPainter.cpp
index 4f5593e..b8db1aa 100644
--- a/src/RowPainter.cpp
+++ b/src/RowPainter.cpp
@@ -67,7 +67,7 @@ RowPainter::RowPainter(PainterInfo & pi,
 
 FontInfo RowPainter::labelFont(bool end) const
 {
-       FontInfo f = text_.labelFont(par_);
+       FontInfo f = tm_.labelDisplayFont(row_.pit()).fontInfo();
        // selected text?
        if ((end ? row_.end_margin_sel : row_.begin_margin_sel)
            || pi_.selected)
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index f424118..88c8e81 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -289,55 +289,64 @@ void TextMetrics::applyOuterFont(Font & font) const
 }
 
 
+// This is an arbitrary magic value
+static const pos_type force_label = -12345;
+
+// if pos == force_label, this function will return the label font instead.
+// This undocumented and only for use by labelDisplayFont()
 Font TextMetrics::displayFont(pit_type pit, pos_type pos) const
 {
-       LASSERT(pos >= 0, { static Font f; return f; });
+       LASSERT(pos >= 0 || pos == force_label, { static Font f; return f; });
 
        ParagraphList const & pars = text_->paragraphs();
        Paragraph const & par = pars[pit];
        Layout const & layout = par.layout();
        Buffer const & buffer = bv_->buffer();
        // FIXME: broken?
-       BufferParams const & params = buffer.params();
-       pos_type const body_pos = par.beginOfBody();
+       BufferParams const & bparams = buffer.params();
+       bool const label = pos < par.beginOfBody() || pos == force_label;
+
+       Font f = (pos == force_label) ? Font(inherit_font, bparams.language)
+                                     : par.getFontSettings(bparams, pos);
+       FontInfo const & lf = label ? layout.labelfont : layout.font;
 
        // We specialize the 95% common case:
        if (!par.getDepth()) {
-               Font f = par.getFontSettings(params, pos);
                if (!text_->isMainText())
                        applyOuterFont(f);
-               bool lab = layout.labeltype == LABEL_MANUAL && pos < body_pos;
 
-               FontInfo const & lf = lab ? layout.labelfont : layout.font;
-               FontInfo rlf = lab ? layout.reslabelfont : layout.resfont;
+               FontInfo rlf = label ? layout.reslabelfont : layout.resfont;
 
                // In case the default family has been customized
                if (lf.family() == INHERIT_FAMILY)
-                       rlf.setFamily(params.getFont().fontInfo().family());
+                       rlf.setFamily(bparams.getFont().fontInfo().family());
                f.fontInfo().realize(rlf);
                return f;
        }
 
-       // The uncommon case need not be optimized as much
-       FontInfo const & layoutfont = pos < body_pos ?
-               layout.labelfont : layout.font;
-
-       Font font = par.getFontSettings(params, pos);
-       font.fontInfo().realize(layoutfont);
+       // The uncommon case need not be optimized as much.
+       // FIXME : the two code paths seem different in function.
+       f.fontInfo().realize(lf);
 
        if (!text_->isMainText())
-               applyOuterFont(font);
+               applyOuterFont(f);
 
        // 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.fontInfo().realize(text_->outerFont(pit).fontInfo());
+               f.fontInfo().realize(text_->outerFont(pit).fontInfo());
 
        // Realize with the fonts of lesser depth.
-       font.fontInfo().realize(params.getFont().fontInfo());
+       f.fontInfo().realize(bparams.getFont().fontInfo());
 
-       return font;
+       return f;
+}
+
+
+Font TextMetrics::labelDisplayFont(pit_type pit) const
+{
+       return displayFont(pit, force_label);
 }
 
 
diff --git a/src/TextMetrics.h b/src/TextMetrics.h
index cf53faa..74eef5e 100644
--- a/src/TextMetrics.h
+++ b/src/TextMetrics.h
@@ -79,13 +79,19 @@ public:
        /// cache. Related to BufferView::updatePosCache.
        void updatePosCache(pit_type pit) const;
 
-       /// Gets the fully instantiated font at a given position in a paragraph
+       /// 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 displayFont(pit_type pit, pos_type pos) const;
 
+       /// Gets the fully instantiated label font of a paragraph.
+       /// Basically the same routine as displayFont, but specialized for
+       /// a layout font.
+       Font labelDisplayFont(pit_type pit) const;
+
+
        /// 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
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to