blob: f727457a1817fedc56301610e13d250c6c3daa1e [file] [log] [blame]
Cary Clarkd2ca79c2018-08-10 13:09:13 -04001#Topic Text_Blob
2#Alias Text_Blob_Reference ##
3
Cary Clarkd2ca79c2018-08-10 13:09:13 -04004#Class SkTextBlob
5
Cary Clark61313f32018-10-08 14:57:48 -04006#Code
7#Populate
8##
9
Cary Clarkd2ca79c2018-08-10 13:09:13 -040010SkTextBlob combines multiple text runs into an immutable container. Each text
11run consists of Glyphs, Paint, and position. Only parts of Paint related to
12fonts and text rendering are used by run.
13
14# ------------------------------------------------------------------------------
15
16#Method const SkRect& bounds() const
17#In Property
18#Line # returns conservative bounding box ##
Cary Clark09d80c02018-10-31 12:14:03 -040019#Populate
Cary Clarkd2ca79c2018-08-10 13:09:13 -040020
21#Example
22#Height 70
Cary Clark14768f62018-10-29 20:33:51 -040023 SkTextBlobBuilder textBlobBuilder;
24 const char bunny[] = "/(^x^)\\";
25 const int len = sizeof(bunny) - 1;
26 uint16_t glyphs[len];
27 SkPaint paint;
28 paint.textToGlyphs(bunny, len, glyphs);
29 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
30 int runs[] = { 3, 1, 3 };
31 SkPoint textPos = { 20, 50 };
32 int glyphIndex = 0;
33 for (auto runLen : runs) {
34 paint.setTextSize(1 == runLen ? 20 : 50);
35 const SkTextBlobBuilder::RunBuffer& run =
36 textBlobBuilder.allocRun(paint, runLen, textPos.fX, textPos.fY);
37 memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen);
38 textPos.fX += paint.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen, nullptr);
39 glyphIndex += runLen;
40 }
41 sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
42 paint.reset();
43 canvas->drawTextBlob(blob.get(), 0, 0, paint);
44 paint.setStyle(SkPaint::kStroke_Style);
Cary Clarkd2ca79c2018-08-10 13:09:13 -040045 canvas->drawRect(blob->bounds(), paint);
46##
47
48#SeeAlso SkPath::getBounds
49
50#Method ##
51
52# ------------------------------------------------------------------------------
53
54#Method uint32_t uniqueID() const
55#In Property
56#Line # returns identifier for Text_Blob ##
Cary Clark09d80c02018-10-31 12:14:03 -040057#Populate
Cary Clarkd2ca79c2018-08-10 13:09:13 -040058
59#Example
Cary Clark14768f62018-10-29 20:33:51 -040060for (int index = 0; index < 2; ++index) {
61 SkTextBlobBuilder textBlobBuilder;
62 const char bunny[] = "/(^x^)\\";
63 const int len = sizeof(bunny) - 1;
64 uint16_t glyphs[len];
65 SkPaint paint;
66 paint.textToGlyphs(bunny, len, glyphs);
67 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
68 paint.setTextScaleX(0.5);
69 int runs[] = { 3, 1, 3 };
70 SkPoint textPos = { 20, 50 };
71 int glyphIndex = 0;
72 for (auto runLen : runs) {
73 paint.setTextSize(1 == runLen ? 20 : 50);
74 const SkTextBlobBuilder::RunBuffer& run =
75 textBlobBuilder.allocRun(paint, runLen, textPos.fX, textPos.fY);
76 memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen);
77 textPos.fX += paint.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen, nullptr);
78 glyphIndex += runLen;
79 }
80 sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
81 paint.reset();
82 canvas->drawTextBlob(blob.get(), 0, 0, paint);
83 std::string id = "unique ID:" + std::to_string(blob->uniqueID());
84 canvas->drawString(id.c_str(), 30, blob->bounds().fBottom + 15, paint);
85 canvas->translate(blob->bounds().fRight + 10, 0);
Cary Clarkd2ca79c2018-08-10 13:09:13 -040086}
87##
88
89#SeeAlso SkRefCnt
90
91#Method ##
92
93# ------------------------------------------------------------------------------
94
Cary Clark14768f62018-10-29 20:33:51 -040095#Method static sk_sp<SkTextBlob> MakeFromText(const void* text, size_t byteLength, const SkFont& font,
96 SkPaint::TextEncoding encoding = SkPaint::kUTF8_TextEncoding)
Cary Clark61313f32018-10-08 14:57:48 -040097#In Constructors
Cary Clarkd2ca79c2018-08-10 13:09:13 -040098#Line # constructs Text_Blob with one run ##
99
100Creates Text_Blob with a single run. text meaning depends on Paint_Text_Encoding;
101by default, text is encoded as UTF-8.
102
Cary Clark14768f62018-10-29 20:33:51 -0400103font contains attributes used to define the run text: #paint_font_metrics#.
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400104
105#Param text character code points or Glyphs drawn ##
106#Param byteLength byte length of text array ##
Cary Clark14768f62018-10-29 20:33:51 -0400107#Param font text size, typeface, text scale, and so on, used to draw ##
108#Param encoding one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
109 kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
110##
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400111
112#Return Text_Blob constructed from one run ##
113
114#Example
115#Height 24
Cary Clark14768f62018-10-29 20:33:51 -0400116 SkFont font;
117 font.setSize(24);
118 SkPaint canvasPaint;
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400119 canvasPaint.setColor(SK_ColorBLUE); // respected
120 canvasPaint.setTextSize(2); // ignored
Cary Clark14768f62018-10-29 20:33:51 -0400121 sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, font);
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400122 canvas->drawTextBlob(blob, 20, 20, canvasPaint);
123##
124
Cary Clark77b3f3a2018-11-07 14:59:03 -0500125#SeeAlso MakeFromString SkTextBlobBuilder
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400126
127##
128
129# ------------------------------------------------------------------------------
130
Cary Clark14768f62018-10-29 20:33:51 -0400131#Method static sk_sp<SkTextBlob> MakeFromString(const char* string, const SkFont& font,
132 SkPaint::TextEncoding encoding = SkPaint::kUTF8_TextEncoding)
Cary Clark61313f32018-10-08 14:57:48 -0400133#In Constructors
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400134#Line # constructs Text_Blob with one run ##
135
136Creates Text_Blob with a single run. string meaning depends on Paint_Text_Encoding;
137by default, string is encoded as UTF-8.
138
Cary Clark14768f62018-10-29 20:33:51 -0400139font contains Paint_Font_Metrics used to define the run text: #paint_font_metrics#.
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400140
141#Param string character code points or Glyphs drawn ##
Cary Clark14768f62018-10-29 20:33:51 -0400142#Param font text size, typeface, text scale, and so on, used to draw ##
143#Param encoding one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
144 kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
145##
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400146
147#Return Text_Blob constructed from one run ##
148
149#Example
150#Height 24
Cary Clark14768f62018-10-29 20:33:51 -0400151 SkFont font;
152 font.setSize(24);
153 SkPaint canvasPaint;
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400154 canvasPaint.setColor(SK_ColorBLUE); // respected
155 canvasPaint.setTextSize(2); // ignored
Cary Clark14768f62018-10-29 20:33:51 -0400156 sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString("Hello World", font);
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400157 canvas->drawTextBlob(blob, 20, 20, canvasPaint);
158##
159
Cary Clark77b3f3a2018-11-07 14:59:03 -0500160#SeeAlso MakeFromText SkTextBlobBuilder
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400161
162##
163
164# ------------------------------------------------------------------------------
165
166#Method size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const
167#In Utility
168#Line # writes Text_Blob to memory ##
Cary Clark09d80c02018-10-31 12:14:03 -0400169#Populate
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400170
171#Example
172#Height 64
173###$
174$Function
175#include "SkSerialProcs.h"
176$$
177$$$#
Cary Clark09d80c02018-10-31 12:14:03 -0400178 SkFont blobFont;
179 blobFont.setSize(24);
180 sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, blobFont);
Cary Clark14768f62018-10-29 20:33:51 -0400181 char storage[2048];
182 size_t used = blob->serialize(SkSerialProcs(), storage, sizeof(storage));
183 sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(storage, used, SkDeserialProcs());
184 canvas->drawTextBlob(copy, 20, 20, SkPaint());
185 std::string usage = "size=" + std::to_string(sizeof(storage)) + " used=" + std::to_string(used);
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400186 canvas->drawString(usage.c_str(), 20, 40, SkPaint());
187##
188
189#SeeAlso Deserialize SkSerialProcs
190
191#Method ##
192
193# ------------------------------------------------------------------------------
194
195#Method sk_sp<SkData> serialize(const SkSerialProcs& procs) const
196#In Utility
197#Line # writes Text_Blob to Data ##
Cary Clark09d80c02018-10-31 12:14:03 -0400198#Populate
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400199
200#Example
201#Height 24
202###$
203$Function
204#include "SkSerialProcs.h"
205$$
206$$$#
Cary Clark09d80c02018-10-31 12:14:03 -0400207 SkFont blobFont;
208 blobFont.setSize(24);
Cary Clark14768f62018-10-29 20:33:51 -0400209 sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, blobFont);
210 sk_sp<SkData> data = blob->serialize(SkSerialProcs());
211 sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(data->data(), data->size(), SkDeserialProcs());
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400212 canvas->drawTextBlob(copy, 20, 20, SkPaint());
213##
214
215#SeeAlso Deserialize SkData SkSerialProcs
216
217#Method ##
218
219# ------------------------------------------------------------------------------
220
221#Method static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs& procs)
Cary Clark61313f32018-10-08 14:57:48 -0400222#In Constructors
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400223#Line # constructs Text_Blob from memory ##
Cary Clark09d80c02018-10-31 12:14:03 -0400224#Populate
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400225
226#Example
227#Height 24
228#Description
229Text "Hacker" replaces "World!", but does not update its metrics.
230When drawn, "Hacker" uses the spacing computed for "World!".
231##
232###$
233$Function
234#include "SkSerialProcs.h"
235$$
236$$$#
Cary Clark09d80c02018-10-31 12:14:03 -0400237 SkFont blobFont;
238 blobFont.setSize(24);
239 sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World!", 12, blobFont);
240 sk_sp<SkData> data = blob->serialize(SkSerialProcs());
241 uint16_t glyphs[6];
242 SkPaint blobPaint;
Cary Clark14768f62018-10-29 20:33:51 -0400243 blobPaint.textToGlyphs("Hacker", 6, glyphs);
244 memcpy((char*)data->writable_data() + 0x54, glyphs, sizeof(glyphs));
245 sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(data->data(), data->size(), SkDeserialProcs());
Cary Clarkd2ca79c2018-08-10 13:09:13 -0400246 canvas->drawTextBlob(copy, 20, 20, SkPaint());
247##
248
249#SeeAlso serialize SkDeserialProcs
250
251#Method ##
252
253#Class SkTextBlob ##
254
255#Topic Text_Blob ##