Skip to main content

graphics - How can text-specific antialiasing be disabled in Rasterize?



While tinkering with the code in the Falling Matrix question, I was hoping to improve the rendering speed by making the rasterizations of the text grayscale. I changed the base color of the text to white and left the background black. However, this did not yield a grayscale raster. The problem seems to be that Mathematica tries to help with the readability of text by using special antialiasing schemes.


Consider the following test:


GraphicsRow[
Rasterize[#, Background -> Black, RasterSize -> 15,
ImageSize -> 150] & /@ {Style["\[FilledCircle]", White],
Graphics[{White, Disk[]}]}]

Antialiased disk test


Interesting and (I think) undocumented, but the relevant question for me is: How can I control or eliminate this behavior? Adding a ColorConvert[#,"Grayscale"] or just taking the Norms of the ImageData gives an acceptable result but is pretty slow.



Answer




One possible way would be to extract the text outline as in this answer (scroll to the end) and rasterize the resulting graphics. First, define a convenient function:


textGraphics[t_String | t_Style] := Graphics@First@First@
ImportString[ExportString[t, "PDF"], "PDF", "TextMode" -> "Outlines"];

We can see this in action:


textGraphics /@ {Style["A", FontSize -> 12, FontColor -> Red, FontFamily -> "Times"], "B"} // Row


If you now try your example above and apply this function on the styled text, you should get this:




Comments