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

Commit 4893c557 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Refactor Typeface class for ensuring all members are initialized

Bug: 385136165
Test: TreeHugger
Flag: EXEMPT refactoring
Change-Id: I430d7086afa256d899d2201cc568f361d2177c15
parent 3c424e87
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ minikin::MinikinPaint MinikinUtils::prepareMinikinPaint(const Paint* paint,
    const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
    const SkFont& font = paint->getSkFont();

    minikin::MinikinPaint minikinPaint(resolvedFace->fFontCollection);
    minikin::MinikinPaint minikinPaint(resolvedFace->getFontCollection());
    /* Prepare minikin Paint */
    minikinPaint.size =
            font.isLinearMetrics() ? font.getSize() : static_cast<int>(font.getSize());
@@ -46,9 +46,9 @@ minikin::MinikinPaint MinikinUtils::prepareMinikinPaint(const Paint* paint,
    minikinPaint.wordSpacing = paint->getWordSpacing();
    minikinPaint.fontFlags = MinikinFontSkia::packFontFlags(font);
    minikinPaint.localeListId = paint->getMinikinLocaleListId();
    minikinPaint.fontStyle = resolvedFace->fStyle;
    minikinPaint.fontStyle = resolvedFace->getFontStyle();
    minikinPaint.fontFeatureSettings = paint->getFontFeatureSettings();
    if (!resolvedFace->fIsVariationInstance) {
    if (!resolvedFace->isVariationInstance()) {
        // This is an optimization for direct private API use typically done by System UI.
        // In the public API surface, if Typeface is already configured for variation instance
        // (Target SDK <= 35) the font variation settings of Paint is not set.
@@ -132,7 +132,7 @@ minikin::MinikinExtent MinikinUtils::getFontExtent(const Paint* paint, minikin::

bool MinikinUtils::hasVariationSelector(const Typeface* typeface, uint32_t codepoint, uint32_t vs) {
    const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
    return resolvedFace->fFontCollection->hasVariationSelector(codepoint, vs);
    return resolvedFace->getFontCollection()->hasVariationSelector(codepoint, vs);
}

float MinikinUtils::xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout) {
+24 −59
Original line number Diff line number Diff line
@@ -70,74 +70,45 @@ const Typeface* Typeface::resolveDefault(const Typeface* src) {

Typeface* Typeface::createRelative(Typeface* src, Typeface::Style style) {
    const Typeface* resolvedFace = Typeface::resolveDefault(src);
    Typeface* result = new Typeface;
    if (result != nullptr) {
        result->fFontCollection = resolvedFace->fFontCollection;
        result->fBaseWeight = resolvedFace->fBaseWeight;
        result->fAPIStyle = style;
        result->fStyle = computeRelativeStyle(result->fBaseWeight, style);
        result->fIsVariationInstance = resolvedFace->fIsVariationInstance;
    }
    return result;
    return new Typeface(resolvedFace->getFontCollection(),
                        computeRelativeStyle(resolvedFace->getBaseWeight(), style), style,
                        resolvedFace->getBaseWeight(), resolvedFace->isVariationInstance());
}

Typeface* Typeface::createAbsolute(Typeface* base, int weight, bool italic) {
    const Typeface* resolvedFace = Typeface::resolveDefault(base);
    Typeface* result = new Typeface();
    if (result != nullptr) {
        result->fFontCollection = resolvedFace->fFontCollection;
        result->fBaseWeight = resolvedFace->fBaseWeight;
        result->fAPIStyle = computeAPIStyle(weight, italic);
        result->fStyle = computeMinikinStyle(weight, italic);
        result->fIsVariationInstance = resolvedFace->fIsVariationInstance;
    }
    return result;
    return new Typeface(resolvedFace->getFontCollection(), computeMinikinStyle(weight, italic),
                        computeAPIStyle(weight, italic), resolvedFace->getBaseWeight(),
                        resolvedFace->isVariationInstance());
}

Typeface* Typeface::createFromTypefaceWithVariation(Typeface* src,
                                                    const minikin::VariationSettings& variations) {
    const Typeface* resolvedFace = Typeface::resolveDefault(src);
    Typeface* result = new Typeface();
    if (result != nullptr) {
        result->fFontCollection =
                resolvedFace->fFontCollection->createCollectionWithVariation(variations);
        if (result->fFontCollection == nullptr) {
    const std::shared_ptr<minikin::FontCollection>& fc =
            resolvedFace->getFontCollection()->createCollectionWithVariation(variations);
    return new Typeface(
            // None of passed axes are supported by this collection.
            // So we will reuse the same collection with incrementing reference count.
            result->fFontCollection = resolvedFace->fFontCollection;
        }
            fc ? fc : resolvedFace->getFontCollection(),
            // Do not update styles.
            // TODO: We may want to update base weight if the 'wght' is specified.
        result->fBaseWeight = resolvedFace->fBaseWeight;
        result->fAPIStyle = resolvedFace->fAPIStyle;
        result->fStyle = resolvedFace->fStyle;
        result->fIsVariationInstance = true;
    }
    return result;
            resolvedFace->fStyle, resolvedFace->getAPIStyle(), resolvedFace->getBaseWeight(), true);
}

Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
    const Typeface* resolvedFace = Typeface::resolveDefault(src);
    Typeface* result = new Typeface;
    if (result != nullptr) {
        result->fFontCollection = resolvedFace->fFontCollection;
        result->fBaseWeight = weight;
        result->fAPIStyle = resolvedFace->fAPIStyle;
        result->fStyle = computeRelativeStyle(weight, result->fAPIStyle);
        result->fIsVariationInstance = resolvedFace->fIsVariationInstance;
    }
    return result;
    return new Typeface(resolvedFace->getFontCollection(),
                        computeRelativeStyle(weight, resolvedFace->getAPIStyle()),
                        resolvedFace->getAPIStyle(), weight, resolvedFace->isVariationInstance());
}

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

    if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
        int weightFromFont;
@@ -171,11 +142,8 @@ Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::Font
        weight = SkFontStyle::kNormal_Weight;
    }

    result->fBaseWeight = weight;
    result->fAPIStyle = computeAPIStyle(weight, italic);
    result->fStyle = computeMinikinStyle(weight, italic);
    result->fIsVariationInstance = false;
    return result;
    return new Typeface(fc, computeMinikinStyle(weight, italic), computeAPIStyle(weight, italic),
                        weight, false);
}

void Typeface::setDefault(const Typeface* face) {
@@ -205,11 +173,8 @@ void Typeface::setRobotoTypefaceForTest() {
    std::shared_ptr<minikin::FontCollection> collection =
            minikin::FontCollection::create(minikin::FontFamily::create(std::move(fonts)));

    Typeface* hwTypeface = new Typeface();
    hwTypeface->fFontCollection = collection;
    hwTypeface->fAPIStyle = Typeface::kNormal;
    hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
    hwTypeface->fStyle = minikin::FontStyle();
    Typeface* hwTypeface = new Typeface(collection, minikin::FontStyle(), Typeface::kNormal,
                                        SkFontStyle::kNormal_Weight, false);

    Typeface::setDefault(hwTypeface);
#endif
+24 −6
Original line number Diff line number Diff line
@@ -32,21 +32,39 @@ constexpr int RESOLVE_BY_FONT_TABLE = -1;

struct ANDROID_API Typeface {
public:
    std::shared_ptr<minikin::FontCollection> fFontCollection;
    enum Style : uint8_t { kNormal = 0, kBold = 0x01, kItalic = 0x02, kBoldItalic = 0x03 };
    Typeface(const std::shared_ptr<minikin::FontCollection> fc, minikin::FontStyle style,
             Style apiStyle, int baseWeight, bool isVariationInstance)
            : fFontCollection(fc)
            , fStyle(style)
            , fAPIStyle(apiStyle)
            , fBaseWeight(baseWeight)
            , fIsVariationInstance(isVariationInstance) {}

    const std::shared_ptr<minikin::FontCollection>& getFontCollection() const {
        return fFontCollection;
    }

    // resolved style actually used for rendering
    minikin::FontStyle fStyle;
    minikin::FontStyle getFontStyle() const { return fStyle; }

    // style used in the API
    enum Style : uint8_t { kNormal = 0, kBold = 0x01, kItalic = 0x02, kBoldItalic = 0x03 };
    Style fAPIStyle;
    Style getAPIStyle() const { return fAPIStyle; }

    // base weight in CSS-style units, 1..1000
    int fBaseWeight;
    int getBaseWeight() const { return fBaseWeight; }

    // True if the Typeface is already created for variation settings.
    bool fIsVariationInstance;
    bool isVariationInstance() const { return fIsVariationInstance; }

private:
    std::shared_ptr<minikin::FontCollection> fFontCollection;
    minikin::FontStyle fStyle;
    Style fAPIStyle;
    int fBaseWeight;
    bool fIsVariationInstance = false;

public:
    static const Typeface* resolveDefault(const Typeface* src);

    // The following three functions create new Typeface from an existing Typeface with a different
+3 −2
Original line number Diff line number Diff line
@@ -609,7 +609,8 @@ namespace PaintGlue {
        SkFont* font = &paint->getSkFont();
        const Typeface* typeface = paint->getAndroidTypeface();
        typeface = Typeface::resolveDefault(typeface);
        minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
        minikin::FakedFont baseFont =
                typeface->getFontCollection()->baseFontFaked(typeface->getFontStyle());
        float saveSkewX = font->getSkewX();
        bool savefakeBold = font->isEmbolden();
        MinikinFontSkia::populateSkFont(font, baseFont.typeface().get(), baseFont.fakery);
@@ -641,7 +642,7 @@ namespace PaintGlue {
        if (useLocale) {
            minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface);
            minikin::MinikinExtent extent =
                    typeface->fFontCollection->getReferenceExtentForLocale(minikinPaint);
                    typeface->getFontCollection()->getReferenceExtentForLocale(minikinPaint);
            metrics->fAscent = std::min(extent.ascent, metrics->fAscent);
            metrics->fDescent = std::max(extent.descent, metrics->fDescent);
            metrics->fTop = std::min(metrics->fAscent, metrics->fTop);
+20 −20
Original line number Diff line number Diff line
@@ -99,17 +99,17 @@ static jlong Typeface_getReleaseFunc(CRITICAL_JNI_PARAMS) {

// CriticalNative
static jint Typeface_getStyle(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    return toTypeface(faceHandle)->fAPIStyle;
    return toTypeface(faceHandle)->getAPIStyle();
}

// CriticalNative
static jint Typeface_getWeight(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    return toTypeface(faceHandle)->fStyle.weight();
    return toTypeface(faceHandle)->getFontStyle().weight();
}

// Critical Native
static jboolean Typeface_isVariationInstance(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    return toTypeface(faceHandle)->fIsVariationInstance;
    return toTypeface(faceHandle)->isVariationInstance();
}

static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray,
@@ -128,18 +128,18 @@ static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArr
// CriticalNative
static void Typeface_setDefault(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    Typeface::setDefault(toTypeface(faceHandle));
    minikin::SystemFonts::registerDefault(toTypeface(faceHandle)->fFontCollection);
    minikin::SystemFonts::registerDefault(toTypeface(faceHandle)->getFontCollection());
}

static jobject Typeface_getSupportedAxes(JNIEnv *env, jobject, jlong faceHandle) {
    Typeface* face = toTypeface(faceHandle);
    const size_t length = face->fFontCollection->getSupportedAxesCount();
    const size_t length = face->getFontCollection()->getSupportedAxesCount();
    if (length == 0) {
        return nullptr;
    }
    std::vector<jint> tagVec(length);
    for (size_t i = 0; i < length; i++) {
        tagVec[i] = face->fFontCollection->getSupportedAxisAt(i);
        tagVec[i] = face->getFontCollection()->getSupportedAxisAt(i);
    }
    std::sort(tagVec.begin(), tagVec.end());
    const jintArray result = env->NewIntArray(length);
@@ -150,7 +150,7 @@ static jobject Typeface_getSupportedAxes(JNIEnv *env, jobject, jlong faceHandle)
static void Typeface_registerGenericFamily(JNIEnv *env, jobject, jstring familyName, jlong ptr) {
    ScopedUtfChars familyNameChars(env, familyName);
    minikin::SystemFonts::registerFallback(familyNameChars.c_str(),
                                           toTypeface(ptr)->fFontCollection);
                                           toTypeface(ptr)->getFontCollection());
}

#ifdef __ANDROID__
@@ -315,18 +315,19 @@ static jint Typeface_writeTypefaces(JNIEnv* env, jobject, jobject buffer, jint p
    std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections;
    std::unordered_map<std::shared_ptr<minikin::FontCollection>, size_t> fcToIndex;
    for (Typeface* typeface : typefaces) {
        bool inserted = fcToIndex.emplace(typeface->fFontCollection, fontCollections.size()).second;
        bool inserted =
                fcToIndex.emplace(typeface->getFontCollection(), fontCollections.size()).second;
        if (inserted) {
            fontCollections.push_back(typeface->fFontCollection);
            fontCollections.push_back(typeface->getFontCollection());
        }
    }
    minikin::FontCollection::writeVector(&writer, fontCollections);
    writer.write<uint32_t>(typefaces.size());
    for (Typeface* typeface : typefaces) {
      writer.write<uint32_t>(fcToIndex.find(typeface->fFontCollection)->second);
      typeface->fStyle.writeTo(&writer);
      writer.write<Typeface::Style>(typeface->fAPIStyle);
      writer.write<int>(typeface->fBaseWeight);
        writer.write<uint32_t>(fcToIndex.find(typeface->getFontCollection())->second);
        typeface->getFontStyle().writeTo(&writer);
        writer.write<Typeface::Style>(typeface->getAPIStyle());
        writer.write<int>(typeface->getBaseWeight());
    }
    return static_cast<jint>(writer.size());
}
@@ -349,12 +350,10 @@ static jlongArray Typeface_readTypefaces(JNIEnv* env, jobject, jobject buffer, j
    std::vector<jlong> faceHandles;
    faceHandles.reserve(typefaceCount);
    for (uint32_t i = 0; i < typefaceCount; i++) {
        Typeface* typeface = new Typeface;
        typeface->fFontCollection = fontCollections[reader.read<uint32_t>()];
        typeface->fStyle = minikin::FontStyle(&reader);
        typeface->fAPIStyle = reader.read<Typeface::Style>();
        typeface->fBaseWeight = reader.read<int>();
        typeface->fIsVariationInstance = false;
        Typeface* typeface =
                new Typeface(fontCollections[reader.read<uint32_t>()], minikin::FontStyle(&reader),
                             reader.read<Typeface::Style>(), reader.read<int>(),
                             false /* isVariationInstance */);
        faceHandles.push_back(toJLong(typeface));
    }
    const jlongArray result = env->NewLongArray(typefaceCount);
@@ -382,7 +381,8 @@ static void Typeface_warmUpCache(JNIEnv* env, jobject, jstring jFilePath) {

// Critical Native
static void Typeface_addFontCollection(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    std::shared_ptr<minikin::FontCollection> collection = toTypeface(faceHandle)->fFontCollection;
    std::shared_ptr<minikin::FontCollection> collection =
            toTypeface(faceHandle)->getFontCollection();
    minikin::SystemFonts::addFontMap(std::move(collection));
}

Loading