CadQuery / CadQuery/cadquery

Add Text box method

Open
#2,025 3 comments 1 reaction 0 assignees View on GitHub
text rendering
Dominant language
Python
Stars
5.8k
Forks
541
Avg merge
3d 2h
Merged PRs (30d)
5

Description

I worked out a way to do text boxes. Happy to put in a PR if you have a place that this would be best in, This is a lazy method for doing text wrapping in the form of text boxes.

For example if you have a long string you want to put on the back of an object before printing. Rather then having to manually add the returns this will figure out where the line breaks would have to be and put them in.

I have not looked into your formating, but the example for a text box would look like this

```import cadquery as cq

def textbox(width,thickness,text,font,size,plane="XZ",halign="left"):
lines = text.split("\n")
endLines=[]
finalText=cq.Workplane("XZ")
for line in lines:
curLen=0
tempText = ""
bb=cq.Workplane("XZ").text(line,size,thickness,font=font).val().BoundingBox()
average_per_char=(bb.xmax - bb.xmin)/len(line)
characterCutOff=int(width/average_per_char)
for word in line.split(" "):
if len(tempText)+len(word)>characterCutOff:
endLines.append(tempText)
tempText = word+" "
else:
tempText+=word+" "
endLines.append(tempText)
final_string = "\n".join(endLines)
finalText=cq.Workplane(plane).text(final_string,size,thickness,font=font,halign=halign)
return finalText
show_object(textbox(200,0.3,"this is a long string, sort of a runon sentence, the point is to get extra lines.","impact",20))
```

If it is something that would be value added, let me know. If not i will keep it in an external library.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.