summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavmjong Bah <tavmjong@free.fr>2018-03-19 11:44:48 +0000
committerTavmjong Bah <tavmjong@free.fr>2018-03-19 11:44:48 +0000
commitee1369b5a8d3fc94cfb99280d1a65d1bcd766111 (patch)
tree819a383405b8923e9e20416ec468533b8f527e89 /src
parentRemoving sample meson build files. (diff)
downloadinkscape-ee1369b5a8d3fc94cfb99280d1a65d1bcd766111.tar.gz
inkscape-ee1369b5a8d3fc94cfb99280d1a65d1bcd766111.zip
Add basic rendering support for 'font-variation-settings'.
Requires Pango 1.41.1 and FreeType 2.8 (maybe 2.9).
Diffstat (limited to 'src')
-rw-r--r--src/libnrtype/FontInstance.cpp95
1 files changed, 93 insertions, 2 deletions
diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp
index 527c06ce9..cab8237ba 100644
--- a/src/libnrtype/FontInstance.cpp
+++ b/src/libnrtype/FontInstance.cpp
@@ -22,7 +22,12 @@
#include FT_TRUETYPE_TAGS_H
#include FT_TRUETYPE_TABLES_H
#include FT_GLYPH_H
+#include FT_MULTIPLE_MASTERS_H
+
#include <pango/pangoft2.h>
+
+#include <glibmm/regex.h>
+
#include <2geom/pathvector.h>
#include <2geom/path-sink.h>
#include "libnrtype/font-glyph.h"
@@ -209,12 +214,98 @@ void font_instance::InitTheFace()
theFace = pango_fc_font_lock_face(PANGO_FC_FONT(pFont));
if ( theFace ) {
- FT_Select_Charmap(theFace,ft_encoding_unicode) && FT_Select_Charmap(theFace,ft_encoding_symbol);
+ FT_Select_Charmap(theFace, ft_encoding_unicode);
+ FT_Select_Charmap(theFace, ft_encoding_symbol);
}
#endif
- FindFontMetrics();
+#ifndef USE_PANGO_WIN32
+#if PANGO_VERSION_CHECK(1,41,1)
+#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 8 // 2.8 does not seem to work even though it has some support.
+
+ // 'font-variation-settings' support.
+ // The font returned from pango_fc_font_lock_face does not include variation settings. We must set them.
+
+ // We need to:
+ // Get a list of axes supported in the font with the range of allowed values.
+ // Extract axes with values from Pango font description.
+ // Fill an array indexed by FT axis with values from Pango.
+
+ char const *var = pango_font_description_get_variations( descr );
+ if (var) {
+
+ Glib::ustring variations(var);
+
+ FT_MM_Var* mmvar = NULL;
+ FT_Multi_Master mmtype;
+ if (FT_HAS_MULTIPLE_MASTERS( theFace ) && // Font has variables
+ FT_Get_MM_Var(theFace, &mmvar) == 0 && // We found the data
+ FT_Get_Multi_Master(theFace, &mmtype) !=0) { // It's not an Adobe MM font
+
+ // std::cout << " Multiple Masters: variables: " << mmvar->num_axis
+ // << " named styles: " << mmvar->num_namedstyles << std::endl;
+
+ // Get a list of axis and their default values from the font.
+ std::map<Glib::ustring, int> axis_map;
+ std::vector<int> axis_value;
+ for (FT_UInt axisIndex = 0; axisIndex < mmvar->num_axis; ++axisIndex) {
+
+ const FT_Var_Axis& ftAxis = mmvar->axis[axisIndex];
+ const FT_Tag axisTag = ftAxis.tag;
+
+ Glib::ustring axis_name;
+ axis_name += (char)(axisTag>>24 & 0xff);
+ axis_name += (char)(axisTag>>16 & 0xff);
+ axis_name += (char)(axisTag>> 8 & 0xff);
+ axis_name += (char)(axisTag & 0xff);
+
+ axis_map.insert(std::pair<Glib::ustring, int>(axis_name, axisIndex));
+ axis_value.push_back(static_cast<FT_Int32>(ftAxis.def/65536.0));
+ }
+
+ // Get the required values from Pango Font Description
+ // Need to check format of values from Pango, for the moment accept any format.
+ Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create("(\\w{4})=([-+]?\\d*\\.?\\d+([eE][-+]?\\d+)?)");
+ Glib::MatchInfo matchInfo;
+
+ std::vector<Glib::ustring> tokens = Glib::Regex::split_simple(",", variations);
+ for (auto token: tokens) {
+
+ regex->match(token, matchInfo);
+ if (matchInfo.matches()) {
+ float value = std::stod(matchInfo.fetch(2)); // Should clamp value
+
+ auto it = axis_map.find(matchInfo.fetch(1));
+ if (it != axis_map.end()) {
+ axis_value[it->second] = value;
+ }
+ }
+ }
+
+ // Fill coordinate array
+ const FT_UInt num_axis = axis_map.size();
+ FT_Fixed w[num_axis];
+ for (FT_UInt axisIndex = 0; axisIndex < num_axis; ++axisIndex) {
+ w[axisIndex] = axis_value[axisIndex] * 65536;
+ }
+
+ // Set design coordinates
+ FT_Error err;
+ err = FT_Set_Var_Design_Coordinates (theFace, 2, w);
+ if (err) {
+ std::cout << "font_instance::InitTheFace(): Error in call to FT_Set_Var_Design_Coordinates(): " << err << std::endl;
+ }
+
+ // FT_Done_MM_Var(mmlib, mmvar);
+ }
+ }
+
+#endif // FreeType
+#endif // Pango
+#endif // !USE_PANGO_WIN32
+
+ FindFontMetrics();
}
}