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 const value root font size loading #2

Open
wants to merge 3 commits into
base: render
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/AngleSharp.Css/RenderTree/RenderTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public IRenderNode RenderDocument()
var stylesheets = _defaultSheets.Concat(currentSheets).ToList();
var collection = new StyleCollection(stylesheets, _device);
var rootStyle = collection.ComputeCascadedStyle(document.DocumentElement);
var rootFontSize = ((Length?) rootStyle.GetProperty(PropertyNames.FontSize)?.RawValue)?.Value ?? 16;
var rootFontValue = rootStyle.GetProperty(PropertyNames.FontSize)?.RawValue;
var rootFontSize = rootFontValue switch
{
Length length => length.Value,
Constant<Length> constant => constant.Value.Value,
_ => 16
};
return RenderElement(rootFontSize, document.DocumentElement, collection);
}

Expand Down
6 changes: 5 additions & 1 deletion src/AngleSharp.Css/Values/Primitives/Length.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public Double ToPixel(IRenderDimensions renderDimensions)
return _value * 96.0 / 72.0;
case Unit.Cm: // 1 cm = 50/127 in
return _value * 50.0 * 96.0 / 127.0;
case Unit.Ex:
CheckForValidRenderDimensionsForFont(renderDimensions);
// 1ex is the x-height of the font, or half of 1em.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't true. It depends on the font. If there are places where we can use an approximate value, can we do that calculation there?
Screenshot 2025-01-13 at 11 24 57 AM

return _value * 0.5 * renderDimensions.FontSize;
case Unit.Px: // 1 px = 1/96 in
return _value;
case Unit.Rem:
Expand All @@ -309,7 +313,7 @@ public Double ToPixel(IRenderDimensions renderDimensions)
CheckForValidRenderDimensions(renderDimensions, RenderMode.Vertical);
return _value * 0.01 * Math.Min(renderDimensions.RenderHeight, renderDimensions.RenderWidth);
default:
throw new InvalidOperationException("Unsupported unit cannot be converted.");
throw new InvalidOperationException($"Unsupported unit '{_unit}' cannot be converted.");
}
}

Expand Down