2013-09-19 12:08:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-04-14 15:04:45 +00:00
|
|
|
#include "SkRefCnt.h"
|
2013-09-19 12:08:40 +00:00
|
|
|
#include "SkTypeface.h"
|
2016-04-14 15:04:45 +00:00
|
|
|
#include "SkTypefaceCache.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2013-09-19 12:08:40 +00:00
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Typeface, reporter) {
|
2013-09-19 12:08:40 +00:00
|
|
|
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkFontStyle()));
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal));
|
2013-09-19 12:08:40 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get()));
|
|
|
|
REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get()));
|
|
|
|
REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get()));
|
|
|
|
REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0));
|
|
|
|
REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0));
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> t3(SkTypeface::MakeFromName("non-existent-font", SkFontStyle()));
|
2016-05-12 17:09:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == t3);
|
2013-09-19 12:08:40 +00:00
|
|
|
#endif
|
|
|
|
}
|
2016-04-14 15:04:45 +00:00
|
|
|
|
2016-04-14 16:44:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-04-14 15:04:45 +00:00
|
|
|
class SkEmptyTypeface : public SkTypeface {
|
|
|
|
public:
|
2016-07-13 12:16:58 +00:00
|
|
|
static sk_sp<SkTypeface> Create() { return sk_sp<SkTypeface>(new SkEmptyTypeface()); }
|
2016-04-14 15:04:45 +00:00
|
|
|
protected:
|
2016-07-13 12:16:58 +00:00
|
|
|
SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { }
|
2016-04-14 15:04:45 +00:00
|
|
|
|
|
|
|
SkStreamAsset* onOpenStream(int* ttcIndex) const override { return nullptr; }
|
|
|
|
SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
|
|
|
|
const SkDescriptor*) const override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
void onFilterRec(SkScalerContextRec*) const override { }
|
|
|
|
virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
|
|
|
|
PerGlyphInfo,
|
|
|
|
const uint32_t*, uint32_t) const override { return nullptr; }
|
|
|
|
void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { }
|
|
|
|
virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
|
|
|
|
uint16_t glyphs[], int glyphCount) const override {
|
|
|
|
SK_ABORT("unimplemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int onCountGlyphs() const override { return 0; };
|
|
|
|
int onGetUPEM() const override { return 0; };
|
|
|
|
void onGetFamilyName(SkString* familyName) const override { familyName->reset(); }
|
|
|
|
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
|
|
|
|
SK_ABORT("unimplemented");
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
|
|
|
|
size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override { return 0; }
|
|
|
|
};
|
|
|
|
|
2016-04-14 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:04:45 +00:00
|
|
|
static bool count_proc(SkTypeface* face, void* ctx) {
|
|
|
|
int* count = static_cast<int*>(ctx);
|
|
|
|
*count = *count + 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
static int count(skiatest::Reporter* reporter, const SkTypefaceCache& cache) {
|
|
|
|
int count = 0;
|
|
|
|
SkTypeface* none = cache.findByProcAndRef(count_proc, &count);
|
|
|
|
REPORTER_ASSERT(reporter, none == nullptr);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(TypefaceCache, reporter) {
|
2016-07-13 12:16:58 +00:00
|
|
|
sk_sp<SkTypeface> t1(SkEmptyTypeface::Create());
|
2016-04-14 15:04:45 +00:00
|
|
|
{
|
|
|
|
SkTypefaceCache cache;
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 0);
|
|
|
|
{
|
2016-07-13 12:16:58 +00:00
|
|
|
sk_sp<SkTypeface> t0(SkEmptyTypeface::Create());
|
2016-04-14 15:04:45 +00:00
|
|
|
cache.add(t0.get());
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 1);
|
|
|
|
cache.add(t1.get());
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
|
|
|
|
cache.purgeAll();
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
|
|
|
|
cache.purgeAll();
|
|
|
|
REPORTER_ASSERT(reporter, count(reporter, cache) == 1);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, t1->unique());
|
|
|
|
}
|