sdf
sdf copied to clipboard
Text() function doesn't support unicode strings
I was trying to print Chinese/Japanese words on my model. But only blank squares are drawn:

I'm pretty sure PIL package supports Chinese font and strings. So I guess there is a problem with sdf implementation.
sample code
from sdf import *
base = rectangle((100, 100)).extrude_to(
rectangle((100, 100)), 100, ease.linear
)
base = base.translate((100 / 2, 100 / 2, 100 / 2))
FONT = "simsunb.ttf"
TEXT_ENGLISH = "test"
w, h = measure_text(FONT, TEXT_ENGLISH)
text_test = (
text(FONT, TEXT_ENGLISH, 8 * (w / h), 8)
.extrude(h)
.translate((10 + w + 10, 100 / 2, h / 2))
)
text_test = text_test.orient(Y)
TEXT_CHINESE = "中文测试"
w, h = measure_text(FONT, TEXT_CHINESE)
text_error = (
text(FONT, TEXT_CHINESE, 8 * (w / h), 8)
.extrude(h)
.translate((10 + w + 10, 140 / 2, h / 2))
)
text_error = text_error.orient(Y)
f= base | text_test | text_error
f.save('out.stl', step=0.1)
Environment
OS: win10 Python 3.9.1 sdf version: commit 2a17d2fe0332ce5c2e554469fcd9cf1a525a70b3 (HEAD -> main, origin/main, origin/HEAD)
Appendix: PIL works fine with Chinese
from PIL import Image, ImageDraw, ImageFont
image= Image.new('RGB', (559, 320),(255,255,255))
draw = ImageDraw.Draw(image)
TEXT_CHINESE = "中文测试"
# draw.text()
font = ImageFont.truetype("simsun.ttc", 40)
draw.text((100, 50), TEXT_CHINESE, fill = 255, font = font)
image.show()
output:
