Add SkTypeface::openExistingStream.

Allow users to retrieve the stream from an SkTypeface, but only if that stream is inexpensive to create.

Bug: https://github.com/flutter/flutter/issues/97384

Change-Id: I9bab1a775273363500cf7482f1babeee75c5a3df
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/503349
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Derek Sollenberger <djsollen@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
This commit is contained in:
Julia Lavrova 2022-02-02 15:36:01 -05:00 committed by SkCQ
parent dcafc5d2bc
commit 14f559fd23
5 changed files with 31 additions and 1 deletions

View File

@ -319,6 +319,15 @@ public:
*/
std::unique_ptr<SkStreamAsset> openStream(int* ttcIndex) const;
/**
* Return a stream for the contents of the font data.
* Returns nullptr on failure or if the font data isn't already available in stream form.
* Use when the stream can be used opportunistically but the calling code would prefer
* to fall back to table access if creating the stream would be expensive.
* Otherwise acts the same as openStream.
*/
std::unique_ptr<SkStreamAsset> openExistingStream(int* ttcIndex) const;
/**
* Return a scalercontext for the given descriptor. It may return a
* stub scalercontext that will not crash, but will draw nothing.
@ -377,6 +386,8 @@ protected:
virtual std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const = 0;
virtual std::unique_ptr<SkStreamAsset> onOpenExistingStream(int* ttcIndex) const;
virtual bool onGlyphMaskNeedsCurrentColor() const = 0;
virtual int onGetVariationDesignPosition(

View File

@ -265,7 +265,7 @@ SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;)
HBFace create_hb_face(const SkTypeface& typeface) {
int index = 0;
std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openStream(&index);
std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openExistingStream(&index);
HBFace face;
if (typefaceAsset && typefaceAsset->getMemoryBase()) {
HBBlob blob(stream_to_blob(std::move(typefaceAsset)));

View File

@ -290,6 +290,15 @@ std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
return this->onOpenStream(ttcIndex);
}
std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const {
int ttcIndexStorage;
if (nullptr == ttcIndex) {
// So our subclasses don't need to check for null param
ttcIndex = &ttcIndexStorage;
}
return this->onOpenExistingStream(ttcIndex);
}
std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
const SkScalerContextEffects& effects, const SkDescriptor* desc) const {
std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc);
@ -438,6 +447,10 @@ bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
return false;
}
std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const {
return this->onOpenStream(ttcIndex);
}
///////////////////////////////////////////////////////////////////////////////
#include "include/core/SkPaint.h"

View File

@ -775,6 +775,11 @@ std::unique_ptr<SkStreamAsset> SkTypeface_Mac::onOpenStream(int* ttcIndex) const
return fStream->duplicate();
}
std::unique_ptr<SkStreamAsset> SkTypeface_Mac::onOpenExistingStream(int* ttcIndex) const {
*ttcIndex = 0;
return fStream ? fStream->duplicate() : nullptr;
}
int SkTypeface_Mac::onGetVariationDesignPosition(
SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
{

View File

@ -94,6 +94,7 @@ public:
protected:
int onGetUPEM() const override;
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
std::unique_ptr<SkStreamAsset> onOpenExistingStream(int* ttcIndex) const override;
bool onGlyphMaskNeedsCurrentColor() const override { return false; }
int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
int coordinateCount) const override;