Implement system font position, bold, non-ascii characters

This commit is contained in:
UnknownShadow200 2021-12-11 09:22:19 +11:00
parent c18e8e9649
commit a3a0e30592
2 changed files with 47 additions and 24 deletions

View File

@ -720,17 +720,26 @@ void Font_Free(struct FontDesc* desc) {
}
void SysFonts_Register(const cc_string* path) { }
extern int interop_TextWidth(const char* font, int size, const char* text, const int len);
extern void interop_TextDraw(const char* font, int size, const char* text, const int len,
struct Bitmap* bmp, int x, int y);
extern void interop_SetFont(const char* font, int size, int flags);
extern int interop_TextWidth(const char* text, const int len);
extern void interop_TextDraw(const char* text, const int len, struct Bitmap* bmp, int x, int y, cc_bool shadow);
static int Font_SysTextWidth(struct DrawTextArgs* args) {
struct FontDesc* font = args->font;
return interop_TextWidth(font->handle, font->size, args->text.buffer, args->text.length);
char buffer[NATIVE_STR_LEN];
int len = Platform_EncodeUtf8(buffer, &args->text);
interop_SetFont(font->handle, font->size, font->flags);
return interop_TextWidth(buffer, len);
}
static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) {
struct FontDesc* font = args->font;
interop_TextDraw(font->handle, font->size, args->text.buffer, args->text.length, bmp, x, y);
char buffer[NATIVE_STR_LEN];
int len = Platform_EncodeUtf8(buffer, &args->text);
interop_SetFont(font->handle, font->size, font->flags);
interop_TextDraw(buffer, len, bmp, x, y, shadow);
}
#else
#include "freetype/ft2build.h"

View File

@ -820,45 +820,53 @@ mergeInto(LibraryManager.library, {
//########################################################################################################################
//-----------------------------------------------------------Font---------------------------------------------------------
//########################################################################################################################
interop_TextInit: function(font, size) {
interop_SetFont: function(fontStr, size, flags) {
if (!window.FONT_CANVAS) {
window.FONT_CANVAS = document.createElement('canvas');
window.FONT_CONTEXT = window.FONT_CANVAS.getContext('2d');
}
var ctx = window.FONT_CONTEXT;
ctx.font = size + 'px ' + font;
var prefix = '';
if (flags & 1) prefix += 'Bold ';
var font = UTF8ToString(fontStr);
var ctx = window.FONT_CONTEXT;
ctx.font = prefix + size + 'px ' + font;
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
return ctx;
},
interop_TextWidth__deps: ['interop_TextInit'],
interop_TextWidth: function(fontStr, fontSize, textStr, textLen) {
var font = UTF8ToString(fontStr);
interop_TextWidth: function(textStr, textLen) {
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
var ctx = _interop_TextInit(font, fontSize);
var ctx = window.FONT_CONTEXT;
var data = ctx.measureText(text);
return Math.ceil(data.width)|0;
},
interop_TextDraw__deps: ['interop_TextInit'],
interop_TextDraw: function(fontStr, fontSize, textStr, textLen, bmp, dstX, dstY) {
var font = UTF8ToString(fontStr);
interop_TextDraw: function(textStr, textLen, bmp, dstX, dstY, shadow) {
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
var ctx = _interop_TextInit(font, fontSize);
var ctx = window.FONT_CONTEXT;
// resize canvas if necessary so test fits
var data = ctx.measureText(text);
var text_width = Math.ceil(data.width)|0;
if (text_width > ctx.canvas.width)
ctx.canvas.width = text_width;
ctx.canvas.width = text_width;
var text_offset = 0.0;
ctx.fillStyle = "#ffffff";
if (shadow) {
text_offset = 1.3;
ctx.fillStyle = "#7f7f7f";
}
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = "#ffffff";
ctx.fillText(text, 0, 0);
ctx.fillText(text, text_offset, text_offset);
bmp = bmp|0;
var dst_pixels = HEAP32[(bmp + 0|0)>>2];
bmp = bmp|0;
dstX = dstX|0;
dstY = dstY|0;
var dst_pixels = HEAP32[(bmp + 0|0)>>2] + (dstX << 2);
var dst_width = HEAP32[(bmp + 4|0)>>2];
var dst_height = HEAP32[(bmp + 8|0)>>2];
@ -873,11 +881,17 @@ mergeInto(LibraryManager.library, {
for (var y = 0; y < img_height; y++)
{
var src_row = (y*(src_width << 2))|0;
var dst_row = dst_pixels + (y*(dst_width << 2))|0;
var yy = y + dstY;
if (yy < 0 || yy >= dst_height) continue;
var src_row = (y *(src_width << 2))|0;
var dst_row = dst_pixels + (yy*(dst_width << 2))|0;
for (var x = 0; x < img_width; x++)
{
var xx = x + dstX;
if (xx < 0 || xx >= dst_width) continue;
HEAPU8[dst_row + (x<<2)+0] = src_pixels[src_row + (x<<2)+0];
HEAPU8[dst_row + (x<<2)+1] = src_pixels[src_row + (x<<2)+1];
HEAPU8[dst_row + (x<<2)+2] = src_pixels[src_row + (x<<2)+2];