1 module skia.SKString;
2 
3 import skia.SkiaApi;
4 import skia.Definitions;
5 import skia.Exceptions;
6 import skia.SKObject;
7 
8 class SKString : SKObject, ISKSkipObjectRegistration
9 {
10 	this(void* handle, bool owns)
11 	{
12 		super(handle, owns);
13 	}
14 
15 	this()
16 	{
17 		super(SkiaApi.sk_string_new_empty(), true);
18 		if (Handle is null)
19 		{
20 			throw new InvalidOperationException("Unable to create a new SKString instance.");
21 		}
22 	}
23 
24 	this(byte[] src, long length)
25 	{
26 		super(CreateCopy(src, length), true);
27 		if (Handle is null)
28 		{
29 			throw new InvalidOperationException("Unable to copy the SKString instance.");
30 		}
31 	}
32 
33 	private static void* CreateCopy(byte[] src, long length)
34 	{
35 		byte* s = src.ptr;
36 		return SkiaApi.sk_string_new_with_copy(cast(const(char)*)s, cast(size_t)length);
37 	}
38 
39 	this(byte[] src)
40 	{
41 		this(src, cast(long)src.length);
42 	}
43 
44 	this(string str)
45 	{
46 		this(cast(byte[])str);
47 	}
48 
49 	override string toString()
50 	{
51 		auto cstr = SkiaApi.sk_string_get_c_str(cast(sk_string_t*)Handle);
52 		auto clen = SkiaApi.sk_string_get_size(cast(sk_string_t*)Handle);
53 		// return StringUtilities.GetString(cast(void*) cstr, cast(int) clen, SKTextEncoding.Utf8);
54 		return cast(string)cstr[0..clen].dup;
55 	}
56 
57 	string opCast(string)() {
58 		return toString();
59 	}
60 
61 	static SKString Create(string str)
62 	{
63 		if (str is null)
64 		{
65 			return null;
66 		}
67 		return new SKString(str);
68 	}
69 
70 	// protected override void Dispose(bool disposing)
71 	// {
72 	// 	return super.Dispose(disposing);
73 	// }
74 
75 	protected override void DisposeNative()
76 	{
77 		return SkiaApi.sk_string_destructor(cast(sk_string_t*)Handle);
78 	}
79 
80 	static SKString GetObject(void* handle)
81 	{
82 		return handle is null ? null : new SKString(cast(sk_string_t*)handle, true);
83 	}
84 }