diff options
author | Khaled Hosny <khaled@aliftype.com> | 2022-08-24 07:54:15 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2022-08-25 13:04:27 +0200 |
commit | 9ca3c6794ca1969f453ddaa66c4e02ac8c695cc6 (patch) | |
tree | 2ea75caefa815dca69ece9400f08e10f343b55fb /vcl/source | |
parent | 19787042b1f139cb9c366801d283c6c0227e85e6 (diff) |
FeatureCollector: Detect ENUM features in OpenType fonts
A bit heavy handed, but the only way to detect the number of alternates
a feature has is to get the maximum number of alternates for all lookups
in all glyphs of this feature, which is what this code does.
Additionally, for cvXX features, parameter names from the font are used
when available.
Change-Id: Ib7f7ea5e5288b47b03dac8703668ca6c4007f517
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138749
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl/source')
-rw-r--r-- | vcl/source/font/FeatureCollector.cxx | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/vcl/source/font/FeatureCollector.cxx b/vcl/source/font/FeatureCollector.cxx index 8b506f7dfd47..c792d50d7e17 100644 --- a/vcl/source/font/FeatureCollector.cxx +++ b/vcl/source/font/FeatureCollector.cxx @@ -11,6 +11,9 @@ #include <font/OpenTypeFeatureDefinitionList.hxx> #include <i18nlangtag/languagetag.hxx> +#include <font/OpenTypeFeatureStrings.hrc> +#include <svdata.hxx> + #include <hb-ot.h> #include <hb-graphite2.h> @@ -123,24 +126,76 @@ void FeatureCollector::collectForTable(hb_tag_t aTableTag) rFeature.m_nCode = aFeatureTag; FeatureDefinition aDefinition = OpenTypeFeatureDefinitionList().getDefinition(aFeatureTag); - - if (OpenTypeFeatureDefinitionListPrivate::isSpecialFeatureCode(aFeatureTag)) + std::vector<vcl::font::FeatureParameter> aParameters{ + { 0, VclResId(STR_FONT_FEATURE_PARAM_NONE) } + }; + + unsigned int nFeatureIdx; + if (hb_ot_layout_language_find_feature(m_pHbFace, aTableTag, 0, + HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX, aFeatureTag, + &nFeatureIdx)) { - unsigned int nFeatureIdx; - if (hb_ot_layout_language_find_feature(m_pHbFace, aTableTag, 0, - HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX, aFeatureTag, - &nFeatureIdx)) + if (OpenTypeFeatureDefinitionListPrivate::isSpecialFeatureCode(aFeatureTag)) { + // ssXX and cvXX can have name ID defined for them, check for + // them and use as appropriate. hb_ot_name_id_t aLabelID; + hb_ot_name_id_t aFirstParameterID; + unsigned nNamedParameters; if (hb_ot_layout_feature_get_name_ids(m_pHbFace, aTableTag, nFeatureIdx, &aLabelID, - nullptr, nullptr, nullptr, nullptr)) + nullptr, nullptr, &nNamedParameters, + &aFirstParameterID)) { OString sLanguage = m_rLanguageTag.getBcp47().toUtf8(); OUString sLabel = getName(m_pHbFace, aLabelID, sLanguage); if (!sLabel.isEmpty()) aDefinition = vcl::font::FeatureDefinition(aFeatureTag, sLabel); + + // cvXX features can have parameters name IDs, check for + // them and populate feature parameters as appropriate. + for (unsigned i = 0; i < nNamedParameters; i++) + { + hb_ot_name_id_t aNameID = aFirstParameterID + i; + OUString sName = getName(m_pHbFace, aNameID, sLanguage); + if (!sName.isEmpty()) + aParameters.emplace_back(uint32_t(i), sName); + } + } + } + + // Collect lookups in this feature, and input glyphs for each + // lookup, and calculate the max number of alternates they have. + unsigned int nLookups = hb_ot_layout_feature_get_lookups( + m_pHbFace, aTableTag, nFeatureIdx, 0, nullptr, nullptr); + std::vector<unsigned int> aLookups(nLookups); + hb_ot_layout_feature_get_lookups(m_pHbFace, aTableTag, nFeatureIdx, 0, &nLookups, + aLookups.data()); + unsigned int nAlternates = 0; + for (unsigned int nLookupIdx : aLookups) + { + hb_set_t* aGlyphs = hb_set_create(); + hb_ot_layout_lookup_collect_glyphs(m_pHbFace, aTableTag, nLookupIdx, nullptr, + aGlyphs, nullptr, nullptr); + hb_codepoint_t nGlyphIdx = HB_SET_VALUE_INVALID; + while (hb_set_next(aGlyphs, &nGlyphIdx)) + { + nAlternates = std::max( + nAlternates, hb_ot_layout_lookup_get_glyph_alternates( + m_pHbFace, nLookupIdx, nGlyphIdx, 0, nullptr, nullptr)); } } + + // Append the alternates to the feature parameters, keeping any + // existing ones calculated from cvXX features above. + for (unsigned int i = aParameters.size() - 1; i < nAlternates; i++) + aParameters.emplace_back(uint32_t(i + 1), OUString::number(i + 1)); + + if (aParameters.size() > 1) + { + aDefinition = vcl::font::FeatureDefinition( + aFeatureTag, aDefinition.getDescription(), + vcl::font::FeatureParameterType::ENUM, std::move(aParameters), 0); + } } if (aDefinition) |