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