mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:37:35 +00:00
Make a preparation pass for variable-width fonts.
This commit is contained in:
parent
b85fe0bd07
commit
0a86366c71
9 changed files with 72 additions and 32 deletions
|
@ -55,7 +55,16 @@ Font::Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_heig
|
|||
, m_rows(rows)
|
||||
, m_glyph_width(glyph_width)
|
||||
, m_glyph_height(glyph_height)
|
||||
, m_min_glyph_width(glyph_width)
|
||||
, m_max_glyph_width(glyph_width)
|
||||
{
|
||||
m_fixed_width = true;
|
||||
if (!m_fixed_width) {
|
||||
byte minimum = 255;
|
||||
for (int i = 0; i < 256; ++i)
|
||||
minimum = min(minimum, m_glyph_widths[i]);
|
||||
m_min_glyph_width = minimum;
|
||||
}
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
|
@ -137,3 +146,14 @@ bool Font::write_to_file(const String& path)
|
|||
ASSERT(rc == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
int Font::width(const String& string) const
|
||||
{
|
||||
if (m_fixed_width)
|
||||
return string.length() * m_glyph_width;
|
||||
|
||||
int width = 0;
|
||||
for (int i = 0; i < string.length(); ++i)
|
||||
width += glyph_width(string[i]);
|
||||
return width;
|
||||
}
|
||||
|
|
|
@ -54,21 +54,30 @@ public:
|
|||
|
||||
GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(byte)ch * m_glyph_height], { m_glyph_width, m_glyph_height }); }
|
||||
|
||||
byte glyph_width() const { return m_glyph_width; }
|
||||
byte glyph_width(char ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[(byte)ch]; }
|
||||
byte glyph_height() const { return m_glyph_height; }
|
||||
int width(const String& string) const { return string.length() * glyph_width(); }
|
||||
byte min_glyph_width() const { return m_min_glyph_width; }
|
||||
byte max_glyph_width() const { return m_max_glyph_width; }
|
||||
int width(const String& string) const;
|
||||
|
||||
String name() const { return m_name; }
|
||||
void set_name(const String& name) { m_name = name; }
|
||||
|
||||
bool is_fixed_width() const { return m_fixed_width; }
|
||||
|
||||
private:
|
||||
Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height);
|
||||
|
||||
String m_name;
|
||||
|
||||
unsigned* m_rows { nullptr };
|
||||
byte* m_glyph_widths { nullptr };
|
||||
void* m_mmap_ptr { nullptr };
|
||||
|
||||
byte m_glyph_width { 0 };
|
||||
byte m_glyph_height { 0 };
|
||||
byte m_min_glyph_width { 0 };
|
||||
byte m_max_glyph_width { 0 };
|
||||
|
||||
bool m_fixed_width { false };
|
||||
};
|
||||
|
|
|
@ -332,21 +332,25 @@ void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alig
|
|||
} else if (alignment == TextAlignment::CenterLeft) {
|
||||
point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
|
||||
} else if (alignment == TextAlignment::CenterRight) {
|
||||
int text_width = text.length() * font().glyph_width();
|
||||
int text_width = font().width(text);
|
||||
point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
|
||||
} else if (alignment == TextAlignment::Center) {
|
||||
int text_width = text.length() * font().glyph_width();
|
||||
int text_width = font().width(text);
|
||||
point = rect.center();
|
||||
point.move_by(-(text_width / 2), -(font().glyph_height() / 2));
|
||||
} else {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
|
||||
int space_width = font().glyph_width(' ');
|
||||
for (ssize_t i = 0; i < text.length(); ++i) {
|
||||
byte ch = text[i];
|
||||
if (ch == ' ')
|
||||
if (ch == ' ') {
|
||||
point.move_by(space_width, 0);
|
||||
continue;
|
||||
}
|
||||
draw_glyph(point, ch, color);
|
||||
point.move_by(font().glyph_width(ch), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue