137 lines
4.1 KiB
Plaintext
137 lines
4.1 KiB
Plaintext
// #import "base.kl"
|
|
#load "gdi32.kl"
|
|
#load "user32.kl"
|
|
|
|
String16 :: struct;; str: *U16; len: S64
|
|
String32 :: struct;; str: *U32; len: S64
|
|
UTF32_Result :: struct
|
|
out_str: U32
|
|
advance: S64
|
|
error : S32
|
|
UTF16_Result :: struct
|
|
out_str: [2]U16
|
|
len : S32
|
|
error : S32
|
|
|
|
utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result
|
|
result: UTF32_Result
|
|
if (c[0] & 0b10000000) == 0
|
|
if max_advance >= 1
|
|
c0 := c[0]->U32
|
|
result.out_str = c0
|
|
result.advance = 1
|
|
else;; result.error = 1
|
|
|
|
elif (c[0] & 0b11100000) == 0b11000000
|
|
if (c[1] & 0b11000000) == 0b10000000 // Continuation byte required
|
|
if max_advance >= 2
|
|
c0 := c[0]->U32; c1 := c[1]->U32
|
|
result.out_str = (c0 & 0b00011111) << 6 | (c1 & 0b00111111)
|
|
result.advance = 2
|
|
else;; result.error = 2
|
|
else;; result.error = 2
|
|
|
|
elif (c[0] & 0b11110000) == 0b11100000
|
|
if (c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000 // Two continuation bytes required
|
|
if max_advance >= 3
|
|
c0 := c[0]->U32; c1 := c[1]->U32; c2 := c[2]->U32
|
|
result.out_str = (c0 & 0b00001111) << 12 | (c1 & 0b00111111) << 6 | (c2 & 0b00111111)
|
|
result.advance = 3
|
|
else;; result.error = 3
|
|
else;; result.error = 3
|
|
|
|
elif (c[0] & 0b11111000) == 0b11110000
|
|
if (c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000 && (c[3] & 0b11000000) == 0b10000000 // Three continuation bytes required
|
|
if max_advance >= 4
|
|
c0 := c[0]->U32; c1 := c[1]->U32; c2 := c[2]->U32; c3 := c[3]->U32
|
|
result.out_str = (c0 & 0b00001111) << 18 | (c1 & 0b00111111) << 12 | (c2 & 0b00111111) << 6 | (c3 & 0b00111111)
|
|
result.advance = 4
|
|
else;; result.error = 4
|
|
else;; result.error = 4
|
|
else;; result.error = 4
|
|
|
|
return result
|
|
|
|
utf32_to_utf16 :: (codepoint: U32): UTF16_Result
|
|
result: UTF16_Result
|
|
if codepoint < 0x10000
|
|
result.out_str[0] = codepoint->U16
|
|
result.out_str[1] = 0
|
|
result.len = 1
|
|
elif codepoint <= 0x10FFFF
|
|
code: U32 = (codepoint - 0x10000)
|
|
result.out_str[0] = (0xD800 | (code >> 10))->U16
|
|
result.out_str[1] = (0xDC00 | (code & 0x3FF))->U16
|
|
result.len = 2
|
|
else
|
|
result.error = 1
|
|
|
|
return result
|
|
|
|
// string_to_string16 :: (allocator: *Allocator, int: String): String16
|
|
// String16 result = {exp_alloc_array(allocator, U16, (in.len*2)+1)}; // @Note(Krzosa): Should be more then enough space
|
|
// for(S64 i = 0; i < in.len;){
|
|
// UTF32_Result decode = utf8_to_utf32(in.str + i, in.len - i);
|
|
// if(!decode.error){
|
|
// i += decode.advance;
|
|
// UTF16_Result encode = utf32_to_utf16(decode.out_str);
|
|
// if(!encode.error){
|
|
// for(S32 j = 0; j < encode.len; j++){
|
|
// result.str[result.len++] = encode.out_str[j];
|
|
// }
|
|
// }
|
|
// else unicode_error(question_mark16);
|
|
// }
|
|
// else unicode_error(question_mark16);
|
|
// }
|
|
|
|
// result.str[result.len] = 0;
|
|
// return result;
|
|
// }
|
|
|
|
Vec2I :: struct;; x: S32; y: S32
|
|
Vec2 :: struct;; x: F32; y: F32
|
|
Windows_Bitmap :: struct
|
|
size: Vec2I
|
|
data: *U32
|
|
hdc: HDC
|
|
dib: HBITMAP
|
|
|
|
create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
|
|
result: Windows_Bitmap = {size = size}
|
|
if bottom_up == false
|
|
result.size.y = -result.size.y
|
|
|
|
bminfo := BITMAPINFO{
|
|
BITMAPINFOHEADER{
|
|
biSize = size_of(BITMAPINFOHEADER),
|
|
biWidth = size.x->LONG,
|
|
biHeight = size.y->LONG,
|
|
biPlanes = 1,
|
|
biBitCount = 32,
|
|
biCompression = BI_RGB,
|
|
biXPelsPerMeter = 1,
|
|
biYPelsPerMeter = 1,
|
|
}
|
|
}
|
|
|
|
hdc := GetDC(0)
|
|
result.dib = CreateDIBSection(hdc, &bminfo, DIB_RGB_COLORS, &result.data->**void, 0, 0)
|
|
result.hdc = CreateCompatibleDC(hdc)
|
|
return result
|
|
|
|
|
|
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
|
|
if msg == WM_DESTROY
|
|
PostQuitMessage(0)
|
|
return 0
|
|
else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
|
|
|
|
main :: (argc: int, argv: **char): int
|
|
bitmap := create_bitmap({1280, 720})
|
|
result := utf8_to_utf32(&"A"[0], 1)
|
|
assert(result.out_str == 'A, "Invalid decode")
|
|
result = utf8_to_utf32(&"ć"[0], 2)
|
|
result = utf8_to_utf32(&"ó"[0], 2)
|
|
|