1 /* 2 * Copyright (C) 2017 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 package android.graphics.fonts; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.os.Build; 23 import android.text.TextUtils; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 import java.util.Objects; 28 import java.util.regex.Pattern; 29 30 /** 31 * Class that holds information about single font variation axis. 32 */ 33 public final class FontVariationAxis { 34 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 35 private final int mTag; 36 private final String mTagString; 37 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 38 private final float mStyleValue; 39 40 /** 41 * Construct FontVariationAxis. 42 * 43 * The axis tag must contain four ASCII characters. Tag string that are longer or shorter than 44 * four characters, or contains characters outside of U+0020..U+007E are invalid. 45 * 46 * @throws IllegalArgumentException If given tag string is invalid. 47 */ FontVariationAxis(@onNull String tagString, float styleValue)48 public FontVariationAxis(@NonNull String tagString, float styleValue) { 49 if (!isValidTag(tagString)) { 50 throw new IllegalArgumentException("Illegal tag pattern: " + tagString); 51 } 52 mTag = makeTag(tagString); 53 mTagString = tagString; 54 mStyleValue = styleValue; 55 } 56 57 /** 58 * Returns the OpenType style tag value. 59 * @hide 60 */ getOpenTypeTagValue()61 public int getOpenTypeTagValue() { 62 return mTag; 63 } 64 65 /** 66 * Returns the variable font axis tag associated to this axis. 67 */ getTag()68 public String getTag() { 69 return mTagString; 70 } 71 72 /** 73 * Returns the style value associated to the given axis for this font. 74 */ getStyleValue()75 public float getStyleValue() { 76 return mStyleValue; 77 } 78 79 /** 80 * Returns a valid font variation setting string for this object. 81 */ 82 @Override toString()83 public @NonNull String toString() { 84 return "'" + mTagString + "' " + Float.toString(mStyleValue); 85 } 86 87 /** 88 * The 'tag' attribute value is read as four character values between U+0020 and U+007E 89 * inclusive. 90 */ 91 private static final Pattern TAG_PATTERN = Pattern.compile("[\u0020-\u007E]{4}"); 92 93 /** 94 * Returns true if 'tagString' is valid for font variation axis tag. 95 */ isValidTag(String tagString)96 private static boolean isValidTag(String tagString) { 97 return tagString != null && TAG_PATTERN.matcher(tagString).matches(); 98 } 99 100 /** 101 * The 'styleValue' attribute has an optional leading '-', followed by '<digits>', 102 * '<digits>.<digits>', or '.<digits>' where '<digits>' is one or more of [0-9]. 103 */ 104 private static final Pattern STYLE_VALUE_PATTERN = 105 Pattern.compile("-?(([0-9]+(\\.[0-9]+)?)|(\\.[0-9]+))"); 106 isValidValueFormat(String valueString)107 private static boolean isValidValueFormat(String valueString) { 108 return valueString != null && STYLE_VALUE_PATTERN.matcher(valueString).matches(); 109 } 110 111 /** @hide */ makeTag(String tagString)112 public static int makeTag(String tagString) { 113 final char c1 = tagString.charAt(0); 114 final char c2 = tagString.charAt(1); 115 final char c3 = tagString.charAt(2); 116 final char c4 = tagString.charAt(3); 117 return (c1 << 24) | (c2 << 16) | (c3 << 8) | c4; 118 } 119 120 /** 121 * Construct FontVariationAxis array from font variation settings. 122 * 123 * The settings string is constructed from multiple pairs of axis tag and style values. The axis 124 * tag must contain four ASCII characters and must be wrapped with single quotes (U+0027) or 125 * double quotes (U+0022). Axis strings that are longer or shorter than four characters, or 126 * contain characters outside of U+0020..U+007E are invalid. If a specified axis name is not 127 * defined in the font, the settings will be ignored. 128 * 129 * <pre> 130 * FontVariationAxis.fromFontVariationSettings("'wdth' 1.0"); 131 * FontVariationAxis.fromFontVariationSettings("'AX ' 1.0, 'FB ' 2.0"); 132 * </pre> 133 * 134 * @param settings font variation settings. 135 * @return FontVariationAxis[] the array of parsed font variation axis. {@code null} if settings 136 * has no font variation settings. 137 * @throws IllegalArgumentException If given string is not a valid font variation settings 138 * format. 139 */ fromFontVariationSettings( @ullable String settings)140 public static @Nullable FontVariationAxis[] fromFontVariationSettings( 141 @Nullable String settings) { 142 if (settings == null || settings.isEmpty()) { 143 return null; 144 } 145 final ArrayList<FontVariationAxis> axisList = new ArrayList<>(); 146 final int length = settings.length(); 147 for (int i = 0; i < length; i++) { 148 final char c = settings.charAt(i); 149 if (Character.isWhitespace(c)) { 150 continue; 151 } 152 if (!(c == '\'' || c == '"') || length < i + 6 || settings.charAt(i + 5) != c) { 153 throw new IllegalArgumentException( 154 "Tag should be wrapped with double or single quote: " + settings); 155 } 156 final String tagString = settings.substring(i + 1, i + 5); 157 158 i += 6; // Move to end of tag. 159 int endOfValueString = settings.indexOf(',', i); 160 if (endOfValueString == -1) { 161 endOfValueString = length; 162 } 163 final float value; 164 try { 165 // Float.parseFloat ignores leading/trailing whitespaces. 166 value = Float.parseFloat(settings.substring(i, endOfValueString)); 167 } catch (NumberFormatException e) { 168 throw new IllegalArgumentException( 169 "Failed to parse float string: " + e.getMessage()); 170 } 171 axisList.add(new FontVariationAxis(tagString, value)); 172 i = endOfValueString; 173 } 174 if (axisList.isEmpty()) { 175 return null; 176 } 177 return axisList.toArray(new FontVariationAxis[0]); 178 } 179 180 /** 181 * Stringify the array of FontVariationAxis. 182 * 183 * @param axes an array of FontVariationAxis. 184 * @return String a valid font variation settings string. 185 */ toFontVariationSettings(@ullable FontVariationAxis[] axes)186 public static @NonNull String toFontVariationSettings(@Nullable FontVariationAxis[] axes) { 187 if (axes == null) { 188 return ""; 189 } 190 return TextUtils.join(",", axes); 191 } 192 193 /** 194 * Stringify the array of FontVariationAxis. 195 * @hide 196 */ toFontVariationSettings(@ullable List<FontVariationAxis> axes)197 public static @NonNull String toFontVariationSettings(@Nullable List<FontVariationAxis> axes) { 198 if (axes == null) { 199 return ""; 200 } 201 return TextUtils.join(",", axes); 202 } 203 204 @Override equals(@ullable Object o)205 public boolean equals(@Nullable Object o) { 206 if (o == this) { 207 return true; 208 } 209 if (o == null || !(o instanceof FontVariationAxis)) { 210 return false; 211 } 212 FontVariationAxis axis = (FontVariationAxis) o; 213 return axis.mTag == mTag && axis.mStyleValue == mStyleValue; 214 } 215 216 @Override hashCode()217 public int hashCode() { 218 return Objects.hash(mTag, mStyleValue); 219 } 220 } 221 222