2015-10-04 58 views
0

List of all unicode's open/close brackets?啓發我試圖找到給定字體中所有unicode字形的列表,這些字形是彼此的反射。首先,我只需要能夠測試一個字形是否是另一個字形的反射。下面我有兩個不同的嘗試(我的render_char函數的兩個不同的實現),但我無法使用任何一個將鏡像映射爲'('和')'。我怎樣才能做到這一點?在Python中測試一個字形是否是同一字體中的另一個字形的反射

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 
+1

我認爲rendereing是錯誤的方法。它取決於字體和*字體*知道如何渲染這種對稱性。我聽說unicode字符有這個規範。也許它是用他們的名字編碼的。 「左」和「右」「下標」。看看http://xahlee.info/comp/unicode_matching_brackets.html – User

回答

5

有一個叫以Unicode純文本數據庫BidiMirroring.txt與所有鏡像字符列表的文本文件。該文件很容易被程序解析。

當前的URL是http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt

我不使用所呈現的字形能可靠工作的想法。有很多原因,例如。 ()沒有精確的鏡像,如字符間的間距,提示和反走樣,也許字體稍微傾斜,或者字體設計器可能使兩個括號稍微不同等。其他字符旋轉,而不是比鏡像,如某些字體中的,以及中文引號

相關問題