Clip off text in textBox



  • Hello,

    Started playing around in Drawbot and loving it so far. I am wondering – is there a way to clip off text inside a textBox on a glyph-by-glyph basis?

    I understand dictionary-based hyphenation takes priority, so that‘s what happens:

    Screenshot 2022-08-06 at 10.11.30.png

    What I am after is more like:
    AaBbCc 2
    AaBbCc 22
    AaBbCc 225
    AaBbCc 225p
    AaBbCc 225pt

    And not:
    AaBbCc
    AaBbCc
    AaBbCc 225
    AaBbCc 225pt

    My code so far:

    newPage(1500,1500)
    
    lh = 10
    
    factor = 1
    for i in range(17):
        fs = 1 * (i + 1) * factor
        fontSize(fs)
        lineHeight(fs * 0.8)
        textBox(f"AaBbCc {round(fs)} pt", (10, lh, width(), fs))
        lh += fontLineHeight() + 7
        factor += 1
    

    Hopefully makes sense :—) Thanks for any pointers!


  • admin

    Just make the textBox wider? 😉



  • Yes, that would be a workaround. Ideally I‘d like to show all fully visible glyphs and hide all remaining that don‘t fit within canvas bounds, like this:

    Screenshot 2022-08-08 at 12.46.27.png

    I was just wondering if there‘s something like hyphenation() that switches from words to glyphs if you know what I mean. 😇


  • admin

    you can calculate the textSize in side a while loop and stop if the text is smaller then the page width:

    newPage(1500,1500)
    
    lh = 10
    
    factor = 1
    for i in range(17):
        fs = 1 * (i + 1) * factor
        fontSize(fs)
        lineHeight(fs * 0.8)
        # store the text to draw in a variable
        t = f"AaBbCc {round(fs)} pt"
        # start the while, while there is some text in t
        while t:
            # get the text size of that string
            w, h = textSize(t)
            # check if the width is smaller then the page width
            if w < width():
                # break the while loop 
                break
            # the string is still wider then the page width: remove the last character
            t = t[:-1]
        textBox(t, (10, lh, width(), fs))
        lh += fontLineHeight() + 7
        factor += 1
    

    good luck!



  • @frederik Works beautifully, thank you!


Log in to reply