typemytype / drawbot

http://www.drawbot.com
Other
393 stars 61 forks source link

FormattedString.appendGlyph("Space") creates unexpected justification ? #526

Closed mathieureguer closed 1 year ago

mathieureguer commented 1 year ago

I am not sure this is bug, maybe it is expected or desirable behaviour.

Using appendGlyph("space") on a FormattedString() can lead to a visible word space at the start of a line in an aligned left TextBox().

chars = ["A", "B", "C", "D"] * 30
fs = FormattedString()
fs.font("Georgia")
fs.fontSize(50)
for c in chars:
    fs.appendGlyph(c)
    fs.appendGlyph("space")   
textBox(fs, (100, 100, 320, 800))

fs = FormattedString()
fs.fontSize(50)
fs.font("Georgia")
fs.append("A B C D "*30)   
textBox(fs, (500, 100, 320, 800))

On the left, with appendGlyph() the unexpected leading space. On the right, with append() the expected justification.

AppendGlyphSpace

I assume this has something to do with appendGlyph() using a non breaking space a baseString? Should it use a breaking baseString when the appended glyph is a breaking space? I guess this would required checking for a potential unicode for the appended glyph name / glyph index and creates a lot of additional work…

I can work around this in my specific use case, so I am not sure this is worth fixing, if it is a bug at all :)

mathieureguer commented 1 year ago

Oups, uploaded an image with no justification issue. Fixed!

typemytype commented 1 year ago

appendGlyph() adds glyph by ids directly, no unicodes involved, so no text layout processing, no features... just that very specific glyph. see

you could roundtrip this by adding a " "

chars = ["A", "B", "C", "D"] * 30
fs = FormattedString()
fs.font("Georgia")
fs.fontSize(50)
for c in chars:
    fs.appendGlyph(c)
    fs += " "  
textBox(fs, (100, 100, 320, 800
mathieureguer commented 1 year ago

That's what I ended up doing. Works like a charm. Thanks!