skia-bindings 0.97.2

Skia Bindings for Rust
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/// Skia skparagraph Module C Wrapper Functions

#include "bindings.h"

#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/FontCollection.h"
#include "modules/skparagraph/include/Metrics.h"
#include "modules/skparagraph/include/ParagraphCache.h"
#include "modules/skparagraph/include/Paragraph.h"
#include "modules/skparagraph/include/ParagraphBuilder.h"
#include "modules/skparagraph/include/ParagraphStyle.h"
#include "modules/skparagraph/include/TextShadow.h"
#include "modules/skparagraph/include/TextStyle.h"
#include "modules/skparagraph/include/TypefaceFontProvider.h"

#include "modules/skunicode/include/SkUnicode_icu.h"

// m84: needs definition of SkFontData
#include "src/core/SkFontDescriptor.h"

#include <optional>

using namespace skia::textlayout;

//
// FontArguments.h
//

extern "C" {
    void C_FontArguments_Construct(const SkFontArguments* fontArguments, FontArguments* uninitialized) {
        new (uninitialized) FontArguments(*fontArguments);
    }

    void C_FontArguments_CopyConstruct(FontArguments* uninitialized, const FontArguments* self) {
        new (uninitialized) FontArguments(*self);
    }

    void C_FontArguments_destruct(FontArguments* self) {
        self->~FontArguments();
    }

    bool C_FontArguments_Equals(const FontArguments* lhs, const FontArguments* rhs) {
        return *lhs == *rhs;
    }

    size_t C_FontArguments_hash(const FontArguments* self) {
        return std::hash<FontArguments>{}(*self);
    }

    SkTypeface* C_FontArguments_cloneTypeface(const FontArguments* self, SkTypeface* tf) {
        return self->CloneTypeface(sp(tf)).release();
    }
}

//
// FontCollection.h
//

extern "C" {
    FontCollection* C_FontCollection_new() {
        return new FontCollection();
    }

    void C_FontCollection_setAssetFontManager(FontCollection* self, const SkFontMgr* fontManager) {
        self->setAssetFontManager(spFromConst(fontManager));
    }

    void C_FontCollection_setDynamicFontManager(FontCollection* self, const SkFontMgr* fontManager) {
        self->setDynamicFontManager(spFromConst(fontManager));
    }

    void C_FontCollection_setTestFontManager(FontCollection* self, const SkFontMgr* fontManager) {
        self->setTestFontManager(spFromConst(fontManager));
    }

    void C_FontCollection_setDefaultFontManager(FontCollection* self, const SkFontMgr* fontManager) {
        self->setDefaultFontManager(spFromConst(fontManager));
    }

    void C_FontCollection_setDefaultFontManager2(FontCollection* self, const SkFontMgr* fontManager, const char* defaultFamilyName) {
        self->setDefaultFontManager(spFromConst(fontManager), defaultFamilyName);
    }

    void C_FontCollection_setDefaultFontManager3(FontCollection* self, const SkFontMgr* fontManager, const SkStrings* familyNames) {
        self->setDefaultFontManager(spFromConst(fontManager), familyNames->strings);
    }

    SkFontMgr* C_FontCollection_getFallbackManager(const FontCollection* self) {
        return self->getFallbackManager().release();
    }

    void C_FontCollection_findTypefaces(
        FontCollection* self, 
        const SkStrings* familyNames, 
        SkFontStyle fontStyle,
        const FontArguments* fontArguments,
        VecSink<sk_sp<SkTypeface>>* typefaces) {
        // TODO: Don't create a copy of `fontArguments`.
        auto fa = fontArguments ? std::optional(*fontArguments) : std::nullopt;
        auto tfs = self->findTypefaces(familyNames->strings, fontStyle, fa);
        typefaces->set(tfs);
    }

    SkTypeface* C_FontCollection_defaultFallback(FontCollection* self, SkUnichar unicode, const SkStrings* familyNames, SkFontStyle fontStyle, const SkString* locale, const FontArguments* arguments) {
        return self->defaultFallback(unicode, familyNames->strings, fontStyle, *locale, arguments ? std::make_optional(*arguments) : std::nullopt).release();
    }


    SkTypeface* C_FontCollection_defaultFallback2(FontCollection* self) {
        return self->defaultFallback().release();
    }

    SkTypeface* C_FontCollection_defaultEmojiFallback(FontCollection* self, SkUnichar emojiStart, SkFontStyle fontStyle, const SkString* locale) {
        return self->defaultEmojiFallback(emojiStart, fontStyle, *locale).release();
    }

    bool C_FontCollection_fontFallbackEnabled(const FontCollection* self) {
        return const_cast<FontCollection*>(self)->fontFallbackEnabled();
    }

    ParagraphCache* C_FontCollection_paragraphCache(FontCollection* self) {
        return self->getParagraphCache();
    }
}

