首页 > 解决方案 > 在圆形路径中写入文本

问题描述

我正在尝试使用GTK/Cairo在圆形路径中编写一串文本,类似于此图像(https://logosbynick.com/wp-content/uploads/2018/12/step8.png):

在此处输入图像描述

最好我想在Go中执行此操作,但如果我有CPython或任何示例,我相信我可以自己将其转换为GoGoTK3

所以我找到了一个Python示例https://eurion.net/python-snippets/snippet/Warped%20Text.html,它输出以下图像:

在此处输入图像描述

我想我可以很容易地简化代码来写一个圆圈,毕竟我有 30 年的 15 多种不同语言的编程经验。不幸的是,我现在已经花了几个小时尝试,但我无法让它发挥作用。也许我对Python的了解不够多,也许我对GTK/Cairo和文本路径的了解不够多,或者我 30 年的编程经验还不够。

所以,这有点尴尬,但是有人可以帮我吗?

[编辑] 这是我在 Python 中最接近的尝试:

#!/usr/bin/env python
# [SNIPPET_NAME: Warped Text]
# [SNIPPET_CATEGORIES: Cairo]
# [SNIPPET_DESCRIPTION: Generate some warped text]
# [SNIPPET_DOCS: http://www.tortall.net/mu/wiki/CairoTutorial]
# [SNIPPET_LICENSE: MPL]

import cairo
import math
import os


def warp_path(ctx, function):
    first = True

    for pathType, points in ctx.copy_path():
        if pathType == cairo.PATH_MOVE_TO:
            if first:
                ctx.new_path()
                first = False
            x, y = function(*points)
            ctx.move_to(x, y)

        elif pathType == cairo.PATH_LINE_TO:
            x, y = function(*points)
            ctx.line_to(x, y)

        elif pathType == cairo.PATH_CURVE_TO:
            x1, y1, x2, y2, x3, y3 = points
            x1, y1 = function(x1, y1)
            x2, y2 = function(x2, y2)
            x3, y3 = function(x3, y3)
            ctx.curve_to(x1, y1, x2, y2, x3, y3)

        elif pathType == cairo.PATH_CLOSE_PATH:
            ctx.close_path()


def circle(x, y):
    radius = math.sqrt(x*x + y*y)
    theta = math.sinh(y/radius)
    x_new = radius*math.sin(theta)
    y_new = radius*math.cos(theta)
    return x_new, y_new


Width, Height = 512, 512
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, Width, Height)
context = cairo.Context(surface)
solid_pattern = context.get_source()

# background
context.rectangle(0, 0, Width, Height)
context.fill()

# foreground
context.set_source(solid_pattern)
context.set_source_rgb(1, 1, 1)
context.select_font_face("Sans")
context.set_font_size(10)

# circle text
context.new_path()
context.move_to(20, 150)
# context.arc(256, 256, 200, 0, 2*math.pi)
context.text_path("circle test circle test circle test circle test circle test circle test")
warp_path(context, circle)
context.fill()

surface.write_to_png(os.path.expanduser("~") + "/temp/warpedtext.png")

生成此图像: 在此处输入图像描述

[编辑 2] 我想我需要一个像这样将矩形转换为圆形的函数(如果可以理解的话)。然后我应该能够将文本写入 (0,0) 并看到它变成一个圆圈,我相信。我明天会继续努力。:

在此处输入图像描述

标签: pythongogtkcairo

解决方案


好吧,就我而言,这是:

迄今为止最近的

使用下面的 Python 代码,结果很丑陋,根本不是我想要的。开头和结尾是扭曲的,并且没有定位在我期望的位置。无论如何,我暂时放弃这个小项目,如果有人提出一个完美的变换方程,我可能会再次接受它。

#!/usr/bin/env python
# [SNIPPET_NAME: Warped Text]
# [SNIPPET_CATEGORIES: Cairo]
# [SNIPPET_DESCRIPTION: Generate some warped text]
# [SNIPPET_DOCS: http://www.tortall.net/mu/wiki/CairoTutorial]
# [SNIPPET_LICENSE: MPL]

import cairo
import math
import os


def warp_path(ctx, function):
    first = True

    for pathType, points in ctx.copy_path():
        if pathType == cairo.PATH_MOVE_TO:
            if first:
                ctx.new_path()
                first = False
            x, y = function(*points)
            ctx.move_to(x, y)

        elif pathType == cairo.PATH_LINE_TO:
            x, y = function(*points)
            ctx.line_to(x, y)

        elif pathType == cairo.PATH_CURVE_TO:
            x1, y1, x2, y2, x3, y3 = points
            x1, y1 = function(x1, y1)
            x2, y2 = function(x2, y2)
            x3, y3 = function(x3, y3)
            ctx.curve_to(x1, y1, x2, y2, x3, y3)

        elif pathType == cairo.PATH_CLOSE_PATH:
            ctx.close_path()


def circle(x, y):
    radius = -y
    theta = -math.sin(x * math.pi/2) + math.pi
    x_new = radius * math.sin(theta)
    y_new = radius * math.cos(theta)

    return x_new, y_new


Width, Height = 512, 512
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, Width, Height)
context = cairo.Context(surface)
solid_pattern = context.get_source()

# background
context.rectangle(0, 0, Width, Height)
context.fill()

# foreground
context.set_source(solid_pattern)
context.set_source_rgb(1, 1, 1)
context.select_font_face("Sans")
context.set_font_size(0.05)

SCALE = 256
CENTER = 1

# circle text
context.new_path()
context.scale(SCALE, SCALE)
context.translate(CENTER, CENTER)
context.move_to(-0.98, -0.95)
context.text_path("Administration Settings Sound & Video Programming Office Games Silicon")
warp_path(context, circle)
context.fill()

surface.write_to_png(os.path.expanduser("~") + "/temp/warpedtext.png")

推荐阅读