rkusa / ttfjs

TTFjs is a TrueType font parser entirely written in JavaScript and compatible to both Node.js and the Browser
MIT License
21 stars 4 forks source link

char width not exist #1

Closed MinimaJack closed 10 years ago

MinimaJack commented 10 years ago
TTFFont.prototype.stringWidth = function(string, size) {
  var width = 0, scale = size / 1000
  for (var i = 0, len = string.length; i < len; ++i) {
    var code = string.charCodeAt(i) - 32 // - 32 because of non AFM font
    width += this.widths[code] || 0
  }
  return width * scale
}

when this.widths[code] is undefined - use xAvgCharWidth * scale...then all calculated well

rkusa commented 10 years ago

Thanks! Do you like to make a PR? Or do you know a font I can test this case with?

MinimaJack commented 10 years ago

open-sans.regular.ttf --cyrillic symbols samle string: "Відповідальний за здійснення господарської операції і правильність Відповідальний за здійснення господарської операції і правильність"

rkusa commented 10 years ago

Hmm... using xAvgCharWidth seems to be not the correct way of calculating the width of such glyphs. Here is a test (the line break is calculated using .stringWidth(), i.e., the glyphs are calculated to be wider than they actually are):

screenshot 2014-08-01 11 25 33

MinimaJack commented 10 years ago
 this.scaleFactor = 1000.0 / this.tables.head.unitsPerEm
 this.xAvgCharWidth = this.tables['OS/2'].xAvgCharWidth * this.scaleFactor;

need to be scaled...not ideal but well

rkusa commented 10 years ago

Works sufficiently, thank you!