//
// ParagraphCache.h
//

extern "C" {
    void C_ParagraphCache_destruct(ParagraphCache* self) {
        self->~ParagraphCache();
    }

    void C_ParagraphCache_turnOn(ParagraphCache* self, bool value) {
        self->turnOn(value);
    }

    int C_ParagraphCache_count(ParagraphCache* self) {
        return self->count();
    }
}

//
// ParagraphStyle.h
//

extern "C" {
    void C_StrutStyle_Construct(StrutStyle* uninitialized) {
        new(uninitialized) StrutStyle();
    }

    void C_StrutStyle_CopyConstruct(StrutStyle* uninitialized, const StrutStyle* other) {
        new(uninitialized) StrutStyle(*other);
    }

    void C_StrutStyle_destruct(StrutStyle* self) {
        self->~StrutStyle();
    }

    const SkString* C_StrutStyle_getFontFamilies(const StrutStyle* self, size_t* count) {
        auto& v = self->getFontFamilies();
        *count = v.size();
        return v.data();
    }

    void C_StrutStyle_setFontFamilies(StrutStyle* self, const SkString* data, size_t count) {
        self->setFontFamilies(std::vector<SkString>(data, data + count));
    }

    bool C_StrutStyle_equals(const StrutStyle* self, const StrutStyle* rhs) {
        return *self == *rhs;
    }
}

extern "C" {
    ParagraphStyle* C_ParagraphStyle_new() {
        return new ParagraphStyle();
    }

    ParagraphStyle* C_ParagraphStyle_newCopy(const ParagraphStyle* other) {
        return new ParagraphStyle(*other);
    }

    void C_ParagraphStyle_delete(ParagraphStyle* self) {
        delete self;
    }

    bool C_ParagraphStyle_Equals(const ParagraphStyle* left, const ParagraphStyle* right) {
        return *left == *right;
    }

    bool C_ParagraphStyle_ellipsized(const ParagraphStyle* self) {
        return self->ellipsized();
    }
}

//
// TextShadow.h
//

extern "C" {
    bool C_TextShadow_Equals(const TextShadow* self, const TextShadow* other) {
        return *self == *other;
    }
}

//
// Metrics.h
//

extern "C" {
    size_t C_LineMetrics_styleMetricsCount(const LineMetrics* self) {
        return self->fLineMetrics.size();
    }

    struct IndexedStyleMetrics {
        size_t index;
        StyleMetrics metrics;
    };

    void C_LineMetrics_getAllStyleMetrics(const LineMetrics* self, IndexedStyleMetrics* result) {
        auto begin = self->fLineMetrics.begin();
        auto end = self->fLineMetrics.end();

        for (auto it = begin; it != end; ++it) {
            *result++ = IndexedStyleMetrics { it->first, it->second };
        }
    }
}

//
// Paragraph.h
//

