[Libreoffice-commits] core.git: include/svtools include/vcl vcl/inc vcl/source vcl/unx

Caolán McNamara (via logerrit) logerrit at kemper.freedesktop.org
Thu Sep 30 08:06:37 UTC 2021


 include/svtools/editbrowsebox.hxx |    5 +
 include/vcl/weld.hxx              |    3 +
 vcl/inc/salvtables.hxx            |    2 
 vcl/source/app/salvtables.cxx     |   11 +++-
 vcl/unx/gtk3/gtkinst.cxx          |   99 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+), 3 deletions(-)

New commits:
commit 4d445ac9aa43b3399a579e3dbb1aac94507c9520
Author:     Caolán McNamara <caolanm at redhat.com>
AuthorDate: Wed Sep 29 16:57:40 2021 +0100
Commit:     Caolán McNamara <caolanm at redhat.com>
CommitDate: Thu Sep 30 10:06:02 2021 +0200

    Related: tdf#141633 support setting a custom font for TextView
    
    Change-Id: I4c27a21c5c682fc73c37594749627e4cc7c0822d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122833
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/include/svtools/editbrowsebox.hxx b/include/svtools/editbrowsebox.hxx
index d557634ad8c8..9ebbb3a2696b 100644
--- a/include/svtools/editbrowsebox.hxx
+++ b/include/svtools/editbrowsebox.hxx
@@ -395,6 +395,11 @@ namespace svt
             m_xWidget->set_editable(!bReadOnly);
         }
 
+        virtual void SetPointFont(const vcl::Font& rFont) override
+        {
+            m_xWidget->set_font(rFont);
+        }
+
         virtual void GetFocus() override;
 
         virtual void dispose() override;
diff --git a/include/vcl/weld.hxx b/include/vcl/weld.hxx
index ee647b341e21..5ca6a468e33c 100644
--- a/include/vcl/weld.hxx
+++ b/include/vcl/weld.hxx
@@ -2131,6 +2131,9 @@ public:
         return get_text_height() * nRows;
     }
 
