atext() to text3d(). Overrode text() for 2D attachability.

This commit is contained in:
Garth Minette 2022-01-18 21:29:23 -08:00
parent e02795a09b
commit 7ca44e81c4
4 changed files with 116 additions and 21 deletions

View file

@ -12,13 +12,15 @@ module _square(size,center=false) square(size,center=center);
module _circle(r,d) circle(r=r,d=d);
module _text(t,size,font,halign,valign,spacing,direction,language,script)
text(t, size=size, font=font,
module _text(text,size,font,halign,valign,spacing,direction,language,script)
text(text, size=size, font=font,
halign=halign, valign=valign,
spacing=spacing, direction=direction,
language=language, script=script
);
module _color(color) if (color==undef) children(); else color(color) children();
module _cube(size,center) cube(size,center=center);
module _cylinder(h,r1,r2,center,r,d,d1,d2) cylinder(h,r=r,d=d,r1=r1,r2=r2,d1=d1,d2=d2,center=center);

View file

@ -9,6 +9,9 @@
//////////////////////////////////////////////////////////////////////
use <builtins.scad>
// Section: Coloring Objects
// Module: rainbow()
@ -116,3 +119,5 @@ function HSV(h,s=1,v=1) =
module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -1204,7 +1204,7 @@ function teardrop2d(r, ang=45, cap_h, d, anchor=CENTER, spin=0) =
// "right" = center of the right circle
// Example(2D,NoAxes): This first example shows how the egg is constructed from two circles and two joining arcs.
// $fn=100;
// color("red")stroke(egg(78,25,12, 60),closed=true);
// color("red") stroke(egg(78,25,12, 60),closed=true);
// stroke([left(14,circle(25)),
// right(27,circle(12))]);
// Example(2D,Anim,VPD=250,VPR=[0,0,0]): Varying length between circles
@ -1481,6 +1481,92 @@ function reuleaux_polygon(N=3, r, d, anchor=CENTER, spin=0) =
// Section: Text
// Module: text()
// Topics: Attachments, Text
// Usage:
// text(text, [size], [font]);
// Description:
// Creates a 3D text block that can be attached to other attachable objects.
// NOTE: This cannot have children attached to it.
// Arguments:
// text = The text string to instantiate as an object.
// size = The font size used to create the text block. Default: 10
// font = The name of the font used to create the text block. Default: "Helvetica"
// ---
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `"baseline"`
// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
// See Also: attachable()
// Extra Anchors:
// "baseline" = Anchors at the baseline of the text, at the start of the string.
// str("baseline",VECTOR) = Anchors at the baseline of the text, modified by the X and Z components of the appended vector.
// Examples(2D):
// text("Foobar", size=10);
// text("Foobar", size=12, font="Helvetica");
// text("Foobar", anchor=CENTER);
// text("Foobar", anchor=str("baseline",CENTER));
// Example: Using line_of() distributor
// txt = "This is the string.";
// line_of(spacing=[10,-5],n=len(txt))
// text(txt[$idx], size=10, anchor=CENTER);
// Example: Using arc_of() distributor
// txt = "This is the string";
// arc_of(r=50, n=len(txt), sa=0, ea=180)
// text(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module text(text, size=10, font="Helvetica", halign, valign, spacing=1.0, direction="ltr", language="en", script="latin", anchor="baseline", spin=0) {
no_children($children);
dummy1 =
assert(is_undef(anchor) || is_vector(anchor) || is_string(anchor), str("Got: ",anchor))
assert(is_undef(spin) || is_vector(spin,3) || is_num(spin), str("Got: ",spin));
anchor = default(anchor, CENTER);
spin = default(spin, 0);
geom = _attach_geom(size=[size,size],two_d=true);
anch = !any([for (c=anchor) c=="["])? anchor :
let(
parts = str_split(str_split(str_split(anchor,"]")[0],"[")[1],","),
vec = [for (p=parts) parse_float(str_strip(p," ",start=true))]
) vec;
ha = halign!=undef? halign :
anchor=="baseline"? "left" :
anchor==anch && is_string(anchor)? "center" :
anch.x<0? "left" :
anch.x>0? "right" :
"center";
va = valign != undef? valign :
starts_with(anchor,"baseline")? "baseline" :
anchor==anch && is_string(anchor)? "center" :
anch.y<0? "bottom" :
anch.y>0? "top" :
"center";
base = anchor=="baseline"? CENTER :
anchor==anch && is_string(anchor)? CENTER :
anch.z<0? BOTTOM :
anch.z>0? TOP :
CENTER;
m = _attach_transform(base,spin,undef,geom);
multmatrix(m) {
$parent_anchor = anchor;
$parent_spin = spin;
$parent_orient = undef;
$parent_geom = geom;
$parent_size = _attach_geom_size(geom);
$attach_to = undef;
do_show = _attachment_is_shown($tags);
if (do_show) {
_color($color) {
_text(
text=text, size=size, font=font,
halign=ha, valign=va, spacing=spacing,
direction=direction, language=language,
script=script
);
}
}
}
}
// Section: Rounding 2D shapes
// Module: round2d()