extern "C" {
    void C_Paragraph_delete(Paragraph* self) {
        delete self;
    }
    
    void C_Paragraph_layout(Paragraph* self, SkScalar width) {
        self->layout(width);
    }

    void C_Paragraph_paint(Paragraph* self, SkCanvas* canvas, SkScalar x, SkScalar y) {
        self->paint(canvas, x, y);
    }

    void C_Paragraph_getRectsForRange(Paragraph *self, unsigned start, unsigned end, RectHeightStyle rectHeightStyle,
                                            RectWidthStyle rectWidthStyle, VecSink<TextBox>* textBoxes) {
        auto v = self->getRectsForRange(start, end, rectHeightStyle, rectWidthStyle);
        textBoxes->set(v);
    }

    void C_Paragraph_getRectsForPlaceholders(Paragraph* self, VecSink<TextBox>* result) {
        auto v = self->getRectsForPlaceholders();
        result->set(v);
    }

    void C_Paragraph_getGlyphPositionAtCoordinate(Paragraph* self, SkScalar x, SkScalar y, PositionWithAffinity* position) {
        *position = self->getGlyphPositionAtCoordinate(x, y);
    }

    void C_Paragraph_getWordBoundary(Paragraph* self, unsigned offset, size_t range[2]) {
        auto sk_range = self->getWordBoundary(offset);
        range[0] = sk_range.start;
        range[1] = sk_range.end;
    }

    void C_Paragraph_getLineMetrics(Paragraph* self, VecSink<LineMetrics>* result) {
        std::vector<LineMetrics> vec;
        self->getLineMetrics(vec);
        result->set(vec);
    }

    size_t C_Paragraph_lineNumber(Paragraph* self) {
        return self->lineNumber();
    }

    void C_Paragraph_markDirty(Paragraph* self) {
        self->markDirty();
    }

    int32_t C_Paragraph_unresolvedGlyphs(Paragraph* self) {
        return self->unresolvedGlyphs();
    }

    void C_Paragraph_unresolvedCodepoints(Paragraph* self, VecSink<SkUnichar>* result) {
        auto set = self->unresolvedCodepoints();
        std::vector<SkUnichar> vec(set.begin(), set.end());
        result->set(vec);
    }

    void C_Paragraph_visit(Paragraph* self, void* ctx, void (*visit)(void *, size_t, const Paragraph::VisitorInfo *)) {
        auto visitFn = [ctx,visit](int i, const Paragraph::VisitorInfo *info_)
        {
            visit(ctx, i, info_);
        };
        self->visit(visitFn);
    }

    void C_Paragraph_extendedVisit(Paragraph* self, void* ctx, void (*visit)(void *, size_t, const Paragraph::ExtendedVisitorInfo *)) {
        auto visitFn = [ctx,visit](int i, const Paragraph::ExtendedVisitorInfo *info_)
        {
            visit(ctx, i, info_);
        };
        self->extendedVisit(visitFn);
    }

    int C_Paragraph_getPath(Paragraph* self, int lineNumber, SkPath* path) {
        return self->getPath(lineNumber, path);
    }

    void C_Paragraph_GetPath(SkTextBlob* textBlob, SkPath* uninitialized) {
        new (uninitialized) SkPath(Paragraph::GetPath(textBlob));
    }

    bool C_Paragraph_containsEmoji(Paragraph* self, SkTextBlob* textBlob) {
        return self->containsEmoji(textBlob);
    }

    bool C_Paragraph_containsColorFontOrBitmap(Paragraph* self, SkTextBlob* textBlob) {
        return self->containsColorFontOrBitmap(textBlob);
    }

    int C_Paragraph_getLineNumberAt(const Paragraph* self, TextIndex codeUnitIndex) {
        return self->getLineNumberAt(codeUnitIndex);
    }

    int C_Paragraph_getLineNumberAtUTF16Offset(Paragraph* self, size_t codeUnitIndex) {
        return self->getLineNumberAtUTF16Offset(codeUnitIndex);
    }

    void C_Paragraph_getLineMetricsAt(const Paragraph* self, size_t lineNumber, Sink<LineMetrics>* lineMetrics) {
        LineMetrics lm;
        if (self->getLineMetricsAt(lineNumber, &lm)) {
            lineMetrics->set(lm);
        }
    }

    void C_Paragraph_getActualTextRange(const Paragraph* self, size_t lineNumber, bool includeSpaces, size_t r[2]) {
        auto range = self->getActualTextRange(lineNumber, includeSpaces);
        r[0] = range.start;
        r[1] = range.end;
    }

    void C_Paragraph_getGlyphClusterAt(const Paragraph* self, TextIndex codeUnitIndex, Sink<Paragraph::GlyphClusterInfo>* r) {
        Paragraph::GlyphClusterInfo gci;
        // Most likely const, implementation does not seem to mutate Paragraph.
        if (const_cast<Paragraph*>(self)->getGlyphClusterAt(codeUnitIndex, &gci)) {
            r->set(gci);
        }
    }

    void C_Paragraph_getClosestGlyphClusterAt(const Paragraph* self, SkScalar dx, SkScalar dy, Sink<Paragraph::GlyphClusterInfo>* r) {
        Paragraph::GlyphClusterInfo gci;
        // Most likely const, implementation does not seem to mutate Paragraph.
        if (const_cast<Paragraph*>(self)->getClosestGlyphClusterAt(dx, dy, &gci)) {
            r->set(gci);
        }
    }

    bool C_Paragraph_getGlyphInfoAtUTF16Offset(Paragraph* self, size_t codeUnitIndex, Paragraph::GlyphInfo* uninitialized) {
        Paragraph::GlyphInfo gi;
        if (self->getGlyphInfoAtUTF16Offset(codeUnitIndex, &gi)) {
            new (uninitialized) Paragraph::GlyphInfo(gi);
            return true;
        }
        return false;
    }

    bool C_Paragraph_getClosestUTF16GlyphInfoAt(Paragraph* self, SkScalar dx, SkScalar dy, Paragraph::GlyphInfo* uninitialized) {
        Paragraph::GlyphInfo gi;
        if (self->getClosestUTF16GlyphInfoAt(dx, dy, &gi)) {
            new (uninitialized) Paragraph::GlyphInfo(gi);
            return true;
        }
        return false;
    }

    void C_Paragraph_getFontAt(const Paragraph* self, TextIndex codeUnitIndex, SkFont* uninitialized) {
        new (uninitialized) SkFont(self->getFontAt(codeUnitIndex));
    }

    void C_Paragraph_getFontAtUTF16Offset(Paragraph* self, size_t codeUnitIndex, SkFont* uninitialized) {
        new (uninitialized) SkFont(self->getFontAtUTF16Offset(codeUnitIndex));
    }

    void C_Paragraph_getFonts(const Paragraph* self, VecSink<Paragraph::FontInfo>* r) {
        auto fonts = self->getFonts();
        r->set(fonts);
    }
}

