BezierPath offset problem.



  • Hi,
    I would like to translate a BezierPath, without changing it's bounds.

    Here is code where you can see my problem.

    newPage(2000,2000)
    
    # Grey Rectangle
    path0 = BezierPath()
    path0.rect(1000,10,200,1990)
    with savedState():
        fill(0, 0.1)
        drawPath(path0)
    
    # Green Rectangle
    path1 = BezierPath()
    path1.text("1", fontSize=200)
    with savedState():
        fill(0,1,0)
        rect(*path1.bounds())
    drawPath(path1)
    
    # Red Rectangle
    path2 = BezierPath()
    path2.text("2", fontSize=200, offset=(200,300))
    with savedState():
        fill(1,0,0)
        rect(*path2.bounds())
    drawPath(path2)
    
    # Yellow Rectangle
    path3 = BezierPath()
    path3.text("3", fontSize=200)
    path3.translate(100,800)
    with savedState():
        fill(1,1,0)
        rect(*path3.bounds())
    drawPath(path3)
    
    # Magenta Rectangle
    translate(900,800)
    path4 = BezierPath()
    path4.text("4", fontSize=200)
    with savedState():
        fill(1,0,1)
        rect(*path4.bounds())
    drawPath(path4)
    

    I tested 3 solution:

    • use offset in path.text
      ❌ Change BezierPath bounds

    • use path.translate
      ❌ Change BezierPath bounds

    • use translate
      ✅ Don't change BezierPath bounds

    But this is annoying, because I don't want to translate all my canvas just for that.

    So what is the solution ?



  • I found a way.

    newPage(2000,2000)
    
    # Red Rectangle
    path2 = BezierPath()
    path2.text("2", fontSize=200, offset=(200,300))
    with savedState():
        fill(1,0,0)
        x = path2.getNSBezierPath().bounds().origin.x
        y = path2.getNSBezierPath().bounds().origin.y
        w = path2.getNSBezierPath().bounds().size.width
        h = path2.getNSBezierPath().bounds().size.height
        rect(x,y,w,h)
    drawPath(path2)
    

    I’m not sure if this is even allowed, maybe @frederik will take some reputation points away from me ;-). Also, I’m not sure if the way offset works is correct, or if it’s a bug.

    Personally, I would just wrap your fourth solution in a savedState() and accept all the translations of the canvas. I think that would be the DrawBot way.

    # Magenta Rectangle
    with savedState():
        translate(900,800)
        path4 = BezierPath()
        path4.text("4", fontSize=200)
        with savedState():
            fill(1,0,1)
            rect(*path4.bounds())
        drawPath(path4)
    

  • admin

    the mistake here is that bounds are not structured as x, y, width, height as required for rect(...)but as minx, miny, maxx, maxy.

    minx, miny, maxx, maxy = path.bounds()
    rect(minx, miny, maxx-minx, maxy-miny)
    


  • @frederik Thanks for this clarification !


Log in to reply