Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e62e215 authored by Kohsuke Yatoh's avatar Kohsuke Yatoh
Browse files

Consolidate getFamilyAt() calls to minikin.

- Typeface: Use FontCollection::createCollectionWithFamilies() instead
  of creating fallback list inside hwui.
- system_fonts: Use SystemFonts::getFontSet() intead of getFontMap().

Bug: 174672300
Test: atest CtsGraphicsTestCases:android.graphics.cts.TypefaceTest
Test: atest CtsGraphicsTestCases:android.graphics.fonts.NativeSystemFontTest
Change-Id: I940f8de441b7bd102eca2d08bc5dd4d0fc4a2685
parent 4d83b158
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -125,9 +125,14 @@ Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
}

Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
                                       int weight, int italic) {
                                       int weight, int italic, const Typeface* fallback) {
    Typeface* result = new Typeface;
    result->fFontCollection = minikin::FontCollection::create(families);
    if (fallback == nullptr) {
        result->fFontCollection = minikin::FontCollection::create(std::move(families));
    } else {
        result->fFontCollection =
                fallback->fFontCollection->createCollectionWithFamilies(std::move(families));
    }

    if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
        int weightFromFont;
+2 −1
Original line number Diff line number Diff line
@@ -78,7 +78,8 @@ public:
            Typeface* src, const std::vector<minikin::FontVariation>& variations);

    static Typeface* createFromFamilies(
            std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic);
            std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic,
            const Typeface* fallback);

    static void setDefault(const Typeface* face);

+6 −19
Original line number Diff line number Diff line
@@ -109,27 +109,14 @@ static jint Typeface_getWeight(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray,
                                      jlong fallbackPtr, int weight, int italic) {
    ScopedLongArrayRO families(env, familyArray);
    std::vector<std::shared_ptr<minikin::FontFamily>> familyVec;
    Typeface* typeface = (fallbackPtr == 0) ? nullptr : toTypeface(fallbackPtr);
    if (typeface != nullptr) {
        const std::shared_ptr<minikin::FontCollection>& fallbackCollection =
                toTypeface(fallbackPtr)->fFontCollection;
        familyVec.reserve(families.size() + fallbackCollection->getFamilyCount());
        for (size_t i = 0; i < families.size(); i++) {
            FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(families[i]);
            familyVec.emplace_back(family->family);
        }
        for (size_t i = 0; i < fallbackCollection->getFamilyCount(); i++) {
            familyVec.emplace_back(fallbackCollection->getFamilyAt(i));
        }
    } else {
    std::vector<std::shared_ptr<minikin::FontFamily>> familyVec;
    familyVec.reserve(families.size());
    for (size_t i = 0; i < families.size(); i++) {
        FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(families[i]);
        familyVec.emplace_back(family->family);
    }
    }
    return toJLong(Typeface::createFromFamilies(std::move(familyVec), weight, italic));
    return toJLong(Typeface::createFromFamilies(std::move(familyVec), weight, italic, typeface));
}

// CriticalNative
+43 −24
Original line number Diff line number Diff line
@@ -73,7 +73,8 @@ std::vector<std::shared_ptr<minikin::FontFamily>> makeSingleFamlyVector(const ch

TEST(TypefaceTest, resolveDefault_and_setDefaultTest) {
    std::unique_ptr<Typeface> regular(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
            makeSingleFamlyVector(kRobotoVariable), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE,
            nullptr /* fallback */));
    EXPECT_EQ(regular.get(), Typeface::resolveDefault(regular.get()));

    // Keep the original to restore it later.
@@ -351,24 +352,24 @@ TEST(TypefaceTest, createAbsolute) {
TEST(TypefaceTest, createFromFamilies_Single) {
    // In Java, new
    // Typeface.Builder("Roboto-Regular.ttf").setWeight(400).setItalic(false).build();
    std::unique_ptr<Typeface> regular(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRobotoVariable), 400, false));
    std::unique_ptr<Typeface> regular(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), 400, false, nullptr /* fallback */));
    EXPECT_EQ(400, regular->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, regular->fStyle.slant());
    EXPECT_EQ(Typeface::kNormal, regular->fAPIStyle);

    // In Java, new
    // Typeface.Builder("Roboto-Regular.ttf").setWeight(700).setItalic(false).build();
    std::unique_ptr<Typeface> bold(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRobotoVariable), 700, false));
    std::unique_ptr<Typeface> bold(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), 700, false, nullptr /* fallback */));
    EXPECT_EQ(700, bold->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, bold->fStyle.slant());
    EXPECT_EQ(Typeface::kBold, bold->fAPIStyle);

    // In Java, new
    // Typeface.Builder("Roboto-Regular.ttf").setWeight(400).setItalic(true).build();
    std::unique_ptr<Typeface> italic(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRobotoVariable), 400, true));
    std::unique_ptr<Typeface> italic(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), 400, true, nullptr /* fallback */));
    EXPECT_EQ(400, italic->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::ITALIC, italic->fStyle.slant());
    EXPECT_EQ(Typeface::kItalic, italic->fAPIStyle);
