[librsvg: 1/2] Use the smcp (small caps) OpenType feature for fonts that support it




commit e9db53c710d22ccce8d03808e9ca3897205cb6a5
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Jun 8 17:25:42 2021 -0500

    Use the smcp (small caps) OpenType feature for fonts that support it
    
    This is part of #668.  It turns out that Pango does not implement
    PANGO_VARIANT_SMALL_CAPS.  Instead, one must turn on the "smcp"
    OpenType feature, but not all fonts support that.  In the future we
    may need to synthesize small caps on the librsvg side if Pango doesn't
    do it itself.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/545>

 src/text.rs                                         |  13 ++++++++++++-
 tests/fixtures/reftests/bugs/668-small-caps-ref.png | Bin 0 -> 1645 bytes
 tests/fixtures/reftests/bugs/668-small-caps.svg     |   5 +++++
 3 files changed, 17 insertions(+), 1 deletion(-)
---
diff --git a/src/text.rs b/src/text.rs
index 9182f1fb..91515246 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -836,7 +836,11 @@ fn create_pango_layout(draw_ctx: &DrawingCtx, props: &FontProperties, text: &str
     let mut font_desc = pango_context.get_font_description().unwrap();
     font_desc.set_family(props.font_family.as_str());
     font_desc.set_style(pango::Style::from(props.font_style));
-    font_desc.set_variant(pango::Variant::from(props.font_variant));
+
+    // PANGO_VARIANT_SMALL_CAPS does nothing: https://gitlab.gnome.org/GNOME/pango/-/issues/566
+    // see below for using the "smcp" OpenType feature for fonts that support it.
+    // font_desc.set_variant(pango::Variant::from(props.font_variant));
+
     font_desc.set_weight(pango::Weight::from(props.font_weight));
     font_desc.set_stretch(pango::Stretch::from(props.font_stretch));
 
@@ -872,6 +876,13 @@ fn create_pango_layout(draw_ctx: &DrawingCtx, props: &FontProperties, text: &str
         attr_list.insert(pango::Attribute::new_strikethrough(true).unwrap());
     }
 
+    // FIXME: Using the "smcp" OpenType feature only works for fonts that support it.  We
+    // should query if the font supports small caps, and synthesize them if it doesn't.
+    if props.font_variant == FontVariant::SmallCaps {
+        // smcp - small capitals - https://docs.microsoft.com/en-ca/typography/opentype/spec/features_pt#smcp
+        attr_list.insert(pango::Attribute::new_font_features("'smcp' 1").unwrap());
+    }
+
     layout.set_attributes(Some(&attr_list));
     layout.set_alignment(pango::Alignment::from(props.direction));
     layout.set_text(text);
diff --git a/tests/fixtures/reftests/bugs/668-small-caps-ref.png 
b/tests/fixtures/reftests/bugs/668-small-caps-ref.png
new file mode 100644
index 00000000..675c84a8
Binary files /dev/null and b/tests/fixtures/reftests/bugs/668-small-caps-ref.png differ
diff --git a/tests/fixtures/reftests/bugs/668-small-caps.svg b/tests/fixtures/reftests/bugs/668-small-caps.svg
new file mode 100644
index 00000000..a69f52b5
--- /dev/null
+++ b/tests/fixtures/reftests/bugs/668-small-caps.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; version="1.1" width="300" height="100">
+  <!-- This doesn't work with font-family:sans - one needs to use the real font name -->
+  <text x="10" y="70" style="font-family:Roboto; font-size:40; font-variant:small-caps;">BlahBlehBlih</text>
+</svg>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]