[英]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.