OSDN Git Service

46efc6538880882afc18563e16dcc9a7f3308eee
[fooeditengine/FooEditEngine.git] / Core / Util.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10  */
11 using System;
12 using System.Linq;
13 using System.Collections.Generic;
14 using System.Globalization;
15 using System.Text;
16 using System.Reflection;
17 using System.Collections;
18
19 namespace FooEditEngine
20 {
21     class Util
22     {
23 #if METRO || WINDOWS_UWP
24         public static void GetDpi(out float dpix, out float dpiy)
25         {
26             dpix = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
27             dpiy = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
28         }
29
30         public static double GetScale()
31         {
32             float dpi;
33             Util.GetDpi(out dpi, out dpi);
34             return dpi / 96.0;
35         }
36
37         public static Windows.Foundation.Point GetClientPoint(Windows.Foundation.Point screen, Windows.UI.Xaml.UIElement element)
38         {
39             //Windows10以降では補正する必要がある
40             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
41             screen.X -= win_rect.X;
42             screen.Y -= win_rect.Y;
43
44             var gt = element.TransformToVisual(element);
45             return gt.TransformPoint(screen);
46         }
47         public static Point GetScreentPoint(Point client, Windows.UI.Xaml.UIElement element)
48         {
49             //ウィンドウ内での絶対座標を取得する
50             var gt = element.TransformToVisual(element);
51             Point screenPoint = gt.TransformPoint(client);
52
53             //Windows10以降では補正する必要がある
54             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
55             screenPoint.X += win_rect.X;
56             screenPoint.Y += win_rect.Y;
57
58             return screenPoint;
59         }
60         public static Windows.Foundation.Rect GetClientRect(Windows.Foundation.Rect screen, Windows.UI.Xaml.UIElement element)
61         {
62             //Windows10以降では補正する必要がある
63             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
64             screen.X -= win_rect.X;
65             screen.Y -= win_rect.Y;
66
67             var gt = element.TransformToVisual(element);
68             return gt.TransformBounds(screen);
69         }
70         public static Windows.Foundation.Rect GetScreentRect(Windows.Foundation.Rect client, Windows.UI.Xaml.UIElement element)
71         {
72             //ウィンドウ内での絶対座標を取得する
73             var gt = element.TransformToVisual(element);
74             Windows.Foundation.Rect screenRect = gt.TransformBounds(client);
75
76             //Windows10以降では補正する必要がある
77             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
78             screenRect.X += win_rect.X;
79             screenRect.Y += win_rect.Y;
80
81             return screenRect;
82         }
83         public static IEnumerable<char> GetEnumrator(string s)
84         {
85             char[] chars = s.ToCharArray();
86             return chars;
87         }
88 #else
89         public static IEnumerable<char> GetEnumrator(string s)
90         {
91             return s;
92         }
93 #endif
94
95         public static T ConvertAbsIndexToRelIndex<T>(T n, int StartIndex, int Length) where T : IRange
96         {
97             n = Util.NormalizeIMaker<T>(n);
98
99             int markerEnd = n.start + n.length - 1;
100
101             int EndIndex = StartIndex + Length;
102
103             if (n.start >= StartIndex && markerEnd <= EndIndex)
104                 n.start -= StartIndex;
105             else if (n.start >= StartIndex && n.start <= EndIndex)
106             {
107                 n.start -= StartIndex;
108                 n.length = EndIndex - StartIndex + 1;
109             }
110             else if (markerEnd >= StartIndex && markerEnd <= EndIndex)
111             {
112                 n.start = 0;
113                 n.length = markerEnd - StartIndex + 1;
114             }
115             else if (n.start >= StartIndex && markerEnd <= EndIndex)
116                 n.start -= StartIndex;
117             else if (n.start <= StartIndex && markerEnd > EndIndex)
118             {
119                 n.start = 0;
120                 n.length = EndIndex - StartIndex + 1;
121             }
122             else
123             {
124                 n.start = -1;
125                 n.length = 0;
126             }
127             return n;
128         }
129
130         public static bool IsWordSeparator(char c)
131         {
132             if (c == Document.NewLine || char.IsSeparator(c) || char.IsPunctuation(c) || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.MathSymbol)
133                 return true;
134             else
135                 return false;
136         }
137         public static void Swap<T>(ref T a, ref T b)
138         {
139             T c = b;
140             b = a;
141             a = c;
142         }
143
144         public static string Generate(char c, int count)
145         {
146             StringBuilder tabstr = new StringBuilder();
147             for (int j = count; j > 0; j--)
148                 tabstr.Append(c);
149             return tabstr.ToString();
150         }
151
152         public static Rectangle OffsetAndDeflate(Rectangle r, Size s)
153         {
154             return new Rectangle(r.X + s.Width,r.Y + s.Height, r.Width - s.Width, r.Height - s.Height);
155         }
156
157         public static T NormalizeIMaker<T>(T m) where T : IRange
158         {
159             if (m.length > 0)
160                 return m;
161             m.start = m.start + m.length;
162             m.length = Math.Abs(m.length);
163             return m;
164         }
165
166         public static int RoundUp(double x)
167         {
168             return (int)(x + 0.5);
169         }
170
171     }
172 }