真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

字的研究(3)fontTools-TrueType輪廓坐標(biāo)的獲取以及基于TrueType的Glyph實(shí)例的構(gòu)建

前言

本文主要介紹如果使用Python第三方庫(kù)fontTools提取OpenType字體文件中的TrueType輪廓坐標(biāo)以及如何構(gòu)建基于TrueType的Glyph實(shí)例

成都創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)公司成都網(wǎng)站制作、網(wǎng)站營(yíng)銷(xiāo)推廣、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),對(duì)服務(wù)成都衛(wèi)生間隔斷等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司成立于2013年,提供專(zhuān)業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!

TrueType輪廓坐標(biāo)的獲取

對(duì)于TrueType輪廓描述的OpenType文件,除了前文提到的利用ttx組件將表結(jié)構(gòu)轉(zhuǎn)化為XML文件方法,利用如下代碼也可以直接獲取具體的輪廓數(shù)據(jù):

from fontTools.ttLib import TTFont

font = TTFont("Resources/simsun.ttf")
glyph = font.getGlyphSet()["uni70E0"] # 獲取_TTGlyph實(shí)例
print(glyph._glyph.coordinates) # 坐標(biāo)
print(glyph._glyph.endPtsOfContours) # 輪廓結(jié)束點(diǎn)
print(list(glyph._glyph.flags)) # 點(diǎn)類(lèi)型flag

運(yùn)行結(jié)果如下:

GlyphCoordinates([(138, 118),(138, 86),(206, 86),(206, 118),(138, 80),(138, 49),(206, 49),(206, 80),(138, 43),(138, -19),(123, -26),(124, -5),(124, 16),(124, 99),(110, 81),(86, 67),(84, 70),(118, 100),(142, 158),(125, 158),(112, 158),(101, 155),(92, 164),(144, 164),(154, 192),(156, 209),(176, 197),(169, 192),(161, 170),(159, 164),(207, 164),(221, 177),(238, 158),(157, 158),(151, 142),(140, 124),(205, 124),(214, 134),(229, 119),(220, 114),(220, 1),(220, -17),(199, -25),(197, -9),(168, -4),(168, 0),(195, -2),(206, 0),(206, 8),(206, 43),(52, 206),(74, 194),(67, 187),(67, 123),(87, 148),(91, 161),(105, 147),(99, 146),(90, 137),(81, 128),(67, 115),(67, 91),(64, 57),(87, 46),(103, 29),(103, 22),(103, 18),(99, 7),(92, 9),(87, 22),(82, 34),(63, 52),(56, 8),(12, -26),(11, -23),(41, 13),(53, 74),(53, 149),(33, 140),(34, 126),(33, 104),(25, 92),(13, 88),(10, 95),(10, 97),(10, 102),(14, 105),(19, 109),(28, 128),(29, 140)])
[3, 7, 49, 77, 89]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]

描述TrueType輪廓的數(shù)據(jù)主要由坐標(biāo)、輪廓結(jié)束點(diǎn)以及各點(diǎn)的類(lèi)型flag組成。其中,輪廓結(jié)束點(diǎn)為各輪廓的最后一個(gè)點(diǎn)的坐標(biāo);點(diǎn)類(lèi)型flag則和坐標(biāo)一一對(duì)應(yīng),說(shuō)明該點(diǎn)是普通點(diǎn)還是貝塞爾曲線的控制點(diǎn),0為控制點(diǎn),1為普通點(diǎn)(注意,TrueType輪廓只包含二次貝塞爾曲線)。

相比之下,我個(gè)人更傾向?qū)?shù)據(jù)轉(zhuǎn)化為如下由三元組組成的二維數(shù)組的形式,更方便理解和處理:

coordinates = list(glyph._glyph.coordinates)
endPts = glyph._glyph.endPtsOfContours
flags = list(glyph._glyph.flags)

contours = []
contour = []
for i, (x,y) in enumerate(coordinates):
    contour.append((x,y,flags[i]))
    if i in endPts:
        contours.append(contour)
        contour = []

print(contours)

運(yùn)行結(jié)果如下:

