y-axis jittering in SkCanvas::drawSimpleText

162 views
Skip to first unread message

שחר לנגבהיים

unread,
Jun 1, 2020, 10:50:23 AM6/1/20
to skia-discuss
Hi,
I try to linearly scale a drawn emoji between frames by drawing it in a constant size over a scaled canvas (using the system that worked for with Skottie, as suggested here). I use this code for it:

{
 
SkAutoCanvasRestore canvasRestore(canvas, true);
  canvas
->scale(ratio, ratio);
  canvas
->drawSimpleText(emojiString.c_str(), emojiString.length(), SkTextEncoding::kUTF8,
                         xOffset
/ ratio, yOffset / ratio, font, paint);
}
where ratio is slightly modified between each frame. While the drawn emoji is scaled smoothly, there's a visible jitter in its spatial position - see here for an example. It looks as if drawSimpleText loses some precision on the y axis, or maybe vertically snaps to the pixel's edge. When the y-axis correction is removed, the video is much smoother, but spatially incorrect. Is this a bug, or am I missing something in the drawSimpleText signature?

שחר לנגבהיים

unread,
Jun 1, 2020, 10:51:54 AM6/1/20
to skia-discuss
I tried to demonstrate this in a fiddle, but couldn't get how to load the emoji's typeface in the fiddle. If anyone knows how to do that, I'd appreciate a correction. lines 34-38
https://jsfiddle.skia.org/canvaskit/e452d6c7f1d64dddfb18150dda5aed458492b6cc3add1160675338331623a50f 

Ben Wagner

unread,
Jun 1, 2020, 11:24:20 AM6/1/20
to skia-d...@googlegroups.com
So there are a few things. If you're animating you'll want to
SkFont::setBaselineSnap(false) (is this what you're referring to
about y-axis correction?). Also, note that (especially on mac) bitmap
based color fonts (sbix, CBDT) may not actually scale linearly
internally. While mode font designers try to keep things scaling more
or less linearly, there can be jumps around internal strike size
selection (and on mac sometimes you just don't get what you ask for).
> --
> You received this message because you are subscribed to the Google Groups "skia-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/9cbacb29-6e55-4cab-9dd7-424b193f57d3%40googlegroups.com.

Shachar Langbeheim

unread,
Jun 1, 2020, 11:46:41 AM6/1/20
to skia-discuss
I'm aware of the fact that fonts don't scale internally - that's why I use a font with a constant size, and just scale the canvas. I assumed that that would give me smooth scaling. Does the canvas take the scale into account and uses a different font size?
setting baselineSnap doesn't seem to help.

Ben Wagner

unread,
Jun 1, 2020, 11:54:58 AM6/1/20
to skia-d...@googlegroups.com
Yes, the final font size actually drawn is calculated in device space
(the actual pixels). If you want a bitmap to just scale up that would
need to be a bitmap instead of a font. I can try to take a look at
this if you let me know which OS you're on (or if you know more
specifically which SkFontMgr is in use).
> To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/CA%2B_KjGaXELB%2BoBN3TW7qLuBxyPSDuBzZhUak6Fp%2BsrQJxnG-fw%40mail.gmail.com.

Shachar Langbeheim

unread,
Jun 1, 2020, 12:12:07 PM6/1/20
to skia-discuss
This happens on Ubuntu 18.04 (in docker over MacOS, if it matters), the fonts are acquired using
auto typeface = SkTypeface::MakeFromFile(filepath.c_str());
auto font = SkFont(typeface, kDefaultFontSize);
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
font.setSubpixel(true);

Ben Wagner

unread,
Jun 10, 2020, 9:33:15 AM6/10/20
to skia-d...@googlegroups.com
At the very least you'll want to add font.setBaselineSnap(false) to
that (at least on any text you're animating). You may also want to
specify font.setLinearMetrics(true). It probably won't make much
difference in this particular case, but you'll also want
font.setHinting(SkFontHinting::kNone) whenever animating text since
you'll probably want it to scale smoothly as much as possible.
> To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/CA%2B_KjGYB4WbXawbcM50LReCCDT%2B6nFBBpq%3Dmu-uWSXi1_qesGA%40mail.gmail.com.

Shachar Langbeheim

unread,
Jun 10, 2020, 10:25:53 AM6/10/20
to skia-discuss
Thanks, these settings seem to work great!

Reply all
Reply to author
Forward
0 new messages