text fixes

This commit is contained in:
Michael Fogleman 2018-03-11 15:13:23 -04:00
parent 4a81a50ac4
commit 506e8b6744
1 changed files with 9 additions and 5 deletions

View File

@ -1,7 +1,11 @@
from __future__ import division from __future__ import division
from .drawing import Drawing
from .hershey_fonts import * from .hershey_fonts import *
import itertools
import string
def text(string, font=FUTURAL, spacing=0, extra=0): def text(string, font=FUTURAL, spacing=0, extra=0):
result = [] result = []
x = 0 x = 0
@ -46,10 +50,10 @@ def _word_wrap(text, width, measure_func):
class Font(object): class Font(object):
def __init__(self, font, point_size): def __init__(self, font, point_size):
self.font = font self.font = font
self.max_height = axi.Drawing(axi.text(string.printable, font)).height self.max_height = Drawing(text(string.printable, font)).height
self.scale = (point_size / 72) / self.max_height self.scale = (point_size / 72) / self.max_height
def text(self, text): def text(self, string):
d = axi.Drawing(axi.text(text, self.font)) d = Drawing(text(string, self.font))
d = d.scale(self.scale) d = d.scale(self.scale)
return d return d
def justify_text(self, text, width): def justify_text(self, text, width):
@ -59,7 +63,7 @@ class Font(object):
if spaces == 0 or w >= width: if spaces == 0 or w >= width:
return d return d
e = ((width - w) / spaces) / self.scale e = ((width - w) / spaces) / self.scale
d = axi.Drawing(axi.text(text, self.font, extra=e)) d = Drawing(text(text, self.font, extra=e))
d = d.scale(self.scale) d = d.scale(self.scale)
return d return d
def measure(self, text): def measure(self, text):
@ -72,7 +76,7 @@ class Font(object):
jds = [self.justify_text(line, max_width) for line in lines] jds = [self.justify_text(line, max_width) for line in lines]
ds = jds[:-1] + [ds[-1]] ds = jds[:-1] + [ds[-1]]
spacing = line_spacing * self.max_height * self.scale spacing = line_spacing * self.max_height * self.scale
result = axi.Drawing() result = Drawing()
y = 0 y = 0
for d in ds: for d in ds:
if align == 0: if align == 0: