diff options
author | 2025-01-08 21:29:40 +0900 | |
---|---|---|
committer | 2025-01-09 23:30:09 +0900 | |
commit | 2d312c8ef2d7fb3155989e979b80eea5a1b0f97d (patch) | |
tree | 810e2be8cadd605164d7aabcbfa3dd2061813a2b /graphics/java | |
parent | b9b0e1647f4cc84bbb16aa9fcd6de40db8364e05 (diff) |
Add setFontVariationOverride instead of changing setFontVariationSettings behavior
Bug: 361260253
Test: CtsWidgetTestCases
Test: CtsTextTestCases
Test: CtsGraphicsTestCases
Test: minikin_tests
Flag: com.android.text.flags.typeface_redesign_readonly
Change-Id: I6a45314f28fea4237ec2d10bc24b5814325fd5b4
Diffstat (limited to 'graphics/java')
-rw-r--r-- | graphics/java/android/graphics/Paint.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 3378cc11d565..b332cf0d751f 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -19,6 +19,7 @@ package android.graphics; import static com.android.text.flags.Flags.FLAG_DEPRECATE_ELEGANT_TEXT_HEIGHT_API; import static com.android.text.flags.Flags.FLAG_FIX_LINE_HEIGHT_FOR_LOCALE; import static com.android.text.flags.Flags.FLAG_LETTER_SPACING_JUSTIFICATION; +import static com.android.text.flags.Flags.FLAG_TYPEFACE_REDESIGN_READONLY; import static com.android.text.flags.Flags.FLAG_VERTICAL_TEXT_LAYOUT; import android.annotation.ColorInt; @@ -57,6 +58,7 @@ import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Objects; @@ -96,6 +98,7 @@ public class Paint { private LocaleList mLocales; private String mFontFeatureSettings; private String mFontVariationSettings; + private String mFontVariationOverride; private float mShadowLayerRadius; private float mShadowLayerDx; @@ -2140,6 +2143,12 @@ public class Paint { * @see #getFontVariationSettings() * @see FontVariationAxis */ + // Add following API description once the setFontVariationOverride becomes public. + // This method generates new variation instance of the {@link Typeface} instance and set it to + // this object. Therefore, subsequent {@link #setTypeface(Typeface)} call will clear the font + // variation settings. Also, creating variation instance of the Typeface requires non trivial + // amount of time and memories, therefore consider using + // {@link #setFontVariationOverride(String, int)} for better performance. public boolean setFontVariationSettings(String fontVariationSettings) { final String settings = TextUtils.nullIfEmpty(fontVariationSettings); if (settings == mFontVariationSettings @@ -2174,6 +2183,68 @@ public class Paint { } /** + * Sets TrueType or OpenType font variation settings for overriding. + * + * The settings string is constructed from multiple pairs of axis tag and style values. The axis + * tag must contain four ASCII characters and must be wrapped with single quotes (U+0027) or + * double quotes (U+0022). Axis strings that are longer or shorter than four characters, or + * contain characters outside of U+0020..U+007E are invalid. + * + * If invalid font variation settings is provided, this method does nothing and returning false + * with printing error message to the logcat. + * + * Different from {@link #setFontVariationSettings(String)}, this overrides the font variation + * settings which is already assigned to the font instance. For example, if the underlying font + * is configured as {@code 'wght' 500, 'ital' 1}, and if the override is specified as + * {@code 'wght' 700, `wdth` 150}, then the effective font variation setting is + * {@code `wght' 700, 'ital' 1, 'wdth' 150}. The `wght` value is updated by override, 'ital' + * value is preserved because no overrides, and `wdth` value is added by override. + * + * @param fontVariationOverride font variation settings. You can pass null or empty string as + * no variation settings. + * + * @return true if the provided font variation settings is valid. Otherwise returns false. + * + * @see #getFontVariationSettings() + * @see #setFontVariationSettings(String) + * @see #getFontVariationOverride() + * @see FontVariationAxis + */ + @FlaggedApi(FLAG_TYPEFACE_REDESIGN_READONLY) + public boolean setFontVariationOverride(@Nullable String fontVariationOverride) { + if (Objects.equals(fontVariationOverride, mFontVariationOverride)) { + return true; + } + + List<FontVariationAxis> axes; + try { + axes = FontVariationAxis.fromFontVariationSettingsForList(fontVariationOverride); + } catch (IllegalArgumentException e) { + Log.i(TAG, "failed to parse font variation settings.", e); + return false; + } + long builderPtr = nCreateFontVariationBuilder(axes.size()); + for (int i = 0; i < axes.size(); ++i) { + FontVariationAxis axis = axes.get(i); + nAddFontVariationToBuilder( + builderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue()); + } + nSetFontVariationOverride(mNativePaint, builderPtr); + mFontVariationOverride = fontVariationOverride; + return true; + } + + /** + * Gets the current font variation override value. + * + * @return a current font variation override value. + */ + @FlaggedApi(FLAG_TYPEFACE_REDESIGN_READONLY) + public @Nullable String getFontVariationOverride() { + return mFontVariationOverride; + } + + /** * Get the current value of start hyphen edit. * * The default value is 0 which is equivalent to {@link #START_HYPHEN_EDIT_NO_EDIT}. |