Skip to content

Monospace characters can exceed width of single character #1002

Closed
@mojavelinux

Description

Certain fonts have a glyph width that is wider than the width of a space character (and wider than latin characters in general). This is typically seen in the box drawing glyphs. For example, in the M+ 1mn font, the char width of the space (and "a") is 500. But the char width of the horizontal box line is 1000. As a result, the box drawings don't align correctly.

Here's an example that demonstrates the problem:

require 'prawn'

Prawn::Document.generate 'mono.pdf' do

  font_families['mplus1mn'] = {
    normal: '/usr/share/fonts/mplus/mplus-1mn-regular.ttf'
  }

  font 'mplus1mn', size: 10 do
    text '---'
    warn width_of '---' # prints 15.0
    text '———'
    warn width_of '———' # prints 15.0
    text '───'
    warn width_of '───' # prints 30.0
  end
end

The last line is twice as wide as the first two.

(This behavior is not seen with DejaVu Sans Mono since all the glyphs have the same width)

If I look at the PDF source, I see something like:

BT
36.0 725.6 Td
/F1.1 10 Tf
<212121> Tj
ET

It may be that Prawn can't do anything about this and that it's something the PDF viewer is responsible for handling. I'm not sure.

The only thing I can really see that Prawn could do is adjust the character spacing. The alternative is when Prawn embeds the font in the PDF, it can somehow scale the glyph widths of the monospace font so that they are all the same. But even if I try to force that to happen, I still get overlap. So maybe it is just an issue with this font.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions