OK,
I'm attaching a file that converts svg to emf, which is based on
librsvg and cairo.
I've spent the all night working on this, but the result is still not
satisfying. The converted emf file is even worse than the png file
produced from matplotlib. I'm not sure if it's because I did something
wrong or it's because of the limitation of this method itself. I'm
posting here in hope of some one with more knowledge would enlighten
me. Thanks.
To build the problem, you need to download librsvg and it's dependency
from http://ftp.gnome.org/pub/gnome/binaries/win32/
Best Regards
Shixin Zeng
On Tue, Sep 1, 2009 at 6:43 PM, Shixin Zeng<zeng.shi...@gmail.com> wrote:
> Hi,
>
> Could someone tell me what's the best format that matplotlib can
> produce for insertion to MS word? I'm working on a paper using MS
> word. I used matplotlib to produce the pictures in "png' format, but
> my professor doesn't satisfy with the quality of the pictures, he asks
> me to do it in "emf" format, but I can't get an "emf" output from
> matplotlib. While other vector formats that are supported by
> matplotlib are not supported by MS word. I have worked days on
> producing this pictures, I don't want to abandon them just because
> they can't be imported to MS word. I really like to produce my
> pictures by using matplotlib, but I can't really throw away MS word. I
> also tried pstoedit to try to convert to emf from the ps, but it
> doesn't work on my system due to some weired missing procedure entry
> points in imagick dll.
>
> I'm kinda in a hurry, any help would be greatly appreciated.
>
> Best Regards
>
> Shixin Zeng
>
/* A simple program that converts an svg file to an emf file
* based on librsvg and cairo
*
* Author Shixin Zeng <zeng.shi...@gmail.com>
* License: GPL V2 or newer
**/
#include <cairo.h>
#include <librsvg/rsvg.h>
#include <windows.h>
//#define FACTOR_INCH_TO_MM 25.4
//#define DPI 90
//static double dpi = 90;
static char *src = NULL;
static char *dest = NULL;
static GOptionEntry entries[] =
{
//{"dpi", 'd', 0, G_OPTION_ARG_DOUBLE, &dpi, "the source dpi", "dpi"},
{"from", 'f', 0, G_OPTION_ARG_STRING, &src, "the source file", "src"},
{"to", 't', 0, G_OPTION_ARG_STRING, &dest, "the destination file",
"dest"},
{NULL}
};
int
main (int argc, char *argv[])
{
cairo_surface_t *surface;
cairo_t *cr;
HDC hdc;
RsvgHandle * rsvg_hd = NULL;
RsvgDimensionData rdim;
HDC dc = GetDC(NULL);
RECT dim;
float MetaPixelsX, MetaPixelsY;
float MetaMMX, MetaMMY;
int i = 1;
GError *error = NULL;
GOptionContext *context;
context = g_option_context_new("Convert SVG to EMF");
g_option_context_add_main_entries(context, entries, "SVG_TO_EMF");
if(!g_option_context_parse(context, &argc, &argv, &error))
{
g_print("option parsing failed: %s\n", error->message);
return 1;
}
if (src == NULL || dest == NULL){
g_print("source and dest files must be given\n%s",
g_option_context_get_help(context, TRUE, NULL));
return 1;
}
rsvg_init();
rsvg_hd = rsvg_handle_new_from_file(src, NULL);
//dpi = GetDeviceCaps(dc, HORZRES);
//rsvg_handle_set_dpi (rsvg_hd, dpi);
rsvg_handle_get_dimensions(rsvg_hd, &rdim);
g_print("SVG: height = %d pt, width = %d pit\n", rdim.height,
rdim.width);
/* MetaPixelsX = MetaWidthMM * MetaPixels / (MetaMM * 100)
*
* where MetaPixelsX = number of pixels on the X axis
* MetaWidthMM = metafile width in 0.01mm units
* MetaPixels = width in pixels of the reference device
* MetaMM = width in millimeters of the reference device
*
* MetaWidthMM = MetaPixelsx * MetaMM * 100 / MetaPixels
*/
MetaPixelsX = GetDeviceCaps(dc, HORZRES);
MetaPixelsY = GetDeviceCaps(dc, VERTRES);
MetaMMX = GetDeviceCaps(dc, HORZSIZE);
MetaMMY = GetDeviceCaps(dc, VERTSIZE);
dim.left = 0;
dim.top = 0;
dim.bottom = rdim.height * MetaMMY * 100 / MetaPixelsY;
dim.right = rdim.width * MetaMMX * 100 / MetaPixelsX;
g_print("EMF: height = %d mm, width = %d mm\n", dim.bottom/100,
dim.right/100);
hdc = CreateEnhMetaFile(dc,
dest,
&dim,
NULL);
ReleaseDC(NULL, dc);
if(!hdc){
g_print("creating emf file failed\n");
return 1;
}
surface = cairo_win32_printing_surface_create (hdc);
cr = cairo_create(surface);
if(!rsvg_handle_render_cairo(rsvg_hd, cr))
g_print("render to cairo failed\n");
cairo_destroy (cr);
cairo_surface_destroy (surface);
CloseEnhMetaFile(hdc);
rsvg_term();
return 0;
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users