Module pyemf
pyemf is a pure python module that provides a cross-platform ability
to generate enhanced metafiles (.emf files), a vector graphics format
defined by the ECMA-234 standard. Enhanced metafiles are a natively
supported image and scalable clip-art format in the OpenOffice suite of
tools and in Windows applications.
ECMA-234
is the published interface for enhanced metafiles, which is also a
file-based representation of the Windows graphics device interface. This
API follows most of the naming conventions of ECMA-234, and most of the
parameter lists of methods are the same as their ECMA-234 equivalents.
The primary difference is that pyemf has extended the API to be
object-oriented based on the class EMF
. So, while in ECMA-234 the first
argument is generally the device context, here in pyemf it is implicit in
the class instance.
ECMA-234 defines a lot of constants (mostly integers that are used as
flags to various functions) that pyemf defines as module level variables.
So, rather than pollute your global namespace, it is recommended that you
use import pyemf
rather than from pyemf import
*
.
Introduction
To use pyemf in your programs, you instantiate
an EMF
object,
draw some stuff using its methods, and save the file. An example:
#!/usr/bin/env python
import pyemf
width=8.0
height=6.0
dpi=300
emf=pyemf.EMF(width,height,dpi)
thin=emf.CreatePen(pyemf.PS_SOLID,1,(0x01,0x02,0x03))
emf.SelectObject(thin)
emf.Polyline([(0,0),(width*dpi,height*dpi)])
emf.Polyline([(0,height*dpi),(width*dpi,0)])
emf.save("test-1.emf")
This small program creates a 8in x 6in EMF at 300 dots per inch, and
draws two lines connecting the opposite corners. This simple test is
available as test-1.py
in the examples
directory of the pyemf distribution. There are many other small test
programs to demonstrate other features of the EMF class.
Naming Conventions in pyemf
Methods that belong to ECMA-234 are CamelCased
starting
with a capital letter. Methods that apply to the operation of the EMF
class
itself (i.e. load
and save
)
are lower
cased. Constants described in pyemf
that are
used as parameters are ALL_UPPER_CASE
.
Coordinate System
Coordinates are addressed a coordinate system called page
space by integer pixels in a horizontal range (increasing to the
right) from 0
to width*density
, and
vertically (from the top down) 0
to
height*density
. Density is either dots per inch if working
in english units, or dots per millimeter if working in metric.
World and Page Space
Note that there are four coordinate spaces used by GDI: world,
page, device, and physical device. World and page are the same,
unless a world transform (SetWorldTransform
, ModifyWorldTransform
) is used. In that
case, you operate in world space (that is transformed into page space
by multiplying by the transformation matrix), and it could be scaled
differently.
Experimental Coordinate System
Experimental support for device coordinates is available through
SetMapMode
and the various Window and
Viewport methods. Device coordinates are referenced by physical
dimensions corresponding to the mapping mode currently used. [The
methods work correctly (in the sense that they generate the correct
records in the metafile) and the API won't change, but it's not clear
to me what the parameters should do.]
Drawing Characteristics
GDI has a concept of the current object for the each of the
three drawing characteristics: line style, fill style, and font. Once a
characteristic is made current using SelectObject
, it remains current until it
is replaced by another call to SelectObject. Note that a call to
SelectObject only affects that characteristic, and not the other two,
so changing the line style doesn't effect the fill style or the
font.
Additionally, there is a set of stock objects retrievable
with GetStockObject
that should be available
on any system capable of rendering an EMF.
Colors
A quick note about color. Colors in pyemf are specified one of
three ways:
-
(r,g,b) tuple, where each component is a integer between 0 and
255 inclusive.
-
(r,g,b) tuple, where each component is a float between 0.0 and
1.0 inclusive.
-
packed integer created by a call to
RGB
Line Styles
Line styles are created by CreatePen
and specify the style, width,
and color.
Note that there is a NULL_PEN stock object if you don't actually
want to draw a line with a drawing primitive.
Fill Styles
Polygon fill styles are created by CreateSolidBrush
and theoretically CreateHatchBrush
, although the latter
doesn't seem to be supported currently in OpenOffice. So, reliably we
can only use CreateSolidBrush and thus can only specify a fill color
and not a fill pattern.
Note that there is a stock object NULL_BRUSH that doesn't fill,
useful if you want to only draw an outline of a primitive that is
normally filled.
An interesting side-note is that there is no direct support for
gradients in EMF. Examining some .emfs that do have gradients shows
that Windows produces them using clipping regions and subdividing the
object into areas of a single color an drawing slices of the
individual color. Better support for clipping regions is the subject
of a future release of pyemf, but they also don't seem to work well
in OpenOffice, so it hasn't been a high priority.
Fonts
CreateFont
requires a large number of
parameters, the most important being the height, the rotation, and
the name. Note that the height can either be specified as a positive
or negative integer, where negative means use that value as the
average glyph height and positive means use the value as the
average cell height. Since a glyph is contained within a cell,
the negative value will yield a slightly larger font when rendered on
screen.
Note that the two rotation values must specify the same angle.
Also note that font color is not part of a SelectObject
characteristic. It is
specified using the separate method SetTextColor
. SetBkMode
and SetBkColor
are supposed to work with
text, but in my testing with OpenOffice it hasn't been consistent. I
tend to just SetBkMode(pyemf.TRANSPARENT)
and leave it
at that.
Drawing
The methods listed under Drawing Primitives below use either
the current line style or the current fill style (or both). Any
primitive that creates a closed figure (Polygon
, PolyPolygon
, Rectangle
, RoundRect
, Ellipse
, Chord
,
and Pie
) will use both the line and fill
style. Others (Polyline
, PolyPolyline
and Arc
) will
only use the line style, excepting SetPixel
which doesn't use either.
Paths
To create more complicated shapes, the Path Primitives are
used. A path is started with a call to BeginPath
and the initial point should be
set with MoveTo
. Calls to LineTo
, PolylineTo
, ArcTo
,
and PolyBezierTo
extend the path. CloseFigure
should be used to connect the
final point to the starting point, otherwise the path may be filled
incorrectly. EndPath
then completes the path, and it
may be outlined with StrokePath
, filled with FillPath
or both with StrokeAndFillPath
.
Note that OpenOffice ignores ArcTo
in terms of path continuity -- the
arc is drawn, but it is not connected to the path.
Note that SelectClipPath
is broken in
OpenOffice.
Coordinate System Transformation
You might have noticed that methods like Ellipse
and Rectangle
can only create objects that
are aligned with the X-Y axis. This would be a real limitation without
some way to rotate the figures. SetWorldTransform
and ModifyWorldTransform
provide this. These
methods provide a generalized linear transformation that can translate,
rotate, scale and shear subsequent graphics operations.
These methods aren't required by the ECMA-234 spec, which may
explain why their support in OpenOffice is mixed. Drawing primitives
and paths seem to be supported and are transformed, but text is not
(though it should be).
Version: 2.0.0
Author: Rob McMullen
Classes |
EMF |
Reference page of the public API for enhanced metafile creation. |
Function Summary |
int
|
RGB (r,
g,
b)
Pack integer color values into a 32-bit integer format. |
RGB(r,
g,
b)
Pack integer color values into a 32-bit integer format.
-
- Parameters:
r -
0 - 255 or 0.0 - 1.0 specifying red
(type=int or float)
g -
0 - 255 or 0.0 - 1.0 specifying green
(type=int or float)
b -
0 - 255 or 0.0 - 1.0 specifying blue
(type=int or float)
- Returns:
-
single integer that should be used when any function needs a
color value
(type=int)
|
__author__
-
- Type:
-
str
- Value:
|
__author_email__
-
- Type:
-
str
- Value:
'robm@users.sourceforge.net'
|
|
__description__
-
- Type:
-
str
- Value:
'Pure Python Enhanced Metafile Library'
|
|
__download_url__
-
- Type:
-
str
- Value:
'http://sourceforge.net/project/showfiles.php?group_id=148144'
|
|
__keywords__
-
- Type:
-
str
- Value:
'graphics, scalable, vector, image, clipart, emf'
|
|
__license__
-
- Type:
-
str
- Value:
|
__url__
-
- Type:
-
str
- Value:
'http://pyemf.sourceforge.net'
|
|
__version__
-
- Type:
-
str
- Value:
|
AD_CLOCKWISE
-
- Type:
-
int
- Value:
|
AD_COUNTERCLOCKWISE
-
- Type:
-
int
- Value:
|
ALTERNATE
-
- Type:
-
int
- Value:
|
ANSI_FIXED_FONT
-
- Type:
-
int
- Value:
|
ANSI_VAR_FONT
-
- Type:
-
int
- Value:
|
ANTIALIASED_QUALITY
-
- Type:
-
int
- Value:
|
BKMODE_LAST
-
- Type:
-
int
- Value:
|
BLACK_BRUSH
-
- Type:
-
int
- Value:
|
BLACK_PEN
-
- Type:
-
int
- Value:
|
BS_DIBPATTERN
-
- Type:
-
int
- Value:
|
BS_DIBPATTERN8X8
-
- Type:
-
int
- Value:
|
BS_DIBPATTERNPT
-
- Type:
-
int
- Value:
|
BS_HATCHED
-
- Type:
-
int
- Value:
|
BS_HOLLOW
-
- Type:
-
int
- Value:
|
BS_INDEXED
-
- Type:
-
int
- Value:
|
BS_MONOPATTERN
-
- Type:
-
int
- Value:
|
BS_PATTERN
-
- Type:
-
int
- Value:
|
BS_PATTERN8X8
-
- Type:
-
int
- Value:
|
BS_SOLID
-
- Type:
-
int
- Value:
|
CLIP_CHARACTER_PRECIS
-
- Type:
-
int
- Value:
|
CLIP_DEFAULT_PRECIS
-
- Type:
-
int
- Value:
|
CLIP_EMBEDDED
-
- Type:
-
int
- Value:
|
CLIP_LH_ANGLES
-
- Type:
-
int
- Value:
|
CLIP_MASK
-
- Type:
-
int
- Value:
|
CLIP_STROKE_PRECIS
-
- Type:
-
int
- Value:
|
CLIP_TT_ALWAYS
-
- Type:
-
int
- Value:
|
DEFAULT_CHARSET
-
- Type:
-
int
- Value:
|
DEFAULT_GUI_FONT
-
- Type:
-
int
- Value:
|
DEFAULT_PALETTE
-
- Type:
-
int
- Value:
|
DEFAULT_PITCH
-
- Type:
-
int
- Value:
|
DEFAULT_QUALITY
-
- Type:
-
int
- Value:
|
DEVICE_DEFAULT_FONT
-
- Type:
-
int
- Value:
|
DKGRAY_BRUSH
-
- Type:
-
int
- Value:
|
DRAFT_QUALITY
-
- Type:
-
int
- Value:
|
EASTEUROPE_CHARSET
-
- Type:
-
int
- Value:
|
FF_DECORATIVE
-
- Type:
-
int
- Value:
|
FF_DONTCARE
-
- Type:
-
int
- Value:
|
FF_MODERN
-
- Type:
-
int
- Value:
|
FF_ROMAN
-
- Type:
-
int
- Value:
|
FF_SCRIPT
-
- Type:
-
int
- Value:
|
FF_SWISS
-
- Type:
-
int
- Value:
|
FIXED_PITCH
-
- Type:
-
int
- Value:
|
FS_ARABIC
-
- Type:
-
long
- Value:
|
FS_BALTIC
-
- Type:
-
long
- Value:
|
FS_CHINESESIMP
-
- Type:
-
long
- Value:
|
FS_CHINESETRAD
-
- Type:
-
long
- Value:
|
FS_CYRILLIC
-
- Type:
-
long
- Value:
|
FS_GREEK
-
- Type:
-
long
- Value:
|
FS_HEBREW
-
- Type:
-
long
- Value:
|
FS_JISJAPAN
-
- Type:
-
long
- Value:
|
FS_JOHAB
-
- Type:
-
long
- Value:
|
FS_LATIN1
-
- Type:
-
long
- Value:
|
FS_LATIN2
-
- Type:
-
long
- Value:
|
FS_SYMBOL
-
- Type:
-
long
- Value:
|
FS_THAI
-
- Type:
-
long
- Value:
|
FS_TURKISH
-
- Type:
-
long
- Value:
|
FS_VIETNAMESE
-
- Type:
-
long
- Value:
|
FS_WANSUNG
-
- Type:
-
long
- Value:
|
FW_BLACK
-
- Type:
-
int
- Value:
|
FW_DEMIBOLD
-
- Type:
-
int
- Value:
|
FW_DONTCARE
-
- Type:
-
int
- Value:
|
FW_EXTRABOLD
-
- Type:
-
int
- Value:
|
FW_EXTRALIGHT
-
- Type:
-
int
- Value:
|
FW_HEAVY
-
- Type:
-
int
- Value:
|
FW_LIGHT
-
- Type:
-
int
- Value:
|
FW_MEDIUM
-
- Type:
-
int
- Value:
|
FW_NORMAL
-
- Type:
-
int
- Value:
|
FW_REGULAR
-
- Type:
-
int
- Value:
|
FW_SEMIBOLD
-
- Type:
-
int
- Value:
|
FW_ULTRABOLD
-
- Type:
-
int
- Value:
|
FW_ULTRALIGHT
-
- Type:
-
int
- Value:
|
GM_ADVANCED
-
- Type:
-
int
- Value:
|
GM_COMPATIBLE
-
- Type:
-
int
- Value:
|
GRAY_BRUSH
-
- Type:
-
int
- Value:
|
HANGUL_CHARSET
-
- Type:
-
int
- Value:
|
HOLLOW_BRUSH
-
- Type:
-
int
- Value:
|
HS_BDIAGONAL
-
- Type:
-
int
- Value:
|
HS_CROSS
-
- Type:
-
int
- Value:
|
HS_DIAGCROSS
-
- Type:
-
int
- Value:
|
HS_FDIAGONAL
-
- Type:
-
int
- Value:
|
HS_HORIZONTAL
-
- Type:
-
int
- Value:
|
HS_VERTICAL
-
- Type:
-
int
- Value:
|
ICM_QUERY
-
- Type:
-
int
- Value:
|
LTGRAY_BRUSH
-
- Type:
-
int
- Value:
|
MAC_CHARSET
-
- Type:
-
int
- Value:
|
MM_ANISOTROPIC
-
- Type:
-
int
- Value:
|
MM_HIENGLISH
-
- Type:
-
int
- Value:
|
MM_HIMETRIC
-
- Type:
-
int
- Value:
|
MM_ISOTROPIC
-
- Type:
-
int
- Value:
|
MM_LOENGLISH
-
- Type:
-
int
- Value:
|
MM_LOMETRIC
-
- Type:
-
int
- Value:
|
MM_TWIPS
-
- Type:
-
int
- Value:
|
MONO_FONT
-
- Type:
-
int
- Value:
|
MWT_IDENTITY
-
- Type:
-
int
- Value:
|
MWT_LEFTMULTIPLY
-
- Type:
-
int
- Value:
|
MWT_RIGHTMULTIPLY
-
- Type:
-
int
- Value:
|
NONANTIALIASED_QUALITY
-
- Type:
-
int
- Value:
|
NULL_BRUSH
-
- Type:
-
int
- Value:
|
NULL_PEN
-
- Type:
-
int
- Value:
|
OEM_CHARSET
-
- Type:
-
int
- Value:
|
OEM_FIXED_FONT
-
- Type:
-
int
- Value:
|
OUT_CHARACTER_PRECIS
-
- Type:
-
int
- Value:
|
OUT_DEFAULT_PRECIS
-
- Type:
-
int
- Value:
|
OUT_DEVICE_PRECIS
-
- Type:
-
int
- Value:
|
OUT_OUTLINE_PRECIS
-
- Type:
-
int
- Value:
|
OUT_RASTER_PRECIS
-
- Type:
-
int
- Value:
|
OUT_STRING_PRECIS
-
- Type:
-
int
- Value:
|
OUT_STROKE_PRECIS
-
- Type:
-
int
- Value:
|
OUT_TT_ONLY_PRECIS
-
- Type:
-
int
- Value:
|
OUT_TT_PRECIS
-
- Type:
-
int
- Value:
|
POLYFILL_LAST
-
- Type:
-
int
- Value:
|
PROOF_QUALITY
-
- Type:
-
int
- Value:
|
PS_ALTERNATE
-
- Type:
-
int
- Value:
|
PS_COSMETIC
-
- Type:
-
int
- Value:
|
PS_DASHDOT
-
- Type:
-
int
- Value:
|
PS_DASHDOTDOT
-
- Type:
-
int
- Value:
|
PS_ENDCAP_FLAT
-
- Type:
-
int
- Value:
|
PS_ENDCAP_MASK
-
- Type:
-
int
- Value:
|
PS_ENDCAP_ROUND
-
- Type:
-
int
- Value:
|
PS_ENDCAP_SQUARE
-
- Type:
-
int
- Value:
|
PS_GEOMETRIC
-
- Type:
-
int
- Value:
|
PS_INSIDEFRAME
-
- Type:
-
int
- Value:
|
PS_JOIN_BEVEL
-
- Type:
-
int
- Value:
|
PS_JOIN_MASK
-
- Type:
-
int
- Value:
|
PS_JOIN_MITER
-
- Type:
-
int
- Value:
|
PS_JOIN_ROUND
-
- Type:
-
int
- Value:
|
PS_SOLID
-
- Type:
-
int
- Value:
|
PS_STYLE_MASK
-
- Type:
-
int
- Value:
|
PS_TYPE_MASK
-
- Type:
-
int
- Value:
|
PS_USERSTYLE
-
- Type:
-
int
- Value:
|
RGN_COPY
-
- Type:
-
int
- Value:
|
RGN_DIFF
-
- Type:
-
int
- Value:
|
RGN_ERROR
-
- Type:
-
int
- Value:
|
STOCK_LAST
-
- Type:
-
int
- Value:
|
SYMBOL_CHARSET
-
- Type:
-
int
- Value:
|
SYSTEM_FIXED_FONT
-
- Type:
-
int
- Value:
|
SYSTEM_FONT
-
- Type:
-
int
- Value:
|
TA_BASELINE
-
- Type:
-
int
- Value:
|
TA_BOTTOM
-
- Type:
-
int
- Value:
|
TA_CENTER
-
- Type:
-
int
- Value:
|
TA_NOUPDATECP
-
- Type:
-
int
- Value:
|
TA_RIGHT
-
- Type:
-
int
- Value:
|
TA_RTLREADING
-
- Type:
-
int
- Value:
|
TA_UPDATECP
-
- Type:
-
int
- Value:
|
TRANSPARENT
-
- Type:
-
int
- Value:
|
VARIABLE_PITCH
-
- Type:
-
int
- Value:
|
WHITE_BRUSH
-
- Type:
-
int
- Value:
|
WHITE_PEN
-
- Type:
-
int
- Value:
|