Skip to content

Commit

Permalink
LibWeb/Canvas: Calculate text width for measureText correctly
Browse files Browse the repository at this point in the history
Previously this simply added the widths of each glyph, not accounting
for glyph spacing.
  • Loading branch information
circl-lastname authored and awesomekling committed Oct 6, 2023
1 parent 28c9015 commit 274e0f4
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,10 @@ CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(De
// ...and with all other properties set to their initial values.
// FIXME: Actually use a LineBox here instead of, you know, using the default font and measuring its size (which is not the spec at all).
// FIXME: Once we have CanvasTextDrawingStyles, add the CSS attributes.
size_t width = 0;
size_t width = font->width(text.view());
size_t height = font->pixel_size();

Utf8View replaced_text_view { replaced_text };
for (auto it = replaced_text_view.begin(); it != replaced_text_view.end(); ++it)
width += font->glyph_or_emoji_width(it);

// 6. If maxWidth was provided and the hypothetical width of the inline box in the hypothetical line box is greater than maxWidth CSS pixels, then change font to have a more condensed font (if one is available or if a reasonably readable one can be synthesized by applying a horizontal scale factor to the font) or a smaller font, and return to the previous step.
// FIXME: Record the font size used for this piece of text, and actually retry with a smaller size if needed.
Expand Down

0 comments on commit 274e0f4

Please sign in to comment.