Boustrophedonic typesetting



  • boustrophedonic.png

    txt = "Boustrophedon is a type of bi-directional text, mostly seen in ancient manuscripts and other inscriptions. Alternate lines of writing are flipped, or reversed, with reversed letters. Rather than going left-to-right as in modern European languages, or right-to-left as in Arabic and Hebrew, alternate lines in boustrophedon must be read in opposite directions. Also, the individual characters are reversed, or mirrored. It was a common way of writing in stone in ancient Greece.".upper()
    
    
    def boustrophedonicTextBox(txt, box, drawFrame=False):
    
        x, y, w, h = box
        L = fontLineHeight()
    
        save()
        translate(x, y-L)
    
        T = ''
        lineLength = 0
        backward = False
        words = txt.split()
        lineCount = 0
        overflow = ''
    
        for i, word in enumerate(words):
    
            # add word to current line
            if lineLength + textSize(word)[0] <= w:
                T += word + ' '
                lineLength += textSize(word + ' ')[0]
    
            # break to next line
            else:
                # no more space for new line
                if not (lineCount+1) * L < h:
                    overflow = T + ' '.join(words[i:])
                    break
    
                # draw current line
                with savedState():
                    # flip direction
                    if backward:
                        scale(-1, 1)
                    text(T, (0, 0), align='center')
    
                # begin new line
                translate(0, -L)
                backward = not backward
                lineCount += 1
                T = word + ' '
                lineLength = textSize(word + ' ')[0]
    
            # draw last line
            if i == len(words)-1:
                lineCount += 1
                with savedState():
                    if backward:
                        scale(-1, 1)
                    text(T, (0, 0), align='center')
    
        restore()
    
        if drawFrame:
            with savedState():
                fill(None)
                stroke(1, 0, 0)
                rect(x - w/2, y, w, -h)
    
        return overflow
    
    
    font('Skia')
    fontSize(36)
    lineHeight(66)
    txt = boustrophedonicTextBox(txt, (width()/2, height()-100, width()-80, 800), drawFrame=False)
    
    print(txt)
    


  • Thank you @gferreira! This is a nice example 🙂
    Also a great motivation to spent a bit more time on writing well structured code. My scripts tend to get messy very quickly. 😂



  • @imik glad to hear that. cheers!!


Log in to reply