MV (Windows only)Some test of pixel fonts

1723051713852.png
1723052228268.png
1723051268050.png
1723051304710.png


Just did some tests. So you can see in the screenshots, I rewrote the functions the game uses to draw text.
With the right fonts, it can display text in 'big pixel' style.
But unforunately, I haven't found a perfect font yet.
Spoiler: code
JavaScript:
//copy the canvas twice, use fairly bit more memory trading for the effect
//I'm lazy so I just use the original size
function _createFontScaleBuffer(ctx){
  if(!ctx.scalecanvas){
    ctx.scalecanvas = document.createElement('canvas');
    ctx.scalecanvas.width = ctx.canvas.width;
    ctx.scalecanvas.height = ctx.canvas.height;
    ctx.sctx = ctx.scalecanvas.getContext('2d');
    //shadow buffer, to draw semi-transparent outline
    ctx.scalecanvas2 = document.createElement('canvas');
    ctx.scalecanvas2.width = ctx.canvas.width;
    ctx.scalecanvas2.height = ctx.canvas.height;
    ctx.sctx2 = ctx.scalecanvas2.getContext('2d');
  }
}
//measureText, small text size x 2
var $mt = CanvasRenderingContext2D.prototype.measureText;
CanvasRenderingContext2D.prototype.measureText= function(t){
  var f = getFont(this.font);
  if(f.scale==1) {
    return $mt.call(this,t);
  }
  _createFontScaleBuffer(this);
  this.sctx.save();
  this.sctx.font = f.fontSmall;
  var metrix = $mt.call(this.sctx,t);
  this.sctx.restore();
  var result = {};
  for(var k in metrix) result[k] = metrix[k]*f.scale;
  return result;
};
var $ft = CanvasRenderingContext2D.prototype.fillText;
CanvasRenderingContext2D.prototype.fillText = function(t,x,y,w){
  var f = getFont(this.font);
  if(f.scale==1) {
    return $ft.call(this,t,x,y,w);
  }
  _createFontScaleBuffer(this);//console.log(t,f)
  this.sctx.save();
  this.sctx.clearRect(0,0,this.scalecanvas.width,this.scalecanvas.height);
  this.sctx.font = f.fontSmall;
  this.sctx.globalAlpha = 1;
  this.sctx.textBaseline = this.textBaseline;
  this.sctx.textAlign = this.textAlign;
  this.sctx.fillStyle = this.fillStyle;
  $ft.call(this.sctx,t,x,y,w?w/f.scale:w);
  this.sctx.restore();
  this.save();
  var dx=-x*(f.scale-1), dy = -y*(f.scale-1);
  this.imageSmoothingEnabled = false;
  this.sctx.imageSmoothingEnabled = false;
  //this.globalCompositeOperation = 'xor';
  this.drawImage(this.scalecanvas,dx,dy, this.canvas.width*f.scale,this.canvas.height*f.scale);
  this.restore();
};

