2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-10-11 23:45:57 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-03 13:51:49 +02:00
|
|
|
#include <AK/MappedFile.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2020-02-06 11:56:38 +01:00
|
|
|
#include <AK/RefPtr.h>
|
|
|
|
#include <AK/String.h>
|
2018-10-11 23:45:57 +02:00
|
|
|
#include <AK/Types.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-02-06 11:56:38 +01:00
|
|
|
|
|
|
|
namespace Gfx {
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-02-05 06:43:33 +01:00
|
|
|
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
|
|
|
|
class GlyphBitmap {
|
|
|
|
friend class Font;
|
2019-06-07 11:46:55 +02:00
|
|
|
|
2019-02-05 06:43:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size size() const { return m_size; }
|
|
|
|
int width() const { return m_size.width(); }
|
|
|
|
int height() const { return m_size.height(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
GlyphBitmap(const unsigned* rows, Size size)
|
|
|
|
: m_rows(rows)
|
|
|
|
, m_size(size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned* m_rows { nullptr };
|
|
|
|
Size m_size;
|
|
|
|
};
|
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
class Font : public RefCounted<Font> {
|
2018-10-11 23:45:57 +02:00
|
|
|
public:
|
2019-01-16 17:54:06 +01:00
|
|
|
static Font& default_font();
|
2019-02-04 11:37:15 +01:00
|
|
|
static Font& default_bold_font();
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-03-06 14:06:40 +01:00
|
|
|
static Font& default_fixed_width_font();
|
2019-08-04 08:14:53 +02:00
|
|
|
static Font& default_bold_fixed_width_font();
|
2019-03-06 14:06:40 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Font> clone() const;
|
2019-02-02 08:05:14 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
static RefPtr<Font> load_from_file(const StringView& path);
|
2019-06-02 14:58:02 +02:00
|
|
|
bool write_to_file(const StringView& path);
|
2019-02-02 23:13:12 +01:00
|
|
|
|
2018-10-11 23:45:57 +02:00
|
|
|
~Font();
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(u8)ch * m_glyph_height], { glyph_width(ch), m_glyph_height }); }
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 glyph_width(char ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[(u8)ch]; }
|
2019-09-04 23:45:55 +03:00
|
|
|
int glyph_or_emoji_width(u32 codepoint) const;
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 glyph_height() const { return m_glyph_height; }
|
|
|
|
u8 min_glyph_width() const { return m_min_glyph_width; }
|
|
|
|
u8 max_glyph_width() const { return m_max_glyph_width; }
|
2019-09-04 23:45:55 +03:00
|
|
|
int width(const StringView&) const;
|
|
|
|
int width(const Utf8View&) const;
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-02-03 03:06:58 +01:00
|
|
|
String name() const { return m_name; }
|
2019-06-02 14:58:02 +02:00
|
|
|
void set_name(const StringView& name) { m_name = name; }
|
2019-02-03 03:06:58 +01:00
|
|
|
|
2019-03-06 11:03:10 +01:00
|
|
|
bool is_fixed_width() const { return m_fixed_width; }
|
2019-03-06 12:52:41 +01:00
|
|
|
void set_fixed_width(bool b) { m_fixed_width = b; }
|
|
|
|
|
2019-12-30 11:36:55 +01:00
|
|
|
u8 glyph_spacing() const { return m_glyph_spacing; }
|
|
|
|
void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void set_glyph_width(char ch, u8 width)
|
2019-03-06 12:52:41 +01:00
|
|
|
{
|
|
|
|
ASSERT(m_glyph_widths);
|
2019-07-03 21:17:35 +02:00
|
|
|
m_glyph_widths[(u8)ch] = width;
|
2019-03-06 12:52:41 +01:00
|
|
|
}
|
2019-03-06 11:03:10 +01:00
|
|
|
|
2018-10-11 23:45:57 +02:00
|
|
|
private:
|
2019-12-30 11:36:55 +01:00
|
|
|
Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing);
|
2019-02-02 23:13:12 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static RefPtr<Font> load_from_memory(const u8*);
|
2019-04-03 13:51:49 +02:00
|
|
|
|
2019-02-02 23:13:12 +01:00
|
|
|
String m_name;
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-02-05 06:43:33 +01:00
|
|
|
unsigned* m_rows { nullptr };
|
2019-07-03 21:17:35 +02:00
|
|
|
u8* m_glyph_widths { nullptr };
|
2019-04-03 13:51:49 +02:00
|
|
|
MappedFile m_mapped_file;
|
2019-02-05 06:43:33 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 m_glyph_width { 0 };
|
|
|
|
u8 m_glyph_height { 0 };
|
|
|
|
u8 m_min_glyph_width { 0 };
|
|
|
|
u8 m_max_glyph_width { 0 };
|
2019-12-30 11:36:55 +01:00
|
|
|
u8 m_glyph_spacing { 0 };
|
2019-03-06 11:03:10 +01:00
|
|
|
|
|
|
|
bool m_fixed_width { false };
|
2018-10-11 23:45:57 +02:00
|
|
|
};
|
2020-02-06 11:56:38 +01:00
|
|
|
|
|
|
|
}
|