summaryrefslogtreecommitdiff
path: root/graphics/java
diff options
context:
space:
mode:
authorDanny Lin <danny@kdrag0n.dev>2022-02-17 17:38:19 -0800
committeralk3pInjection <webmaster@raspii.tech>2022-05-07 00:20:58 +0800
commit2264c25527258109f3c0b804c65f1b95da67e265 (patch)
tree44072b5d07babd606919889f3bce62aa07284c84 /graphics/java
parentfa799a9081aa0912598a3a88f13ca456964bf718 (diff)
Implement Inter Dynamic Metrics for system UI font
Inter, our system UI font of choice, works best with the official Dynamic Metrics. The font's default spacing makes text relatively sparse compared to Roboto, Google Sans Text, and other common UI fonts. Dynamic Metrics optimizes tracking (letter spacing) based on font size to improve appearance. Spacing is increased for small text and decreased for larger text, making it generally more compact and closer to Apple's SF Pro. Math.exp is relatively expensive, so use a precalculated lookup table for common sizes (up to 32dp). The LUT follows steps of 0.5dp so we can safely use integer casting. For compatibility, only the default system UI font is affected, and only if spacing is unset (null or 0). Change-Id: I48642f05f4b60b07326f657aaac76d968465a5dd
Diffstat (limited to 'graphics/java')
-rw-r--r--graphics/java/android/graphics/Typeface.java30
1 files changed, 25 insertions, 5 deletions
diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java
index 61f7facf0916..2e11253f9fb8 100644
--- a/graphics/java/android/graphics/Typeface.java
+++ b/graphics/java/android/graphics/Typeface.java
@@ -212,6 +212,8 @@ public class Typeface {
private @IntRange(from = 0, to = FontStyle.FONT_WEIGHT_MAX) final int mWeight;
+ private boolean mIsSystemDefault;
+
// Value for weight and italic. Indicates the value is resolved by font metadata.
// Must be the same as the C++ constant in core/jni/android/graphics/FontFamily.cpp
/** @hide */
@@ -237,6 +239,7 @@ public class Typeface {
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static void setDefault(Typeface t) {
+ t.mIsSystemDefault = true;
synchronized (SYSTEM_FONT_MAP_LOCK) {
sDefaultTypeface = t;
nativeSetDefault(t.native_instance);
@@ -933,7 +936,7 @@ public class Typeface {
}
}
- typeface = new Typeface(nativeCreateFromTypeface(ni, style));
+ typeface = new Typeface(nativeCreateFromTypeface(ni, style), family.mIsSystemDefault);
styles.put(style, typeface);
}
return typeface;
@@ -1001,7 +1004,8 @@ public class Typeface {
}
typeface = new Typeface(
- nativeCreateFromTypefaceWithExactStyle(base.native_instance, weight, italic));
+ nativeCreateFromTypefaceWithExactStyle(base.native_instance, weight, italic),
+ base.mIsSystemDefault);
innerCache.put(key, typeface);
}
return typeface;
@@ -1011,7 +1015,8 @@ public class Typeface {
public static Typeface createFromTypefaceWithVariation(@Nullable Typeface family,
@NonNull List<FontVariationAxis> axes) {
final Typeface base = family == null ? Typeface.DEFAULT : family;
- return new Typeface(nativeCreateFromTypefaceWithVariation(base.native_instance, axes));
+ return new Typeface(nativeCreateFromTypefaceWithVariation(base.native_instance, axes),
+ base.mIsSystemDefault);
}
/**
@@ -1174,6 +1179,12 @@ public class Typeface {
mCleaner = sRegistry.registerNativeAllocation(this, native_instance);
mStyle = nativeGetStyle(ni);
mWeight = nativeGetWeight(ni);
+ mIsSystemDefault = false;
+ }
+
+ private Typeface(long ni, boolean isSystemDefault) {
+ this(ni);
+ mIsSystemDefault = isSystemDefault;
}
private static Typeface getSystemDefaultTypeface(@NonNull String familyName) {
@@ -1189,6 +1200,7 @@ public class Typeface {
for (Map.Entry<String, FontFamily[]> entry : fallbacks.entrySet()) {
outSystemFontMap.put(entry.getKey(), createFromFamilies(entry.getValue()));
}
+ outSystemFontMap.get("sans-serif").mIsSystemDefault = true;
for (int i = 0; i < aliases.size(); ++i) {
final FontConfig.Alias alias = aliases.get(i);
@@ -1203,7 +1215,8 @@ public class Typeface {
}
final int weight = alias.getWeight();
final Typeface newFace = weight == 400 ? base :
- new Typeface(nativeCreateWeightAlias(base.native_instance, weight));
+ new Typeface(nativeCreateWeightAlias(base.native_instance, weight),
+ base.mIsSystemDefault);
outSystemFontMap.put(alias.getName(), newFace);
}
}
@@ -1230,6 +1243,7 @@ public class Typeface {
for (Map.Entry<String, Typeface> entry : fontMap.entrySet()) {
nativePtrs[i++] = entry.getValue().native_instance;
writeString(namesBytes, entry.getKey());
+ writeInt(namesBytes, entry.getValue().mIsSystemDefault ? 1 : 0);
}
int typefacesBytesCount = nativeWriteTypefaces(null, nativePtrs);
// int (typefacesBytesCount), typefaces, namesBytes
@@ -1271,7 +1285,8 @@ public class Typeface {
buffer.position(buffer.position() + typefacesBytesCount);
for (long nativePtr : nativePtrs) {
String name = readString(buffer);
- out.put(name, new Typeface(nativePtr));
+ boolean isSystemDefault = buffer.getInt() == 1;
+ out.put(name, new Typeface(nativePtr, isSystemDefault));
}
return nativePtrs;
}
@@ -1496,6 +1511,11 @@ public class Typeface {
return families;
}
+ /** @hide */
+ public boolean isSystemFont() {
+ return mIsSystemDefault;
+ }
+
private static native long nativeCreateFromTypeface(long native_instance, int style);
private static native long nativeCreateFromTypefaceWithExactStyle(
long native_instance, int weight, boolean italic);