blob: 8198f04a80bba1269ac3481e8b6f4075db248d91 [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"
halcanary4f0a23a2016-08-30 11:58:33 -070013#include "SkString.h"
fmalita00d5c2c2014-08-21 08:53:26 -070014#include "SkRefCnt.h"
fmalita00d5c2c2014-08-21 08:53:26 -070015
fmalitab7425172014-08-26 07:56:44 -070016class SkReadBuffer;
17class SkWriteBuffer;
18
fmalita00d5c2c2014-08-21 08:53:26 -070019/** \class SkTextBlob
20
21 SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
22*/
mtkleinb47cd4b2016-08-09 12:20:04 -070023class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
fmalita00d5c2c2014-08-21 08:53:26 -070024public:
25 /**
fmalita3dc40ac2015-01-28 10:56:06 -080026 * Returns a conservative blob bounding box.
fmalita00d5c2c2014-08-21 08:53:26 -070027 */
28 const SkRect& bounds() const { return fBounds; }
29
30 /**
31 * Return a non-zero, unique value representing the text blob.
32 */
joshualitt2af85832015-03-25 13:40:13 -070033 uint32_t uniqueID() const { return fUniqueID; }
fmalita00d5c2c2014-08-21 08:53:26 -070034
fmalita228a6f22014-08-28 13:59:42 -070035 /**
36 * Serialize to a buffer.
37 */
38 void flatten(SkWriteBuffer&) const;
39
40 /**
41 * Recreate an SkTextBlob that was serialized into a buffer.
42 *
43 * @param SkReadBuffer Serialized blob data.
44 * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is
45 * invalid.
46 */
reed2ab90572016-08-10 14:16:41 -070047 static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
48
49 static const SkTextBlob* CreateFromBuffer(SkReadBuffer& buffer) {
50 return MakeFromBuffer(buffer).release();
51 }
fmalita228a6f22014-08-28 13:59:42 -070052
halcanary4f0a23a2016-08-30 11:58:33 -070053 enum GlyphPositioning : uint8_t {
fmalita00d5c2c2014-08-21 08:53:26 -070054 kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
55 kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
56 kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
57 };
58
halcanary33779752015-10-27 14:01:05 -070059private:
mtkleinb47cd4b2016-08-09 12:20:04 -070060 friend class SkNVRefCnt<SkTextBlob>;
fmalita3c196de2014-09-20 05:40:22 -070061 class RunRecord;
62
fmalita3c196de2014-09-20 05:40:22 -070063 SkTextBlob(int runCount, const SkRect& bounds);
fmalita00d5c2c2014-08-21 08:53:26 -070064
mtkleinb47cd4b2016-08-09 12:20:04 -070065 ~SkTextBlob();
bsalomon07280312014-11-20 08:02:46 -080066
67 // Memory for objects of this class is created with sk_malloc rather than operator new and must
68 // be freed with sk_free.
69 void operator delete(void* p) { sk_free(p); }
70 void* operator new(size_t) {
71 SkFAIL("All blobs are created by placement new.");
72 return sk_malloc_throw(0);
73 }
74 void* operator new(size_t, void* p) { return p; }
fmalita00d5c2c2014-08-21 08:53:26 -070075
fmalitab7425172014-08-26 07:56:44 -070076 static unsigned ScalarsPerGlyph(GlyphPositioning pos);
77
fmalita00d5c2c2014-08-21 08:53:26 -070078 friend class SkTextBlobBuilder;
halcanary33779752015-10-27 14:01:05 -070079 friend class SkTextBlobRunIterator;
fmalita00d5c2c2014-08-21 08:53:26 -070080
fmalita3c196de2014-09-20 05:40:22 -070081 const int fRunCount;
82 const SkRect fBounds;
joshualitt2af85832015-03-25 13:40:13 -070083 const uint32_t fUniqueID;
fmalita00d5c2c2014-08-21 08:53:26 -070084
fmalita3c196de2014-09-20 05:40:22 -070085 SkDEBUGCODE(size_t fStorageSize;)
fmalita00d5c2c2014-08-21 08:53:26 -070086
fmalita3c196de2014-09-20 05:40:22 -070087 // The actual payload resides in externally-managed storage, following the object.
88 // (see the .cpp for more details)
fmalita00d5c2c2014-08-21 08:53:26 -070089
90 typedef SkRefCnt INHERITED;
91};
92
93/** \class SkTextBlobBuilder
94
95 Helper class for constructing SkTextBlobs.
96 */
jbroman3053dfa2014-08-25 06:22:12 -070097class SK_API SkTextBlobBuilder {
fmalita00d5c2c2014-08-21 08:53:26 -070098public:
fmalita3c196de2014-09-20 05:40:22 -070099 SkTextBlobBuilder();
fmalita00d5c2c2014-08-21 08:53:26 -0700100
101 ~SkTextBlobBuilder();
102
103 /**
104 * Returns an immutable SkTextBlob for the current runs/glyphs. The builder is reset and
105 * can be reused.
106 */
reed2ab90572016-08-10 14:16:41 -0700107 sk_sp<SkTextBlob> make();
108
fmalita00d5c2c2014-08-21 08:53:26 -0700109 /**
110 * Glyph and position buffers associated with a run.
111 *
halcanary4f0a23a2016-08-30 11:58:33 -0700112 * A run is a sequence of glyphs sharing the same font metrics
113 * and positioning mode.
114 *
115 * If textByteCount is 0, utf8text and clusters will be NULL (no
116 * character information will be associated with the glyphs).
117 *
118 * utf8text will point to a buffer of size textByteCount bytes.
119 *
120 * clusters (if not NULL) will point to an array of size count.
121 * For each glyph, give the byte-offset into the text for the
122 * first byte in the first character in that glyph's cluster.
123 * Each value in the array should be an integer less than
124 * textByteCount. Values in the array should either be
125 * monotonically increasing (left-to-right text) or monotonically
126 * decreasing (right-to-left text). This definiton is conviently
127 * the same as used by Harfbuzz's hb_glyph_info_t::cluster field,
128 * except that Harfbuzz interleaves glyphs and clusters.
fmalita00d5c2c2014-08-21 08:53:26 -0700129 */
130 struct RunBuffer {
halcanaryd0e95a52016-07-25 07:18:12 -0700131 SkGlyphID* glyphs;
fmalita00d5c2c2014-08-21 08:53:26 -0700132 SkScalar* pos;
halcanary4f0a23a2016-08-30 11:58:33 -0700133 char* utf8text;
134 uint32_t* clusters;
fmalita00d5c2c2014-08-21 08:53:26 -0700135 };
136
137 /**
138 * Allocates a new default-positioned run and returns its writable glyph buffer
139 * for direct manipulation.
140 *
141 * @param font The font to be used for this run.
142 * @param count Number of glyphs.
143 * @param x,y Position within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700144 * @param textByteCount length of the original UTF-8 text that
145 * corresponds to this sequence of glyphs. If 0,
146 * text will not be included in the textblob.
147 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700148 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
149 * be used when computing the blob bounds, to avoid re-measuring.
150 *
151 * @return A writable glyph buffer, valid until the next allocRun() or
152 * build() call. The buffer is guaranteed to hold @count@ glyphs.
153 */
halcanary4f0a23a2016-08-30 11:58:33 -0700154 const RunBuffer& allocRunText(const SkPaint& font,
155 int count,
156 SkScalar x,
157 SkScalar y,
158 int textByteCount,
159 SkString lang,
160 const SkRect* bounds = NULL);
fmalita00d5c2c2014-08-21 08:53:26 -0700161 const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
halcanary4f0a23a2016-08-30 11:58:33 -0700162 const SkRect* bounds = NULL) {
163 return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
164 }
fmalita00d5c2c2014-08-21 08:53:26 -0700165
166 /**
167 * Allocates a new horizontally-positioned run and returns its writable glyph and position
168 * buffers for direct manipulation.
169 *
170 * @param font The font to be used for this run.
171 * @param count Number of glyphs.
172 * @param y Vertical offset within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700173 * @param textByteCount length of the original UTF-8 text that
174 * corresponds to this sequence of glyphs. If 0,
175 * text will not be included in the textblob.
176 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700177 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
178 * be used when computing the blob bounds, to avoid re-measuring.
179 *
180 * @return Writable glyph and position buffers, valid until the next allocRun()
181 * or build() call. The buffers are guaranteed to hold @count@ elements.
182 */
halcanary4f0a23a2016-08-30 11:58:33 -0700183 const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
184 int textByteCount, SkString lang,
185 const SkRect* bounds = NULL);
fmalita00d5c2c2014-08-21 08:53:26 -0700186 const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
halcanary4f0a23a2016-08-30 11:58:33 -0700187 const SkRect* bounds = NULL) {
188 return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
189 }
fmalita00d5c2c2014-08-21 08:53:26 -0700190
191 /**
192 * Allocates a new fully-positioned run and returns its writable glyph and position
193 * buffers for direct manipulation.
194 *
195 * @param font The font to be used for this run.
196 * @param count Number of glyphs.
halcanary4f0a23a2016-08-30 11:58:33 -0700197 * @param textByteCount length of the original UTF-8 text that
198 * corresponds to this sequence of glyphs. If 0,
199 * text will not be included in the textblob.
200 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700201 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
202 * be used when computing the blob bounds, to avoid re-measuring.
203 *
204 * @return Writable glyph and position buffers, valid until the next allocRun()
205 * or build() call. The glyph buffer and position buffer are
206 * guaranteed to hold @count@ and 2 * @count@ elements, respectively.
207 */
halcanary4f0a23a2016-08-30 11:58:33 -0700208 const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
209 int textByteCount, SkString lang,
210 const SkRect* bounds = NULL);
211 const RunBuffer& allocRunPos(const SkPaint& font, int count,
212 const SkRect* bounds = NULL) {
213 return this->allocRunTextPos(font, count, 0, SkString(), bounds);
214 }
fmalita00d5c2c2014-08-21 08:53:26 -0700215
216private:
fmalita3c196de2014-09-20 05:40:22 -0700217 void reserve(size_t size);
fmalita00d5c2c2014-08-21 08:53:26 -0700218 void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
halcanary4f0a23a2016-08-30 11:58:33 -0700219 int count, int textBytes, SkPoint offset, const SkRect* bounds);
fmalita3c196de2014-09-20 05:40:22 -0700220 bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
221 int count, SkPoint offset);
fmalita00d5c2c2014-08-21 08:53:26 -0700222 void updateDeferredBounds();
223
fmalita3dc40ac2015-01-28 10:56:06 -0800224 static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
225 static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
226
fmalita3c196de2014-09-20 05:40:22 -0700227 SkAutoTMalloc<uint8_t> fStorage;
228 size_t fStorageSize;
229 size_t fStorageUsed;
fmalita00d5c2c2014-08-21 08:53:26 -0700230
fmalita3c196de2014-09-20 05:40:22 -0700231 SkRect fBounds;
232 int fRunCount;
233 bool fDeferredBounds;
234 size_t fLastRun; // index into fStorage
fmalita00d5c2c2014-08-21 08:53:26 -0700235
fmalita3c196de2014-09-20 05:40:22 -0700236 RunBuffer fCurrentRunBuffer;
fmalita00d5c2c2014-08-21 08:53:26 -0700237};
238
239#endif // SkTextBlob_DEFINED