FoundationPartsSketchesText & text on a path

Text & text on a path

Text(sketch, text, size) turns a string into closed outlines that you extrude like any other shape. Holes in letters (“o”, “e”, “A”) are kept. size is the font size, so capitals are about 0.7 × size tall, and xshift / yshift move the baseline start.

Text uses the kernel’s default font (Roboto Regular) unless you pass font_url= a TTF/OTF/WOFF URL or a data: URI.

TextOnPath(path, text, size) sets text along a curve. Each letter sits on the path with its baseline tangent to it, reading in the path’s direction of travel. The path can be a Line, an Arc, a Circle or a custom open/closed shape.

  • start is the distance along the path where the text is anchored. align=TextPathAlign.START | CENTER | END says whether the text begins, is centred or ends there.
  • baseline_offset lifts the letters off the path, towards the left of travel.
  • A Circle runs clockwise from its leftmost point. To centre text over the top, use start = π·r/2 (a quarter of the way round) with align=CENTER.
  • For text that reads upright along the bottom of a badge, draw an Arc from left to right underneath, as above.
import math

from cadbuildr.foundation import *


class Badge(Part):
    """TextOnPath: lettering raised 1 mm around a 50 mm badge. The top line
    follows a Circle (which runs clockwise from its leftmost point, so
    centring the text a quarter-turn in puts it over the top); the bottom line
    follows an Arc drawn left to right, so it reads upright too."""

    def __init__(self, radius=25, thickness=3):
        s = Sketch(self.xy())
        disc = Extrusion(Circle(s.origin, radius), thickness)
        self.add_operation(disc)

        face = Sketch(self.xy().get_parallel_plane(thickness))
        top_ring = Circle(face.origin, radius - 7)
        top = TextOnPath(
            top_ring,
            "CADBUILDR",
            6,
            start=math.pi * (radius - 7) / 2,  # a quarter of the way round: the top
            align=TextPathAlign.CENTER,
        )
        self.add_operation(Extrusion(top, 1))

        r = radius - 4
        a0, a1 = math.radians(220), math.radians(320)
        bottom_arc = Arc(
            Point(face, r * math.cos(a0), r * math.sin(a0)),
            Point(face, 0, -r),
            Point(face, r * math.cos(a1), r * math.sin(a1)),
        )
        bottom = TextOnPath(
            bottom_arc, "FOUNDATION", 4, start=r * (a1 - a0) / 2, align=TextPathAlign.CENTER
        )
        self.add_operation(Extrusion(bottom, 1))

        hello = Text(face, "Hello", 7, xshift=-10, yshift=-2.5)
        self.add_operation(Extrusion(hello, 1))
        self.paint("light_blue")


show(Badge())