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"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-03-28 13:39:35 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkFontMgr.h"
|
2018-02-15 15:20:04 +00:00
|
|
|
#include "SkPath.h"
|
2013-03-28 13:39:35 +00:00
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
// 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) {
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(text, x, y, paint);
|
2013-03-28 13:39:35 +00:00
|
|
|
return x + paint.measureText(text.c_str(), text.size());
|
|
|
|
}
|
|
|
|
|
2014-08-27 14:03:13 +00:00
|
|
|
static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
|
|
|
|
SkScalar y, SkPaint& paint, SkFontMgr* fm,
|
2014-11-10 21:29:33 +00:00
|
|
|
const char* fontName, const char* bcp47[], int bcp47Count,
|
2014-08-27 14:03:13 +00:00
|
|
|
const SkFontStyle& fontStyle) {
|
|
|
|
// find typeface containing the requested character and draw it
|
|
|
|
SkString ch;
|
|
|
|
ch.appendUnichar(character);
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(fontName, fontStyle,
|
|
|
|
bcp47, bcp47Count, character));
|
|
|
|
paint.setTypeface(typeface);
|
2014-08-27 14:03:13 +00:00
|
|
|
x = drawString(canvas, ch, x, y, paint) + 20;
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == typeface) {
|
2014-08-27 14:03:13 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// repeat the process, but this time use the family name of the typeface
|
|
|
|
// from the first pass. This emulates the behavior in Blink where it
|
|
|
|
// it expects to get the same glyph when following this pattern.
|
|
|
|
SkString familyName;
|
|
|
|
typeface->getFamilyName(&familyName);
|
2017-09-26 13:46:08 +00:00
|
|
|
paint.setTypeface(fm->legacyMakeTypeface(familyName.c_str(), typeface->fontStyle()));
|
2014-08-27 14:03:13 +00:00
|
|
|
return drawString(canvas, ch, x, y, paint) + 20;
|
|
|
|
}
|
|
|
|
|
2014-11-10 21:29:33 +00:00
|
|
|
static const char* zh = "zh";
|
|
|
|
static const char* ja = "ja";
|
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
class FontMgrGM : public skiagm::GM {
|
|
|
|
public:
|
2017-11-13 23:10:54 +00:00
|
|
|
FontMgrGM() {
|
2017-01-03 18:32:36 +00:00
|
|
|
SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
|
|
|
|
|
|
|
|
fName.set("fontmgr_iter");
|
2017-11-13 23:10:54 +00:00
|
|
|
fFM = SkFontMgr::RefDefault();
|
2017-11-13 16:03:16 +00:00
|
|
|
fName.append(sk_tool_utils::platform_font_manager());
|
2017-01-03 18:32:36 +00:00
|
|
|
}
|
2013-03-28 13:39:35 +00:00
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-21 15:20:43 +00:00
|
|
|
return fName;
|
2013-03-28 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-08-27 14:03:13 +00:00
|
|
|
return SkISize::Make(1536, 768);
|
2013-03-28 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-03-28 13:39:35 +00:00
|
|
|
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
|
|
|
|
2016-11-04 20:26:16 +00:00
|
|
|
SkFontMgr* fm = fFM.get();
|
2013-03-28 13:39:35 +00:00
|
|
|
int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2015-04-29 15:15:31 +00:00
|
|
|
SkString familyName;
|
|
|
|
fm->getFamilyName(i, &familyName);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setTypeface(nullptr);
|
2015-04-29 15:15:31 +00:00
|
|
|
(void)drawString(canvas, familyName, 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
|
|
|
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
|
2013-03-28 13:39:35 +00:00
|
|
|
for (int j = 0; j < set->count(); ++j) {
|
|
|
|
SkString sname;
|
|
|
|
SkFontStyle fs;
|
|
|
|
set->getStyle(j, &fs, &sname);
|
2016-04-27 17:21:04 +00:00
|
|
|
sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.slant());
|
2013-03-29 07:01:22 +00:00
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
|
2013-03-28 13:39:35 +00:00
|
|
|
x = drawString(canvas, sname, x, y, paint) + 20;
|
2014-08-27 14:03:13 +00:00
|
|
|
|
|
|
|
// check to see that we get different glyphs in japanese and chinese
|
2015-04-29 15:15:31 +00:00
|
|
|
x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &zh, 1, fs);
|
|
|
|
x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &ja, 1, fs);
|
2014-08-27 14:03:13 +00:00
|
|
|
// check that emoji characters are found
|
2015-08-27 14:41:13 +00:00
|
|
|
x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, familyName.c_str(), nullptr,0, fs);
|
2013-03-29 07:01:22 +00:00
|
|
|
}
|
2013-03-28 13:39:35 +00:00
|
|
|
y += 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontMgr> fFM;
|
2013-08-21 15:20:43 +00:00
|
|
|
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 {
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontMgr> fFM;
|
2013-03-29 14:57:22 +00:00
|
|
|
|
|
|
|
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:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-07-20 20:38:56 +00:00
|
|
|
SkString name("fontmgr_match");
|
2017-11-13 16:03:16 +00:00
|
|
|
name.append(sk_tool_utils::platform_font_manager());
|
2015-07-20 20:38:56 +00:00
|
|
|
return name;
|
2013-03-29 14:57:22 +00:00
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-03-29 14:57:22 +00:00
|
|
|
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());
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
p.setTypeface(sk_sp<SkTypeface>(fset->createTypeface(j)));
|
2013-03-29 14:57:22 +00:00
|
|
|
(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);
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> face(fset->matchStyle(fs));
|
2013-03-29 14:57:22 +00:00
|
|
|
if (face) {
|
|
|
|
SkString str;
|
|
|
|
str.printf("request [%d %d]", fs.weight(), fs.width());
|
2016-05-12 17:09:30 +00:00
|
|
|
p.setTypeface(std::move(face));
|
2013-03-29 14:57:22 +00:00
|
|
|
(void)drawString(canvas, str, 0, y, p);
|
|
|
|
y += 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-03-29 14:57:22 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(17);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* gNames[] = {
|
2013-03-29 14:57:22 +00:00
|
|
|
"Helvetica Neue", "Arial"
|
|
|
|
};
|
|
|
|
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontStyleSet> fset;
|
2013-03-30 07:01:27 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
|
2013-11-12 15:25:29 +00:00
|
|
|
fset.reset(fFM->matchFamily(gNames[i]));
|
|
|
|
if (fset->count() > 0) {
|
2013-03-29 14:57:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fset.get()) {
|
2013-03-29 14:57:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->translate(20, 40);
|
2016-11-04 20:26:16 +00:00
|
|
|
this->exploreFamily(canvas, paint, fset.get());
|
2013-03-29 14:57:22 +00:00
|
|
|
canvas->translate(150, 0);
|
2016-11-04 20:26:16 +00:00
|
|
|
this->iterateFamily(canvas, paint, fset.get());
|
2013-03-29 14:57:22 +00:00
|
|
|
}
|
2013-03-30 07:01:27 +00:00
|
|
|
|
2013-03-29 14:57:22 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-10-22 20:20:58 +00:00
|
|
|
class FontMgrBoundsGM : public skiagm::GM {
|
|
|
|
public:
|
2014-12-15 21:27:26 +00:00
|
|
|
FontMgrBoundsGM(double scale, double skew)
|
|
|
|
: fScaleX(SkDoubleToScalar(scale))
|
|
|
|
, fSkewX(SkDoubleToScalar(skew))
|
|
|
|
{
|
2014-10-22 20:20:58 +00:00
|
|
|
fName.set("fontmgr_bounds");
|
2014-12-15 21:27:26 +00:00
|
|
|
if (scale != 1 || skew != 0) {
|
|
|
|
fName.appendf("_%g_%g", scale, skew);
|
|
|
|
}
|
2017-11-13 16:03:16 +00:00
|
|
|
fName.append(sk_tool_utils::platform_font_manager());
|
2017-01-03 18:32:36 +00:00
|
|
|
fFM = SkFontMgr::RefDefault();
|
2014-10-22 20:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
|
2017-07-17 19:32:25 +00:00
|
|
|
SkColor boundsColor)
|
|
|
|
{
|
|
|
|
SkPaint glyphPaint(paint);
|
|
|
|
SkRect fontBounds = glyphPaint.getFontBounds();
|
|
|
|
fontBounds.offset(x, y);
|
|
|
|
SkPaint boundsPaint(glyphPaint);
|
|
|
|
boundsPaint.setColor(boundsColor);
|
2018-02-15 15:20:04 +00:00
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
2017-07-17 19:32:25 +00:00
|
|
|
canvas->drawRect(fontBounds, boundsPaint);
|
|
|
|
|
|
|
|
SkPaint::FontMetrics fm;
|
|
|
|
glyphPaint.getFontMetrics(&fm);
|
|
|
|
SkPaint metricsPaint(boundsPaint);
|
|
|
|
metricsPaint.setStyle(SkPaint::kFill_Style);
|
|
|
|
metricsPaint.setAlpha(0x40);
|
|
|
|
if ((fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag) &&
|
|
|
|
(fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag))
|
|
|
|
{
|
|
|
|
SkRect underline{ fontBounds.fLeft, fm.fUnderlinePosition+y,
|
|
|
|
fontBounds.fRight, fm.fUnderlinePosition+y + fm.fUnderlineThickness };
|
|
|
|
canvas->drawRect(underline, metricsPaint);
|
|
|
|
}
|
2014-10-22 20:20:58 +00:00
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
if ((fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag) &&
|
|
|
|
(fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag))
|
|
|
|
{
|
|
|
|
SkRect strikeout{ fontBounds.fLeft, fm.fStrikeoutPosition+y - fm.fStrikeoutThickness,
|
|
|
|
fontBounds.fRight, fm.fStrikeoutPosition+y };
|
|
|
|
canvas->drawRect(strikeout, metricsPaint);
|
2014-10-22 20:20:58 +00:00
|
|
|
}
|
2015-01-06 01:18:51 +00:00
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
SkGlyphID left = 0, right = 0, top = 0, bottom = 0;
|
|
|
|
{
|
|
|
|
int numGlyphs = glyphPaint.getTypeface()->countGlyphs();
|
|
|
|
SkRect min = {0, 0, 0, 0};
|
|
|
|
glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
for (int i = 0; i < numGlyphs; ++i) {
|
|
|
|
SkGlyphID glyphId = i;
|
|
|
|
SkRect cur;
|
|
|
|
glyphPaint.measureText(&glyphId, sizeof(glyphId), &cur);
|
|
|
|
if (cur.fLeft < min.fLeft ) { min.fLeft = cur.fLeft; left = i; }
|
|
|
|
if (cur.fTop < min.fTop ) { min.fTop = cur.fTop ; top = i; }
|
|
|
|
if (min.fRight < cur.fRight ) { min.fRight = cur.fRight; right = i; }
|
|
|
|
if (min.fBottom < cur.fBottom) { min.fBottom = cur.fBottom; bottom = i; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkGlyphID str[] = { left, right, top, bottom };
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(str); ++i) {
|
2018-02-15 15:20:04 +00:00
|
|
|
SkPath path;
|
|
|
|
glyphPaint.getTextPath(&str[i], sizeof(str[0]), x, y, &path);
|
|
|
|
SkPaint::Style style = path.isEmpty() ? SkPaint::kFill_Style : SkPaint::kStroke_Style;
|
|
|
|
glyphPaint.setStyle(style);
|
2017-07-17 19:32:25 +00:00
|
|
|
canvas->drawText(&str[i], sizeof(str[0]), x, y, glyphPaint);
|
|
|
|
}
|
2014-10-22 20:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-10-22 20:20:58 +00:00
|
|
|
return fName;
|
|
|
|
}
|
2015-01-06 01:18:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-10-22 20:20:58 +00:00
|
|
|
return SkISize::Make(1024, 850);
|
|
|
|
}
|
2015-01-06 01:18:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-10-22 20:20:58 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(100);
|
2014-12-15 21:27:26 +00:00
|
|
|
paint.setTextScaleX(fScaleX);
|
|
|
|
paint.setTextSkewX(fSkewX);
|
2014-10-22 20:20:58 +00:00
|
|
|
|
|
|
|
const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-11-04 20:26:16 +00:00
|
|
|
SkFontMgr* fm = fFM.get();
|
2014-10-22 20:20:58 +00:00
|
|
|
int count = SkMin32(fm->countFamilies(), 32);
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
SkScalar x = 0, y = 0;
|
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
canvas->translate(10, 120);
|
2014-10-22 20:20:58 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
|
2017-07-17 19:32:25 +00:00
|
|
|
for (int j = 0; j < set->count() && j < 3; ++j) {
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
|
2017-07-24 15:04:21 +00:00
|
|
|
// Fonts with lots of glyphs are interesting, but can take a long time to find
|
|
|
|
// the glyphs which make up the maximum extent.
|
|
|
|
if (paint.getTypeface() && paint.getTypeface()->countGlyphs() < 1000) {
|
2017-07-17 19:32:25 +00:00
|
|
|
SkRect fontBounds = paint.getFontBounds();
|
|
|
|
x -= fontBounds.fLeft;
|
2014-10-22 20:20:58 +00:00
|
|
|
show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
|
2017-07-17 19:32:25 +00:00
|
|
|
x += fontBounds.fRight + 20;
|
2014-10-22 20:20:58 +00:00
|
|
|
index += 1;
|
2017-07-17 19:32:25 +00:00
|
|
|
if (x > 900) {
|
2014-10-22 20:20:58 +00:00
|
|
|
x = 0;
|
|
|
|
y += 160;
|
|
|
|
}
|
2017-07-17 19:32:25 +00:00
|
|
|
if (y >= 700) {
|
2014-10-22 20:20:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 19:07:07 +00:00
|
|
|
|
2014-10-22 20:20:58 +00:00
|
|
|
private:
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontMgr> fFM;
|
2014-10-22 20:20:58 +00:00
|
|
|
SkString fName;
|
2014-12-15 21:27:26 +00:00
|
|
|
SkScalar fScaleX, fSkewX;
|
2014-10-22 20:20:58 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-03-28 13:39:35 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new FontMgrGM;)
|
|
|
|
DEF_GM(return new FontMgrMatchGM;)
|
|
|
|
DEF_GM(return new FontMgrBoundsGM(1.0, 0);)
|
|
|
|
DEF_GM(return new FontMgrBoundsGM(0.75, 0);)
|
|
|
|
DEF_GM(return new FontMgrBoundsGM(1.0, -0.25);)
|