brodykenrick / text_on_OpenSCAD

A library for putting customised "text on" 3D shapes in OpenSCAD with changeable fonts, languages/scripts, text direction.
GNU Lesser General Public License v2.1
135 stars 31 forks source link

Work with non-monospaced fonts? #8

Open badvision opened 7 years ago

badvision commented 7 years ago

The letter spacing is off for proportional fonts -- is it possible to support non-monospaced fonts?

badvision commented 7 years ago

Here's an example of what I used to make the letters align properly for a proportional font:

print("Emerging", updown=-19, charWidth=[0,1.6,1.5,0.95,1.05,0.9,0.9,1.2], eastwest=69);
print("Technologies", updown=-25, charWidth=[0,1.1,1,1.05,1.1,1.1,0.9,0.9,1.1,0.9,0.8,1],eastwest=61);

function getRelativePos(vec,idx,s=0) = (
    idx==s ? vec[idx] 
           : vec[idx] + getRelativePos(vec,idx-1,s)
);

module print(text, transform, updown=0, charWidth = [], r1=r1, r2=r2, h=h, font = "Adobe Clean:style=bold", eastwest = 0) {
    for (idx = [0 : len(text) - 1]) {
        text_on_cylinder(text[idx],[0,0,0],r1=r1,r2=r2,h=h, updown=updown, font=font, eastwest=eastwest+getRelativePos(charWidth, idx) * 5.5);
    }
}

The result looked fantastic in the final project.
emerging tech

dunaevai135 commented 1 month ago

Sadly its no way (I could not find) to calculate char width for font. So here is Phyton script to generate it:

from PIL import ImageFont
font = ImageFont.truetype('Trafaret Kit.ttf', 12)

for i in range(0, 1127):
    width = font.getlength(chr(i))
    print(width, ",", end="")
    # print(chr(i),i, width)

and how i use it (need modification to work in you project) :


#print("#!/bin/perl sp0777i<X+d*lMLlMLlMLlMLlMLlML",rotate=2.8, extrusion_height=2);

charWidths=[7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,4.375 ,5.765625 ,7.859375 ,11.640625 ,14.0625 ,11.28125 ,3.125 ,3.234375 ,3.234375 ,5.140625 ,5.765625 ,3.234375 ,5.515625 ,3.234375 ,5.75 ,11.515625 ,5.40625 ,11.515625 ,11.640625 ,10.578125 ,11.765625 ,11.515625 ,9.359375 ,11.28125 ,11.515625 ,3.125 ,3.125 ,4.078125 ,6.90625 ,4.078125 ,10.078125 ,10.5625 ,12.375 ,11.15625 ,10.90625 ,10.796875 ,10.3125 ,10.203125 ,10.90625 ,11.046875 ,4.3125 ,10.6875 ,10.796875 ,9.90625 ,14.65625 ,11.046875 ,11.046875 ,11.046875 ,11.34375 ,11.15625 ,11.0625 ,9.953125 ,11.28125 ,10.6875 ,17.328125 ,10.21875 ,10.6875 ,9.78125 ,4.6875 ,5.75 ,4.6875 ,6.0 ,6.421875 ,3.125 ,12.375 ,11.15625 ,10.90625 ,10.796875 ,10.3125 ,10.203125 ,10.90625 ,11.046875 ,4.3125 ,10.6875 ,10.796875 ,9.90625 ,14.65625 ,11.046875 ,11.046875 ,11.046875 ,11.34375 ,11.15625 ,11.0625 ,9.953125 ,11.28125 ,10.6875 ,17.328125 ,10.21875 ,10.6875 ,9.78125 ,4.6875 ,3.234375 ,4.6875 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,11.671875 ,7.203125 ,7.203125 ,7.203125 ,6.359375 ,7.203125 ,0.0 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,6.359375 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,6.65625 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 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.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,10.3125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,12.375 ,11.046875 ,11.15625 ,9.90625 ,14.640625 ,10.3125 ,15.59375 ,11.28125 ,11.046875 ,11.046875 ,10.796875 ,11.71875 ,14.65625 ,11.046875 ,11.046875 ,11.046875 ,11.046875 ,10.90625 ,9.953125 ,12.65625 ,15.359375 ,10.21875 ,12.359375 ,10.796875 ,14.15625 ,15.484375 ,11.765625 ,14.875 ,10.921875 ,10.90625 ,16.265625 ,11.15625 ,12.375 ,11.046875 ,11.15625 ,9.90625 ,14.640625 ,10.3125 ,15.59375 ,11.28125 ,11.046875 ,11.046875 ,10.796875 ,11.71875 ,14.65625 ,11.046875 ,11.046875 ,11.046875 ,11.046875 ,10.90625 ,9.953125 ,12.65625 ,15.359375 ,10.21875 ,12.359375 ,10.796875 ,14.15625 ,15.484375 ,11.765625 ,14.875 ,10.921875 ,10.90625 ,16.265625 ,11.15625 ,7.203125 ,10.3125 ,15.71875 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125 ,7.203125];

function getRelativePos(text, idx) = (
    idx==0 ? 0
           : charWidths[ord(text[idx])] + getRelativePos(text,idx-1)
);

module print(text, rotate=rotate, extrusion_height=9, r=20,h=20) {
    for (idx = [0 : len(text) - 1]) {
        translate([0,0,-4.1*sin(rotate)*idx])
        text_on_cylinder(text[idx],[0,0,0],r=r,h=h,extrusion_height=extrusion_height,spacing=2.0,size=4,rotate=rotate, eastwest=getRelativePos(text, idx) * 1.3, font="Trafaret Kit:style=Regular");
    }
}

image

after/before image