var $st = CanvasRenderingContext2D.prototype.strokeText;
//outline using fake text shadow, this is faster maybe sometimes looks better for pixel fonts
CanvasRenderingContext2D.prototype.strokeText2 = function(t,x,y,w){
  var f = getFont(this.font);
  //if(f.scale==1) {
  //  return $st.call(this,t,x,y,w);
  //}
  _createFontScaleBuffer(this);
  this.sctx.save();
  this.sctx.clearRect(0,0,this.scalecanvas.width,this.scalecanvas.height);
  this.sctx.font = f.fontSmall;
  this.sctx.globalAlpha = 1;
  this.sctx.textBaseline = this.textBaseline;
  this.sctx.textAlign = this.textAlign;
  this.sctx.fillStyle = this.strokeStyle;
  $ft.call(this.sctx,t,x,y,w?w/f.scale:w);
  this.sctx.restore();
  this.save();
  this.imageSmoothingEnabled = false;
  this.sctx.imageSmoothingEnabled = false;
  var lw = Math.round(this.lineWidth/2);
  if(lw<1) lw = 1;
  var dx=-x*(f.scale-1), dy = -y*(f.scale-1);
  this.drawImage(this.scalecanvas,dx+lw,dy+lw, this.canvas.width*f.scale,this.canvas.height*f.scale);
  this.restore();
};
//used to get color value
var pix = document.createElement('canvas');
pix.willReadFrequently = true;
pix.width=1;pix.height=1;
var pctx = pix.getContext('2d');
function getRgba(color){
  pctx.fillStyle = color;
  pctx.clearRect(0,0,1,1);
  pctx.fillRect(0,0,1,1);
  return pctx.getImageData(0,0,1,1).data;
}
//draw text outline, tricky and slow, draw multiple times around the text
CanvasRenderingContext2D.prototype.strokeText = function(t,x,y,w){
  var f = getFont(this.font);
  //if(f.scale==1) {
  //  return $st.call(this,t,x,y,w);
  //}
  _createFontScaleBuffer(this);
  this.sctx.save();
  this.sctx.clearRect(0,0,this.scalecanvas.width,this.scalecanvas.height);
  this.sctx.font = f.fontSmall;
  this.sctx.globalAlpha = 1;
  this.sctx.textBaseline = this.textBaseline;
  this.sctx.textAlign = this.textAlign;
  var rawcolor = getRgba(this.strokeStyle||'black');
  this.sctx.fillStyle = 'rgb('+rawcolor[0]+','+rawcolor[1]+','+rawcolor[2]+')';
  $ft.call(this.sctx,t,x,y,w?w/f.scale:w);
  this.sctx.restore();
  this.sctx2.save();
  this.sctx2.clearRect(0,0,this.canvas.width,this.canvas.height);
  this.sctx2.imageSmoothingEnabled = false;
  this.sctx.imageSmoothingEnabled = false;
  this.sctx2.globalAlpha=1;
  var dx=-x*(f.scale-1), dy = -y*(f.scale-1);
  var lw = Math.round(this.lineWidth/2);
  if(lw<1) lw = 1;
  for(var i=-lw;i<=lw;i=Math.min(i+f.scale,lw)){
    for(var j=-lw;j<=lw;j=Math.min(j+f.scale,lw)){
      this.sctx2.drawImage(this.scalecanvas,dx+i,dy+j, this.canvas.width*f.scale,this.canvas.height*f.scale);
      if(j==lw) break;
    }
    if(i==lw)break;
  }
  //finally hollow the text with xor
  this.sctx2.globalCompositeOperation = 'xor';
  this.sctx2.drawImage(this.scalecanvas,dx,dy, this.canvas.width*f.scale,this.canvas.height*f.scale);
  this.sctx2.restore();
  this.save();
  //this.reset();
  this.globalAlpha *= rawcolor[3]/255;
  this.globalCompositeOperation = 'xor'; //works if background is transparent and shadow alpha is 0.5 or 1
  this.drawImage(this.scalecanvas2,0,0);
  this.restore();
};
var $$font = {};//cache the results
var $$$p= document.createElement('p');
function getFont(font){
  var f = $$font[font];
  if(!f){
    $$$p.style.font = font;
    f = {};
    f.fontSize = $$$p.style.fontSize;
    f.fontFamily = $$$p.style.fontFamily.replace(/['"]/g,'').toLowerCase();
    f.fontStyle = $$$p.style.fontStyle;
    f.fontWeight = $$$p.style.fontWeight;
    f.size = parseInt(f.fontSize);
    determinePixelFontScale(f);
    $$font[font] = f;
  }
  return f;
}

//minSize minimal size the text is readable
//maxSize max size the text remains 'pixelated'
function _determineScale(f, minSize, maxSize){
    var size = f.size;
    f.minSize = minSize;
    f.maxSize = maxSize;
    if(size<=maxSize) {
      f.scale = 1;
      f.sizeSmall = f.size;
    }else{
      var close = Infinity;
      var result = -1;
      for(var i=1;i<=size;i++){
        var s = Math.round(size/i);
        if(s>=minSize&&s<=maxSize){
          var r = Math.abs(s * i / size - 1);
          if(r<close) {
            close = r;
            result = i;
            f.scale = i;
            f.sizeSmall = s;
          }
        }
      }
      close = Infinity;
      if(result<0){
        for(var i=1;i<=size;i++){
          var s = Math.round(size/i);
          var r = Math.abs(s - (minSize+maxSize)/2);
          if(r<close) {
            close = r;
            result = i;
            f.scale = i;
            f.sizeSmall = Math.round((minSize+maxSize)/2);
          }
        }
      }
      if(result<0){
        f.scale = 1;
        f.sizeSmall = f.size;
      }
    }
}
//determine a font's scale from its basic pixelate form
//params: font, size
function determinePixelFontScale(f){
    if(f.fontFamily=='ms gothic'){
      _determineScale(f,10,22); //10,22
    }else if(f.fontFamily=='simsun'){
      _determineScale(f, 12, 17); //12,17
    }else{
      f.scale = 2;
      f.sizeSmall = Math.floor(f.size/2);
    }
    f.fontSmall = f.fontStyle + (f.fontStyle?' ':'') + f.sizeSmall + 'px "' + f.fontFamily + '"';
}
//find "clear" size of current font family ff
//clear means the font use raster image
// ff font family
// min smallest font size to test
// max largest font size to test
// tc character to test with, default 'A'
function findClearFontSize(ff, min, max, tc){
  tc = tc||'A';
  var c = document.createElement('canvas');
  c.willReadFrequently = true;
  c.width = max*2;c.height = max*2;
  var ctx = c.getContext('2d');
  var smin=Infinity, smax=-Infinity;
  for(var i = min;i<=max;i++){
    ctx.clearRect(0,0,c.width,c.height);
    ctx.font = i + 'px "' + ff + '"';
    ctx.fillStyle = 'white';
    ctx.fillText(tc, Math.floor(c.width/4),Math.floor(c.height/4));
    var img = ctx.getImageData(0,0,c.width,c.height);
    var found = true;
    for(var j=3;j<img.data.length;j+=4){
      var a = img.data[j];
      if(a>0&&a<255){
        found=false;
        break;
      }
    }
    if(found){
      if(i<smin){
        smin = i;
      }
      if(i>smax){
        smax = i;
      }
    }
  }
  return {min:smin, max:smax};
}
/////////////////////////////end of re-writing canvas methods////////////////////////////
if(typeof Bitmap!='undefined'){
var globalfont = 'ms gothic';
var bmpi = Bitmap.prototype.initialize;
Bitmap.prototype.initialize = function(width, height){
  bmpi.call(this,width,height);
  this.fontFace = globalfont;
}
Window_Base.prototype.standardFontFace = function() {
  return globalfont;
};
Window_Base.prototype.standardFontSize = function() {
  return 28;
};
Window_Message.prototype.newLineX = function() {
  return $gameMessage.faceName() === '' ? 2 : 170;
};
}

1723220649283.png

1723220635664.png


And I'm trying to find a way to fix the outline overlapping in the message box, with global composite operation set to 'xor'. This usually works unless the outline overlaps with text body.
And the outline code is suppose to be slow because it draws the text multiple times to fake it.
This code you posted, it can use for plugin?
This code you posted, it can use for plugin?
Yes. But it is not quite finished.
I just want to ask you something, does it support mz as well?
I just want to ask you something, does it support mz as well?
It should. But this plugin uses specific windows system fonts (est Asian fonts) so it may not work on every device.
Maybe you can check my other plugin:

Original thread: https://forums.rpgmakerweb.com/threads/windows-only-some-test-of-pixel-fonts.170924/