1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "StringPiece.h"
18 #include "minikin/FontFeature.h"
19 #include "minikin/MinikinPaint.h"
20
21 namespace minikin {
22
parse(std::string_view fontFeatureSettings)23 std::vector<FontFeature> FontFeature::parse(std::string_view fontFeatureSettings) {
24 std::vector<FontFeature> features;
25
26 SplitIterator it(StringPiece(fontFeatureSettings), ',');
27 while (it.hasNext()) {
28 StringPiece featureStr = it.next();
29 static hb_feature_t feature;
30 // We do not allow setting features on ranges. As such, reject any setting that has
31 // non-universal range.
32 if (hb_feature_from_string(featureStr.data(), featureStr.size(), &feature) &&
33 feature.start == 0 && feature.end == (unsigned int)-1) {
34 features.push_back({feature.tag, feature.value});
35 }
36 }
37 return features;
38 }
39
cleanAndAddDefaultFontFeatures(const MinikinPaint & paint)40 std::vector<hb_feature_t> cleanAndAddDefaultFontFeatures(const MinikinPaint& paint) {
41 std::vector<hb_feature_t> features;
42 // Disable default-on non-required ligature features if letter-spacing
43 // See http://dev.w3.org/csswg/css-text-3/#letter-spacing-property
44 // "When the effective spacing between two characters is not zero (due to
45 // either justification or a non-zero value of letter-spacing), user agents
46 // should not apply optional ligatures."
47 if (fabs(paint.letterSpacing) > 0.03) {
48 static constexpr hb_feature_t no_liga = {HB_TAG('l', 'i', 'g', 'a'), 0, 0, ~0u};
49 static constexpr hb_feature_t no_clig = {HB_TAG('c', 'l', 'i', 'g'), 0, 0, ~0u};
50 features.push_back(no_liga);
51 features.push_back(no_clig);
52 }
53
54 bool default_enable_chws = true;
55
56 static constexpr hb_tag_t chws_tag = HB_TAG('c', 'h', 'w', 's');
57 static constexpr hb_tag_t halt_tag = HB_TAG('h', 'a', 'l', 't');
58 static constexpr hb_tag_t palt_tag = HB_TAG('p', 'a', 'l', 't');
59
60 for (const FontFeature& feature : paint.fontFeatureSettings) {
61 // OpenType requires disabling default `chws` feature if glyph-width features.
62 // https://docs.microsoft.com/en-us/typography/opentype/spec/features_ae#tag-chws
63 // Here, we follow Chrome's impl: not enabling default `chws` feature if `palt` or
64 // `halt` is enabled.
65 // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/fonts/shaping/font_features.cc;drc=77a9a09de0688ca449f5333a305ceaf3f36b6daf;l=215
66 if (feature.tag == chws_tag ||
67 (feature.value && (feature.tag == halt_tag || feature.tag == palt_tag))) {
68 default_enable_chws = false;
69 }
70 features.push_back({feature.tag, feature.value, 0, ~0u});
71 }
72
73 if (default_enable_chws) {
74 static constexpr hb_feature_t chws = {chws_tag, 1, 0, ~0u};
75 features.push_back(chws);
76 }
77
78 return features;
79 }
80
81 } // namespace minikin
82