| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.graphics.fonts; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.ParcelFileDescriptor; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.text.FontConfig; |
| 27 |
import android.util.TypedXmlSerializer; |
| 28 |
|
| 29 |
import org.xmlpull.v1.XmlPullParser; |
| 30 |
import org.xmlpull.v1.XmlPullParserException; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.lang.annotation.Retention; |
| 34 |
import java.lang.annotation.RetentionPolicy; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.List; |
| 37 |
import java.util.Objects; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public final class FontUpdateRequest implements Parcelable { |
| 44 |
|
| 45 |
public static final int TYPE_UPDATE_FONT_FILE = 0; |
| 46 |
public static final int TYPE_UPDATE_FONT_FAMILY = 1; |
| 47 |
|
| 48 |
@IntDef(prefix = "TYPE_", value = { |
| 49 |
TYPE_UPDATE_FONT_FILE, |
| 50 |
TYPE_UPDATE_FONT_FAMILY, |
| 51 |
}) |
| 52 |
@Retention(RetentionPolicy.SOURCE) |
| 53 |
public @interface Type {} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final class Font implements Parcelable { |
| 72 |
private static final String ATTR_INDEX = "index"; |
| 73 |
private static final String ATTR_WEIGHT = "weight"; |
| 74 |
private static final String ATTR_SLANT = "slant"; |
| 75 |
private static final String ATTR_AXIS = "axis"; |
| 76 |
private static final String ATTR_POSTSCRIPT_NAME = "name"; |
| 77 |
|
| 78 |
private final @NonNull String mPostScriptName; |
| 79 |
private final @NonNull FontStyle mFontStyle; |
| 80 |
private final @IntRange(from = 0) int mIndex; |
| 81 |
private final @NonNull String mFontVariationSettings; |
| 82 |
|
| 83 |
public Font(@NonNull String postScriptName, @NonNull FontStyle fontStyle, |
| 84 |
@IntRange(from = 0) int index, @NonNull String fontVariationSettings) { |
| 85 |
mPostScriptName = postScriptName; |
| 86 |
mFontStyle = fontStyle; |
| 87 |
mIndex = index; |
| 88 |
mFontVariationSettings = fontVariationSettings; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public int describeContents() { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void writeToParcel(Parcel dest, int flags) { |
| 98 |
dest.writeString8(mPostScriptName); |
| 99 |
dest.writeInt(mFontStyle.getWeight()); |
| 100 |
dest.writeInt(mFontStyle.getSlant()); |
| 101 |
dest.writeInt(mIndex); |
| 102 |
dest.writeString8(mFontVariationSettings); |
| 103 |
} |
| 104 |
|
| 105 |
public static final @NonNull Creator<Font> CREATOR = new Creator<Font>() { |
| 106 |
@Override |
| 107 |
public Font createFromParcel(Parcel source) { |
| 108 |
String fontName = source.readString8(); |
| 109 |
int weight = source.readInt(); |
| 110 |
int slant = source.readInt(); |
| 111 |
int index = source.readInt(); |
| 112 |
String varSettings = source.readString8(); |
| 113 |
return new Font(fontName, new FontStyle(weight, slant), index, varSettings); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public Font[] newArray(int size) { |
| 118 |
return new Font[size]; |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static void writeToXml(TypedXmlSerializer out, Font font) throws IOException { |
| 131 |
out.attribute(null, ATTR_POSTSCRIPT_NAME, font.getPostScriptName()); |
| 132 |
out.attributeInt(null, ATTR_INDEX, font.getIndex()); |
| 133 |
out.attributeInt(null, ATTR_WEIGHT, font.getFontStyle().getWeight()); |
| 134 |
out.attributeInt(null, ATTR_SLANT, font.getFontStyle().getSlant()); |
| 135 |
out.attribute(null, ATTR_AXIS, font.getFontVariationSettings()); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static Font readFromXml(XmlPullParser parser) throws IOException { |
| 148 |
String psName = parser.getAttributeValue(null, ATTR_POSTSCRIPT_NAME); |
| 149 |
if (psName == null) { |
| 150 |
throw new IOException("name attribute is missing in font tag."); |
| 151 |
} |
| 152 |
int index = getAttributeValueInt(parser, ATTR_INDEX, 0); |
| 153 |
int weight = getAttributeValueInt(parser, ATTR_WEIGHT, FontStyle.FONT_WEIGHT_NORMAL); |
| 154 |
int slant = getAttributeValueInt(parser, ATTR_SLANT, FontStyle.FONT_SLANT_UPRIGHT); |
| 155 |
String varSettings = parser.getAttributeValue(null, ATTR_AXIS); |
| 156 |
if (varSettings == null) { |
| 157 |
varSettings = ""; |
| 158 |
} |
| 159 |
return new Font(psName, new FontStyle(weight, slant), index, varSettings); |
| 160 |
} |
| 161 |
|
| 162 |
public @NonNull String getPostScriptName() { |
| 163 |
return mPostScriptName; |
| 164 |
} |
| 165 |
|
| 166 |
public @NonNull FontStyle getFontStyle() { |
| 167 |
return mFontStyle; |
| 168 |
} |
| 169 |
|
| 170 |
public @IntRange(from = 0) int getIndex() { |
| 171 |
return mIndex; |
| 172 |
} |
| 173 |
|
| 174 |
public @NonNull String getFontVariationSettings() { |
| 175 |
return mFontVariationSettings; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public boolean equals(Object o) { |
| 180 |
if (this == o) return true; |
| 181 |
if (o == null || getClass() != o.getClass()) return false; |
| 182 |
Font font = (Font) o; |
| 183 |
return mIndex == font.mIndex |
| 184 |
&& mPostScriptName.equals(font.mPostScriptName) |
| 185 |
&& mFontStyle.equals(font.mFontStyle) |
| 186 |
&& mFontVariationSettings.equals(font.mFontVariationSettings); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public int hashCode() { |
| 191 |
return Objects.hash(mPostScriptName, mFontStyle, mIndex, mFontVariationSettings); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public String toString() { |
| 196 |
return "Font{" |
| 197 |
+ "mPostScriptName='" + mPostScriptName + '\'' |
| 198 |
+ ", mFontStyle=" + mFontStyle |
| 199 |
+ ", mIndex=" + mIndex |
| 200 |
+ ", mFontVariationSettings='" + mFontVariationSettings + '\'' |
| 201 |
+ '}'; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static final class Family implements Parcelable { |
| 209 |
private static final String TAG_FAMILY = "family"; |
| 210 |
private static final String ATTR_NAME = "name"; |
| 211 |
private static final String TAG_FONT = "font"; |
| 212 |
|
| 213 |
private final @NonNull String mName; |
| 214 |
private final @NonNull List<Font> mFonts; |
| 215 |
|
| 216 |
public Family(String name, List<Font> fonts) { |
| 217 |
mName = name; |
| 218 |
mFonts = fonts; |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public int describeContents() { |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void writeToParcel(Parcel dest, int flags) { |
| 228 |
dest.writeString8(mName); |
| 229 |
dest.writeParcelableList(mFonts, flags); |
| 230 |
} |
| 231 |
|
| 232 |
public static final @NonNull Creator<Family> CREATOR = new Creator<Family>() { |
| 233 |
|
| 234 |
@Override |
| 235 |
public Family createFromParcel(Parcel source) { |
| 236 |
String familyName = source.readString8(); |
| 237 |
List<Font> fonts = source.readParcelableList( |
| 238 |
new ArrayList<>(), Font.class.getClassLoader(), android.graphics.fonts.FontUpdateRequest.Font.class); |
| 239 |
return new Family(familyName, fonts); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public Family[] newArray(int size) { |
| 244 |
return new Family[size]; |
| 245 |
} |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public static void writeFamilyToXml(@NonNull TypedXmlSerializer out, @NonNull Family family) |
| 257 |
throws IOException { |
| 258 |
out.attribute(null, ATTR_NAME, family.getName()); |
| 259 |
List<Font> fonts = family.getFonts(); |
| 260 |
for (int i = 0; i < fonts.size(); ++i) { |
| 261 |
Font font = fonts.get(i); |
| 262 |
out.startTag(null, TAG_FONT); |
| 263 |
Font.writeToXml(out, font); |
| 264 |
out.endTag(null, TAG_FONT); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public static @NonNull Family readFromXml(@NonNull XmlPullParser parser) |
| 277 |
throws XmlPullParserException, IOException { |
| 278 |
List<Font> fonts = new ArrayList<>(); |
| 279 |
if (parser.getEventType() != XmlPullParser.START_TAG |
| 280 |
|| !parser.getName().equals(TAG_FAMILY)) { |
| 281 |
throw new IOException("Unexpected parser state: must be START_TAG with family"); |
| 282 |
} |
| 283 |
String name = parser.getAttributeValue(null, ATTR_NAME); |
| 284 |
if (name == null) { |
| 285 |
throw new IOException("name attribute is missing in family tag."); |
| 286 |
} |
| 287 |
int type = 0; |
| 288 |
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 289 |
if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_FONT)) { |
| 290 |
fonts.add(Font.readFromXml(parser)); |
| 291 |
} else if (type == XmlPullParser.END_TAG && parser.getName().equals(TAG_FAMILY)) { |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
return new Family(name, fonts); |
| 296 |
} |
| 297 |
|
| 298 |
public @NonNull String getName() { |
| 299 |
return mName; |
| 300 |
} |
| 301 |
|
| 302 |
public @NonNull List<Font> getFonts() { |
| 303 |
return mFonts; |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public boolean equals(Object o) { |
| 308 |
if (this == o) return true; |
| 309 |
if (o == null || getClass() != o.getClass()) return false; |
| 310 |
Family family = (Family) o; |
| 311 |
return mName.equals(family.mName) && mFonts.equals(family.mFonts); |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public int hashCode() { |
| 316 |
return Objects.hash(mName, mFonts); |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public String toString() { |
| 321 |
return "Family{mName='" + mName + '\'' + ", mFonts=" + mFonts + '}'; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
public static final Creator<FontUpdateRequest> CREATOR = new Creator<FontUpdateRequest>() { |
| 326 |
@Override |
| 327 |
public FontUpdateRequest createFromParcel(Parcel in) { |
| 328 |
return new FontUpdateRequest(in); |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public FontUpdateRequest[] newArray(int size) { |
| 333 |
return new FontUpdateRequest[size]; |
| 334 |
} |
| 335 |
}; |
| 336 |
|
| 337 |
private final @Type int mType; |
| 338 |
|
| 339 |
@Nullable |
| 340 |
private final ParcelFileDescriptor mFd; |
| 341 |
|
| 342 |
@Nullable |
| 343 |
private final byte[] mSignature; |
| 344 |
|
| 345 |
@Nullable |
| 346 |
private final Family mFontFamily; |
| 347 |
|
| 348 |
public FontUpdateRequest(@NonNull ParcelFileDescriptor fd, @NonNull byte[] signature) { |
| 349 |
mType = TYPE_UPDATE_FONT_FILE; |
| 350 |
mFd = fd; |
| 351 |
mSignature = signature; |
| 352 |
mFontFamily = null; |
| 353 |
} |
| 354 |
|
| 355 |
public FontUpdateRequest(@NonNull Family fontFamily) { |
| 356 |
mType = TYPE_UPDATE_FONT_FAMILY; |
| 357 |
mFd = null; |
| 358 |
mSignature = null; |
| 359 |
mFontFamily = fontFamily; |
| 360 |
} |
| 361 |
|
| 362 |
public FontUpdateRequest(@NonNull String familyName, |
| 363 |
@NonNull List<FontFamilyUpdateRequest.Font> variations) { |
| 364 |
this(createFontFamily(familyName, variations)); |
| 365 |
} |
| 366 |
|
| 367 |
private static Family createFontFamily(@NonNull String familyName, |
| 368 |
@NonNull List<FontFamilyUpdateRequest.Font> fonts) { |
| 369 |
List<Font> updateFonts = new ArrayList<>(fonts.size()); |
| 370 |
for (FontFamilyUpdateRequest.Font font : fonts) { |
| 371 |
updateFonts.add(new Font( |
| 372 |
font.getPostScriptName(), |
| 373 |
font.getStyle(), |
| 374 |
font.getIndex(), |
| 375 |
FontVariationAxis.toFontVariationSettings(font.getAxes()))); |
| 376 |
} |
| 377 |
return new Family(familyName, updateFonts); |
| 378 |
} |
| 379 |
|
| 380 |
protected FontUpdateRequest(Parcel in) { |
| 381 |
mType = in.readInt(); |
| 382 |
mFd = in.readParcelable(ParcelFileDescriptor.class.getClassLoader(), android.os.ParcelFileDescriptor.class); |
| 383 |
mSignature = in.readBlob(); |
| 384 |
mFontFamily = in.readParcelable(FontConfig.FontFamily.class.getClassLoader(), android.graphics.fonts.FontUpdateRequest.Family.class); |
| 385 |
} |
| 386 |
|
| 387 |
public @Type int getType() { |
| 388 |
return mType; |
| 389 |
} |
| 390 |
|
| 391 |
@Nullable |
| 392 |
public ParcelFileDescriptor getFd() { |
| 393 |
return mFd; |
| 394 |
} |
| 395 |
|
| 396 |
@Nullable |
| 397 |
public byte[] getSignature() { |
| 398 |
return mSignature; |
| 399 |
} |
| 400 |
|
| 401 |
@Nullable |
| 402 |
public Family getFontFamily() { |
| 403 |
return mFontFamily; |
| 404 |
} |
| 405 |
|
| 406 |
@Override |
| 407 |
public int describeContents() { |
| 408 |
return mFd != null ? mFd.describeContents() : 0; |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public void writeToParcel(Parcel dest, int flags) { |
| 413 |
dest.writeInt(mType); |
| 414 |
dest.writeParcelable(mFd, flags); |
| 415 |
dest.writeBlob(mSignature); |
| 416 |
dest.writeParcelable(mFontFamily, flags); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
private static int getAttributeValueInt(XmlPullParser parser, String name, int defaultValue) { |
| 421 |
try { |
| 422 |
String value = parser.getAttributeValue(null, name); |
| 423 |
if (value == null) { |
| 424 |
return defaultValue; |
| 425 |
} |
| 426 |
return Integer.parseInt(value); |
| 427 |
} catch (NumberFormatException e) { |
| 428 |
return defaultValue; |
| 429 |
} |
| 430 |
} |
| 431 |
} |