blob: 02001238e975c4719d4dbd656efd31f80753860d [file] [log] [blame]
tfarina20108912014-06-21 10:54:17 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +00008#include "sk_tool_utils.h"
Cary Clark992c7b02014-07-31 08:58:44 -04009#include "sk_tool_utils_flags.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000010
tfarina20108912014-06-21 10:54:17 -070011#include "SkBitmap.h"
12#include "SkCanvas.h"
Cary Clark992c7b02014-07-31 08:58:44 -040013#include "SkTestScalerContext.h"
14
15DEFINE_bool(portableFonts, false, "Use portable fonts");
16DEFINE_bool(resourceFonts, false, "Use resource fonts");
tfarina20108912014-06-21 10:54:17 -070017
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018namespace sk_tool_utils {
19
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000020const char* colortype_name(SkColorType ct) {
21 switch (ct) {
22 case kUnknown_SkColorType: return "Unknown";
23 case kAlpha_8_SkColorType: return "Alpha_8";
24 case kIndex_8_SkColorType: return "Index_8";
25 case kARGB_4444_SkColorType: return "ARGB_4444";
26 case kRGB_565_SkColorType: return "RGB_565";
27 case kRGBA_8888_SkColorType: return "RGBA_8888";
28 case kBGRA_8888_SkColorType: return "BGRA_8888";
29 default:
30 SkASSERT(false);
31 return "unexpected colortype";
32 }
33}
34
Cary Clark992c7b02014-07-31 08:58:44 -040035SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style) {
36 SkTypeface* face;
37 if (FLAGS_portableFonts) {
38 face = create_font(name, style);
39 } else if (FLAGS_resourceFonts) {
40 face = resource_font(name, style);
41 } else {
42 face = SkTypeface::CreateFromName(name, style);
caryclark5fb6bd42014-06-23 11:25:00 -070043 }
Cary Clark992c7b02014-07-31 08:58:44 -040044 return face;
45}
46
47void set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) {
48 SkTypeface* face = create_portable_typeface(name, style);
49 SkSafeUnref(paint->setTypeface(face));
caryclark5fb6bd42014-06-23 11:25:00 -070050}
51
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000052void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
53 SkColorType colorType, SkAlphaType alphaType) {
54 SkBitmap tmp(bitmap);
55 tmp.lockPixels();
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000056
reede5ea5002014-09-03 11:54:58 -070057 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000058
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000059 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
60}
61
tfarina20108912014-06-21 10:54:17 -070062} // namespace sk_tool_utils