Flutter Engine
The Flutter Engine
fonts.cpp
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "export.h"
10#include "wrappers.h"
11
12#include <memory>
13
14using namespace skia::textlayout;
15using namespace Skwasm;
16
18 auto collection = sk_make_sp<FontCollection>();
19 auto provider = sk_make_sp<TypefaceFontProvider>();
20 collection->enableFontFallback();
21 collection->setDefaultFontManager(provider, "Roboto");
22 return new FlutterFontCollection{
23 std::move(collection),
24 std::move(provider),
25 };
26}
27
29 delete collection;
30}
31
34 return mgr;
35}
36
38 auto typeface = default_fontmgr()->makeFromData(sk_ref_sp<SkData>(fontData));
39 return typeface.release();
40}
41
43 typeface->unref();
44}
45
46// Calculates the code points that are not covered by the specified typefaces.
47// This function mutates the `codePoints` buffer in place and returns the count
48// of code points that are not covered by the fonts.
50 int typefaceCount,
51 SkUnichar* codePoints,
52 int codePointCount) {
53 std::unique_ptr<SkGlyphID[]> glyphBuffer =
54 std::make_unique<SkGlyphID[]>(codePointCount);
55 SkGlyphID* glyphPointer = glyphBuffer.get();
56 int remainingCodePointCount = codePointCount;
57 for (int typefaceIndex = 0; typefaceIndex < typefaceCount; typefaceIndex++) {
58 typefaces[typefaceIndex]->unicharsToGlyphs(
59 codePoints, remainingCodePointCount, glyphPointer);
60 int outputIndex = 0;
61 for (int inputIndex = 0; inputIndex < remainingCodePointCount;
62 inputIndex++) {
63 if (glyphPointer[inputIndex] == 0) {
64 if (outputIndex != inputIndex) {
65 codePoints[outputIndex] = codePoints[inputIndex];
66 }
67 outputIndex++;
68 }
69 }
70 if (outputIndex == 0) {
71 return 0;
72 } else {
73 remainingCodePointCount = outputIndex;
74 }
75 }
76 return remainingCodePointCount;
77}
78
80 FlutterFontCollection* collection,
81 SkTypeface* typeface,
82 SkString* fontName) {
83 if (fontName) {
84 SkString alias = *fontName;
85 collection->provider->registerTypeface(sk_ref_sp<SkTypeface>(typeface),
86 alias);
87 } else {
88 collection->provider->registerTypeface(sk_ref_sp<SkTypeface>(typeface));
89 }
90}
91
93 FlutterFontCollection* collection) {
94 collection->collection->clearCaches();
95}
SK_API sk_sp< SkFontMgr > SkFontMgr_New_Custom_Empty()
int32_t SkUnichar
Definition: SkTypes.h:175
uint16_t SkGlyphID
Definition: SkTypes.h:179
Definition: SkData.h:25
sk_sp< SkTypeface > makeFromData(sk_sp< SkData >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:120
void unref() const
Definition: SkRefCnt.h:72
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
Definition: SkTypeface.cpp:357
T * release()
Definition: SkRefCnt.h:324
size_t registerTypeface(sk_sp< SkTypeface > typeface)
static sk_sp< SkFontMgr > default_fontmgr()
Definition: fonts.cpp:32
SKWASM_EXPORT FlutterFontCollection * fontCollection_create()
Definition: fonts.cpp:17
SKWASM_EXPORT int typefaces_filterCoveredCodePoints(SkTypeface **typefaces, int typefaceCount, SkUnichar *codePoints, int codePointCount)
Definition: fonts.cpp:49
SKWASM_EXPORT void fontCollection_registerTypeface(FlutterFontCollection *collection, SkTypeface *typeface, SkString *fontName)
Definition: fonts.cpp:79
SKWASM_EXPORT void typeface_dispose(SkTypeface *typeface)
Definition: fonts.cpp:42
SKWASM_EXPORT void fontCollection_dispose(FlutterFontCollection *collection)
Definition: fonts.cpp:28
SKWASM_EXPORT void fontCollection_clearCaches(FlutterFontCollection *collection)
Definition: fonts.cpp:92
SKWASM_EXPORT SkTypeface * typeface_create(SkData *fontData)
Definition: fonts.cpp:37
Definition: helpers.h:12
sk_sp< skia::textlayout::TypefaceFontProvider > provider
Definition: wrappers.h:34
sk_sp< skia::textlayout::FontCollection > collection
Definition: wrappers.h:33
#define SKWASM_EXPORT
Definition: export.h:10