public static double ToPx(this PrimitiveTerm term, StyleContext context, bool isFont) { switch (term.PrimitiveType) { case UnitType.Centimeter: return Math.Round(((float)term.Value) / 2.54 * context.Global.Dpi); case UnitType.Ems: return Math.Round(((float)term.Value) * context.FontSizePx); case UnitType.Exs: return Math.Round(((float)term.Value) * context.FontSizePx * 0.447265); case UnitType.Inch: return Math.Round(((float)term.Value) * context.Global.Dpi); case UnitType.Millimeter: return Math.Round(((float)term.Value) / 25.4 * context.Global.Dpi); case UnitType.Percentage: return Math.Round(((float)term.Value) * (isFont ? context.FontSizePx : context.ContentWidth) / 100.0); case UnitType.Number: case UnitType.Pixel: return System.Convert.ToDouble(term.Value); case UnitType.Point: return Math.Round(((float)term.Value) * 96.0 / 72); case UnitType.ViewportHeight: return Math.Round(((float)term.Value) * context.Global.ViewportHeight / 100.0); case UnitType.ViewportMax: return Math.Round(((float)term.Value) * Math.Max(context.Global.ViewportWidth, context.Global.ViewportHeight) / 100.0); case UnitType.ViewportMin: return Math.Round(((float)term.Value) * Math.Min(context.Global.ViewportWidth, context.Global.ViewportHeight) / 100.0); case UnitType.ViewportWidth: return Math.Round(((float)term.Value) * context.Global.ViewportWidth / 100.0); default: throw new NotSupportedException(); } }
private static bool MediaMatches(MediaDefinition d, GlobalStyleContext settings) { var context = new StyleContext(); var result = (d.Modifier != MediaTypeModifier.Not && (d.Type & settings.Media) > 0) || (d.Modifier == MediaTypeModifier.Not && (d.Type & settings.Media) == 0); if (result) { var width = d.Properties.Where(p => string.Compare(p.Name, "width", StringComparison.OrdinalIgnoreCase) == 0 || p.Name.EndsWith("-width", StringComparison.OrdinalIgnoreCase)); var plainProps = width.OfType<MediaPropertyPlain>(); double px; foreach (var plain in plainProps) { px = ToPx(plain.Value as PrimitiveTerm, context, false); if (plain.Name.StartsWith("max-", StringComparison.OrdinalIgnoreCase) && px > settings.MediaWidth) return false; if (plain.Name.StartsWith("min-", StringComparison.OrdinalIgnoreCase) && px < settings.MediaWidth) return false; } var rangeProps = width.OfType<MediaPropertyRange>(); foreach (var range in rangeProps) { if (range.LowerBound != null) { px = ToPx(range.LowerBound as PrimitiveTerm, context, false); if (settings.MediaWidth < px || (settings.MediaWidth == px && !range.LowerCompare.EndsWith("="))) return false; } if (range.UpperBound != null) { px = ToPx(range.UpperBound as PrimitiveTerm, context, false); if (settings.MediaWidth > px || (settings.MediaWidth == px && !range.UpperCompare.EndsWith("="))) return false; } } } return result; }