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