diff options
| author | Tavmjong Bah <tavmjong@free.fr> | 2018-03-13 12:23:04 +0000 |
|---|---|---|
| committer | Tavmjong Bah <tavmjong@free.fr> | 2018-03-13 12:23:04 +0000 |
| commit | de12635049e919e8d6f8f374f45cc82f99c48105 (patch) | |
| tree | 2851a0f2b4c2dafa9170f631bdd447cf7e51e098 /src/libnrtype | |
| parent | Remove unused functions and reformat section. (diff) | |
| download | inkscape-de12635049e919e8d6f8f374f45cc82f99c48105.tar.gz inkscape-de12635049e919e8d6f8f374f45cc82f99c48105.zip | |
Remove duplicate code.
Diffstat (limited to 'src/libnrtype')
| -rw-r--r-- | src/libnrtype/FontFactory.cpp | 263 | ||||
| -rw-r--r-- | src/libnrtype/FontFactory.h | 4 | ||||
| -rw-r--r-- | src/libnrtype/Layout-TNG-Input.cpp | 123 | ||||
| -rw-r--r-- | src/libnrtype/Layout-TNG.h | 10 |
4 files changed, 156 insertions, 244 deletions
diff --git a/src/libnrtype/FontFactory.cpp b/src/libnrtype/FontFactory.cpp index bf8cf589c..6a970c789 100644 --- a/src/libnrtype/FontFactory.cpp +++ b/src/libnrtype/FontFactory.cpp @@ -70,6 +70,139 @@ bool font_descr_equal::operator()( PangoFontDescription *const&a, PangoFontDesc return true; } +// User must free return value. +PangoFontDescription* ink_font_description_from_style(SPStyle const *style) +{ + PangoFontDescription *descr = pango_font_description_new(); + + pango_font_description_set_family(descr, style->font_family.value); + + // This duplicates Layout::EnumConversionItem... perhaps we can share code? + switch ( style->font_style.computed ) { + case SP_CSS_FONT_STYLE_ITALIC: + pango_font_description_set_style(descr, PANGO_STYLE_ITALIC); + break; + + case SP_CSS_FONT_STYLE_OBLIQUE: + pango_font_description_set_style(descr, PANGO_STYLE_OBLIQUE); + break; + + case SP_CSS_FONT_STYLE_NORMAL: + default: + pango_font_description_set_style(descr, PANGO_STYLE_NORMAL); + break; + } + + switch( style->font_weight.computed ) { + case SP_CSS_FONT_WEIGHT_100: + pango_font_description_set_weight(descr, PANGO_WEIGHT_THIN); + break; + + case SP_CSS_FONT_WEIGHT_200: + pango_font_description_set_weight(descr, PANGO_WEIGHT_ULTRALIGHT); + break; + + case SP_CSS_FONT_WEIGHT_300: + pango_font_description_set_weight(descr, PANGO_WEIGHT_LIGHT); + break; + + case SP_CSS_FONT_WEIGHT_400: + case SP_CSS_FONT_WEIGHT_NORMAL: + pango_font_description_set_weight(descr, PANGO_WEIGHT_NORMAL); + break; + + case SP_CSS_FONT_WEIGHT_500: + pango_font_description_set_weight(descr, PANGO_WEIGHT_MEDIUM); + break; + + case SP_CSS_FONT_WEIGHT_600: + pango_font_description_set_weight(descr, PANGO_WEIGHT_SEMIBOLD); + break; + + case SP_CSS_FONT_WEIGHT_700: + case SP_CSS_FONT_WEIGHT_BOLD: + pango_font_description_set_weight(descr, PANGO_WEIGHT_BOLD); + break; + + case SP_CSS_FONT_WEIGHT_800: + pango_font_description_set_weight(descr, PANGO_WEIGHT_ULTRABOLD); + break; + + case SP_CSS_FONT_WEIGHT_900: + pango_font_description_set_weight(descr, PANGO_WEIGHT_HEAVY); + break; + + case SP_CSS_FONT_WEIGHT_LIGHTER: + case SP_CSS_FONT_WEIGHT_BOLDER: + default: + g_warning("FaceFromStyle: Unrecognized font_weight.computed value"); + pango_font_description_set_weight(descr, PANGO_WEIGHT_NORMAL); + break; + } + // PANGO_WIEGHT_ULTRAHEAVY not used (not CSS2) + + switch (style->font_stretch.computed) { + case SP_CSS_FONT_STRETCH_ULTRA_CONDENSED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_ULTRA_CONDENSED); + break; + + case SP_CSS_FONT_STRETCH_EXTRA_CONDENSED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_EXTRA_CONDENSED); + break; + + case SP_CSS_FONT_STRETCH_CONDENSED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_CONDENSED); + break; + + case SP_CSS_FONT_STRETCH_SEMI_CONDENSED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_SEMI_CONDENSED); + break; + + case SP_CSS_FONT_STRETCH_NORMAL: + pango_font_description_set_stretch(descr, PANGO_STRETCH_NORMAL); + break; + + case SP_CSS_FONT_STRETCH_SEMI_EXPANDED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_SEMI_EXPANDED); + break; + + case SP_CSS_FONT_STRETCH_EXPANDED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_EXPANDED); + break; + + case SP_CSS_FONT_STRETCH_EXTRA_EXPANDED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_EXTRA_EXPANDED); + break; + + case SP_CSS_FONT_STRETCH_ULTRA_EXPANDED: + pango_font_description_set_stretch(descr, PANGO_STRETCH_ULTRA_EXPANDED); + + case SP_CSS_FONT_STRETCH_WIDER: + case SP_CSS_FONT_STRETCH_NARROWER: + default: + g_warning("FaceFromStyle: Unrecognized font_stretch.computed value"); + pango_font_description_set_stretch(descr, PANGO_STRETCH_NORMAL); + break; + } + + switch ( style->font_variant.computed ) { + case SP_CSS_FONT_VARIANT_SMALL_CAPS: + pango_font_description_set_variant(descr, PANGO_VARIANT_SMALL_CAPS); + break; + + case SP_CSS_FONT_VARIANT_NORMAL: + default: + pango_font_description_set_variant(descr, PANGO_VARIANT_NORMAL); + break; + } + +#if PANGO_VERSION_CHECK(1,41,1) + pango_font_description_set_variations(descr, style->font_variation_settings.toString().c_str()); +#endif + + return descr; +} + /////////////////// helper functions static void noop(...) {} @@ -439,134 +572,8 @@ font_instance* font_factory::FaceFromStyle(SPStyle const *style) // If that failed, try using the CSS information in the style if (!font) { - - PangoFontDescription *temp_descr = pango_font_description_new(); - - pango_font_description_set_family(temp_descr, style->font_family.value); - - // This duplicates Layout::EnumConversionItem... perhaps we can share code? - switch ( style->font_style.computed ) { - case SP_CSS_FONT_STYLE_ITALIC: - pango_font_description_set_style(temp_descr, PANGO_STYLE_ITALIC); - break; - - case SP_CSS_FONT_STYLE_OBLIQUE: - pango_font_description_set_style(temp_descr, PANGO_STYLE_OBLIQUE); - break; - - case SP_CSS_FONT_STYLE_NORMAL: - default: - pango_font_description_set_style(temp_descr, PANGO_STYLE_NORMAL); - break; - } - - switch( style->font_weight.computed ) { - case SP_CSS_FONT_WEIGHT_100: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_THIN); - break; - - case SP_CSS_FONT_WEIGHT_200: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_ULTRALIGHT); - break; - - case SP_CSS_FONT_WEIGHT_300: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_LIGHT); - break; - - case SP_CSS_FONT_WEIGHT_400: - case SP_CSS_FONT_WEIGHT_NORMAL: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_NORMAL); - break; - - case SP_CSS_FONT_WEIGHT_500: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_MEDIUM); - break; - - case SP_CSS_FONT_WEIGHT_600: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_SEMIBOLD); - break; - - case SP_CSS_FONT_WEIGHT_700: - case SP_CSS_FONT_WEIGHT_BOLD: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_BOLD); - break; - - case SP_CSS_FONT_WEIGHT_800: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_ULTRABOLD); - break; - - case SP_CSS_FONT_WEIGHT_900: - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_HEAVY); - break; - - case SP_CSS_FONT_WEIGHT_LIGHTER: - case SP_CSS_FONT_WEIGHT_BOLDER: - default: - g_warning("FaceFromStyle: Unrecognized font_weight.computed value"); - pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_NORMAL); - break; - } - // PANGO_WIEGHT_ULTRAHEAVY not used (not CSS2) - - switch (style->font_stretch.computed) { - case SP_CSS_FONT_STRETCH_ULTRA_CONDENSED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_ULTRA_CONDENSED); - break; - - case SP_CSS_FONT_STRETCH_EXTRA_CONDENSED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXTRA_CONDENSED); - break; - - case SP_CSS_FONT_STRETCH_CONDENSED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_CONDENSED); - break; - - case SP_CSS_FONT_STRETCH_SEMI_CONDENSED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_SEMI_CONDENSED); - break; - - case SP_CSS_FONT_STRETCH_NORMAL: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_NORMAL); - break; - - case SP_CSS_FONT_STRETCH_SEMI_EXPANDED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_SEMI_EXPANDED); - break; - - case SP_CSS_FONT_STRETCH_EXPANDED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXPANDED); - break; - - case SP_CSS_FONT_STRETCH_EXTRA_EXPANDED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXTRA_EXPANDED); - break; - - case SP_CSS_FONT_STRETCH_ULTRA_EXPANDED: - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_ULTRA_EXPANDED); - - case SP_CSS_FONT_STRETCH_WIDER: - case SP_CSS_FONT_STRETCH_NARROWER: - default: - g_warning("FaceFromStyle: Unrecognized font_stretch.computed value"); - pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_NORMAL); - break; - } - - switch ( style->font_variant.computed ) { - case SP_CSS_FONT_VARIANT_SMALL_CAPS: - pango_font_description_set_variant(temp_descr, PANGO_VARIANT_SMALL_CAPS); - break; - - case SP_CSS_FONT_VARIANT_NORMAL: - default: - pango_font_description_set_variant(temp_descr, PANGO_VARIANT_NORMAL); - break; - } - -#if PANGO_VERSION_CHECK(1,41,1) - pango_font_description_set_variations(temp_descr, style->font_variation_settings.toString().c_str()); -#endif - + PangoFontDescription* temp_descr = + ink_font_description_from_style(style); font = Face(temp_descr); pango_font_description_free(temp_descr); } diff --git a/src/libnrtype/FontFactory.h b/src/libnrtype/FontFactory.h index 12260f99a..2f8907e27 100644 --- a/src/libnrtype/FontFactory.h +++ b/src/libnrtype/FontFactory.h @@ -48,6 +48,10 @@ struct font_descr_equal : public std::binary_function<PangoFontDescription*, Pan bool operator()(PangoFontDescription *const &a, PangoFontDescription *const &b) const; }; +// Constructs a PangoFontDescription from SPStyle. Font size is not included. +// User must free return value. +PangoFontDescription* ink_font_description_from_style(SPStyle const *style); + // Wraps calls to pango_font_description_get_family with some name substitution const char *sp_font_description_get_family(PangoFontDescription const *fontDescr); diff --git a/src/libnrtype/Layout-TNG-Input.cpp b/src/libnrtype/Layout-TNG-Input.cpp index 6ad3bc23b..6100fa262 100644 --- a/src/libnrtype/Layout-TNG-Input.cpp +++ b/src/libnrtype/Layout-TNG-Input.cpp @@ -122,15 +122,6 @@ void Layout::appendWrapShape(Shape const *shape, DisplayAlign display_align) _input_wrap_shapes.back().display_align = display_align; } -int Layout::_enum_converter(int input, EnumConversionItem const *conversion_table, unsigned conversion_table_size) -{ - for (unsigned i = 0 ; i < conversion_table_size ; i++) { - if (conversion_table[i].input == input) - return conversion_table[i].output; - } - return conversion_table[0].output; -} - Layout::Direction Layout::InputStreamTextSource::styleGetBlockProgression() const { switch( style->writing_mode.computed ) { @@ -203,45 +194,6 @@ Layout::Alignment Layout::InputStreamTextSource::styleGetAlignment(Layout::Direc return para_direction == LEFT_TO_RIGHT ? LEFT : RIGHT; } -static const Layout::EnumConversionItem enum_convert_spstyle_style_to_pango_style[] = { - {SP_CSS_FONT_STYLE_NORMAL, PANGO_STYLE_NORMAL}, - {SP_CSS_FONT_STYLE_ITALIC, PANGO_STYLE_ITALIC}, - {SP_CSS_FONT_STYLE_OBLIQUE, PANGO_STYLE_OBLIQUE} -}; - -static const Layout::EnumConversionItem enum_convert_spstyle_weight_to_pango_weight[] = { - // NB: The Pango web page calls 500 "the normal font" but both CSS2 and the Pango - // enumeration define 400 as normal. - {SP_CSS_FONT_WEIGHT_NORMAL, PANGO_WEIGHT_NORMAL}, - {SP_CSS_FONT_WEIGHT_BOLD, PANGO_WEIGHT_BOLD}, - {SP_CSS_FONT_WEIGHT_100, PANGO_WEIGHT_THIN}, - {SP_CSS_FONT_WEIGHT_200, PANGO_WEIGHT_ULTRALIGHT}, - {SP_CSS_FONT_WEIGHT_300, PANGO_WEIGHT_LIGHT}, - {SP_CSS_FONT_WEIGHT_400, PANGO_WEIGHT_NORMAL}, - {SP_CSS_FONT_WEIGHT_500, PANGO_WEIGHT_MEDIUM}, - {SP_CSS_FONT_WEIGHT_600, PANGO_WEIGHT_SEMIBOLD}, - {SP_CSS_FONT_WEIGHT_700, PANGO_WEIGHT_BOLD}, - {SP_CSS_FONT_WEIGHT_800, PANGO_WEIGHT_ULTRABOLD}, - {SP_CSS_FONT_WEIGHT_900, PANGO_WEIGHT_HEAVY} -}; - -static const Layout::EnumConversionItem enum_convert_spstyle_stretch_to_pango_stretch[] = { - {SP_CSS_FONT_STRETCH_NORMAL, PANGO_STRETCH_NORMAL}, - {SP_CSS_FONT_STRETCH_ULTRA_CONDENSED, PANGO_STRETCH_ULTRA_CONDENSED}, - {SP_CSS_FONT_STRETCH_EXTRA_CONDENSED, PANGO_STRETCH_EXTRA_CONDENSED}, - {SP_CSS_FONT_STRETCH_CONDENSED, PANGO_STRETCH_CONDENSED}, - {SP_CSS_FONT_STRETCH_SEMI_CONDENSED, PANGO_STRETCH_SEMI_CONDENSED}, - {SP_CSS_FONT_STRETCH_SEMI_EXPANDED, PANGO_STRETCH_SEMI_EXPANDED}, - {SP_CSS_FONT_STRETCH_EXPANDED, PANGO_STRETCH_EXPANDED}, - {SP_CSS_FONT_STRETCH_EXTRA_EXPANDED, PANGO_STRETCH_EXTRA_EXPANDED}, - {SP_CSS_FONT_STRETCH_ULTRA_EXPANDED, PANGO_STRETCH_ULTRA_EXPANDED} -}; - -static const Layout::EnumConversionItem enum_convert_spstyle_variant_to_pango_variant[] = { - {SP_CSS_FONT_VARIANT_NORMAL, PANGO_VARIANT_NORMAL}, - {SP_CSS_FONT_VARIANT_SMALL_CAPS, PANGO_VARIANT_SMALL_CAPS} -}; - font_instance *Layout::InputStreamTextSource::styleGetFontInstance() const { PangoFontDescription *descr = styleGetFontDescription(); @@ -253,71 +205,30 @@ font_instance *Layout::InputStreamTextSource::styleGetFontInstance() const PangoFontDescription *Layout::InputStreamTextSource::styleGetFontDescription() const { - PangoFontDescription *descr = pango_font_description_new(); - // Pango can't cope with spaces before or after the commas - let's remove them. - // this code is not exactly unicode-safe, but it's similar to what's done in - // pango, so it's not the limiting factor - Glib::ustring family; - if (style->font_family.value == NULL) { - family = "sans-serif"; - } else { - gchar **families = g_strsplit(style->font_family.value, ",", -1); - if (families) { - for (gchar **f = families ; *f ; ++f) { - g_strstrip(*f); - if (!family.empty()) family += ','; - family += *f; - } - } - g_strfreev(families); - } + // This use to be done by code here but it duplicated more complete code in FontFactory.cpp. + PangoFontDescription *descr = ink_font_description_from_style( style ); - pango_font_description_set_family(descr,family.c_str()); - pango_font_description_set_weight( - descr, - (PangoWeight)_enum_converter( - style->font_weight.computed, - enum_convert_spstyle_weight_to_pango_weight, - sizeof(enum_convert_spstyle_weight_to_pango_weight) / sizeof(enum_convert_spstyle_weight_to_pango_weight[0]) - ) - ); - pango_font_description_set_style( - descr, - (PangoStyle)_enum_converter( - style->font_style.computed, - enum_convert_spstyle_style_to_pango_style, - sizeof(enum_convert_spstyle_style_to_pango_style) / sizeof(enum_convert_spstyle_style_to_pango_style[0]) - ) - ); - pango_font_description_set_variant( - descr, - (PangoVariant)_enum_converter( - style->font_variant.computed, - enum_convert_spstyle_variant_to_pango_variant, - sizeof(enum_convert_spstyle_variant_to_pango_variant) / sizeof(enum_convert_spstyle_variant_to_pango_variant[0]) - ) - ); + // Font size not yet set #ifdef USE_PANGO_WIN32 - // damn Pango fudges the size, so we need to unfudge. See source of pango_win32_font_map_init() - pango_font_description_set_size( - descr, - (int) ((font_factory::Default())->fontSize*PANGO_SCALE*72 / GetDeviceCaps(pango_win32_get_dc(),LOGPIXELSY)) // mandatory huge size (hinting workaround) + + // Damn Pango fudges the size, so we need to unfudge. See source of pango_win32_font_map_init() + pango_font_description_set_size(descr, + (int) ((font_factory::Default())->fontSize*PANGO_SCALE*72 / GetDeviceCaps(pango_win32_get_dc(),LOGPIXELSY)) ); - // we don't set stretch on Win32, because pango-win32 has no concept of it + + // We unset stretch on Win32, because pango-win32 has no concept of it // (Windows doesn't really provide any useful field it could use). // If we did set stretch, then any text with a font-stretch attribute would - // end up falling back to Arial. + // end up falling back to a default. + pango_font_description_unset_fields(descr, PANGO_FONT_MASK_STRETCH); + #else - pango_font_description_set_size(descr, (int) ((font_factory::Default())->fontSize*PANGO_SCALE)); // mandatory huge size (hinting workaround) - pango_font_description_set_stretch( - descr, - (PangoStretch)_enum_converter( - style->font_stretch.computed, - enum_convert_spstyle_stretch_to_pango_stretch, - sizeof(enum_convert_spstyle_stretch_to_pango_stretch) / sizeof(enum_convert_spstyle_stretch_to_pango_stretch[0]) - ) - ); + + // mandatory huge size (hinting workaround) + pango_font_description_set_size(descr, (int) ((font_factory::Default())->fontSize*PANGO_SCALE)); + #endif + return descr; } diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h index 878c76af8..51df43974 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -678,11 +678,6 @@ public: /** The strut is the minimum value used in calculating line height. */ FontMetrics strut; - /// see _enum_converter() - struct EnumConversionItem { - int input, output; - }; - private: /** Erases all the stuff set by the owner as input, ie #_input_stream and #_input_wrap_shapes. */ @@ -756,11 +751,6 @@ private: different variable names. */ static void _copyInputVector(std::vector<SVGLength> const &input_vector, unsigned input_offset, std::vector<SVGLength> *output_vector, size_t max_length); - /** There are a few cases where we have different sets of enums meaning - the same thing, eg Pango font styles vs. SPStyle font styles. These need - converting. */ - static int _enum_converter(int input, EnumConversionItem const *conversion_table, unsigned conversion_table_size); - /** The overall block-progression of the whole flow. */ inline Direction _blockProgression() const { |
