typemytype / drawbot

http://www.drawbot.com
Other
406 stars 63 forks source link

TextBox() text clipping when used as a module #527

Closed chrisjansky closed 1 year ago

chrisjansky commented 1 year ago

Hi,

There seems to be a difference how text is rendered in a TextBox when a very low lineHeight is used.

This works fine in DrawBot app (first screenshot):

import drawBot

fs = 300
leading = 0.3
drawBot.newPage()
drawBot.font("Times")
drawBot.fontSize(fs)
drawBot.lineHeight(fs * leading)

txt = "ABC\nDEF"
txtSize = drawBot.textSize(txt)

coords = 100, 100, *txtSize
drawBot.textBox(txt, coords)

drawBot.fill(None)
drawBot.stroke(0)
drawBot.rect(*coords)

The same code used with executeVanillaTest() in a standalone .py file clips the text (second screenshot):

import vanilla
from vanilla.test.testTools import executeVanillaTest
import drawBot
from drawBot.ui.drawView import DrawView

windowSize = (1000, 1000)

def draw():
    fs = 300
    leading = 0.3
    drawBot.newPage()
    drawBot.font("Times")
    drawBot.fontSize(fs)
    drawBot.lineHeight(fs * leading)

    txt = "ABC\nDEF"
    txtSize = drawBot.textSize(txt)

    coords = 100, 100, *txtSize
    drawBot.textBox(txt, coords)

    drawBot.fill(None)
    drawBot.stroke(0)
    drawBot.rect(*coords)

# Test
def render():
    drawBot.newDrawing()
    draw()
    pdf = drawBot.pdfImage()
    drawBot.endDrawing()
    return pdf

def run():
    w = vanilla.Window(
        windowSize,
        minSize=(400, 400),
    )
    w.canvas = DrawView((0, 0, -0, -0))
    w.canvas.setPDFDocument(render())

    w.open()
    w.center()

if __name__ == "__main__":
    executeVanillaTest(run)

Is such behaviour by design? Any help appreciated!

image image
chrisjansky commented 1 year ago

Update: Running the same without executeVanillaTest in a packaged .drawbot file fixes the issue. Seems like a bug then?

typemytype commented 1 year ago

Never use executeVanillaTest inside an running app, this creates a new runloop and closes runloops, which is not good!!

This a made just to be able to run vanilla in terminal

chrisjansky commented 1 year ago

@typemytype Thank you for the quick response.

Perhaps I worded it poorly: yes, I know not to use executeVanillaTest inside DrawBot.app. I am executing it as a standalone .py file from Terminal—that’s where the bug (?) occurs.

A bit related: Frederik, do you know any resource regarding how the font vertical metrics (hhea, OS/2 and win) should be set up to work correctly in DrawBot (= CoreText, I presume), please?

I am using Glyphs and have been following the best practices (specifically "the Webfont strategy (2019)") but that seems to be problematic in DrawBot.

For instance, lineGap seems to make a big difference between left (positive lineGap) and right (lineGap = 0) when using the code below.

image
fs = 340
leading = 1
txt = "Hg\nHg"
fonts = ["FontA.otf", "FontB.otf"]

fontSize(fs)
lineHeight(fs * leading)

x, y = 50, 50
for fnt in fonts:
    font(fnt)
    w, h = textSize(txt)
    coords = x, y, w, h
    textBox(txt, coords)
    with savedState():
        fill(None)
        stroke(1, 0, 0)
        rect(*coords)
    x += w

Thank you!