huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#Topic Text_Blob
#Alias Text_Blob_Reference ##
 
#Class SkTextBlob
 
#Code
#Populate
##
 
SkTextBlob combines multiple text runs into an immutable container. Each text
run consists of Glyphs, Paint, and position. Only parts of Paint related to
fonts and text rendering are used by run.
 
# ------------------------------------------------------------------------------
 
#Method const SkRect& bounds() const
#In Property
#Line # returns conservative bounding box ##
#Populate
 
#Example
#Height 70
    SkTextBlobBuilder textBlobBuilder;
    const char bunny[] = "/(^x^)\\";
    const int len = sizeof(bunny) - 1;
    uint16_t glyphs[len];
    SkFont font;
    font.textToGlyphs(bunny, len, SkTextEncoding::kUTF8, glyphs, sizeof(glyphs));
    int runs[] = { 3, 1, 3 };
    SkPoint textPos = { 20, 50 };
    int glyphIndex = 0;
    for (auto runLen : runs) {
        font.setSize(1 == runLen ? 20 : 50);
        const SkTextBlobBuilder::RunBuffer& run =
                textBlobBuilder.allocRun(font, runLen, textPos.fX, textPos.fY);
        memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen);
        textPos.fX += font.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen, 
                SkTextEncoding::kGlyphID);
        glyphIndex += runLen;
    }
    sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
    SkPaint paint;
    canvas->drawTextBlob(blob.get(), 0, 0, paint);
    paint.setStyle(SkPaint::kStroke_Style);
    canvas->drawRect(blob->bounds(), paint);
##
 
#SeeAlso SkPath::getBounds
 
#Method ##
 
# ------------------------------------------------------------------------------
 
#Method uint32_t uniqueID() const
#In Property
#Line # returns identifier for Text_Blob ##
#Populate
 
#Example
for (int index = 0; index < 2; ++index) {
    SkTextBlobBuilder textBlobBuilder;
    const char bunny[] = "/(^x^)\\";
    const int len = sizeof(bunny) - 1;
    uint16_t glyphs[len];
    SkFont font;
    font.textToGlyphs(bunny, len, SkTextEncoding::kUTF8, glyphs, sizeof(glyphs));
    font.setScaleX(0.5);
    int runs[] = { 3, 1, 3 };
    SkPoint textPos = { 20, 50 };
    int glyphIndex = 0;
    for (auto runLen : runs) {
        font.setSize(1 == runLen ? 20 : 50);
        const SkTextBlobBuilder::RunBuffer& run =
                textBlobBuilder.allocRun(font, runLen, textPos.fX, textPos.fY);
        memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen);
        textPos.fX += font.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen,
                SkTextEncoding::kGlyphID);
        glyphIndex += runLen;
    }
    sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
    SkPaint paint;
    canvas->drawTextBlob(blob.get(), 0, 0, paint);
    std::string id = "unique ID:" + std::to_string(blob->uniqueID());
    canvas->drawString(id.c_str(), 30, blob->bounds().fBottom + 15, paint);
    canvas->translate(blob->bounds().fRight + 10, 0);
}
##
 
#SeeAlso SkRefCnt
 
#Method ##
 
# ------------------------------------------------------------------------------
 
#Subtopic Text_Intercepts
#Line # advanced underline, strike through ##
 
Text_Intercepts describe the intersection of drawn text Glyphs with a pair
of lines parallel to the text advance. Text_Intercepts permits creating a
underline that skips Descenders.
 
#Method int getIntercepts(const SkScalar bounds[2], SkScalar intervals[],
                      const SkPaint* paint = nullptr) const;
#In Text_Intercepts
#Line # returns where lines intersect Text_Blob; underlines ##
#Populate
 
#Example
#Height 143
    void draw(SkCanvas* canvas) {
        SkFont font;
        font.setSize(120);
        SkPoint textPos = { 20, 110 };
        int len = 3;
        SkTextBlobBuilder textBlobBuilder;
        const SkTextBlobBuilder::RunBuffer& run =
                textBlobBuilder.allocRun(font, len, textPos.fX, textPos.fY);
        run.glyphs[0] = 10;
        run.glyphs[1] = 20;
        run.glyphs[2] = 30;
        sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
        SkPaint paint;
        SkScalar bounds[] = { 116, 134 };
        int count = blob->getIntercepts(bounds, nullptr);
        std::vector<SkScalar> intervals;
        intervals.resize(count);
        (void) paint.getTextBlobIntercepts(blob.get(), bounds, &intervals.front());
        canvas->drawTextBlob(blob.get(), 0, 0, paint);
        paint.setColor(0xFFFF7777);
        SkScalar x = textPos.fX;
        for (int i = 0; i < count; i+= 2) {
            canvas->drawRect({x, bounds[0], intervals[i], bounds[1]}, paint);
            x = intervals[i + 1];
        }
        canvas->drawRect({intervals[count - 1], bounds[0], 180, bounds[1]}, paint);
    }
##
 
#Method ##
 
#Subtopic Text_Intercepts ##
 
# ------------------------------------------------------------------------------
 
#Method static sk_sp<SkTextBlob> MakeFromText(const void* text, size_t byteLength, const SkFont& font,
                                          SkTextEncoding encoding = kUTF8_SkTextEncoding)
#In Constructors
#Line # constructs Text_Blob with one run ##
 
Creates Text_Blob with a single run. text meaning depends on Text_Encoding;
by default, text is encoded as UTF-8.
 
font contains attributes used to define the run text: #font_metrics#.
 
#Param text character code points or Glyphs drawn ##
#Param  byteLength   byte length of text array ##
#Param  font    text size, typeface, text scale, and so on, used to draw ##
#Param  encoding  one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
                          kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
##
 
#Return Text_Blob constructed from one run ##
 
#Example
#Height 24
    SkFont font;
    font.setSize(24);
    SkPaint canvasPaint;
    canvasPaint.setColor(SK_ColorBLUE); // respected
    canvasPaint.setTextSize(2); // ignored
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, font);
    canvas->drawTextBlob(blob, 20, 20, canvasPaint);
##
 
#SeeAlso MakeFromString SkTextBlobBuilder
 
##
 
# ------------------------------------------------------------------------------
 
#Method static sk_sp<SkTextBlob> MakeFromString(const char* string, const SkFont& font,
             SkTextEncoding encoding = kUTF8_SkTextEncoding)
#In Constructors
#Line # constructs Text_Blob with one run ##
 
Creates Text_Blob with a single run. string meaning depends on Text_Encoding;
by default, string is encoded as UTF-8.
 
font contains Font_Metrics used to define the run text: #font_metrics#.
 
#Param string character code points or Glyphs drawn ##
#Param  font    text size, typeface, text scale, and so on, used to draw ##
#Param  encoding  one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
                          kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
##
 
#Return Text_Blob constructed from one run ##
 
#Example
#Height 24
    SkFont font;
    font.setSize(24);
    SkPaint canvasPaint;
    canvasPaint.setColor(SK_ColorBLUE); // respected
    canvasPaint.setTextSize(2); // ignored
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString("Hello World", font);
    canvas->drawTextBlob(blob, 20, 20, canvasPaint);
##
 
#SeeAlso MakeFromText SkTextBlobBuilder
 
##
 
# ------------------------------------------------------------------------------
 
#Method size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const
#In Utility
#Line # writes Text_Blob to memory ##
#Populate
 
#Example
#Height 64
###$
$Function
#include "SkSerialProcs.h"
$$
$$$#
    SkFont blobFont;
    blobFont.setSize(24);
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, blobFont);
    char storage[2048];
    size_t used = blob->serialize(SkSerialProcs(), storage, sizeof(storage));
    sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(storage, used, SkDeserialProcs());
    canvas->drawTextBlob(copy, 20, 20, SkPaint());
    std::string usage = "size=" + std::to_string(sizeof(storage)) + " used=" + std::to_string(used);
    canvas->drawString(usage.c_str(), 20, 40, SkPaint());
##
 
#SeeAlso Deserialize SkSerialProcs
 
#Method ##
 
# ------------------------------------------------------------------------------
 
#Method sk_sp<SkData> serialize(const SkSerialProcs& procs) const
#In Utility
#Line # writes Text_Blob to Data ##
#Populate
 
#Example
#Height 24
###$
$Function
#include "SkSerialProcs.h"
$$
$$$#
    SkFont blobFont;
    blobFont.setSize(24);
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, blobFont);
    sk_sp<SkData> data = blob->serialize(SkSerialProcs());
    sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(data->data(), data->size(), SkDeserialProcs());
    canvas->drawTextBlob(copy, 20, 20, SkPaint());
##
 
#SeeAlso Deserialize SkData SkSerialProcs
 
#Method ##
 
# ------------------------------------------------------------------------------
 
#Method static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs& procs)
#In Constructors
#Line # constructs Text_Blob from memory ##
#Populate
 
#Example
#Height 24
#Description
Text "Hacker" replaces "World!", but does not update its metrics.
When drawn, "Hacker" uses the spacing computed for "World!".
##
###$
$Function
#include "SkSerialProcs.h"
$$
$$$#
    SkFont blobFont;
    blobFont.setSize(24);
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World!", 12, blobFont);
    sk_sp<SkData> data = blob->serialize(SkSerialProcs());
    uint16_t glyphs[6];
    SkPaint blobPaint;
    blobPaint.textToGlyphs("Hacker", 6, glyphs);
    memcpy((char*)data->writable_data() + 0x54, glyphs, sizeof(glyphs));
    sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(data->data(), data->size(), SkDeserialProcs());
    canvas->drawTextBlob(copy, 20, 20, SkPaint());
##
 
#SeeAlso serialize SkDeserialProcs
 
#Method ##
 
#Class SkTextBlob ##
 
#Topic Text_Blob ##