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

Fix Woff2 bounds decompression and size measurement calculations. #380

Merged
merged 5 commits into from
Feb 9, 2024

Conversation

JimBobSquarePants
Copy link
Member

@JimBobSquarePants JimBobSquarePants commented Feb 8, 2024

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Fixes #375 #373 #367

We were calculating the bounds bitmap length calculation incorrectly which led to the wrong bounds being read.

Further investigation into determining the correct bounds led to the discovery that the bounds calculation could be simplified and that the size was being incorrectly calculated.

I've added some additional visual tooling in the samples project that helps to verify measurement results. This helped me to discover that we were not rendering the results of some ligature substitutions if the remaining glyph code point was non-rendering. The output is pretty cool.

👩🏻‍🤝‍👩🏼

I also fixed the line breaking algorithm to include a missed breaking opportunity and exactly match browser output.

@@ -552,7 +556,9 @@ private static IReadOnlyList<GlyphLayout> LayoutText(TextBox textBox, TextOption
advanceX,
data.ScaledAdvance,
GlyphLayoutMode.Vertical,
i == 0));
i == 0 && j == 0));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevents individual metrics within a composite glyph adding to the advance.

for (int i = removalIndices.Length - 1; i >= 0; i--)
{
int match = removalIndices[i];
codePointCount += this.glyphs[match].Data.CodePointCount;
CodePoint currentCodePoint = this.glyphs[match].Data.CodePoint;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do not update the code point, then we can end up with a codepoint that is then skipped during rendering.

for (int i = count; i > 0; i--)
{
int match = index + i;
codePointCount += this.glyphs[match].Data.CodePointCount;
CodePoint currentCodePoint = this.glyphs[match].Data.CodePoint;
if (!UnicodeUtility.IsDefaultIgnorableCodePoint((uint)codePoint.Value) || UnicodeUtility.ShouldRenderWhiteSpaceOnly(codePoint))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When substituting a ligation, we need to ensure that the remaining codepoint is renderable.

FontRectangle bounds = GetBounds(glyphLayouts, dpi);
return new FontRectangle(0, 0, MathF.Ceiling(bounds.Right - topLeft.X), MathF.Ceiling(bounds.Bottom - topLeft.Y));
return new FontRectangle(0, 0, MathF.Ceiling(bounds.Width), MathF.Ceiling(bounds.Height));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know the bounds width/height here so should use it directly.

@@ -1166,35 +1184,6 @@ private static void SubstituteBidiMirrors(FontMetrics fontMetrics, GlyphSubstitu
: metric.FontMetrics.VerticalMetrics;
float ascender = metricsHeader.Ascender * scaleY;

// Adjust ascender for glyphs with a negative tsb. e.g. emoji to prevent cutoff.
if (!CodePoint.IsWhiteSpace(codePoint))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a hack based upon incorrect assumptions. The proper fix is to use a larger line spacing to meet the font design.

@JimBobSquarePants JimBobSquarePants merged commit cda8d06 into main Feb 9, 2024
8 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/fix-375 branch February 9, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants