sk_tool_utils.cpp revision 992c7b03ef7914a18bfd78e965b0b4c99a5f5672
12010891425f10257363ae6eb0da70453746dc087tfarina/*
22010891425f10257363ae6eb0da70453746dc087tfarina * Copyright 2014 Google Inc.
32010891425f10257363ae6eb0da70453746dc087tfarina *
42010891425f10257363ae6eb0da70453746dc087tfarina * Use of this source code is governed by a BSD-style license that can be
52010891425f10257363ae6eb0da70453746dc087tfarina * found in the LICENSE file.
62010891425f10257363ae6eb0da70453746dc087tfarina */
72010891425f10257363ae6eb0da70453746dc087tfarina
84cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org#include "sk_tool_utils.h"
9992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark#include "sk_tool_utils_flags.h"
104cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org
112010891425f10257363ae6eb0da70453746dc087tfarina#include "SkBitmap.h"
122010891425f10257363ae6eb0da70453746dc087tfarina#include "SkCanvas.h"
13992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark#include "SkTestScalerContext.h"
142010891425f10257363ae6eb0da70453746dc087tfarina
15992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary ClarkDEFINE_bool(portableFonts, false, "Use portable fonts");
16992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary ClarkDEFINE_bool(resourceFonts, false, "Use resource fonts");
174cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org
18992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clarknamespace sk_tool_utils {
195fb6bd4b7e8d00b7f2543ca10ec9022b32632f29caryclark
20a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.orgconst char* colortype_name(SkColorType ct) {
21a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org    switch (ct) {
22a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kUnknown_SkColorType:      return "Unknown";
23a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kAlpha_8_SkColorType:      return "Alpha_8";
24a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kIndex_8_SkColorType:      return "Index_8";
25a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kARGB_4444_SkColorType:    return "ARGB_4444";
26a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kRGB_565_SkColorType:      return "RGB_565";
27a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kRGBA_8888_SkColorType:    return "RGBA_8888";
28a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        case kBGRA_8888_SkColorType:    return "BGRA_8888";
29a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org        default:
30a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org            SkASSERT(false);
31a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org            return "unexpected colortype";
32a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org    }
33a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org}
34a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org
35992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary ClarkSkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style) {
36992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    SkTypeface* face;
37992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    if (FLAGS_portableFonts) {
38992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark        face = create_font(name, style);
39992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    } else if (FLAGS_resourceFonts) {
40992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark        face = resource_font(name, style);
41992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    } else {
42992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark        face = SkTypeface::CreateFromName(name, style);
435fb6bd4b7e8d00b7f2543ca10ec9022b32632f29caryclark    }
44992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    return face;
45992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark}
46992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark
47992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clarkvoid set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) {
48992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    SkTypeface* face = create_portable_typeface(name, style);
49992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark    SkSafeUnref(paint->setTypeface(face));
505fb6bd4b7e8d00b7f2543ca10ec9022b32632f29caryclark}
515fb6bd4b7e8d00b7f2543ca10ec9022b32632f29caryclark
524cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.orgvoid write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
534cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org                  SkColorType colorType, SkAlphaType alphaType) {
544cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    SkBitmap tmp(bitmap);
554cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    tmp.lockPixels();
56e62513fb9274b65bcd9fecf61acc418dd3949df5skia.committer@gmail.com
574cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    SkImageInfo info = tmp.info();
584cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    info.fColorType = colorType;
594cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    info.fAlphaType = alphaType;
60e62513fb9274b65bcd9fecf61acc418dd3949df5skia.committer@gmail.com
614cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
624cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org}
634cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org
642010891425f10257363ae6eb0da70453746dc087tfarina}  // namespace sk_tool_utils
65