Armed with a text editor

mu's views on program and recipe! design

#! /usr/bin/env python
import cairo
from math import pi, sqrt

class Tips(object):
    def __init__(self, filename, width, height):
        self.surface = cairo.SVGSurface(filename + '.svg', width, height)
        cr = self.cr = cairo.Context(self.surface)

        cr.scale(width, height)
        cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)

        self.draw_dest(cr)

        self.surface.write_to_png(filename + '.png')
        cr.show_page()
        self.surface.finish()

class TextLetterAlign(Tips):
    def draw_dest(self, cr):
        cr.scale(1.0/26, 1.0)
        cr.set_font_size(0.8)

        for cx, letter in enumerate('AbCdEfGhIjKlMnOpQrStUvWxYz'):    #center
            xbearing, ybearing, width, height, xadvance, yadvance = ( #center
                    cr.text_extents(letter))                          #center
            cr.move_to(cx + 0.5 - xbearing - width / 2,               #center
                    0.5 - ybearing - height / 2)                      #center
            cr.show_text(letter)                                      #center

class TextFontAlign(Tips):
    def draw_dest(self, cr):
        cr.scale(1.0/26, 1.0)
        cr.set_font_size(0.8)

        fascent, fdescent, fheight, fxadvance, fyadvance = cr.font_extents() #baseline
        for cx, letter in enumerate('AbCdEfGhIjKlMnOpQrStUvWxYz'):           #baseline
            xbearing, ybearing, width, height, xadvance, yadvance = (        #baseline
                    cr.text_extents(letter))                                 #baseline
            cr.move_to(cx + 0.5 - xbearing - width / 2,                      #baseline
                    0.5 - fdescent + fheight / 2)                            #baseline
            cr.show_text(letter)                                             #baseline

class Ellipse(Tips):
    def draw_dest(self, cr):
        cr.set_line_width(0.1)
        cr.save()                         #deform
        cr.scale(0.5, 1)                  #deform
        cr.arc(0.5, 0.5, 0.40, 0, 2 * pi) #deform
        cr.stroke()                       #deform
                                          #deform
        cr.translate(1, 0)                #deform
        cr.arc(0.5, 0.5, 0.40, 0, 2 * pi) #deform
        cr.restore()                      #deform
        cr.stroke()                       #deform

if __name__ == '__main__':
    size = 120
    TextLetterAlign('tips-letter', 26 * size/4, size / 4)
    TextFontAlign('tips-font', 26 * size/4, size / 4)
    Ellipse('tips-ellipse', size, size)