Function Repository Resource:

 StrokeText3D

Source Notebook

Fit lightweight line-stroke text to a planar polygon in three dimensions

Contributed by: Ed Pegg Jr

ResourceFunction["StrokeText3D"][text, face]

returns line-stroke text centered and fitted inside a convex planar face.

ResourceFunction["StrokeText3D"][text, Polygon[vertices]]

accepts the face as a Polygon list of vertices.

ResourceFunction["StrokeText3D"][text, face, fit]

scales the largest fitted text polygon by fit; the default is 4/5.

Details

The result is a Line primitive containing multiple polylines, suitable for Graphics3D.
The built-in glyphs cover digits 0-9, letters A-Z, space, hyphen and question mark. Lowercase letters are converted to uppercase; unsupported characters are displayed as question marks.
Text is converted with ToString. Integers can be passed directly. Decimal points, plus signs, Greek letters and general mathematical typesetting are not supported by this glyph set.

Examples

Basic Examples (2) 

Number the faces of a cube:

In[1]:=
Graphics3D[
 MapIndexed[{#1, ResourceFunction["StrokeText3D"][First[#2], #1]} &, MeshPrimitives[Cuboid[], 2]]]
Out[1]=

Color each cube face and its label independently:

In[2]:=
Graphics3D[MapIndexed[
   {Opacity[.8], Hue[First[#2]/6], #1, Black, AbsoluteThickness[2],
       ResourceFunction["StrokeText3D"][First[#2], #1]} &, MeshPrimitives[Cuboid[], 2]], Boxed -> False]
Out[2]=

Scope (5) 

Letter the eight faces of an octahedron:

In[3]:=
Graphics3D[
 MapIndexed[{#1, ResourceFunction["StrokeText3D"][
     FromCharacterCode[64 + First[#2]], #1]} &, MeshPrimitives[Octahedron[], 2]]]
Out[3]=

Fit a five-digit label on every cube face:

In[4]:=
Graphics3D[({#, ResourceFunction["StrokeText3D"]["99999", #]} &) /@ MeshPrimitives[Cuboid[], 2]]
Out[4]=

Label a polygon:

In[5]:=
With[{face = {{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}}, Graphics3D[{Polygon[face], ResourceFunction["StrokeText3D"]["HELLO 123", face]}, PlotRange -> {All, All, {-1, 1}}]]
Out[5]=

Long strings also fit within triangular faces:

In[6]:=
Graphics3D[({#, ResourceFunction["StrokeText3D"]["ABCDE", #]} &) /@ MeshPrimitives[Octahedron[], 2]]
Out[6]=

Display the supported letters and digits:

In[7]:=
Grid[Table[
  With[{face = Polygon[{{0, 0, 0}, {8, 0, 0}, {8, 1, 0}, {0, 1, 0}}]},
    {Graphics3D[ResourceFunction["StrokeText3D"][text, face], ViewPoint -> {0, 0, Infinity},
        Boxed -> False, ImageSize -> 600]}],
   {text, {"ABCDEFGHIJKLM", "NOPQRSTUVWXYZ", "0123456789 -?"}}]]
Out[7]=

Options (2) 

Compare three fit values. Each panel uses the same face and text:

In[8]:=
With[{face = Polygon[{{0, 0, 0}, {3, 0, 0}, {3, 1, 0}, {0, 1, 0}}]},
  Grid[{Table[
    Graphics3D[{face, ResourceFunction["StrokeText3D"]["123", face, fit]},
         ViewPoint -> {0, 0, Infinity}, Boxed -> False,
         PlotLabel -> fit, ImageSize -> 220], {fit, {2/5, 4/5, .95}}]}]]
Out[8]=

Use a dashed stroke style:

In[9]:=
With[{face = Polygon[{{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}]},
  Graphics3D[{Blue, AbsoluteThickness[2], Dashing[{1/50, 1/100}],
      ResourceFunction["StrokeText3D"]["DASH", face]}, Boxed -> False]]
Out[9]=

Applications (4) 

Use custom labels rather than face indices:

In[10]:=
With[{faces = MeshPrimitives[Cuboid[], 2]},
  Graphics3D[
  MapThread[{#1, ResourceFunction["StrokeText3D"][#2, #1]} &,
      {faces, {"A1", "A2", "B1", "B2", "C1", "C2"}}]]]
Out[10]=

Number a larger collection of faces:

In[11]:=
Graphics3D[
 MapIndexed[{#1, ResourceFunction["StrokeText3D"][First[#2], #1]} &,
   MeshPrimitives[PolyhedronData["Dodecahedron", "BoundaryMeshRegion"], 2]]]
Out[11]=

For interactive styling, construct the lettering in Initialization. Changing the control only changes its color:

In[12]:=
Manipulate[
  Graphics3D[{color, lettering}, Boxed -> False],
  {color, {Black, Red, Blue}}, {{lettering, {}}, ControlType -> None},
  Initialization :> (lettering = ResourceFunction["StrokeText3D"]["READY", Polygon[{{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}]]),
  SynchronousInitialization -> True, ContinuousAction -> False,
  TrackedSymbols :> {color}, SaveDefinitions -> True]
Out[12]=

Choose a different baseline by cycling the face vertices:

In[13]:=
With[{vertices = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}}},
  Grid[{Table[With[{face = Polygon[RotateLeft[vertices, shift]]}, Graphics3D[{face, ResourceFunction["StrokeText3D"]["UP", face]}, ViewPoint -> {0, 0, Infinity},
           Boxed -> False, ImageSize -> 180]], {shift, 0, 3}]}]]
Out[13]=

Properties and Relations (2) 

The time to render characters in 3D was a main driver for making this function:

In[14]:=
With[{faces = MeshPrimitives[Cuboid[], 2]}, With[{results = {AbsoluteTiming[
      Graphics3D[
       MapIndexed[{#1, ResourceFunction["StrokeText3D"][First[#2], #1]} &, faces], ImageSize -> 200]], AbsoluteTiming[
      Graphics3D[
       MapIndexed[
        Function[{face, index}, Module[{vertices = First[face], curves, bounds, right, up}, curves = ResourceFunction["CharacterCurves"][
            Style[ToString[First[index]], FontFamily -> "Arial"], "Segments"]; bounds = MinMax /@ Transpose[
             Cases[curves, {_?NumericQ, _?NumericQ}, Infinity]];
          right = vertices[[2]] - vertices[[1]];
          up = vertices[[-1]] - vertices[[1]];
          {face, curves /. {xx_?NumericQ, yy_?NumericQ} :> Mean[vertices] + Cross[right, up]/1000 + 4/5 (({xx, yy} - (Mean /@ bounds)) . {right, up})/
                Max[Subtract @@@ Reverse[bounds, 2]]}]], faces], ImageSize -> 200]], AbsoluteTiming[
      Graphics3D[
       MapIndexed[
        Function[{face, index}, Module[{vertices = First[face], curves, bounds, right, up}, curves = ResourceFunction["TextToLines3D"][ToString[First[index]], FontFamily -> "Arial", "LineColor" -> Black]; bounds = (MinMax /@ Transpose[
               Cases[curves, {_?NumericQ, _?NumericQ, _?NumericQ}, Infinity]])[[{1, 3}]];
          right = vertices[[2]] - vertices[[1]];
          up = vertices[[-1]] - vertices[[1]];
          {face, curves /. {xx_?NumericQ, yy_?NumericQ, zz_?NumericQ} :> Mean[vertices] + Cross[right, up]/1000 + 4/5 (({xx, zz} - (Mean /@ bounds)) . {right, up})/
                Max[Subtract @@@ Reverse[bounds, 2]]}]], faces], ImageSize -> 200]]}}, Grid[{{"StrokeText3D", "CharacterCurves", "TextToLines3D"}, Row[{"Timing: ", First[#], " seconds"}] & /@ results, Last /@ results}]]]
Out[14]=

Whereas ResourceFunction "CharacterCurves" extracts from an installed font, StrokeText3D uses simple fixed strokes, such as with the letter R:

In[15]:=
 face = Polygon[{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}}];
 stroke = ResourceFunction["StrokeText3D"]["R", face] /. {xx_?NumericQ, yy_?NumericQ, zz_?NumericQ} :> {xx, yy};
 outline = Flatten@ResourceFunction["CharacterCurves"][
    Style["R", FontFamily -> "Arial"], "Segments"];
Grid[{{"StrokeText3D", "CharacterCurves"},
     {Graphics[{AbsoluteThickness[2], stroke}, ImageSize -> 100],
       Graphics[{AbsoluteThickness[2], outline}, ImageSize -> 100]}}]
Out[18]=

Inspect the primitive types returned by each method:

In[19]:=
Grid[Prepend[MapThread[{#1,
          Count[#2, _Line, Infinity], Count[#2, _BezierCurve, Infinity]} &,
       {{"StrokeText3D", "CharacterCurves"}, {{stroke}, outline}}],
     {"Method", "Line primitives", "BezierCurve primitives"}], Frame -> All]
Out[19]=

Possible Issues (4) 

Text on a face with zero depth can appear to float:

In[20]:=
With[{face = {{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}}, Row[{
   Graphics3D[{Polygon[face], ResourceFunction["StrokeText3D"]["HELLO 123", face], Polygon[Reverse /@ face]}],
   Graphics3D[{Polygon[face], ResourceFunction["StrokeText3D"]["HELLO 123", face]}],
   Graphics3D[{Polygon[face], ResourceFunction["StrokeText3D"]["HELLO 123", face]}, PlotRange -> {All, All, {-1, 1}}]}]]
Out[20]=

Unsupported punctuation becomes a question mark:

In[21]:=
With[{face = Polygon[{{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}]},
  Graphics3D[ResourceFunction["StrokeText3D"]["3.14", face], Boxed -> False]]
Out[21]=

Concave, self-intersecting, nonplanar and degenerate faces are outside the supported domain:

In[22]:=
With[{layers = {Reverse[{{0, 2, 0}, {2, 2, 0}, {2, 0, 0}, {3, 0, 0}, {3, 3, 0}, {0, 3, 0}}], {{1, 2, 1}, {2, 2, 1}, {2, 1, 1}, {3, 1, 1}, {3, 3, 1}, {1, 3, 1}}, Reverse[{{0, 0, 2}, {2, 0, 2}, {2, 1, 2}, {1, 1, 2}, {1, 2, 2}, {0, 2, 2}}], {{0, 0, 3}, {3, 0, 3}, {3, 1, 3}, {1, 1, 3}, {1, 3, 3}, {0, 3, 3}}}}, With[{faces = Polygon /@ Join[layers, Map[RotateLeft, layers, {2}], Map[RotateRight, layers, {2}]]}, Graphics3D[
   MapIndexed[{#1, ResourceFunction["StrokeText3D"][First[#2], #1]} &,
     faces], Boxed -> False, ImageSize -> 450]]]
Out[22]=

Vertex winding determines the front side, allowing mirror forms for numbers like 4 and 6:

In[23]:=
Graphics3D[
 MapIndexed[{#1, ResourceFunction["StrokeText3D"][First[#2], Reverse[First[#1]]]} &,
   MeshPrimitives[Cuboid[], 2]]]
Out[23]=

Neat Examples (3) 

Replace each polyline by a tube to make solid lettering:

In[24]:=
With[{text = ResourceFunction["StrokeText3D"]["TUBE", Polygon[{{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}], 3/5]},
  Graphics3D[{Orange, Specularity[White, 30],
      text /. Line[paths_] :> (Tube[#, 1/50] & /@ paths)}, Boxed -> False]]
Out[24]=

Draw two stroke widths to give the text a contrasting outline:

In[25]:=
With[{text = ResourceFunction["StrokeText3D"]["GLOW", Polygon[{{0, 0, 0}, {4, 0, 0}, {4, 1, 0}, {0, 1, 0}}]]},
  Graphics3D[{{Blue, AbsoluteThickness[8], text},
      {Cyan, AbsoluteThickness[2], text}}, Background -> Black,
    ViewPoint -> {0, 0, Infinity}, Boxed -> False]]
Out[25]=

Make a labeled empire map, where each of twelve empires has two regions and every empire neighbor all the others:

In[26]:=
Module[{phi = GoldenRatio, bigface, smallface, rotations, pairs}, bigface = {{1, 1, 1}, {phi/2, phi^2/2, 1/2}, {-phi/2, phi^2/2, 1/2}, {-1, 1, 1}, {0, 1/phi, phi}};
 smallface = {{phi/2, phi^2/2, 1/2}, {1/phi, phi, 0}, {-1/phi, phi, 0}, {-phi/2, phi^2/2, 1/2}};
 rotations = Flatten[NestList[RotateLeft /@ # &, DiagonalMatrix /@ {{1, 1, 1}, {-1, 1, -1}, {1, -1, -1}, {-1, -1, 1}}, 2], 1][[{1, 2, 10, 12, 6, 7, 3, 4, 11, 9, 5, 8}]];
 pairs = Transpose[{(Polygon[N[bigface . #]] & /@ rotations), (Polygon[N[smallface . #]] & /@ rotations[[{7, 8, 9, 10, 12, 11, 2, 1, 4, 3, 5, 6}]])}];
 Graphics3D[
  MapIndexed[
   Function[{faces, index}, {{Opacity[4/5], ColorData["BrightBands"][First[index]/12],
       faces}, {Opacity[1], Black, (ResourceFunction["StrokeText3D"][First[index], #] /. Line[paths_] :> (Tube[#, 1/50] & /@ paths)) & /@ faces}}], pairs], Boxed -> False, ImageSize -> 500]]
Out[26]=

Publisher

Ed Pegg

Version History

  • 1.0.0 – 28 October 2026

Related Resources

Author Notes

Intended for compact geometric annotations, not typographic fidelity. Convex planar faces and ordered vertices are required. The font is fixed; lower case maps to upper case. Polygonal fitting is performed when the function is called, so cache the result for repeated display.

License Information