Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / tools / sk_tool_utils.cpp
1 /*
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
8 #include "sk_tool_utils.h"
9 #include "sk_tool_utils_flags.h"
10
11 #include "SkBitmap.h"
12 #include "SkCanvas.h"
13 #include "SkTestScalerContext.h"
14
15 DEFINE_bool(portableFonts, false, "Use portable fonts");
16 DEFINE_bool(resourceFonts, false, "Use resource fonts");
17
18 namespace sk_tool_utils {
19
20 const 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
35 SkTypeface* 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);
43     }
44     return face;
45 }
46
47 void set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) {
48     SkTypeface* face = create_portable_typeface(name, style);
49     SkSafeUnref(paint->setTypeface(face));
50 }
51
52 void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
53                   SkColorType colorType, SkAlphaType alphaType) {
54     SkBitmap tmp(bitmap);
55     tmp.lockPixels();
56
57     const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
58
59     canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
60 }
61
62 }  // namespace sk_tool_utils