//
// ParagraphBuilder.h
//

extern "C" {
    void C_ParagraphBuilder_delete(ParagraphBuilder* self) {
        delete self;
    }

    void C_ParagraphBuilder_pushStyle(ParagraphBuilder* self, const TextStyle* style) {
        self->pushStyle(*style);
    }

    void C_ParagraphBuilder_pop(ParagraphBuilder* self) {
        self->pop();
    }

    void C_ParagraphBuilder_peekStyle(ParagraphBuilder* self, TextStyle* style) {
        *style = self->peekStyle();
    }

    void C_ParagraphBuilder_addText(ParagraphBuilder* self, const char* text, size_t len) {
        self->addText(text, len);
    }

    void C_ParagraphBuilder_addPlaceholder(ParagraphBuilder* self, const PlaceholderStyle* placeholderStyle) {
        self->addPlaceholder(*placeholderStyle);
    }

    Paragraph* C_ParagraphBuilder_Build(ParagraphBuilder* self) {
        return self->Build().release();
    }

    void C_ParagraphBuilder_getText(ParagraphBuilder* self, char** text, size_t* len) {
        auto span = self->getText();
        *text = span.data();
        *len = span.size();
    }

    ParagraphStyle* C_ParagraphBuilder_getParagraphStyle(const ParagraphBuilder* self) {
        return new ParagraphStyle(self->getParagraphStyle());
    }

    void C_ParagraphBuilder_Reset(ParagraphBuilder* self) {
        return self->Reset();
    }

    ParagraphBuilder* C_ParagraphBuilder_make(const ParagraphStyle* style, const FontCollection* fontCollection) {
        auto unicode = SkUnicodes::ICU::Make();
        if (!unicode) {
            return nullptr;
        }

        return ParagraphBuilder::make(*style, spFromConst(fontCollection), unicode).release();
    }
}

//
// TextStyle.h
//

