1 | /* |
2 | * Copyright 2017 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 SkAtlasTextTarget_DEFINED |
9 | #define SkAtlasTextTarget_DEFINED |
10 | |
11 | #include "include/core/SkRefCnt.h" |
12 | #include "include/core/SkScalar.h" |
13 | #include "include/private/SkDeque.h" |
14 | |
15 | #include <memory> |
16 | |
17 | class SkAtlasTextContext; |
18 | class SkAtlasTextFont; |
19 | class SkMatrix; |
20 | struct SkPoint; |
21 | |
22 | /** Represents a client-created renderable surface and is used to draw text into the surface. */ |
23 | class SK_API SkAtlasTextTarget { |
24 | public: |
25 | virtual ~SkAtlasTextTarget(); |
26 | |
27 | /** |
28 | * Creates a text drawing target. ‘handle’ is used to identify this rendering surface when |
29 | * draws are flushed to the SkAtlasTextContext's SkAtlasTextRenderer. |
30 | */ |
31 | static std::unique_ptr<SkAtlasTextTarget> Make(sk_sp<SkAtlasTextContext>, |
32 | int width, |
33 | int height, |
34 | void* handle); |
35 | |
36 | /** |
37 | * Enqueues a text draw in the target. The caller provides an array of glyphs and their |
38 | * positions. The meaning of 'color' here is interpreted by the client's SkAtlasTextRenderer |
39 | * when it actually renders the text. |
40 | */ |
41 | virtual void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color, |
42 | const SkAtlasTextFont&) = 0; |
43 | |
44 | /** Issues all queued text draws to SkAtlasTextRenderer. */ |
45 | virtual void flush() = 0; |
46 | |
47 | int width() const { return fWidth; } |
48 | int height() const { return fHeight; } |
49 | |
50 | void* handle() const { return fHandle; } |
51 | |
52 | SkAtlasTextContext* context() const { return fContext.get(); } |
53 | |
54 | /** Saves the current matrix in a stack. Returns the prior depth of the saved matrix stack. */ |
55 | int save(); |
56 | /** Pops the top matrix on the stack if the stack is not empty. */ |
57 | void restore(); |
58 | /** |
59 | * Pops the matrix stack until the stack depth is count. Does nothing if the depth is already |
60 | * less than count. |
61 | */ |
62 | void restoreToCount(int count); |
63 | |
64 | /** Pre-translates the current CTM. */ |
65 | void translate(SkScalar dx, SkScalar dy); |
66 | /** Pre-scales the current CTM. */ |
67 | void scale(SkScalar sx, SkScalar sy); |
68 | /** Pre-rotates the current CTM about the origin. */ |
69 | void rotate(SkScalar degrees); |
70 | /** Pre-rotates the current CTM about the (px, py). */ |
71 | void rotate(SkScalar degrees, SkScalar px, SkScalar py); |
72 | /** Pre-skews the current CTM. */ |
73 | void skew(SkScalar sx, SkScalar sy); |
74 | /** Pre-concats the current CTM. */ |
75 | void concat(const SkMatrix& matrix); |
76 | |
77 | protected: |
78 | SkAtlasTextTarget(sk_sp<SkAtlasTextContext>, int width, int height, void* handle); |
79 | |
80 | const SkMatrix& ctm() const { return *static_cast<const SkMatrix*>(fMatrixStack.back()); } |
81 | |
82 | void* const fHandle; |
83 | const sk_sp<SkAtlasTextContext> fContext; |
84 | const int fWidth; |
85 | const int fHeight; |
86 | |
87 | private: |
88 | SkDeque fMatrixStack; |
89 | int fSaveCnt; |
90 | |
91 | SkMatrix* accessCTM() const { |
92 | return static_cast<SkMatrix*>(const_cast<void*>(fMatrixStack.back())); |
93 | } |
94 | |
95 | SkAtlasTextTarget() = delete; |
96 | SkAtlasTextTarget(const SkAtlasTextContext&) = delete; |
97 | SkAtlasTextTarget& operator=(const SkAtlasTextContext&) = delete; |
98 | }; |
99 | |
100 | #endif |
101 | |