Skip to content

Commit 496593b

Browse files
committed
Added actual logic and docs
1 parent b56645c commit 496593b

25 files changed

+123
-62
lines changed

src/AngleSharp.Css/DefaultRenderDevice.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ public Int32 ViewPortWidth
9191
set;
9292
} = 0;
9393

94-
//todo
95-
public int RenderWidth { get; set; }
96-
public int RenderHeight { get; set; }
97-
public double FontSize { get; set; }
94+
/// <inheritdoc />
95+
public Double RenderWidth => ViewPortWidth;
96+
97+
/// <inheritdoc />
98+
public Double RenderHeight => ViewPortHeight;
99+
100+
/// <inheritdoc />
101+
public double FontSize
102+
{
103+
get;
104+
set;
105+
} = 16;
98106
}
99-
}
107+
}

src/AngleSharp.Css/Extensions/CssValueExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ public static Double AsDouble(this ICssValue value)
4040
/// Tries to convert the value to a number of pixels.
4141
/// </summary>
4242
/// <param name="value">The value to convert.</param>
43+
/// <param name="renderDimensions">the render device used to calculate relative units, can be null if units are absolute.</param>
44+
/// <param name="isWidth">If a relative unit is being calculated, true signifies that that unit is width, false height.</param>
4345
/// <returns>The resulting number.</returns>
44-
public static Double AsPx(this ICssValue value, IRenderDimensions renderDimensions)
46+
public static Double AsPx(this ICssValue value, IRenderDimensions renderDimensions, Boolean isWidth = true)
4547
{
4648
if (value is Length length && length.Type != Length.Unit.None)
4749
{
48-
return length.ToPixel(renderDimensions);
50+
return length.ToPixel(renderDimensions, isWidth);
4951
}
5052
else if (value is ICssMultipleValue multiple && multiple.Count == 1)
5153
{
52-
return multiple[0].AsPx(renderDimensions);
54+
return multiple[0].AsPx(renderDimensions, isWidth);
5355
}
5456
else if (value is ICssSpecialValue special && special.Value != null)
5557
{
56-
return special.Value.AsPx(renderDimensions);
58+
return special.Value.AsPx(renderDimensions, isWidth);
5759
}
5860

5961
return 0.0;
@@ -201,20 +203,21 @@ public static String AsUrl(this ICssValue value)
201203
/// Tries to convert the value to a transformation matrix.
202204
/// </summary>
203205
/// <param name="value">The value to convert.</param>
206+
/// <param name="renderDimensions">the render device used to calculate relative units, can be null if units are absolute.</param>
204207
/// <returns>The resulting matrix.</returns>
205-
public static TransformMatrix AsMatrix(this ICssValue value, IRenderDimensions dimensions)
208+
public static TransformMatrix AsMatrix(this ICssValue value, IRenderDimensions renderDimensions)
206209
{
207210
if (value is ICssTransformFunctionValue res)
208211
{
209-
return res.ComputeMatrix(dimensions);
212+
return res.ComputeMatrix(renderDimensions);
210213
}
211214
else if (value is ICssMultipleValue multiple && multiple.Count == 1)
212215
{
213-
return multiple[0].AsMatrix(dimensions);
216+
return multiple[0].AsMatrix(renderDimensions);
214217
}
215218
else if (value is ICssSpecialValue special && special.Value != null)
216219
{
217-
return special.Value.AsMatrix(dimensions);
220+
return special.Value.AsMatrix(renderDimensions);
218221
}
219222

220223
return null;

src/AngleSharp.Css/FeatureValidators/AspectRatioFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class AspectRatioFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var ratio = RatioConverter.Convert(feature.Value);
1313

1414
if (ratio != null)
1515
{
1616
var desired = ratio.AsDouble();
17-
var available = device.ViewPortWidth / (Double)device.ViewPortHeight;
17+
var available = renderDevice.ViewPortWidth / (Double)renderDevice.ViewPortHeight;
1818

1919
if (feature.IsMaximum)
2020
{

src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace AngleSharp.Css.FeatureValidators
88

99
sealed class ColorFeatureValidator : IFeatureValidator
1010
{
11-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
11+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1212
{
1313
var defaultValue = new Length(1.0, Length.Unit.None);
1414
var converter = feature.IsMinimum || feature.IsMaximum ? PositiveIntegerConverter : PositiveIntegerConverter.Option(defaultValue);
@@ -17,7 +17,7 @@ public Boolean Validate(IMediaFeature feature, IRenderDevice device)
1717
if (color != null)
1818
{
1919
var desired = color.AsInt32();
20-
var available = Math.Pow(device.ColorBits, 2);
20+
var available = Math.Pow(renderDevice.ColorBits, 2);
2121

2222
if (feature.IsMaximum)
2323
{

src/AngleSharp.Css/FeatureValidators/ColorIndexFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace AngleSharp.Css.FeatureValidators
88

99
sealed class ColorIndexFeatureValidator : IFeatureValidator
1010
{
11-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
11+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1212
{
1313
var defaultValue = new Length(1.0, Length.Unit.None);
1414
var converter = feature.IsMinimum || feature.IsMaximum ? NaturalIntegerConverter : NaturalIntegerConverter.Option(defaultValue);
@@ -17,7 +17,7 @@ public Boolean Validate(IMediaFeature feature, IRenderDevice device)
1717
if (index != null)
1818
{
1919
var desired = index.AsInt32();
20-
var available = device.ColorBits;
20+
var available = renderDevice.ColorBits;
2121

2222
if (feature.IsMaximum)
2323
{

src/AngleSharp.Css/FeatureValidators/DeviceAspectRatioFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class DeviceAspectRatioFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var ratio = RatioConverter.Convert(feature.Value);
1313

1414
if (ratio != null)
1515
{
1616
var desired = ratio.AsDouble();
17-
var available = device.DeviceWidth / (Double)device.DeviceHeight;
17+
var available = renderDevice.DeviceWidth / (Double)renderDevice.DeviceHeight;
1818

1919
if (feature.IsMaximum)
2020
{

src/AngleSharp.Css/FeatureValidators/DeviceHeightFeatureValidator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class DeviceHeightFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var length = LengthConverter.Convert(feature.Value);
1313

1414
if (length != null)
1515
{
16-
var desired = length.AsPx(device);
17-
var available = (Double)device.DeviceHeight;
16+
var desired = length.AsPx(renderDevice, false);
17+
var available = (Double)renderDevice.DeviceHeight;
1818

1919
if (feature.IsMaximum)
2020
{

src/AngleSharp.Css/FeatureValidators/DevicePixelRatioFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class DevicePixelRatioFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var value = NaturalNumberConverter.Convert(feature.Value);
1313

1414
if (value != null)
1515
{
1616
var desired = value.AsDouble();
17-
var available = device.Resolution / 96f;
17+
var available = renderDevice.Resolution / 96f;
1818

1919
if (feature.IsMaximum)
2020
{

src/AngleSharp.Css/FeatureValidators/DeviceWidthFeatureValidator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class DeviceWidthFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var length = LengthConverter.Convert(feature.Value);
1313

1414
if (length != null)
1515
{
16-
var desired = length.AsPx(device);
17-
var available = (Double)device.DeviceWidth;
16+
var desired = length.AsPx(renderDevice, true);
17+
var available = (Double)renderDevice.DeviceWidth;
1818

1919
if (feature.IsMaximum)
2020
{

src/AngleSharp.Css/FeatureValidators/GridFeatureValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace AngleSharp.Css.FeatureValidators
77

88
sealed class GridFeatureValidator : IFeatureValidator
99
{
10-
public Boolean Validate(IMediaFeature feature, IRenderDevice device)
10+
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
1111
{
1212
var grid = BinaryConverter.Convert(feature.Value);
1313

1414
if (grid != null)
1515
{
1616
var desired = grid.AsBoolean();
17-
var available = device.IsGrid;
17+
var available = renderDevice.IsGrid;
1818
return desired == available;
1919
}
2020

0 commit comments

Comments
 (0)