From 9ca3c6794ca1969f453ddaa66c4e02ac8c695cc6 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Wed, 24 Aug 2022 07:54:15 +0200 Subject: FeatureCollector: Detect ENUM features in OpenType fonts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- vcl/source/font/FeatureCollector.cxx | 69 ++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 7 deletions(-) (limited to 'vcl/source') 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 #include +#include +#include + #include #include @@ -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 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 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) -- cgit