Menu

Find label boundingbox (no margins)

Help
gummybears
2015-01-04
2015-01-08
  • gummybears

    gummybears - 2015-01-04

    What I try to do is to get the exact bounding box of
    a label with no margins.

    This is what I tested

    import labelpath;
    size(1cm,1cm);

    // use Helvetica, but not needed
    pen fontpen = Helvetica();

    Label lv_label;
    pair lv_pos;

    lv_pos = (2,1);
    lv_label = Label("text",pos,Align,fontpen);

    // Get Tex path
    path[] p=texpath(lv_label);
    filldraw(p,red,green);

    // bounding box of label
    draw(min(p)--(min(p).x,max(p).y)--max(p)--(max(p).x,min(p).y)--cycle);
    // draw the label
    label(lv_label);

    The label as drawn by label is different in size as that drawn using the texpath.
    The Tex path boundingbox is correct, but I don't understand why the labels differ.

     
  • James

    James - 2015-01-05

    Hi gummybears,
    I don't have the solution for you, but I did notice that the "import labelpath;" command is not needed in the example. Also, removing the "size(1cm,1cm);" command causes the two labels to be the same size. Maybe that will help you figure out the root cause?
    Good luck!

     
  • gummybears

    gummybears - 2015-01-05

    Hi James,

    I now understand the problem, it has to do with deferred drawing, which I don't understand (yet).

    Thanks James for helping.

     

    Last edit: gummybears 2015-01-05
  • Olivier Guibé

    Olivier Guibé - 2015-01-05

    Hello

    As you understand, since the label is transformed in a path
    the final size of the labelpath depends on the whole picture and is
    adapted. On the other side, a standard label has the right size
    (in 12pt by default) whatever the size of the picture is (and if no transformation is made on the label).
    Yes it is related to deferred drawing (which I do not understand also).
    Depending on your goal it is possible to have a label with the right
    size + zero margin bounding box by using a frame. A frame is in postscript coordinates. For example

    import fontsize;
    size(5cm,5cm);
    
    // use Helvetica, but not needed
    pen fontpen = Helvetica()+fontsize(40);
    Label lv_label;
    pair lv_pos;
    
    lv_pos = (2,1);
    lv_label = Label("Happy New Year",lv_pos,Align,fontpen);
    
    // Get Tex path
    path[] p=texpath(lv_label);
    frame f;
    filldraw(f,p,red,green);
    draw(f,box(f,defaultpen+3bp));
    add(f);
    

    Perhaps I do not answer exactly to your question.

    O.G.

     
  • gummybears

    gummybears - 2015-01-06

    Hi Olivier

    Happy New Year too, and to everyone on this list, well any list.
    Thanks for your detailed answer.

    My goal is just to get the bounding box of the label (irrespective of
    its font size, with zero margin, in user coordinates) and use the information to do some extra drawing. I will investigate both examples to see where I need to change my code.

     
  • Olivier Guibé

    Olivier Guibé - 2015-01-08

    Hi

    I am not sure that it is possible to compute the exact bounding box
    of the label in the user coordinate, since a new draw command can change
    all. However you can study deeply the deferred drawing and flowchart.asy
    and any drawing is possible around a label, between two labels.
    For example

    import fontsize;
    size(15cm,15cm);
    // use Helvetica, but not needed
    pen fontpen = Helvetica()+fontsize(40);
    Label lv_label;
    pair lv_pos;
    
    lv_pos = (2,1);
    lv_label = Label("Happy New Year",lv_pos,Align,fontpen);
    // Get Tex path
    path[] p=texpath(lv_label);
    frame f1;
    filldraw(f1,p,red,green);
    draw(f1,box(f1,defaultpen+3bp));
    add(f1,(0,1));
    
    frame f2;
    filldraw(f2,p,red,green);
    draw(f2,box(f2,defaultpen+3bp));
    add(f2,(1,2));
    
    add(new void(picture pic, transform t) {
        pair a=t*(0,1)+size(f1)-5bp*dir((1,1));
        pair b=t*(1,2);
        path s=a {-2,0} .. b {1,0};
        draw(pic,s,red+4bp);
      });
    

    Another solution is to work with 1bp unitsize.

     

    Last edit: Olivier Guibé 2015-01-08

Log in to post a comment.