1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-05-12 08:00:12 +00:00

Updates for default font rendering for browsers (primarily safari) that do not support the letterSpacing attribute in the 2d canvas

This commit is contained in:
boxgaming 2024-01-11 11:44:17 -06:00
parent fbc034da1a
commit 000d4f35b7

93
qb.js
View file

@ -60,6 +60,7 @@ var QB = new function() {
var _windowDef = [];
var _fileHandles = null;
var _typeMap = {};
var _letterSpacingSupport = null;
// Array handling methods
@ -750,23 +751,43 @@ var QB = new function() {
ctx.beginPath();
var f = _fonts[_font];
ctx.font = f.size + " " + f.name;
var tm = ctx.measureText(s);
var fheight = 0;
if (tm.fontBoundingBoxAscent) {
fheight = tm.fontBoundingBoxAscent + tm.fontBoundingBoxDescent;
// ------------------------------------------------------------------
// special case for built-in fonts and safari
// if safari adds 2d canvas letterSpacing support this can be removed
if (!_letterSpacingSupport && _font < 1000) {
for (var i=0; i < s.length; i++) {
var ch = s.substring(i, i+1);
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x + (i * QB.func__FontWidth()), y, QB.func__FontWidth(), QB.func__FontHeight());
}
if (_printMode != QB._ONLYBACKGROUND) {
// Draw the string
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(ch, x + (i * QB.func__FontWidth()), y + QB.func__FontHeight() - f.offset);
}
}
}
// ------------------------------------------------------------------
else {
fheight = tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent;
}
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x, y, tm.width, fheight);
}
if (_printMode != QB._ONLYBACKGROUND) {
// Draw the string
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(s, x, y + fheight - f.offset);
var tm = ctx.measureText(s);
var fheight = 0;
if (tm.fontBoundingBoxAscent) {
fheight = tm.fontBoundingBoxAscent + tm.fontBoundingBoxDescent;
}
else {
fheight = tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent;
}
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x, y, tm.width, fheight);
}
if (_printMode != QB._ONLYBACKGROUND) {
// Draw the string
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(s, x, y + fheight - f.offset);
}
}
};
@ -2387,18 +2408,39 @@ var QB = new function() {
}
var y = _locY * QB.func__FontHeight();
// Draw the text background
var tm = ctx.measureText(subline);
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x, y, tm.width, QB.func__FontHeight());
// ------------------------------------------------------------------
// special case for built-in fonts and safari
// if safari adds 2d canvas letterSpacing support this can be removed
if (!_letterSpacingSupport && _font < 1000) {
for (var i=0; i < subline.length; i++) {
var ch = subline.substring(i, i+1);
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x + (i * QB.func__FontWidth()), y, QB.func__FontWidth(), QB.func__FontHeight());
}
if (_printMode != QB._ONLYBACKGROUND) {
// Draw the string
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(ch, x + (i * QB.func__FontWidth()), y + QB.func__FontHeight() - f.offset);
}
}
x += QB.func__FontWidth();
}
if (_printMode != QB._ONLYBACKGROUND) {
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(subline, x, (y + QB.func__FontHeight() - f.offset));
// ------------------------------------------------------------------
else {
// Draw the text background
var tm = ctx.measureText(subline);
if (_printMode != QB._KEEPBACKGROUND) {
ctx.fillStyle = _bgColor.rgba();
ctx.fillRect(x, y, tm.width, QB.func__FontHeight());
}
if (_printMode != QB._ONLYBACKGROUND) {
ctx.fillStyle = _fgColor.rgba();
ctx.fillText(subline, x, (y + QB.func__FontHeight() - f.offset));
}
x += tm.width;
}
x += tm.width;
_locX += subline.length;
}
@ -2916,6 +2958,9 @@ var QB = new function() {
_keyDownMap = {};
// TODO: set the appropriate default font for the selected screen mode above instead of here
if (_letterSpacingSupport == null) {
_letterSpacingSupport = (GX.ctx().letterSpacing) ? true : false;
}
QB.sub__Font(_font);
};