登录
首页 >  Golang >  Go问答

用文本书写在圆形轨道上

来源:stackoverflow

时间:2024-02-17 10:45:23 125浏览 收藏

哈喽!今天心血来潮给大家带来了《用文本书写在圆形轨道上》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我正在尝试使用 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) 并看到它变成一个圆圈。明天我会继续努力。 :


解决方案


嗯,这就是我目前为止的情况:

使用下面的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")

今天关于《用文本书写在圆形轨道上》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>