SkFontMgr_FontConfigInterface create typeface from FontParameters.

This implements
SkFontMgr_FontConfigInterface::onCreateFromStream(SkStreamAsset*, const FontParameters&)
and makes the changes needed to support it. This will allow Chromium to
create variation fonts from data.

BUG=skia:5697
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2296843002

Review-Url: https://codereview.chromium.org/2296843002
This commit is contained in:
bungeman 2016-09-01 08:52:29 -07:00 committed by Commit bot
parent d0cd2ecf31
commit 4bb029cd9f
2 changed files with 67 additions and 25 deletions

View File

@ -6,17 +6,18 @@
*/
#include "SkFontConfigInterface.h"
#include "SkFontDescriptor.h"
#include "SkFontHost_FreeType_common.h"
#include "SkRefCnt.h"
#include "SkStream.h"
#include "SkTypefaceCache.h"
class SkFontDescriptor;
class SkTypeface_FCI : public SkTypeface_FreeType {
SkAutoTUnref<SkFontConfigInterface> fFCI;
sk_sp<SkFontConfigInterface> fFCI;
SkFontConfigInterface::FontIdentity fIdentity;
SkString fFamilyName;
SkAutoTDelete<SkStreamAsset> fLocalStream;
std::unique_ptr<SkFontData> fFontData;
public:
static SkTypeface_FCI* Create(SkFontConfigInterface* fci,
@ -27,24 +28,16 @@ public:
return new SkTypeface_FCI(fci, fi, familyName, style);
}
static SkTypeface_FCI* Create(const SkFontStyle& style, bool fixedWidth,
SkStreamAsset* localStream, int index)
static SkTypeface_FCI* Create(std::unique_ptr<SkFontData> data,
SkFontStyle style, bool isFixedPitch)
{
return new SkTypeface_FCI(style, fixedWidth, localStream, index);
return new SkTypeface_FCI(std::move(data), style, isFixedPitch);
}
const SkFontConfigInterface::FontIdentity& getIdentity() const {
return fIdentity;
}
SkStreamAsset* getLocalStream() const {
return fLocalStream.get();
}
bool isFamilyName(const char* name) const {
return fFamilyName.equals(name);
}
protected:
SkTypeface_FCI(SkFontConfigInterface* fci,
const SkFontConfigInterface::FontIdentity& fi,
@ -54,18 +47,20 @@ protected:
, fFCI(SkRef(fci))
, fIdentity(fi)
, fFamilyName(familyName)
, fLocalStream(nullptr) {}
, fFontData(nullptr) {}
SkTypeface_FCI(const SkFontStyle& style, bool fixedWidth, SkStreamAsset* localStream, int index)
: INHERITED(style, fixedWidth)
, fLocalStream(localStream)
SkTypeface_FCI(std::unique_ptr<SkFontData> data, SkFontStyle style, bool isFixedPitch)
: INHERITED(style, isFixedPitch)
, fFontData(std::move(data))
{
fIdentity.fTTCIndex = index;
SkASSERT(fFontData);
fIdentity.fTTCIndex = fFontData->getIndex();
}
void onGetFamilyName(SkString* familyName) const override { *familyName = fFamilyName; }
void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
SkStreamAsset* onOpenStream(int* ttcIndex) const override;
SkFontData* onCreateFontData() const override;
private:
typedef SkTypeface_FreeType INHERITED;

View File

@ -19,20 +19,32 @@
SkStreamAsset* SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
*ttcIndex = this->getIdentity().fTTCIndex;
SkStreamAsset* stream = this->getLocalStream();
if (stream) {
if (fFontData) {
SkStreamAsset* stream = fFontData->getStream();
if (!stream) {
return nullptr;
}
return stream->duplicate();
}
return fFCI->openStream(this->getIdentity());
}
SkFontData* SkTypeface_FCI::onCreateFontData() const {
if (fFontData) {
return new SkFontData(*fFontData.get());
}
const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
return new SkFontData( fFCI->openStream(id), id.fTTCIndex, nullptr, 0);
}
void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const {
SkString name;
this->getFamilyName(&name);
desc->setFamilyName(name.c_str());
desc->setStyle(this->fontStyle());
*isLocalStream = SkToBool(this->getLocalStream());
*isLocalStream = SkToBool(fFontData);
}
///////////////////////////////////////////////////////////////////////////////
@ -198,12 +210,47 @@ protected:
// TODO should the caller give us the style or should we get it from freetype?
SkFontStyle style;
bool isFixedWidth = false;
if (!fScanner.scanFont(stream, 0, nullptr, &style, &isFixedWidth, nullptr)) {
bool isFixedPitch = false;
if (!fScanner.scanFont(stream, 0, nullptr, &style, &isFixedPitch, nullptr)) {
return nullptr;
}
return SkTypeface_FCI::Create(style, isFixedWidth, stream.release(), ttcIndex);
std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(), ttcIndex,
nullptr, 0));
return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch);
}
SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
using Scanner = SkTypeface_FreeType::Scanner;
SkAutoTDelete<SkStreamAsset> stream(s);
const size_t length = stream->getLength();
if (!length) {
return nullptr;
}
if (length >= 1024 * 1024 * 1024) {
return nullptr; // don't accept too large fonts (>= 1GB) for safety.
}
bool isFixedPitch;
SkFontStyle style;
SkString name;
Scanner::AxisDefinitions axisDefinitions;
if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
&axisDefinitions))
{
return nullptr;
}
int paramAxisCount;
const FontParameters::Axis* paramAxes = params.getAxes(&paramAxisCount);
SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(),
params.getCollectionIndex(),
axisValues.get(),
axisDefinitions.count()));
return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch);
}
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {