1 /* FreeType 2 and UTF-8 encoding support for
11 ================================================================================
12 Function definitions. Taken from the freetype2 headers.
13 ================================================================================
18 (*qFT_Init_FreeType)( FT_Library *alibrary );
20 (*qFT_Done_FreeType)( FT_Library library );
22 (*qFT_New_Face)( FT_Library library,
23 const char* filepathname,
27 (*qFT_New_Memory_Face)( FT_Library library,
28 const FT_Byte* file_base,
33 (*qFT_Done_Face)( FT_Face face );
35 (*qFT_Select_Size)( FT_Face face,
36 FT_Int strike_index );
38 (*qFT_Request_Size)( FT_Face face,
39 FT_Size_Request req );
41 (*qFT_Set_Char_Size)( FT_Face face,
42 FT_F26Dot6 char_width,
43 FT_F26Dot6 char_height,
44 FT_UInt horz_resolution,
45 FT_UInt vert_resolution );
47 (*qFT_Set_Pixel_Sizes)( FT_Face face,
49 FT_UInt pixel_height );
51 (*qFT_Load_Glyph)( FT_Face face,
53 FT_Int32 load_flags );
55 (*qFT_Load_Char)( FT_Face face,
57 FT_Int32 load_flags );
59 (*qFT_Get_Char_Index)( FT_Face face,
62 (*qFT_Render_Glyph)( FT_GlyphSlot slot,
63 FT_Render_Mode render_mode );
66 ================================================================================
67 Support for dynamically loading the FreeType2 library
68 ================================================================================
71 static dllfunction_t ft2funcs[] =
73 {"FT_Init_FreeType", (void **) &qFT_Init_FreeType},
74 {"FT_Done_FreeType", (void **) &qFT_Done_FreeType},
75 {"FT_New_Face", (void **) &qFT_New_Face},
76 {"FT_New_Memory_Face", (void **) &qFT_New_Memory_Face},
77 {"FT_Done_Face", (void **) &qFT_Done_Face},
78 {"FT_Select_Size", (void **) &qFT_Select_Size},
79 {"FT_Request_Size", (void **) &qFT_Request_Size},
80 {"FT_Set_Char_Size", (void **) &qFT_Set_Char_Size},
81 {"FT_Set_Pixel_Sizes", (void **) &qFT_Set_Pixel_Sizes},
82 {"FT_Load_Glyph", (void **) &qFT_Load_Glyph},
83 {"FT_Load_Char", (void **) &qFT_Load_Char},
84 {"FT_Get_Char_Index", (void **) &qFT_Get_Char_Index},
85 {"FT_Render_Glyph", (void **) &qFT_Render_Glyph},
89 /// Handle for FreeType2 DLL
90 static dllhandle_t ft2_dll = NULL;
96 Unload the FreeType2 DLL
99 void FT2_CloseLibrary (void)
101 Sys_UnloadLibrary (&ft2_dll);
109 Try to load the FreeType2 DLL
112 qboolean FT2_OpenLibrary (void)
114 const char* dllnames [] =
117 #error path for freetype 2 dll
119 #error path for freetype 2 dll
120 #elif defined(MACOSX)
134 return Sys_LoadLibrary (dllnames, &ft2_dll, ft2funcs);
138 ================================================================================
139 UTF-8 encoding and decoding functions follow.
140 ================================================================================
143 /** Get the number of characters in in an UTF-8 string.
144 * @param _s An utf-8 encoded null-terminated string.
145 * @return The number of unicode characters in the string.
147 size_t u8_strlen(const char *_s)
150 unsigned char *s = (unsigned char*)_s;
161 // start of a wide character
165 for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
169 // part of a wide character, we ignore that one
170 if (*s <= 0xBF) // 10111111
179 /** Fetch a character from an utf-8 encoded string.
180 * @param _s The start of an utf-8 encoded multi-byte character.
181 * @param _end Will point to after the first multi-byte character.
182 * @return The 32-bit integer representation of the first multi-byte character.
184 Uchar u8_getchar(const char *_s, const char **_end)
186 const unsigned char *s = (unsigned char*)_s;
200 // starting within a wide character - skip it and retrieve the one after it
201 for (++s; *s >= 0x80 && *s < 0xC0; ++s);
202 // or we could return '?' here?
208 for (mask >>= 1; v > (*s & mask); mask >>= 1)
210 u = (Uchar)(*s & mask);
211 for (++s; *s >= 0x80 && *s < 0xC0; ++s)
212 u = (u << 6) | (*s & 0x3F);
215 *_end = (const char*)s;
220 /** Encode a wide-character into utf-8.
221 * @param w The wide character to encode.
222 * @param to The target buffer the utf-8 encoded string is stored to.
223 * @param maxlen The maximum number of bytes that fit into the target buffer.
224 * @return Number of bytes written to the buffer, or less or equal to 0 if the buffer is too small.
226 int u8_fromchar(Uchar w, char *to, size_t maxlen)
244 // check how much space we need and store data into a
245 // temp buffer - this is faster than recalculating again
250 tmp[i++] = 0x80 | (w & 0x3F);
251 bt = (bt >> 1) | 0x80;
253 // see if we still fit into the target buffer
254 if (i+1 >= maxlen) // +1 for the \0
257 // there are no characters which take up that much space yet
258 // and there won't be for the next many many years, still... let's be safe
259 if (i >= sizeof(tmp))
263 for (j = 0; j < i; ++j)
272 /** Convert a utf-8 multibyte string to a wide character string.
273 * @param wcs The target wide-character buffer.
274 * @param mb The utf-8 encoded multibyte string to convert.
275 * @param maxlen The maximum number of wide-characters that fit into the target buffer.
276 * @return The number of characters written to the target buffer.
278 size_t u8_mbstowcs(Uchar *wcs, const char *mb, size_t maxlen)
281 for (i = 0; *mb && i < maxlen; ++i)
282 *wcs++ = u8_getchar(mb, &mb);
288 /** Convert a wide-character string to a utf-8 multibyte string.
289 * @param mb The target buffer the utf-8 string is written to.
290 * @param wcs The wide-character string to convert.
291 * @param maxlen The number bytes that fit into the multibyte target buffer.
292 * @return The number of bytes written, not including the terminating \0
294 size_t u8_wcstombs(char *mb, const Uchar *wcs, size_t maxlen)
297 const char *start = mb;
298 for (i = 0; *wcs && i < maxlen; ++i)
301 if ( (len = u8_fromchar(*wcs++, mb, maxlen - i)) < 0)