xref: /aosp_15_r20/external/skia/include/utils/SkCustomTypeface.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
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 SkCustomTypeface_DEFINED
9 #define SkCustomTypeface_DEFINED
10 
11 #include "include/core/SkDrawable.h"
12 #include "include/core/SkFontMetrics.h"
13 #include "include/core/SkFontStyle.h"
14 #include "include/core/SkFourByteTag.h"
15 #include "include/core/SkPath.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/core/SkTypes.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 class SkStream;
25 class SkStreamAsset;
26 struct SkFontArguments;
27 
28 class SK_API SkCustomTypefaceBuilder {
29 public:
30     SkCustomTypefaceBuilder();
31 
32     void setGlyph(SkGlyphID, float advance, const SkPath&);
33     void setGlyph(SkGlyphID, float advance, sk_sp<SkDrawable>, const SkRect& bounds);
34 
35     void setMetrics(const SkFontMetrics& fm, float scale = 1);
36     void setFontStyle(SkFontStyle);
37 
38     sk_sp<SkTypeface> detach();
39 
40     static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('u','s','e','r');
41     static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&);
42 
43 private:
44     struct GlyphRec {
45         // logical union
46         SkPath            fPath;
47         sk_sp<SkDrawable> fDrawable;
48 
49         SkRect            fBounds  = {0,0,0,0}; // only used for drawable glyphs atm
50         float             fAdvance = 0;
51 
isDrawableGlyphRec52         bool isDrawable() const {
53             SkASSERT(!fDrawable || fPath.isEmpty());
54             return fDrawable != nullptr;
55         }
56     };
57 
58     std::vector<GlyphRec> fGlyphRecs;
59     SkFontMetrics         fMetrics;
60     SkFontStyle           fStyle;
61 
62     GlyphRec& ensureStorage(SkGlyphID);
63 
64     static sk_sp<SkTypeface> Deserialize(SkStream*);
65 
66     friend class SkTypeface;
67     friend class SkUserTypeface;
68 };
69 
70 #endif
71