@@ -376,8 +377,8 @@ TEST(TypefaceTest, createFromFamilies_Single) {
    // In Java,
    // new
    // Typeface.Builder("Roboto-Regular.ttf").setWeight(700).setItalic(true).build();
    std::unique_ptr<Typeface> boldItalic(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRobotoVariable), 700, true));
    std::unique_ptr<Typeface> boldItalic(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), 700, true, nullptr /* fallback */));
    EXPECT_EQ(700, boldItalic->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::ITALIC, boldItalic->fStyle.slant());
    EXPECT_EQ(Typeface::kItalic, italic->fAPIStyle);
@@ -385,8 +386,8 @@ TEST(TypefaceTest, createFromFamilies_Single) {
    // In Java,
    // new
    // Typeface.Builder("Roboto-Regular.ttf").setWeight(1100).setItalic(false).build();
    std::unique_ptr<Typeface> over1000(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRobotoVariable), 1100, false));
    std::unique_ptr<Typeface> over1000(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRobotoVariable), 1100, false, nullptr /* fallback */));
    EXPECT_EQ(1000, over1000->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, over1000->fStyle.slant());
    EXPECT_EQ(Typeface::kBold, over1000->fAPIStyle);
@@ -394,30 +395,33 @@ TEST(TypefaceTest, createFromFamilies_Single) {

TEST(TypefaceTest, createFromFamilies_Single_resolveByTable) {
    // In Java, new Typeface.Builder("Family-Regular.ttf").build();
    std::unique_ptr<Typeface> regular(Typeface::createFromFamilies(
            makeSingleFamlyVector(kRegularFont), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> regular(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRegularFont), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    EXPECT_EQ(400, regular->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, regular->fStyle.slant());
    EXPECT_EQ(Typeface::kNormal, regular->fAPIStyle);

    // In Java, new Typeface.Builder("Family-Bold.ttf").build();
    std::unique_ptr<Typeface> bold(Typeface::createFromFamilies(
            makeSingleFamlyVector(kBoldFont), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> bold(
            Typeface::createFromFamilies(makeSingleFamlyVector(kBoldFont), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    EXPECT_EQ(700, bold->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, bold->fStyle.slant());
    EXPECT_EQ(Typeface::kBold, bold->fAPIStyle);

    // In Java, new Typeface.Builder("Family-Italic.ttf").build();
    std::unique_ptr<Typeface> italic(Typeface::createFromFamilies(
            makeSingleFamlyVector(kItalicFont), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> italic(
            Typeface::createFromFamilies(makeSingleFamlyVector(kItalicFont), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    EXPECT_EQ(400, italic->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::ITALIC, italic->fStyle.slant());
    EXPECT_EQ(Typeface::kItalic, italic->fAPIStyle);

    // In Java, new Typeface.Builder("Family-BoldItalic.ttf").build();
    std::unique_ptr<Typeface> boldItalic(
            Typeface::createFromFamilies(makeSingleFamlyVector(kBoldItalicFont),
                                         RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> boldItalic(Typeface::createFromFamilies(
            makeSingleFamlyVector(kBoldItalicFont), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE,
            nullptr /* fallback */));
    EXPECT_EQ(700, boldItalic->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::ITALIC, boldItalic->fStyle.slant());
    EXPECT_EQ(Typeface::kItalic, italic->fAPIStyle);
@@ -427,8 +431,9 @@ TEST(TypefaceTest, createFromFamilies_Family) {
    std::vector<std::shared_ptr<minikin::FontFamily>> families = {
            buildFamily(kRegularFont), buildFamily(kBoldFont), buildFamily(kItalicFont),
            buildFamily(kBoldItalicFont)};
    std::unique_ptr<Typeface> typeface(Typeface::createFromFamilies(
            std::move(families), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> typeface(
            Typeface::createFromFamilies(std::move(families), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    EXPECT_EQ(400, typeface->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, typeface->fStyle.slant());
}
@@ -436,10 +441,24 @@ TEST(TypefaceTest, createFromFamilies_Family) {
TEST(TypefaceTest, createFromFamilies_Family_withoutRegular) {
    std::vector<std::shared_ptr<minikin::FontFamily>> families = {
            buildFamily(kBoldFont), buildFamily(kItalicFont), buildFamily(kBoldItalicFont)};
    std::unique_ptr<Typeface> typeface(Typeface::createFromFamilies(
            std::move(families), RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE));
    std::unique_ptr<Typeface> typeface(
            Typeface::createFromFamilies(std::move(families), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    EXPECT_EQ(700, typeface->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, typeface->fStyle.slant());
}

TEST(TypefaceTest, createFromFamilies_Family_withFallback) {
    std::vector<std::shared_ptr<minikin::FontFamily>> fallbackFamilies = {
            buildFamily(kBoldFont), buildFamily(kItalicFont), buildFamily(kBoldItalicFont)};
    std::unique_ptr<Typeface> fallback(
            Typeface::createFromFamilies(std::move(fallbackFamilies), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, nullptr /* fallback */));
    std::unique_ptr<Typeface> regular(
            Typeface::createFromFamilies(makeSingleFamlyVector(kRegularFont), RESOLVE_BY_FONT_TABLE,
                                         RESOLVE_BY_FONT_TABLE, fallback.get()));
    EXPECT_EQ(400, regular->fStyle.weight());
    EXPECT_EQ(minikin::FontStyle::Slant::UPRIGHT, regular->fStyle.slant());
}

}  // namespace
+16 −25
Original line number Diff line number Diff line
@@ -245,14 +245,9 @@ ASystemFontIterator* ASystemFontIterator_open() {
    std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator());

    std::unordered_set<AFont, FontHasher> fonts;
    minikin::SystemFonts::getFontMap(
            [&fonts](const std::vector<std::shared_ptr<minikin::FontCollection>>& collections) {
                for (const auto& fc : collections) {
                    for (uint32_t i = 0; i < fc->getFamilyCount(); ++i) {
                        const auto& family = fc->getFamilyAt(i);
                        for (uint32_t j = 0; j < family->getNumFonts(); ++j) {
                            const minikin::Font* font = family->getFont(j);

    minikin::SystemFonts::getFontSet(
            [&fonts](const std::vector<std::shared_ptr<minikin::Font>>& fontSet) {
                for (const auto& font : fontSet) {
                    std::optional<std::string> locale;
                    uint32_t localeId = font->getLocaleListId();
                    if (localeId != minikin::kEmptyLocaleListId) {
@@ -263,14 +258,10 @@ ASystemFontIterator* ASystemFontIterator_open() {
                        axes.push_back(std::make_pair(tag, value));
                    }

                            fonts.insert(
                                    {font->typeface()->GetFontPath(), std::move(locale),
                    fonts.insert({font->typeface()->GetFontPath(), std::move(locale),
                                  font->style().weight(),
                                  font->style().slant() == minikin::FontStyle::Slant::ITALIC,
                                     static_cast<uint32_t>(font->typeface()->GetFontIndex()),
                                     axes});
                        }
                    }
                                  static_cast<uint32_t>(font->typeface()->GetFontIndex()), axes});
                }
            });