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 #ifndef sk_tool_utils_DEFINED
9 #define sk_tool_utils_DEFINED
10 
11 #include "SkColor.h"
12 #include "SkImageEncoder.h"
13 #include "SkImageInfo.h"
14 #include "SkPixelSerializer.h"
15 #include "SkTypeface.h"
16 
17 class SkBitmap;
18 class SkCanvas;
19 class SkPaint;
20 class SkShader;
21 class SkTestFont;
22 class SkTextBlobBuilder;
23 
24 namespace sk_tool_utils {
25 
26     const char* colortype_name(SkColorType);
27 
28     /**
29      * Sets the paint to use a platform-independent text renderer.
30      */
31     void set_portable_typeface(SkPaint* paint, const char* name = NULL,
32                                SkTypeface::Style style = SkTypeface::kNormal);
33     SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style);
34     void report_used_chars();
35 
36     /**
37      *  Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
38      *  the pixels are colorType + alphaType
39      */
40     void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
41 
42     // private to sk_tool_utils
43     SkTypeface* create_font(const char* name, SkTypeface::Style );
44     SkTypeface* resource_font(const char* name, SkTypeface::Style );
45 
46     /** Returns a newly created CheckerboardShader. */
47     SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size);
48 
49     /** Draw a checkerboard pattern in the current canvas, restricted to
50         the current clip, using SkXfermode::kSrc_Mode. */
51     void draw_checkerboard(SkCanvas* canvas,
52                            SkColor color1,
53                            SkColor color2,
54                            int size);
55 
56     /** A default checkerboard. */
draw_checkerboard(SkCanvas * canvas)57     inline void draw_checkerboard(SkCanvas* canvas) {
58         sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
59     }
60 
61     // Encodes to PNG, unless there is already encoded data, in which case that gets
62     // used.
63     class PngPixelSerializer : public SkPixelSerializer {
64     public:
onUseEncodedData(const void *,size_t)65         bool onUseEncodedData(const void*, size_t) override { return true; }
onEncodePixels(const SkImageInfo & info,const void * pixels,size_t rowBytes)66         SkData* onEncodePixels(const SkImageInfo& info, const void* pixels,
67                                size_t rowBytes) override {
68             return SkImageEncoder::EncodeData(info, pixels, rowBytes,
69                                               SkImageEncoder::kPNG_Type, 100);
70         }
71     };
72 
73     // A helper for inserting a drawtext call into a SkTextBlobBuilder
74     void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
75                           SkScalar x, SkScalar y);
76 
77 }  // namespace sk_tool_utils
78 
79 #endif  // sk_tool_utils_DEFINED
80