如何使用 Cairo 将文本保留在圆圈内?

发布于 2024-08-31 14:01:02 字数 1305 浏览 9 评论 0原文

我使用 Cairo (特别是 pycairo)绘制图表,我需要知道如何通过将文本保持在圆的边界内,在不重叠的情况下在圆内绘制文本。我有这个简单的代码片段,在圆圈内绘制一个字母“a”:

'''
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

问题是我的标签具有可变数量的字符(限制为 20),并且我需要动态设置字体的大小。无论圆圈大小或标签大小,它都必须适合圆圈内。此外,每个标签都有一行文本,没有空格,没有换行符。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-09-07 14:01:02

我遇到了类似的问题,我需要调整字体的大小,以将对象的名称保持在矩形的边界内,而不是圆形的边界内。我使用了 while 循环,并不断检查字符串的文本范围大小,减小字体大小直到适合。

这是我所做的:(这是在 Kylix 下使用 C++,Delphi 的衍生产品)。

    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;
}

当然,这并不显示错误检查。如果矩形(或圆)太小,则必须跳出循环。

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.

我很OK 2024-09-07 14:01:02

由于圆圈的大小并不重要,因此您应该以与代码相反的顺序绘制它们。

  1. 在屏幕上打印文本
  2. 计算文本边界(使用文本范围)
  3. 在文本周围画一个比文本稍大一点的圆圈。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文