【问题标题】:Text alignment control in ShadyShady 中的文本对齐控制
【发布时间】:2020-06-06 21:03:01
【问题描述】:

我正在使用 Shady 在屏幕上写一些文本,我想知道控制字符串对齐的最简单方法是什么。据我了解,Shady 文本对象中的 align 参数控制段落对齐方式,但我对控制单行文本的对齐方式感兴趣。
本质上,我想复制 matplotlib 文本函数的 horizo​​ntalalignmentverticalalignmentrotation 参数的行为。但要做到这一点,我需要估计渲染后字符串将占用的区域(以像素为单位)。我能以某种方式从Shady那里得到它吗?在手册中说渲染是在 CPU 上完成的,然后渲染的 String 被推送到 GPU,所以它应该是可行的。

【问题讨论】:

    标签: python text text-alignment shady


    【解决方案1】:

    您说得对,.text.align.text.wrapping 属性仅在文本流的逻辑级别与“对齐”有关,即多行文本刺激的行如何对齐 相对于彼此在它们被读取的坐标系中(与整个刺激的物理方式无关)。

    您正在谈论的属性——旋转、“垂直对齐”,甚至如果只有一行文本在播放,甚至你所谓的“水平对齐”——都不是特定于文本的属性:它们同样适用于任何矩形补丁。因此,您要操作的属性是stim.* 级别的属性,而不是stim.text.*。具体来说,它们是.anchor.rotation,如下所示:

    #!/usr/bin/env python -m Shady shell
    
    import Shady, Shady.Text
    w = Shady.World(fullScreenMode=False)
    
    axes = w.Stimulus(Shady.PixelRuler(1000), anchor=Shady.LOWER_LEFT, size=600)
    
    xlabel = w.Stimulus(text='x axis label', x=300, y=0, anchor=Shady.TOP) 
    ylabel = w.Stimulus(text='y axis label', x=0, y=300, anchor=Shady.BOTTOM, rotation=90)
    
    speed = 30
    msg = w.Stimulus(
        xy = 300,
        rotation = Shady.Integral( lambda t: speed ),
        text = 'Change msg.anchor to anything\nbetween [-1,-1] and [+1,+1]\nand see what happens',
        text_blockbg = [0, 0, 0, 0.5],
    )
    Shady.AutoFinish(w)
    

    Shady.Text 的未记录函数中的某个地方,可能有某种方法可以提前估计渲染文本刺激的大小。事实上,仔细研究后,看起来最不烦人的方法是实际制作纹理数组:

    img = Shady.Text.MakeTextImage('hello world')
    heightInPixels, widthInPixels, _ = img.shape
    

    但希望通过适当地使用.anchor,您将不再需要它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多