Skip to content

Text rendered with wrong font family #899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matthias-w opened this issue Mar 31, 2017 · 8 comments
Open

Text rendered with wrong font family #899

matthias-w opened this issue Mar 31, 2017 · 8 comments

Comments

@matthias-w
Copy link

matthias-w commented Mar 31, 2017

The text is rendered with the default sans-serif font family instead with the font defined with ctx.font = "12px 'Marker SD'";

The issue appears since I upgraded to canvas 1.6.5. It works correctly with canvas version 1.6.4. Currently, I went back to version 1.6.4 as a workaround.

My environment

  • Version of node-canvas: 1.6.5
  • Environment: node 6.10.1 on Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-70-generic x86_64)

EDIT:
Maybe the same issue #868

@chearon
Copy link
Collaborator

chearon commented Mar 31, 2017

I don't think it's the same as #868, since that is for canvas v2. More likely this is the same underlying issue as #898 (we recently upgraded the font string parser). Do you experience the same issue if you use node-canvas 1.6.4?

@matthias-w
Copy link
Author

Hello @chearon, thanks for your comment. The issue does not appear when using node-canvas 1.6.4 (see my first post).

@LinusU
Copy link
Collaborator

LinusU commented Apr 3, 2017

Does the exact string tx.font = "12px 'Marker SD'"; work in chrome or firefox?

@matthias-w
Copy link
Author

Hello @LinusU, this works in Chrome, Firefox, Opera and IE11.

@Enginebeck
Copy link

It works for me with v1.6.4

@matthias-w
Copy link
Author

matthias-w commented Apr 5, 2017

This is what I said in my very first post. It works with node-canvas 1.6.4 but it does NOT with node-canvas 1.6.5. That's why I switched back to version 1.6.4 for now.

@LinusU
Copy link
Collaborator

LinusU commented Apr 8, 2017

Whipped this together real quick to be able to test, but it seems like the output is identical 🤔

const parseCssFont = require('parse-css-font')
const unitsCss = require('units-css')

const weights = 'normal|bold|bolder|lighter|[1-9]00'
const styles = 'normal|italic|oblique'
const units = 'px|pt|pc|in|cm|mm|%'
const string = '\'([^\']+)\'|"([^"]+)"|[\\w-]+'

const fontre = new RegExp(
  '^ *' +
  '(?:(' + weights + ') *)?' +
  '(?:(' + styles + ') *)?' +
  '([\\d\\.]+)(' + units + ') *' +
  '((?:' + string + ')( *, *(?:' + string + '))*)'
)

function oldParse (input) {
  let font = {}
  let captures = fontre.exec(input)

  if (!captures) return

  font.weight = captures[1] || 'normal'
  font.style = captures[2] || 'normal'
  font.size = parseFloat(captures[3])
  font.unit = captures[4]
  font.family = captures[5].replace(/["']/g, '').split(',')[0].trim()

  return font
}

function newParse (input) {
  const parsedFont = parseCssFont(input)
  const parsedSize = unitsCss.parse(parsedFont.size)

  return {
    weight: parsedFont.weight,
    style: parsedFont.style,
    size: parsedSize.value,
    unit: parsedSize.unit,
    family: parsedFont.family[0]
  }
}

console.log('OLD:')
console.log(oldParse(process.argv[2]))

console.log('NEW:')
console.log(newParse(process.argv[2]))

output:

OLD:
{ weight: 'normal',
  style: 'normal',
  size: 12,
  unit: 'px',
  family: 'Marker SD' }
NEW:
{ weight: 'normal',
  style: 'normal',
  size: 12,
  unit: 'px',
  family: 'Marker SD' }

@matthias-w
Copy link
Author

Any news on this? I tried out node-canvas 1.6.7 and the issue still exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants