skia2/tools/sk_tool_utils.cpp
Cary Clark 992c7b03ef Add standard fonts to all GMs.
Allow GM results to be compared across machines and platforms by
standardizing the fonts used by all tests.

This adds runtime flags to DM to use either the system font context (the
default), the fonts in the resources directory ( --resourceFonts ) or a set
of canonical paths generated from the fonts ( --portableFonts ).

This CL should leave the current DM results unchanged by default.

If the portable font data or resource font is missing when DM is run, it
falls back to using the system font context.

The create_test_font tool generates the paths and metrics read by DM
with the --portableFonts flag set, and generates the font substitution
tables read by DM with the --resourceFonts flag set.

If DM is run in SkDebug mode with the --reportUsedChars flag set, it
generates the corresponding data compiled into the create_test_font tool.

All GM tests set their typeface information by calling either

  sk_tool_utils::set_portable_typeface or
  sk_tool_utils::portable_typeface .

(The former takes the paint, the latter returns a SkTypeface.) These calls
can be removed in the future when the Font Manager can be superceded.

BUG=skia:2687
R=mtklein@google.com

Review URL: https://codereview.chromium.org/407183003
2014-07-31 08:58:44 -04:00

65 lines
1.9 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "sk_tool_utils.h"
#include "sk_tool_utils_flags.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkTestScalerContext.h"
DEFINE_bool(portableFonts, false, "Use portable fonts");
DEFINE_bool(resourceFonts, false, "Use resource fonts");
namespace sk_tool_utils {
const char* colortype_name(SkColorType ct) {
switch (ct) {
case kUnknown_SkColorType: return "Unknown";
case kAlpha_8_SkColorType: return "Alpha_8";
case kIndex_8_SkColorType: return "Index_8";
case kARGB_4444_SkColorType: return "ARGB_4444";
case kRGB_565_SkColorType: return "RGB_565";
case kRGBA_8888_SkColorType: return "RGBA_8888";
case kBGRA_8888_SkColorType: return "BGRA_8888";
default:
SkASSERT(false);
return "unexpected colortype";
}
}
SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style) {
SkTypeface* face;
if (FLAGS_portableFonts) {
face = create_font(name, style);
} else if (FLAGS_resourceFonts) {
face = resource_font(name, style);
} else {
face = SkTypeface::CreateFromName(name, style);
}
return face;
}
void set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) {
SkTypeface* face = create_portable_typeface(name, style);
SkSafeUnref(paint->setTypeface(face));
}
void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
SkColorType colorType, SkAlphaType alphaType) {
SkBitmap tmp(bitmap);
tmp.lockPixels();
SkImageInfo info = tmp.info();
info.fColorType = colorType;
info.fAlphaType = alphaType;
canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
}
} // namespace sk_tool_utils