示例#1
0
static JSBool
gjs_cairo_ps_surface_constructor(JSContext *context,
                                 JSObject  *obj,
                                 uintN      argc,
                                 jsval     *argv,
                                 jsval     *retval)
{
    char *filename;
    double width, height;
    cairo_surface_t *surface;

    if (!gjs_check_constructing(context))
        return JS_FALSE;

    if (!gjs_parse_args(context, "PSSurface", "sff", argc, argv,
                        "filename", &filename,
                        "width", &width,
                        "height", &height))
        return JS_FALSE;

    surface = cairo_ps_surface_create(filename, width, height);

    if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
                                "surface")) {
        g_free(filename);
        return JS_FALSE;
    }

    gjs_cairo_surface_construct(context, obj, surface);
    cairo_surface_destroy(surface);
    g_free(filename);

    return JS_TRUE;
}
示例#2
0
static PyObject *
ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    const char *filename;
    double width_in_points, height_in_points;
    cairo_surface_t *surface;
    PyObject *o;

    if (!PyArg_ParseTuple(args, "sdd:PSSurface.__new__",
			  &filename, &width_in_points, &height_in_points))
	return NULL;

    o = type->tp_alloc(type, 0);
    if (o) {
	surface = cairo_ps_surface_create (filename, width_in_points,
					   height_in_points);
	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
	    cairo_surface_destroy (surface);
	    Py_DECREF(o);
	    return NULL;
	}
	((PycairoPSSurface *)o)->surface = surface;
    }
    return o;
}
static PyObject *
ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    double width_in_points, height_in_points;
    PyObject *file, *writer;

    if (!PyArg_ParseTuple(args, "Odd:PSSurface.__new__",
			  &file, &width_in_points, &height_in_points))
	return NULL;

    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
	/* string (filename) argument */
	return PycairoSurface_FromSurface (
                  cairo_ps_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,
"PSSurface 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_ps_surface_create_for_stream (_write_func, file,
                   width_in_points, height_in_points),
	       file);
}
示例#4
0
/////////////////////////////////////////////////////////////////////////////
// SurfaceEps
SurfaceEps::SurfaceEps( const std::string &filePath, double widthInPoints, double heightInPoints, bool enableLevel3 )
	: SurfaceBase( (int32_t)widthInPoints, (int32_t)heightInPoints )
{
	mCairoSurface = cairo_ps_surface_create( filePath.c_str(), widthInPoints, heightInPoints ); 
	cairo_ps_surface_set_eps( mCairoSurface, TRUE );
	cairo_ps_surface_restrict_to_level( mCairoSurface, ( enableLevel3 ) ? CAIRO_PS_LEVEL_3 : CAIRO_PS_LEVEL_2 );
}
示例#5
0
文件: export.c 项目: SayCV/geda-gaf
/* Worker function used by both export_ps and export_eps */
static void
export_postscript (gboolean is_eps)
{
  cairo_surface_t *surface;
  cairo_rectangle_t extents;
  cairo_matrix_t mtx;
  cairo_t *cr;
  GList *iter;

  /* Create a surface. To begin with, we don't know the size. */
  surface = cairo_ps_surface_create (settings.outfile, 1, 1);
  cairo_ps_surface_set_eps (surface, is_eps);
  cr = cairo_create (surface);
  g_object_set (renderer, "cairo-context", cr, NULL);

  for (iter = geda_list_get_glist (toplevel->pages);
       iter != NULL;
       iter = g_list_next (iter)) {
    PAGE *page = (PAGE *) iter->data;

    export_layout_page (page, &extents, &mtx);
    cairo_ps_surface_set_size (surface, extents.width, extents.height);
    cairo_set_matrix (cr, &mtx);
    export_draw_page (page);
    cairo_show_page (cr);
  }

  cairo_surface_finish (surface);
  export_cairo_check_error (cairo_surface_status (surface));
}
示例#6
0
// cairo_public cairo_surface_t *
// cairo_ps_surface_create (const char		*filename,
//              double			 width_in_points,
//              double			 height_in_points);
static int l_cairo_ps_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_ps_surface_create(filename, width_in_points, height_in_points);
    lua_pushlightuserdata(L, v);
    return 1;
}
示例#7
0
文件: lcairo.c 项目: elq/torch5
static int lcairo_create_ps_surface(lua_State *L){
#ifdef CAIRO_HAS_PS_SURFACE
  const char *filename=luaL_checkstring(L,1);
  double w=luaL_checknumber(L,2);
  double h=luaL_checknumber(L,3);
  cairo_surface_t *p = (cairo_surface_t*)cairo_ps_surface_create(filename,w,h);
  luaT_pushudata(L, p, tSurface_id); 
  return 1;
#else
  luaL_error(L,"Installed Cairo does not support PS");
  return 0;
#endif
}
static int new_PsSurface (lua_State *L)
{
    lua_remove(L, 1); // remove cairo.PsSurface

    //FIXME
    //{"create_for_stream",             l_cairo_ps_surface_create_for_stream},

    //{"create",                        l_cairo_ps_surface_create},
    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 *cs = cairo_ps_surface_create(filename, width_in_points, height_in_points);

    return new_Surface(L, LUACAIRO ".PsSurface.mt", cs, CAIRO_SURFACE_TYPE_PS, 1);
}
示例#9
0
/*==================================================================================
 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)));
}
示例#10
0
void CairoBox::out_eps(const char* filename, int wpts, int hpts)
  {
  cairo_surface_t* surface;
  cairo_t*         cr;

  surface = cairo_ps_surface_create (filename, wpts, hpts);
  cairo_ps_surface_set_eps(surface, 1);
  cr      = cairo_create (surface);
  cairo_set_source_rgb (cr, 0, 0, 0);

  // user coordinates -- see cairo_ps_surface_create 
  graphic(cr, 0, 0, wpts, hpts);          

  cairo_show_page (cr);
  cairo_destroy (cr);
  cairo_surface_destroy (surface);
  return;
  }
int main( int argc, const char *argv[] )
{
    cairo_surface_t *cairoSurface;
    cairo_t         *cairoContext;

    cairoSurface = cairo_ps_surface_create( "ext-cairo-test.ps", 720, 540 );
    cairoContext = cairo_create( cairoSurface );

    plparseopts( &argc, argv, PL_PARSE_FULL );

    plsdev( "extcairo" );
    plinit();
    pl_cmd( PLESC_DEVINIT, cairoContext );
    plenv( 0.0, 1.0, 0.0, 1.0, 1, 0 );
    pllab( "x", "y", "title" );
    plend();

    cairo_destroy( cairoContext );
    cairo_surface_destroy( cairoSurface );
    exit( 0 );
}
示例#12
0
void Shape::Render(const char *path){
  if(geom_ == NULL) return;
  cairo_surface_t *surface = cairo_ps_surface_create(path, width_, height_);
  if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
    std::cout << cairo_status_to_string(cairo_surface_status(surface)) << std::endl;
    exit(1);
    return;
  }

  ctx_ = cairo_create(surface);

  OGREnvelope env;
  geom_->getEnvelope(&env);
  cairo_matrix_t mat;
  matrix_init(&mat, width_, height_, &env);
  cairo_set_matrix(ctx_, &mat);

  Dispatch(geom_);

  cairo_show_page(ctx_);
  cairo_destroy(ctx_);
  cairo_surface_destroy(surface);
}
示例#13
0
static int gt_sketch_page_runner(GT_UNUSED int argc,
                                 const char **argv,
                                 int parsed_args,
                                 void *tool_arguments,
                                 GtError *err)
{
  SketchPageArguments *arguments = tool_arguments;
  int had_err = 0;
  GtFeatureIndex *features = NULL;
  GtRange qry_range, sequence_region_range;
  GtStyle *sty = NULL;
  GtStr *prog, *gt_style_file;
  GtDiagram *d = NULL;
  GtLayout *l = NULL;
  GtBioseq *bioseq = NULL;
  GtCanvas *canvas = NULL;
  const char *seqid = NULL, *outfile;
  unsigned long start, height, num_pages = 0;
  double offsetpos, usable_height;
  cairo_surface_t *surf = NULL;
  cairo_t *cr = NULL;
  GtTextWidthCalculator *twc;
  gt_error_check(err);

  features = gt_feature_index_memory_new();

  if (cairo_version() < CAIRO_VERSION_ENCODE(1, 8, 6))
    gt_warning("Your cairo library (version %s) is older than version 1.8.6! "
               "These versions contain a bug which may result in "
               "corrupted PDF output!", cairo_version_string());

  /* get style */
  sty = gt_style_new(err);
  if (gt_str_length(arguments->stylefile) == 0)
  {
    prog = gt_str_new();
    gt_str_append_cstr_nt(prog, argv[0],
                          gt_cstr_length_up_to_char(argv[0], ' '));
    gt_style_file = gt_get_gtdata_path(gt_str_get(prog), err);
    gt_str_delete(prog);
    gt_str_append_cstr(gt_style_file, "/sketch/default.style");
  }
  else
  {
    gt_style_file = gt_str_ref(arguments->stylefile);
  }
  had_err = gt_style_load_file(sty, gt_str_get(gt_style_file), err);

  outfile = argv[parsed_args];
  if (!had_err)
  {
    /* get features */
    had_err = gt_feature_index_add_gff3file(features, argv[parsed_args+1], err);
     if (!had_err && gt_str_length(arguments->seqid) == 0) {
      seqid = gt_feature_index_get_first_seqid(features);
      if (seqid == NULL)
      {
        gt_error_set(err, "GFF input file must contain a sequence region!");
        had_err = -1;
      }
    }
    else if (!had_err
               && !gt_feature_index_has_seqid(features,
                                              gt_str_get(arguments->seqid)))
    {
      gt_error_set(err, "sequence region '%s' does not exist in GFF input file",
                   gt_str_get(arguments->seqid));
      had_err = -1;
    }
    else if (!had_err)
      seqid = gt_str_get(arguments->seqid);
  }

  /* set text */
  if (gt_str_length(arguments->text) == 0)
  {
    gt_str_delete(arguments->text);
    arguments->text = gt_str_new_cstr(argv[parsed_args+1]);
  }

  if (!had_err)
  {
    /* set display range */
    gt_feature_index_get_range_for_seqid(features, &sequence_region_range,
                                         seqid);
    qry_range.start = (arguments->range.start == GT_UNDEF_ULONG ?
                         sequence_region_range.start :
                         arguments->range.start);
    qry_range.end   = (arguments->range.end == GT_UNDEF_ULONG ?
                         sequence_region_range.end :
                         arguments->range.end);

    /* set output format */
    if (strcmp(gt_str_get(arguments->format), "pdf") == 0)
    {
      surf = cairo_pdf_surface_create(outfile,
                                      mm_to_pt(arguments->pwidth),
                                      mm_to_pt(arguments->pheight));
    }
    else if (strcmp(gt_str_get(arguments->format), "ps") == 0)
    {
      surf =  cairo_ps_surface_create(outfile,
                                      mm_to_pt(arguments->pwidth),
                                      mm_to_pt(arguments->pheight));
    }
    gt_log_log("created page with %.2f:%.2f dimensions\n",
                                                  mm_to_pt(arguments->pwidth),
                                                  mm_to_pt(arguments->pheight));

    offsetpos = TEXT_SPACER + arguments->theight + TEXT_SPACER;
    usable_height = mm_to_pt(arguments->pheight)
                              - arguments->theight
                              - arguments->theight
                              - 4*TEXT_SPACER;

    if (gt_str_length(arguments->seqfile) > 0) {
      bioseq = gt_bioseq_new(gt_str_get(arguments->seqfile), err);
    }

    cr = cairo_create(surf);
    cairo_set_font_size(cr, 8);
    twc = gt_text_width_calculator_cairo_new(cr, sty);
    for (start = qry_range.start; start <= qry_range.end;
         start += arguments->width)
    {
      GtRange single_range;
      GtCustomTrack *ct = NULL;
      const char *seq;
      single_range.start = start;
      single_range.end = start + arguments->width;

      if (had_err)
        break;

      d = gt_diagram_new(features, seqid, &single_range, sty, err);
      if (!d) {
        had_err = -1;
        break;
      }
      if (bioseq) {
        seq = gt_bioseq_get_sequence(bioseq, 0);
        ct = gt_custom_track_gc_content_new(seq,
                                      gt_bioseq_get_sequence_length(bioseq, 0),
                                      800, 70, 0.4, true);
        gt_diagram_add_custom_track(d, ct);
      }

      l = gt_layout_new_with_twc(d, mm_to_pt(arguments->width), sty, twc, err);
      had_err = gt_layout_get_height(l, &height, err);
      if (!had_err) {
        if (gt_double_smaller_double(usable_height - 10 - 2*TEXT_SPACER
              - arguments->theight, offsetpos + height))
        {
            draw_header(cr, gt_str_get(arguments->text), argv[parsed_args+1],
                        seqid, num_pages, mm_to_pt(arguments->pwidth),
                        mm_to_pt(arguments->pheight),
                        arguments->theight);
          cairo_show_page(cr);
          offsetpos = TEXT_SPACER + arguments->theight + TEXT_SPACER;
          num_pages++;
        }
        canvas = gt_canvas_cairo_context_new(sty,
                                             cr,
                                             offsetpos,
                                             mm_to_pt(arguments->pwidth),
                                             height,
                                             NULL,
                                             err);
        if (!canvas)
          had_err = -1;
        offsetpos += height;
        if (!had_err)
          had_err = gt_layout_sketch(l, canvas, err);
      }
      gt_canvas_delete(canvas);
      gt_layout_delete(l);
      gt_diagram_delete(d);
      if (ct)
        gt_custom_track_delete(ct);
    }
    draw_header(cr, gt_str_get(arguments->text), argv[parsed_args+1], seqid,
                num_pages, mm_to_pt(arguments->pwidth),
                mm_to_pt(arguments->pheight),
                arguments->theight);
    cairo_show_page(cr);
    num_pages++;
    gt_log_log("finished, should be %lu pages\n", num_pages);
    gt_text_width_calculator_delete(twc);
    cairo_destroy(cr);
    cairo_surface_flush(surf);
    cairo_surface_finish(surf);
    cairo_surface_destroy(surf);
    cairo_debug_reset_static_data();
    if (bioseq)
      gt_bioseq_delete(bioseq);
    gt_style_delete(sty);
    gt_str_delete(gt_style_file);
    gt_feature_index_delete(features);
  }
  return had_err;
}
示例#14
0
int
main (void)
{
    cairo_test_context_t ctx;
    cairo_surface_t *surface;
    cairo_t *cr;
    cairo_status_t status;
    const char *filename;
    size_t i;
    char dsc[255];

    cairo_test_init (&ctx, "ps-features");
    if (! cairo_test_is_target_enabled (&ctx, "ps")) {
	cairo_test_fini (&ctx);
	return CAIRO_TEST_UNTESTED;
    }

    filename = "ps-features.ps";

    /* We demonstrate that the initial size doesn't matter (we're
     * passing 0,0), if we use cairo_ps_surface_set_size on the first
     * page. */
    surface = cairo_ps_surface_create (filename, 0, 0);

    cairo_ps_surface_dsc_comment (surface, "%%Title: ps-features");
    cairo_ps_surface_dsc_comment (surface, "%%Copyright: Copyright (C) 2006 Red Hat, Inc.");

    cairo_ps_surface_dsc_begin_setup (surface);
    cairo_ps_surface_dsc_comment (surface, "%%IncludeFeature: *PageSize letter");
    cairo_ps_surface_dsc_comment (surface, "%%IncludeFeature: *MediaColor White");

    cr = cairo_create (surface);

    cairo_select_font_face (cr, "Bitstream Vera Sans",
			    CAIRO_FONT_SLANT_NORMAL,
			    CAIRO_FONT_WEIGHT_NORMAL);
    cairo_set_font_size (cr, TEXT_SIZE);

    for (i=0; i < ARRAY_SIZE(pages); i++) {
	cairo_ps_surface_set_size (surface,
				   pages[i].width_in_points,
				   pages[i].height_in_points);
	cairo_ps_surface_dsc_begin_page_setup (surface);
	snprintf (dsc, 255, "%%IncludeFeature: *PageSize %s", pages[i].page_size_alias);
	cairo_ps_surface_dsc_comment (surface, dsc);
	if (i % 2) {
	    snprintf (dsc, 255, "%%IncludeFeature: *MediaType Glossy");
	    cairo_ps_surface_dsc_comment (surface, dsc);
	}

	cairo_move_to (cr, TEXT_SIZE, TEXT_SIZE);
	cairo_show_text (cr, pages[i].page_size);
	cairo_show_text (cr, " - ");
	cairo_show_text (cr, pages[i].orientation);
	cairo_show_page (cr);
    }

    status = cairo_status (cr);

    cairo_destroy (cr);
    cairo_surface_destroy (surface);

    if (status) {
	cairo_test_log (&ctx, "Failed to create ps surface for file %s: %s\n",
			filename, cairo_status_to_string (status));
	return CAIRO_TEST_FAILURE;
    }

    printf ("ps-features: Please check %s to ensure it looks/prints correctly.\n", filename);

    cairo_test_fini (&ctx);

    return CAIRO_TEST_SUCCESS;
}
示例#15
0
文件: cairoBM.c 项目: radfordneal/pqR
static Rboolean
BM_Open(pDevDesc dd, pX11Desc xd, int width, int height)
{
    char buf[PATH_MAX];
    cairo_status_t res;
    if (xd->type == PNG || xd->type == JPEG ||
	xd->type == TIFF || xd->type == BMP ||
        xd->type == PNGdirect) {
	xd->cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
					    xd->windowWidth,
					    xd->windowHeight);
        res = cairo_surface_status(xd->cs);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        xd->cc = cairo_create(xd->cs);
        res = cairo_status(xd->cc);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        cairo_set_operator(xd->cc, CAIRO_OPERATOR_OVER);
        cairo_reset_clip(xd->cc);
        cairo_set_antialias(xd->cc, xd->antialias);
    }
