项目场景
基于Python的Open3d项目开发的3d点云显示工具。
问题描述
基于 open3d.visualization.gui 的window中,需要在label和button上显示中文。
import open3d.visualization.gui as gui
if __name__ == "__main__":
gui.Application.instance.initialize()
''' 定义window框体 '''
gui.Application.instance.run()
}
定义3个按钮,描述为中文,但框架原本设定并不支持,会显示为问号’???'。
解决方案
使用gui.Application.instance.set_font
导入中文字体。
import open3d.visualization.gui as gui
if __name__ == "__main__":
gui.Application.instance.initialize()
''' 初始化后添加字体导入'''
serif = "c:/windows/fonts/times.ttf" # Times New Roman
hanzi = "c:/windows/fonts/msyh.ttc" # YaHei UI 如果非windows环境请自行修改路径
font = gui.FontDescription(serif)
font.add_typeface_for_language(hanzi, "zh")
gui.Application.instance.set_font(gui.Application.DEFAULT_FONT_ID, font)
'''定义window框体'''
gui.Application.instance.run()
该问题的详细情况在官方Issue #2600和#2655中可以查到,在#2655后支持中文显示。
解决方案参照官方实现examples/python/visualization/non_english.py ,重点看main()和前面的部分。
# ----------------------------------------------------------------------------
# - Open3D: www.open3d.org -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2023 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------
import open3d.visualization.gui as gui
import os.path
import platform
basedir = os.path.dirname(os.path.realpath(__file__))
# This is all-widgets.py with some modifications for non-English languages.
# Please see all-widgets.py for usage of the GUI widgets
MODE_SERIF = "serif"
MODE_COMMON_HANYU = "common"
MODE_SERIF_AND_COMMON_HANYU = "serif+common"
MODE_COMMON_HANYU_EN = "hanyu_en+common"
MODE_ALL_HANYU = "all"
MODE_CUSTOM_CHARS = "custom"
#mode = MODE_SERIF
#mode = MODE_COMMON_HANYU
mode = MODE_SERIF_AND_COMMON_HANYU
#mode = MODE_ALL_HANYU
#mode = MODE_CUSTOM_CHARS
# Fonts can be names or paths
if platform.system() == "Darwin":
serif = "Times New Roman"
hanzi = "STHeiti Light"
chess = "/System/Library/Fonts/Apple Symbols.ttf"
elif platform.system() == "Windows":
# it is necessary to specify paths on Windows since it stores its fonts
# with a cryptic name, so font name searches do not work on Windows
serif = "c:/windows/fonts/times.ttf" # Times New Roman
hanzi = "c:/windows/fonts/msyh.ttc" # YaHei UI
chess = "c:/windows/fonts/seguisym.ttf" # Segoe UI Symbol
else:
# Assumes Ubuntu 18.04
serif = "DejaVuSerif"
hanzi = "NotoSansCJK"
chess = "/usr/share/fonts/truetype/freefont/FreeSerif.ttf"
def main():
gui.Application.instance.initialize()
# Font changes must be done after initialization but before creating
# a window.
# MODE_SERIF changes the English font; Chinese will not be displayed
font = None
if mode == MODE_SERIF:
font = gui.FontDescription(serif)
# MODE_COMMON_HANYU uses the default English font and adds common Chinese
elif mode == MODE_COMMON_HANYU:
font = gui.FontDescription()
font.add_typeface_for_language(hanzi, "zh")
# MODE_SERIF_AND_COMMON_HANYU uses a serif English font and adds common
# Chinese characters
elif mode == MODE_SERIF_AND_COMMON_HANYU:
font = gui.FontDescription(serif)
font.add_typeface_for_language(hanzi, "zh")
# MODE_COMMON_HANYU_EN the Chinese font for both English and the common
# characters
elif mode == MODE_COMMON_HANYU_EN:
font = gui.FontDescription(hanzi)
font.add_typeface_for_language(hanzi, "zh")
# MODE_ALL_HANYU uses the default English font but includes all the Chinese
# characters (which uses a substantial amount of memory)
elif mode == MODE_ALL_HANYU:
font = gui.FontDescription()
font.add_typeface_for_language(hanzi, "zh_all")
elif mode == MODE_CUSTOM_CHARS:
range = [0x2654, 0x2655, 0x2656, 0x2657, 0x2658, 0x2659]
font = gui.FontDescription()
font.add_typeface_for_code_points(chess, range)
if font is not None:
gui.Application.instance.set_font(gui.Application.DEFAULT_FONT_ID, font)
w = ExampleWindow()
gui