From 88930f1df469793f3f3d977396762fb217d34583 Mon Sep 17 00:00:00 2001
From: Seigo Nonaka
Date: Tue, 24 Jul 2018 17:09:23 -0700
Subject: Introduce FontFamily and its builder
This CL is a ground work of the new Typeface construction API and
nobody uses this class except for CTS now.
I'll add new builder in Typeface to be able to create Typeface
from this FontFamily.
Bug: 72665240
Test: atest FontFamilyTest
Test: CtsWidgetTestCases:EditTextTest
CtsWidgetTestCases:TextViewFadingEdgeTest
FrameworksCoreTests:TextViewFallbackLineSpacingTest
FrameworksCoreTests:TextViewTest FrameworksCoreTests:TypefaceTest
CtsGraphicsTestCases:TypefaceTest CtsWidgetTestCases:TextViewTest
CtsTextTestCases FrameworksCoreTests:android.text
CtsWidgetTestCases:TextViewPrecomputedTextTest
Change-Id: I15d412c367037554d911fc9e20c0cfb44aefb89a
---
graphics/java/android/graphics/fonts/Font.java | 5 +
.../java/android/graphics/fonts/FontFamily.java | 164 +++++++++++++++++++++
2 files changed, 169 insertions(+)
create mode 100644 graphics/java/android/graphics/fonts/FontFamily.java
(limited to 'graphics/java')
diff --git a/graphics/java/android/graphics/fonts/Font.java b/graphics/java/android/graphics/fonts/Font.java
index 9da61db94780..9d94a641f795 100644
--- a/graphics/java/android/graphics/fonts/Font.java
+++ b/graphics/java/android/graphics/fonts/Font.java
@@ -469,4 +469,9 @@ public class Font {
public long getNativePtr() {
return mNativePtr;
}
+
+ @Override
+ public String toString() {
+ return "Font {weight=" + mWeight + ", italic=" + mItalic + "}";
+ }
}
diff --git a/graphics/java/android/graphics/fonts/FontFamily.java b/graphics/java/android/graphics/fonts/FontFamily.java
new file mode 100644
index 000000000000..74b58ea76b2f
--- /dev/null
+++ b/graphics/java/android/graphics/fonts/FontFamily.java
@@ -0,0 +1,164 @@
+/*
+ * 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.IntRange;
+import android.annotation.NonNull;
+
+import com.android.internal.util.Preconditions;
+
+import dalvik.annotation.optimization.CriticalNative;
+
+import libcore.util.NativeAllocationRegistry;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+
+/**
+ * A font family class can be used for creating Typeface.
+ *
+ *
+ * A font family is a bundle of fonts for drawing text in various styles.
+ * For example, you can bundle regular style font and bold style font into a single font family,
+ * then system will select the correct style font from family for drawing.
+ *
+ *
+ * FontFamily family = new FontFamily.Builder(new Font.Builder("regular.ttf").build())
+ * .addFont(new Font.Builder("bold.ttf").build()).build();
+ * Typeface typeface = new Typeface.Builder2(family).build();
+ *
+ * SpannableStringBuilder ssb = new SpannableStringBuilder("Hello, World.");
+ * ssb.setSpan(new StyleSpan(Typeface.Bold), 6, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+ *
+ * textView.setTypeface(typeface);
+ * textView.setText(ssb);
+ *
+ *
+ * In this example, "Hello, " is drawn with "regular.ttf", and "World." is drawn with "bold.ttf".
+ *
+ * If there is no font exactly matches with the text style, the system will select the closest font.
+ *
+ *
+ */
+public class FontFamily {
+ private static final String TAG = "FontFamily";
+
+ /**
+ * A builder class for creating new FontFamily.
+ */
+ public static class Builder {
+ private static final NativeAllocationRegistry sFamilyRegistory =
+ new NativeAllocationRegistry(FontFamily.class.getClassLoader(),
+ nGetReleaseNativeFamily(), 64);
+
+ private final ArrayList mFonts = new ArrayList<>();
+ private final HashSet mStyleHashSet = new HashSet<>();
+
+ /**
+ * Constructs a builder.
+ *
+ * @param font a font
+ */
+ public Builder(@NonNull Font font) {
+ Preconditions.checkNotNull(font, "font can not be null");
+ mStyleHashSet.add(makeStyleIdentifier(font));
+ mFonts.add(font);
+ }
+
+ /**
+ * Adds different style font to the builder.
+ *
+ * System will select the font if the text style is closest to the font.
+ * If the same style font is already added to the builder, this method will fail with
+ * {@link IllegalArgumentException}.
+ *
+ * Note that system assumes all fonts bundled in FontFamily have the same coverage for the
+ * code points. For example, regular style font and bold style font must have the same code
+ * point coverage, otherwise some character may be shown as tofu.
+ *
+ * @param font a font
+ * @return this builder
+ */
+ public @NonNull Builder addFont(@NonNull Font font) {
+ Preconditions.checkNotNull(font, "font can not be null");
+ if (!mStyleHashSet.add(makeStyleIdentifier(font))) {
+ throw new IllegalArgumentException(font + " has already been added");
+ }
+ mFonts.add(font);
+ return this;
+ }
+
+ /**
+ * Build the font family
+ * @return a font family
+ */
+ public @NonNull FontFamily build() {
+ final long builderPtr = nInitBuilder();
+ for (int i = 0; i < mFonts.size(); ++i) {
+ nAddFont(builderPtr, mFonts.get(i).getNativePtr());
+ }
+ final long ptr = nBuild(builderPtr);
+ final FontFamily family = new FontFamily(mFonts, ptr);
+ sFamilyRegistory.registerNativeAllocation(family, ptr);
+ return family;
+ }
+
+ private static int makeStyleIdentifier(@NonNull Font font) {
+ return font.getWeight() | (font.isItalic() ? (1 << 16) : 0);
+ }
+
+ private static native long nInitBuilder();
+ @CriticalNative
+ private static native void nAddFont(long builderPtr, long fontPtr);
+ private static native long nBuild(long builderPtr);
+ @CriticalNative
+ private static native long nGetReleaseNativeFamily();
+ }
+
+ private final ArrayList mFonts;
+ private final long mNativePtr;
+
+ // Use Builder instead.
+ private FontFamily(@NonNull ArrayList fonts, long ptr) {
+ mFonts = fonts;
+ mNativePtr = ptr;
+ }
+
+ /**
+ * Returns a font
+ *
+ * @param index an index of the font
+ * @return a registered font
+ */
+ public Font getFont(@IntRange(from = 0) int index) {
+ return mFonts.get(index);
+ }
+
+ /**
+ * Returns the number of fonts in this FontFamily.
+ *
+ * @return the number of fonts registered in this family.
+ */
+ public int getFontCount() {
+ return mFonts.size();
+ }
+
+ /** @hide */
+ public long getNativePtr() {
+ return mNativePtr;
+ }
+}
--
cgit v1.2.3-59-g8ed1b