blob: 70fac4e68eb65789a0140258d15a3894e620f70a [file] [log] [blame]
Brian Salomoncbcb0a12017-11-19 13:20:13 -05001/*
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
Brian Salomonb5086962017-12-13 10:59:33 -050011#include "SkDeque.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050012#include "SkRefCnt.h"
13#include "SkScalar.h"
14
Hal Canaryc2d0fb12018-09-19 10:28:59 -040015#include <memory>
16
Brian Salomoncbcb0a12017-11-19 13:20:13 -050017class SkAtlasTextContext;
18class SkAtlasTextFont;
Brian Salomonb5086962017-12-13 10:59:33 -050019class SkMatrix;
Brian Salomona0ba7142017-11-20 13:17:43 -050020struct SkPoint;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050021
22/** Represents a client-created renderable surface and is used to draw text into the surface. */
23class SK_API SkAtlasTextTarget {
24public:
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 */
Robert Phillipsc994a932018-06-19 13:09:54 -040031 static std::unique_ptr<SkAtlasTextTarget> Make(sk_sp<SkAtlasTextContext>,
32 int width,
33 int height,
Brian Salomoncbcb0a12017-11-19 13:20:13 -050034 void* handle);
35
36 /**
Brian Salomona0ba7142017-11-20 13:17:43 -050037 * 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.
Brian Salomoncbcb0a12017-11-19 13:20:13 -050040 */
Brian Salomona0ba7142017-11-20 13:17:43 -050041 virtual void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
42 const SkAtlasTextFont&) = 0;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050043
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
Brian Salomonb5086962017-12-13 10:59:33 -050054 /** 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
Brian Salomoncbcb0a12017-11-19 13:20:13 -050077protected:
78 SkAtlasTextTarget(sk_sp<SkAtlasTextContext>, int width, int height, void* handle);
79
Brian Salomonb5086962017-12-13 10:59:33 -050080 const SkMatrix& ctm() const { return *static_cast<const SkMatrix*>(fMatrixStack.back()); }
81
Brian Salomoncbcb0a12017-11-19 13:20:13 -050082 void* const fHandle;
83 const sk_sp<SkAtlasTextContext> fContext;
84 const int fWidth;
85 const int fHeight;
86
87private:
Brian Salomonb5086962017-12-13 10:59:33 -050088 SkDeque fMatrixStack;
89 int fSaveCnt;
90
91 SkMatrix* accessCTM() const {
92 return static_cast<SkMatrix*>(const_cast<void*>(fMatrixStack.back()));
93 }
94
Brian Salomoncbcb0a12017-11-19 13:20:13 -050095 SkAtlasTextTarget() = delete;
96 SkAtlasTextTarget(const SkAtlasTextContext&) = delete;
97 SkAtlasTextTarget& operator=(const SkAtlasTextContext&) = delete;
98};
99
100#endif