2013-03-28 13:39:35 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkFontMgr.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2013-08-21 15:20:43 +00:00
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
extern SkFontMgr* SkFontMgr_New_GDI();
|
|
|
|
extern SkFontMgr* SkFontMgr_New_DirectWrite();
|
|
|
|
#endif
|
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
// limit this just so we don't take too long to draw
|
|
|
|
#define MAX_FAMILIES 30
|
|
|
|
|
|
|
|
static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
|
|
|
|
SkScalar y, const SkPaint& paint) {
|
|
|
|
canvas->drawText(text.c_str(), text.size(), x, y, paint);
|
|
|
|
return x + paint.measureText(text.c_str(), text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
class FontMgrGM : public skiagm::GM {
|
|
|
|
public:
|
2013-08-21 15:20:43 +00:00
|
|
|
FontMgrGM(SkFontMgr* (*factory)() = NULL) {
|
2013-03-28 13:39:35 +00:00
|
|
|
SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
|
2013-08-21 15:20:43 +00:00
|
|
|
|
|
|
|
fName.set("fontmgr_iter");
|
|
|
|
if (factory) {
|
|
|
|
fName.append("_factory");
|
|
|
|
fFM.reset(factory());
|
|
|
|
} else {
|
|
|
|
fFM.reset(SkFontMgr::RefDefault());
|
|
|
|
}
|
2013-03-28 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
2013-08-21 15:20:43 +00:00
|
|
|
return fName;
|
2013-03-28 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return SkISize::Make(640, 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkScalar y = 20;
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(17);
|
2013-03-29 07:01:22 +00:00
|
|
|
|
2013-08-21 15:20:43 +00:00
|
|
|
SkFontMgr* fm = fFM;
|
2013-03-28 13:39:35 +00:00
|
|
|
int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkString fname;
|
|
|
|
fm->getFamilyName(i, &fname);
|
|
|
|
paint.setTypeface(NULL);
|
|
|
|
(void)drawString(canvas, fname, 20, y, paint);
|
2013-03-29 07:01:22 +00:00
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
SkScalar x = 220;
|
2013-03-29 14:57:22 +00:00
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
|
|
|
|
for (int j = 0; j < set->count(); ++j) {
|
|
|
|
SkString sname;
|
|
|
|
SkFontStyle fs;
|
|
|
|
set->getStyle(j, &fs, &sname);
|
2013-04-24 19:14:39 +00:00
|
|
|
sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
|
2013-03-29 07:01:22 +00:00
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
|
|
|
|
x = drawString(canvas, sname, x, y, paint) + 20;
|
2013-03-29 07:01:22 +00:00
|
|
|
}
|
2013-03-28 13:39:35 +00:00
|
|
|
y += 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:03:22 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
// fontdescriptors (and therefore serialization) don't yet understand
|
|
|
|
// these new styles, so skip tests that exercise that for now.
|
2013-05-01 14:21:20 +00:00
|
|
|
|
|
|
|
// If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
|
|
|
|
// the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
|
|
|
|
|
|
|
|
return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
|
2013-03-28 15:03:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
private:
|
2013-08-21 15:20:43 +00:00
|
|
|
SkAutoTUnref<SkFontMgr> fFM;
|
|
|
|
SkString fName;
|
2013-03-28 13:39:35 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
class FontMgrMatchGM : public skiagm::GM {
|
|
|
|
SkAutoTUnref<SkFontMgr> fFM;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
|
|
|
|
SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("fontmgr_match");
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
virtual SkISize onISize() {
|
|
|
|
return SkISize::Make(640, 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
SkFontStyleSet* fset) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
SkScalar y = 0;
|
|
|
|
|
|
|
|
for (int j = 0; j < fset->count(); ++j) {
|
|
|
|
SkString sname;
|
|
|
|
SkFontStyle fs;
|
|
|
|
fset->getStyle(j, &fs, &sname);
|
|
|
|
|
|
|
|
sname.appendf(" [%d %d]", fs.weight(), fs.width());
|
|
|
|
|
|
|
|
SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
|
|
|
|
(void)drawString(canvas, sname, 0, y, p);
|
|
|
|
y += 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
SkFontStyleSet* fset) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
SkScalar y = 0;
|
|
|
|
|
|
|
|
for (int weight = 100; weight <= 900; weight += 200) {
|
|
|
|
for (int width = 1; width <= 9; width += 2) {
|
|
|
|
SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
|
|
|
|
SkTypeface* face = fset->matchStyle(fs);
|
|
|
|
if (face) {
|
|
|
|
SkString str;
|
|
|
|
str.printf("request [%d %d]", fs.weight(), fs.width());
|
|
|
|
p.setTypeface(face)->unref();
|
|
|
|
(void)drawString(canvas, str, 0, y, p);
|
|
|
|
y += 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(17);
|
|
|
|
|
|
|
|
static const char* gNames[] = {
|
|
|
|
"Helvetica Neue", "Arial"
|
|
|
|
};
|
|
|
|
|
|
|
|
SkFontStyleSet* fset = NULL;
|
2013-03-30 07:01:27 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
|
2013-03-29 14:57:22 +00:00
|
|
|
fset = fFM->matchFamily(gNames[i]);
|
|
|
|
if (fset && fset->count() > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
if (NULL == fset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkAutoUnref aur(fset);
|
|
|
|
|
|
|
|
canvas->translate(20, 40);
|
|
|
|
this->exploreFamily(canvas, paint, fset);
|
|
|
|
canvas->translate(150, 0);
|
|
|
|
this->iterateFamily(canvas, paint, fset);
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
// fontdescriptors (and therefore serialization) don't yet understand
|
|
|
|
// these new styles, so skip tests that exercise that for now.
|
|
|
|
return kSkipPicture_Flag | kSkipPipe_Flag;
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return SkNEW(FontMgrGM); )
|
2013-03-29 14:57:22 +00:00
|
|
|
DEF_GM( return SkNEW(FontMgrMatchGM); )
|
2013-08-21 15:20:43 +00:00
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite)); )
|
|
|
|
#endif
|