aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/Encoding.cs
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-11-24 03:00:25 +0100
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-11-24 03:00:25 +0100
commitf005d76468dc4c6a368b6f1f6eca69da25214e80 (patch)
tree14a70c7fb9a58be9083dbd7492ecd94d030c18a9 /Drawing2D/src/Encoding.cs
parent362d83b9556355b1d3dfb6cb892896ed33371521 (diff)
default Crow.CairoBackend ok
Diffstat (limited to 'Drawing2D/src/Encoding.cs')
-rw-r--r--Drawing2D/src/Encoding.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/Drawing2D/src/Encoding.cs b/Drawing2D/src/Encoding.cs
new file mode 100644
index 00000000..8999623d
--- /dev/null
+++ b/Drawing2D/src/Encoding.cs
@@ -0,0 +1,54 @@
+// Copyright (c) 2013-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+//
+// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
+using System;
+
+namespace Drawing2D
+{
+ public static class Extensions
+ {
+ public static int ToUtf8 (this ReadOnlySpan<char> source, Span<byte> buff, int tabWidth = 4) {
+ int c = 0;
+ int encodedBytes = 0;
+ int encodedChar = 0;
+ while (c < source.Length) {
+ if (source[c] < 0xD800) {
+ if (source[c] == '\t') {
+ int encTabWidth = tabWidth - encodedChar % tabWidth;
+ for (int i = 0; i < encTabWidth; i++) {
+ buff[encodedBytes++] = 0x20;
+ encodedChar++;
+ }
+ c++;
+ continue;
+ }
+
+ if (source[c] < 0x80) { //1 byte
+ buff[encodedBytes++] = (byte)source[c++];
+ }else if (source[c] < 0x7FF) { //2 bytes
+ buff[encodedBytes++] = (byte)((source[c] >> 6) + 0xC0);
+ buff[encodedBytes++] = (byte)((source[c] & 0x3F) + 0x80);
+ c++;
+ } else { //3 bytes
+ //ushort ch = (ushort)(source[c] - 0x10000u);
+ buff[encodedBytes++] = (byte)((source[c] >> 12) + 0xE0);
+ buff[encodedBytes++] = (byte)((source[c] >> 6) & 0x00BF);
+ buff[encodedBytes++] = (byte)((source[c] & 0x3F) + 0x80);
+ c++;
+ }
+
+ encodedChar++;
+ continue;
+ }
+ throw new NotImplementedException();
+ }
+ buff[encodedBytes] = 0;
+ return encodedBytes;
+ }
+
+
+ /*unsafe static int Utf16toUtf8 (byte* cptr, byte* buff, int tabWidth) {
+
+ }*/
+ }
+}