Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/PixelSnapHelpers.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.7 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Windows;
21using System.Windows.Media;
22
23namespace ICSharpCode.AvalonEdit.Utils
24{
25  /// <summary>
26  /// Contains static helper methods for aligning stuff on a whole number of pixels.
27  /// </summary>
28  public static class PixelSnapHelpers
29  {
30    /// <summary>
31    /// Gets the pixel size on the screen containing visual.
32    /// This method does not take transforms on visual into account.
33    /// </summary>
34    public static Size GetPixelSize(Visual visual)
35    {
36      if (visual == null)
37        throw new ArgumentNullException("visual");
38      PresentationSource source = PresentationSource.FromVisual(visual);
39      if (source != null) {
40        Matrix matrix = source.CompositionTarget.TransformFromDevice;
41        return new Size(matrix.M11, matrix.M22);
42      } else {
43        return new Size(1, 1);
44      }
45    }
46   
47    /// <summary>
48    /// Aligns <paramref name="value"/> on the next middle of a pixel.
49    /// </summary>
50    /// <param name="value">The value that should be aligned</param>
51    /// <param name="pixelSize">The size of one pixel</param>
52    public static double PixelAlign(double value, double pixelSize)
53    {
54      // 0 -> 0.5
55      // 0.1 -> 0.5
56      // 0.5 -> 0.5
57      // 0.9 -> 0.5
58      // 1 -> 1.5
59      return pixelSize * (Math.Round((value / pixelSize) + 0.5) - 0.5);
60    }
61   
62    /// <summary>
63    /// Aligns the borders of rect on the middles of pixels.
64    /// </summary>
65    public static Rect PixelAlign(Rect rect, Size pixelSize)
66    {
67      rect.X = PixelAlign(rect.X, pixelSize.Width);
68      rect.Y = PixelAlign(rect.Y, pixelSize.Height);
69      rect.Width = Round(rect.Width, pixelSize.Width);
70      rect.Height = Round(rect.Height, pixelSize.Height);
71      return rect;
72    }
73   
74    /// <summary>
75    /// Rounds <paramref name="point"/> to whole number of pixels.
76    /// </summary>
77    public static Point Round(Point point, Size pixelSize)
78    {
79      return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
80    }
81   
82    /// <summary>
83    /// Rounds val to whole number of pixels.
84    /// </summary>
85    public static Rect Round(Rect rect, Size pixelSize)
86    {
87      return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
88                      Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
89    }
90   
91    /// <summary>
92    /// Rounds <paramref name="value"/> to a whole number of pixels.
93    /// </summary>
94    public static double Round(double value, double pixelSize)
95    {
96      return pixelSize * Math.Round(value / pixelSize);
97    }
98   
99    /// <summary>
100    /// Rounds <paramref name="value"/> to an whole odd number of pixels.
101    /// </summary>
102    public static double RoundToOdd(double value, double pixelSize)
103    {
104      return Round(value - pixelSize, pixelSize * 2) + pixelSize;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.