View file

@ -2071,10 +2071,10 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// Section: Text
// Module: atext()
// Module: text3d()
// Topics: Attachments, Text
// Usage:
// atext(text, [h], [size], [font]);
// text3d(text, [h], [size], [font]);
// Description:
// Creates a 3D text block that can be attached to other attachable objects.
// NOTE: This cannot have children attached to it.
@ -2082,7 +2082,7 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// text = The text string to instantiate as an object.
// h = The height to which the text should be extruded. Default: 1
// size = The font size used to create the text block. Default: 10
// font = The name of the font used to create the text block. Default: "Courier"
// font = The name of the font used to create the text block. Default: "Helvetica"
// ---
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `"baseline"`
// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
@ -2092,20 +2092,20 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// "baseline" = Anchors at the baseline of the text, at the start of the string.
// str("baseline",VECTOR) = Anchors at the baseline of the text, modified by the X and Z components of the appended vector.
// Examples:
// atext("Foobar", h=3, size=10);
// atext("Foobar", h=2, size=12, font="Helvetica");
// atext("Foobar", h=2, anchor=CENTER);
// atext("Foobar", h=2, anchor=str("baseline",CENTER));
// atext("Foobar", h=2, anchor=str("baseline",BOTTOM+RIGHT));
// text3d("Foobar", h=3, size=10);
// text3d("Foobar", h=2, size=12, font="Helvetica");
// text3d("Foobar", h=2, anchor=CENTER);
// text3d("Foobar", h=2, anchor=str("baseline",CENTER));
// text3d("Foobar", h=2, anchor=str("baseline",BOTTOM+RIGHT));
// Example: Using line_of() distributor
// txt = "This is the string.";
// line_of(spacing=[10,-5],n=len(txt))
// atext(txt[$idx], size=10, anchor=CENTER);
// text3d(txt[$idx], size=10, anchor=CENTER);
// Example: Using arc_of() distributor
// txt = "This is the string";
// arc_of(r=50, n=len(txt), sa=0, ea=180)
// atext(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orient=UP) {
// text3d(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module text3d(text, h=1, size=10, font="Helvetica", halign, valign, spacing=1.0, direction="ltr", language="em", script="latin", anchor="baseline[-1,0,-1]", spin=0, orient=UP) {
no_children($children);
dummy1 =
assert(is_undef(anchor) || is_vector(anchor) || is_string(anchor), str("Got: ",anchor))
@ -2136,6 +2136,7 @@ module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orien
anch.z>0? TOP :
CENTER;
m = _attach_transform(base,spin,orient,geom);
echo(anchor=anchor, anch=anch, base=base);
multmatrix(m) {
$parent_anchor = anchor;
$parent_spin = spin;
@ -2145,18 +2146,20 @@ module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orien
$attach_to = undef;
do_show = _attachment_is_shown($tags);
if (do_show) {
if (is_undef($color)) {
_color($color) {
linear_extrude(height=h, center=true)
text(text=text, size=size, halign=ha, valign=va, font=font);
} else color($color) {
$color = undef;
linear_extrude(height=h, center=true)
text(text=text, size=size, halign=ha, valign=va, font=font);
_text(
text=text, size=size, font=font,
halign=ha, valign=va, spacing=spacing,
direction=direction, language=language,
script=script
);
}
}
}
}
// This could be replaced with _cut_to_seg_u_form
function _cut_interp(pathcut, path, data) =
[for(entry=pathcut)
@ -2171,7 +2174,6 @@ function _cut_interp(pathcut, path, data) =
];
// Module: path_text()
// Usage:
// path_text(path, text, [size], [thickness], [font], [lettersize], [offset], [reverse], [normal], [top], [textmetrics])