27 #define DEFAULT_PTSIZE 30 30 #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf" 33 #define DEFAULT_FONT "C:\\Windows\\Fonts\\yugothic.ttf" 35 #define DEFAULT_FONT "NoDefaultFont.ttf" 38 #define DEFAULT_FONT "unifont-9.0.02.hex" 40 #define MAX_TEXT_LENGTH 256 50 static TTF_Font *font;
52 #define UNIFONT_MAX_CODEPOINT 0x1ffff 53 #define UNIFONT_NUM_GLYPHS 0x20000 55 #define UNIFONT_TEXTURE_WIDTH 512 56 #define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / 16) 57 #define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW) 58 #define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE) 59 #define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4) 60 #define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4) 61 #define UNIFONT_DRAW_SCALE 2 73 if (c >=
'0' && c <=
'9')
75 else if (c >=
'a' && c <=
'f')
77 else if (c >=
'A' && c <=
'F')
90 for (; len > 0; cp++, len--)
123 if (unifontTexture ==
NULL)
128 SDL_memset(unifontTexture, 0, unifontTextureSize);
139 int i, codepointHexSize;
140 size_t bytesOverread;
144 bytesRead =
SDL_RWread(hexFile, hexBuffer, 1, 9);
145 if (numGlyphs > 0 && bytesRead == 0)
147 if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
154 if (hexBuffer[2] ==
':')
155 codepointHexSize = 2;
156 else if (hexBuffer[4] ==
':')
157 codepointHexSize = 4;
158 else if (hexBuffer[6] ==
':')
159 codepointHexSize = 6;
160 else if (hexBuffer[8] ==
':')
161 codepointHexSize = 8;
168 if (!
validate_hex((
const char *)hexBuffer, codepointHexSize, &codepoint))
177 bytesOverread = 8 - codepointHexSize;
178 if (codepointHexSize < 8)
179 SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
180 bytesRead =
SDL_RWread(hexFile, hexBuffer + bytesOverread, 1, 33 - bytesOverread);
181 if (bytesRead < (33 - bytesOverread))
186 if (hexBuffer[32] ==
'\n')
191 bytesRead =
SDL_RWread(hexFile, hexBuffer + 33, 1, 32);
213 for (i = 0; i < glyphWidth * 2; i++)
220 }
while (bytesRead > 0);
223 SDL_Log(
"unifont: Loaded %u glyphs.\n", numGlyphs);
232 for (i = 0; i < width * 2; i++)
235 for (j = 0; j < 8; j++)
239 row[0] = textColor.
r;
240 row[1] = textColor.
g;
241 row[2] = textColor.
b;
242 row[3] = textColor.
a;
255 if (width == 8 || (width == 16 && i % 2 == 1))
275 if (textureRGBA ==
NULL)
285 Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID +
i;
311 SDL_Log(
"unifont error: Failed to update texture %u data for renderer %d.\n", textureID, i);
325 srcrect.
w = srcrect.
h = 16;
355 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i +
j];
373 c = (
unsigned char)(0xff & c);
376 else if ((c >> 5) ==0x6)
378 else if ((c >> 4) == 0xe)
380 else if ((c >> 3) == 0x1e)
405 for (; i < distance &&
p; ++
i)
422 codepoint = (0xff >>
len) & *p;
426 codepoint |= 0x3f & *
p;
438 SDL_Log(
"usage: testime [--font fontfile]\n");
444 textRect.
x = textRect.
y = 100;
469 SDL_Rect drawnTextRect, cursorRect, underlineRect;
483 drawnTextRect.
y = textRect.
y + (textRect.
h - textSur->
h) / 2;
484 drawnTextRect.
w = textSur->
w;
485 drawnTextRect.
h = textSur->
h;
498 dstrect.
x = textRect.
x;
502 drawnTextRect.
y = dstrect.
y;
503 drawnTextRect.
h = dstrect.
h;
508 dstrect.
x += advance;
509 drawnTextRect.
w += advance;
515 markedRect.
x = textRect.
x + drawnTextRect.
w;
516 markedRect.
w = textRect.
w - drawnTextRect.
w;
517 if (markedRect.
w < 0)
528 cursorRect = drawnTextRect;
529 cursorRect.
x += cursorRect.
w;
531 cursorRect.
h = drawnTextRect.
h;
533 drawnTextRect.
x += drawnTextRect.
w;
554 cursorRect.
x += drawnTextRect.
w;
557 textSur = TTF_RenderUTF8_Blended(font,
markedText, textColor);
559 drawnTextRect.
y = textRect.
y + (textRect.
h - textSur->
h) / 2;
560 drawnTextRect.
w = textSur->
w;
561 drawnTextRect.
h = textSur->
h;
575 dstrect.
x = drawnTextRect.
x;
579 drawnTextRect.
y = dstrect.
y;
580 drawnTextRect.
h = dstrect.
h;
585 dstrect.
x += advance;
586 drawnTextRect.
w += advance;
588 cursorRect.
x += advance;
596 cursorRect.
y = drawnTextRect.
y;
597 cursorRect.
h = drawnTextRect.
h;
601 underlineRect.
y = drawnTextRect.
y + drawnTextRect.
h - 2;
603 underlineRect.
w = drawnTextRect.
w;
632 int main(
int argc,
char *argv[])
646 for (i = 1; i < argc;i++) {
649 for (argc--, argv++; argc > 0; argc--, argv++)
651 if (strcmp(argv[0],
"--help") == 0) {
656 else if (strcmp(argv[0],
"--font") == 0)
691 SDL_Log(
"Using font: %s\n", fontname);
727 if ((
text[textlen-1] & 0x80) == 0x00)
730 text[textlen-1]=0x00;
733 if ((
text[textlen-1] & 0xC0) == 0x80)
736 text[textlen-1]=0x00;
739 if ((
text[textlen-1] & 0xC0) == 0xC0)
742 text[textlen-1]=0x00;
757 SDL_Log(
"Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
782 SDL_Log(
"text editing \"%s\", selected range (%d, %d)\n",
786 cursor =
event.edit.start;
#define SDL_CreateTexture
static Uint8 dehex(char c)
static SDL_Color backColor
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
#define UNIFONT_NUM_GLYPHS
#define UNIFONT_DRAW_SCALE
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES]
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
static Uint8 dehex2(char c1, char c2)
#define UNIFONT_GLYPHS_IN_ROW
#define SDL_RenderFillRect
char * utf8_next(char *p)
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
A collection of pixels used in software blitting.
#define SDL_SetTextureBlendMode
#define SDL_SetRenderDrawBlendMode
#define SDL_RWread(ctx, ptr, size, n)
static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
#define UNIFONT_TEXTURE_WIDTH
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
#define UNIFONT_MAX_CODEPOINT
#define SDL_UpdateTexture
static SDL_Color lineColor
GLint GLint GLsizei width
Uint32 utf8_decode(char *p, size_t len)
GLsizei GLsizei GLfloat distance
static SDL_Texture ** unifontTexture
char * utf8_advance(char *p, size_t distance)
#define UNIFONT_GLYPHS_IN_TEXTURE
GLenum GLenum GLuint texture
#define SDL_StopTextInput
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]
#define SDL_CreateTextureFromSurface
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
static SDL_Renderer * renderer
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
void _Redraw(int rendererID)
struct UnifontGlyph * unifontGlyph
char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
int main(int argc, char *argv[])
static SDLTest_CommonState * state
#define SDL_StartTextInput
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
SDL_Renderer ** renderers
static int unifont_init(const char *fontname)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
#define SDL_LogSetPriority
SDL_TextEditingEvent edit
static void unifont_cleanup()
static char text[MAX_TEXT_LENGTH]
#define SDL_GetScancodeName
#define SDL_SetTextInputRect
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
#define SDL_DestroyTexture
#define UNIFONT_TEXTURE_SIZE
GLenum const void GLbitfield GLsizei numGlyphs
size_t utf8_length(unsigned char c)
#define SDL_SetRenderDrawColor
#define UNIFONT_TEXTURE_PITCH
#define UNIFONT_NUM_TEXTURES
#define DEFAULT_WINDOW_WIDTH
static SDL_Rect markedRect
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
static int unifont_load_texture(Uint32 textureID)
static SDL_Color textColor
A rectangle, with the origin at the upper left.
#define SDL_RenderPresent
#define SDL_TEXTEDITINGEVENT_TEXT_SIZE