From f8f1c2c0076441d3cf41267757f746585f6af03a Mon Sep 17 00:00:00 2001 From: Kanak Tanwar Date: Tue, 11 Mar 2025 07:49:18 +0530 Subject: [PATCH] update tolvera/draw/ruler.py --- tolvera/draw/ruler.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tolvera/draw/ruler.py b/tolvera/draw/ruler.py index f5e400e..b777f8d 100644 --- a/tolvera/draw/ruler.py +++ b/tolvera/draw/ruler.py @@ -6,7 +6,8 @@ def np_txt(text:str, w:int, h=None, x=0, y=0, size=None, color:str="#000000"): if h is None: h=w - font = 'Arial.otf' + # replace font path with your own + font = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" font_size = size if size is not None else min(w,h)//(len(text)/2) pil_font = ImageFont.truetype(font, size=font_size, encoding="unic") bbox = pil_font.getbbox(text) @@ -46,6 +47,7 @@ def __init__(self, self.end = ti.math.vec2(*end) self.domain = ti.math.vec2(*domain) self.rgba = [1.,1.,1.,1.] + self.axis = 1 if end[1] > end[0] else -1 # 1 for +y axis and -1 for +x axis self.create_tick_labels() def create_tick_labels(self): @@ -58,21 +60,30 @@ def create_tick_labels(self): self.tick_labels.append(txt) @ti.kernel - def draw(self): + def draw_lines(self): self.tv.px.line(self.start[0], self.start[1], self.end[0], self.end[1], ti.Vector(self.rgba)) for i in ti.static(range(self.ticks)): l = self.tick_len x = self.start[0] + (self.end[0] - self.start[0]) * (i+1) / self.ticks y = self.start[1] + (self.end[1] - self.start[1]) * (i+1) / self.ticks - self.tv.px.line(x, y, x - l, y, ti.Vector(self.rgba)) - self.tv.px.stamp(x - l, y, self.tick_labels[i]) + if self.axis == 1: + self.tv.px.line(x, y, x+l, y, ti.Vector(self.rgba)) + elif self.axis == -1: + self.tv.px.line(x, y, x, y+l, ti.Vector(self.rgba)) + + def draw_labels(self): + for i in range(self.ticks): + l = self.tick_len + x = int(self.start[0] + (self.end[0] - self.start[0]) * (i+1) / self.ticks) + y = int(self.start[1] + (self.end[1] - self.start[1]) * (i+1) / self.ticks) + self.tv.px.stamp(x-l, y, self.tick_labels[i]) def __call__(self): - self.draw() + self.draw_lines() + self.draw_labels() def main(**kwargs): tv = Tolvera(**kwargs) - txt = text(tv, "Tölvera", tv.x, tv.y) x = Ruler(tv, start=(500,500), end=(500, 1000)) y = Ruler(tv, start=(500,500), end=(1000, 500))