diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs index 0e8fb5abf..8a0adf36d 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs @@ -1,4 +1,5 @@ -using mbts.ui.utilities; +using mbts.ui.dpi; +using mbts.ui.utilities; using mbts.ui.utility.text; using System; using System.ComponentModel; @@ -18,9 +19,12 @@ namespace mbts.ui.controls { Brush _background, _foreground_backing, _border_brush_backing; double _glyph_width => _gcr.glyph_run.AdvanceWidths.Sum(); GlyphRun _glyph_run => _gcr.glyph_run; - bool _is_mouse_over_text, _can_execute; + bool _is_mouse_over_text, _can_execute, _snaps_to_device_pixels; Pen _underline_pen_backing, _border_stroke_pen_backing; - GlyphContentRect _gcr = new GlyphContentRect(); + GlyphContentRect _gcr = new GlyphContentRect { + // add 2 pixels to account for underline (1 for space, 1 for the line itself) + content_height_offset = HighDpiExtensions.pixel_unit * 2 + }; Brush _foreground { get => _foreground_backing; @@ -37,13 +41,23 @@ namespace mbts.ui.controls { } } Thickness _border_thickness { - get => _gcr.border_thickness; + get => _snaps_to_device_pixels ? _gcr.border_thickness.RoundToLayout() : _gcr.border_thickness; set { _gcr.border_thickness = new Thickness(value.Left); _border_stroke_pen_backing = null; } } - Pen _underline_pen => _underline_pen_backing ?? (_foreground == null ? null : (_underline_pen_backing = new Pen(_foreground, 1))); + Pen _underline_pen { + get { + if (_underline_pen_backing == null) { + var fg = _foreground; + if (fg != null) { + _underline_pen_backing = new Pen(fg, _snaps_to_device_pixels ? HighDpiExtensions.pixel_unit : 1); + } + } + return _underline_pen_backing; + } + } Pen _border_stroke_pen => // border_thickness.Left is used as the uniform border thickness. This can be changed if need be. _border_stroke_pen_backing == null && _border_brush != null && _border_thickness.Left > 0 ? @@ -64,6 +78,11 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._gcr.width = v, double.NaN); + WPFHelper.override_metadata( + SnapsToDevicePixelsProperty, + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._snaps_to_device_pixels = v, + true); } public HyperlinkCell() { @@ -73,7 +92,7 @@ namespace mbts.ui.controls { _gcr.min_height = MinHeight; _gcr.height = Height; _gcr.max_height = Height; - _gcr.padding = _underline_padding(Padding); + _gcr.padding = Padding; _gcr.border_thickness = BorderThickness; _gcr.horizontal_alignment = HorizontalAlignment; _gcr.horizontal_content_alignment = HorizontalContentAlignment; @@ -86,6 +105,7 @@ namespace mbts.ui.controls { if (Text is string str && !string.IsNullOrEmpty(str)) _gcr.text_lines = new[] { str }; _gcr.glyph_context = GlyphContext.create(FontFamily, FontWeight); + _snaps_to_device_pixels = SnapsToDevicePixels; } #region BorderThickness @@ -135,7 +155,8 @@ namespace mbts.ui.controls { public static readonly DependencyProperty VerticalContentAlignmentProperty = Control.VerticalContentAlignmentProperty.AddOwner(typeof(HyperlinkCell), new FrameworkPropertyMetadata( VerticalAlignment.Center, - FrameworkPropertyMetadataOptions.AffectsArrange, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, (d, e) => ((HyperlinkCell)d)._gcr.vertical_content_alignment = (VerticalAlignment)e.NewValue)); public VerticalAlignment VerticalContentAlignment { @@ -215,7 +236,7 @@ namespace mbts.ui.controls { nameof(Padding), FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, - (p, v) => p._gcr.padding = _underline_padding(v), DIYGrid.DefaultCellPadding); + (p, v) => p._gcr.padding = default, default); public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } #endregion @@ -347,25 +368,23 @@ namespace mbts.ui.controls { var rs = RenderSize; var rect = new Rect(0, 0, rs.Width, rs.Height); var bg = Background; - if (bg != null) { - drawingContext.DrawRectangle(bg, _border_stroke_pen, rect); - } - if (_glyph_run != null) { + drawingContext.DrawRectangle(bg, _border_stroke_pen, rect, _snaps_to_device_pixels); + var gr = _glyph_run; + if (gr != null) { drawingContext.PushClip(new RectangleGeometry(rect)); - drawingContext.DrawGlyphRun(_foreground, _glyph_run); + drawingContext.DrawGlyphRun(_foreground, gr); if (_is_mouse_over_text) { // draw underline - double x = _gcr.glyph_run.BaselineOrigin.X; - double y = _gcr.glyph_run.BaselineOrigin.Y + 2; - drawingContext.DrawLine(_underline_pen, new Point(x, y), new Point(x + _gcr.glyph_run.AdvanceWidths.Sum(), y)); + var ul_pen = _underline_pen; + if (ul_pen != null) { + var line_start = new Point(gr.BaselineOrigin.X, gr.BaselineOrigin.Y + 2 * ul_pen.Thickness); + drawingContext.DrawHorizontalLine(ul_pen, line_start, gr.AdvanceWidths.Sum(), _snaps_to_device_pixels); + } } drawingContext.Pop(); } } - static Thickness _underline_padding(Thickness padding) => - new Thickness(padding.Left, padding.Top, padding.Right, padding.Bottom + 2); - void _check_mouse_over_text(MouseEventArgs e) { var pt = e.GetPosition(this); var orig = _gcr.content_area; diff --git a/src/client/spark/view/Extensions/HighDpiExtensions.cs b/src/client/spark/view/Extensions/HighDpiExtensions.cs index 755972641..94c1af4db 100644 --- a/src/client/spark/view/Extensions/HighDpiExtensions.cs +++ b/src/client/spark/view/Extensions/HighDpiExtensions.cs @@ -1,4 +1,5 @@ -using mbts.config; +using bts.utils; +using mbts.config; using mbts.ui.controls; using System; using System.Collections.Generic; @@ -127,6 +128,13 @@ namespace mbts.ui.dpi { } #endregion + public static Point PointToScreenPixels(this Visual visual, double x, double y) => + PointToScreenPixels(visual, new Point(x, y)); + + public static Point PointToScreenPixels(this Visual visual, Point point) { + var pt = visual.PointToScreen(point); + return new Point(pt.X * pixel_unit, pt.Y * pixel_unit); + } /// /// Calculates the value to be used for layout rounding at high DPI. /// @@ -139,16 +147,22 @@ namespace mbts.ui.dpi { public static double RoundLayoutValue(this double value) => RoundLayoutValue(value, dpi_ratio); - public static double RoundLayoutValue(double value, double dpi_scale) { - if (DoubleUtil.AreClose(dpi_scale, 1.0)) + public static double RoundLayoutValue(double value, double dpiScale) { + if (DoubleUtil.AreClose(dpiScale, 1.0)) return Math.Round(value); - double result = Math.Round(value * dpi_scale) / dpi_scale; + double result = Math.Round(value * dpiScale) / dpiScale; return DoubleHelper.IsNaNOrInfinity(result) || DoubleUtil.AreClose(result, double.MaxValue) ? value : result; } public static Point RoundToLayout(this Point point) => new Point(RoundLayoutValue(point.X), RoundLayoutValue(point.Y)); + public static Rect RoundToLayout(this Rect rect) => + new Rect(rect.TopLeft.RoundToLayout(), rect.Size.RoundToLayout()); + + public static System.Windows.Size RoundToLayout(this System.Windows.Size size) => + new System.Windows.Size(size.Width.RoundLayoutValue(), size.Height.RoundLayoutValue()); + public static Thickness RoundToLayout(this Thickness thickness) => new Thickness( thickness.Left.RoundLayoutValue(), @@ -167,5 +181,72 @@ namespace mbts.ui.dpi { new[] { rect.Top.RoundLayoutValue() - half_pen_thickness, rect.Bottom.RoundLayoutValue() + half_pen_thickness }); return guidelines; } + + public static void DrawHorizontalLine(this DrawingContext drawing_context, System.Windows.Media.Pen pen, Point start, double length, bool snap_to_device_pixels) { + if (pen == null || !length.IsRealNonZero()) { + // no need to flip out and throw an exception + return; + } + if (snap_to_device_pixels) { + start = start.RoundToLayout(); + var half_pen = pen.Thickness / 2; + var stop = new Point((start.X + length).RoundLayoutValue(), start.Y); + var (left_edge, right_edge) = MathUtils.get_outliers(start.X, stop.X); + var guidelines = new GuidelineSet( + new[] { left_edge + half_pen, right_edge - half_pen }, + new[] { start.Y - half_pen }); + drawing_context.PushGuidelineSet(guidelines); + drawing_context.DrawLine(pen, start, stop); + drawing_context.Pop(); + } else { + drawing_context.DrawLine(pen, start, new Point(start.X + length, start.Y)); + } + } + + public static void DrawLine(this DrawingContext drawing_context, System.Windows.Media.Pen pen, Point point0, Point point1, bool snap_to_device_pixels) { + if (pen == null) { + // no need to flip out and throw an exception + return; + } + if (snap_to_device_pixels) { + point0 = point0.RoundToLayout(); + point1 = point1.RoundToLayout(); + var (left_edge, right_edge) = MathUtils.get_outliers(point0.X, point1.X); + var (top_edge, bottom_edge) = MathUtils.get_outliers(point0.Y, point1.Y); + var x = point0.X; + var y = point0.Y; + var half_pen = pen.Thickness / 2; + drawing_context.PushGuidelineSet(new GuidelineSet( + new[] { left_edge + half_pen, right_edge - half_pen }, + new[] { top_edge + half_pen, bottom_edge - half_pen })); + drawing_context.DrawLine(pen, point0, point1); + drawing_context.Pop(); + } else { + drawing_context.DrawLine(pen, point0, point1); + } + } + + public static void DrawRectangle(this DrawingContext drawing_context, System.Windows.Media.Brush brush, System.Windows.Media.Pen pen, Rect rectangle, bool snap_to_device_pixels) { + if ((brush == null && pen == null) || rectangle.IsEmpty) { + // no need to flip out and throw an exception + return; + } + if (snap_to_device_pixels) { + if (pen != null) { + if (rectangle.Width > pen.Thickness && rectangle.Height > pen.Thickness) { + var half_pen = pen.Thickness / 2; + rectangle = rectangle.RoundToLayout(); + rectangle = new Rect(rectangle.Left + half_pen, rectangle.Top + half_pen, rectangle.Width - pen.Thickness, rectangle.Height - pen.Thickness); + drawing_context.DrawRectangle(brush, pen, rectangle); + } + } else { + drawing_context.PushGuidelineSet(rectangle.AntiAliasGuidelines(0)); + drawing_context.DrawRectangle(brush, pen, rectangle); + drawing_context.Pop(); + } + } else { + drawing_context.DrawRectangle(brush, pen, rectangle); + } + } } } diff --git a/src/client/spark/view/Utility/GlyphContext.cs b/src/client/spark/view/Utility/GlyphContext.cs index 045552da7..147c615fd 100644 --- a/src/client/spark/view/Utility/GlyphContext.cs +++ b/src/client/spark/view/Utility/GlyphContext.cs @@ -1,4 +1,5 @@ -using mbts.ui.dpi; +using bts.utils; +using mbts.ui.dpi; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -231,7 +232,8 @@ namespace mbts.ui.utility.text { public bool arrange_glyph_run( IReadOnlyList lines, double line_spacing, TextAlignment text_alignment, double font_size, - out ushort[] glyph_indices, out double[] advance_widths, out Point[] glyph_offsets, out double glyph_width, out double glyph_height) { + out ushort[] glyph_indices, out double[] advance_widths, out Point[] glyph_offsets, out double glyph_width, out double glyph_height, + bool use_layout_rounding = true) { // glyph_width = 0; glyph_height = 0; @@ -258,7 +260,8 @@ namespace mbts.ui.utility.text { new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, indices, origin, advance_widths, offsets, null, null, null, null, null); bool _try_get_glyph_params_left_aligned( - IReadOnlyList lines, double line_spacing, double font_size, ref ushort[] glyph_indices, ref double[] advance_widths, ref Point[] glyph_offsets, out double glyph_width, out double glyph_height) { + IReadOnlyList lines, double line_spacing, double font_size, ref ushort[] glyph_indices, ref double[] advance_widths, ref Point[] glyph_offsets, out double glyph_width, out double glyph_height, + bool use_layout_rounding = true) { // glyph_width = 0; glyph_height = 0; @@ -266,9 +269,12 @@ namespace mbts.ui.utility.text { int n = 0; foreach (string text in lines) { double line_width = 0; + double max_height_mult = 0; foreach (char c in text) { - if (!_char_map.TryGetValue(c, out (ushort index, double width) gc)) + if (!_char_map.TryGetValue(c, out (ushort index, double width) gc) || + !_glyph_typeface.AdvanceHeights.TryGetValue(c, out var height_mult)) return false; + MathUtils.exchange_if_gt(ref max_height_mult, height_mult); glyph_indices[n] = gc.index; double w = gc.width * font_size; advance_widths[n] = w; @@ -279,7 +285,8 @@ namespace mbts.ui.utility.text { x_offset -= line_width; if (line_width > glyph_width) glyph_width = line_width; - glyph_height += (font_size + line_spacing); + glyph_height += (font_size * max_height_mult + line_spacing); + max_height_mult = 0; } glyph_height -= line_spacing; // remove the last line space return true; diff --git a/src/client/spark/view/Utility/IVisualContentRect.cs b/src/client/spark/view/Utility/IVisualContentRect.cs index bae0f8c3d..8aefa733b 100644 --- a/src/client/spark/view/Utility/IVisualContentRect.cs +++ b/src/client/spark/view/Utility/IVisualContentRect.cs @@ -113,7 +113,11 @@ namespace mbts.ui { bool _arrange_dirty; // fields of public properties - double _font_size, _line_spacing; + double + _font_size, + _content_width_offset, + _content_height_offset, + _line_spacing; IReadOnlyList _text_lines; GlyphContext _glyph_context; GlyphRun _glyph_run; @@ -135,6 +139,8 @@ namespace mbts.ui { public double max_width { get => _vcr.max_width; set => _vcr.max_width = value; } public double min_height { get => _vcr.min_height; set => _vcr.min_height = value; } public double height { get => _vcr.height; set => _vcr.height = value; } + public double content_width_offset { get => _content_width_offset; set => _set(ref _content_width_offset, value); } + public double content_height_offset { get => _content_height_offset; set => _set(ref _content_height_offset, value); } public double max_height { get => _vcr.max_height; set => _vcr.max_height = value; } public Size available_size { get => _vcr.available_size; set => _vcr.available_size = value; } public Size layout_size { get { _ensure_current(); return _vcr.layout_size; } } @@ -145,6 +151,28 @@ namespace mbts.ui { return null; _ensure_current(); Point pt = _vcr.content_area.BottomLeft; + if (_content_width_offset != 0) { + switch (horizontal_alignment) { + case HorizontalAlignment.Center: + case HorizontalAlignment.Stretch: + pt.X -= _content_width_offset / 2; + break; + default: + pt.X -= _content_width_offset; + break; + } + } + if (_content_height_offset != 0) { + switch (vertical_alignment) { + case VerticalAlignment.Center: + case VerticalAlignment.Stretch: + pt.Y -= _content_height_offset / 2; + break; + default: + pt.Y -= _content_height_offset; + break; + } + } if (_glyph_run == null || _glyph_run.BaselineOrigin != pt) _glyph_run = _glyph_context.from_arranged(_font_size, pt, _glyph_indices, _advance_widths, _glyph_offsets); return _glyph_run; @@ -162,12 +190,12 @@ namespace mbts.ui { void _ensure_current() { if (_arrange_dirty) { if (_text_lines.is_null_or_empty()) { - _vcr.content_width = 0; - _vcr.content_height = 0; + _vcr.content_width = _content_width_offset; + _vcr.content_height = _content_height_offset; } else { _glyph_context.arrange_glyph_run(_text_lines, _line_spacing, TextAlignment.Left, _font_size, out _glyph_indices, out _advance_widths, out _glyph_offsets, out double glyph_width, out double glyph_height); - _vcr.content_width = glyph_width; - _vcr.content_height = glyph_height; + _vcr.content_width = glyph_width + _content_width_offset; + _vcr.content_height = glyph_height + _content_height_offset; } _glyph_run = null; _arrange_dirty = false; diff --git a/src/client/spark_api/utils/MathUtils.cs b/src/client/spark_api/utils/MathUtils.cs index e4807316f..ffa6ebe26 100644 --- a/src/client/spark_api/utils/MathUtils.cs +++ b/src/client/spark_api/utils/MathUtils.cs @@ -11,6 +11,15 @@ namespace bts.utils { public static IComparer error_margin_comparer(double error_margin) => new ErrorMarginComparer.DoubleImpl(error_margin); + public static T exchange_if_gt(ref T target, T value) where T : struct, IComparable { + if (value.CompareTo(target) > 0) { + var orig = target; + target = value; + return orig; + } + return target; + } + public static bool epsilon_equals(double x, double y) => Math.Abs(x - y) < epsilon; @@ -128,6 +137,34 @@ namespace bts.utils { return result < 0 ? result + mod : result; } + public static (T min, T max) get_outliers(params T[] values) where T : struct, IComparable { + if (values.Length == 0) { + return (default, default); + } + switch (values.Length) { + case 0: return (default, default); + case 1: return (values[0], values[0]); + default: + T min = values[0], max = values[values.Length - 1]; + if (min.CompareTo(max) > 0) + _swap(ref min, ref max); + for (int i = 1; i < values.Length; i++) { + if (min.CompareTo(values[i]) > 0) { + _swap(ref values[i], ref min); + } else if (max.CompareTo(values[i]) < 0) { + _swap(ref values[i], ref max); + } + } + return (min, max); + } + } + + static void _swap(ref T a, ref T b) { + var temp = a; + a = b; + b = temp; + } + public static class Primes { public const int P_1087 = 1087, @@ -168,4 +205,4 @@ namespace bts.utils { } } } -} \ No newline at end of file +}