Chromium Code Reviews| Index: src/ports/SkFontMgr_android.cpp |
| diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp |
| index afa78734544f685bfba82be17062e89f6b2f543b..7532dfdb540570f43ad27c8304a507987751ca9b 100644 |
| --- a/src/ports/SkFontMgr_android.cpp |
| +++ b/src/ports/SkFontMgr_android.cpp |
| @@ -7,6 +7,7 @@ |
| #include "SkTypes.h" |
| +#include "SkData.h" |
| #include "SkFixed.h" |
| #include "SkFontDescriptor.h" |
| #include "SkFontHost_FreeType_common.h" |
| @@ -14,6 +15,7 @@ |
| #include "SkFontMgr_android.h" |
| #include "SkFontMgr_android_parser.h" |
| #include "SkFontStyle.h" |
| +#include "SkOSFile.h" |
| #include "SkPaint.h" |
| #include "SkRefCnt.h" |
| #include "SkString.h" |
| @@ -51,6 +53,7 @@ private: |
| class SkTypeface_AndroidSystem : public SkTypeface_Android { |
| public: |
| SkTypeface_AndroidSystem(const SkString& pathName, |
| + const bool cacheFontFiles, |
| int index, |
| const SkFixed* axes, int axesCount, |
| const SkFontStyle& style, |
| @@ -60,10 +63,29 @@ public: |
| FontVariant variantStyle) |
| : INHERITED(style, isFixedPitch, familyName) |
| , fPathName(pathName) |
| + , fCacheFontFiles(cacheFontFiles) |
| , fIndex(index) |
| , fAxes(axes, axesCount) |
| , fLang(lang) |
| - , fVariantStyle(variantStyle) { } |
| + , fVariantStyle(variantStyle) { |
| + if (fCacheFontFiles) { |
| + fFile = sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag); |
|
bungeman-skia
2016/02/08 22:11:56
This should just be handled in the init list with
Khushal
2016/02/09 00:53:40
I wanted to add an assert here. It might as well f
|
| + SkASSERT(fFile); |
| + } |
| + } |
| + |
| + ~SkTypeface_AndroidSystem() { |
| + if (fCacheFontFiles) |
| + sk_fclose(fFile); |
| + } |
| + |
| + SkStreamAsset* onOpenStream() const { |
|
bungeman-skia
2016/02/08 22:11:56
The 'on' part of the name indicates that this is a
Khushal
2016/02/09 00:53:40
Done.
|
| + if (fCacheFontFiles) { |
| + SkData* data = SkData::NewFromFILE(fFile); |
| + return data ? new SkMemoryStream(data) : nullptr; |
| + } |
| + return SkStream::NewFromFile(fPathName.c_str()); |
| + } |
| virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { |
| SkASSERT(desc); |
| @@ -73,18 +95,20 @@ public: |
| } |
| SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| *ttcIndex = fIndex; |
| - return SkStream::NewFromFile(fPathName.c_str()); |
| + return this->onOpenStream(); |
| } |
| SkFontData* onCreateFontData() const override { |
| - return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, |
| + return new SkFontData(this->onOpenStream(), fIndex, |
| fAxes.begin(), fAxes.count()); |
| } |
| const SkString fPathName; |
| + const bool fCacheFontFiles; |
|
bungeman-skia
2016/02/08 22:11:56
This seems redundant with fFile being nullptr or n
Khushal
2016/02/09 00:53:40
Removed.
|
| int fIndex; |
| const SkSTArray<4, SkFixed, true> fAxes; |
| const SkLanguage fLang; |
| const FontVariant fVariantStyle; |
| + FILE* fFile; |
|
bungeman-skia
2016/02/08 22:11:56
This should be SkAutoCallVProc<FILE, sk_fclose> so
Khushal
2016/02/09 00:53:40
Done.
|
| typedef SkTypeface_Android INHERITED; |
| }; |
| @@ -125,7 +149,7 @@ class SkFontStyleSet_Android : public SkFontStyleSet { |
| typedef SkTypeface_FreeType::Scanner Scanner; |
| public: |
| - explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner) { |
| + explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner, const bool cacheFontFiles) { |
|
bungeman-skia
2016/02/08 22:11:56
Skia has a 100 character limit (there are a few in
Khushal
2016/02/09 00:53:40
Done.
|
| const SkString* cannonicalFamilyName = nullptr; |
| if (family.fNames.count() > 0) { |
| cannonicalFamilyName = &family.fNames[0]; |
| @@ -226,7 +250,7 @@ public: |
| ) |
| fStyles.push_back().reset(new SkTypeface_AndroidSystem( |
| - pathName, ttcIndex, axisValues.get(), axisDefinitions.count(), style, |
| + pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDefinitions.count(), style, |
| isFixedWidth, familyName, lang, variant)); |
| } |
| } |
| @@ -305,7 +329,8 @@ struct NameToFamily { |
| class SkFontMgr_Android : public SkFontMgr { |
| public: |
| - SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| + SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) |
| + : fCacheFontFiles(custom ? custom->fCacheFontFiles : false) { |
|
bungeman-skia
2016/02/08 22:11:56
The '{' should go on the next line, four spaces in
Khushal
2016/02/09 00:53:40
Done.
|
| SkTDArray<FontFamily*> families; |
| if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) { |
| SkString base(custom->fBasePath); |
| @@ -520,7 +545,11 @@ protected: |
| // named typeface so that the system/app can provide their own recovery |
| // mechanism. On other platforms we'd provide a typeface from the |
| // default family instead. |
| - return this->onMatchFamilyStyle(familyName, style); |
| + // We only return the default family when the font manager is caching |
|
bungeman-skia
2016/02/08 22:11:56
Why this change? Does blink on Linux do bad things
Khushal
2016/02/09 00:53:39
It just crashes. Because like the comment above sa
|
| + // font families. |
| + SkTypeface* typeface = this->onMatchFamilyStyle(familyName, style); |
| + if (typeface || !fCacheFontFiles) |
| + return typeface; |
| } |
| return fDefaultFamily->matchStyle(style); |
| } |
| @@ -537,6 +566,8 @@ private: |
| SkTDArray<NameToFamily> fNameToFamilyMap; |
| SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
| + const bool fCacheFontFiles; |
| + |
| void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
| for (int i = 0; i < families.count(); i++) { |
| FontFamily& family = *families[i]; |
| @@ -551,7 +582,7 @@ private: |
| } |
| } |
| - SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner); |
| + SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner, fCacheFontFiles); |
| if (0 == newSet->count()) { |
| delete newSet; |
| continue; |