+    // font size is in points, not pixels, e.g. see Window::[G]etPointFont
+    virtual void set_font(const vcl::Font& rFont) = 0;
+
     /*
        Typically you want to avoid the temptation of customizing
        font colors
diff --git a/vcl/inc/salvtables.hxx b/vcl/inc/salvtables.hxx
index 8c11e17c2ba2..804250b40e95 100644
--- a/vcl/inc/salvtables.hxx
+++ b/vcl/inc/salvtables.hxx
@@ -1298,6 +1298,8 @@ public:
 
     virtual void set_monospace(bool bMonospace) override;
 
+    virtual void set_font(const vcl::Font& rFont) override;
+
     virtual void set_font_color(const Color& rColor) override;
 
     virtual void connect_cursor_position(const Link<TextView&, void>& rLink) override;
diff --git a/vcl/source/app/salvtables.cxx b/vcl/source/app/salvtables.cxx
index 68303bfb391d..9b56472a4241 100644
--- a/vcl/source/app/salvtables.cxx
+++ b/vcl/source/app/salvtables.cxx
@@ -5763,9 +5763,7 @@ void SalInstanceTextView::set_monospace(bool bMonospace)
     else
         aFont = Application::GetSettings().GetStyleSettings().GetFieldFont();
     aFont.SetFontHeight(aOrigFont.GetFontHeight());
-    m_xTextView->SetFont(aFont);
-    m_xTextView->SetControlFont(aFont);
-    m_xTextView->Invalidate();
+    set_font(aFont);
 }
 
 void SalInstanceTextView::set_font_color(const Color& rColor)
@@ -5776,6 +5774,13 @@ void SalInstanceTextView::set_font_color(const Color& rColor)
         m_xTextView->SetControlForeground();
 }
 
+void SalInstanceTextView::set_font(const vcl::Font& rFont)
+{
+    m_xTextView->SetFont(rFont);
+    m_xTextView->SetControlFont(rFont);
+    m_xTextView->Invalidate();
+}
+
 void SalInstanceTextView::connect_cursor_position(const Link<TextView&, void>& rLink)
 {
     assert(!m_aCursorPositionHdl.IsSet());
diff --git a/vcl/unx/gtk3/gtkinst.cxx b/vcl/unx/gtk3/gtkinst.cxx
index 21504ad7be5c..913494fb47bd 100644
--- a/vcl/unx/gtk3/gtkinst.cxx
+++ b/vcl/unx/gtk3/gtkinst.cxx
@@ -8872,6 +8872,81 @@ IMPL_LINK_NOARG(GtkInstanceNotebook, SizeAllocateHdl, void*, void)
 }
 #endif
 
+
+OUString vcl_font_to_css(const vcl::Font& rFont)
+{
+    OUStringBuffer sCSS;
+    sCSS.append("font-family: \"" + rFont.GetFamilyName() + "\"; ");
+    sCSS.append("font-size: " + OUString::number(rFont.GetFontSize().Height()) + "pt; ");
+    switch (rFont.GetItalic())
+    {
+        case ITALIC_NONE:
+            sCSS.append("font-style: normal; ");
+            break;
+        case ITALIC_NORMAL:
+            sCSS.append("font-style: italic; ");
+            break;
+        case ITALIC_OBLIQUE:
+            sCSS.append("font-style: oblique; ");
+            break;
+        default:
+            break;
+    }
+    switch (rFont.GetWeight())
+    {
+        case WEIGHT_ULTRALIGHT:
+            sCSS.append("font-weight: 200; ");
+            break;
+        case WEIGHT_LIGHT:
+            sCSS.append("font-weight: 300; ");
+            break;
+        case WEIGHT_NORMAL:
+            sCSS.append("font-weight: 400; ");
+            break;
+        case WEIGHT_BOLD:
+            sCSS.append("font-weight: 700; ");
+            break;
+        case WEIGHT_ULTRABOLD:
+            sCSS.append("font-weight: 800; ");
+            break;
+        default:
+            break;
+    }
+    switch (rFont.GetWidthType())
+    {
+        case WIDTH_ULTRA_CONDENSED:
+            sCSS.append("font-stretch: ultra-condensed; ");
+            break;
+        case WIDTH_EXTRA_CONDENSED:
+            sCSS.append("font-stretch: extra-condensed; ");
+            break;
+        case WIDTH_CONDENSED:
+            sCSS.append("font-stretch: condensed; ");
+            break;
+        case WIDTH_SEMI_CONDENSED:
+            sCSS.append("font-stretch: semi-condensed; ");
+            break;
+        case WIDTH_NORMAL:
+            sCSS.append("font-stretch: normal; ");
+            break;
+        case WIDTH_SEMI_EXPANDED:
+            sCSS.append("font-stretch: semi-expanded; ");
+            break;
+        case WIDTH_EXPANDED:
+            sCSS.append("font-stretch: expanded; ");
+            break;
+        case WIDTH_EXTRA_EXPANDED:
+            sCSS.append("font-stretch: extra-expanded; ");
+            break;
+        case WIDTH_ULTRA_EXPANDED:
+            sCSS.append("font-stretch: ultra-expanded; ");
+            break;
+        default:
+            break;
+    }
+    return sCSS.toString();
+}
+
 void update_attr_list(PangoAttrList* pAttrList, const vcl::Font& rFont)
 {
     pango_attr_list_change(pAttrList, pango_attr_family_new(OUStringToOString(rFont.GetFamilyName(), RTL_TEXTENCODING_UTF8).getStr()));
@@ -16415,6 +16490,8 @@ private:
     GtkTextBuffer* m_pTextBuffer;
     GtkAdjustment* m_pVAdjustment;
     GtkCssProvider* m_pFgCssProvider;
+    GtkCssProvider* m_pFontCssProvider;
+    std::optional<vcl::Font> m_xFont;
     int m_nMaxTextLength;
     gulong m_nChangedSignalId; // we don't disable/enable this one, it's to implement max-length
     gulong m_nInsertTextSignalId;
@@ -16506,6 +16583,7 @@ public:
         , m_pTextBuffer(gtk_text_view_get_buffer(pTextView))
         , m_pVAdjustment(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(pTextView)))
         , m_pFgCssProvider(nullptr)
+        , m_pFontCssProvider(nullptr)
         , m_nMaxTextLength(0)
         , m_nChangedSignalId(g_signal_connect(m_pTextBuffer, "changed", G_CALLBACK(signalChanged), this))
         , m_nInsertTextSignalId(g_signal_connect_after(m_pTextBuffer, "insert-text", G_CALLBACK(signalInserText), this))
@@ -16620,6 +16698,27 @@ public:
                                        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
     }
 
+    virtual vcl::Font get_font() override
+    {
+        if (m_xFont)
+            return *m_xFont;
+        return GtkInstanceWidget::get_font();
+    }
+
+    virtual void set_font(const vcl::Font& rFont) override
+    {
+        m_xFont = rFont;
+        GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(GTK_WIDGET(m_pTextView));
+        if (m_pFontCssProvider)
+            gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider));
+        m_pFontCssProvider = gtk_css_provider_new();
+        OUString aBuffer = "textview { " + vcl_font_to_css(rFont) +  "}";
+        OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8);
+        css_provider_load_from_data(m_pFontCssProvider, aResult.getStr(), aResult.getLength());
+        gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pFontCssProvider),
+                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
     virtual void disable_notify_events() override
     {
         g_signal_handler_block(m_pVAdjustment, m_nVAdjustChangedSignalId);


More information about the Libreoffice-commits mailing list