static void export_svg () { cairo_surface_t *surface; cairo_rectangle_t extents; cairo_matrix_t mtx; cairo_t *cr; /* Create a surface and run export_layout_page() to figure out * the picture extents and set up the cairo transformation * matrix. The surface is created only in order to force * eda_renderer_default_get_user_bounds() to behave quietly. */ surface = cairo_svg_surface_create (settings.outfile, 0, 0); cr = cairo_create (surface); g_object_set (renderer, "cairo-context", cr, NULL); export_layout_page (NULL, &extents, &mtx); cairo_destroy (cr); /* Now create a new surface with the known extents. */ surface = cairo_svg_surface_create (settings.outfile, extents.width, extents.height); cr = cairo_create (surface); g_object_set (renderer, "cairo-context", cr, NULL); cairo_set_matrix (cr, &mtx); export_draw_page (NULL); cairo_show_page (cr); cairo_surface_finish (surface); export_cairo_check_error (cairo_surface_status (surface)); }
int main (void) { cairo_t *cr; const char *filename = "svg-clip.svg"; cairo_surface_t *surface; cairo_test_init ("svg-clip"); surface = cairo_svg_surface_create (filename, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); if (surface == NULL) { fprintf (stderr, "Failed to create svg surface for file %s\n", filename); return CAIRO_TEST_FAILURE; } cr = cairo_create (surface); test_clip (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); cairo_show_page (cr); cairo_destroy (cr); cairo_surface_destroy (surface); printf ("svg-clip: Please check %s to make sure it looks happy.\n", filename); cairo_test_fini (); return 0; }
static void gvloadimage_rsvg_cairo(GVJ_t * job, usershape_t *us, boxf b, boolean filled) { RsvgHandle* rsvgh = gvloadimage_rsvg_load(job, us); cairo_t *cr = (cairo_t *) job->context; /* target context */ cairo_surface_t *surface; /* source surface */ if (rsvgh) { cairo_save(cr); surface = cairo_svg_surface_create(NUL_FILE, us->w, us->h); cairo_surface_reference(surface); cairo_set_source_surface(cr, surface, 0, 0); cairo_translate(cr, ROUND(b.LL.x), ROUND(-b.UR.y)); cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h); rsvg_handle_render_cairo(rsvgh, cr); cairo_paint (cr); cairo_restore(cr); } }
int convertPage(PopplerPage *page, const char* svgFilename) { // Poppler stuff double width, height; // Cairo stuff cairo_surface_t *surface; cairo_t *drawcontext; if (page == NULL) { fprintf(stderr, "Page does not exist\n"); return -1; } poppler_page_get_size (page, &width, &height); // Open the SVG file surface = cairo_svg_surface_create(svgFilename, width, height); drawcontext = cairo_create(surface); // Render the PDF file into the SVG file poppler_page_render_for_printing(page, drawcontext); cairo_show_page(drawcontext); // Close the SVG file cairo_destroy(drawcontext); cairo_surface_destroy(surface); // Close the PDF file g_object_unref(page); return 0; }
static PyObject * svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { double width_in_points, height_in_points; PyObject *file, *writer; if (!PyArg_ParseTuple(args, "Odd:SVGSurface.__new__", &file, &width_in_points, &height_in_points)) return NULL; if (PyObject_TypeCheck (file, &PyBaseString_Type)) { /* string (filename) argument */ return PycairoSurface_FromSurface ( cairo_svg_surface_create (PyString_AsString(file), width_in_points, height_in_points), NULL); } /* else: file or file-like object argument */ writer = PyObject_GetAttrString (file, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, "SVGSurface argument 1 must be a filename (str), file object, or an object " "that has a \"write\" method (like StringIO)"); return NULL; } Py_DECREF(writer); return PycairoSurface_FromSurface ( cairo_svg_surface_create_for_stream (_write_func, file, width_in_points, height_in_points), file); }
int conv_svg (RioData *data, const char *filename) { int status; /* Determine original image height & width */ uint32_t height, width; status = rio_data_get_metadata_uint32 (data, RIO_KEY_IMAGE_ROWS, &height) && rio_data_get_metadata_uint32 (data, RIO_KEY_IMAGE_COLS, &width); if (!status) { fprintf (stderr, "ERROR: Could not determine image dimensions.\n"); return 0; } /* Create output surface */ cairo_surface_t *svg = cairo_svg_surface_create (filename, width, height); if (cairo_surface_status (svg) != CAIRO_STATUS_SUCCESS) { fprintf (stderr, "ERROR: Could not create SVG surface for %s: %s\n", filename, cairo_status_to_string (cairo_surface_status (svg))); return 0; } /* FIXME naively assumes that all will be fine from now on. */ svg_draw (data, svg); cairo_surface_finish (svg); return 1; }
static cairo_test_status_t preamble (cairo_test_context_t *ctx) { cairo_t *cr; const char *filename = "svg-clip.out.svg"; cairo_surface_t *surface; if (! cairo_test_is_target_enabled (ctx, "svg11") && ! cairo_test_is_target_enabled (ctx, "svg12")) { return CAIRO_TEST_UNTESTED; } surface = cairo_svg_surface_create (filename, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); if (cairo_surface_status (surface)) { cairo_test_log (ctx, "Failed to create svg surface for file %s: %s\n", filename, cairo_status_to_string (cairo_surface_status (surface))); return CAIRO_TEST_FAILURE; } cr = cairo_create (surface); test_clip (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); cairo_show_page (cr); cairo_destroy (cr); cairo_surface_destroy (surface); printf ("svg-clip: Please check %s to make sure it looks happy.\n", filename); return CAIRO_TEST_SUCCESS; }
IoObject *IoCairoSVGSurface_create(IoCairoSVGSurface *self, IoObject *locals, IoMessage *m) { char *filename = CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 0)); double w = IoMessage_locals_doubleArgAt_(m, locals, 1); double h = IoMessage_locals_doubleArgAt_(m, locals, 2); return IoCairoSurface_newWithRawSurface_(IOSTATE, m, cairo_svg_surface_create(filename, w, h)); }
// cairo_public cairo_surface_t * // cairo_svg_surface_create (const char *filename, // double width_in_points, // double height_in_points); static int l_cairo_svg_surface_create(lua_State* L) { const char *filename = luaL_checkstring(L, 1); double width_in_points = luaL_checknumber(L, 2); double height_in_points = luaL_checknumber(L, 3); cairo_surface_t *v = cairo_svg_surface_create(filename, width_in_points, height_in_points); lua_pushlightuserdata(L, v); return 1; }
void ofCairoRenderer::setup(string _filename, Type _type, bool multiPage_, bool b3D_, ofRectangle _viewport){ if( _viewport.width == 0 || _viewport.height == 0 ){ _viewport.set(0, 0, ofGetWidth(), ofGetHeight()); } filename = _filename; type = _type; streamBuffer.clear(); if(type == FROM_FILE_EXTENSION){ string ext = ofFilePath::getFileExt(filename); if(ofToLower(ext)=="svg"){ type = SVG; }else if(ofToLower(ext)=="pdf"){ type = PDF; }else{ // default to image type = IMAGE; } } switch(type){ case PDF: if(filename==""){ surface = cairo_pdf_surface_create_for_stream(&ofCairoRenderer::stream_function,this,_viewport.width, _viewport.height); }else{ surface = cairo_pdf_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); } break; case SVG: if(filename==""){ surface = cairo_svg_surface_create_for_stream(&ofCairoRenderer::stream_function,this,_viewport.width, _viewport.height); }else{ surface = cairo_svg_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); } break; case IMAGE: imageBuffer.allocate(_viewport.width, _viewport.height, 4); surface = cairo_image_surface_create_for_data(imageBuffer.getPixels(),CAIRO_FORMAT_ARGB32,_viewport.width, _viewport.height,_viewport.width*4); break; case FROM_FILE_EXTENSION: ofLogFatalError("ofCairoRenderer") << "Type not determined from file extension!"; break; default: ofLogError("ofCairoRenderer") << "Unknown type encountered!"; break; } cr = cairo_create(surface); cairo_set_antialias(cr,CAIRO_ANTIALIAS_SUBPIXEL); viewportRect = _viewport; viewport(viewportRect); page = 0; b3D = b3D_; multiPage = multiPage_; setStyle(ofGetStyle()); }
static cairo_surface_t * _cairo_boilerplate_svg_create_surface (const char *name, cairo_content_t content, cairo_svg_version_t version, int width, int height, int max_width, int max_height, cairo_boilerplate_mode_t mode, int id, void **closure) { svg_target_closure_t *ptc; cairo_surface_t *surface; cairo_status_t status; *closure = ptc = xmalloc (sizeof (svg_target_closure_t)); ptc->width = width; ptc->height = height; xasprintf (&ptc->filename, "%s-out.svg", name); xunlink (ptc->filename); surface = cairo_svg_surface_create (ptc->filename, width, height); if (cairo_surface_status (surface)) goto CLEANUP_FILENAME; cairo_svg_surface_restrict_to_version (surface, version); cairo_surface_set_fallback_resolution (surface, 72., 72.); if (content == CAIRO_CONTENT_COLOR) { ptc->target = surface; surface = cairo_surface_create_similar (ptc->target, CAIRO_CONTENT_COLOR, width, height); if (cairo_surface_status (surface)) goto CLEANUP_TARGET; } else ptc->target = NULL; status = cairo_surface_set_user_data (surface, &svg_closure_key, ptc, NULL); if (status == CAIRO_STATUS_SUCCESS) return surface; cairo_surface_destroy (surface); surface = cairo_boilerplate_surface_create_in_error (status); CLEANUP_TARGET: cairo_surface_destroy (ptc->target); CLEANUP_FILENAME: free (ptc->filename); free (ptc); return surface; }
void dv_export_all_viewports() { double w, h; dv_export_viewports_get_size_r(DVG->VP, &w, &h); cairo_surface_t * surface; cairo_t * cr; /* PNG */ surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h); dv_export_viewports_to_img_r(DVG->VP, surface, 0.0, 0.0); cairo_surface_write_to_png(surface, "00dv.png"); fprintf(stdout, "Exported all viewports to 00dv.png\n"); cairo_surface_destroy(surface); GdkRGBA white[1]; gdk_rgba_parse(white, "white"); /* EPS */ /* surface = cairo_ps_surface_create("00dv.eps", w, h); cairo_ps_surface_set_eps(surface, TRUE); cr = cairo_create(surface); // Whiten background cairo_set_source_rgba(cr, white->red, white->green, white->blue, white->alpha); cairo_paint(cr); dv_export_viewports_to_eps_r(DVG->VP, cr, 0.0, 0.0); fprintf(stdout, "Exported all viewports to 00dv.eps\n"); cairo_destroy(cr); cairo_surface_destroy(surface); */ /* SVG */ surface = cairo_svg_surface_create("00dv.svg", w, h); cr = cairo_create(surface); // Whiten background cairo_set_source_rgba(cr, white->red, white->green, white->blue, white->alpha); cairo_paint(cr); dv_export_viewports_to_svg_r(DVG->VP, cr, 0.0, 0.0); fprintf(stdout, "Exported all viewports to 00dv.svg\n"); cairo_destroy(cr); cairo_surface_destroy(surface); /* PDF */ surface = cairo_pdf_surface_create("00dv.pdf", w, h); cr = cairo_create(surface); // Whiten background cairo_set_source_rgba(cr, white->red, white->green, white->blue, white->alpha); cairo_paint(cr); dv_export_viewports_to_pdf_r(DVG->VP, cr, 0.0, 0.0); fprintf(stdout, "Exported all viewports to 00dv.pdf\n"); cairo_destroy(cr); cairo_surface_destroy(surface); return; }
static cairo_surface_t * create_source_surface (int size) { cairo_surface_t *surface; surface = cairo_svg_surface_create ("svg-surface-source.out.svg", size, size); cairo_surface_set_fallback_resolution (surface, 72., 72.); return surface; }
/* * This function initializes any supported environment (Cairo Surface and Cairo * main object, cairo_t). It must be called before ANY cairo operation. * @context: * CP_Context which holds the pointers to the cairo structures, and * other plot settings. * @ft: * File Type, to be plotted. For supported filetypes check the util.h * file (_CP_FILETYPE_ structure). */ void cp_initEnv(CP_Context *context, CP_FileType ft) { if (ft == CP_PNG){ context->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, context->width, context->height); }else{ cp_addFileExtension(&(context->fname), context->name, ft); context->surface = cairo_svg_surface_create(context->fname, context->width, context->height); cairo_svg_surface_restrict_to_version(context->surface, CAIRO_SVG_VERSION_1_2); } context->cr = cairo_create(context->surface); }
void CairoBox::out_svg(const char* filename, int wpts, int hpts) { cairo_surface_t* surface; cairo_t* cr; surface = cairo_svg_surface_create (filename, wpts, hpts); cr = cairo_create (surface); cairo_set_source_rgb (cr, 0, 0, 0); // user coordinates -- see cairo_svg_surface_create graphic(cr, 0, 0, wpts, hpts); cairo_destroy (cr); cairo_surface_destroy (surface); }
/*================================================================================== pspltrgn - subroutine to set which small plot region to use This routine is called by pschem in psvdraw_new in order to plot multiple ternary chemographies on a single page. Each page can hold MAXPLOTSPERPAGE. If another is requested, then a new document will be created with a name similar to that of the first, and the process will begin again. This overrides any settings made in psssc2, and assumes that x and y (real-unit) bounds are 0.0-1.0. Note that if this routine is called, then plot_aspect_ratio will be ignored. Note that plotnum is a zero-based index ==================================================================================*/ void pspltrgn_ (int *plotnum) { char *outFileName = malloc((strlen(dmh_fileNameRoot)+50) * sizeof(char)); int plotPosition = *plotnum % MAXPLOTSPERPAGE; int pageNum = *plotnum / MAXPLOTSPERPAGE; int boxEdge, rowNum, colNum; DEBUGPRINT(("In pspltrgn. Plotnum = %i, plotPosition = %i, pageNum = %i\n", *plotnum, plotPosition, pageNum)); if (plotPosition == 0 && pageNum > 0) { /* we need to start a new page, so let's close the current one, and start a new one with a related name */ DEBUGPRINT(("In pspltrgn. Closing current page and starting a new one with\n page number=%i and file type=%i\n", pageNum, dmh_outputFileType)); closeSurface(); // close existing surface switch(dmh_outputFileType) { case PDFTYPE: sprintf(outFileName, "%s_%i.%s", dmh_fileNameRoot, pageNum, "pdf"); dmh_surf = cairo_pdf_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); break; case PSTYPE: sprintf(outFileName, "%s_%i.%s", dmh_fileNameRoot, pageNum, "ps"); dmh_surf = cairo_ps_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); cairo_ps_surface_set_eps (dmh_surf, 1); break; case SVGTYPE: sprintf(outFileName, "%s_%i.%s", dmh_fileNameRoot, pageNum, "svg"); dmh_surf = cairo_svg_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); break; } dmh_cr = cairo_create (dmh_surf); cairo_identity_matrix(dmh_cr); cairo_set_line_join(dmh_cr, CAIRO_LINE_JOIN_ROUND); dmh_min_tracked_x = DBL_MAX; } /* Set the location on the page for the small plot */ dmh_aspectRatio = 1.0; /* Ignores plot_aspect_ratio. */ boxEdge = (dmh_pageWidth - LEFTMARGIN - RIGHTMARGIN - MULTIPLOTGUTTER) / 2; rowNum = plotPosition / 2; colNum = plotPosition % 2; dmh_xoffset = LEFTMARGIN + ((boxEdge + MULTIPLOTGUTTER) * colNum); dmh_yoffset = (dmh_pageHeight * 0.5) + (boxEdge * 1.5) + MULTIPLOTGUTTER - (boxEdge * (rowNum+1)) - (MULTIPLOTGUTTER * rowNum); dmh_xscale = boxEdge; dmh_yscale = boxEdge; DEBUGPRINT(("End pspltrgn. boxEdge=%i; r,c=(%i,%i); scale=(%f, %f); offset=(%f, %f); DevPtRange=(%f,%f)-(%f,%f).\n", boxEdge, rowNum, colNum, dmh_xscale, dmh_yscale, dmh_xoffset, dmh_yoffset,deviceX(0), deviceY(0), deviceX(1), deviceY(1))); }
SDL_Surface *load_svg2(frame_t *frame, const char *name, int alpha) { cairo_surface_t *cairo_surf; cairo_status_t cairo_status; cairo_surface_type_t cairo_surf_type; SDL_Surface *sdl_surf; unsigned char *image_data; assert(frame->image_list_size >= 0); assert(frame->image_list_size < IMAGE_LIST_MAX); { char buf[1024]; snprintf(buf, 1024, "%s/%s", SVG_DIR, name); cairo_surf = cairo_svg_surface_create(name, 0, 0); cairo_status = cairo_surface_status(cairo_surf); if (cairo_status != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "ugh: unable to load SVG '%s' from '%s': status=%d: %s\n", name, buf, cairo_status, cairo_status_to_string(cairo_status)); exit(1); } if (!cairo_surf) { fprintf(stderr, "ugh: unable to create cairo SVG surface\n"); exit(1); } cairo_surf_type = cairo_surface_get_type(cairo_surf); fprintf(stderr, "cairo svg surf type: %d\n", cairo_surf_type); } image_data = cairo_image_surface_get_data(cairo_surf); if (!image_data) { fprintf(stderr, "ugh: unable to get cairo surface data\n"); exit(1); } cairo_surface_destroy(cairo_surf); assert(0); show_image_info(frame, sdl_surf, name, alpha); return sdl_surf; }
void dv_export_viewport() { dv_view_t * V = DVG->activeV; if (!V) { fprintf(stderr, "Warning: there is no active V to export.\n"); return; } dv_viewport_t * VP = V->mainVP; if (!VP) { fprintf(stderr, "Warning: there is no main VP for the active V.\n"); return; } cairo_surface_t * surface; /* resize to remove ruler areas */ double surface_width = VP->vpw - 2 * DVG->opts.ruler_width; double surface_height = VP->vph - 2 * DVG->opts.ruler_width; /* PNG */ surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surface_width, surface_height); dv_viewport_export_to_surface(VP, surface); cairo_surface_write_to_png(surface, "00dv.png"); fprintf(stdout, "Exported viewport %ld to 00dv.png\n", VP - DVG->VP); cairo_surface_destroy(surface); /* EPS */ /* surface = cairo_ps_surface_create("00dv.eps", surface_width, surface_height); cairo_ps_surface_set_eps(surface, TRUE); dv_viewport_export_to_surface(VP, surface); fprintf(stdout, "Exported viewport %ld to 00dv.eps\n", VP - DVG->VP); cairo_surface_destroy(surface); */ /* SVG */ surface = cairo_svg_surface_create("00dv.svg", surface_width, surface_height); dv_viewport_export_to_surface(VP, surface); fprintf(stdout, "Exported viewport %ld to 00dv.svg\n", VP - DVG->VP); cairo_surface_destroy(surface); /* PDF */ surface = cairo_pdf_surface_create("00dv.pdf", surface_width, surface_height); dv_viewport_export_to_surface(VP, surface); fprintf(stdout, "Exported viewport %ld to 00dv.pdf\n", VP - DVG->VP); cairo_surface_destroy(surface); return; }
void ofCairoRenderer::setup(string filename, Type type, bool multiPage_, bool b3D_){ switch(type){ case PDF: surface = cairo_pdf_surface_create(ofToDataPath(filename).c_str(),ofGetWidth(),ofGetHeight()); break; case SVG: surface = cairo_svg_surface_create(ofToDataPath(filename).c_str(),ofGetWidth(),ofGetHeight()); break; } cr = cairo_create(surface); viewportRect.set(0,0,ofGetWidth(),ofGetHeight()); viewport(viewportRect); page = 0; b3D = b3D_; multiPage = multiPage_; }
void render_page(page_t *page_meta, PopplerPage *page) {; cairo_t *cairoctx; cairo_surface_t *surface; double width, height; char outfilename[NAME_MAX]; poppler_page_get_size(page, &width, &height); snprintf(outfilename, sizeof outfilename - 1, "img-%d.svg", page_meta->pagenum); surface = cairo_svg_surface_create(outfilename, width, height); cairoctx = cairo_create(surface); if (cairoctx == NULL) ERROR("Cannot create a Cairo buffer"); poppler_page_render(page, cairoctx); cairo_surface_show_page(surface); cairo_surface_destroy(surface); cairo_destroy(cairoctx); }
int main (void) { cairo_test_context_t ctx; cairo_t *cr; const char *filename = "svg-surface.svg"; cairo_surface_t *surface; cairo_test_init (&ctx, "svg-surface"); if (! (cairo_test_is_target_enabled (&ctx, "svg11") || cairo_test_is_target_enabled (&ctx, "svg12"))) { cairo_test_fini (&ctx); return CAIRO_TEST_UNTESTED; } surface = cairo_svg_surface_create (filename, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); if (cairo_surface_status (surface)) { cairo_test_log (&ctx, "Failed to create svg surface for file %s: %s\n", filename, cairo_status_to_string (cairo_surface_status (surface))); cairo_test_fini (&ctx); return CAIRO_TEST_FAILURE; } cr = cairo_create (surface); draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS); cairo_show_page (cr); cairo_destroy (cr); cairo_surface_destroy (surface); printf ("svg-surface: Please check svg-surface.svg to make sure it looks happy.\n"); cairo_test_fini (&ctx); return CAIRO_TEST_SUCCESS; }
void ofCairoRenderer::setup(string filename, Type type, bool multiPage_, bool b3D_, ofRectangle _viewport){ if( _viewport.width == 0 || _viewport.height == 0 ){ _viewport.set(0, 0, ofGetWidth(), ofGetHeight()); } switch(type){ case PDF: surface = cairo_pdf_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); break; case SVG: surface = cairo_svg_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); break; } cr = cairo_create(surface); viewportRect = _viewport; viewport(viewportRect); page = 0; b3D = b3D_; multiPage = multiPage_; }
int main (void) { cairo_surface_t *surface; cairo_t *cr; surface = cairo_svg_surface_create ("image.svg", 504, 648); cr = cairo_create (surface); cairo_set_source_rgb (cr, 0, 0, 0); cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (cr, 40.0); cairo_move_to (cr, 10.0, 50.0); cairo_show_text (cr, "Hello world!"); cairo_show_page (cr); cairo_surface_destroy (surface); cairo_destroy (cr); return 0; }
int main (int argc, char*argv[]) { cairo_surface_t* svg = cairo_svg_surface_create (SVG_FILE, SVG_WIDTH, SVG_HEIGHT); cairo_t* cr = cairo_create (svg); double angle, x, y; cairo_set_line_width (cr, 10); cairo_move_to (cr, 250, 50); for (angle = 90.0; angle <= 810.0; angle += 144.0) { x = SVG_WIDTH/2 + SVG_RADIUS * cos (angle * M_PI / 180.0); y = SVG_HEIGHT/2 - SVG_RADIUS * sin (angle * M_PI / 180.0); cairo_line_to (cr, x, y); } cairo_close_path (cr); cairo_stroke (cr); cairo_destroy (cr); cairo_surface_destroy (svg); return 0; }
static cairo_t * create_cairo(struct objlist *obj, N_VALUE *inst, char *fname, int iw, int ih, int *err) { cairo_surface_t *ps; cairo_t *cairo; double w, h; int format, dpi, r; struct gra2cairo_local *local; #ifdef WINDOWS fname = g_locale_from_utf8(fname, -1, NULL, NULL, NULL); #else /* WINDOWS */ fname = g_filename_from_utf8(fname, -1, NULL, NULL, NULL); #endif /* WINDOWS */ if (fname == NULL) { *err = CAIRO_STATUS_NO_MEMORY; return NULL; } *err = 0; _getobj(obj, "format", inst, &format); _getobj(obj, "dpi", inst, &dpi); _getobj(obj, "_local", inst, &local); w = iw * dpi / 25.4 / 100; h = ih * dpi / 25.4 / 100; switch (format) { case TYPE_PS2: ps = cairo_ps_surface_create(fname, w, h); cairo_ps_surface_restrict_to_level(ps, CAIRO_PS_LEVEL_2); cairo_ps_surface_set_eps(ps, FALSE); break; case TYPE_PS3: ps = cairo_ps_surface_create(fname, w, h); cairo_ps_surface_restrict_to_level(ps, CAIRO_PS_LEVEL_3); cairo_ps_surface_set_eps(ps, FALSE); break; case TYPE_EPS2: ps = cairo_ps_surface_create(fname, w, h); cairo_ps_surface_restrict_to_level(ps, CAIRO_PS_LEVEL_2); cairo_ps_surface_set_eps(ps, TRUE); break; case TYPE_EPS3: ps = cairo_ps_surface_create(fname, w, h); cairo_ps_surface_restrict_to_level(ps, CAIRO_PS_LEVEL_3); cairo_ps_surface_set_eps(ps, TRUE); break; case TYPE_PDF: ps = cairo_pdf_surface_create(fname, w, h); break; case TYPE_SVG1_1: ps = cairo_svg_surface_create(fname, w, h); cairo_svg_surface_restrict_to_version(ps, CAIRO_SVG_VERSION_1_1); break; case TYPE_SVG1_2: ps = cairo_svg_surface_create(fname, w, h); cairo_svg_surface_restrict_to_version(ps, CAIRO_SVG_VERSION_1_2); break; case TYPE_PNG: ps = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h); break; #ifdef CAIRO_HAS_WIN32_SURFACE case TYPE_EMF: ps = open_emf(dpi, fname); if (ps == NULL) { g_free(fname); return NULL; } break; #endif /* CAIRO_HAS_WIN32_SURFACE */ default: ps = cairo_ps_surface_create(fname, w, h); } g_free(fname); r = cairo_surface_status(ps); if (r != CAIRO_STATUS_SUCCESS) { *err = r; cairo_surface_destroy(ps); return NULL; } cairo = cairo_create(ps); /* cairo_create() references target, so you can immediately call cairo_surface_destroy() on it */ cairo_surface_destroy(ps); r = cairo_status(cairo); if (r != CAIRO_STATUS_SUCCESS) { *err = r; cairo_destroy(cairo); return NULL; } switch (format) { case TYPE_PNG: cairo_set_source_rgb(cairo, 1, 1, 1); cairo_paint(cairo); cairo_new_path(cairo); break; } return cairo; }
/*================================================================================== psopen - opens a document for writing ==================================================================================*/ void psopen_(char *fname, int fnamelen) { char *outFileName; char outputType[255]; char pageWidthString[255]; char pageHeightString[255]; /* Set debug status based upon presence of file named 'debug_yes' in directory */ FILE *debugFile = fopen("debug_yes", "r"); if (debugFile == NULL) { dmh_debug = 0; } else { dmh_debug = 1; fclose(debugFile); } fname[fnamelen]='\0'; fname = trim(fname); outFileName = malloc((strlen(fname) + 50) * sizeof(char)); strcpy(outFileName, fname); DEBUGPRINT(("Found file name:%s of length: %lu\n", fname, strlen(fname))); dmh_fileNameRoot = malloc(255*sizeof(char)); strcpy(dmh_fileNameRoot, fname); /* Look for plot options file to specify what sort of output we want: SVG/PS/PDF Could use the Perplex routine to do this, but calling into C from fortran is bad enough. */ /* Default Values - If these get changed, then need to change the display in pscom_new.f as well */ if (!getOptionForKey("plot_output_type", outputType, 255)) { strcpy(outputType, "PDF"); } if (get2OptionsForKey("page_size", pageWidthString, pageHeightString, 255)) { DEBUGPRINT(("Found page size strings: (%s, %s)\n", pageWidthString, pageHeightString)); sscanf(pageWidthString, "%i", &dmh_pageWidth); sscanf(pageHeightString, "%i", &dmh_pageHeight); DEBUGPRINT(("Found page size values: (%i, %i)\n", dmh_pageWidth, dmh_pageHeight)); } else { dmh_pageWidth = 612; dmh_pageHeight = 792; } dmh_fontFace = malloc(255*sizeof(char)); if (!getCompleteOptionForKey("new_font", dmh_fontFace, 255)) { strcpy(dmh_fontFace, "Arial"); } if (!strcmp(outputType, "SVG") || !strcmp(outputType, "svg")) { dmh_outputFileType = SVGTYPE; strcat(outFileName, ".svg"); dmh_surf = cairo_svg_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); } else if (!strcmp(outputType, "PS") || !strcmp(outputType, "ps")) { dmh_outputFileType = PSTYPE; strcat(outFileName, ".ps"); dmh_surf = cairo_ps_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); cairo_ps_surface_set_eps (dmh_surf, 1); } else { /* PDF is the default if nothing is specified in the plot options file */ dmh_outputFileType = PDFTYPE; strcat(outFileName, ".pdf"); dmh_surf = cairo_pdf_surface_create (outFileName, dmh_pageWidth, dmh_pageHeight); } dmh_cr = cairo_create (dmh_surf); cairo_identity_matrix(dmh_cr); cairo_set_line_join(dmh_cr, CAIRO_LINE_JOIN_ROUND); dmh_min_tracked_x = DBL_MAX; free(outFileName); }
///////////////////////////////////////////////////////////////////////////// // SurfaceSvg SurfaceSvg::SurfaceSvg( const std::string &filePath, uint32_t width, uint32_t height ) : SurfaceBase( width, height ) { mCairoSurface = cairo_svg_surface_create( filePath.c_str(), width, height ); }
void ExportWindow_init( ExportWindow* ew, Scene* scene ) { GtkWidget *dialog = gtk_file_chooser_dialog_new ("Exporter la scene", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); GtkFileFilter* filtre = gtk_file_filter_new(); gtk_file_filter_add_pattern( GTK_FILE_FILTER (filtre), "*.png"); gtk_file_filter_add_pattern( GTK_FILE_FILTER (filtre), "*.pdf"); gtk_file_filter_add_pattern( GTK_FILE_FILTER (filtre), "*.svg"); gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(dialog), GTK_FILE_FILTER(filtre) ); ew->comboBox = gtk_combo_box_new_text(); gtk_combo_box_append_text( GTK_COMBO_BOX( ew->comboBox ), "png" ); gtk_combo_box_append_text( GTK_COMBO_BOX( ew->comboBox ), "svg" ); gtk_combo_box_append_text( GTK_COMBO_BOX( ew->comboBox ), "pdf" ); gtk_file_chooser_set_extra_widget( GTK_FILE_CHOOSER(dialog), ew->comboBox ); gtk_combo_box_set_active( GTK_COMBO_BOX( ew->comboBox ), 0 ); g_signal_emit_by_name( GTK_OBJECT( ew->comboBox ), "changed", NULL ); if( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT ) { char *filename = NULL; filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); filename = (char*)realloc( filename, ( strlen(filename) + 4 ) * sizeof( char ) ); filename = strcat( filename,"." ); filename = strcat( filename, gtk_combo_box_get_active_text( GTK_COMBO_BOX( ew->comboBox ) ) ); printf( "Exportation : %s\n", filename ); cairo_surface_t *surface = NULL; /*Surface sur laquelle on va dessiner*/ cairo_t *cr = NULL; /*COntexte associé à la surface */ if( strcmp( filename, "" ) != 0 ) /*On test si le nom a été renseigné */ { if( strcmp( gtk_combo_box_get_active_text( GTK_COMBO_BOX( ew->comboBox ) ), "png" ) == 0 ) { surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, scene->dWidth, scene->dHeight); cr = cairo_create (surface); Scene_clear_scene(scene , cr, 1, 1, 1); /* Nettoyage de la scene */ Scene_dessiner_scene( scene, cr ); /*Dessin de tous les objets*/ cairo_surface_write_to_png(surface, filename ); /* Projection sur une surfae PNG*/ cairo_surface_destroy(surface); g_free (filename); gtk_widget_destroy (dialog); } else if(strcmp( gtk_combo_box_get_active_text( GTK_COMBO_BOX( ew->comboBox ) ), "pdf" ) == 0 ) { surface = cairo_pdf_surface_create( filename, scene->dWidth, scene->dHeight); cr = cairo_create(surface); Scene_clear_scene(scene , cr, 1, 1, 1); /* Nettoyage de la scene */ Scene_dessiner_scene( scene, cr ); /*Dessin de tous les objets*/ cairo_surface_destroy(surface); g_free (filename); gtk_widget_destroy (dialog); } else if(strcmp( gtk_combo_box_get_active_text( GTK_COMBO_BOX( ew->comboBox ) ), "svg" ) == 0 ) { surface = cairo_svg_surface_create( filename, scene->dWidth, scene->dHeight ); cr = cairo_create(surface); Scene_clear_scene(scene , cr, 1, 1, 1); /* Nettoyage de la scene */ Scene_dessiner_scene( scene, cr ); /*Dessin de tous les objets*/ cairo_surface_destroy(surface); g_free (filename); gtk_widget_destroy (dialog); } cairo_destroy( cr ); } else { GtkWidget* avertissement = gtk_message_dialog_new( NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Veuillez entrer un nom de fichier" ); if( gtk_dialog_run ( GTK_DIALOG ( avertissement ) ) == GTK_RESPONSE_OK ) { gtk_widget_destroy( avertissement ); } } } else { gtk_widget_destroy (dialog); } }
void ofCairoRenderer::setup(string _filename, Type _type, bool multiPage_, bool b3D_, ofRectangle _viewport){ if( _viewport.width == 0 || _viewport.height == 0 ){ _viewport.set(0, 0, ofGetViewportWidth(), ofGetViewportHeight()); } filename = _filename; type = _type; streamBuffer.clear(); if(type == FROM_FILE_EXTENSION){ string ext = ofFilePath::getFileExt(filename); if(ofToLower(ext)=="svg"){ type = SVG; }else if(ofToLower(ext)=="pdf"){ type = PDF; }else{ // default to image type = IMAGE; } } if(filename != "") { switch(type) { case PDF: case SVG: case IMAGE: ofFilePath::createEnclosingDirectory(filename); case FROM_FILE_EXTENSION: break; } } switch(type){ case PDF: if(filename==""){ surface = cairo_pdf_surface_create_for_stream(&ofCairoRenderer::stream_function,this,_viewport.width, _viewport.height); }else{ surface = cairo_pdf_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); } break; case SVG: if(filename==""){ surface = cairo_svg_surface_create_for_stream(&ofCairoRenderer::stream_function,this,_viewport.width, _viewport.height); }else{ surface = cairo_svg_surface_create(ofToDataPath(filename).c_str(),_viewport.width, _viewport.height); } break; case IMAGE: imageBuffer.allocate(_viewport.width, _viewport.height, OF_PIXELS_BGRA); imageBuffer.set(0); surface = cairo_image_surface_create_for_data(imageBuffer.getData(),CAIRO_FORMAT_ARGB32,_viewport.width, _viewport.height,_viewport.width*4); break; case FROM_FILE_EXTENSION: ofLogFatalError("ofCairoRenderer") << "setup(): couldn't determine type from extension for filename: \"" << _filename << "\"!"; break; default: ofLogError("ofCairoRenderer") << "setup(): encountered unknown type for filename \"" << _filename << "\""; break; } cr = cairo_create(surface); cairo_set_antialias(cr,CAIRO_ANTIALIAS_SUBPIXEL); viewportRect = _viewport; originalViewport = _viewport; viewport(viewportRect); page = 0; b3D = b3D_; multiPage = multiPage_; }
/* Takes a filename of the output file, grid structure and maximum image size (width or height) Writes the grid image to the file */ int create_image(const char *filename, double max_size, grid *grd, cl_list *clusters) { cl_list *current = clusters; int_list *head; int i, j; int color, site, count = 0; /* grid structure aspect ratio */ double aspect = (double)grd->width / (double)grd->height; /* image parameters */ double img_width = (aspect > 1) ? max_size : max_size * aspect; double img_height = (aspect < 1) ? max_size : max_size / aspect; double site_size = img_width/grd->width; double line_width = site_size/50.0; /* include border to the image width */ double border_width = max_size/50.0; img_width = img_width + 2 * border_width; img_height = img_height + 2 * border_width; cairo_surface_t *surface; cairo_t *cr; /* extract an extension from filename */ char *ext = strrchr(filename, '.'); if (!ext) /* no extension */ { printf("File extension missing for the output image name: '%s'\n", filename); return 1; } else ext = ext + 1; if (strcmp(ext, "svg") == 0) surface = cairo_svg_surface_create(filename, img_width, img_height); else if (strcmp(ext, "png") == 0) surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, img_width, img_height); else { printf("Unsupported output image format: '%s'\n", ext); return 1; } cr = cairo_create(surface); /* set background */ cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); cairo_rectangle(cr, 0, 0, img_width, img_height); cairo_fill(cr); /* line settings */ cairo_set_line_width(cr, line_width); cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER); /* draw grid sites */ while (current != NULL) { color = generate_color(count + 1); head = current->item->head; site = (current->item->upper_boundary && current->item->lower_boundary) ? SITE_FULL : SITE_OPEN; while (head != NULL) { i = head->item / grd->width; j = head->item % grd->width; draw_site(cr, border_width + j * site_size, border_width + i * site_size, site_size, site); head = head->next; } current = current->next; count = count + 1; } /* draw block sites */ //for (i = 0; i < grd->height; i++) // for (j = 0; j < grd->width; j++) // if (grd->cells[i*grd->width + j] == SITE_BLOCK) // draw_site(cr, border_width + j * site_size, // border_width + i * site_size, // site_size, grd->cells[i*grd->width + j]); if (strcmp(ext, "png") == 0) cairo_surface_write_to_png(surface, filename); cairo_destroy(cr); cairo_surface_destroy(surface); return 0; }