python - 在圆形路径中写入文本
问题描述
我正在尝试使用GTK/Cairo在圆形路径中编写一串文本,类似于此图像(https://logosbynick.com/wp-content/uploads/2018/12/step8.png):
最好我想在Go中执行此操作,但如果我有C或Python或任何示例,我相信我可以自己将其转换为Go和GoTK3。
所以我找到了一个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) 并看到它变成一个圆圈,我相信。我明天会继续努力。:
解决方案
好吧,就我而言,这是:
使用下面的 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")
推荐阅读
- amazon-web-services - 如何从 ALB OIDC 会话中注销?
- math - 如何使用高斯插值?
- mongodb - go mongodb FindOneAndUpdate 几个字段
- java - 使用 Java swing GUI 的形状计算器
- android - 特定铃声 firebase 通知 xamarin.android
- java - 没有枚举常量 org.gradle.api.JavaVersion.VERSION_14
- android - 如何使用数据绑定在同一视图上添加单击和长单击侦听器?
- python - 在python中的字典中获取具有最大值的键
- amazon-web-services - 访问 Amazon 全球基础设施区域表(用于 RestAPI 或 dotNet)
- reactjs - 在 React 中突出显示菜单