1// LAF OS Library
2// Copyright (c) 2019-2020 Igara Studio S.A.
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifndef OS_SKIA_SKIA_FONT_MANAGER_INCLUDED
8#define OS_SKIA_SKIA_FONT_MANAGER_INCLUDED
9#pragma once
10
11#include "os/font_manager.h"
12
13#include "os/font_style.h"
14
15#include "include/core/SkFontMgr.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTypeface.h"
18
19namespace os {
20
21class SkiaTypeface : public Typeface {
22public:
23 SkiaTypeface(SkTypeface* skTypeface)
24 : m_skTypeface(skTypeface) {
25 }
26
27 FontStyle fontStyle() const override {
28 SkFontStyle skStyle = m_skTypeface->fontStyle();
29 return FontStyle((FontStyle::Weight)skStyle.weight(),
30 (FontStyle::Width)skStyle.width(),
31 (FontStyle::Slant)skStyle.slant());
32 }
33
34private:
35 sk_sp<SkTypeface> m_skTypeface;
36};
37
38class SkiaFontStyleSet : public FontStyleSet {
39public:
40 SkiaFontStyleSet(SkFontStyleSet* set)
41 : m_skSet(set) {
42 }
43
44 int count() override {
45 return m_skSet->count();
46 }
47
48 void getStyle(int index,
49 FontStyle& style,
50 std::string& name) override {
51 SkFontStyle skStyle;
52 SkString skName;
53 m_skSet->getStyle(index, &skStyle, &skName);
54 style = FontStyle((FontStyle::Weight)skStyle.weight(),
55 (FontStyle::Width)skStyle.width(),
56 (FontStyle::Slant)skStyle.slant());
57 name = skName.c_str();
58 }
59
60 TypefaceRef typeface(int index) override {
61 return make_ref<SkiaTypeface>(m_skSet->createTypeface(index));
62 }
63
64 TypefaceRef matchStyle(const FontStyle& style) override {
65 SkFontStyle skStyle((SkFontStyle::Weight)style.weight(),
66 (SkFontStyle::Width)style.width(),
67 (SkFontStyle::Slant)style.slant());
68 return make_ref<SkiaTypeface>(m_skSet->matchStyle(skStyle));
69 }
70
71private:
72 sk_sp<SkFontStyleSet> m_skSet;
73};
74
75class SkiaFontManager : public FontManager {
76public:
77 SkiaFontManager()
78 : m_skFontMgr(SkFontMgr::RefDefault()) {
79 }
80
81 ~SkiaFontManager() {
82 }
83
84 int countFamilies() const override {
85 return m_skFontMgr->countFamilies();
86 }
87
88 std::string familyName(int i) const override {
89 SkString name;
90 m_skFontMgr->getFamilyName(i, &name);
91 return std::string(name.c_str());
92 }
93
94 Ref<FontStyleSet> familyStyleSet(int i) const override {
95 return make_ref<SkiaFontStyleSet>(m_skFontMgr->createStyleSet(i));
96 }
97
98 Ref<FontStyleSet> matchFamily(const std::string& familyName) const override {
99 return make_ref<SkiaFontStyleSet>(m_skFontMgr->matchFamily(familyName.c_str()));
100 }
101
102private:
103 sk_sp<SkFontMgr> m_skFontMgr;
104};
105
106} // namespace os
107
108#endif
109