Skip to content

Truly independent web browser

LibGfx+LibCompositing+Compositor: Draw synthetic bold and oblique

sideshowbarker406922c67f6d
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
index 9d899fabc52..e2faa37a415 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Math.h>
#include <LibCompositing/DisplayList/DisplayList.h>
#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>
#include <LibGfx/Bitmap.h>
@@ -43,6 +44,8 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::DisplayListFontResource cons
TRY(encoder.encode(font.point_size()));
TRY(encoder.encode(font.variation_settings()));
TRY(encoder.encode(font.features()));
+ TRY(encoder.encode(font.synthesis().bold));
+ TRY(encoder.encode(font.synthesis().oblique_angle));
TRY(encoder.encode(font.is_invisible()));
return {};
}
@@ -55,8 +58,13 @@ ErrorOr<Compositing::DisplayListFontResource> decode(Decoder& decoder)
auto point_size = TRY(decoder.decode<float>());
auto variations = TRY(decoder.decode<Gfx::FontVariationSettings>());
auto features = TRY(decoder.decode<Gfx::ShapeFeatures>());
+ Gfx::FontSynthesis synthesis;
+ synthesis.bold = TRY(decoder.decode<bool>());
+ synthesis.oblique_angle = TRY(decoder.decode<float>());
+ if (!__builtin_isfinite(synthesis.oblique_angle) || AK::fabs(synthesis.oblique_angle) > 90)
+ return Error::from_string_literal("Invalid synthetic oblique angle");
auto invisible = TRY(decoder.decode<bool>());
- auto font = invisible ? typeface->invisible_font(point_size, variations, features) : typeface->font(point_size, variations, features);
+ auto font = invisible ? typeface->invisible_font(point_size, variations, features, synthesis) : typeface->font(point_size, variations, features, synthesis);
return Compositing::DisplayListFontResource {
.id = id,
diff --git a/Libraries/LibGfx/Font/Font.cpp b/Libraries/LibGfx/Font/Font.cpp
index 05ed53a500f..439b7f62f43 100644
--- a/Libraries/LibGfx/Font/Font.cpp
+++ b/Libraries/LibGfx/Font/Font.cpp
@@ -7,6 +7,7 @@
*/
#include <AK/Atomic.h>
+#include <AK/Math.h>
#include <AK/NumericLimits.h>
#include <AK/TypeCasts.h>
#include <AK/Utf16String.h>
@@ -43,13 +44,14 @@ public:
FFI::FfiMathData data {};
};
-Font::Font(NonnullRefPtr<Typeface const> typeface, float point_width, float point_height, FontVariationSettings const variations, ShapeFeatures const& features)
+Font::Font(NonnullRefPtr<Typeface const> typeface, float point_width, float point_height, FontVariationSettings const variations, ShapeFeatures const& features, FontSynthesis synthesis)
: m_id(s_next_id.fetch_add(1, AK::MemoryOrder::memory_order_relaxed))
, m_typeface(move(typeface))
, m_point_width(point_width)
, m_point_height(point_height)
, m_font_variation_settings(move(variations))
, m_shape_features(features)
+ , m_synthesis(synthesis)
{
m_pixel_size = m_point_height * (DEFAULT_DPI / POINTS_PER_INCH);
m_style = m_typeface->style_for_variations(m_font_variation_settings.to_sorted_list());
@@ -125,7 +127,7 @@ float Font::width(Utf16View const& view) const { return measure_text_width(view,
NonnullRefPtr<Font> Font::invisible_variant() const
{
- return m_typeface->invisible_font(m_point_height, m_font_variation_settings, m_shape_features);
+ return m_typeface->invisible_font(m_point_height, m_font_variation_settings, m_shape_features, m_synthesis);
}
float Font::pixel_size() const
@@ -180,6 +182,9 @@ hb_font_t* Font::create_harfbuzz_font() const
hb_font_set_variations(font, hb_list.data(), hb_list.size());
}
+ // NB: HarfBuzz slants the glyph outlines and extents it reports, so that text is measured as it's drawn.
+ if (m_synthesis.oblique_angle != 0)
+ hb_font_set_synthetic_slant(font, AK::tan(AK::to_radians(m_synthesis.oblique_angle)));
hb_font_make_immutable(font);
return font;
}
diff --git a/Libraries/LibGfx/Font/Font.h b/Libraries/LibGfx/Font/Font.h
index 6c231ee2780..3c301b2eb74 100644
--- a/Libraries/LibGfx/Font/Font.h
+++ b/Libraries/LibGfx/Font/Font.h
@@ -54,7 +54,7 @@ constexpr float text_shaping_resolution = 64;
class Font : public AtomicRefCounted<Font> {
public:
- Font(NonnullRefPtr<Typeface const>, float point_width, float point_height, FontVariationSettings const variations, ShapeFeatures const& features);
+ Font(NonnullRefPtr<Typeface const>, float point_width, float point_height, FontVariationSettings const variations, ShapeFeatures const& features, FontSynthesis synthesis = {});
~Font();
u64 id() const { return m_id; }
@@ -82,6 +82,8 @@ public:
hb_font_t* harfbuzz_font() const { return m_harfbuzz_font; }
FontVariationSettings const& variation_settings() const { return m_font_variation_settings; }
ShapeFeatures const& features() const { return m_shape_features; }
+ // How this font is drawn bolder or slanted than its face; weight() and slope() describe the face itself.
+ FontSynthesis synthesis() const { return m_synthesis; }
bool is_emoji_font() const;
@@ -122,6 +124,7 @@ private:
float m_point_height { 0.0f };
FontVariationSettings const m_font_variation_settings;
ShapeFeatures m_shape_features;
+ FontSynthesis m_synthesis;
FontPixelMetrics m_pixel_metrics;
FaceStyle m_style;
diff --git a/Libraries/LibGfx/Font/Typeface.cpp b/Libraries/LibGfx/Font/Typeface.cpp
index 278aa2298fd..76902d5b836 100644
--- a/Libraries/LibGfx/Font/Typeface.cpp
+++ b/Libraries/LibGfx/Font/Typeface.cpp
@@ -308,27 +308,27 @@ void keep_recent_font(NonnullRefPtr<Font const> font)
}
-NonnullRefPtr<Font> Typeface::font(float point_size, FontVariationSettings const& variations, Gfx::ShapeFeatures const& shape_features) const
+NonnullRefPtr<Font> Typeface::font(float point_size, FontVariationSettings const& variations, Gfx::ShapeFeatures const& shape_features, FontSynthesis synthesis) const
{
RefPtr<Font> font;
{
MutexLocker locker { m_fonts_mutex };
- FontCacheKey key { point_size, variations.to_sorted_list(), shape_features };
+ FontCacheKey key { point_size, variations.to_sorted_list(), shape_features, synthesis };
// A font that another thread is destroying cannot be referenced any more, and a new font replaces it.
if (auto it = m_fonts.find(key); it != m_fonts.end() && it->value->try_ref())
return adopt_ref(*it->value);
- font = adopt_ref(*new Font(*this, point_size, point_size, variations, shape_features));
+ font = adopt_ref(*new Font(*this, point_size, point_size, variations, shape_features, synthesis));
m_fonts.set(move(key), font.ptr());
}
keep_recent_font(*font);
return font.release_nonnull();
}
-NonnullRefPtr<Font> Typeface::invisible_font(float point_size, FontVariationSettings const& variations, Gfx::ShapeFeatures const& shape_features) const
+NonnullRefPtr<Font> Typeface::invisible_font(float point_size, FontVariationSettings const& variations, Gfx::ShapeFeatures const& shape_features, FontSynthesis synthesis) const
{
- auto font = adopt_ref(*new Font(*this, point_size, point_size, variations, shape_features));
+ auto font = adopt_ref(*new Font(*this, point_size, point_size, variations, shape_features, synthesis));
font->m_is_invisible = true;
return font;
}
@@ -336,7 +336,7 @@ NonnullRefPtr<Font> Typeface::invisible_font(float point_size, FontVariationSett
void Typeface::forget_font(Font const& font) const
{
MutexLocker locker { m_fonts_mutex };
- FontCacheKey key { font.point_size(), font.variation_settings().to_sorted_list(), font.features() };
+ FontCacheKey key { font.point_size(), font.variation_settings().to_sorted_list(), font.features(), font.synthesis() };
// Invisible variants are not in the cache, and a newer font may have replaced this one.
if (auto it = m_fonts.find(key); it != m_fonts.end() && it->value == &font)
m_fonts.remove(it);
diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.h
index 89c8f49561d..9869d0ae89c 100644
--- a/Libraries/LibGfx/Font/Typeface.h
+++ b/Libraries/LibGfx/Font/Typeface.h
@@ -57,15 +57,27 @@ struct SystemUIFontStyle {
u8 slope;
};
+// https://drafts.csswg.org/css-fonts-4/#font-synthesis-intro
+// How a font is drawn bolder or slanted than its face, where the face lacks the weight or style asked for.
+struct FontSynthesis {
+ bool bold { false };
+ // The angle of a synthetic oblique, in degrees; positive slants the glyphs' tops toward the end of the line.
+ float oblique_angle { 0 };
+
+ bool operator==(FontSynthesis const&) const = default;
+};
+
struct FontCacheKey {
float point_size;
Vector<FontVariationAxis> axes;
Gfx::ShapeFeatures shape_features;
+ FontSynthesis synthesis;
// Sizes and values compare by their bits, as the hash reads them, so that a key with NaN finds itself.
bool operator==(FontCacheKey const& other) const
{
- if (bit_cast<u32>(point_size) != bit_cast<u32>(other.point_size) || axes.size() != other.axes.size() || shape_features != other.shape_features)
+ if (bit_cast<u32>(point_size) != bit_cast<u32>(other.point_size) || axes.size() != other.axes.size() || shape_features != other.shape_features
+ || synthesis.bold != other.synthesis.bold || bit_cast<u32>(synthesis.oblique_angle) != bit_cast<u32>(other.synthesis.oblique_angle))
return false;
for (size_t index = 0; index < axes.size(); ++index) {
if (axes[index].tag != other.axes[index].tag || bit_cast<u32>(axes[index].value) != bit_cast<u32>(other.axes[index].value))
@@ -80,6 +92,7 @@ struct FontCacheKey {
for (auto const& axis : axes)
h = pair_int_hash(h, pair_int_hash(axis.tag.to_u32(), bit_cast<u32>(axis.value)));
h = pair_int_hash(h, Traits<Gfx::ShapeFeatures>::hash(shape_features));
+ h = pair_int_hash(h, pair_int_hash(synthesis.bold, bit_cast<u32>(synthesis.oblique_angle)));
return h;
}
};
@@ -121,9 +134,9 @@ public:
RefPtr<FontDataBacking> font_data_backing() const { return m_font_data; }
u32 collection_index() const { return m_ttc_index; }
- [[nodiscard]] NonnullRefPtr<Font> font(float point_size, FontVariationSettings const& variations = {}, Gfx::ShapeFeatures const& shape_features = {}) const;
+ [[nodiscard]] NonnullRefPtr<Font> font(float point_size, FontVariationSettings const& variations = {}, Gfx::ShapeFeatures const& shape_features = {}, FontSynthesis synthesis = {}) const;
// A font that measures and shapes text like font() but draws nothing. Each call makes a new font.
- [[nodiscard]] NonnullRefPtr<Font> invisible_font(float point_size, FontVariationSettings const& variations = {}, Gfx::ShapeFeatures const& shape_features = {}) const;
+ [[nodiscard]] NonnullRefPtr<Font> invisible_font(float point_size, FontVariationSettings const& variations = {}, Gfx::ShapeFeatures const& shape_features = {}, FontSynthesis synthesis = {}) const;
void set_system_font_identifier(SystemFontIdentifier identifier) { m_system_font_identifier = identifier; }
Optional<SystemFontIdentifier> system_font_identifier() const { return m_system_font_identifier; }
diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cpp
index 6556c8c97fe..f588b7cb909 100644
--- a/Libraries/LibGfx/TextLayout.cpp
+++ b/Libraries/LibGfx/TextLayout.cpp
@@ -24,6 +24,16 @@
namespace Gfx {
+// How far synthetic bold can spread the ink of a font's glyphs past their outlines, in device pixels. The rasterizer
+// decides the stroke, so this allows for the widest it's known to use: It bounds what a run may paint, and isn't a
+// measurement of its ink.
+static float synthetic_bold_outset(Font const& font, float scale)
+{
+ if (!font.synthesis().bold)
+ return 0;
+ return max(font.pixel_size() * scale / 24, 1.0f);
+}
+
// Tight bounds of the ink of the painted glyphs, from their own extents, in the coordinates of glyph_run_bounding_box().
// Empty if no painted glyph has ink.
static FloatRect glyph_run_ink_bounding_box(Font const& font, ReadonlySpan<ShapedGlyph> glyphs, float scale)
@@ -94,11 +104,28 @@ FloatRect glyph_run_bounding_box(Font const& font, ReadonlySpan<ShapedGlyph> gly
auto right = max_x + font_bounding_box.x_max * font_units_to_pixels;
auto top = min_y - font_bounding_box.y_max * font_units_to_pixels;
auto bottom = max_y - font_bounding_box.y_min * font_units_to_pixels;
+ // A synthetic oblique shifts each point of a glyph along the line by its height above the baseline.
+ if (auto angle = font.synthesis().oblique_angle; angle != 0) {
+ auto slant = AK::tan(AK::to_radians(angle));
+ auto shift_at_top = font_bounding_box.y_max * font_units_to_pixels * slant;
+ auto shift_at_bottom = font_bounding_box.y_min * font_units_to_pixels * slant;
+ left += min(0.0f, min(shift_at_top, shift_at_bottom));
+ right += max(0.0f, max(shift_at_top, shift_at_bottom));
+ }
+ auto outset = synthetic_bold_outset(font, scale);
+ left -= outset;
+ right += outset;
+ top -= outset;
+ bottom += outset;
return { left, top, right - left, bottom - top };
}
// The font doesn't record an overall bounding box (e.g. bitmap-only fonts), so unite the glyphs' own extents.
- return glyph_run_ink_bounding_box(font, glyphs, scale);
+ // NB: HarfBuzz slants the extents of a synthetic oblique itself.
+ auto bounds = glyph_run_ink_bounding_box(font, glyphs, scale);
+ if (auto outset = synthetic_bold_outset(font, scale); outset > 0 && !bounds.is_empty())
+ bounds.inflate(outset * 2, outset * 2);
+ return bounds;
}
namespace {
diff --git a/Services/Compositor/FontSkia.cpp b/Services/Compositor/FontSkia.cpp
index bc6c06d2c7f..14ea4b005e2 100644
--- a/Services/Compositor/FontSkia.cpp
+++ b/Services/Compositor/FontSkia.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/Atomic.h>
+#include <AK/Math.h>
#include <AK/NeverDestroyed.h>
#include <AK/ScopeGuard.h>
#include <AK/TypeCasts.h>
@@ -241,6 +242,13 @@ Optional<SkFont> skia_font(Gfx::Font const& font, float scale)
SkFont sk_font { data.typeface, font.pixel_size() * scale };
sk_font.setSubpixel(true);
+ // NB: Skia's x axis runs as text does but its y axis runs downward, so a slant that tips the glyphs' tops toward
+ // the end of the line is a negative skew.
+ auto synthesis = font.synthesis();
+ sk_font.setEmbolden(synthesis.bold);
+ if (synthesis.oblique_angle != 0)
+ sk_font.setSkewX(-AK::tan(AK::to_radians(synthesis.oblique_angle)));
+
#if defined(USE_FONTCONFIG)
if (s_force_hinting_for_testing) {
sk_font.setHinting(SkFontHinting::kNormal);