views:

201

answers:

2

I a drawing a graph using Cairo (pycairo specifically) and I need to know how can I draw text inside a circle without overlapping it, by keeping it inside the bounds of the circle. I have this simple code snippet that draws a letter "a" inside the circle:

'''
Created on May 8, 2010

@author: mrios
'''
import cairo, math

WIDTH, HEIGHT = 1000, 1000

#surface = cairo.PDFSurface ("/Users/mrios/Desktop/exampleplaces.pdf", WIDTH, HEIGHT)
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.scale (WIDTH/1.0, HEIGHT/1.0) # Normalizing the canvas


ctx.rectangle(0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
ctx.set_source_rgb(255,255,255)
ctx.fill()

ctx.arc(0.5, 0.5, .4, 0, 2*math.pi)
ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.03)
ctx.stroke() 

ctx.arc(0.5, 0.5, .4, 0, 2*math.pi)
ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.01)
ctx.set_source_rgb(255,0,255) 
ctx.fill()
ctx.set_source_rgb(0,0,0)

ctx.select_font_face("Georgia",
            cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ctx.set_font_size(1.0)
x_bearing, y_bearing, width, height = ctx.text_extents("a")[:4]
print ctx.text_extents("a")[:4]
ctx.move_to(0.5 - width / 2 - x_bearing, 0.5 - height / 2 - y_bearing)
ctx.show_text("a")

surface.write_to_png ("/Users/mrios/Desktop/node.png") # Output to PNG

The problem is that my labels have variable amount of characters (with a limit of 20) and I need to set the size of the font dynamically. It must fit inside the circle, no matter the size of the circle nor the size of the label. Also, every label has one line of text, no spaces, no line breaks.

Any suggestion?

+1  A: 

I had a similar issue, where I need to adjust the size of the font to keep the name of my object within the boundaries of rectangles, not circles. I used a while loop, and kept checking the text extent size of the string, decreasing the font size until it fit.

Here what I did: (this is using C++ under Kylix, a Delphi derivative).

    double fontSize = 20.0;
    bool bFontFits = false;

    while (bFontFits == false)
    {
        m_pCanvas->Font->Size = (int)fontSize;
        TSize te = m_pCanvas->TextExtent(m_name.c_str());
        if (te.cx < (width*0.90))  // Allow a little room on each side
        {
            // Calculate the position
            m_labelOrigin.x = rectX + (width/2.0) - (te.cx/2);
            m_labelOrigin.y = rectY + (height/2.0) - te.cy/2);
            m_fontSize = fontSize;
            bFontFits = true;
            break;
        }
        fontSize -= 1.0;
}

Of course, this doesn't show error checking. If the rectangle (or your circle) is too small, you'll have to break out of the loop.

Steve
A: 

Since the size of the circle does not matter you should draw them in the opposite order than your code.

  1. Print the text on screen
  2. Calculate the text boundaries (using text extents)
  3. Draw a circle around the text that is just a little bigger from the text.
kazanaki