Skip to content
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

TextMetrics & rendered text changed from v1.0.2 to v^2.0.0 #208

Open
MarosPistej opened this issue Dec 17, 2024 · 1 comment
Open

TextMetrics & rendered text changed from v1.0.2 to v^2.0.0 #208

MarosPistej opened this issue Dec 17, 2024 · 1 comment

Comments

@MarosPistej
Copy link

MarosPistej commented Dec 17, 2024

About

Texts aren't rendered same as in v1.0.2, TextMetrics are different for all textBaseLine but it is most noticeable for top && bottom, when middle || alphabetic are used changes are very small.

baseline: alphabetic image image
baseline: bottom image image

old: v1.0.2, new: v2.0.1

Notice: how text is shifted upwards when baseline: bottom

Reproducible example

Toggle me!
function drawTestText() {
  const width = 150
  const height = 100
  const canvas = new Canvas(width, height)
  const context = canvas.getContext('2d')

  context.fillStyle = 'white'
  context.fillRect(0, 0, width, height)

  context.strokeStyle = 'blue'
  context.beginPath()
  for (let i = 0; i < height; i += 10) {
    context.moveTo(0, i)
    context.lineTo(width, i)
  }
  context.stroke()
  context.closePath()

  context.font = '50px Lato'
  context.fillStyle = 'black'
  context.textAlign = 'left'
  context.textBaseline = 'bottom'
  const text = 'TEST'
  const measurements = context.measureText(text)
  console.log(measurements)
  context.fillText(text, 25, 75)

  context.beginPath()
  context.strokeStyle = 'red'
  context.strokeRect(
    25 + measurements.lines[0].x,
    75 + measurements.lines[0].y,
    measurements.lines[0].width,
    measurements.lines[0].height
  )
}

Expected behavior

I am unsure about this, to be honest. This is causing me problems because I had fine-tuned my render functions to display text in specific positions based on TextMetrics. Primarily, I would like to know if this is/was an expected change between versions and, if possible, what caused it.

@samizdatco
Copy link
Owner

Nothing changed between v1 and v2 within skia-canvas itself, but it upgrades to a much newer revision of rust-skia, so things have almost certainly changed under the hood. However: one alternative explanation that comes to mind is that your system is now using gpu-based rendering as of v2 (in which case setting canvas.gpu = false would yield different results).

That said, after looking over the way different baselines are computed (for #209) I think I've found some modifications that would better match the way browsers calculate offsets and determine the default leading when a lineHeight value isn't included in the ctx.font assignment.

More to come…

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

No branches or pull requests

2 participants