blob: e43ff74a85f22ee2ebb751de060e757299f53653 [file] [log] [blame]
fmalita00d5c2c2014-08-21 08:53:26 -07001/*
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 SkTextBlob_DEFINED
9#define SkTextBlob_DEFINED
10
bungemanbf521ff2016-02-17 13:13:44 -080011#include "../private/SkTemplates.h"
fmalita00d5c2c2014-08-21 08:53:26 -070012#include "SkPaint.h"
13#include "SkRefCnt.h"
fmalita00d5c2c2014-08-21 08:53:26 -070014
fmalitab7425172014-08-26 07:56:44 -070015class SkReadBuffer;
16class SkWriteBuffer;
17
fmalita00d5c2c2014-08-21 08:53:26 -070018/** \class SkTextBlob
19
20 SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
21*/
jbroman3053dfa2014-08-25 06:22:12 -070022class SK_API SkTextBlob : public SkRefCnt {
fmalita00d5c2c2014-08-21 08:53:26 -070023public:
24 /**
fmalita3dc40ac2015-01-28 10:56:06 -080025 * Returns a conservative blob bounding box.
fmalita00d5c2c2014-08-21 08:53:26 -070026 */
27 const SkRect& bounds() const { return fBounds; }
28
29 /**
30 * Return a non-zero, unique value representing the text blob.
31 */
joshualitt2af85832015-03-25 13:40:13 -070032 uint32_t uniqueID() const { return fUniqueID; }
fmalita00d5c2c2014-08-21 08:53:26 -070033
fmalita228a6f22014-08-28 13:59:42 -070034 /**
35 * Serialize to a buffer.
36 */
37 void flatten(SkWriteBuffer&) const;
38
39 /**
40 * Recreate an SkTextBlob that was serialized into a buffer.
41 *
42 * @param SkReadBuffer Serialized blob data.
43 * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is
44 * invalid.
45 */
46 static const SkTextBlob* CreateFromBuffer(SkReadBuffer&);
47
fmalita00d5c2c2014-08-21 08:53:26 -070048 enum GlyphPositioning {
49 kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
50 kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
51 kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
52 };
53
halcanary33779752015-10-27 14:01:05 -070054private:
fmalita3c196de2014-09-20 05:40:22 -070055 class RunRecord;
56
fmalita3c196de2014-09-20 05:40:22 -070057 SkTextBlob(int runCount, const SkRect& bounds);
fmalita00d5c2c2014-08-21 08:53:26 -070058
fmalita3c196de2014-09-20 05:40:22 -070059 virtual ~SkTextBlob();
bsalomon07280312014-11-20 08:02:46 -080060
61 // Memory for objects of this class is created with sk_malloc rather than operator new and must
62 // be freed with sk_free.
63 void operator delete(void* p) { sk_free(p); }
64 void* operator new(size_t) {
65 SkFAIL("All blobs are created by placement new.");
66 return sk_malloc_throw(0);
67 }
68 void* operator new(size_t, void* p) { return p; }
fmalita00d5c2c2014-08-21 08:53:26 -070069
fmalitab7425172014-08-26 07:56:44 -070070 static unsigned ScalarsPerGlyph(GlyphPositioning pos);
71
fmalita00d5c2c2014-08-21 08:53:26 -070072 friend class SkTextBlobBuilder;
halcanary33779752015-10-27 14:01:05 -070073 friend class SkTextBlobRunIterator;
fmalita00d5c2c2014-08-21 08:53:26 -070074
fmalita3c196de2014-09-20 05:40:22 -070075 const int fRunCount;
76 const SkRect fBounds;
joshualitt2af85832015-03-25 13:40:13 -070077 const uint32_t fUniqueID;
fmalita00d5c2c2014-08-21 08:53:26 -070078
fmalita3c196de2014-09-20 05:40:22 -070079 SkDEBUGCODE(size_t fStorageSize;)
fmalita00d5c2c2014-08-21 08:53:26 -070080
fmalita3c196de2014-09-20 05:40:22 -070081 // The actual payload resides in externally-managed storage, following the object.
82 // (see the .cpp for more details)
fmalita00d5c2c2014-08-21 08:53:26 -070083
84 typedef SkRefCnt INHERITED;
85};
86
87/** \class SkTextBlobBuilder
88
89 Helper class for constructing SkTextBlobs.
90 */
jbroman3053dfa2014-08-25 06:22:12 -070091class SK_API SkTextBlobBuilder {
fmalita00d5c2c2014-08-21 08:53:26 -070092public:
fmalita3c196de2014-09-20 05:40:22 -070093 SkTextBlobBuilder();
fmalita00d5c2c2014-08-21 08:53:26 -070094
95 ~SkTextBlobBuilder();
96
97 /**
98 * Returns an immutable SkTextBlob for the current runs/glyphs. The builder is reset and
99 * can be reused.
100 */
101 const SkTextBlob* build();
102
103 /**
104 * Glyph and position buffers associated with a run.
105 *
106 * A run is a sequence of glyphs sharing the same font metrics and positioning mode.
107 */
108 struct RunBuffer {
109 uint16_t* glyphs;
110 SkScalar* pos;
111 };
112
113 /**
114 * Allocates a new default-positioned run and returns its writable glyph buffer
115 * for direct manipulation.
116 *
117 * @param font The font to be used for this run.
118 * @param count Number of glyphs.
119 * @param x,y Position within the blob.
120 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
121 * be used when computing the blob bounds, to avoid re-measuring.
122 *
123 * @return A writable glyph buffer, valid until the next allocRun() or
124 * build() call. The buffer is guaranteed to hold @count@ glyphs.
125 */
126 const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
127 const SkRect* bounds = NULL);
128
129 /**
130 * Allocates a new horizontally-positioned run and returns its writable glyph and position
131 * buffers for direct manipulation.
132 *
133 * @param font The font to be used for this run.
134 * @param count Number of glyphs.
135 * @param y Vertical offset within the blob.
136 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
137 * be used when computing the blob bounds, to avoid re-measuring.
138 *
139 * @return Writable glyph and position buffers, valid until the next allocRun()
140 * or build() call. The buffers are guaranteed to hold @count@ elements.
141 */
142 const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
143 const SkRect* bounds = NULL);
144
145 /**
146 * Allocates a new fully-positioned run and returns its writable glyph and position
147 * buffers for direct manipulation.
148 *
149 * @param font The font to be used for this run.
150 * @param count Number of glyphs.
151 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
152 * be used when computing the blob bounds, to avoid re-measuring.
153 *
154 * @return Writable glyph and position buffers, valid until the next allocRun()
155 * or build() call. The glyph buffer and position buffer are
156 * guaranteed to hold @count@ and 2 * @count@ elements, respectively.
157 */
158 const RunBuffer& allocRunPos(const SkPaint& font, int count, const SkRect* bounds = NULL);
159
160private:
fmalita3c196de2014-09-20 05:40:22 -0700161 void reserve(size_t size);
fmalita00d5c2c2014-08-21 08:53:26 -0700162 void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
163 int count, SkPoint offset, const SkRect* bounds);
fmalita3c196de2014-09-20 05:40:22 -0700164 bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
165 int count, SkPoint offset);
fmalita00d5c2c2014-08-21 08:53:26 -0700166 void updateDeferredBounds();
167
fmalita3dc40ac2015-01-28 10:56:06 -0800168 static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
169 static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
170
fmalita3c196de2014-09-20 05:40:22 -0700171 SkAutoTMalloc<uint8_t> fStorage;
172 size_t fStorageSize;
173 size_t fStorageUsed;
fmalita00d5c2c2014-08-21 08:53:26 -0700174
fmalita3c196de2014-09-20 05:40:22 -0700175 SkRect fBounds;
176 int fRunCount;
177 bool fDeferredBounds;
178 size_t fLastRun; // index into fStorage
fmalita00d5c2c2014-08-21 08:53:26 -0700179
fmalita3c196de2014-09-20 05:40:22 -0700180 RunBuffer fCurrentRunBuffer;
fmalita00d5c2c2014-08-21 08:53:26 -0700181};
182
183#endif // SkTextBlob_DEFINED