ReportLab:使用 Chinese/Unicode 个字符
ReportLab: working with Chinese/Unicode characters
长话短说:
是否有某种方法可以告诉 ReportLab 使用特定字体,并在某些字符的字形丢失时回退到另一种字体? 或者,您知道压缩 TrueType 字体吗其中包含所有欧洲语言、希伯来语、俄语、中文、日语和阿拉伯语的字形?
我一直在使用 ReportLab 创建报告,但在呈现包含中文字符的字符串时遇到了问题。我一直在使用的字体是 DejaVu Sans Condensed,它不包含中文的字形(但是,它确实包含西里尔文、希伯来文、阿拉伯文和各种欧洲语言支持的变音符号——这使得它非常通用,我需要他们不时)
然而,该字体不支持中文,我一直无法找到支持所有语言并满足我们的图形设计要求的 TrueType 字体。作为一个临时的解决方法,我让中国客户的报告使用完全不同的字体,只包含英文和中文字形,希望其他语言的字符不会出现在字符串中。然而,由于明显的原因,这很笨拙并且破坏了图形设计,因为它不是 DejaVu Sans,整个外观和感觉都是围绕它设计的。
所以问题是,您将如何处理在一个文档中支持多种语言的需要,并为每种语言维护指定字体的使用。由于有时字符串包含多种语言,这变得更加复杂,因此无法确定每个字符串应使用哪种字体。
是否有某种方式告诉 ReportLab 使用特定字体,并在缺少某些字符的字形时回退到另一种字体?我在文档中发现它应该是可能的模糊提示,尽管我可能理解不正确。
或者,您是否知道一种浓缩的 TrueType 字体,它包含所有欧洲语言、希伯来语、俄语、中文、日语和阿拉伯语的字形?
谢谢。
这个问题整个星期都让我着迷,所以因为是周末,我直接投入其中并准确地找到了一个我称之为 MultiFontParagraph
的解决方案,它是一个正常的 Paragraph
,但有一个很大的不同你可以准确设置字体回退顺序。
例如,我从互联网上提取的这个随机日语文本使用了以下字体后备 "Bauhaus", "Arial", "HanaMinA"
。它检查第一个字体是否有该字符的字形,如果有则使用它,如果没有则回退到下一个字体。目前代码并不是很有效,因为它在每个字符周围放置了标签,这很容易修复,但为了清楚起见,我没有在这里这样做。
我使用以下代码创建了上面的示例:
foreign_string = u'6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927'
P = MultiFontParagraph(foreign_string, styles["Normal"],
[ ("Bauhaus", "C:\Windows\Fonts\BAUHS93.TTF"),
("Arial", "C:\Windows\Fonts\arial.ttf"),
("HanaMinA", 'C:\Windows\Fonts\HanaMinA.ttf')])
MultiFontParagraph
(git)的来源如下:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Paragraph
class MultiFontParagraph(Paragraph):
# Created by B8Vrede for
def __init__(self, text, style, fonts_locations):
font_list = []
for font_name, font_location in fonts_locations:
# Load the font
font = TTFont(font_name, font_location)
# Get the char width of all known symbols
font_widths = font.face.charWidths
# Register the font to able it use
pdfmetrics.registerFont(font)
# Store the font and info in a list for lookup
font_list.append((font_name, font_widths))
# Set up the string to hold the new text
new_text = u''
# Loop through the string
for char in text:
# Loop through the fonts
for font_name, font_widths in font_list:
# Check whether this font know the width of the character
# If so it has a Glyph for it so use it
if ord(char) in font_widths:
# Set the working font for the current character
new_text += u'<font name="{}">{}</font>'.format(font_name, char)
break
Paragraph.__init__(self, new_text, style)
Google has been developing a font family called Noto, which aims to support all languages with a harmonious look and feel.
unified Noto Sans font 包含单一字体,支持来自以下领域的 581 种语言:
希伯来语、阿拉伯语和日语等其他语言在 Noto 网站上被列为单独的项目。
长话短说: 是否有某种方法可以告诉 ReportLab 使用特定字体,并在某些字符的字形丢失时回退到另一种字体? 或者,您知道压缩 TrueType 字体吗其中包含所有欧洲语言、希伯来语、俄语、中文、日语和阿拉伯语的字形?
我一直在使用 ReportLab 创建报告,但在呈现包含中文字符的字符串时遇到了问题。我一直在使用的字体是 DejaVu Sans Condensed,它不包含中文的字形(但是,它确实包含西里尔文、希伯来文、阿拉伯文和各种欧洲语言支持的变音符号——这使得它非常通用,我需要他们不时)
然而,该字体不支持中文,我一直无法找到支持所有语言并满足我们的图形设计要求的 TrueType 字体。作为一个临时的解决方法,我让中国客户的报告使用完全不同的字体,只包含英文和中文字形,希望其他语言的字符不会出现在字符串中。然而,由于明显的原因,这很笨拙并且破坏了图形设计,因为它不是 DejaVu Sans,整个外观和感觉都是围绕它设计的。
所以问题是,您将如何处理在一个文档中支持多种语言的需要,并为每种语言维护指定字体的使用。由于有时字符串包含多种语言,这变得更加复杂,因此无法确定每个字符串应使用哪种字体。
是否有某种方式告诉 ReportLab 使用特定字体,并在缺少某些字符的字形时回退到另一种字体?我在文档中发现它应该是可能的模糊提示,尽管我可能理解不正确。
或者,您是否知道一种浓缩的 TrueType 字体,它包含所有欧洲语言、希伯来语、俄语、中文、日语和阿拉伯语的字形?
谢谢。
这个问题整个星期都让我着迷,所以因为是周末,我直接投入其中并准确地找到了一个我称之为 MultiFontParagraph
的解决方案,它是一个正常的 Paragraph
,但有一个很大的不同你可以准确设置字体回退顺序。
例如,我从互联网上提取的这个随机日语文本使用了以下字体后备 "Bauhaus", "Arial", "HanaMinA"
。它检查第一个字体是否有该字符的字形,如果有则使用它,如果没有则回退到下一个字体。目前代码并不是很有效,因为它在每个字符周围放置了标签,这很容易修复,但为了清楚起见,我没有在这里这样做。
我使用以下代码创建了上面的示例:
foreign_string = u'6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927'
P = MultiFontParagraph(foreign_string, styles["Normal"],
[ ("Bauhaus", "C:\Windows\Fonts\BAUHS93.TTF"),
("Arial", "C:\Windows\Fonts\arial.ttf"),
("HanaMinA", 'C:\Windows\Fonts\HanaMinA.ttf')])
MultiFontParagraph
(git)的来源如下:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Paragraph
class MultiFontParagraph(Paragraph):
# Created by B8Vrede for
def __init__(self, text, style, fonts_locations):
font_list = []
for font_name, font_location in fonts_locations:
# Load the font
font = TTFont(font_name, font_location)
# Get the char width of all known symbols
font_widths = font.face.charWidths
# Register the font to able it use
pdfmetrics.registerFont(font)
# Store the font and info in a list for lookup
font_list.append((font_name, font_widths))
# Set up the string to hold the new text
new_text = u''
# Loop through the string
for char in text:
# Loop through the fonts
for font_name, font_widths in font_list:
# Check whether this font know the width of the character
# If so it has a Glyph for it so use it
if ord(char) in font_widths:
# Set the working font for the current character
new_text += u'<font name="{}">{}</font>'.format(font_name, char)
break
Paragraph.__init__(self, new_text, style)
Google has been developing a font family called Noto, which aims to support all languages with a harmonious look and feel.
unified Noto Sans font 包含单一字体,支持来自以下领域的 581 种语言:
希伯来语、阿拉伯语和日语等其他语言在 Noto 网站上被列为单独的项目。