blob: 7e4ae449ed361fd50331d8920c72c89a7302e809 [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
11#include <memory>
Brian Salomonb5086962017-12-13 10:59:33 -050012#include "SkDeque.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050013#include "SkRefCnt.h"
14#include "SkScalar.h"
15
16class SkAtlasTextContext;
17class SkAtlasTextFont;
Brian Salomonb5086962017-12-13 10:59:33 -050018class SkMatrix;
Brian Salomona0ba7142017-11-20 13:17:43 -050019struct SkPoint;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050020
21/** Represents a client-created renderable surface and is used to draw text into the surface. */
22class SK_API SkAtlasTextTarget {
23public:
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 Phillipsc994a932018-06-19 13:09:54 -040030 static std::unique_ptr<SkAtlasTextTarget> Make(sk_sp<SkAtlasTextContext>,
31 int width,
32 int height,
Brian Salomoncbcb0a12017-11-19 13:20:13 -050033 void* handle);
34
35 /**
Brian Salomona0ba7142017-11-20 13:17:43 -050036 * 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 Salomoncbcb0a12017-11-19 13:20:13 -050039 */
Brian Salomona0ba7142017-11-20 13:17:43 -050040 virtual void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
41 const SkAtlasTextFont&) = 0;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050042
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 Salomonb5086962017-12-13 10:59:33 -050053 /** 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 Salomoncbcb0a12017-11-19 13:20:13 -050076protected:
77 SkAtlasTextTarget(sk_sp<SkAtlasTextContext>, int width, int height, void* handle);
78
Brian Salomonb5086962017-12-13 10:59:33 -050079 const SkMatrix& ctm() const { return *static_cast<const SkMatrix*>(fMatrixStack.back()); }
80
Brian Salomoncbcb0a12017-11-19 13:20:13 -050081 void* const fHandle;
82 const sk_sp<SkAtlasTextContext> fContext;
83 const int fWidth;
84 const int fHeight;
85
86private:
Brian Salomonb5086962017-12-13 10:59:33 -050087 SkDeque fMatrixStack;
88 int fSaveCnt;
89
90 SkMatrix* accessCTM() const {
91 return static_cast<SkMatrix*>(const_cast<void*>(fMatrixStack.back()));
92 }
93
Brian Salomoncbcb0a12017-11-19 13:20:13 -050094 SkAtlasTextTarget() = delete;
95 SkAtlasTextTarget(const SkAtlasTextContext&) = delete;
96 SkAtlasTextTarget& operator=(const SkAtlasTextContext&) = delete;
97};
98
99#endif