UT_Error IE_ImpGraphic_WMF::convertGraphicToSVG(UT_ByteBuf* pBBwmf, UT_ByteBuf** ppBB) { int status = 0; unsigned int disp_width = 0; unsigned int disp_height = 0; float wmf_width; float wmf_height; float ratio_wmf; float ratio_bounds; unsigned long flags; unsigned int max_width = 768; unsigned int max_height = 512; unsigned long max_flags = 0; static const char* Default_Description = "wmf2svg"; wmf_error_t err; wmf_svg_t* ddata = 0; wmfAPI* API = 0; wmfD_Rect bbox; wmfAPI_Options api_options; bbuf_read_info read_info; char *stream = NULL; unsigned long stream_len = 0; *ppBB = 0; flags = 0; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_svg_function; err = wmf_api_create (&API,flags,&api_options); status = explicit_wmf_error ("wmf_api_create",err); if (status) { if (API) wmf_api_destroy (API); return (UT_ERROR); } read_info.pByteBuf = pBBwmf; read_info.len = pBBwmf->getLength(); read_info.pos = 0; err = wmf_bbuf_input (API,AbiWord_WMF_read,AbiWord_WMF_seek,AbiWord_WMF_tell,(void *) &read_info); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Bad input set\n")); goto ErrorHandler; } err = wmf_scan (API,0,&(bbox)); status = explicit_wmf_error ("wmf_scan",err); if (status) { goto ErrorHandler; } /* Okay, got this far, everything seems cool. */ ddata = WMF_SVG_GetData (API); ddata->out = wmf_stream_create(API, NULL); ddata->Description = (char *)Default_Description; ddata->bbox = bbox; wmf_display_size (API,&disp_width,&disp_height,72,72); wmf_width = (float) disp_width; wmf_height = (float) disp_height; if ((wmf_width <= 0) || (wmf_height <= 0)) { fputs ("Bad image size - but this error shouldn't occur...\n",stderr); status = 1; wmf_api_destroy (API); return UT_ERROR; } if ((wmf_width > (float) max_width ) || (wmf_height > (float) max_height)) { if (max_flags == 0) max_flags = WMF2SVG_MAXPECT; } if (max_flags == WMF2SVG_MAXPECT) /* scale the image */ { ratio_wmf = wmf_height / wmf_width; ratio_bounds = (float) max_height / (float) max_width; if (ratio_wmf > ratio_bounds) { ddata->height = max_height; ddata->width = (unsigned int) ((float) ddata->height / ratio_wmf); } else { ddata->width = max_width; ddata->height = (unsigned int) ((float) ddata->width * ratio_wmf); } } else if (max_flags == WMF2SVG_MAXSIZE) /* bizarre option, really */ { ddata->width = max_width; ddata->height = max_height; } else { ddata->width = (unsigned int) ceil ((double) wmf_width ); ddata->height = (unsigned int) ceil ((double) wmf_height); } ddata->flags |= WMF_SVG_INLINE_IMAGES; ddata->flags |= WMF_GD_OUTPUT_MEMORY | WMF_GD_OWN_BUFFER; if (status == 0) { err = wmf_play (API,0,&(bbox)); status = explicit_wmf_error ("wmf_play",err); } wmf_stream_destroy(API, ddata->out, &stream, &stream_len); if (status == 0) { UT_ByteBuf* pBB = new UT_ByteBuf; pBB->append((const UT_Byte*)stream, (UT_uint32)stream_len); *ppBB = pBB; DELETEP(pBBwmf); wmf_free(API, stream); wmf_api_destroy (API); return UT_OK; } ErrorHandler: DELETEP(pBBwmf); if(API) { if(stream) { wmf_free(API, stream); } wmf_api_destroy (API); } return UT_ERROR; }
int wmf2svg_draw (PlotData* pdata) { int status = 0; float wmf_width; float wmf_height; float ratio_wmf; float ratio_bounds; unsigned long flags; unsigned long max_flags; static char* Default_Description = "wmf2svg"; ImageContext IC; wmf_error_t err; wmf_svg_t* ddata = 0; wmfAPI* API = 0; wmfAPI_Options api_options; flags = 0; flags |= WMF_OPT_FUNCTION; api_options.function = wmf_svg_function; flags |= WMF_OPT_ARGS; api_options.argc = pdata->argc; api_options.argv = pdata->argv; #ifndef DEBUG flags |= WMF_OPT_IGNORE_NONFATAL; #endif err = wmf_api_create (&API,flags,&api_options); status = explicit_wmf_error ("wmf_api_create",err); if (status) { if (API) wmf_api_destroy (API); return (status); } err = wmf_file_open (API,pdata->wmf_filename); status = explicit_wmf_error ("wmf_file_open",err); if (status) { wmf_api_destroy (API); return (status); } err = wmf_scan (API,0,&(pdata->options.bbox)); status = explicit_wmf_error ("wmf_scan",err); if (status) { wmf_api_destroy (API); return (status); } /* Okay, got this far, everything seems cool. */ ddata = WMF_SVG_GetData (API); if (pdata->out) { ddata->out = wmf_stream_create (API,pdata->out); } else { ddata->out = wmf_ztream_create (API,pdata->outz); } if (pdata->options.Description) ddata->Description = pdata->options.Description; else ddata->Description = Default_Description; ddata->bbox = pdata->options.bbox; wmf_size (API,&wmf_width,&wmf_height); if ((wmf_width <= 0) || (wmf_height <= 0)) { fputs ("Bad image size - but this error shouldn't occur...\n",stderr); status = 1; wmf_api_destroy (API); return (status); } max_flags = pdata->max_flags; if ((wmf_width > (float) pdata->max_width ) || (wmf_height > (float) pdata->max_height)) { if (max_flags == 0) max_flags = WMF2SVG_MAXPECT; } if (max_flags == WMF2SVG_MAXPECT) /* scale the image */ { ratio_wmf = wmf_height / wmf_width; ratio_bounds = (float) pdata->max_height / (float) pdata->max_width; if (ratio_wmf > ratio_bounds) { ddata->height = pdata->max_height; ddata->width = (unsigned int) ((float) ddata->height / ratio_wmf); } else { ddata->width = pdata->max_width; ddata->height = (unsigned int) ((float) ddata->width * ratio_wmf); } } else if (max_flags == WMF2SVG_MAXSIZE) /* bizarre option, really */ { ddata->width = pdata->max_width; ddata->height = pdata->max_height; } else { ddata->width = (unsigned int) ceil ((double) wmf_width ); ddata->height = (unsigned int) ceil ((double) wmf_height); } if (pdata->inline_images) { ddata->flags |= WMF_SVG_INLINE_IMAGES; } else { IC.number = 0; IC.prefix = (char*) malloc (strlen (pdata->wmf_filename) + 1); if (IC.prefix) { strcpy (IC.prefix,pdata->wmf_filename); if (wmf_strstr (pdata->wmf_filename,".wmf")) { IC.prefix[strlen (pdata->wmf_filename)-4] = 0; } ddata->image.context = (void*) (&IC); ddata->image.name = image_name; } } if (status == 0) { err = wmf_play (API,0,&(pdata->options.bbox)); status = explicit_wmf_error ("wmf_play",err); } wmf_api_destroy (API); return (status); }