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
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/MappedFile.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibGfx/Size.h>
namespace Gfx {
enum FontTypes {
Default = 0,
LatinExtendedA = 1
};
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
class GlyphBitmap {
friend class Font;
public:
const unsigned* rows() const { return m_rows; }
unsigned row(unsigned index) const { return m_rows[index]; }
bool bit_at(int x, int y) const { return row(y) & (1 << x); }
void set_bit_at(int x, int y, bool b)
{
auto& mutable_row = const_cast<unsigned*>(m_rows)[y];
if (b)
mutable_row |= 1 << x;
else
mutable_row &= ~(1 << x);
}
IntSize size() const { return m_size; }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
private:
GlyphBitmap(const unsigned* rows, IntSize size)
: m_rows(rows)
, m_size(size)
{
}
const unsigned* m_rows { nullptr };
IntSize m_size;
};
class Font : public RefCounted<Font> {
public:
static Font& default_font();
static Font& default_bold_font();
static Font& default_fixed_width_font();
static Font& default_bold_fixed_width_font();
NonnullRefPtr<Font> clone() const;
static NonnullRefPtr<Font> create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type);
static RefPtr<Font> load_from_file(const StringView& path);
bool write_to_file(const StringView& path);
~Font();
GlyphBitmap glyph_bitmap(u32 code_point) const;
u8 glyph_width(size_t ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; }
int glyph_or_emoji_width(u32 code_point) const;
u8 glyph_height() const { return m_glyph_height; }
int x_height() const { return m_x_height; }
u8 min_glyph_width() const { return m_min_glyph_width; }
u8 max_glyph_width() const { return m_max_glyph_width; }
u8 glyph_fixed_width() const { return m_glyph_width; }
int width(const StringView&) const;
int width(const Utf8View&) const;
int width(const Utf32View&) const;
String name() const { return m_name; }
void set_name(const StringView& name) { m_name = name; }
bool is_fixed_width() const { return m_fixed_width; }
void set_fixed_width(bool b) { m_fixed_width = b; }
const Font& bold_family_font() const { return *m_bold_family_font; }
bool has_boldface() const { return m_boldface; }
void set_boldface(bool b) { m_boldface = b; }
u8 glyph_spacing() const { return m_glyph_spacing; }
void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
void set_glyph_width(size_t ch, u8 width)
{
ASSERT(m_glyph_widths);
m_glyph_widths[ch] = width;
}
int glyph_count() const { return m_glyph_count; }
FontTypes type() { return m_type; }
void set_type(FontTypes type);
private:
Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type);
static RefPtr<Font> load_from_memory(const u8*);
static size_t glyph_count_by_type(FontTypes type);
void set_family_fonts();
RefPtr<Font> m_bold_family_font;
String m_name;
FontTypes m_type;
size_t m_glyph_count { 256 };
unsigned* m_rows { nullptr };
u8* m_glyph_widths { nullptr };
MappedFile m_mapped_file;
u8 m_glyph_width { 0 };
u8 m_glyph_height { 0 };
u8 m_x_height { 0 };
u8 m_min_glyph_width { 0 };
u8 m_max_glyph_width { 0 };
u8 m_glyph_spacing { 0 };
bool m_fixed_width { false };
bool m_boldface { false };
};
}
|