1 module skia.Util;
2 
3 import skia.Definitions;
4 import skia.Exceptions;
5 
6 // import skia.SkiaApi;
7 
8 import std.math;
9 import std.range;
10 
11 enum
12 {
13 	UTF8,
14 	UTF16,
15 	UTF32
16 }
17 
18 bool HasFlag(E)(E e, E flag) if (is(E == enum))
19 {
20 	if (e & flag)
21 		return true;
22 
23 	return false;
24 }
25 
26 struct Utils
27 {
28 	enum float NearlyZero = 1.0f / (1 << 12);
29 
30 	static byte[] AsSpan(void* ptr, int size)
31 	{
32 		return (cast(byte*) ptr)[0 .. size];
33 	}
34 
35 	static const(byte)[] AsReadOnlySpan(void* ptr, int size)
36 	{
37 		return cast(const(byte)[])(ptr[0 .. size]);
38 	}
39 
40 	static bool NearlyEqual(float a, float b, float tolerance)
41 	{
42 		return abs(a - b) <= tolerance;
43 	}
44 
45 	static byte[] GetBytes(SKEncoding encoding, const(char)[] text)
46 	{
47 		return cast(byte[]) text.dup;
48 		// if (text.Length == 0)
49 		// 	return new byte[0];
50 
51 		// char* t = text; {
52 		// 	var byteCount = encoding.GetByteCount (t, text.Length);
53 		// 	if (byteCount == 0)
54 		// 		return new byte[0];
55 
56 		// 	var bytes = new byte[byteCount];
57 		// 	byte* b = bytes;
58 		// 	encoding.GetBytes (t, text.Length, b, byteCount);
59 		// 	return bytes;
60 		// }
61 	}
62 
63 	// static RentedArray!(T) RentArray(T)(int length)
64 	// {
65 	// 	return new RentedArray!(T)(length);
66 	// }
67 
68 	// ref struct RentedArray(T)
69 	// {
70 	// 	this(int length)
71 	// 	{
72 	// 		Array = ArrayPool!(T).Shared.Rent(length);
73 	// 		span = new Span!(T)(Array, 0, length);
74 	// 	}
75 
76 	// 	const T[] Array;
77 
78 	// 	const Span!(T) span;
79 
80 	// 	int Length()
81 	//   {
82 	//     return span.Length;
83 	//   } 
84 
85 	// 	void Dispose()
86 	// 	{
87 	// 		// return ArrayPool!(T).Shared.Return(Array);
88 	// 	}
89 
90 	// 	// static explicit operator T[](RentedArray!(T) scope)
91 	// 	// {
92 	// 	// 	return scope.Array;
93 	// 	// }
94 
95 	// 	// static implicit operator Span!(T)(RentedArray!(T) scope)
96 	// 	// {
97 	// 	// 	return scope.Span;
98 	// 	// }
99 
100 	// 	// static implicit operator ReadOnlySpan!(T)(RentedArray!(T) scope)
101 	// 	// {
102 	// 	// 	return scope.Span;
103 	// 	// }
104 	// }
105 
106 	// static bool IsAssignableFrom(this Type type, Type c)
107 	// {
108 	// 	return type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
109 	// }
110 }
111 
112 /**
113  * 
114  */
115 class StringUtilities
116 {
117 	enum string NullTerminator = "\0";
118 
119 	// GetUnicodeStringLength
120 
121 	private static int GetUnicodeStringLength(SKTextEncoding encoding)
122 	{
123 		switch (encoding)
124 		{
125 		case SKTextEncoding.Utf8:
126 			return 1;
127 		case SKTextEncoding.Utf16:
128 			return 1;
129 		case SKTextEncoding.Utf32:
130 			return 2;
131 		default:
132 			// throw new ArgumentOutOfRangeException(encoding.stringof(
133 			// 		"Encoding {encoding} is not supported."));
134 			throw new ArgumentOutOfRangeException("Encoding {encoding} is not supported.");
135 		}
136 	}
137 
138 	// GetCharacterByteSize
139 
140 	static int GetCharacterByteSize(SKTextEncoding encoding)
141 	{
142 		switch (encoding)
143 		{
144 		case SKTextEncoding.Utf8:
145 			return 1;
146 		case SKTextEncoding.Utf16:
147 			return 2;
148 		case SKTextEncoding.Utf32:
149 			return 4;
150 		default:
151 			// throw new ArgumentOutOfRangeException(encoding.stringof(
152 			// 		"Encoding {encoding} is not supported."));
153 			throw new ArgumentOutOfRangeException("Encoding {encoding} is not supported.");
154 		}
155 	}
156 
157 	// GetUnicodeCharacterCode
158 
159 	// static int GetUnicodeCharacterCode(string character, SKTextEncoding encoding)
160 	// {
161 	// 	if (character is null)
162 	// 		throw new ArgumentNullException(character.stringof);
163 	// 	if (GetUnicodeStringLength(encoding) != character.Length)
164 	// 		throw new ArgumentException(character.stringof,
165 	// 				"Only a single character can be specified.");
166 
167 	// 	GetEncodedText bytes = GetEncodedText(character, encoding);
168 	// 	return BitConverter.ToInt32(bytes, 0);
169 	// }
170 
171 	// GetEncodedText
172 
173 	static byte[] GetEncodedText(string text, SKEncoding encoding)
174 	{
175 		return GetEncodedText(cast(const(char)[]) text, encoding.ToTextEncoding());
176 	}
177 
178 	static byte[] GetEncodedText(string text, SKTextEncoding encoding)
179 	{
180 		return GetEncodedText(cast(const(char)[]) text, encoding);
181 	}
182 
183 	static byte[] GetEncodedText(string text, SKTextEncoding encoding, bool addNull)
184 	{
185 		if (!text.empty && addNull)
186 			text ~= NullTerminator;
187 
188 		return GetEncodedText(cast(const(char)[]) text, encoding);
189 	}
190 
191 	static byte[] GetEncodedText(const(char)[] text, SKTextEncoding encoding)
192 	{
193 		// switch (encoding)
194 		// {
195 		// case SKTextEncoding.Utf8:
196 		// 	return Encoding.UTF8.GetBytes(text);
197 		// case SKTextEncoding.Utf16:
198 		// 	return Encoding.Unicode.GetBytes(text);
199 		// case SKTextEncoding.Utf32:
200 		// 	return Encoding.UTF32.GetBytes(text);
201 		// default:
202 		// 	// throw new ArgumentOutOfRangeException(encoding.stringof(
203 		// 	// 		"Encoding {encoding} is not supported."));
204 		//   	throw new ArgumentOutOfRangeException("Encoding {encoding} is not supported.");
205 		// }
206 		return cast(byte[]) text;
207 	}
208 
209 	// GetString
210 
211 	// static string GetString(void* data, int dataLength, SKTextEncoding encoding)
212 	// {
213 	// 	return GetString(data.AsReadOnlySpan(dataLength), 0, dataLength, encoding);
214 	// }
215 
216 	// static string GetString(byte[] data, SKTextEncoding encoding)
217 	// {
218 	// 	return GetString(data, 0, data.Length, encoding);
219 	// }
220 
221 	// static string GetString(byte[] data, int index, int count, SKTextEncoding encoding)
222 	// {
223 	// 	if (data is null)
224 	// 		throw new ArgumentNullException(data.stringof);
225 
226 	// 	switch (encoding)
227 	// 	{
228 	// 	case SKTextEncoding.Utf8:
229 	// 		return Encoding.UTF8.GetString(data, index, count);
230 	// 	case SKTextEncoding.Utf16:
231 	// 		return Encoding.Unicode.GetString(data, index, count);
232 	// 	case SKTextEncoding.Utf32:
233 	// 		return Encoding.UTF32.GetString(data, index, count);
234 	// 	default:
235 	// 		throw new ArgumentOutOfRangeException(encoding.stringof(
236 	// 				"Encoding {encoding} is not supported."));
237 	// 	}
238 	// }
239 
240 	// static string GetString(const(byte)[] data, SKTextEncoding encoding)
241 	// {
242 	// 	return GetString(data, 0, data.Length, encoding);
243 	// }
244 
245 	// static string GetString(const(byte)[] data, int index, int count, SKTextEncoding encoding)
246 	// {
247 	// 	data = data.Slice(index, count);
248 
249 	// 	if (data.Length == 0)
250 	// 		return string.Empty;
251 
252 	// 	byte* bp = data;
253 
254 	// 		switch (encoding)
255 	// 		{
256 	// 		case SKTextEncoding.Utf8:
257 	// 			return Encoding.UTF8.GetString(bp, data.Length);
258 	// 		case SKTextEncoding.Utf16:
259 	// 			return Encoding.Unicode.GetString(bp, data.Length);
260 	// 		case SKTextEncoding.Utf32:
261 	// 			return Encoding.UTF32.GetString(bp, data.Length);
262 	// 		default:
263 	// 			throw new ArgumentOutOfRangeException(encoding.stringof(
264 	// 					"Encoding {encoding} is not supported."));
265 	// 		}
266 
267 	// }
268 
269 }