Search code examples
androidcanvasandroid-typeface

Android Monospace doesn't behave as a fixed-pitch


As you can see in the screenshot below the two different strings with the same length not drawn with the same width on the canvas. I'm using a monospaced typeface, so isn't it promised to be drawn with the same width? Is that because of the kerning?

Typeface.create(Typeface.MONOSPACE,Typeface.NORMAL)

enter image description here


Solution

  • This is a well-known kerning issue with monosapce font on Android. It looks like this is a quite old topic.

    Some people have already faced this problem:

    Android Monospace fonts aren't fixed width - 2013-06-11

    Are Android monospace fonts actually fixed size? - 2016-06-22

    Android monospace space ( ) width is different than character width - 2017-03-02

    The issue also occurred in mobile web environment:

    Font monospace Android 2.3 - 2013-07-11

    where problem explained with missing glyph:

    https://github.com/googlefonts/noto-fonts/issues/617

    Solutions:

    Option 1.:

    Looking for an alternative fixed-width font might be the easiest way. Some helpful links to find one:

    Option 2.:

    You can override text drawing using Spans. This post may help you: https://stackoverflow.com/a/53452941/5823014

    Option 3.:

    You can modify the monospace font file to fix kerning. This online tool can be useful: http://www.glyphrstudio.com/online/