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

Improve text alignment #1455

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@
)

from OCP.StdPrs import StdPrs_BRepFont, StdPrs_BRepTextBuilder as Font_BRepTextBuilder
from OCP.Graphic3d import (
Graphic3d_HTA_LEFT,
Graphic3d_HTA_CENTER,
Graphic3d_HTA_RIGHT,
Graphic3d_VTA_BOTTOM,
Graphic3d_VTA_CENTER,
Graphic3d_VTA_TOP,
)

from OCP.NCollection import NCollection_Utf8String

Expand Down Expand Up @@ -3649,23 +3657,28 @@ def makeText(
font_kind,
float(size),
)
text_flat = Shape(builder.Perform(font_i, NCollection_Utf8String(text)))

bb = text_flat.BoundingBox()

t = Vector()

if halign == "center":
t.x = -bb.xlen / 2
elif halign == "right":
t.x = -bb.xlen

if valign == "center":
t.y = -bb.ylen / 2
elif valign == "top":
t.y = -bb.ylen

text_flat = text_flat.translate(t)
if halign == "left":
theHAlign = Graphic3d_HTA_LEFT
elif halign == "center":
theHAlign = Graphic3d_HTA_CENTER
else: # halign == "right"
theHAlign = Graphic3d_HTA_RIGHT

if valign == "bottom":
theVAlign = Graphic3d_VTA_BOTTOM
elif valign == "center":
theVAlign = Graphic3d_VTA_CENTER
else: # valign == "top":
theVAlign = Graphic3d_VTA_TOP

text_flat = Shape(
builder.Perform(
font_i,
NCollection_Utf8String(text),
theHAlign=theHAlign,
theVAlign=theVAlign,
)
)

if height != 0:
vecNormal = text_flat.Faces()[0].normalAt() * height
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,22 @@ def testText(self):
)
)

def testTextAlignment(self):
left_bottom = Workplane().text("I", 10, 0, halign="left", valign="bottom")
lb_bb = left_bottom.val().BoundingBox()
self.assertGreaterEqual(lb_bb.xmin, 0)
self.assertGreaterEqual(lb_bb.ymin, 0)

centers = Workplane().text("I", 10, 0, halign="center", valign="center")
c_bb = centers.val().BoundingBox()
self.assertAlmostEqual(c_bb.center.x, 0, places=0)
self.assertAlmostEqual(c_bb.center.y, 0, places=0)

right_top = Workplane().text("I", 10, 0, halign="right", valign="top")
rt_bb = right_top.val().BoundingBox()
self.assertLessEqual(rt_bb.xmax, 0)
self.assertLessEqual(rt_bb.ymax, 0)

def testParametricCurve(self):

from math import sin, cos, pi
Expand Down
2 changes: 1 addition & 1 deletion tests/test_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ def test_dxf_text(tmpdir, testdatadir):
s2 = Sketch().importDXF(fname)
w2 = Workplane("XZ", origin=(0, -0.5, 0)).placeSketch(s2).extrude(-1)

assert w1.val().Volume() == approx(59.983287, 1e-2)
assert w1.val().Volume() == approx(61.669465, 1e-2)
assert w2.val().Volume() == approx(w1.val().Volume(), 1e-2)
assert w2.intersect(w1).val().Volume() == approx(w1.val().Volume(), 1e-2)

Expand Down