extern "C" {
    void C_TextStyle_Types(const Block*, const Placeholder*) {}

    void C_FontFeature_CopyConstruct(FontFeature* uninitialized, const FontFeature* other) {
        new(uninitialized) FontFeature(*other);
    }

    void C_FontFeature_destruct(FontFeature* self) {
        self->~FontFeature();
    }

    void C_TextStyle_Construct(TextStyle* uninitialized) {
        new(uninitialized) TextStyle();
    }

    void C_TextStyle_CopyConstruct(TextStyle* uninitialized, const TextStyle* other) {
        new(uninitialized) TextStyle(*other);
    }

    void C_TextStyle_cloneForPlaceholder(const TextStyle* self, TextStyle* uninitialized) {
        // m102: We assume that they just forgot to mark `TextStyle::cloneForPlaceholder` as const.
        new (uninitialized) TextStyle(const_cast<TextStyle*>(self)->cloneForPlaceholder());
    }

    void C_TextStyle_destruct(TextStyle* self) {
        self->~TextStyle();
    }

    void C_TextStyle_getForeground(const TextStyle* self, SkPaint* uninitialized) {
        new (uninitialized) SkPaint(self->getForeground());
    }

    void C_TextStyle_setForegroundPaint(TextStyle* self, const SkPaint* paint) {
        self->setForegroundPaint(*paint);
    }

    void C_TextStyle_getBackground(const TextStyle* self, SkPaint* uninitialized) {
        new (uninitialized) SkPaint(self->getBackground());
    }

    void C_TextStyle_setBackgroundPaint(TextStyle* self, const SkPaint* paint) {
        self->setBackgroundColor(*paint);
    }

    const TextShadow* C_TextStyle_getShadows(const std::vector<TextShadow>* self, size_t* len_ref) {
        auto len = self->size();
        *len_ref = len;
        return len ? self->data() : nullptr;
    }

    void C_TextStyle_addShadow(TextStyle* self, const TextShadow* shadow) {
        self->addShadow(*shadow);
    }

    void C_TextStyle_resetShadows(TextStyle* self) {
        self->resetShadows();
    }

    const FontFeature* C_TextStyle_getFontFeatures(const std::vector<FontFeature>* self, size_t* len_ref) {
        auto size = self->size();
        *len_ref = size;
        return size ? self->data() : nullptr;
    }

    void C_TextStyle_addFontFeature(TextStyle* self, const SkString* fontFeature, int value) {
        self->addFontFeature(*fontFeature, value);
    }

    void C_TextStyle_resetFontFeatures(TextStyle* self) {
        self->resetFontFeatures();
    }

    const FontArguments* C_TextStyle_getFontArguments(const TextStyle* self) {
        auto& fontArguments = self->getFontArguments();
        return fontArguments ? &*fontArguments : nullptr;
    }

    void C_TextStyle_setFontArguments(TextStyle* self, const SkFontArguments* arguments) {
        self->setFontArguments(arguments ? std::optional(*arguments) : std::nullopt);
    }

    const SkString* C_TextStyle_getFontFamilies(const TextStyle* self, size_t* count) {
        auto& v = self->getFontFamilies();
        *count = v.size();
        return v.data();
    }

    void C_TextStyle_setFontFamilies(TextStyle* self, const SkString* data, size_t count) {
        self->setFontFamilies(std::vector<SkString>(data, data + count));
    }

    void C_TextStyle_setTypeface(TextStyle* self, SkTypeface* typeface) {
        self->setTypeface(sk_sp<SkTypeface>(typeface));
    }
}

//
// TypefaceFontProvider
//

extern "C" {
    TypefaceFontStyleSet* C_TypefaceFontStyleSet_new(const SkString* family_name) {
        return new TypefaceFontStyleSet(*family_name);
    }

    void C_TypefaceFontStyleSet_appendTypeface(TypefaceFontStyleSet* self, SkTypeface* typeface) {
        self->appendTypeface(sk_sp<SkTypeface>(typeface));
    }
}

extern "C" {
    TypefaceFontProvider* C_TypefaceFontProvider_new() {
        return new TypefaceFontProvider();
    }

    size_t C_TypefaceFontProvider_registerTypeface(TypefaceFontProvider* self, SkTypeface* typeface, const SkString* alias) {
        if (alias) {
            return self->registerTypeface(sk_sp<SkTypeface>(typeface), *alias);
        }
        return self->registerTypeface(sk_sp<SkTypeface>(typeface));
    }
}