Rename Codepoint typedef to cc_unichar to more accurately reflect what it is

Bah, strings. A unicode codepoint definitely does not fit in 16 bits
This commit is contained in:
UnknownShadow200 2020-10-17 16:47:18 +11:00
parent a69ca77e2c
commit 4e982f3b4b
8 changed files with 55 additions and 55 deletions

View file

@ -96,7 +96,7 @@ typedef uintptr_t cc_uintptr;
#define CC_VAR
#endif
typedef cc_uint16 Codepoint;
typedef cc_uint16 cc_unichar;
typedef cc_uint8 cc_bool;
#ifdef __APPLE__
/* TODO: REMOVE THIS AWFUL AWFUL HACK */

View file

@ -1021,7 +1021,7 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) {
String text = args->text;
int i, width = 0, charWidth;
FT_Error res;
Codepoint cp;
cc_unichar uc;
for (i = 0; i < text.length; i++) {
char c = text.buffer[i];
@ -1032,8 +1032,8 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) {
charWidth = font->widths[(cc_uint8)c];
/* need to calculate glyph width */
if (charWidth == UInt16_MaxValue) {
cp = Convert_CP437ToUnicode(c);
res = FT_Load_Char(face, cp, 0);
uc = Convert_CP437ToUnicode(c);
res = FT_Load_Char(face, uc, 0);
if (res) {
Platform_Log2("Error %i measuring width of %r", &res, &c);
@ -1118,7 +1118,7 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int
FT_Bitmap* img;
int i, offset;
FT_Error res;
Codepoint cp;
cc_unichar uc;
if (shadow) {
glyphs = font->shadow_glyphs;
@ -1142,8 +1142,8 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int
glyph = glyphs[(cc_uint8)c];
if (!glyph) {
cp = Convert_CP437ToUnicode(c);
res = FT_Load_Char(face, cp, FT_LOAD_RENDER);
uc = Convert_CP437ToUnicode(c);
res = FT_Load_Char(face, uc, FT_LOAD_RENDER);
if (res) {
Platform_Log2("Error %i drawing %r", &res, &text.buffer[i]);

View file

@ -637,7 +637,7 @@ static void Http_BackendFree(void) {
#endif
#ifdef UNICODE
#define Platform_DecodeString(dst, src, len) String_AppendUtf16(dst, (Codepoint*)(src), (len) * 2)
#define Platform_DecodeString(dst, src, len) String_AppendUtf16(dst, (cc_unichar*)(src), (len) * 2)
#else
#define Platform_DecodeString(dst, src, len) String_DecodeCP1252(dst, (cc_uint8*)(src), len)
#endif

View file

@ -19,7 +19,7 @@
#endif
#ifdef UNICODE
#define Platform_DecodeString(dst, src, len) String_AppendUtf16(dst, (Codepoint*)(src), (len) * 2)
#define Platform_DecodeString(dst, src, len) String_AppendUtf16(dst, (cc_unichar*)(src), (len) * 2)
#else
#define Platform_DecodeString(dst, src, len) String_DecodeCP1252(dst, (cc_uint8*)(src), len)
#endif

View file

@ -442,7 +442,7 @@ cc_result Stream_ReadU32_BE(struct Stream* s, cc_uint32* value) {
*#########################################################################################################################*/
cc_result Stream_ReadLine(struct Stream* s, String* text) {
cc_bool readAny = false;
Codepoint cp;
cc_unichar uc;
cc_result res;
cc_uint8 tmp[8];
@ -457,19 +457,19 @@ cc_result Stream_ReadLine(struct Stream* s, String* text) {
do {
if ((res = s->ReadU8(s, &tmp[len]))) break;
len++;
} while (!Convert_Utf8ToUnicode(&cp, tmp, len));
} while (!Convert_Utf8ToUnicode(&uc, tmp, len));
if (res == ERR_END_OF_STREAM) break;
if (res) return res;
readAny = true;
/* Handle \r\n or \n line endings */
if (cp == '\r') continue;
if (cp == '\n') return 0;
if (uc == '\r') continue;
if (uc == '\n') return 0;
/* ignore byte order mark */
if (cp == 0xFEFF) continue;
String_Append(text, Convert_UnicodeToCP437(cp));
if (uc == 0xFEFF) continue;
String_Append(text, Convert_UnicodeToCP437(uc));
}
return readAny ? 0 : ERR_END_OF_STREAM;
}

View file

@ -470,14 +470,14 @@ void String_Format4(String* str, const char* format, const void* a1, const void*
/*########################################################################################################################*
*------------------------------------------------Character set conversions------------------------------------------------*
*#########################################################################################################################*/
static const Codepoint controlChars[32] = {
static const cc_unichar controlChars[32] = {
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
};
static const Codepoint extendedChars[129] = { 0x2302,
static const cc_unichar extendedChars[129] = { 0x2302,
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
@ -496,70 +496,70 @@ static const Codepoint extendedChars[129] = { 0x2302,
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
Codepoint Convert_CP437ToUnicode(char c) {
cc_unichar Convert_CP437ToUnicode(char c) {
cc_uint8 raw = (cc_uint8)c;
if (raw < 0x20) return controlChars[raw];
if (raw < 0x7F) return raw;
return extendedChars[raw - 0x7F];
}
char Convert_UnicodeToCP437(Codepoint cp) {
char c; Convert_TryUnicodeToCP437(cp, &c); return c;
char Convert_UnicodeToCP437(cc_unichar uc) {
char c; Convert_TryUnicodeToCP437(uc, &c); return c;
}
cc_bool Convert_TryUnicodeToCP437(Codepoint cp, char* c) {
cc_bool Convert_TryUnicodeToCP437(cc_unichar uc, char* c) {
int i;
if (cp >= 0x20 && cp < 0x7F) { *c = (char)cp; return true; }
if (uc >= 0x20 && uc < 0x7F) { *c = (char)uc; return true; }
for (i = 0; i < Array_Elems(controlChars); i++) {
if (controlChars[i] == cp) { *c = i; return true; }
if (controlChars[i] == uc) { *c = i; return true; }
}
for (i = 0; i < Array_Elems(extendedChars); i++) {
if (extendedChars[i] == cp) { *c = i + 0x7F; return true; }
if (extendedChars[i] == uc) { *c = i + 0x7F; return true; }
}
*c = '?'; return false;
}
int Convert_Utf8ToUnicode(Codepoint* cp, const cc_uint8* data, cc_uint32 len) {
*cp = '\0';
int Convert_Utf8ToUnicode(cc_unichar* uc, const cc_uint8* data, cc_uint32 len) {
*uc = '\0';
if (!len) return 0;
if (data[0] <= 0x7F) {
*cp = data[0];
*uc = data[0];
return 1;
} else if ((data[0] & 0xE0) == 0xC0) {
if (len < 2) return 0;
*cp = ((data[0] & 0x1F) << 6) | ((data[1] & 0x3F));
*uc = ((data[0] & 0x1F) << 6) | ((data[1] & 0x3F));
return 2;
} else if ((data[0] & 0xF0) == 0xE0) {
if (len < 3) return 0;
*cp = ((data[0] & 0x0F) << 12) | ((data[1] & 0x3F) << 6)
*uc = ((data[0] & 0x0F) << 12) | ((data[1] & 0x3F) << 6)
| ((data[2] & 0x3F));
return 3;
} else {
if (len < 4) return 0;
*cp = ((data[0] & 0x07) << 18) | ((data[1] & 0x3F) << 12)
*uc = ((data[0] & 0x07) << 18) | ((data[1] & 0x3F) << 12)
| ((data[2] & 0x3F) << 6) | (data[3] & 0x3F);
return 4;
}
}
int Convert_UnicodeToUtf8(Codepoint cp, cc_uint8* data) {
if (cp <= 0x7F) {
data[0] = (cc_uint8)cp;
int Convert_UnicodeToUtf8(cc_unichar uc, cc_uint8* data) {
if (uc <= 0x7F) {
data[0] = (cc_uint8)uc;
return 1;
} else if (cp <= 0x7FF) {
data[0] = 0xC0 | ((cp >> 6) & 0x1F);
data[1] = 0x80 | ((cp) & 0x3F);
} else if (uc <= 0x7FF) {
data[0] = 0xC0 | ((uc >> 6) & 0x1F);
data[1] = 0x80 | ((uc) & 0x3F);
return 2;
} else {
data[0] = 0xE0 | ((cp >> 12) & 0x0F);
data[1] = 0x80 | ((cp >> 6) & 0x3F);
data[2] = 0x80 | ((cp) & 0x3F);
data[0] = 0xE0 | ((uc >> 12) & 0x0F);
data[1] = 0x80 | ((uc >> 6) & 0x3F);
data[2] = 0x80 | ((uc) & 0x3F);
return 3;
}
}
@ -573,7 +573,7 @@ int Convert_CP437ToUtf8(char c, cc_uint8* data) {
return Convert_UnicodeToUtf8(Convert_CP437ToUnicode(c), data);
}
void String_AppendUtf16(String* value, const Codepoint* chars, int numBytes) {
void String_AppendUtf16(String* value, const cc_unichar* chars, int numBytes) {
int i; char c;
for (i = 0; i < (numBytes >> 1); i++) {
@ -582,13 +582,13 @@ void String_AppendUtf16(String* value, const Codepoint* chars, int numBytes) {
}
void String_AppendUtf8(String* value, const cc_uint8* chars, int numBytes) {
int len; Codepoint cp; char c;
int len; cc_unichar uc; char c;
for (; numBytes > 0; numBytes -= len) {
len = Convert_Utf8ToUnicode(&cp, chars, numBytes);
len = Convert_Utf8ToUnicode(&uc, chars, numBytes);
if (!len) return;
if (Convert_TryUnicodeToCP437(cp, &c)) String_Append(value, c);
if (Convert_TryUnicodeToCP437(uc, &c)) String_Append(value, c);
chars += len;
}
}

View file

@ -163,24 +163,24 @@ NOTE: This is a low level API. Argument count and types are not checked at all.
CC_API void String_Format4(String* str, const char* format, const void* a1, const void* a2, const void* a3, const void* a4);
/* Converts a code page 437 character to its unicode equivalent. */
Codepoint Convert_CP437ToUnicode(char c);
cc_unichar Convert_CP437ToUnicode(char c);
/* Converts a unicode character to its code page 437 equivalent, or '?' if no match. */
char Convert_UnicodeToCP437(Codepoint cp);
char Convert_UnicodeToCP437(cc_unichar uc);
/* Attempts to convert a unicode character to its code page 437 equivalent. */
cc_bool Convert_TryUnicodeToCP437(Codepoint cp, char* c);
cc_bool Convert_TryUnicodeToCP437(cc_unichar uc, char* c);
/* Decodes a unicode character from UTF8, returning number of bytes read. */
/* Returns 0 if not enough input data to read the character. */
int Convert_Utf8ToUnicode(Codepoint* cp, const cc_uint8* data, cc_uint32 len);
int Convert_Utf8ToUnicode(cc_unichar* uc, const cc_uint8* data, cc_uint32 len);
/* Encodes a unicode character in UTF8, returning number of bytes written. */
/* The number of bytes written is always either 1,2 or 3. */
int Convert_UnicodeToUtf8(Codepoint cp, cc_uint8* data);
int Convert_UnicodeToUtf8(cc_unichar uc, cc_uint8* data);
/* Encodes a code page 437 character in UTF8, returning number of bytes written. */
/* The number of bytes written is always either 1,2 or 3. */
int Convert_CP437ToUtf8(char c, cc_uint8* data);
/* Attempts to append all characters from UTF16 encoded data to the given string. */
/* Characters not in code page 437 are omitted. */
void String_AppendUtf16(String* str, const Codepoint* chars, int numBytes);
void String_AppendUtf16(String* str, const cc_unichar* chars, int numBytes);
/* Attempts to append all characters from UTF8 encoded data to the given string. */
/* Characters not in code page 437 are omitted. */
void String_AppendUtf8(String* str, const cc_uint8* chars, int numBytes);

View file

@ -544,7 +544,7 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
break;
case WM_CHAR:
if (Convert_TryUnicodeToCP437((Codepoint)wParam, &keyChar)) {
if (Convert_TryUnicodeToCP437((cc_unichar)wParam, &keyChar)) {
Event_RaiseInt(&InputEvents.Press, keyChar);
}
break;
@ -755,7 +755,7 @@ void Clipboard_GetText(String* value) {
/* ignore trailing NULL at end */
/* TODO: Verify it's always there */
if (unicode) {
String_AppendUtf16(value, (Codepoint*)src, size - 2);
String_AppendUtf16(value, (cc_unichar*)src, size - 2);
} else {
String_DecodeCP1252(value, (cc_uint8*)src, size - 1);
}
@ -767,7 +767,7 @@ void Clipboard_GetText(String* value) {
}
void Clipboard_SetText(const String* value) {
Codepoint* text;
cc_unichar* text;
HANDLE hGlobal;
int i;
@ -781,7 +781,7 @@ void Clipboard_SetText(const String* value) {
hGlobal = GlobalAlloc(GMEM_MOVEABLE, (value->length + 1) * 2);
if (!hGlobal) { CloseClipboard(); return; }
text = (Codepoint*)GlobalLock(hGlobal);
text = (cc_unichar*)GlobalLock(hGlobal);
for (i = 0; i < value->length; i++, text++) {
*text = Convert_CP437ToUnicode(value->buffer[i]);
}
@ -2003,7 +2003,7 @@ void Clipboard_GetText(String* value) {
if (!(err = PasteboardCopyItemFlavorData(pbRef, itemID, FMT_UTF16, &outData))) {
ptr = CFDataGetBytePtr(outData);
len = CFDataGetLength(outData);
if (ptr) String_AppendUtf16(value, (Codepoint*)ptr, len);
if (ptr) String_AppendUtf16(value, (cc_unichar*)ptr, len);
} else if (!(err = PasteboardCopyItemFlavorData(pbRef, itemID, FMT_UTF8, &outData))) {
ptr = CFDataGetBytePtr(outData);
len = CFDataGetLength(outData);
@ -3671,7 +3671,7 @@ static void JNICALL java_processKeyChar(JNIEnv* env, jobject o, jint code) {
int key = MapNativeKey(code);
Platform_Log2("KEY - PRESS %i,%i", &code, &key);
if (Convert_TryUnicodeToCP437((Codepoint)code, &keyChar)) {
if (Convert_TryUnicodeToCP437((cc_unichar)code, &keyChar)) {
Event_RaiseInt(&InputEvents.Press, keyChar);
}
}