1 decade ago by mkreitler
Hey all,
I just ran into this problem today and thought I'd share a possible solution.
The symptom: your font displays the wrong characters, usually past what you expect and sometimes printing more characters than it should.
The cause: for some reason, you'll get non-zero alpha values in the spaces between the bottom row of pixels in the font's bitmap. Since Impact uses this line to compute character spacings, the screws up the spacings.
The catch: this doesn't happen on all machines! With the same font, I ran fine on my iMac at work, but not on my Mac mini at home or a MacBook in the office. Who knows, maybe it's browser-dependent, too (though I was using Chrome in all cases).
One solution: modify the Impact code to look for alpha values below a threshold, rather than strictly zero.
In font.js:
Generally, I don't like messing with engine code, but this is fairly safe.
I just ran into this problem today and thought I'd share a possible solution.
The symptom: your font displays the wrong characters, usually past what you expect and sometimes printing more characters than it should.
The cause: for some reason, you'll get non-zero alpha values in the spaces between the bottom row of pixels in the font's bitmap. Since Impact uses this line to compute character spacings, the screws up the spacings.
The catch: this doesn't happen on all machines! With the same font, I ran fine on my iMac at work, but not on my Mac mini at home or a MacBook in the office. Who knows, maybe it's browser-dependent, too (though I was using Chrome in all cases).
One solution: modify the Impact code to look for alpha values below a threshold, rather than strictly zero.
In font.js:
_loadMetrics: function( image ) { // Draw the bottommost line of this font image into an offscreen canvas // and analyze it pixel by pixel. // A run of non-transparent pixels represents a character and its width this.height = image.height-1; this.widthMap = []; this.indices = []; var canvas = ig.$new('canvas'); canvas.width = image.width; canvas.height = image.height; var ctx = canvas.getContext('2d'); ctx.drawImage( image, 0, 0 ); var px = ctx.getImageData(0, image.height-1, image.width, 1); var currentChar = 0; var currentWidth = 0; for( var x = 0; x < image.width; x++ ) { var index = x * 4 + 3; // alpha component of this pixel // CHANGES IN HERE ------------------------------------------------------------------------ if( px.data[index] > ig.Font.ALPHA_THRESHOLD ) { currentWidth++; } else if( px.data[index] <= ig.Font.ALPHA_THRESHOLD && currentWidth ) { this.widthMap.push( currentWidth ); this.indices.push( x-currentWidth ); currentChar++; currentWidth = 0; } // END CHANGES ------------------------------------------------------------------------------- } this.widthMap.push( currentWidth ); this.indices.push( x-currentWidth ); } }); // ANOTHER CHANGE ------------------------------------------------------------------------ ig.Font.ALPHA_THRESHOLD = 2; // END CHANGE ---------------------------------------------------------------------------------
Generally, I don't like messing with engine code, but this is fairly safe.