2022-04-09 08:14:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-09-04 15:29:01 +00:00
|
|
|
#include <AK/TypeCasts.h>
|
2022-04-09 08:14:58 +00:00
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include <LibGfx/Font/ScaledFont.h>
|
2024-09-04 15:29:01 +00:00
|
|
|
#include <LibGfx/Font/TypefaceSkia.h>
|
2024-09-03 19:12:29 +00:00
|
|
|
#include <LibGfx/TextLayout.h>
|
2022-04-09 08:14:58 +00:00
|
|
|
|
2024-09-04 15:29:01 +00:00
|
|
|
#include <core/SkFont.h>
|
|
|
|
#include <core/SkFontMetrics.h>
|
|
|
|
#include <core/SkFontTypes.h>
|
|
|
|
|
2022-04-09 08:14:58 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2024-07-12 12:08:40 +00:00
|
|
|
ScaledFont::ScaledFont(NonnullRefPtr<Typeface> typeface, float point_width, float point_height, unsigned dpi_x, unsigned dpi_y)
|
|
|
|
: m_typeface(move(typeface))
|
2023-01-16 08:26:01 +00:00
|
|
|
, m_point_width(point_width)
|
|
|
|
, m_point_height(point_height)
|
|
|
|
{
|
2024-09-04 15:29:01 +00:00
|
|
|
float const units_per_em = m_typeface->units_per_em();
|
2023-01-16 08:26:01 +00:00
|
|
|
m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em);
|
|
|
|
m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em);
|
|
|
|
|
2023-07-19 01:35:58 +00:00
|
|
|
m_pixel_size = m_point_height * (DEFAULT_DPI / POINTS_PER_INCH);
|
2023-03-03 18:30:55 +00:00
|
|
|
m_pixel_size_rounded_up = static_cast<int>(ceilf(m_pixel_size));
|
|
|
|
|
2024-09-04 15:29:01 +00:00
|
|
|
auto const* sk_typeface = verify_cast<TypefaceSkia>(*m_typeface).sk_typeface();
|
|
|
|
SkFont const font { sk_ref_sp(sk_typeface), m_pixel_size };
|
|
|
|
|
|
|
|
SkFontMetrics skMetrics;
|
|
|
|
font.getMetrics(&skMetrics);
|
|
|
|
|
|
|
|
FontPixelMetrics metrics;
|
|
|
|
metrics.size = font.getSize();
|
|
|
|
metrics.x_height = skMetrics.fXHeight;
|
|
|
|
metrics.advance_of_ascii_zero = font.measureText("0", 1, SkTextEncoding::kUTF8);
|
|
|
|
metrics.ascent = -skMetrics.fAscent;
|
|
|
|
metrics.descent = skMetrics.fDescent;
|
|
|
|
metrics.line_gap = skMetrics.fLeading;
|
|
|
|
|
|
|
|
m_pixel_metrics = metrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScaledFontMetrics ScaledFont::metrics() const
|
|
|
|
{
|
|
|
|
SkFontMetrics sk_metrics;
|
|
|
|
skia_font(1).getMetrics(&sk_metrics);
|
|
|
|
|
|
|
|
ScaledFontMetrics metrics;
|
|
|
|
metrics.ascender = -sk_metrics.fAscent;
|
|
|
|
metrics.descender = sk_metrics.fDescent;
|
|
|
|
metrics.line_gap = sk_metrics.fLeading;
|
|
|
|
metrics.x_height = sk_metrics.fXHeight;
|
|
|
|
return metrics;
|
2023-01-16 08:26:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-03 19:12:29 +00:00
|
|
|
float ScaledFont::width(StringView view) const { return measure_text_width(Utf8View(view), *this); }
|
|
|
|
float ScaledFont::width(Utf8View const& view) const { return measure_text_width(view, *this); }
|
2022-04-09 08:14:58 +00:00
|
|
|
|
2023-01-03 13:55:48 +00:00
|
|
|
float ScaledFont::glyph_width(u32 code_point) const
|
2022-04-09 08:14:58 +00:00
|
|
|
{
|
2024-09-03 20:09:32 +00:00
|
|
|
auto string = String::from_code_point(code_point);
|
|
|
|
return measure_text_width(Utf8View(string), *this);
|
2022-04-09 08:14:58 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 13:06:42 +00:00
|
|
|
NonnullRefPtr<ScaledFont> ScaledFont::scaled_with_size(float point_size) const
|
2023-02-09 08:55:05 +00:00
|
|
|
{
|
2023-12-25 11:45:18 +00:00
|
|
|
if (point_size == m_point_height && point_size == m_point_width)
|
2024-03-01 13:06:42 +00:00
|
|
|
return *const_cast<ScaledFont*>(this);
|
2024-07-12 12:08:40 +00:00
|
|
|
return m_typeface->scaled_font(point_size);
|
2023-02-09 08:55:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 17:12:16 +00:00
|
|
|
NonnullRefPtr<Font> ScaledFont::with_size(float point_size) const
|
2024-03-01 13:06:42 +00:00
|
|
|
{
|
|
|
|
return scaled_with_size(point_size);
|
|
|
|
}
|
|
|
|
|
2022-04-09 08:14:58 +00:00
|
|
|
Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
|
|
|
|
{
|
2023-01-16 08:26:01 +00:00
|
|
|
return m_pixel_metrics;
|
2022-04-09 08:14:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:30:55 +00:00
|
|
|
float ScaledFont::pixel_size() const
|
|
|
|
{
|
|
|
|
return m_pixel_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScaledFont::pixel_size_rounded_up() const
|
|
|
|
{
|
|
|
|
return m_pixel_size_rounded_up;
|
|
|
|
}
|
|
|
|
|
2023-03-15 09:46:06 +00:00
|
|
|
float ScaledFont::point_size() const
|
|
|
|
{
|
|
|
|
return m_point_height;
|
|
|
|
}
|
|
|
|
|
2022-04-09 08:14:58 +00:00
|
|
|
}
|