/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/plugin/core/gvloadimage_core.c

Go to the documentation of this file.
00001 /* $Id: gvloadimage_core.c,v 1.24 2007/12/26 16:53:24 ellson Exp $ $Revision: 1.24 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 #include <stdlib.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #if HAVE_SYS_MMAN_H
00025 #include <sys/mman.h>
00026 #endif
00027 
00028 #include "gvplugin_loadimage.h"
00029 
00030 /* for n->name */
00031 #include "graph.h"
00032 
00033 extern void core_loadimage_xdot(GVJ_t*, usershape_t*, boxf, boolean);
00034 extern void epsf_emit_body(usershape_t *us, FILE *of);
00035 extern shape_desc *find_user_shape(char *name);
00036 
00037 typedef enum {
00038     FORMAT_PNG_SVG, FORMAT_GIF_SVG, FORMAT_JPEG_SVG,
00039     FORMAT_PNG_XDOT, FORMAT_GIF_XDOT, FORMAT_JPEG_XDOT,
00040     FORMAT_PNG_FIG, FORMAT_GIF_FIG, FORMAT_JPEG_FIG,
00041     FORMAT_PNG_VRML, FORMAT_GIF_VRML, FORMAT_JPEG_VRML,
00042     FORMAT_PS_PS, FORMAT_PSLIB_PS, FORMAT_SVG_SVG,
00043 } format_type;
00044 
00045 static void core_loadimage_svg(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
00046 {
00047     assert(job);
00048     assert(us);
00049     assert(us->name);
00050     assert(us->f);
00051 
00052     gvdevice_fputs(job, "<image xlink:href=\"");
00053     gvdevice_fputs(job, us->name);
00054     if (job->rotation) {
00055         gvdevice_printf (job, "\" width=\"%gpx\" height=\"%gpx\" preserveAspectRatio=\"xMidYMid meet\" x=\"%g\" y=\"%g\"",
00056             b.UR.y - b.LL.y, b.UR.x - b.LL.x, b.LL.x, b.UR.y);
00057         gvdevice_printf (job, " transform=\"rotate(%d %g %g)\"",
00058             job->rotation, b.LL.x, b.UR.y);
00059     }
00060     else {
00061         gvdevice_printf (job, "\" width=\"%gpx\" height=\"%gpx\" preserveAspectRatio=\"xMinYMin meet\" x=\"%g\" y=\"%g\"",
00062             b.UR.x - b.LL.x, b.UR.y - b.LL.y, b.LL.x, -b.UR.y);
00063     }
00064     gvdevice_fputs(job, "/>\n");
00065 }
00066 
00067 static void core_loadimage_fig(GVJ_t * job, usershape_t *us, boxf bf, boolean filled)
00068 {
00069     int object_code = 2;        /* always 2 for polyline */
00070     int sub_type = 5;           /* always 5 for image */
00071     int line_style = 0;         /* solid, dotted, dashed */
00072     int thickness = 0;
00073     int pen_color = 0;
00074     int fill_color = -1;
00075     int depth = 1;
00076     int pen_style = -1;         /* not used */
00077     int area_fill = 0;
00078     double style_val = 0.0;
00079     int join_style = 0;
00080     int cap_style = 0;
00081     int radius = 0;
00082     int forward_arrow = 0;
00083     int backward_arrow = 0;
00084     int npoints = 5;
00085     int flipped = 0;
00086 
00087     box b;
00088 
00089     assert(job);
00090     assert(us);
00091     assert(us->name);
00092     assert(us->f);
00093 
00094     BF2B(bf, b);
00095 
00096     gvdevice_printf(job, "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d %d %d\n %d %s\n",
00097             object_code, sub_type, line_style, thickness, pen_color,
00098             fill_color, depth, pen_style, area_fill, style_val, join_style,
00099             cap_style, radius, forward_arrow, backward_arrow, npoints,
00100             flipped, us->name);
00101     gvdevice_printf(job," %d %d %d %d %d %d %d %d %d %d\n",
00102             b.LL.x, b.LL.y,
00103             b.LL.x, b.UR.y,
00104             b.UR.x, b.UR.y,
00105             b.UR.x, b.LL.y,
00106             b.LL.x, b.LL.y);
00107 }
00108 
00109 static void core_loadimage_vrml(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
00110 {
00111     FILE *out;
00112     obj_state_t *obj;
00113     node_t *n;
00114 
00115     assert(job);
00116 
00117     out = job->output_file;
00118     obj = job->obj;
00119     assert(out);
00120     assert(obj);
00121 
00122     assert(us);
00123     assert(us->name);
00124     assert(us->f);
00125 
00126     n = job->obj->u.n;
00127     assert(n);
00128 
00129     fprintf(out, "Shape {\n");
00130     fprintf(out, "  appearance Appearance {\n");
00131     fprintf(out, "    material Material {\n");
00132     fprintf(out, "      ambientIntensity 0.33\n");
00133     fprintf(out, "        diffuseColor 1 1 1\n");
00134     fprintf(out, "    }\n");
00135     fprintf(out, "    texture ImageTexture { url \"%s\" }\n", us->name);
00136     fprintf(out, "  }\n");
00137     fprintf(out, "}\n");
00138 
00139 
00140 }
00141 
00142 static void ps_freeimage(usershape_t *us)
00143 {
00144 #if HAVE_SYS_MMAN_H
00145     munmap(us->data, us->datasize);
00146 #else
00147     free(us->data);
00148 #endif
00149 }
00150 
00151 /* usershape described by a postscript file */
00152 static void core_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
00153 {
00154     FILE *out;
00155 
00156     assert(job);
00157     assert(us);
00158     assert(us->name);
00159     assert(us->f);
00160 
00161     out = job->output_file;
00162     assert(out);
00163 
00164     if (us->data) {
00165         if (us->datafree != ps_freeimage) {
00166             us->datafree(us);        /* free incompatible cache data */
00167             us->data = NULL;
00168             us->datafree = NULL;
00169             us->datasize = 0;
00170         }
00171     }
00172 
00173     if (!us->data) { /* read file into cache */
00174         int fd = fileno(us->f);
00175         struct stat statbuf;
00176 
00177         fseek(us->f, 0, SEEK_SET);
00178         switch (us->type) {
00179             case FT_PS:
00180             case FT_EPS:
00181                 fstat(fd, &statbuf);
00182                 us->datasize = statbuf.st_size;
00183 #if HAVE_SYS_MMAN_H
00184                 us->data = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
00185 #else
00186                 us->data = malloc(statbuf.st_size);
00187                 read(fd, us->data, statbuf.st_size);
00188 #endif
00189                 us->must_inline = TRUE;
00190                 break;
00191             default:
00192                 break;
00193         }
00194         if (us->data)
00195             us->datafree = ps_freeimage;
00196     }
00197 
00198     if (us->data) {
00199         fprintf(out, "gsave %g %g translate newpath\n",
00200                 b.LL.x - (double)(us->x), b.LL.y - (double)(us->y));
00201         if (us->must_inline)
00202             epsf_emit_body(us, out);
00203         else
00204             fprintf(out, "user_shape_%d\n", us->macro_id);
00205         fprintf(out, "grestore\n");
00206     }
00207 }
00208 
00209 /* usershape described by a member of a postscript library */
00210 static void core_loadimage_pslib(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
00211 {
00212     int i;
00213     pointf AF[4];
00214     FILE *out;
00215     shape_desc *shape;
00216 
00217     assert(job);
00218     assert(us);
00219     assert(us->name);
00220     assert(!(us->f));
00221 
00222     out = job->output_file;
00223     assert(out);
00224 
00225     if ((shape = (shape_desc*)us->data)) {
00226         AF[0] = b.LL;
00227         AF[2] = b.UR;
00228         AF[1].x = AF[0].x;
00229         AF[1].y = AF[2].y;
00230         AF[3].x = AF[2].x;
00231         AF[3].y = AF[0].y;
00232         if (filled) {
00233 //            ps_begin_context();
00234 //            ps_set_color(S[SP].fillcolor);
00235             fprintf(out, "[ ");
00236             for (i = 0; i < 4; i++)
00237                 fprintf(out, "%g %g ", AF[i].x, AF[i].y);
00238             fprintf(out, "%g %g ", AF[0].x, AF[0].y);
00239             fprintf(out, "]  %d true %s\n", 4, us->name);
00240 //            ps_end_context();
00241         }
00242         fprintf(out, "[ ");
00243         for (i = 0; i < 4; i++)
00244             fprintf(out, "%g %g ", AF[i].x, AF[i].y);
00245         fprintf(out, "%g %g ", AF[0].x, AF[0].y);
00246         fprintf(out, "]  %d false %s\n", 4, us->name);
00247     }
00248 }
00249 
00250 static gvloadimage_engine_t engine_svg = {
00251     core_loadimage_svg
00252 };
00253 
00254 static gvloadimage_engine_t engine_fig = {
00255     core_loadimage_fig
00256 };
00257 
00258 static gvloadimage_engine_t engine_vrml = {
00259     core_loadimage_vrml
00260 };
00261 
00262 static gvloadimage_engine_t engine_ps = {
00263     core_loadimage_ps
00264 };
00265 
00266 static gvloadimage_engine_t engine_pslib = {
00267     core_loadimage_pslib
00268 };
00269 
00270 static gvloadimage_engine_t engine_xdot = {
00271     core_loadimage_xdot
00272 };
00273 
00274 gvplugin_installed_t gvloadimage_core_types[] = {
00275     {FORMAT_PNG_SVG, "png:svg", 1, &engine_svg, NULL},
00276     {FORMAT_GIF_SVG, "gif:svg", 1, &engine_svg, NULL},
00277     {FORMAT_JPEG_SVG, "jpeg:svg", 1, &engine_svg, NULL},
00278     {FORMAT_JPEG_SVG, "jpe:svg", 1, &engine_svg, NULL},
00279     {FORMAT_JPEG_SVG, "jpg:svg", 1, &engine_svg, NULL},
00280     {FORMAT_PNG_FIG, "png:fig", 1, &engine_fig, NULL},
00281     {FORMAT_GIF_FIG, "gif:fig", 1, &engine_fig, NULL},
00282     {FORMAT_JPEG_FIG, "jpeg:fig", 1, &engine_fig, NULL},
00283     {FORMAT_JPEG_FIG, "jpe:fig", 1, &engine_fig, NULL},
00284     {FORMAT_JPEG_FIG, "jpg:fig", 1, &engine_fig, NULL},
00285     {FORMAT_PNG_VRML, "png:vrml", 1, &engine_vrml, NULL},
00286     {FORMAT_GIF_VRML, "gif:vrml", 1, &engine_vrml, NULL},
00287     {FORMAT_JPEG_VRML, "jpeg:vrml", 1, &engine_vrml, NULL},
00288     {FORMAT_JPEG_VRML, "jpe:vrml", 1, &engine_vrml, NULL},
00289     {FORMAT_JPEG_VRML, "jpg:vrml", 1, &engine_vrml, NULL},
00290     {FORMAT_PS_PS, "ps:ps", 1, &engine_ps, NULL},
00291     {FORMAT_PSLIB_PS, "(lib):ps", 1, &engine_pslib, NULL},  /* for pslib */
00292     {FORMAT_PNG_XDOT, "png:xdot", 1, &engine_xdot, NULL},
00293     {FORMAT_GIF_XDOT, "gif:xdot", 1, &engine_xdot, NULL},
00294     {FORMAT_JPEG_XDOT, "jpeg:xdot", 1, &engine_xdot, NULL},
00295     {FORMAT_JPEG_XDOT, "jpe:xdot", 1, &engine_xdot, NULL},
00296     {FORMAT_JPEG_XDOT, "jpg:xdot", 1, &engine_xdot, NULL},
00297     {FORMAT_SVG_SVG, "svg:svg", 1, &engine_svg, NULL},
00298     {0, NULL, 0, NULL, NULL}
00299 };

Generated on Mon Mar 31 19:03:28 2008 for Graphviz by  doxygen 1.5.1