Sie sind auf Seite 1von 1

User Guide Chapter 2 Graphics and Text with pdfgen

Translate then scale


(4, 2.5)

Scale then translate


(4, 2.5) (2.5,1) in inches

(0,0) the Origin


(2.5,1) in inches

(0,0) the Origin

Figure 2-5: Scaling and Translating

NOTE Note: scaling shrinks or grows everything including line widths so using the canvas.scale method to render a
microscopic drawing in scaled microscopic units may produce a blob (because all line widths will get
expanded a huge amount). Also rendering an aircraft wing in meters scaled to centimeters may cause the lines
to shrink to the point where they disappear. For engineering or scientific purposes such as these scale and
translate the units externally before rendering them using the canvas.

Saving and restoring the canvas state: saveState and restoreState


The scaletranslate function used an important feature of the canvas object: the ability to save and
restore the current parameters of the canvas. By enclosing a sequence of operations in a matching pair of
canvas.saveState() an canvas.restoreState() operations all changes of font, color, line style,
scaling, translation, or other aspects of the canvas graphics state can be restored to the state at the point of
the saveState(). Remember that the save/restore calls must match: a stray save or restore operation may
cause unexpected and undesirable behavior. Also, remember that no canvas state is preserved across page
breaks, and the save/restore mechanism does not work across page breaks.

Mirror image
It is interesting although perhaps not terribly useful to note that scale factors can be negative. For example
the following function

def mirror(canvas):
from reportlab.lib.units import inch
canvas.translate(5.5*inch, 0)
canvas.scale(-1.0, 1.0)
coords(canvas)

creates a mirror image of the elements drawn by the coord function.

Page 20

Das könnte Ihnen auch gefallen