diff --git a/CHANGELOG.md b/CHANGELOG.md index fcd8c5b4d..42007d0d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Added - - Multi-cursor visual effects. + +* Added `font` option to `TextObject`. +* Multi-cursor visual effects. + ### Changed ### Removed diff --git a/scripts/v120_fonts.py b/scripts/v120_fonts.py new file mode 100644 index 000000000..cfa546f51 --- /dev/null +++ b/scripts/v120_fonts.py @@ -0,0 +1,18 @@ +from compas_view2.shapes import Text +from compas_view2.app import App + +viewer = App() + +# By default, the text is rendered using the FreeSans font from the library. +t = Text("EN", [0, 0, 0], height=50) +viewer.add(t) + +# Font specified is possible. +t = Text("EN", [3, 0, 0], height=50, font = "Times New Roman") +viewer.add(t) + +# Multi-language text is possible if the machine has the font installed. +t = Text("中文 CN", [3, 3, 0], height=50, font = "DengXian") +viewer.add(t) + +viewer.show() diff --git a/src/compas_view2/objects/textobject.py b/src/compas_view2/objects/textobject.py index 7f987ab46..ae63fb27b 100644 --- a/src/compas_view2/objects/textobject.py +++ b/src/compas_view2/objects/textobject.py @@ -7,6 +7,7 @@ from compas_view2.gl import make_vertex_buffer from .object import Object +from matplotlib import font_manager here = os.path.dirname(__file__) fonts = os.path.join(here, "..", "fonts") @@ -35,7 +36,14 @@ def make_buffers(self): def make_text_texture(self): # change the filename if necessary - face = ft.Face(os.path.join(fonts, "FreeSans.ttf")) + + font_dir = os.path.join(fonts, "FreeSans.ttf") + if self._data.font: + for font in font_manager.fontManager.ttflist: + if font.name == self._data.font: + font_dir = font.fname + break + face = ft.Face(font_dir) char_width = 48 char_height = 80 diff --git a/src/compas_view2/shapes/text.py b/src/compas_view2/shapes/text.py index c52746b60..02dfd6ea7 100644 --- a/src/compas_view2/shapes/text.py +++ b/src/compas_view2/shapes/text.py @@ -5,8 +5,9 @@ class Text(Shape): """ """ - def __init__(self, text, position=[0, 0, 0], height=50): + def __init__(self, text, position=[0, 0, 0], height=50, font=None): super().__init__() self.text = text self.position = Vector(*position) self.height = height + self.font = font