Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #169 from ZacZhangzhuo/text-font-option
Browse files Browse the repository at this point in the history
Text font option
  • Loading branch information
Licini authored Oct 5, 2023
2 parents 7e49972 + d455112 commit 1821a32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions scripts/v120_fonts.py
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 9 additions & 1 deletion src/compas_view2/objects/textobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/compas_view2/shapes/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1821a32

Please sign in to comment.