Group Details Private

drafts

  • RE: Different fonts in a text box

    hello @mït,

    I think I understand what you mean – it’s not so easy because, once a piece of text is drawn into the page, you can’t really ‘search’ for it anymore.

    it can be done if you lay out the text yourself: word by word, line by line, page by page. the example script below is based on this one.

    Screen Shot 2020-09-21 at 12.51.01.png

    TXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    FONT = 'Menlo-Bold'
    FONTSIZE = 36
    LINEHEIGHT = 120
    WIDTH = 570
    X, Y = 150, 900
    HIGHLIGHT = 'voluptate'
    PAGEHIGHLIGHT = None
    
    # start with an empty string, add words one by one
    T = FormattedString(font=FONT, fontSize=FONTSIZE, lineHeight=FONTSIZE*1.2)
    
    # set text properties globally - needed for correct measurements with `textSize`
    font(FONT)
    fontSize(FONTSIZE)
    
    # variables to keep track of the current line
    lineLength = 0
    lineNumber = 1
    lineY = Y
    
    # iterate over words in text
    for word in TXT.split():
    
        # if word fits in the current line, add word to line
        if lineLength + textSize(word)[0] <= WIDTH:
            T += word + ' '
            lineLength += textSize(word + ' ')[0]
    
        # word does not fit in current line, make a new line
        else:        
    
            # if next line does not fit in current page, start a new page
            if lineY - LINEHEIGHT < 0:
                newPage()
                font(FONT)
                fontSize(FONTSIZE)
                lineY = Y
                
            # draw the current line
            text(T, (X, lineY))
    
            # check if highlight word was in this line and page
            if HIGHLIGHT in str(T):
                PAGEHIGHLIGHT = pageCount()
                color = (1, 0, 0)
            else: 
                color = (0.7,)
            fill(*color)
            text(str(lineNumber), (X-50, lineY), align='right')
    
            # make a fresh new line, add word to line
            T = FormattedString(font=FONT, fontSize=FONTSIZE, lineHeight=FONTSIZE*1.2)
            T += word + ' '
    
            # update variables for new line
            lineLength = textSize(word + ' ')[0]
            lineNumber += 1
            lineY -= LINEHEIGHT
    
    # draw page numbers, check if highlight word in page
    for i, page in enumerate(pages()):
        with page:
            color = (1, 0, 0) if (i+1) == PAGEHIGHLIGHT else (0.7,)
            fill(*color)
            text(str(i+1), (width()-50, Y), align='right')
    

    hope this makes sense!

    posted in General Discussion
  • RE: Different fonts in a text box

    pageCount() returning the current page count -->https://www.drawbot.com/content/canvas/pages.html#drawBot.pageCount

    textBoxCharacterBounds (I see its not added to the docs yet..) returning a list of typesetted bounding boxes with characters and a subsetted formattedString

    help(textBoxCharacterBounds)  # :)
    

    hope this helps with your puzzle!

    posted in General Discussion
  • RE: Different fonts in a text box

    hello @mït,

    not sure if we can call it clean 🙂 but here’s a way to apply formatting to a FormattedString a posteriori using a simple find & replace:

    txt = '''Some title here
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tempus justo in finibus condimentum. Proin vitae mauris ac nibh elementum consectetur. Aenean justo mi, cursus vel placerat vitae, gravida in mi.
    '''
    
    def applyFormatting(formattedString, findText, styleDict):
    
        start = str(formattedString).find(findText)
        end = start + len(findText)
        
        before = formattedString[:start]
        after  = formattedString[end:]
    
        replaceText = formattedString.copy()
        replaceText.clear()
        replaceText.append(findText, **styleDict)
    
        return before + replaceText + after
    
    T = FormattedString(txt, fontSize=36, font="Arial")
    
    T = applyFormatting(T, 'Some title here', dict(fontSize=72, font='Times New Roman', paragraphBottomSpacing=20))
    T = applyFormatting(T, 'sit amet', dict(font='Arial Bold', fill=(1, 0, 0),))
    T = applyFormatting(T, 'condimentum', dict(font='Arial Italic', fill=(0, 1, 0),))
    
    while len(T):
        newPage('A4')
        T = textBox(T, (20, 20, width() - 40, height() - 40))
    

    adding support for markdown or html markup to FormattedString would be super useful!

    hope this helps

    posted in General Discussion
  • RE: Different fonts in a text box

    hello @mït,

    you can change text properties in the FormattedString, before passing it to textBox.

    like this:

    T = FormattedString(fontSize=42)
    T.font("Arial")
    T.append('Lorem ipsum dolor ')
    
    T.font("Arial Bold")
    T.append('sit amet, ')
    
    T.font("Arial")
    T.append('consectetur adipiscing elit. Nulla tempus justo in finibus ')
    
    T.font("Arial Italic")
    T.append('condimentum. ')
    
    T.font("Arial")
    T.append('Proin vitae mauris ac nibh elementum consectetur. Aenean justo mi, cursus vel placerat vitae, gravida in mi.')
    
    while len(T):
        newPage('A4')
        T = textBox(T, (10, 10, width() - 20, height() - 20))
    

    hope this helps! cheers

    posted in General Discussion
  • RE: access a specific frame from a gif

    hello @traviskochel,

    you can use image() inside an ImageObject to get a specific page, and then apply the filters using the image object. like this:

    path = 'myFolder/myDocument.pdf'
    
    img = ImageObject()
    
    with img:
        w, h = imageSize(path)
        size(w, h)
        image(path, (0, 0), pageNumber=4)
        
    img.falseColor(color0=(1,0,0,1))
    image(img, (0, 0))
    

    cheers!

    posted in General Discussion
  • RE: Turn off kerning
    t = "AVA"
    
    font("Garamond")
    fontSize(200)
    
    text(t, (10, 10))
    
    openTypeFeatures(kern=False)
    text(t, (10, 200))
    
    posted in General Discussion
  • RE: newDrawing() size?

    newDrawing() completely reset the drawing stack, as @imik mentioned.

    Use newDrawing if you want to generate multiple drawings, or when you are in an environment where the drawing stack is kept in memory and a hard reset of the drawing stack is required.

    see the docs: https://www.drawbot.com/content/canvas/pages.html#drawBot.newDrawing

    posted in General Discussion
  • RE: access a specific frame from a gif

    hello @eduairet,

    you can use the pageNumber argument in image or imageSize to get a given frame in a multipage image (gif or pdf). important: page numbering starts at 1.

    see Inserting a PDF

    path = '~/Desktop/image.gif'
    pageCount = numberOfPages(path)
    
    for pageNumber in range(1, pageCount+1):
        w, h = imageSize(path, pageNumber=pageNumber)
        newPage(w, h)
        image(path, (0, 0), pageNumber=pageNumber)
    

    cheers!

    posted in General Discussion
  • RE: Pixelating a Bezier Path posted in General Discussion
  • RE: Pixelating a Bezier Path

    an other option is to use path.expandStroke(10) so you can do point inside testing.

    see https://www.drawbot.com/content/shapes/bezierPath.html#drawBot.context.baseContext.BezierPath.expandStroke

    posted in General Discussion