[[(138, 118, 1), (138, 86, 1), (206, 86, 1), (206, 118, 1)], 
[(138, 80, 1), (138, 49, 1), (206, 49, 1), (206, 80, 1)], 
[(138, 43, 1), (138, -19, 1), (123, -26, 1), (124, -5, 0), (124, 16, 1), (124, 99, 1), (110, 81, 0), (86, 67, 1), (84, 70, 1), (118, 100, 0), (142, 158, 1), (125, 158, 1), (112, 158, 0), (101, 155, 1), (92, 164, 1), (144, 164, 1), (154, 192, 0), (156, 209, 1), (176, 197, 1), (169, 192, 1), (161, 170, 0), (159, 164, 1), (207, 164, 1), (221, 177, 1), (238, 158, 1), (157, 158, 1), (151, 142, 0), (140, 124, 1), (205, 124, 1), (214, 134, 1), (229, 119, 1), (220, 114, 1), (220, 1, 1), (220, -17, 0), (199, -25, 1), (197, -9, 0), (168, -4, 1), (168, 0, 1), (195, -2, 0), (206, 0, 0), (206, 8, 1), (206, 43, 1)], 
[(52, 206, 1), (74, 194, 1), (67, 187, 1), (67, 123, 1), (87, 148, 0), (91, 161, 1), (105, 147, 1), (99, 146, 0), (90, 137, 1), (81, 128, 0), (67, 115, 1), (67, 91, 0), (64, 57, 1), (87, 46, 0), (103, 29, 0), (103, 22, 1), (103, 18, 0), (99, 7, 0), (92, 9, 0), (87, 22, 1), (82, 34, 0), (63, 52, 1), (56, 8, 0), (12, -26, 1), (11, -23, 1), (41, 13, 0), (53, 74, 0), (53, 149, 0)],
[(33, 140, 1), (34, 126, 0), (33, 104, 0), (25, 92, 0), (13, 88, 0), (10, 95, 0), (10, 97, 1), (10, 102, 0), (14, 105, 1), (19, 109, 0), (28, 128, 0), (29, 140, 1)]]

基于TrueType的Glyph實(shí)例的構(gòu)建

構(gòu)建fontTools中的Glyph實(shí)例主要可以用于后續(xù)建立新的基于TrueType輪廓的字體文件。所采用的方法是基于前文所提到的Pen對(duì)象的子類(lèi)TTGlyphPointPen,輸入坐標(biāo)、輪廓結(jié)束點(diǎn)以及各點(diǎn)的類(lèi)型flag三項(xiàng)數(shù)據(jù),輸出Glyph實(shí)例:

from fontTools.pens.ttGlyphPen import TTGlyphPointPen

coordinates = [(138, 118),(138, 86),(206, 86),(206, 118),(138, 80),(138, 49),(206, 49),(206, 80),(138, 43),(138, -19),(123, -26),(124, -5),(124, 16),(124, 99),(110, 81),(86, 67),(84, 70),(118, 100),(142, 158),(125, 158),(112, 158),(101, 155),(92, 164),(144, 164),(154, 192),(156, 209),(176, 197),(169, 192),(161, 170),(159, 164),(207, 164),(221, 177),(238, 158),(157, 158),(151, 142),(140, 124),(205, 124),(214, 134),(229, 119),(220, 114),(220, 1),(220, -17),(199, -25),(197, -9),(168, -4),(168, 0),(195, -2),(206, 0),(206, 8),(206, 43),(52, 206),(74, 194),(67, 187),(67, 123),(87, 148),(91, 161),(105, 147),(99, 146),(90, 137),(81, 128),(67, 115),(67, 91),(64, 57),(87, 46),(103, 29),(103, 22),(103, 18),(99, 7),(92, 9),(87, 22),(82, 34),(63, 52),(56, 8),(12, -26),(11, -23),(41, 13),(53, 74),(53, 149),(33, 140),(34, 126),(33, 104),(25, 92),(13, 88),(10, 95),(10, 97),(10, 102),(14, 105),(19, 109),(28, 128),(29, 140)]
endPts = [3, 7, 49, 77, 89]
flags = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]

pen = TTGlyphPointPen(None)
beginflag = 0
for i, pt in enumerate(coordinates):
    if pen._isClosed():
        pen.beginPath()
    if flags[i] == 1:
        pen.addPoint(pt,segmentType="line")
    else:
        pen.addPoint(pt)
    if i in endPts:
        pen.endPath()
glyph = pen.glyph()

返回的glyph即為Glyph實(shí)例,可直接用于構(gòu)建基于TrueType輪廓的字體文件。注意,Glyph對(duì)象只包含字形輪廓數(shù)據(jù),屬于glyf表,對(duì)應(yīng)前一節(jié)提到的_TTGlyph中的_glyph屬性,_TTGlyph中的字寬和上下沿等數(shù)據(jù)則來(lái)自在字體文件的其他表格。


文章題目:字的研究(3)fontTools-TrueType輪廓坐標(biāo)的獲取以及基于TrueType的Glyph實(shí)例的構(gòu)建
地址分享:http://weahome.cn/article/dsojdcc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部