Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibGfx: Don't try to paint glyphs that aren't in a font #179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Userland/Libraries/LibAccelGfx/GlyphAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ void GlyphAtlas::update(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_
for (auto const& [font, code_points] : unique_glyphs) {
for (auto const& code_point : code_points) {
auto glyph = font->glyph(code_point);
if (!glyph.has_value())
continue;
auto atlas_key = GlyphsTextureKey { font, code_point };
if (!m_glyphs_texture_map.contains(atlas_key))
need_to_rebuild_texture = true;
if (glyph.bitmap()) {
glyph_bitmaps.set(atlas_key, *glyph.bitmap());
}
glyph_bitmaps.set(atlas_key, *glyph->bitmap());
}
}

Expand Down
35 changes: 0 additions & 35 deletions Userland/Libraries/LibGfx/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,13 @@
#include <LibGfx/Forward.h>
#include <LibGfx/Rect.h>

#define ENUMERATE_IMAGE_FORMATS \
__ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
__ENUMERATE_IMAGE_FORMAT(dds, ".dds") \
__ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
__ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
__ENUMERATE_IMAGE_FORMAT(iff, ".iff") \
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jb2") \
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jbig2") \
__ENUMERATE_IMAGE_FORMAT(jpeg2000, ".jp2") \
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jpeg") \
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jpg") \
__ENUMERATE_IMAGE_FORMAT(jpeg2000, ".jpx") \
__ENUMERATE_IMAGE_FORMAT(jxl, ".jxl") \
__ENUMERATE_IMAGE_FORMAT(iff, ".lbm") \
__ENUMERATE_IMAGE_FORMAT(pam, ".pam") \
__ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
__ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
__ENUMERATE_IMAGE_FORMAT(png, ".png") \
__ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
__ENUMERATE_IMAGE_FORMAT(qoi, ".qoi") \
__ENUMERATE_IMAGE_FORMAT(tga, ".tga") \
__ENUMERATE_IMAGE_FORMAT(tiff, ".tif") \
__ENUMERATE_IMAGE_FORMAT(tiff, ".tiff") \
__ENUMERATE_IMAGE_FORMAT(tvg, ".tvg") \
__ENUMERATE_IMAGE_FORMAT(tvg, ".webp")

namespace Gfx {

enum class BitmapFormat {
Invalid,
BGRx8888,
BGRA8888,
RGBA8888,

FirstValid = BGRx8888,
LastValid = RGBA8888,
};

inline bool is_valid_bitmap_format(unsigned format)
Expand Down Expand Up @@ -88,12 +59,6 @@ inline StorageFormat determine_storage_format(BitmapFormat format)

struct BackingStore;

enum class RotationDirection {
CounterClockwise,
Flip,
Clockwise,
};

class Bitmap : public RefCounted<Bitmap> {
public:
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize);
Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibGfx/Font/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Glyph {

bool is_color_bitmap() const { return m_color_bitmap; }

RefPtr<Bitmap> bitmap() const { return m_bitmap; }
[[nodiscard]] NonnullRefPtr<Bitmap> bitmap() const { return m_bitmap; }
float left_bearing() const { return m_left_bearing; }
float advance() const { return m_advance; }
float ascent() const { return m_ascent; }
Expand Down Expand Up @@ -116,8 +116,8 @@ class Font : public RefCounted<Font> {
virtual int pixel_size_rounded_up() const = 0;

virtual u16 weight() const = 0;
virtual Glyph glyph(u32 code_point) const = 0;
virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const = 0;
virtual Optional<Glyph> glyph(u32 code_point) const = 0;
virtual Optional<Glyph> glyph(u32 code_point, GlyphSubpixelOffset) const = 0;
virtual bool contains_glyph(u32 code_point) const = 0;

virtual float glyph_left_bearing(u32 code_point) const = 0;
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibGfx/Font/ScaledFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ bool ScaledFont::append_glyph_path_to(Gfx::Path& path, u32 glyph_id) const
return m_font->append_glyph_path_to(path, glyph_id, m_x_scale, m_y_scale);
}

Gfx::Glyph ScaledFont::glyph(u32 code_point) const
Optional<Glyph> ScaledFont::glyph(u32 code_point) const
{
return glyph(code_point, GlyphSubpixelOffset { 0, 0 });
}

Gfx::Glyph ScaledFont::glyph(u32 code_point, GlyphSubpixelOffset subpixel_offset) const
Optional<Glyph> ScaledFont::glyph(u32 code_point, GlyphSubpixelOffset subpixel_offset) const
{
auto id = glyph_id_for_code_point(code_point);
auto bitmap = rasterize_glyph(id, subpixel_offset);
if (!bitmap)
return {};
auto metrics = glyph_metrics(id);
return Gfx::Glyph(*bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender, m_font->has_color_bitmaps());
}
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibGfx/Font/ScaledFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class ScaledFont final : public Gfx::Font {
virtual Gfx::FontPixelMetrics pixel_metrics() const override;
virtual u8 slope() const override { return m_font->slope(); }
virtual u16 weight() const override { return m_font->weight(); }
virtual Gfx::Glyph glyph(u32 code_point) const override;
virtual Optional<Glyph> glyph(u32 code_point) const override;
virtual float glyph_left_bearing(u32 code_point) const override;
virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const override;
virtual Optional<Glyph> glyph(u32 code_point, GlyphSubpixelOffset) const override;
virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; }
virtual float glyph_width(u32 code_point) const override;
virtual float glyph_or_emoji_width(Utf8CodePointIterator&) const override;
Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibGfx/Painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,10 @@ FLATTEN void Painter::draw_glyph(FloatPoint point, u32 code_point, Font const& f
{
auto top_left = point + FloatPoint(font.glyph_left_bearing(code_point), 0);
auto glyph_position = Gfx::GlyphRasterPosition::get_nearest_fit_for(top_left);
auto glyph = font.glyph(code_point, glyph_position.subpixel_offset);
auto maybe_glyph = font.glyph(code_point, glyph_position.subpixel_offset);
if (!maybe_glyph.has_value())
return;
auto glyph = maybe_glyph.release_value();

if (glyph.is_color_bitmap()) {
float scaled_width = glyph.advance();
Expand Down