简体   繁体   English

在Python中测试一个字形是否以相同字体反映了另一个字形

[英]Testing in Python whether one glyph is a reflection of another in the same font

Inspired by List of all unicode's open/close brackets? 受到所有Unicode的打开/关闭方括号列表的启发吗? I'm trying to find a list of all unicode glyphs in a given font that are reflections of each other. 我正在尝试查找给定字体中所有相互反映的Unicode字形的列表。 First I just need to be able to test whether one glyph is a reflection of another. 首先,我只需要能够测试一个字形是否是另一个字形的反映。 Below I have two different attempts (two different implementations of my render_char function) but I'm not able to identify '(' and ')' as mirror images using either one. 下面,我有两种不同的尝试(我的render_char函数的两种不同实现),但是我无法使用其中任何一种将'('和')'识别为镜像。 How can I do this? 我怎样才能做到这一点?

from PIL import Image,ImageDraw,ImageFont
import freetype
import numpy as np

def render_char0(c):
    # Based on https://github.com/rougier/freetype-py/blob/master/examples/hello-world.py
    # Needs numpy (blech) and the image comes out the inverse of the way I expect
    face = freetype.Face("/Library/Fonts/Verdana.ttf")
    face.set_char_size( 48*64 )
    face.load_char(c)
    bitmap = face.glyph.bitmap
    w,h = bitmap.width, bitmap.rows
    Z = np.array(bitmap.buffer, dtype=np.ubyte).reshape(h,w)
    return Image.fromarray(Z, mode='L').convert('1')

def render_char1(c):
    # Based on https://stackoverflow.com/a/14446201/2829764
    verdana_font = ImageFont.truetype("/Library/Fonts/Verdana.ttf", 20, encoding="unic")
    text_width, text_height = verdana_font.getsize(c)
    canvas = Image.new('RGB', (text_width+10, text_height+10), (255, 255, 255))
    draw = ImageDraw.Draw(canvas)
    draw.text((5,5), c, font = verdana_font, fill = "#000000")
    return canvas

for render_char in [render_char0, render_char1]:
    lparen = render_char('(')
    rparen = render_char(')')
    mirror = lparen.transpose(Image.FLIP_LEFT_RIGHT)

    mirror.show()
    rparen.show()
    print mirror.tobytes() == rparen.tobytes() # False

There is a text file called BidiMirroring.txt in the Unicode plain-text database with a list of all mirrored characters. 在Unicode纯文本数据库中有一个名为BidiMirroring.txt的文本文件,其中包含所有镜像字符的列表。 That file is easy to parse by programs. 该文件很容易被程序解析。

Current url is http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt 当前网址是http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt

I don't think using the rendered glyphs can work reliably. 我认为使用渲染字形不能可靠地工作。 There's a lot of reasons why eg. 例如,有很多原因。 ( and ) are no exact mirror images, like spacing around the character, hinting and anti-aliasing, maybe the font is slightly slanted, or maybe the font designer has just make the two brackets a bit different etc. Other characters are rotated, rather than mirrored, like and in some fonts, and the Chinese quotation marks and . ()并非精确的镜像,例如字符周围的间距,提示和抗锯齿,可能是字体略微倾斜,或者字体设计者只是使两个括号略有不同,等等。其他字符是旋转的,而不是而不是镜像,例如某些字体中的 ,并且中文引号

I think rendering is the wrong aproach. 我认为渲染是错误的方法。 It depends on the font and wether the font knows how to render this. 它取决于字体,并且字体是否知道如何呈现它。 I heard that unicode characters have a specification for this symmetry. 我听说Unicode字符对此对称性有规范。 Maybe it is encoded in their name. 也许是以他们的名字编码的。 "LEFT" and "RIGHT" "SUBSCRIPT". “向左”和“向右”“订阅”。 Have a look at http://xahlee.info/comp/unicode_matching_brackets.html 看看http://xahlee.info/comp/unicode_matching_brackets.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 Python 中字体字形的轮廓坐标 - Get outline coordinates of a font glyph in Python Python 散景:矩形不使用另一个字形呈现 - Python Bokeh: Rect not rendering with another glyph Python 测试字符串是否是一组特定值之一 - Python testing whether a string is one of a certain set of values 尝试根据元素是否在 Python 中的另一个列表中来过滤一个列表 - Trying to filter one list according to whether elements are in another list in Python 仅在 Python 散景中显示一个字形的 Hover 工具提示 - Only show Hover Tooltip for One Glyph in Python bokeh 相同的 python 脚本在一台计算机上工作,但在另一台计算机上不起作用 - Same python script works in one computer but not in another Python上的字形匹配 - Glyph matching on Python Python测试字符串是否具有“%”且没有中断 - Python testing whether a string has “%” and not have it break Python测试整数是否在一定范围内 - Python Testing whether an integer is within a certain range 如何将一个数据帧与另一个数据帧进行比较并检查第二个 df 中是否存在第一个 df 中的相同数据 - How to compare one dataframe with another and check whether the same data in first df present in second df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM