FontConfig.java revision 4b5a4d221f377686a730182a3bffb8c6f190e313
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 17package android.text; 18 19import android.os.Parcel; 20import android.os.ParcelFileDescriptor; 21import android.os.Parcelable; 22 23import java.io.IOException; 24import java.util.ArrayList; 25import java.util.List; 26 27/** 28 * Font configuration descriptions for System fonts. 29 */ 30public final class FontConfig implements Parcelable { 31 private final List<Family> mFamilies = new ArrayList<>(); 32 private final List<Alias> mAliases = new ArrayList<>(); 33 34 public FontConfig() { 35 } 36 37 public FontConfig(FontConfig config) { 38 for (int i = 0; i < config.mFamilies.size(); i++) { 39 mFamilies.add(new Family(config.mFamilies.get(i))); 40 } 41 mAliases.addAll(config.mAliases); 42 } 43 44 /** 45 * Returns the ordered list of families included in the system fonts. 46 */ 47 public List<Family> getFamilies() { 48 return mFamilies; 49 } 50 51 /** 52 * Returns the list of aliases defined for the font families in the system fonts. 53 */ 54 public List<Alias> getAliases() { 55 return mAliases; 56 } 57 58 /** 59 * @hide 60 */ 61 public FontConfig(Parcel in) { 62 readFromParcel(in); 63 } 64 65 @Override 66 public void writeToParcel(Parcel out, int flag) { 67 out.writeInt(mFamilies.size()); 68 for (int i = 0; i < mFamilies.size(); i++) { 69 mFamilies.get(i).writeToParcel(out, flag); 70 } 71 out.writeInt(mAliases.size()); 72 for (int i = 0; i < mAliases.size(); i++) { 73 mAliases.get(i).writeToParcel(out, flag); 74 } 75 } 76 77 /** 78 * @hide 79 */ 80 public void readFromParcel(Parcel in) { 81 int size = in.readInt(); 82 for (int i = 0; i < size; i++) { 83 mFamilies.add(new Family(in)); 84 } 85 size = in.readInt(); 86 for (int i = 0; i < size; i++) { 87 mAliases.add(new Alias(in)); 88 } 89 } 90 91 @Override 92 public int describeContents() { 93 return 0; 94 } 95 96 public static final Parcelable.Creator<FontConfig> CREATOR = new Parcelable.Creator() { 97 public FontConfig createFromParcel(Parcel in) { 98 return new FontConfig(in); 99 } 100 public FontConfig[] newArray(int size) { 101 return new FontConfig[size]; 102 } 103 }; 104 105 /** 106 * Class that holds information about a Font axis. 107 */ 108 public static final class Axis implements Parcelable { 109 private final int mTag; 110 private final float mStyleValue; 111 112 public Axis(int tag, float styleValue) { 113 this.mTag = tag; 114 this.mStyleValue = styleValue; 115 } 116 117 /** 118 * Returns the variable font axis tag associated to this axis. 119 */ 120 public int getTag() { 121 return mTag; 122 } 123 124 /** 125 * Returns the style value associated to the given axis for this font. 126 */ 127 public float getStyleValue() { 128 return mStyleValue; 129 } 130 131 /** 132 * @hide 133 */ 134 public Axis(Parcel in) { 135 mTag = in.readInt(); 136 mStyleValue = in.readFloat(); 137 } 138 139 @Override 140 public void writeToParcel(Parcel out, int flag) { 141 out.writeInt(mTag); 142 out.writeFloat(mStyleValue); 143 } 144 145 @Override 146 public int describeContents() { 147 return 0; 148 } 149 150 public static final Creator<Axis> CREATOR = new Creator<Axis>() { 151 @Override 152 public Axis createFromParcel(Parcel in) { 153 return new Axis(in); 154 } 155 156 @Override 157 public Axis[] newArray(int size) { 158 return new Axis[size]; 159 } 160 }; 161 } 162 163 /** 164 * Class that holds information about a Font. 165 */ 166 public static final class Font implements Parcelable { 167 private String mFontName; 168 private final int mTtcIndex; 169 private final List<Axis> mAxes; 170 private final int mWeight; 171 private final boolean mIsItalic; 172 private ParcelFileDescriptor mFd; 173 private final int mResourceId; 174 175 /** 176 * @hide 177 */ 178 public Font(String fontName, int ttcIndex, List<Axis> axes, int weight, boolean isItalic, 179 int resourceId) { 180 mFontName = fontName; 181 mTtcIndex = ttcIndex; 182 mAxes = axes; 183 mWeight = weight; 184 mIsItalic = isItalic; 185 mFd = null; 186 mResourceId = resourceId; 187 } 188 189 public Font(String fontName, int ttcIndex, List<Axis> axes, int weight, boolean isItalic) { 190 this(fontName, ttcIndex, axes, weight, isItalic, 0); 191 } 192 193 public Font(Font origin) { 194 mFontName = origin.mFontName; 195 mTtcIndex = origin.mTtcIndex; 196 mAxes = new ArrayList<>(origin.mAxes); 197 mWeight = origin.mWeight; 198 mIsItalic = origin.mIsItalic; 199 if (origin.mFd != null) { 200 try { 201 mFd = origin.mFd.dup(); 202 } catch (IOException e) { 203 e.printStackTrace(); 204 } 205 } 206 mResourceId = origin.mResourceId; 207 } 208 209 /** 210 * Returns the name associated by the system to this font. 211 */ 212 public String getFontName() { 213 return mFontName; 214 } 215 216 /** 217 * @hide 218 */ 219 public void setFontName(String fontName) { 220 mFontName = fontName; 221 } 222 223 /** 224 * Returns the index to be used to access this font when accessing a TTC file. 225 */ 226 public int getTtcIndex() { 227 return mTtcIndex; 228 } 229 230 /** 231 * Returns the list of axes associated to this font. 232 */ 233 public List<Axis> getAxes() { 234 return mAxes; 235 } 236 237 /** 238 * Returns the weight value for this font. 239 */ 240 public int getWeight() { 241 return mWeight; 242 } 243 244 /** 245 * Returns whether this font is italic. 246 */ 247 public boolean isItalic() { 248 return mIsItalic; 249 } 250 251 /** 252 * Returns a file descriptor to access the specified font. This should be closed after use. 253 */ 254 public ParcelFileDescriptor getFd() { 255 return mFd; 256 } 257 258 /** 259 * @hide 260 */ 261 public void setFd(ParcelFileDescriptor fd) { 262 mFd = fd; 263 } 264 265 /** 266 * @hide 267 */ 268 public int getResourceId() { 269 return mResourceId; 270 } 271 272 /** 273 * @hide 274 */ 275 public Font(Parcel in) { 276 mFontName = in.readString(); 277 mTtcIndex = in.readInt(); 278 final int numAxes = in.readInt(); 279 mAxes = new ArrayList<>(); 280 for (int i = 0; i < numAxes; i++) { 281 mAxes.add(new Axis(in)); 282 } 283 mWeight = in.readInt(); 284 mIsItalic = in.readInt() == 1; 285 if (in.readInt() == 1) { /* has FD */ 286 mFd = ParcelFileDescriptor.CREATOR.createFromParcel(in); 287 } else { 288 mFd = null; 289 } 290 mResourceId = in.readInt(); 291 } 292 293 @Override 294 public void writeToParcel(Parcel out, int flag) { 295 out.writeString(mFontName); 296 out.writeInt(mTtcIndex); 297 out.writeInt(mAxes.size()); 298 for (int i = 0; i < mAxes.size(); i++) { 299 mAxes.get(i).writeToParcel(out, flag); 300 } 301 out.writeInt(mWeight); 302 out.writeInt(mIsItalic ? 1 : 0); 303 out.writeInt(mFd == null ? 0 : 1); 304 if (mFd != null) { 305 mFd.writeToParcel(out, flag); 306 } 307 out.writeInt(mResourceId); 308 } 309 310 @Override 311 public int describeContents() { 312 return 0; 313 } 314 315 public static final Creator<Font> CREATOR = new Creator<Font>() { 316 @Override 317 public Font createFromParcel(Parcel in) { 318 return new Font(in); 319 } 320 321 @Override 322 public Font[] newArray(int size) { 323 return new Font[size]; 324 } 325 }; 326 } 327 328 /** 329 * Class that holds information about a Font alias. 330 */ 331 public static final class Alias implements Parcelable { 332 private final String mName; 333 private final String mToName; 334 private final int mWeight; 335 336 public Alias(String name, String toName, int weight) { 337 this.mName = name; 338 this.mToName = toName; 339 this.mWeight = weight; 340 } 341 342 /** 343 * Returns the new name for the alias. 344 */ 345 public String getName() { 346 return mName; 347 } 348 349 /** 350 * Returns the existing name to which this alias points to. 351 */ 352 public String getToName() { 353 return mToName; 354 } 355 356 /** 357 * Returns the weight associated with this alias. 358 */ 359 public int getWeight() { 360 return mWeight; 361 } 362 363 /** 364 * @hide 365 */ 366 public Alias(Parcel in) { 367 mName = in.readString(); 368 mToName = in.readString(); 369 mWeight = in.readInt(); 370 } 371 372 @Override 373 public void writeToParcel(Parcel out, int flag) { 374 out.writeString(mName); 375 out.writeString(mToName); 376 out.writeInt(mWeight); 377 } 378 379 @Override 380 public int describeContents() { 381 return 0; 382 } 383 384 public static final Creator<Alias> CREATOR = new Creator<Alias>() { 385 @Override 386 public Alias createFromParcel(Parcel in) { 387 return new Alias(in); 388 } 389 390 @Override 391 public Alias[] newArray(int size) { 392 return new Alias[size]; 393 } 394 }; 395 } 396 397 /** 398 * Class that holds information about a Font family. 399 */ 400 public static final class Family implements Parcelable { 401 private final String mName; 402 private final List<Font> mFonts; 403 private final String mLanguage; 404 private final String mVariant; 405 private final String mProviderAuthority; 406 private final String mQuery; 407 408 public Family(String name, List<Font> fonts, String language, String variant) { 409 mName = name; 410 mFonts = fonts; 411 mLanguage = language; 412 mVariant = variant; 413 mProviderAuthority = null; 414 mQuery = null; 415 } 416 417 /** 418 * @hide 419 */ 420 public Family(String providerAuthority, String query) { 421 mName = null; 422 mFonts = null; 423 mLanguage = null; 424 mVariant = null; 425 mProviderAuthority = providerAuthority; 426 mQuery = query; 427 } 428 429 public Family(Family origin) { 430 mName = origin.mName; 431 mLanguage = origin.mLanguage; 432 mVariant = origin.mVariant; 433 mFonts = new ArrayList<>(); 434 for (int i = 0; i < origin.mFonts.size(); i++) { 435 mFonts.add(new Font(origin.mFonts.get(i))); 436 } 437 mProviderAuthority = origin.mProviderAuthority; 438 mQuery = origin.mQuery; 439 } 440 441 /** 442 * Returns the name given by the system to this font family. 443 */ 444 public String getName() { 445 return mName; 446 } 447 448 /** 449 * Returns the list of fonts included in this family. 450 */ 451 public List<Font> getFonts() { 452 return mFonts; 453 } 454 455 /** 456 * Returns the language for this family. May be null. 457 */ 458 public String getLanguage() { 459 return mLanguage; 460 } 461 462 /** 463 * Returns the font variant for this family, e.g. "elegant" or "compact". May be null. 464 */ 465 public String getVariant() { 466 return mVariant; 467 } 468 469 /** 470 * @hide 471 */ 472 public String getProviderAuthority() { 473 return mProviderAuthority; 474 } 475 476 /** 477 * @hide 478 */ 479 public String getQuery() { 480 return mQuery; 481 } 482 483 /** 484 * @hide 485 */ 486 public Family(Parcel in) { 487 mName = in.readString(); 488 final int size = in.readInt(); 489 mFonts = new ArrayList<>(); 490 for (int i = 0; i < size; i++) { 491 mFonts.add(new Font(in)); 492 } 493 mLanguage = in.readString(); 494 mVariant = in.readString(); 495 if (in.readInt() == 1) { 496 mProviderAuthority = in.readString(); 497 } else { 498 mProviderAuthority = null; 499 } 500 if (in.readInt() == 1) { 501 mQuery = in.readString(); 502 } else { 503 mQuery = null; 504 } 505 } 506 507 @Override 508 public void writeToParcel(Parcel out, int flag) { 509 out.writeString(mName); 510 out.writeInt(mFonts.size()); 511 for (int i = 0; i < mFonts.size(); i++) { 512 mFonts.get(i).writeToParcel(out, flag); 513 } 514 out.writeString(mLanguage); 515 out.writeString(mVariant); 516 out.writeInt(mProviderAuthority == null ? 0 : 1); 517 if (mProviderAuthority != null) { 518 out.writeString(mProviderAuthority); 519 } 520 out.writeInt(mQuery == null ? 0 : 1); 521 if (mQuery != null) { 522 out.writeString(mQuery); 523 } 524 } 525 526 @Override 527 public int describeContents() { 528 return 0; 529 } 530 531 public static final Creator<Family> CREATOR = new Creator<Family>() { 532 @Override 533 public Family createFromParcel(Parcel in) { 534 return new Family(in); 535 } 536 537 @Override 538 public Family[] newArray(int size) { 539 return new Family[size]; 540 } 541 }; 542 } 543} 544