2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
2016-10-01 22:43:14 +03:00
|
|
|
/* A simple program to test the Input Method support in the SDL library (2.0+)
|
|
|
|
If you build without SDL_ttf, you can use the GNU Unifont hex file instead.
|
|
|
|
Download at http://unifoundry.com/unifont.html */
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2024-08-02 16:56:51 +03:00
|
|
|
#include <SDL3/SDL_test_font.h>
|
2015-06-21 18:33:46 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
#include "SDL_ttf.h"
|
|
|
|
#endif
|
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
2022-04-12 15:07:18 +03:00
|
|
|
#include "testutils.h"
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
2016-10-01 22:43:14 +03:00
|
|
|
#define DEFAULT_PTSIZE 30
|
2022-12-15 16:11:08 +03:00
|
|
|
#endif
|
|
|
|
|
2016-10-01 22:43:14 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_MACOS
|
2016-10-01 22:43:14 +03:00
|
|
|
#define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
|
2024-01-24 04:40:51 +03:00
|
|
|
#elif defined(SDL_PLATFORM_WIN32)
|
2016-10-01 22:43:14 +03:00
|
|
|
/* Some japanese font present on at least Windows 8.1. */
|
|
|
|
#define DEFAULT_FONT "C:\\Windows\\Fonts\\yugothic.ttf"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_FONT "NoDefaultFont.ttf"
|
|
|
|
#endif
|
|
|
|
#else
|
2024-06-27 05:29:24 +03:00
|
|
|
#define DEFAULT_FONT "unifont-15.1.05.hex"
|
2016-10-01 22:43:14 +03:00
|
|
|
#endif
|
2015-06-21 18:33:46 +03:00
|
|
|
#define MAX_TEXT_LENGTH 256
|
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
#define WINDOW_WIDTH 640
|
|
|
|
#define WINDOW_HEIGHT 480
|
|
|
|
|
|
|
|
#define MARGIN 32.0f
|
|
|
|
#define LINE_HEIGHT (FONT_CHARACTER_SIZE + 4.0f)
|
2024-06-27 23:29:37 +03:00
|
|
|
#define CURSOR_BLINK_INTERVAL_MS 500
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
int rendererID;
|
2024-08-02 16:56:51 +03:00
|
|
|
SDL_bool settings_visible;
|
|
|
|
SDL_Texture *settings_icon;
|
|
|
|
SDL_FRect settings_rect;
|
|
|
|
SDL_PropertiesID text_settings;
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_FRect textRect;
|
|
|
|
SDL_FRect markedRect;
|
|
|
|
char text[MAX_TEXT_LENGTH];
|
|
|
|
char markedText[MAX_TEXT_LENGTH];
|
|
|
|
int cursor;
|
|
|
|
int cursor_length;
|
|
|
|
SDL_bool cursor_visible;
|
|
|
|
Uint64 last_cursor_change;
|
|
|
|
char **candidates;
|
|
|
|
int num_candidates;
|
|
|
|
int selected_candidate;
|
|
|
|
SDL_bool horizontal_candidates;
|
|
|
|
} WindowState;
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
static SDLTest_CommonState *state;
|
2024-08-02 18:39:13 +03:00
|
|
|
static WindowState *windowstate;
|
|
|
|
static const SDL_Color lineColor = { 0, 0, 0, 255 };
|
|
|
|
static const SDL_Color backColor = { 255, 255, 255, 255 };
|
|
|
|
static const SDL_Color textColor = { 0, 0, 0, 255 };
|
2024-06-27 23:50:49 +03:00
|
|
|
static SDL_BlendMode highlight_mode;
|
2024-08-02 18:39:13 +03:00
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const char *label;
|
|
|
|
const char *setting;
|
|
|
|
int value;
|
|
|
|
} settings[] = {
|
|
|
|
{ "Text", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT },
|
|
|
|
{ "Name", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_NAME },
|
|
|
|
{ "E-mail", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_EMAIL },
|
|
|
|
{ "Username", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_USERNAME },
|
|
|
|
{ "Password (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN },
|
|
|
|
{ "Password (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE },
|
|
|
|
{ "Number", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER },
|
|
|
|
{ "Numeric PIN (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN },
|
|
|
|
{ "Numeric PIN (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE },
|
|
|
|
{ "", NULL },
|
|
|
|
{ "No capitalization", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE },
|
|
|
|
{ "Capitalize sentences", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_SENTENCES },
|
|
|
|
{ "Capitalize words", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_WORDS },
|
|
|
|
{ "All caps", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_LETTERS },
|
|
|
|
{ "", NULL },
|
|
|
|
{ "Auto-correct OFF", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, SDL_FALSE },
|
|
|
|
{ "Auto-correct ON", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, SDL_TRUE },
|
|
|
|
{ "Multiline OFF", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, SDL_FALSE },
|
|
|
|
{ "Multiline ON", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, SDL_TRUE }
|
|
|
|
};
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
static TTF_Font *font;
|
2016-10-01 22:43:14 +03:00
|
|
|
#else
|
2022-11-30 23:51:59 +03:00
|
|
|
#define UNIFONT_MAX_CODEPOINT 0x1ffff
|
|
|
|
#define UNIFONT_NUM_GLYPHS 0x20000
|
2024-06-27 06:23:58 +03:00
|
|
|
#define UNIFONT_REPLACEMENT 0xFFFD
|
2016-10-01 22:43:14 +03:00
|
|
|
/* Using 512x512 textures that are supported everywhere. */
|
2022-11-30 23:51:59 +03:00
|
|
|
#define UNIFONT_TEXTURE_WIDTH 512
|
2024-06-27 18:32:28 +03:00
|
|
|
#define UNIFONT_GLYPH_SIZE 16
|
|
|
|
#define UNIFONT_GLYPH_BORDER 1
|
|
|
|
#define UNIFONT_GLYPH_AREA (UNIFONT_GLYPH_BORDER + UNIFONT_GLYPH_SIZE + UNIFONT_GLYPH_BORDER)
|
|
|
|
#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / UNIFONT_GLYPH_AREA)
|
2016-10-01 22:43:14 +03:00
|
|
|
#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)
|
2022-11-30 23:51:59 +03:00
|
|
|
#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)
|
|
|
|
#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)
|
|
|
|
#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)
|
2024-06-27 23:50:49 +03:00
|
|
|
#define UNIFONT_DRAW_SCALE 2.0f
|
2022-12-15 16:11:08 +03:00
|
|
|
static struct UnifontGlyph
|
2022-11-30 23:51:59 +03:00
|
|
|
{
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint8 width;
|
|
|
|
Uint8 data[32];
|
2022-11-30 23:51:59 +03:00
|
|
|
} * unifontGlyph;
|
2016-10-01 22:43:14 +03:00
|
|
|
static SDL_Texture **unifontTexture;
|
2022-11-30 23:51:59 +03:00
|
|
|
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = { 0 };
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
/* Unifont loading code start */
|
|
|
|
|
|
|
|
static Uint8 dehex(char c)
|
|
|
|
{
|
2022-11-27 19:38:43 +03:00
|
|
|
if (c >= '0' && c <= '9') {
|
2016-10-01 22:43:14 +03:00
|
|
|
return c - '0';
|
2022-11-27 19:38:43 +03:00
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
2016-10-01 22:43:14 +03:00
|
|
|
return c - 'a' + 10;
|
2022-11-27 19:38:43 +03:00
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
2016-10-01 22:43:14 +03:00
|
|
|
return c - 'A' + 10;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Uint8 dehex2(char c1, char c2)
|
|
|
|
{
|
|
|
|
return (dehex(c1) << 4) | dehex(c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
|
|
|
|
{
|
|
|
|
Uint32 n = 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
for (; len > 0; cp++, len--) {
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint8 c = dehex(*cp);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (c == 255) {
|
2016-10-01 22:43:14 +03:00
|
|
|
return 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
n = (n << 4) | c;
|
|
|
|
}
|
2023-11-10 00:29:15 +03:00
|
|
|
if (np) {
|
2016-10-01 22:43:14 +03:00
|
|
|
*np = n;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:00:03 +03:00
|
|
|
static int unifont_init(const char *fontname)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
Uint8 hexBuffer[65];
|
|
|
|
Uint32 numGlyphs = 0;
|
|
|
|
int lineNumber = 1;
|
2023-08-06 23:51:03 +03:00
|
|
|
size_t bytesRead;
|
2024-03-15 02:32:50 +03:00
|
|
|
SDL_IOStream *hexFile;
|
2016-10-01 22:43:14 +03:00
|
|
|
const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
|
|
|
|
const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
|
2022-04-12 15:07:18 +03:00
|
|
|
char *filename;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
/* Allocate memory for the glyph data so the file can be closed after initialization. */
|
2016-10-09 21:31:32 +03:00
|
|
|
unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!unifontGlyph) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
SDL_memset(unifontGlyph, 0, unifontGlyphSize);
|
|
|
|
|
|
|
|
/* Allocate memory for texture pointers for all renderers. */
|
2016-10-09 21:31:32 +03:00
|
|
|
unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!unifontTexture) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
SDL_memset(unifontTexture, 0, unifontTextureSize);
|
|
|
|
|
2022-04-12 15:07:18 +03:00
|
|
|
filename = GetResourceFilename(NULL, fontname);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!filename) {
|
2022-04-12 15:07:18 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-15 02:32:50 +03:00
|
|
|
hexFile = SDL_IOFromFile(filename, "rb");
|
2022-04-12 15:07:18 +03:00
|
|
|
SDL_free(filename);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!hexFile) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read all the glyph data into memory to make it accessible later when textures are created. */
|
|
|
|
do {
|
|
|
|
int i, codepointHexSize;
|
2022-12-15 16:19:02 +03:00
|
|
|
size_t bytesOverread;
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint8 glyphWidth;
|
|
|
|
Uint32 codepoint;
|
|
|
|
|
2024-03-15 02:32:50 +03:00
|
|
|
bytesRead = SDL_ReadIO(hexFile, hexBuffer, 9);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (numGlyphs > 0 && bytesRead == 0) {
|
2016-10-01 22:43:14 +03:00
|
|
|
break; /* EOF */
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2022-11-27 19:38:43 +03:00
|
|
|
if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9)) {
|
2017-07-10 00:00:35 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
|
2022-12-02 00:07:03 +03:00
|
|
|
if (hexBuffer[2] == ':') {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepointHexSize = 2;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if (hexBuffer[4] == ':') {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepointHexSize = 4;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if (hexBuffer[6] == ':') {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepointHexSize = 6;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if (hexBuffer[8] == ':') {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepointHexSize = 8;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint)) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
2022-11-27 19:38:43 +03:00
|
|
|
if (codepoint > UNIFONT_MAX_CODEPOINT) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
/* If there was glyph data read in the last file read, move it to the front of the buffer. */
|
|
|
|
bytesOverread = 8 - codepointHexSize;
|
2022-11-27 19:38:43 +03:00
|
|
|
if (codepointHexSize < 8) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2024-03-15 02:32:50 +03:00
|
|
|
bytesRead = SDL_ReadIO(hexFile, hexBuffer + bytesOverread, 33 - bytesOverread);
|
2022-12-15 16:11:08 +03:00
|
|
|
|
2023-08-06 23:51:03 +03:00
|
|
|
if (bytesRead < (33 - bytesOverread)) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
2022-12-02 00:07:03 +03:00
|
|
|
if (hexBuffer[32] == '\n') {
|
2016-10-01 22:43:14 +03:00
|
|
|
glyphWidth = 8;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else {
|
2016-10-01 22:43:14 +03:00
|
|
|
glyphWidth = 16;
|
2024-03-15 02:32:50 +03:00
|
|
|
bytesRead = SDL_ReadIO(hexFile, hexBuffer + 33, 32);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (bytesRead < 32) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL)) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (codepoint <= UNIFONT_MAX_CODEPOINT) {
|
2022-12-02 00:07:03 +03:00
|
|
|
if (unifontGlyph[codepoint].width > 0) {
|
2022-10-10 08:31:14 +03:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08" SDL_PRIx32 " in hex file on line %d.\n", codepoint, lineNumber);
|
2022-12-02 00:07:03 +03:00
|
|
|
} else {
|
2016-10-01 22:43:14 +03:00
|
|
|
unifontGlyph[codepoint].width = glyphWidth;
|
|
|
|
/* Pack the hex data into a more compact form. */
|
2022-11-27 19:38:43 +03:00
|
|
|
for (i = 0; i < glyphWidth * 2; i++) {
|
2016-10-01 22:43:14 +03:00
|
|
|
unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
numGlyphs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lineNumber++;
|
|
|
|
} while (bytesRead > 0);
|
|
|
|
|
2024-03-15 02:32:50 +03:00
|
|
|
SDL_CloseIO(hexFile);
|
2022-10-10 08:31:14 +03:00
|
|
|
SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.\n", numGlyphs);
|
2017-08-28 05:00:03 +03:00
|
|
|
return 0;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
2024-06-27 06:23:58 +03:00
|
|
|
static void unifont_make_rgba(const Uint8 *src, Uint8 *dst, Uint8 width)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
Uint8 *row = dst;
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
for (i = 0; i < width * 2; i++) {
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint8 data = src[i];
|
2022-11-27 19:38:43 +03:00
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (data & 0x80) {
|
2016-10-01 22:43:14 +03:00
|
|
|
row[0] = textColor.r;
|
|
|
|
row[1] = textColor.g;
|
|
|
|
row[2] = textColor.b;
|
|
|
|
row[3] = textColor.a;
|
2022-11-27 19:38:43 +03:00
|
|
|
} else {
|
2016-10-01 22:43:14 +03:00
|
|
|
row[0] = 0;
|
|
|
|
row[1] = 0;
|
|
|
|
row[2] = 0;
|
|
|
|
row[3] = 0;
|
|
|
|
}
|
|
|
|
data <<= 1;
|
|
|
|
row += 4;
|
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (width == 8 || (width == 16 && i % 2 == 1)) {
|
2016-10-01 22:43:14 +03:00
|
|
|
dst += UNIFONT_TEXTURE_PITCH;
|
|
|
|
row = dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:00:03 +03:00
|
|
|
static int unifont_load_texture(Uint32 textureID)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
int i;
|
2022-11-30 23:51:59 +03:00
|
|
|
Uint8 *textureRGBA;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (textureID >= UNIFONT_NUM_TEXTURES) {
|
2022-10-10 08:31:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %" SDL_PRIu32 "\n", textureID);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 21:31:32 +03:00
|
|
|
textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!textureRGBA) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
|
|
|
|
|
|
|
|
/* Copy the glyphs into memory in RGBA format. */
|
2022-11-27 19:38:43 +03:00
|
|
|
for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++) {
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
|
2022-11-27 19:38:43 +03:00
|
|
|
if (unifontGlyph[codepoint].width > 0) {
|
2016-10-01 22:43:14 +03:00
|
|
|
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
|
2024-06-27 18:32:28 +03:00
|
|
|
const size_t offset = ((size_t)cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * UNIFONT_GLYPH_AREA + (cInTex % UNIFONT_GLYPHS_IN_ROW) * UNIFONT_GLYPH_AREA * 4;
|
2016-10-01 22:43:14 +03:00
|
|
|
unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create textures and upload the RGBA data from above. */
|
2022-11-27 19:38:43 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
|
|
|
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
|
2022-11-27 19:38:43 +03:00
|
|
|
if (state->windows[i] == NULL || renderer == NULL || tex != NULL) {
|
2016-10-01 22:43:14 +03:00
|
|
|
continue;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (tex == NULL) {
|
2022-10-10 08:31:14 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %" SDL_PRIu32 " for renderer %d.\n", textureID, i);
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
|
|
|
|
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH) != 0) {
|
2022-10-10 08:31:14 +03:00
|
|
|
SDL_Log("unifont error: Failed to update texture %" SDL_PRIu32 " data for renderer %d.\n", textureID, i);
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_free(textureRGBA);
|
|
|
|
unifontTextureLoaded[textureID] = 1;
|
2017-08-28 05:00:03 +03:00
|
|
|
return 0;
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 20:19:00 +03:00
|
|
|
static int unifont_glyph_width(Uint32 codepoint)
|
|
|
|
{
|
|
|
|
if (codepoint > UNIFONT_MAX_CODEPOINT ||
|
|
|
|
unifontGlyph[codepoint].width == 0) {
|
|
|
|
codepoint = UNIFONT_REPLACEMENT;
|
|
|
|
}
|
|
|
|
return unifontGlyph[codepoint].width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
SDL_Texture *texture;
|
2024-06-27 06:23:58 +03:00
|
|
|
Uint32 textureID;
|
2023-03-02 19:56:54 +03:00
|
|
|
SDL_FRect srcrect;
|
2024-06-27 18:32:28 +03:00
|
|
|
srcrect.w = srcrect.h = (float)UNIFONT_GLYPH_SIZE;
|
2024-06-27 06:23:58 +03:00
|
|
|
|
|
|
|
if (codepoint > UNIFONT_MAX_CODEPOINT ||
|
|
|
|
unifontGlyph[codepoint].width == 0) {
|
|
|
|
codepoint = UNIFONT_REPLACEMENT;
|
2017-08-28 05:00:03 +03:00
|
|
|
}
|
2024-06-27 06:23:58 +03:00
|
|
|
|
|
|
|
textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
|
2017-08-28 05:00:03 +03:00
|
|
|
if (!unifontTextureLoaded[textureID]) {
|
|
|
|
if (unifont_load_texture(textureID) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
|
2023-11-10 00:29:15 +03:00
|
|
|
if (texture) {
|
2016-10-01 22:43:14 +03:00
|
|
|
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
|
2024-06-27 18:32:28 +03:00
|
|
|
srcrect.x = (float)(cInTex % UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA);
|
|
|
|
srcrect.y = (float)(cInTex / UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA);
|
2022-12-31 22:19:32 +03:00
|
|
|
SDL_RenderTexture(state->renderers[rendererID], texture, &srcrect, dst);
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
return unifontGlyph[codepoint].width;
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
static void unifont_cleanup(void)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
int i, j;
|
2022-11-27 19:38:43 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2023-11-10 00:29:15 +03:00
|
|
|
if (state->windows[i] == NULL || !renderer) {
|
2016-10-01 22:43:14 +03:00
|
|
|
continue;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
|
|
|
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j];
|
2023-11-10 00:29:15 +03:00
|
|
|
if (tex) {
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_DestroyTexture(tex);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
|
|
|
|
unifontTextureLoaded[j] = 0;
|
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
SDL_free(unifontTexture);
|
|
|
|
SDL_free(unifontGlyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unifont code end */
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
static size_t utf8_length(unsigned char c)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
c = (unsigned char)(0xff & c);
|
2022-12-02 00:07:03 +03:00
|
|
|
if (c < 0x80) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 1;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if ((c >> 5) == 0x6) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 2;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if ((c >> 4) == 0xe) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 3;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else if ((c >> 3) == 0x1e) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 4;
|
2022-12-02 00:07:03 +03:00
|
|
|
}
|
|
|
|
return 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
static char *utf8_next(char *p)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
size_t len = utf8_length(*p);
|
|
|
|
size_t i = 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!len) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
for (; i < len; ++i) {
|
2015-06-21 18:33:46 +03:00
|
|
|
++p;
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!*p) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
|
|
|
|
static char *utf8_advance(char *p, size_t distance)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
for (; i < distance && p; ++i) {
|
2015-06-21 18:33:46 +03:00
|
|
|
p = utf8_next(p);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2022-12-15 16:11:08 +03:00
|
|
|
#endif
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-07-19 22:07:19 +03:00
|
|
|
static Uint32 utf8_decode(const char *p, size_t len)
|
2016-10-01 22:43:14 +03:00
|
|
|
{
|
|
|
|
Uint32 codepoint = 0;
|
|
|
|
size_t i = 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!len) {
|
2016-10-01 22:43:14 +03:00
|
|
|
return 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
for (; i < len; ++i) {
|
2022-12-02 00:07:03 +03:00
|
|
|
if (i == 0) {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepoint = (0xff >> len) & *p;
|
2022-12-02 00:07:03 +03:00
|
|
|
} else {
|
2016-10-01 22:43:14 +03:00
|
|
|
codepoint <<= 6;
|
|
|
|
codepoint |= 0x3f & *p;
|
|
|
|
}
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!*p) {
|
2016-10-01 22:43:14 +03:00
|
|
|
return 0;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return codepoint;
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static WindowState *GetWindowStateForWindowID(SDL_WindowID windowID)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2024-06-28 23:17:04 +03:00
|
|
|
int i;
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_Window *window = SDL_GetWindowFromID(windowID);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-28 23:17:04 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2024-08-02 18:39:13 +03:00
|
|
|
if (windowstate[i].window == window) {
|
|
|
|
return &windowstate[i];
|
|
|
|
}
|
2024-06-28 23:17:04 +03:00
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
return NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static void InitInput(WindowState *ctx)
|
|
|
|
{
|
|
|
|
/* Prepare a rect for text input */
|
|
|
|
ctx->textRect.x = ctx->textRect.y = 100.0f;
|
|
|
|
ctx->textRect.w = DEFAULT_WINDOW_WIDTH - 2 * ctx->textRect.x;
|
|
|
|
ctx->textRect.h = 50.0f;
|
|
|
|
ctx->markedRect = ctx->textRect;
|
2024-06-26 20:19:00 +03:00
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
ctx->text_settings = SDL_CreateProperties();
|
|
|
|
|
|
|
|
SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings);
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ClearCandidates(WindowState *ctx)
|
2024-06-26 20:19:00 +03:00
|
|
|
{
|
2024-07-27 04:57:18 +03:00
|
|
|
int i;
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
for (i = 0; i < ctx->num_candidates; ++i) {
|
|
|
|
SDL_free(ctx->candidates[i]);
|
2024-07-27 04:57:18 +03:00
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_free(ctx->candidates);
|
|
|
|
ctx->candidates = NULL;
|
|
|
|
ctx->num_candidates = 0;
|
2024-06-26 20:19:00 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static void SaveCandidates(WindowState *ctx, SDL_Event *event)
|
2024-06-26 20:19:00 +03:00
|
|
|
{
|
2024-07-27 04:57:18 +03:00
|
|
|
int i;
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
ClearCandidates(ctx);
|
2024-06-26 20:19:00 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->num_candidates = event->edit_candidates.num_candidates;
|
|
|
|
if (ctx->num_candidates > 0) {
|
|
|
|
ctx->candidates = (char **)SDL_malloc(ctx->num_candidates * sizeof(*ctx->candidates));
|
|
|
|
if (!ctx->candidates) {
|
|
|
|
ctx->num_candidates = 0;
|
2024-07-27 04:57:18 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
for (i = 0; i < ctx->num_candidates; ++i) {
|
|
|
|
ctx->candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]);
|
2024-07-27 04:57:18 +03:00
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->selected_candidate = event->edit_candidates.selected_candidate;
|
|
|
|
ctx->horizontal_candidates = event->edit_candidates.horizontal;
|
2024-06-26 20:19:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static void DrawCandidates(WindowState *ctx, SDL_FRect *cursorRect)
|
2024-06-26 20:19:00 +03:00
|
|
|
{
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_Renderer *renderer = ctx->renderer;
|
|
|
|
int rendererID = ctx->rendererID;
|
2024-06-26 20:19:00 +03:00
|
|
|
int i;
|
|
|
|
int output_w = 0, output_h = 0;
|
|
|
|
float w = 0.0f, h = 0.0f;
|
|
|
|
SDL_FRect candidatesRect, dstRect, underlineRect;
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->num_candidates == 0) {
|
2024-06-26 20:19:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the size of the candidate list */
|
2024-08-02 18:39:13 +03:00
|
|
|
for (i = 0; i < ctx->num_candidates; ++i) {
|
|
|
|
if (!ctx->candidates[i]) {
|
2024-06-26 20:19:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
/* FIXME */
|
|
|
|
#else
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->horizontal_candidates) {
|
|
|
|
const char *utext = ctx->candidates[i];
|
2024-06-26 20:19:00 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
|
|
|
float advance = 0.0f;
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
advance += unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
|
|
|
}
|
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
|
|
|
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
|
|
|
|
utext += len;
|
|
|
|
}
|
|
|
|
w += advance;
|
|
|
|
h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
} else {
|
2024-08-02 18:39:13 +03:00
|
|
|
const char *utext = ctx->candidates[i];
|
2024-06-26 20:19:00 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
|
|
|
float advance = 0.0f;
|
|
|
|
|
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
|
|
|
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
|
|
|
|
utext += len;
|
|
|
|
}
|
|
|
|
w = SDL_max(w, advance);
|
|
|
|
if (i > 0) {
|
|
|
|
h += 2.0f;
|
|
|
|
}
|
|
|
|
h += UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Position the candidate window */
|
|
|
|
SDL_GetCurrentRenderOutputSize(renderer, &output_w, &output_h);
|
|
|
|
candidatesRect.x = cursorRect->x;
|
|
|
|
candidatesRect.y = cursorRect->y + cursorRect->h + 2.0f;
|
|
|
|
candidatesRect.w = 1.0f + 2.0f + w + 2.0f + 1.0f;
|
|
|
|
candidatesRect.h = 1.0f + 2.0f + h + 2.0f + 1.0f;
|
|
|
|
if ((candidatesRect.x + candidatesRect.w) > output_w) {
|
|
|
|
candidatesRect.x = (output_w - candidatesRect.w);
|
|
|
|
if (candidatesRect.x < 0.0f) {
|
|
|
|
candidatesRect.x = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw the candidate background */
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
|
|
|
|
SDL_RenderFillRect(renderer, &candidatesRect);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
|
|
|
SDL_RenderRect(renderer, &candidatesRect);
|
|
|
|
|
|
|
|
/* Draw the candidates */
|
|
|
|
dstRect.x = candidatesRect.x + 3.0f;
|
|
|
|
dstRect.y = candidatesRect.y + 3.0f;
|
2024-08-02 18:39:13 +03:00
|
|
|
for (i = 0; i < ctx->num_candidates; ++i) {
|
|
|
|
if (!ctx->candidates[i]) {
|
2024-06-26 20:19:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
/* FIXME */
|
|
|
|
#else
|
|
|
|
dstRect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
dstRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->horizontal_candidates) {
|
|
|
|
const char *utext = ctx->candidates[i];
|
2024-06-26 20:19:00 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
|
|
|
float start;
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
dstRect.x += unifont_draw_glyph(' ', rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
|
|
|
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
|
|
|
utext += len;
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (i == ctx->selected_candidate) {
|
2024-06-26 20:19:00 +03:00
|
|
|
underlineRect.x = start;
|
|
|
|
underlineRect.y = dstRect.y + dstRect.h - 2;
|
|
|
|
underlineRect.h = 2;
|
|
|
|
underlineRect.w = dstRect.x - start;
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
|
|
|
SDL_RenderFillRect(renderer, &underlineRect);
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-02 18:39:13 +03:00
|
|
|
const char *utext = ctx->candidates[i];
|
2024-06-26 20:19:00 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
|
|
|
float start;
|
|
|
|
|
|
|
|
dstRect.x = candidatesRect.x + 3.0f;
|
|
|
|
|
|
|
|
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
|
|
|
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
|
|
|
utext += len;
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (i == ctx->selected_candidate) {
|
2024-06-26 20:19:00 +03:00
|
|
|
underlineRect.x = start;
|
|
|
|
underlineRect.y = dstRect.y + dstRect.h - 2;
|
|
|
|
underlineRect.h = 2;
|
|
|
|
underlineRect.w = dstRect.x - start;
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
|
|
|
SDL_RenderFillRect(renderer, &underlineRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
dstRect.y += 2.0f;
|
|
|
|
}
|
|
|
|
dstRect.y += dstRect.h;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static void UpdateTextInputArea(WindowState *ctx, const SDL_FRect *cursorRect)
|
2024-06-28 23:17:04 +03:00
|
|
|
{
|
|
|
|
SDL_Rect rect;
|
2024-08-02 18:39:13 +03:00
|
|
|
int cursor_offset = (int)(cursorRect->x - ctx->textRect.x);
|
2024-06-28 23:17:04 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
rect.x = (int)ctx->textRect.x;
|
|
|
|
rect.y = (int)ctx->textRect.y;
|
|
|
|
rect.w = (int)ctx->textRect.w;
|
|
|
|
rect.h = (int)ctx->textRect.h;
|
|
|
|
SDL_SetTextInputArea(ctx->window, &rect, cursor_offset);
|
2024-06-28 23:17:04 +03:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
static void CleanupVideo(void)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2024-08-02 18:39:13 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
WindowState *ctx = &windowstate[i];
|
|
|
|
|
|
|
|
SDL_StopTextInput(ctx->window);
|
|
|
|
ClearCandidates(ctx);
|
2024-08-02 16:56:51 +03:00
|
|
|
SDL_DestroyProperties(ctx->text_settings);
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
TTF_CloseFont(font);
|
|
|
|
TTF_Quit();
|
2016-10-01 22:43:14 +03:00
|
|
|
#else
|
|
|
|
unifont_cleanup();
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
static void DrawSettingsButton(WindowState *ctx)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer = ctx->renderer;
|
|
|
|
|
|
|
|
SDL_RenderTexture(renderer, ctx->settings_icon, NULL, &ctx->settings_rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ToggleSettings(WindowState *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->settings_visible) {
|
|
|
|
ctx->settings_visible = SDL_FALSE;
|
|
|
|
SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings);
|
|
|
|
} else {
|
|
|
|
SDL_StopTextInput(ctx->window);
|
|
|
|
ctx->settings_visible = SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 00:30:45 +03:00
|
|
|
static int GetDefaultSetting(SDL_PropertiesID props, const char *setting)
|
|
|
|
{
|
|
|
|
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_TYPE_NUMBER) == 0) {
|
|
|
|
return SDL_TEXTINPUT_TYPE_TEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER) == 0) {
|
|
|
|
switch (SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) {
|
|
|
|
case SDL_TEXTINPUT_TYPE_TEXT:
|
|
|
|
return SDL_CAPITALIZE_SENTENCES;
|
|
|
|
case SDL_TEXTINPUT_TYPE_TEXT_NAME:
|
|
|
|
return SDL_CAPITALIZE_WORDS;
|
|
|
|
default:
|
|
|
|
return SDL_CAPITALIZE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN) == 0) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN) == 0) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_assert(!"Unknown setting");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
static void DrawSettings(WindowState *ctx)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer = ctx->renderer;
|
|
|
|
SDL_FRect checkbox;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
checkbox.x = MARGIN;
|
|
|
|
checkbox.y = MARGIN;
|
|
|
|
checkbox.w = (float)FONT_CHARACTER_SIZE;
|
|
|
|
checkbox.h = (float)FONT_CHARACTER_SIZE;
|
|
|
|
|
|
|
|
for (i = 0; i < SDL_arraysize(settings); ++i) {
|
|
|
|
if (settings[i].setting) {
|
2024-08-03 00:30:45 +03:00
|
|
|
int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, GetDefaultSetting(ctx->text_settings, settings[i].setting));
|
2024-08-02 16:56:51 +03:00
|
|
|
if (value == settings[i].value) {
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
|
|
|
|
SDL_RenderFillRect(renderer, &checkbox);
|
|
|
|
}
|
|
|
|
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
|
|
|
|
SDL_RenderRect(renderer, &checkbox);
|
|
|
|
SDLTest_DrawString(renderer, checkbox.x + checkbox.w + 8.0f, checkbox.y, settings[i].label);
|
|
|
|
}
|
|
|
|
checkbox.y += LINE_HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ClickSettings(WindowState *ctx, float x, float y)
|
|
|
|
{
|
|
|
|
int setting = (int)SDL_floorf((y - MARGIN) / LINE_HEIGHT);
|
|
|
|
if (setting >= 0 && setting < SDL_arraysize(settings)) {
|
|
|
|
SDL_SetNumberProperty(ctx->text_settings, settings[setting].setting, settings[setting].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
static void RedrawWindow(WindowState *ctx)
|
2017-08-28 05:00:03 +03:00
|
|
|
{
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_Renderer *renderer = ctx->renderer;
|
|
|
|
int rendererID = ctx->rendererID;
|
2022-12-31 22:19:32 +03:00
|
|
|
SDL_FRect drawnTextRect, cursorRect, underlineRect;
|
2024-08-02 16:56:51 +03:00
|
|
|
char text[MAX_TEXT_LENGTH];
|
|
|
|
|
|
|
|
DrawSettingsButton(ctx);
|
|
|
|
|
|
|
|
if (ctx->settings_visible) {
|
|
|
|
DrawSettings(ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hide the text if it's a password */
|
|
|
|
switch ((SDL_TextInputType)SDL_GetNumberProperty(ctx->text_settings, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) {
|
|
|
|
case SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN:
|
|
|
|
case SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN: {
|
|
|
|
size_t len = SDL_utf8strlen(ctx->text);
|
|
|
|
SDL_memset(text, '*', len);
|
|
|
|
text[len] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SDL_strlcpy(text, ctx->text, sizeof(text));
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2016-03-03 22:11:43 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_RenderFillRect(renderer, &ctx->textRect);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
/* Initialize the drawn text rectangle for the cursor */
|
2024-08-02 18:39:13 +03:00
|
|
|
drawnTextRect.x = ctx->textRect.x;
|
|
|
|
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
|
2024-06-27 23:29:37 +03:00
|
|
|
drawnTextRect.w = 0.0f;
|
|
|
|
drawnTextRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
|
2024-08-02 16:56:51 +03:00
|
|
|
if (text[0]) {
|
2016-10-01 22:43:14 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
2024-08-02 16:56:51 +03:00
|
|
|
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
|
2016-03-10 23:00:27 +03:00
|
|
|
SDL_Texture *texture;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
/* Vertically center text */
|
2024-08-02 18:39:13 +03:00
|
|
|
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - textSur->h) / 2;
|
2016-10-01 22:43:14 +03:00
|
|
|
drawnTextRect.w = textSur->w;
|
|
|
|
drawnTextRect.h = textSur->h;
|
2016-03-10 23:00:27 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
texture = SDL_CreateTextureFromSurface(renderer, textSur);
|
2022-12-27 17:36:39 +03:00
|
|
|
SDL_DestroySurface(textSur);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_RenderTexture(renderer, texture, NULL, &drawnTextRect);
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_DestroyTexture(texture);
|
2016-10-01 22:43:14 +03:00
|
|
|
#else
|
2024-08-02 16:56:51 +03:00
|
|
|
char *utext = text;
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
2022-12-31 22:19:32 +03:00
|
|
|
SDL_FRect dstrect;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
dstrect.x = ctx->textRect.x;
|
|
|
|
dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
|
2024-06-27 18:32:28 +03:00
|
|
|
dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
2016-10-01 22:43:14 +03:00
|
|
|
drawnTextRect.y = dstrect.y;
|
|
|
|
drawnTextRect.h = dstrect.h;
|
|
|
|
|
2024-02-03 22:25:29 +03:00
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
2024-06-27 23:50:49 +03:00
|
|
|
float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
|
2016-10-01 22:43:14 +03:00
|
|
|
dstrect.x += advance;
|
|
|
|
drawnTextRect.w += advance;
|
|
|
|
utext += len;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
2016-10-01 22:43:14 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
/* The marked text rectangle is the text area that hasn't been filled by committed text */
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->markedRect.x = ctx->textRect.x + drawnTextRect.w;
|
|
|
|
ctx->markedRect.w = ctx->textRect.w - drawnTextRect.w;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
/* Update the drawn text rectangle for composition text, after the committed text */
|
2016-10-01 22:43:14 +03:00
|
|
|
drawnTextRect.x += drawnTextRect.w;
|
|
|
|
drawnTextRect.w = 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
/* Set the cursor to the new location, we'll update it as we go, below */
|
|
|
|
cursorRect = drawnTextRect;
|
|
|
|
cursorRect.w = 2;
|
|
|
|
cursorRect.h = drawnTextRect.h;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->markedText[0]) {
|
2015-06-21 18:33:46 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
2016-03-10 23:00:27 +03:00
|
|
|
SDL_Surface *textSur;
|
|
|
|
SDL_Texture *texture;
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->cursor) {
|
|
|
|
char *p = utf8_advance(ctx->markedText, ctx->cursor);
|
2015-06-21 18:33:46 +03:00
|
|
|
char c = 0;
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!p) {
|
2024-08-02 18:39:13 +03:00
|
|
|
p = &ctx->markedText[SDL_strlen(ctx->markedText)];
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
c = *p;
|
|
|
|
*p = 0;
|
2024-08-02 18:39:13 +03:00
|
|
|
TTF_SizeUTF8(font, ctx->markedText, &drawnTextRect.w, NULL);
|
2016-10-01 22:43:14 +03:00
|
|
|
cursorRect.x += drawnTextRect.w;
|
2015-06-21 18:33:46 +03:00
|
|
|
*p = c;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
textSur = TTF_RenderUTF8_Blended(font, ctx->markedText, textColor);
|
2016-10-01 22:43:14 +03:00
|
|
|
/* Vertically center text */
|
2024-08-02 18:39:13 +03:00
|
|
|
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - textSur->h) / 2;
|
2016-10-01 22:43:14 +03:00
|
|
|
drawnTextRect.w = textSur->w;
|
|
|
|
drawnTextRect.h = textSur->h;
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
texture = SDL_CreateTextureFromSurface(renderer, textSur);
|
2022-12-27 17:36:39 +03:00
|
|
|
SDL_DestroySurface(textSur);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_RenderTexture(renderer, texture, NULL, &drawnTextRect);
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_DestroyTexture(texture);
|
2024-06-27 23:50:49 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->cursor_length > 0) {
|
2024-06-27 23:50:49 +03:00
|
|
|
/* FIXME: Need to measure text extents */
|
2024-08-02 18:39:13 +03:00
|
|
|
cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
2024-06-27 23:50:49 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
#else
|
|
|
|
int i = 0;
|
2024-08-02 18:39:13 +03:00
|
|
|
char *utext = ctx->markedText;
|
2016-10-01 22:43:14 +03:00
|
|
|
Uint32 codepoint;
|
|
|
|
size_t len;
|
2022-12-31 22:19:32 +03:00
|
|
|
SDL_FRect dstrect;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
|
|
|
dstrect.x = drawnTextRect.x;
|
2024-08-02 18:39:13 +03:00
|
|
|
dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
|
2024-06-27 18:32:28 +03:00
|
|
|
dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
|
|
|
dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
2016-10-01 22:43:14 +03:00
|
|
|
drawnTextRect.y = dstrect.y;
|
|
|
|
drawnTextRect.h = dstrect.h;
|
|
|
|
|
2024-02-03 22:25:29 +03:00
|
|
|
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
2024-06-27 23:50:49 +03:00
|
|
|
float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
|
2016-10-01 22:43:14 +03:00
|
|
|
dstrect.x += advance;
|
|
|
|
drawnTextRect.w += advance;
|
2024-08-02 18:39:13 +03:00
|
|
|
if (i < ctx->cursor) {
|
2016-10-01 22:43:14 +03:00
|
|
|
cursorRect.x += advance;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
i++;
|
|
|
|
utext += len;
|
|
|
|
}
|
2024-06-27 23:50:49 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->cursor_length > 0) {
|
|
|
|
cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
2024-06-27 23:50:49 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
cursorRect.y = drawnTextRect.y;
|
|
|
|
cursorRect.h = drawnTextRect.h;
|
2016-10-01 22:43:14 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
underlineRect = ctx->markedRect;
|
2016-10-01 22:43:14 +03:00
|
|
|
underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
|
2015-06-21 18:33:46 +03:00
|
|
|
underlineRect.h = 2;
|
2016-10-01 22:43:14 +03:00
|
|
|
underlineRect.w = drawnTextRect.w;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2016-03-03 22:11:43 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
2016-10-01 22:43:14 +03:00
|
|
|
SDL_RenderFillRect(renderer, &underlineRect);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2024-06-27 23:29:37 +03:00
|
|
|
/* Draw the cursor */
|
|
|
|
Uint64 now = SDL_GetTicks();
|
2024-08-02 18:39:13 +03:00
|
|
|
if ((now - ctx->last_cursor_change) >= CURSOR_BLINK_INTERVAL_MS) {
|
|
|
|
ctx->cursor_visible = !ctx->cursor_visible;
|
|
|
|
ctx->last_cursor_change = now;
|
2024-06-27 23:29:37 +03:00
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
if (ctx->cursor_length > 0) {
|
2024-06-27 23:50:49 +03:00
|
|
|
/* We'll show a highlight */
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, highlight_mode);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDL_RenderFillRect(renderer, &cursorRect);
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
|
2024-08-02 18:39:13 +03:00
|
|
|
} else if (ctx->cursor_visible) {
|
2024-06-27 23:29:37 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
|
|
|
SDL_RenderFillRect(renderer, &cursorRect);
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-06-26 20:19:00 +03:00
|
|
|
/* Draw the candidates */
|
2024-08-02 18:39:13 +03:00
|
|
|
DrawCandidates(ctx, &cursorRect);
|
2024-06-26 20:19:00 +03:00
|
|
|
|
2024-06-28 23:17:04 +03:00
|
|
|
/* Update the area used to draw composition UI */
|
2024-08-02 18:39:13 +03:00
|
|
|
UpdateTextInputArea(ctx, &cursorRect);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:11:08 +03:00
|
|
|
static void Redraw(void)
|
2017-08-28 05:00:03 +03:00
|
|
|
{
|
2015-06-21 18:33:46 +03:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2022-11-27 19:38:43 +03:00
|
|
|
if (state->windows[i] == NULL) {
|
2015-06-21 18:33:46 +03:00
|
|
|
continue;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
RedrawWindow(&windowstate[i]);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:00:03 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2024-06-29 05:22:16 +03:00
|
|
|
SDL_bool render_composition = SDL_FALSE;
|
|
|
|
SDL_bool render_candidates = SDL_FALSE;
|
2015-06-21 18:33:46 +03:00
|
|
|
int i, done;
|
|
|
|
SDL_Event event;
|
2023-03-17 02:25:39 +03:00
|
|
|
char *fontname = NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
/* Enable standard application logging */
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (SDL_strcmp(argv[i], "--font") == 0) {
|
2024-06-26 20:19:00 +03:00
|
|
|
if (*argv[i + 1]) {
|
|
|
|
fontname = argv[i + 1];
|
2023-03-17 02:25:39 +03:00
|
|
|
consumed = 2;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2024-06-29 05:22:16 +03:00
|
|
|
} else if (SDL_strcmp(argv[i], "--render-composition") == 0) {
|
|
|
|
render_composition = SDL_TRUE;
|
2024-06-29 02:14:52 +03:00
|
|
|
consumed = 1;
|
2024-06-29 05:22:16 +03:00
|
|
|
} else if (SDL_strcmp(argv[i], "--render-candidates") == 0) {
|
|
|
|
render_candidates = SDL_TRUE;
|
2024-06-26 20:19:00 +03:00
|
|
|
consumed = 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2023-03-17 02:25:39 +03:00
|
|
|
if (consumed <= 0) {
|
2024-06-29 05:22:16 +03:00
|
|
|
static const char *options[] = { "[--font fontfile] [--render-composition] [--render-candidates]", NULL };
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2024-06-29 05:22:16 +03:00
|
|
|
if (render_composition && render_candidates) {
|
|
|
|
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition,candidates");
|
|
|
|
} else if (render_composition) {
|
|
|
|
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition");
|
|
|
|
} else if (render_candidates) {
|
|
|
|
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "candidates");
|
2024-06-29 02:14:52 +03:00
|
|
|
}
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
if (!SDLTest_CommonInit(state)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
windowstate = (WindowState *)SDL_calloc(state->num_windows, sizeof(*windowstate));
|
|
|
|
if (!windowstate) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't allocate window state: %s\n", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
fontname = GetResourceFilename(fontname, DEFAULT_FONT);
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
#ifdef HAVE_SDL_TTF
|
|
|
|
/* Initialize fonts */
|
|
|
|
TTF_Init();
|
|
|
|
|
|
|
|
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!font) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
|
2017-08-28 05:00:03 +03:00
|
|
|
return -1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2016-10-01 22:43:14 +03:00
|
|
|
#else
|
2017-08-28 05:00:03 +03:00
|
|
|
if (unifont_init(fontname) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_Log("Using font: %s\n", fontname);
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
/* Initialize window state */
|
2015-06-21 18:33:46 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2024-08-02 18:39:13 +03:00
|
|
|
WindowState *ctx = &windowstate[i];
|
|
|
|
SDL_Window *window = state->windows[i];
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2024-08-02 16:56:51 +03:00
|
|
|
int icon_w = 0, icon_h = 0;
|
|
|
|
|
|
|
|
SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX, SDL_SCALEMODE_LINEAR);
|
2024-08-02 18:39:13 +03:00
|
|
|
|
|
|
|
ctx->window = window;
|
|
|
|
ctx->renderer = renderer;
|
|
|
|
ctx->rendererID = i;
|
2024-08-02 16:56:51 +03:00
|
|
|
ctx->settings_icon = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &icon_w, &icon_h);
|
|
|
|
ctx->settings_rect.x = (float)WINDOW_WIDTH - icon_w - MARGIN;
|
|
|
|
ctx->settings_rect.y = MARGIN;
|
|
|
|
ctx->settings_rect.w = (float)icon_w;
|
|
|
|
ctx->settings_rect.h = (float)icon_h;
|
2024-08-02 18:39:13 +03:00
|
|
|
|
|
|
|
InitInput(ctx);
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
}
|
2024-06-27 23:50:49 +03:00
|
|
|
highlight_mode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR,
|
|
|
|
SDL_BLENDFACTOR_ZERO,
|
|
|
|
SDL_BLENDOPERATION_ADD,
|
|
|
|
SDL_BLENDFACTOR_ZERO,
|
|
|
|
SDL_BLENDFACTOR_ONE,
|
|
|
|
SDL_BLENDOPERATION_ADD);
|
2024-06-27 23:29:37 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
/* Main render loop */
|
|
|
|
done = 0;
|
|
|
|
while (!done) {
|
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
2022-11-30 23:51:59 +03:00
|
|
|
switch (event.type) {
|
2024-08-02 16:56:51 +03:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP: {
|
|
|
|
SDL_FPoint point;
|
|
|
|
WindowState *ctx = GetWindowStateForWindowID(event.button.windowID);
|
|
|
|
if (!ctx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_ConvertEventToRenderCoordinates(ctx->renderer, &event);
|
|
|
|
point.x = event.button.x;
|
|
|
|
point.y = event.button.y;
|
|
|
|
if (SDL_PointInRectFloat(&point, &ctx->settings_rect)) {
|
|
|
|
ToggleSettings(ctx);
|
|
|
|
} else if (ctx->settings_visible) {
|
|
|
|
ClickSettings(ctx, point.x, point.y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
case SDL_EVENT_KEY_DOWN: {
|
|
|
|
WindowState *ctx = GetWindowStateForWindowID(event.key.windowID);
|
|
|
|
if (!ctx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:50:10 +03:00
|
|
|
switch (event.key.key) {
|
2022-11-30 23:51:59 +03:00
|
|
|
case SDLK_RETURN:
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->text[0] = 0x00;
|
2015-06-21 18:33:46 +03:00
|
|
|
break;
|
2022-11-30 23:51:59 +03:00
|
|
|
case SDLK_BACKSPACE:
|
|
|
|
/* Only delete text if not in editing mode. */
|
2024-08-02 18:39:13 +03:00
|
|
|
if (!ctx->markedText[0]) {
|
|
|
|
size_t textlen = SDL_strlen(ctx->text);
|
2022-11-30 23:51:59 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (textlen == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
if (!(ctx->text[textlen - 1] & 0x80)) {
|
2022-11-30 23:51:59 +03:00
|
|
|
/* One byte */
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->text[textlen - 1] = 0x00;
|
2022-11-30 23:51:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
if ((ctx->text[textlen - 1] & 0xC0) == 0x80) {
|
2022-11-30 23:51:59 +03:00
|
|
|
/* Byte from the multibyte sequence */
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->text[textlen - 1] = 0x00;
|
2022-11-30 23:51:59 +03:00
|
|
|
textlen--;
|
|
|
|
}
|
2024-08-02 18:39:13 +03:00
|
|
|
if ((ctx->text[textlen - 1] & 0xC0) == 0xC0) {
|
2022-11-30 23:51:59 +03:00
|
|
|
/* First byte of multibyte sequence */
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->text[textlen - 1] = 0x00;
|
2022-11-30 23:51:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
break;
|
2024-03-06 21:35:18 +03:00
|
|
|
default:
|
|
|
|
break;
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08" SDL_PRIX32 " = %s\n",
|
2024-06-22 05:50:10 +03:00
|
|
|
event.key.scancode,
|
|
|
|
SDL_GetScancodeName(event.key.scancode),
|
|
|
|
SDL_static_cast(Uint32, event.key.key),
|
2024-08-06 06:58:41 +03:00
|
|
|
SDL_GetKeyName(event.key.key));
|
2022-11-30 23:51:59 +03:00
|
|
|
break;
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
|
|
|
case SDL_EVENT_TEXT_INPUT: {
|
|
|
|
WindowState *ctx = GetWindowStateForWindowID(event.text.windowID);
|
|
|
|
if (!ctx) {
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (event.text.text[0] == '\0' || event.text.text[0] == '\n' || ctx->markedRect.w < 0) {
|
2015-06-21 18:33:46 +03:00
|
|
|
break;
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
if (SDL_strlen(ctx->text) + SDL_strlen(event.text.text) < sizeof(ctx->text)) {
|
|
|
|
SDL_strlcat(ctx->text, event.text.text, sizeof(ctx->text));
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_Log("text inputted: %s\n", ctx->text);
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2023-07-03 18:29:42 +03:00
|
|
|
/* After text inputted, we can clear up markedText because it */
|
2022-11-30 23:51:59 +03:00
|
|
|
/* is committed */
|
2024-08-02 18:39:13 +03:00
|
|
|
ctx->markedText[0] = 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
break;
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
|
|
|
case SDL_EVENT_TEXT_EDITING: {
|
|
|
|
WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID);
|
|
|
|
if (!ctx) {
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_Log("text editing \"%s\", selected range (%" SDL_PRIs32 ", %" SDL_PRIs32 ")\n",
|
|
|
|
event.edit.text, event.edit.start, event.edit.length);
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
SDL_strlcpy(ctx->markedText, event.edit.text, sizeof(ctx->markedText));
|
|
|
|
ctx->cursor = event.edit.start;
|
|
|
|
ctx->cursor_length = event.edit.length;
|
2022-11-30 23:51:59 +03:00
|
|
|
break;
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
|
|
|
case SDL_EVENT_TEXT_EDITING_CANDIDATES: {
|
|
|
|
WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID);
|
|
|
|
if (!ctx) {
|
|
|
|
break;
|
|
|
|
}
|
2024-03-14 18:52:59 +03:00
|
|
|
|
2024-06-26 20:19:00 +03:00
|
|
|
SDL_Log("text candidates:\n");
|
|
|
|
for (i = 0; i < event.edit_candidates.num_candidates; ++i) {
|
|
|
|
SDL_Log("%c%s\n", i == event.edit_candidates.selected_candidate ? '>' : ' ', event.edit_candidates.candidates[i]);
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:39:13 +03:00
|
|
|
ClearCandidates(ctx);
|
|
|
|
SaveCandidates(ctx, &event);
|
2024-06-26 20:19:00 +03:00
|
|
|
break;
|
2024-08-02 18:39:13 +03:00
|
|
|
}
|
2024-03-14 18:52:59 +03:00
|
|
|
default:
|
|
|
|
break;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 23:29:37 +03:00
|
|
|
|
|
|
|
Redraw();
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2023-03-17 02:25:39 +03:00
|
|
|
SDL_free(fontname);
|
2015-06-21 18:33:46 +03:00
|
|
|
CleanupVideo();
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 0;
|
|
|
|
}
|