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 "SkShader.h"
14 #include "SkTestScalerContext.h"
15 #include "SkTextBlob.h"
16
17 DEFINE_bool(portableFonts, false, "Use portable fonts");
18 DEFINE_bool(resourceFonts, false, "Use resource fonts");
19
20 namespace sk_tool_utils {
21
colortype_name(SkColorType ct)22 const char* colortype_name(SkColorType ct) {
23 switch (ct) {
24 case kUnknown_SkColorType: return "Unknown";
25 case kAlpha_8_SkColorType: return "Alpha_8";
26 case kIndex_8_SkColorType: return "Index_8";
27 case kARGB_4444_SkColorType: return "ARGB_4444";
28 case kRGB_565_SkColorType: return "RGB_565";
29 case kRGBA_8888_SkColorType: return "RGBA_8888";
30 case kBGRA_8888_SkColorType: return "BGRA_8888";
31 default:
32 SkASSERT(false);
33 return "unexpected colortype";
34 }
35 }
36
create_portable_typeface(const char * name,SkTypeface::Style style)37 SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style) {
38 SkTypeface* face;
39 if (FLAGS_portableFonts) {
40 face = create_font(name, style);
41 } else if (FLAGS_resourceFonts) {
42 face = resource_font(name, style);
43 } else {
44 face = SkTypeface::CreateFromName(name, style);
45 }
46 return face;
47 }
48
set_portable_typeface(SkPaint * paint,const char * name,SkTypeface::Style style)49 void set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) {
50 SkTypeface* face = create_portable_typeface(name, style);
51 SkSafeUnref(paint->setTypeface(face));
52 }
53
write_pixels(SkCanvas * canvas,const SkBitmap & bitmap,int x,int y,SkColorType colorType,SkAlphaType alphaType)54 void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
55 SkColorType colorType, SkAlphaType alphaType) {
56 SkBitmap tmp(bitmap);
57 tmp.lockPixels();
58
59 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
60
61 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
62 }
63
create_checkerboard_shader(SkColor c1,SkColor c2,int size)64 SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
65 SkBitmap bm;
66 bm.allocN32Pixels(2 * size, 2 * size);
67 bm.eraseColor(c1);
68 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
69 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
70 return SkShader::CreateBitmapShader(
71 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
72 }
73
draw_checkerboard(SkCanvas * canvas,SkColor c1,SkColor c2,int size)74 void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
75 SkPaint paint;
76 paint.setShader(create_checkerboard_shader(c1, c2, size))->unref();
77 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
78 canvas->drawPaint(paint);
79 }
80
add_to_text_blob(SkTextBlobBuilder * builder,const char * text,const SkPaint & origPaint,SkScalar x,SkScalar y)81 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
82 SkScalar x, SkScalar y) {
83 SkPaint paint(origPaint);
84 SkTDArray<uint16_t> glyphs;
85
86 size_t len = strlen(text);
87 glyphs.append(paint.textToGlyphs(text, len, NULL));
88 paint.textToGlyphs(text, len, glyphs.begin());
89
90 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
91 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
92 NULL);
93 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
94 }
95
96
97 } // namespace sk_tool_utils
98