Package lib :: Module xcairo
[hide private]
[frames] | no frames]

Module xcairo

source code

general-purpose drawing routines & higher-level CAIRO routines

Classes [hide private]
  Page
class holding Page properties
  InvalidFormat
exception thrown when an invalid output format is requested
  PageWriter
class to output multiple pages in raster (png) or vector (pdf) format
Functions [hide private]
(int,int)
page_spec(spec=None)
return tuple of page dimensions (width,height) in mm for spec
source code
float
mm_to_dots(mm)
convert millimeters to dots
source code
float
dots_to_mm(dots)
convert dots to millimeters
source code
 
set_color(cr, rgba)
set stroke color
source code
str
extract_font_name(f)
extract the font name from a string or from a tuple (fontname, slant, weight)
source code
 
make_sloppy_rect(cr, rect, sdx=0.0, sdy=0.0, srot=0.0)
slightly rotate and translate a rect to give it a sloppy look
source code
 
draw_shadow(cr, rect, thickness=None, shadow_color=(0, 0, 0, 0.3))
draw a shadow at the bottom-right corner of a rect
source code
 
draw_line(cr, rect, stroke_rgba=None, stroke_width=1.0)
draw a line from (x,y) to (x+w,y+h), where rect=(x,y,w,h)
source code
 
draw_box(cr, rect, stroke_rgba=None, fill_rgba=None, stroke_width=1.0, shadow=None)
draw a box (rectangle) with optional shadow
source code
 
draw_str(cr, text, rect, scaling=-1, stroke_rgba=None, align=(2, 0), bbox=False, font='Times', measure=None, shadow=None)
draw text
source code
Variables [hide private]
  XDPI = 72.0
dots per inch of output device
  ISOPAGE = [1188, 840, 594, 420, 297, 210, 148, 105, 74, 52, 37]
ISO page height list, index k for height of Ak paper
  __package__ = 'lib'
Function Details [hide private]

page_spec(spec=None)

source code 

return tuple of page dimensions (width,height) in mm for spec

Parameters:
  • spec - paper type. Paper type can be an ISO paper type (a0..a9 or a0w..a9w) or of the form W:H; positive values correspond to W or H mm, negative values correspond to -W or -H pixels; 'w' suffix swaps width & height; None defaults to A4 paper
Returns: (int,int)

set_color(cr, rgba)

source code 

set stroke color

Parameters:
  • cr - cairo context
  • rgba (tuple) - (r,g,b) or (r,g,b,a)

make_sloppy_rect(cr, rect, sdx=0.0, sdy=0.0, srot=0.0)

source code 

slightly rotate and translate a rect to give it a sloppy look

Parameters:
  • cr - cairo context
  • sdx - maximum x-offset, true offset will be uniformly distibuted
  • sdy - maximum y-offset
  • sdy - maximum rotation

draw_shadow(cr, rect, thickness=None, shadow_color=(0, 0, 0, 0.3))

source code 

draw a shadow at the bottom-right corner of a rect

Parameters:
  • cr - cairo context
  • rect - tuple (x,y,w,h)
  • thickness - if None nothing is drawn
  • shadow_color - shadow color

draw_line(cr, rect, stroke_rgba=None, stroke_width=1.0)

source code 

draw a line from (x,y) to (x+w,y+h), where rect=(x,y,w,h)

Parameters:
  • cr - cairo context
  • rect - tuple (x,y,w,h)
  • stroke_rgba - stroke color
  • stroke_width - stroke width, if <= 0 nothing is drawn

draw_box(cr, rect, stroke_rgba=None, fill_rgba=None, stroke_width=1.0, shadow=None)

source code 

draw a box (rectangle) with optional shadow

Parameters:
  • cr - cairo context
  • rect - box rectangle as tuple (x,y,w,h)
  • stroke_rgba - stroke color (set if not None)
  • fill_rgba - fill color (set if not None)
  • stroke_width - stroke width
  • shadow - shadow thickness

draw_str(cr, text, rect, scaling=-1, stroke_rgba=None, align=(2, 0), bbox=False, font='Times', measure=None, shadow=None)

source code 

draw text

Parameters:
  • cr - cairo context
  • text - text string to be drawn
  • scaling (int) - text scaling mode
    • -1: auto select x-scaling or y-scaling (whatever fills the rect)
    • 0: no scaling
    • 1: x-scaling, scale so that text fills rect horizontally, preserving ratio
    • 2: y-scaling, scale so that text fills rect vertically, preserving ratio
    • 3: xy-scaling, stretch so that text fills rect completely, does not preserve ratio
  • stroke_rgba - stroke color
  • align (tuple) - alignment mode as (int,int) tuple for horizontal/vertical alignment
    • 0: left/top alignment
    • 1: right/bottom alignment
    • 2: center/middle alignment
  • bbox - draw text bounding box (for debugging)
  • font - font name as string or (font,slant,weight) tuple
  • measure - use this string for measurement instead of text
  • shadow - draw text shadow as tuple (dx,dy)