Skip to content

Commit c92dd36

Browse files
committed
feat: implementa write() API para texto 3D avancado (#1642)
Novo modulo write.scad com API avancada de texto 3D para BOSL2: write(text, h, font_size, font, width, height, box, align, valign, line_spacing, letter_space, margin, anchor, spin, orient): - Auto-sizing: sem font_size, escala texto para caber no box - Word wrapping: com font_size + width, quebra em limites de palavra - Multi-line: aceita array de strings ou string com newlines - Alinhamento: horizontal (left/center/right) e vertical (top/center/bottom/baseline) - Margem configuravel dentro do box - Integracao completa com attachable() do BOSL2 get_font_size(font_size, em_size, cap_height, cap_ratio): - Converte entre unidades tipograficas (font_size, em, cap_height) - Corrige o fator 0.72 do OpenSCAD automaticamente Funcoes auxiliares _wrap_line() e _wrap_words() para word wrapping recursivo respeitando limites de largura. Testes cobrindo: get_font_size com 3 unidades, texto basico, multi-line, auto-sizing, wrapping, alinhamentos e attachment. Modulo nao incluido em std.scad (uso: include <BOSL2/write.scad>).
1 parent b9c5dd7 commit c92dd36

2 files changed

Lines changed: 423 additions & 0 deletions

File tree

tests/test_write.scadtest

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
[[test]]
2+
name = "test_get_font_size"
3+
script = '''
4+
include <../std.scad>
5+
include <../write.scad>
6+
7+
module test_get_font_size() {
8+
// font_size: divide por 0.72 para obter OpenSCAD size
9+
sz1 = get_font_size(font_size=12);
10+
assert_approx(sz1, 12/0.72);
11+
12+
// em_size: multiplica por 0.72
13+
sz2 = get_font_size(em_size=10);
14+
assert_approx(sz2, 10*0.72);
15+
16+
// cap_height: cap / ratio / 0.72
17+
sz3 = get_font_size(cap_height=5);
18+
assert_approx(sz3, 5/0.7/0.72);
19+
20+
// cap_height com ratio customizado
21+
sz4 = get_font_size(cap_height=5, cap_ratio=0.65);
22+
assert_approx(sz4, 5/0.65/0.72);
23+
}
24+
test_get_font_size();
25+
'''
26+
27+
[[test]]
28+
name = "test_write_basic"
29+
script = '''
30+
include <../std.scad>
31+
include <../write.scad>
32+
33+
module test_write_basic() {
34+
$fn = 8;
35+
// Texto basico com font_size
36+
write("Hello", font_size=10, h=2);
37+
}
38+
test_write_basic();
39+
'''
40+
41+
[[test]]
42+
name = "test_write_multiline"
43+
script = '''
44+
include <../std.scad>
45+
include <../write.scad>
46+
47+
module test_write_multiline() {
48+
$fn = 8;
49+
// Multi-line via array
50+
write(["Line 1", "Line 2"], font_size=8, h=1);
51+
// Multi-line via centro
52+
write(["A", "B", "C"], font_size=6, h=1, align="center");
53+
}
54+
test_write_multiline();
55+
'''
56+
57+
[[test]]
58+
name = "test_write_autosize"
59+
script = '''
60+
include <../std.scad>
61+
include <../write.scad>
62+
63+
module test_write_autosize() {
64+
$fn = 8;
65+
// Auto-size com box
66+
write("Fit!", box=[40,15], h=1);
67+
// Auto-size com width e height separados
68+
write("AB", width=30, height=10, h=1);
69+
}
70+
test_write_autosize();
71+
'''
72+
73+
[[test]]
74+
name = "test_write_wrapping"
75+
script = '''
76+
include <../std.scad>
77+
include <../write.scad>
78+
79+
module test_write_wrapping() {
80+
$fn = 8;
81+
// Wrapping com font_size + width
82+
write("Wrap this text please", font_size=5, width=40, h=1);
83+
}
84+
test_write_wrapping();
85+
'''
86+
87+
[[test]]
88+
name = "test_write_alignment"
89+
script = '''
90+
include <../std.scad>
91+
include <../write.scad>
92+
93+
module test_write_alignment() {
94+
$fn = 8;
95+
// Alinhamentos horizontais
96+
write("Left", font_size=8, h=1, align="left");
97+
write("Center", font_size=8, h=1, align="center");
98+
write("Right", font_size=8, h=1, align="right");
99+
// Alinhamentos verticais com box
100+
write("Top", box=[40,20], h=1, valign="top");
101+
write("Bottom", box=[40,20], h=1, valign="bottom");
102+
}
103+
test_write_alignment();
104+
'''
105+
106+
[[test]]
107+
name = "test_write_attachment"
108+
script = '''
109+
include <../std.scad>
110+
include <../write.scad>
111+
112+
module test_write_attachment() {
113+
$fn = 8;
114+
// Attachment a um cubo
115+
cuboid(50)
116+
attach(TOP)
117+
write("Label", font_size=6, h=1, anchor=BOTTOM);
118+
}
119+
test_write_attachment();
120+
'''

0 commit comments

Comments
 (0)