diff options
Diffstat (limited to 'show_rulers.py')
-rw-r--r-- | show_rulers.py | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/show_rulers.py b/show_rulers.py index a4d5857..793d259 100644 --- a/show_rulers.py +++ b/show_rulers.py @@ -1,5 +1,6 @@ # Copyright 2007 Mitchell N. Charity # Copyright 2009 Walter Bender +# Copyright 2012 Aneesh Dogra <lionaneesh@gmail.com> # # This file is part of Ruler. # @@ -29,9 +30,14 @@ class ScreenOfRulers(): self.font_bold = font_bold self.w = w self.h = h + self.custom_unit_in_mm = 24.5 # precise upto 2 decimal places self.hw = get_hardware() + self.offset_of_xo_side_from_screen = 0 + self.dpi = 0 def draw(self, c, dpi): + self.c = c + self.dpi = dpi set_background_color(c, self.w, self.h) nw, nh = dimensions_mm(dpi, self.w, self.h) @@ -42,8 +48,8 @@ class ScreenOfRulers(): # Only calculate offsets if on an OLPC XO-1, 1.5, 1.75 # Not applicable to XO 3.0 if self.hw[0:2] == 'xo' and dpi in [200, 201]: - offset_of_xo_side_from_screen = mm(dpi, -38.5) - c.move_to(offset_of_xo_side_from_screen, mm(dpi, 65)) + self.offset_of_xo_side_from_screen = mm(dpi, -38.5) + c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, 180) c.save() @@ -57,10 +63,12 @@ class ScreenOfRulers(): self.draw_cm_ruler(c, dpi, 150) else: - offset_of_xo_side_from_screen = mm(dpi, 0) - c.move_to(offset_of_xo_side_from_screen, mm(dpi, 65)) + self.offset_of_xo_side_from_screen = mm(dpi, 0) + c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, int(nw / 10 * 10)) - + self.draw_custom_ruler(self.custom_unit_in_mm, + int(nw / 10 * 10)) + def draw_ruler_pair(self, c, dpi, y): c.move_to(mm(dpi, 10), y) self.connect_rulers(c, dpi) @@ -70,7 +78,7 @@ class ScreenOfRulers(): c.move_to(mm(dpi, 10), y+mm(dpi, 10)) self.draw_mm_ruler(c, dpi) - + def connect_rulers(self, c, dpi): c.save() c.set_line_cap(cairo.LINE_CAP_SQUARE) @@ -106,12 +114,49 @@ class ScreenOfRulers(): n = xm/10 c.move_to(x, mm(dpi, -4)) write(c, "%d" % n, self.font_bold, mm(dpi, 2.5), centered=True) - + c.move_to(mm(dpi, 1.5), mm(dpi, -4)) write(c, "cm", self.font_bold, mm(dpi, 2)) c.restore() + # units_per_mm is precise upto 2 decimal places + def draw_custom_ruler(self, units_per_mm, width=130): + self.c.set_source_rgb(255, 255, 255) + self.c.rectangle(0, mm(self.dpi, 85), + width, mm(self.dpi, 85) + 10) + self.c.fill() + self.c.set_source_rgb(0, 0, 0) + + self.c.move_to(self.offset_of_xo_side_from_screen, mm(self.dpi, 85)) + self.c.save() + self.c.set_line_cap(cairo.LINE_CAP_SQUARE) + self.c.translate(*self.c.get_current_point()) + + self.c.set_line_width(5) + self.c.move_to(0, 0) + self.c.line_to(mm(self.dpi, width), 0) + for x in [mm(self.dpi, xm / 100) for xm in xrange(0, (width+1) * 100, + int(units_per_mm * 100))]: + self.c.move_to(x, 0) + self.c.rel_line_to(0, mm(self.dpi, -3)) + self.c.stroke() + pt_list = [x / int(units_per_mm * 100) for x in \ + range(0, (width + 1) * 100, int(units_per_mm * 100))] + coord_list = [mm(self.dpi, xm / 100) for xm in range(0, (width + 1) * 100, + int(units_per_mm * 100))] + for a in range(0, len(coord_list)): + xm = pt_list[a] + x = coord_list[a] + n = xm + self.c.move_to(x, mm(self.dpi, -4)) + write(self.c, "%d" % n, self.font_bold, mm(self.dpi, 2.5), centered=True) + + self.c.move_to(mm(self.dpi, 1.5), mm(self.dpi, -4)) + write(self.c, "unit", self.font_bold, mm(self.dpi, 2)) + + self.c.restore() + def draw_mm_ruler(self, c, dpi, width=130): c.save() c.set_line_cap(cairo.LINE_CAP_SQUARE) |