/*
* Draw UTF-8 text into a RastPort at (x,y).
*/
void UText(struct TextFont *tf, struct RastPort *rp, const char *utf8, LONG x, LONG y)
{
if (!tf || !rp || !utf8) return;
utf8proc_uint8_t *normalized = utf8proc_NFC((const utf8proc_uint8_t *)utf8);
if (!normalized) return;
const utf8proc_uint8_t *ptr = normalized;
utf8proc_int32_t codepoint;
ssize_t len;
LONG pen_x = x;
size_t remaining = strlen(utf8);
while (remaining > 0 && (len = utf8proc_iterate(ptr, -1, &codepoint)) > 0) {
FT_UInt glyph_index = FT_Get_Char_Index(testEngine->ftgle_Sources[0].ftgs_Face, codepoint);
if (glyph_index == 0)
glyph_index = FT_Get_Char_Index(testEngine->ftgle_Sources[0].ftgs_Face, 0xFFFD);
if (FT_Load_Glyph(testEngine->ftgle_Sources[0].ftgs_Face, glyph_index, FT_LOAD_DEFAULT))
continue;
if (FT_Render_Glyph(testEngine->ftgle_Sources[0].ftgs_Face->glyph, FT_RENDER_MODE_NORMAL))
continue;
FT_GlyphSlot g = testEngine->ftgle_Sources[0].ftgs_Face->glyph;
FT_Bitmap *bmp = &g->bitmap;
int sx = pen_x + g->bitmap_left;
int sy = y - g->bitmap_top;
int ex = sx + bmp->width - 1;
int ey = sy + bmp->rows - 1;
if (bmp->width > 0 && bmp->rows > 0) {
/* allocate a temporary chunky buffer (one UBYTE per pixel, tightly packed) */
int w = bmp->width;
int h = bmp->rows;
UBYTE *chunky = malloc(w * h);
if (!chunky) { /* handle OOM */ }
/* simple threshold -> foreground/background pen */
UBYTE fgPen = 1; /* pick a pen number that maps to your desired color */
UBYTE bgPen = 0; /* background pen */
for (int row = 0; row < h; ++row) {
UBYTE *src = bmp->buffer + row * bmp->pitch;
UBYTE *dst = chunky + row * w;
for (int col = 0; col < w; ++col) {
UBYTE alpha = src[col]; /* 0..255 coverage */
dst[col] = (alpha > 128) ? fgPen : bgPen;
}
}
/* write to RastPort: xstart, ystart, xstop, ystop, array, bytesPerRow */
WriteChunkyPixels(rp, sx, sy, ex, ey, chunky, w);
free(chunky);
}
pen_x += g->advance.x >> 6;
ptr += len;
remaining -= len;
}
utf8proc_free(normalized);
}