/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.graphics.fonts; import android.annotation.IntDef; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Objects; /** * A font style object. * * This class represents a single font style which is a pair of weight value and slant value. * Here are common font styles examples: *

*

 * 
 * final FontStyle NORMAL = new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT);
 * final FontStyle BOLD = new FontStyle(FONT_WEIGHT_BOLD, FONT_SLANT_UPRIGHT);
 * final FontStyle ITALIC = new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_ITALIC);
 * final FontStyle BOLD_ITALIC = new FontStyle(FONT_WEIGHT_BOLD, FONT_SLANT_ITALIC);
 * 
 * 
*

* */ public final class FontStyle { private static final String TAG = "FontStyle"; /** * A default value when font weight is unspecified */ public static final int FONT_WEIGHT_UNSPECIFIED = -1; /** * A minimum weight value for the font */ public static final int FONT_WEIGHT_MIN = 1; /** * A font weight value for the thin weight */ public static final int FONT_WEIGHT_THIN = 100; /** * A font weight value for the extra-light weight */ public static final int FONT_WEIGHT_EXTRA_LIGHT = 200; /** * A font weight value for the light weight */ public static final int FONT_WEIGHT_LIGHT = 300; /** * A font weight value for the normal weight */ public static final int FONT_WEIGHT_NORMAL = 400; /** * A font weight value for the medium weight */ public static final int FONT_WEIGHT_MEDIUM = 500; /** * A font weight value for the semi-bold weight */ public static final int FONT_WEIGHT_SEMI_BOLD = 600; /** * A font weight value for the bold weight. */ public static final int FONT_WEIGHT_BOLD = 700; /** * A font weight value for the extra-bold weight */ public static final int FONT_WEIGHT_EXTRA_BOLD = 800; /** * A font weight value for the black weight */ public static final int FONT_WEIGHT_BLACK = 900; /** * A maximum weight value for the font */ public static final int FONT_WEIGHT_MAX = 1000; /** * A font slant value for upright */ public static final int FONT_SLANT_UPRIGHT = 0; /** * A font slant value for italic */ public static final int FONT_SLANT_ITALIC = 1; // TODO: Support FONT_SLANT_OBLIQUE /** @hide */ @IntDef(prefix = { "FONT_SLANT_" }, value = { FONT_SLANT_UPRIGHT, FONT_SLANT_ITALIC }) @Retention(RetentionPolicy.SOURCE) public @interface FontSlant {} private final @IntRange(from = 0, to = 1000) int mWeight; private final @FontSlant int mSlant; // TODO: Support width public FontStyle() { mWeight = FONT_WEIGHT_NORMAL; mSlant = FONT_SLANT_UPRIGHT; } /** * Create FontStyle with specific weight and italic * *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* * @see FontStyle#FONT_WEIGHT_THIN * @see FontStyle#FONT_WEIGHT_EXTRA_LIGHT * @see FontStyle#FONT_WEIGHT_LIGHT * @see FontStyle#FONT_WEIGHT_NORMAL * @see FontStyle#FONT_WEIGHT_MEDIUM * @see FontStyle#FONT_WEIGHT_SEMI_BOLD * @see FontStyle#FONT_WEIGHT_BOLD * @see FontStyle#FONT_WEIGHT_EXTRA_BOLD * @see FontStyle#FONT_WEIGHT_BLACK * @param weight a weight value * @param slant a slant value */ public FontStyle(int weight, @FontSlant int slant) { Preconditions.checkArgument(FONT_WEIGHT_MIN <= weight && weight <= FONT_WEIGHT_MAX, "weight value must be [" + FONT_WEIGHT_MIN + ", " + FONT_WEIGHT_MAX + "]"); Preconditions.checkArgument(slant == FONT_SLANT_UPRIGHT || slant == FONT_SLANT_ITALIC, "slant value must be FONT_SLANT_UPRIGHT or FONT_SLANT_UPRIGHT"); mWeight = weight; mSlant = slant; } /** * Gets the weight value * * @see #FontStyle(int, int) * @return a weight value */ public @IntRange(from = 0, to = 1000) int getWeight() { return mWeight; } /** * Gets the slant value * * @return a slant value */ public @FontSlant int getSlant() { return mSlant; } /** * Compute the matching score for another style. * * The smaller is better. * @hide */ public int getMatchScore(@NonNull FontStyle o) { return Math.abs((getWeight() - o.getWeight())) / 100 + (getSlant() == o.getSlant() ? 0 : 2); } @Override public boolean equals(@Nullable Object o) { if (o == this) { return true; } if (o == null || !(o instanceof FontStyle)) { return false; } FontStyle fontStyle = (FontStyle) o; return fontStyle.mWeight == mWeight && fontStyle.mSlant == mSlant; } @Override public int hashCode() { return Objects.hash(mWeight, mSlant); } @Override public String toString() { return "FontStyle { weight=" + mWeight + ", slant=" + mSlant + "}"; } }
ValueNameAndroid Definition
100Thin{@link FontStyle#FONT_WEIGHT_THIN}
200Extra Light (Ultra Light){@link FontStyle#FONT_WEIGHT_EXTRA_LIGHT}
300Light{@link FontStyle#FONT_WEIGHT_LIGHT}
400Normal (Regular){@link FontStyle#FONT_WEIGHT_NORMAL}
500Medium{@link FontStyle#FONT_WEIGHT_MEDIUM}
600Semi Bold (Demi Bold){@link FontStyle#FONT_WEIGHT_SEMI_BOLD}
700Bold{@link FontStyle#FONT_WEIGHT_BOLD}
800Extra Bold (Ultra Bold){@link FontStyle#FONT_WEIGHT_EXTRA_BOLD}
900Black (Heavy){@link FontStyle#FONT_WEIGHT_BLACK}