mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-14 10:05:44 -04:00
Implement system font position, bold, non-ascii characters
This commit is contained in:
parent
c18e8e9649
commit
a3a0e30592
@ -720,17 +720,26 @@ void Font_Free(struct FontDesc* desc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SysFonts_Register(const cc_string* path) { }
|
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_SetFont(const char* font, int size, int flags);
|
||||||
extern void interop_TextDraw(const char* font, int size, const char* text, const int len,
|
extern int interop_TextWidth(const char* text, const int len);
|
||||||
struct Bitmap* bmp, int x, int y);
|
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) {
|
static int Font_SysTextWidth(struct DrawTextArgs* args) {
|
||||||
struct FontDesc* font = args->font;
|
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) {
|
static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) {
|
||||||
struct FontDesc* font = args->font;
|
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
|
#else
|
||||||
#include "freetype/ft2build.h"
|
#include "freetype/ft2build.h"
|
||||||
|
@ -820,45 +820,53 @@ mergeInto(LibraryManager.library, {
|
|||||||
//########################################################################################################################
|
//########################################################################################################################
|
||||||
//-----------------------------------------------------------Font---------------------------------------------------------
|
//-----------------------------------------------------------Font---------------------------------------------------------
|
||||||
//########################################################################################################################
|
//########################################################################################################################
|
||||||
interop_TextInit: function(font, size) {
|
interop_SetFont: function(fontStr, size, flags) {
|
||||||
if (!window.FONT_CANVAS) {
|
if (!window.FONT_CANVAS) {
|
||||||
window.FONT_CANVAS = document.createElement('canvas');
|
window.FONT_CANVAS = document.createElement('canvas');
|
||||||
window.FONT_CONTEXT = window.FONT_CANVAS.getContext('2d');
|
window.FONT_CONTEXT = window.FONT_CANVAS.getContext('2d');
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctx = window.FONT_CONTEXT;
|
var prefix = '';
|
||||||
ctx.font = size + 'px ' + font;
|
if (flags & 1) prefix += 'Bold ';
|
||||||
|
|
||||||
|
var font = UTF8ToString(fontStr);
|
||||||
|
var ctx = window.FONT_CONTEXT;
|
||||||
|
ctx.font = prefix + size + 'px ' + font;
|
||||||
ctx.textAlign = 'left';
|
ctx.textAlign = 'left';
|
||||||
ctx.textBaseline = 'top';
|
ctx.textBaseline = 'top';
|
||||||
return ctx;
|
return ctx;
|
||||||
},
|
},
|
||||||
interop_TextWidth__deps: ['interop_TextInit'],
|
interop_TextWidth: function(textStr, textLen) {
|
||||||
interop_TextWidth: function(fontStr, fontSize, textStr, textLen) {
|
|
||||||
var font = UTF8ToString(fontStr);
|
|
||||||
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
|
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
|
||||||
|
var ctx = window.FONT_CONTEXT;
|
||||||
var ctx = _interop_TextInit(font, fontSize);
|
|
||||||
var data = ctx.measureText(text);
|
var data = ctx.measureText(text);
|
||||||
return Math.ceil(data.width)|0;
|
return Math.ceil(data.width)|0;
|
||||||
},
|
},
|
||||||
interop_TextDraw__deps: ['interop_TextInit'],
|
interop_TextDraw: function(textStr, textLen, bmp, dstX, dstY, shadow) {
|
||||||
interop_TextDraw: function(fontStr, fontSize, textStr, textLen, bmp, dstX, dstY) {
|
|
||||||
var font = UTF8ToString(fontStr);
|
|
||||||
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
|
var text = UTF8ArrayToString(HEAPU8, textStr, textLen);
|
||||||
var ctx = _interop_TextInit(font, fontSize);
|
var ctx = window.FONT_CONTEXT;
|
||||||
|
|
||||||
// resize canvas if necessary so test fits
|
// resize canvas if necessary so test fits
|
||||||
var data = ctx.measureText(text);
|
var data = ctx.measureText(text);
|
||||||
var text_width = Math.ceil(data.width)|0;
|
var text_width = Math.ceil(data.width)|0;
|
||||||
if (text_width > ctx.canvas.width)
|
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.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||||
ctx.fillStyle = "#ffffff";
|
ctx.fillText(text, text_offset, text_offset);
|
||||||
ctx.fillText(text, 0, 0);
|
|
||||||
|
|
||||||
bmp = bmp|0;
|
bmp = bmp|0;
|
||||||
var dst_pixels = HEAP32[(bmp + 0|0)>>2];
|
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_width = HEAP32[(bmp + 4|0)>>2];
|
||||||
var dst_height = HEAP32[(bmp + 8|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++)
|
for (var y = 0; y < img_height; y++)
|
||||||
{
|
{
|
||||||
var src_row = (y*(src_width << 2))|0;
|
var yy = y + dstY;
|
||||||
var dst_row = dst_pixels + (y*(dst_width << 2))|0;
|
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++)
|
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)+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)+1] = src_pixels[src_row + (x<<2)+1];
|
||||||
HEAPU8[dst_row + (x<<2)+2] = src_pixels[src_row + (x<<2)+2];
|
HEAPU8[dst_row + (x<<2)+2] = src_pixels[src_row + (x<<2)+2];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user