gpt4 book ai didi

font.getGlyphID gives KeyError on unicode(在Unicode上,Font.getGlyphID提供KeyError)

转载 作者:bug小助手 更新时间:2023-10-28 21:05:45 24 4
gpt4 key购买 nike



I'm trying to render SVG out of an unicode using Python's fonttools:

我正在尝试使用Python的字体工具从Unicode中呈现SVG:


from fontTools.ttLib import TTFont
from fontTools.pens.svgPathPen import SVGPathPen

def unicode_to_svg(unicode_char, font_path, output_svg_path):
# Load the TTF font using fonttools
font = TTFont(font_path)

# Get the glyph index for the Unicode character
glyph_index = font.getGlyphID(unicode_char)

# Convert the glyph to an SVG path
glyph_set = font.getGlyphSet()
glyph_name = font.getGlyphName(glyph_index)
glyph = glyph_set[glyph_name]

pen = SVGPathPen(glyph_set)
glyph.draw(pen)
svg_path_data = pen.getCommands()

# Create the SVG content
svg_content = f'''
<svg width="3000" height="3000" xmlns="http://www.w3.org/2000/svg">
<path d="{svg_path_data}" fill="red" />
</svg>
'''

# Save the SVG content to a file
with open(output_svg_path, 'w') as f:
f.write(svg_content)

# Example usage
font_path = './Nikosh.ttf'
# unicode_char = 'A'
unicode_char = 'ম'
output_svg_path = 'output.svg'
unicode_to_svg(unicode_char, font_path, output_svg_path)

But the issue is, it works fine for English alphabets. But when I try to render unicode (such as Bengali) it gives me:

但问题是,它对英文字母很有效。但当我尝试呈现Unicode(如孟加拉语)时,它会给我带来:


KeyError: 'ম'

On this line: glyph_index = font.getGlyphID(unicode_char)

在该行:glyph_index=font.getGlyphID(Unicode_Char)


I simply wanted to take a deeper dive into SVG rendering of different languages, fonts, texts, etc.

我只是想更深入地研究不同语言、字体、文本等的SVG呈现。


How can I resolve this issue? And make it SVG render-able for any unicode character?

我如何解决此问题?并使其能够为任何Unicode字符呈现SVG?


更多回答

I'd be inclined to use hb-view from the terminal, its a a tiliity included with the harfuzz library, you specify what font to use , and the string you want to render. SVG is supported as an output format. It can render individual characters or strings, correctly rendering text, and allows you to control opentype features used in the rendering. Exporting an svg using fonttools is useful for individual glyphs, but not for rendered text.

我倾向于从终端使用HB-view,它是harfuzz库中包含的一个实用程序,您可以指定要使用的字体以及要呈现的字符串。支持将SVG作为输出格式。它可以呈现单个字符或字符串,正确呈现文本,并允许您控制呈现中使用的OpenType功能。使用字体工具导出SVG对于单个字形很有用,但对呈现的文本没有帮助。

优秀答案推荐

help(font.getGlyphID)


Help on method getGlyphID in module fontTools.ttLib.ttFont:


getGlyphID(glyphName) method of
fontTools.ttLib.ttFont.TTFont instance.

Returns the ID of the glyph with the given name.



The function char_in_font comes from my another project, included here unmodified… Check its output before passing to getGlyphID.

函数char_in_FONT来自我的另一个项目,此处包含未经修改的…在传递给getGlyphID之前检查其输出。


from fontTools.ttLib import TTFont
from fontTools.pens.svgPathPen import SVGPathPen

def char_in_font(unicode_char, font):
'''check if a char is in font, return its glyphName'''
for cmap in font['cmap'].tables:
if cmap.isUnicode() or cmap.getEncoding() == 'utf_16_be':
if ord(unicode_char) in cmap.cmap:
# print(type(cmap))
auxcn = cmap.cmap[ord(unicode_char)]
# print(auxcn, type(auxcn))
return auxcn if auxcn != '' else '<nil>'
return ''


def unicode_to_svg(unicode_char, font_path, output_svg_path):
# Load the TTF font using fonttools
font = TTFont(font_path)

# Get the glyph index for the Unicode character
glyph_index = font.getGlyphID(char_in_font(unicode_char, font))

# Convert the glyph to an SVG path
glyph_set = font.getGlyphSet()
glyph_name = font.getGlyphName(glyph_index)
glyph = glyph_set[glyph_name]

pen = SVGPathPen(glyph_set)
glyph.draw(pen)
svg_path_data = pen.getCommands()

# Create the SVG content
svg_content = f'''
<svg width="3000" height="3000" xmlns="http://www.w3.org/2000/svg">
<path d="{svg_path_data}" fill="red" />
</svg>
'''

# Save the SVG content to a file
with open(output_svg_path, 'w') as f:
f.write(svg_content)


# Example usage
font_path = r'D:\Downloads\Fonty\Nikosh\Nikosh.ttf'
# unicode_char = 'A'
unicode_char = 'ম'
output_svg_path = f'output{unicode_char}.svg'
unicode_to_svg(unicode_char, font_path, output_svg_path)

Output:

产出:


.\SO\77072040.py
start "" outputম.svg

enter image description here


dir output*.svg


09/09/2023  15:55               247 outputA.svg
09/09/2023 16:18 751 outputম.svg


start "" outputA.svg

enter image description here


更多回答

thanks... really appreciate it... I just have this question, how can I render any charater?? Right now I'm trying to render CN (chinese alphabets) but it's giving me error... any generic methodology??

谢谢..。我真的很感激。我只是有一个问题,我如何才能表现出任何角色?现在我正在尝试呈现CN(中文字母表),但它给了我错误...有什么通用的方法吗??

Additionally, when multiple glyphs are available for a specific Unicode codepoint, how does the code determine which to use? Or for that matter one-to-many cmap entries as well.

此外,当特定Unicode代码点有多个字形可用时,代码如何确定使用哪一个?或者就此而言也是一对多Cmap条目。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com