Defining lineHeight and first baseline: status?



  • I've reviewed everything I could find that people have posted here and on the DB GitHub repo about the difficulties with defining lineHeight and first baselines, and the conversations/threads feel either abortive or circular/self-referential—so let me ask this outright: Is it (still) impossible to specify exactly the lineHeight of a FormattedString (in a given line of text, across multiple lines, from one line to the next) and the position of the baseline of the first line of text in a textBox? Or, have (precise, reliable) solutions been found to these problems?

    Even fixes that have to be tailored to a given font that still allow me to set type algorithmically would be welcome. I'm going to have a lot of diagrams and specimens to make soon, and I'd really hate to drag InDesign and manual typesetting back into my workflow any more than I absolutely have to …



  • @MauriceMeilleur

    not sure I understand your question correctly.
    my assumption is you want to specify a position for the first baseline and have the textbox draw the text so the first line is at that given position no matter the lineheight or font size?
    the following script should do that. I tested a few fonts, fontSizes and lineHeights.

    # -----------------------------------
    #  S E T T I N G S  
    
    fnt = 'Helvetica'
    
    fnt_s = 16  # the fontsize 
    lh = fnt_s * 1.6 # the lineheight 
    
    txt = 'Testing the position of the baseline and the setting of the line height in drawBot. '*30
    
    tbx_x = 100  # x position of the textbox 
    
    frst_bl_y = 657 # the exact position of the first baseline within the textbox 
    
    tbx_w = 350 # textbox width
    tbx_h = 400 # textbox heigth
    
    
    # --------------
    # F U N C T I O N S 
    
    def calc_baseline_offset(fnt, fnt_s, lh):
        '''calculates the position of the first baseline of a placeholder textbox and returns the distance from the top edge of the textbox'''
        fstr = FormattedString('Hm', font = fnt, fontSize = fnt_s, lineHeight = lh) 
        temp_first_baseline = textBoxBaselines(fstr, (0, 0, fnt_s * 3, fnt_s * lh))[0][1]
        return temp_first_baseline - (fnt_s * lh)
    
    # -----------------------------------
    #  D R A W I N G S  
    
    
    newPage('A4')
    
    fstr = FormattedString(txt, font = fnt, fontSize = fnt_s, lineHeight = lh)
    
    rect(tbx_x - 20, frst_bl_y, tbx_w + 40, -1) # drawing the first baseline 
    
    y_shift  = calc_baseline_offset(fnt, fnt_s, lh)
    
    
    tbx_y = frst_bl_y - tbx_h - y_shift # the y position for the textbox so that the first baseline stays at the given value
    
    fill(1, 0, 0, .1)
    rect(tbx_x, tbx_y, tbx_w, tbx_h)
    
    textBox(fstr, (tbx_x, tbx_y, tbx_w, tbx_h))
    
    # drawing all the baselines 
    for x, y in textBoxBaselines(fstr, (tbx_x, tbx_y, tbx_w, tbx_h)):
        rect(tbx_x, y, tbx_w, -1)
        
    
    

    asdfasdf.png


Log in to reply