Anti-aliased fonts are fantastic, 99% of the time. But for some applications, generally games, unaliased fonts can preferred for aesthetic reasons. Do you think it would be possible to have an option to turn the font antialiasing off?
Designs
Child items
...
Show closed items
Linked items
0
Link issues together to show that they're related.
Learn more.
TTF font has (usually) vector font data.
For every pixel on canvas, rusttype calculates "coverage", which is percentage of area covered by glyphs (described by vector format).
So rusttype does not "anti-alias" for raster bitmap, but does "rasterize" for vector glyph images.
I think you had better use bitmap font directly, because it doesn't need to be rasterized.
You can use rusttype to get binarized fonts by rounding the coverage value given by PositionedGlyph::draw, but it may cause readability issue.
(Pixel coverages of narrow lines may be very low and can be rounded to zero!)
I don't know how to do this easily with glyph cache...
@expenses this is doable without modifying RustType. As @lo48576 describes, you can round the coverage value given by draw. For use with the glyph cache feature, you can simply do a similar rounding conversion within the closure passed to cache_queued. Be aware though that many fonts are not designed with binary aliasing in mind, and may end up looking pretty bad.
@ids1024 @dylanede Thank you for the advice! However, rounding the coverage value doesn't quite get the result I was looking for. Here is the font with anti-aliasing on, off and what I'd ideally like:
Is this something that could be fixed with kerning?
Edit: I realised that you need to also round the glyph positions, so I wrote the following code to do that in an iterator (based off this one in the docs):
lettext="The quick brown fox jumps over the lazy dog";letstart=point(10.0,10.0);letscale=Scale{x:13.0,y:13.0};letiter=font.glyphs_for(text.chars()).scan((None,0.0),|&mut(refmutlast,refmutx),g|{letg=g.scaled(scale);letw=g.h_metrics().advance_width+last.map(|last|font.pair_kerning(scale,last,g.id())).unwrap_or(0.0);// Round the widthletw=w.round();letnext=g.positioned(start+vector(*x,0.0));*last=Some(next.id());*x+=w;Some(next)});
I still think that doing this should be easier, however, especially as rusttype is targeting games.
Since draw gives you the alpha, and using the gpu_cache you can define rounding in a shader, I guess rusttype users should have what they need to handle this issue. Can we close?