#ifdef HAVE_CAIRO_SVG
    else if(xd->type == SVG) {
        snprintf(buf, PATH_MAX, xd->filename, xd->npages + 1);
        xd->cs = cairo_svg_surface_create(R_ExpandFileName(buf),
                                          (double)xd->windowWidth,
                                          (double)xd->windowHeight);
        res = cairo_surface_status(xd->cs);
        if (res != CAIRO_STATUS_SUCCESS) {
            xd->cs = NULL;
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        if(xd->onefile)
            cairo_svg_surface_restrict_to_version(xd->cs, CAIRO_SVG_VERSION_1_2);
        xd->cc = cairo_create(xd->cs);
        res = cairo_status(xd->cc);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        cairo_set_antialias(xd->cc, xd->antialias);
    }
#endif
#ifdef HAVE_CAIRO_PDF
    else if(xd->type == PDF) {
        snprintf(buf, PATH_MAX, xd->filename, xd->npages + 1);
        xd->cs = cairo_pdf_surface_create(R_ExpandFileName(buf),
                                          (double)xd->windowWidth,
                                          (double)xd->windowHeight);
        res = cairo_surface_status(xd->cs);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        cairo_surface_set_fallback_resolution(xd->cs, xd->fallback_dpi,
                                              xd->fallback_dpi);
        xd->cc = cairo_create(xd->cs);
        res = cairo_status(xd->cc);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        cairo_set_antialias(xd->cc, xd->antialias);
    }
#endif
#ifdef HAVE_CAIRO_PS
    else if(xd->type == PS) {
        snprintf(buf, PATH_MAX, xd->filename, xd->npages + 1);
        xd->cs = cairo_ps_surface_create(R_ExpandFileName(buf),
                                         (double)xd->windowWidth,
                                         (double)xd->windowHeight);
        res = cairo_surface_status(xd->cs);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
// We already require >= 1.2
#if CAIRO_VERSION_MAJOR > 2 || CAIRO_VERSION_MINOR >= 6
        if(!xd->onefile)
            cairo_ps_surface_set_eps(xd->cs, TRUE);
#endif
        cairo_surface_set_fallback_resolution(xd->cs, xd->fallback_dpi,
                                              xd->fallback_dpi);
        xd->cc = cairo_create(xd->cs);
        res = cairo_status(xd->cc);
        if (res != CAIRO_STATUS_SUCCESS) {
            warning("cairo error '%s'", cairo_status_to_string(res));
            return FALSE;
        }
        cairo_set_antialias(xd->cc, xd->antialias);
    }
#endif
    else
	error(_("unimplemented cairo-based device"));

    return TRUE;
}
示例#16
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;
}
static cairo_surface_t *
_cairo_boilerplate_ps_create_surface (const char		*name,
				      cairo_content_t		 content,
				      cairo_ps_level_t		 level,
				      double			 width,
				      double			 height,
				      double			 max_width,
				      double			 max_height,
				      cairo_boilerplate_mode_t	 mode,
				      int			 id,
				      void		       **closure)
{
    ps_target_closure_t *ptc;
    cairo_surface_t *surface;
    cairo_status_t status;

    /* Sanitize back to a real cairo_content_t value. */
    if (content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED)
	content = CAIRO_CONTENT_COLOR_ALPHA;

    *closure = ptc = xmalloc (sizeof (ps_target_closure_t));

    xasprintf (&ptc->filename, "%s.out.ps", name);
    xunlink (ptc->filename);

    ptc->level = level;
    ptc->width = ceil (width);
    ptc->height = ceil (height);

    surface = cairo_ps_surface_create (ptc->filename, width, height);
    if (cairo_surface_status (surface))
	goto CLEANUP_FILENAME;

    cairo_ps_surface_restrict_to_level (surface, level);
    _cairo_boilerplate_ps_surface_set_creation_date (surface, 0);
    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,
						ptc->width, ptc->height);
	if (cairo_surface_status (surface))
	    goto CLEANUP_TARGET;
    } else {
	ptc->target = NULL;
    }

    status = cairo_surface_set_user_data (surface, &ps_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;
}
示例#18
0
文件: Board.cpp 项目: lprovot/DGtal
void
Board::saveCairo( const char * filename, CairoType type, double pageWidth, double pageHeight, double margin ) const
{
  cairo_surface_t *surface;
  cairo_t *cr;
  
  double cairoWidth, cairoHeight;
  
  TransformCairo transform;
  Rect box = boundingBox();

  bool clipping = _clippingPath.size() > 2;
  if ( clipping )
    box = box && _clippingPath.boundingBox();
  transform.setBoundingBox( box, pageWidth, pageHeight, margin );
  
  if ( pageWidth > 0 && pageHeight > 0 )
  {
    cairoWidth = pageWidth;
    cairoHeight = pageHeight;
  }
  else
  {
    cairoWidth = box.width;
    cairoHeight = box.height;
  }
  
  switch (type)
  {
  case CairoPDF:
      surface = cairo_pdf_surface_create (filename, cairoWidth, cairoHeight); break;
  case CairoPS:
      surface = cairo_ps_surface_create (filename, cairoWidth, cairoHeight); break;
  case CairoEPS:
    surface = cairo_ps_surface_create (filename, cairoWidth, cairoHeight); 
    cairo_ps_surface_set_eps(surface, true); break;
  case CairoSVG:
      surface = cairo_svg_surface_create (filename, cairoWidth, cairoHeight); break;
    case CairoPNG:
    default:
      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, cairoWidth, cairoHeight);
  }
  
  cr = cairo_create (surface);
  
  /* For 1.0 x 1.0 coordinate space */
  //cairo_scale (cr, cairoWidth, cairoHeight);
  
  //temp: http://zetcode.com/tutorials/cairographicstutorial/basicdrawing/
  //temp: http://www.graphviz.org/pub/scm/graphviz-cairo/plugin/cairo/gvrender_cairo.c
      
  // Draw the background color if needed.
  if ( _backgroundColor != Color::None ) { 
    Rectangle r( box, Color::None, _backgroundColor, 0.0 );
    r.flushCairo( cr, transform );
  }
  
  // Draw the shapes.
  std::vector< Shape* > shapes = _shapes;
  stable_sort( shapes.begin(), shapes.end(), shapeGreaterDepth );
  std::vector< Shape* >::const_iterator i = shapes.begin();
  std::vector< Shape* >::const_iterator end = shapes.end();
  while ( i != end ) {
    (*i)->flushCairo( cr, transform );
    ++i;
  }
  
  if (type==CairoPNG)
    cairo_surface_write_to_png (surface, filename);
      
  cairo_destroy (cr);
  cairo_surface_destroy (surface);
}
示例#19
0
static void BM_NewPage(const pGEcontext gc, pDevDesc dd)
{
    pX11Desc xd = (pX11Desc) dd->deviceSpecific;
    char buf[PATH_MAX];
    cairo_status_t res;

    xd->npages++;
    if (xd->type == PNG || xd->type == JPEG || xd->type == BMP) {
	if (xd->npages > 1) {
	    /* try to preserve the page we do have */
	    BM_Close_bitmap(xd);
	    if (xd->fp) fclose(xd->fp);
	}
	snprintf(buf, PATH_MAX, xd->filename, xd->npages);
	xd->fp = R_fopen(R_ExpandFileName(buf), "wb");
	if (!xd->fp)
	    error(_("could not open file '%s'"), buf);
    }
    else if(xd->type == PNGdirect || xd->type == TIFF) {
	if (xd->npages > 1) {
	    xd->npages--;
	    BM_Close_bitmap(xd);
	    xd->npages++;
	}
    }
#ifdef HAVE_CAIRO_SVG
    else if(xd->type == SVG) {
	if (xd->npages > 1 && xd->cs) {
	    cairo_show_page(xd->cc);
	    if(!xd->onefile) {
		cairo_surface_destroy(xd->cs);
		cairo_destroy(xd->cc);
	    }
	}
	if(xd->npages == 1 || !xd->onefile) {
	    snprintf(buf, PATH_MAX, xd->filename, xd->npages);
	    xd->cs = cairo_svg_surface_create(R_ExpandFileName(buf),
					      (double)xd->windowWidth,
					      (double)xd->windowHeight);
	    res = cairo_surface_status(xd->cs);
	    if (res != CAIRO_STATUS_SUCCESS) {
		xd->cs = NULL;
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
	    if(xd->onefile)
		cairo_svg_surface_restrict_to_version(xd->cs, CAIRO_SVG_VERSION_1_2);
	    xd->cc = cairo_create(xd->cs);
	    res = cairo_status(xd->cc);
	    if (res != CAIRO_STATUS_SUCCESS) {
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
	    cairo_set_antialias(xd->cc, xd->antialias);
	}
    }
#endif
#ifdef HAVE_CAIRO_PDF
    else if(xd->type == PDF) {
	if (xd->npages > 1) {
	    cairo_show_page(xd->cc);
	    if(!xd->onefile) {
		cairo_surface_destroy(xd->cs);
		cairo_destroy(xd->cc);
	    }
	}
	if(xd->npages == 1 || !xd->onefile) {
	    snprintf(buf, PATH_MAX, xd->filename, xd->npages);
	    xd->cs = cairo_pdf_surface_create(R_ExpandFileName(buf),
					      (double)xd->windowWidth,
					      (double)xd->windowHeight);
	    res = cairo_surface_status(xd->cs);
	    if (res != CAIRO_STATUS_SUCCESS) {
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
	    xd->cc = cairo_create(xd->cs);
	    res = cairo_status(xd->cc);
	    if (res != CAIRO_STATUS_SUCCESS) {
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
	    cairo_set_antialias(xd->cc, xd->antialias);
	}
    }
#endif
#ifdef HAVE_CAIRO_PS
    else if(xd->type == PS) {
	if (xd->npages > 1 && !xd->onefile) {
	    cairo_show_page(xd->cc);
	    cairo_surface_destroy(xd->cs);
	    cairo_destroy(xd->cc);
	}
	if(xd->npages == 1 || !xd->onefile) {
	    snprintf(buf, PATH_MAX, xd->filename, xd->npages);
	    xd->cs = cairo_ps_surface_create(R_ExpandFileName(buf),
					     (double)xd->windowWidth,
					     (double)xd->windowHeight);
	    res = cairo_surface_status(xd->cs);
	    if (res != CAIRO_STATUS_SUCCESS) {
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
// We already require >= 1.2
#if CAIRO_VERSION_MAJOR > 2 || CAIRO_VERSION_MINOR >= 6
	    if(!xd->onefile)
		cairo_ps_surface_set_eps(xd->cs, TRUE);
#endif
	    xd->cc = cairo_create(xd->cs);
	    res = cairo_status(xd->cc);
	    if (res != CAIRO_STATUS_SUCCESS) {
		error("cairo error '%s'", cairo_status_to_string(res));
	    }
	    cairo_set_antialias(xd->cc, xd->antialias);
	}
    }
#endif
    else
	error(_("unimplemented cairo-based device"));

    cairo_reset_clip(xd->cc);
    if (xd->type == PNG  || xd->type == TIFF|| xd->type == PNGdirect) {
	/* First clear it */
	cairo_set_operator (xd->cc, CAIRO_OPERATOR_CLEAR);
	cairo_paint (xd->cc);
	cairo_set_operator (xd->cc, CAIRO_OPERATOR_OVER);
	xd->fill = gc->fill;
    } else
	xd->fill = R_OPAQUE(gc->fill) ? gc->fill: xd->canvas;
    CairoColor(xd->fill, xd);
    cairo_new_path(xd->cc);
    cairo_paint(xd->cc);
}
示例#20
0
/*==================================================================================
	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);
}
bool QCairoPaintEngine::begin(QPaintDevice *pd)
{
    if (surface) {
        end();
    }

    const QCairoPaintDevice* cpd=dynamic_cast<const QCairoPaintDevice*>(pd);
    surface = NULL;
    if (cpd) {
        exportText=cpd->getExportText();
        QCairoPaintDevice::CairoFileType ft=cpd->getFileType();
        QSizeF s=cpd->getFileSizeMM();
        //qDebug()<<ft;
        if (ft==QCairoPaintDevice::cftPDF14) {
            //qDebug()<<  "Cairo-PDF1.4";
            surface = cairo_pdf_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_pdf_surface_restrict_to_version(surface, CAIRO_PDF_VERSION_1_4);
        } else if (ft==QCairoPaintDevice::cftPDF15) {
            //Debug()<<  "Cairo-PDF1.5"<<s<<s.width()/25.4*72.0<<s.height()/25.4*72.0;
            surface = cairo_pdf_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_pdf_surface_restrict_to_version(surface, CAIRO_PDF_VERSION_1_5);
        } else if (ft==QCairoPaintDevice::cftPS2) {
           //qDebug()<<  "Cairo-PS2";
            surface = cairo_ps_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
        } else if (ft==QCairoPaintDevice::cftPS3) {
           //qDebug()<<  "Cairo-PS3";
            surface = cairo_ps_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
        } else if (ft==QCairoPaintDevice::cftEPS2) {
           //qDebug()<<  "Cairo-EPS2";
            surface = cairo_ps_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
            cairo_ps_surface_set_eps(surface, 1);
        } else if (ft==QCairoPaintDevice::cftEPS3) {
           //qDebug()<<  "Cairo-EPS3";
            surface = cairo_ps_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
            cairo_ps_surface_set_eps(surface, 1);
        } else if (ft==QCairoPaintDevice::cftSVG11) {
           //qDebug()<<  "Cairo-SVG11";
            surface = cairo_svg_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_svg_surface_restrict_to_version(surface, CAIRO_SVG_VERSION_1_1);
        } else if (ft==QCairoPaintDevice::cftSVG12) {
           //qDebug()<<  "Cairo-SVG12";
            surface = cairo_svg_surface_create (cpd->getFileName().toLocal8Bit().data(), s.width()/25.4*72.0, s.height()/25.4*72.0);
            cairo_svg_surface_restrict_to_version(surface, CAIRO_SVG_VERSION_1_2);
        }
    }
    if (surface) {
        if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS) {
            cr = cairo_create(surface);
            if (cr) {
                if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
                    qDebug()<<"QCairoPaintDevice error initializing CAIRO: "<<cairo_status_to_string(cairo_status(cr));
                    end();
                    return false;
                } else {
                    cairo_set_antialias(cr, CAIRO_ANTIALIAS_DEFAULT);
                }
            } else {
                qDebug()<<"QCairoPaintDevice error initializing CAIRO !!!";
                end();
                return false;
            }
        } else {
            qDebug()<<"QCairoPaintDevice error creating surface: "<<cairo_status_to_string(cairo_surface_status(surface));
            end();
            return false;
        }

    } else {
        qDebug()<<"UNKNOWN QCairoPaintDevice type !!!";
        return false;
    }
    return true;
}