Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font path #453

Merged
merged 20 commits into from
Oct 14, 2020
Merged
2 changes: 2 additions & 0 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3536,6 +3536,7 @@ def text(
combine: bool = False,
clean: bool = True,
font: str = "Arial",
fontPath: Optional[str] = None,
kind: Literal["regular", "bold", "italic"] = "regular",
halign: Literal["center", "left", "right"] = "center",
valign: Literal["center", "top", "bottom"] = "center",
Expand Down Expand Up @@ -3571,6 +3572,7 @@ def text(
fontsize,
distance,
font=font,
fontPath=fontPath,
kind=kind,
halign=halign,
valign=valign,
Expand Down
11 changes: 10 additions & 1 deletion cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
Font_FA_Regular,
Font_FA_Italic,
Font_FA_Bold,
Font_SystemFont,
)

from OCP.BRepFeat import BRepFeat_MakeDPrism
Expand Down Expand Up @@ -2372,6 +2373,7 @@ def makeText(
size: float,
height: float,
font: str = "Arial",
fontPath: Optional[str] = None,
kind: Literal["regular", "bold", "italic"] = "regular",
halign: Literal["center", "left", "right"] = "center",
valign: Literal["center", "top", "bottom"] = "center",
Expand All @@ -2388,7 +2390,14 @@ def makeText(
}[kind]

mgr = Font_FontMgr.GetInstance_s()
font_t = mgr.FindFont(TCollection_AsciiString(font), font_kind)

if fontPath and mgr.CheckFont(TCollection_AsciiString(fontPath).ToCString()):
font_t = Font_SystemFont(TCollection_AsciiString(fontPath))
font_t.SetFontPath(font_kind, TCollection_AsciiString(fontPath))
mgr.RegisterFont(font_t, True)

else:
font_t = mgr.FindFont(TCollection_AsciiString(font), font_kind)

builder = Font_BRepTextBuilder()
text_flat = Shape(
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
makeCube,
)

# test data directory
testdataDir = os.path.join(os.path.dirname(__file__), "testdata")
jmwright marked this conversation as resolved.
Show resolved Hide resolved

# where unit test output will be saved
OUTDIR = tempfile.gettempdir()
SUMMARY_FILE = os.path.join(OUTDIR, "testSummary.html")
Expand Down Expand Up @@ -2699,6 +2702,7 @@ def testClose(self):
self.assertAlmostEqual(obj1.val().Volume(), obj2.val().Volume())

def testText(self):
global testdataDir

box = Workplane("XY").box(4, 4, 0.5)

Expand Down Expand Up @@ -2749,6 +2753,45 @@ def testText(self):
# verify that the number of solids is correct
self.assertEqual(len(obj3.solids().vals()), 5)

obj4 = (
box.faces(">Z")
.workplane()
.text(
"CQ 2.0",
0.5,
0.05,
fontPath=os.path.join(testdataDir, "OpenSans-Regular.ttf"),
cut=False,
combine=False,
halign="right",
valign="top",
font="Sans",
)
)

# verify that the number of solids is correct
self.assertEqual(len(obj4.solids().vals()), 5)

# test to see if non-existent file causes segfault
obj5 = (
box.faces(">Z")
.workplane()
.text(
"CQ 2.0",
0.5,
0.05,
fontPath=os.path.join(testdataDir, "OpenSans-Irregular.ttf"),
cut=False,
combine=False,
halign="right",
valign="top",
font="Sans",
)
)

# verify that the number of solids is correct
self.assertEqual(len(obj5.solids().vals()), 5)

def testParametricCurve(self):

from math import sin, cos, pi
Expand Down
Binary file added tests/testdata/OpenSans-Regular.ttf
Binary file not shown.