Graphviz  2.41.20170921.2350
gvloadimage_lasi.c
Go to the documentation of this file.
1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "config.h"
15 
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #if HAVE_SYS_MMAN_H
20 #include <sys/mman.h>
21 #endif
22 #ifdef _MSC_VER
23 #include <io.h>
24 #endif
25 
26 #include "gvplugin_loadimage.h"
27 #include "agxbuf.h"
28 #include "utils.h"
29 #include "gvio.h"
30 
31 extern shape_desc *find_user_shape(char *name);
32 
33 typedef enum {
35 } format_type;
36 
37 static void ps_freeimage(usershape_t *us)
38 {
39 #if HAVE_SYS_MMAN_H
40  munmap(us->data, us->datasize);
41 #else
42  free(us->data);
43 #endif
44 }
45 
46 /* usershape described by a postscript file */
47 static void lasi_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
48 {
49  assert(job);
50  assert(us);
51  assert(us->name);
52 
53  if (us->data) {
54  if (us->datafree != ps_freeimage) {
55  us->datafree(us); /* free incompatible cache data */
56  us->data = NULL;
57  us->datafree = NULL;
58  us->datasize = 0;
59  }
60  }
61 
62  if (!us->data) { /* read file into cache */
63  int fd;
64  struct stat statbuf;
65 
66  if (!gvusershape_file_access(us))
67  return;
68  fd = fileno(us->f);
69  switch (us->type) {
70  case FT_PS:
71  case FT_EPS:
72  fstat(fd, &statbuf);
73  us->datasize = statbuf.st_size;
74 #if HAVE_SYS_MMAN_H
75  us->data = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
76 #else
77  us->data = malloc(statbuf.st_size);
78  read(fd, us->data, statbuf.st_size);
79 #endif
80  us->must_inline = TRUE;
81  break;
82  default:
83  break;
84  }
85  if (us->data)
86  us->datafree = ps_freeimage;
88  }
89 
90  if (us->data) {
91  gvprintf(job, "gsave %g %g translate newpath\n",
92  b.LL.x - (double)(us->x), b.LL.y - (double)(us->y));
93  if (us->must_inline)
94  epsf_emit_body(job, us);
95  else
96  gvprintf(job, "user_shape_%d\n", us->macro_id);
97  gvprintf(job, "grestore\n");
98  }
99 }
100 
101 static gvloadimage_engine_t engine_ps = {
102  lasi_loadimage_ps
103 };
104 
106  {FORMAT_PS_PS, "eps:lasi", -5, &engine_ps, NULL},
107  {FORMAT_PS_PS, "ps:lasi", -5, &engine_ps, NULL},
108  {0, NULL, 0, NULL, NULL}
109 };
shape_desc * find_user_shape(const char *)
Definition: shapes.c:3803
void(* datafree)(usershape_t *us)
Definition: usershape.h:63
#define assert(x)
Definition: cghdr.h:47
void * data
Definition: usershape.h:61
boolean must_inline
Definition: usershape.h:55
Definition: gvcjob.h:271
int macro_id
Definition: usershape.h:54
boolean gvusershape_file_access(usershape_t *us)
Definition: gvusershape.c:584
FILE * f
Definition: usershape.h:57
double y
Definition: geom.h:28
size_t datasize
Definition: usershape.h:62
imagetype_t type
Definition: usershape.h:58
void gvusershape_file_release(usershape_t *us)
Definition: gvusershape.c:618
void epsf_emit_body(GVJ_t *job, usershape_t *us)
Definition: psusershape.c:185
format_type
#define NULL
Definition: logic.h:39
double x
Definition: geom.h:28
pointf LL
Definition: geom.h:35
gvplugin_installed_t gvloadimage_lasi_types[]
const char * name
Definition: usershape.h:53
Definition: geom.h:35
void gvprintf(GVJ_t *job, const char *format,...)
Definition: gvdevice.c:389
Agraph_t * read(FILE *f)
Definition: gv.cpp:73
#define TRUE
Definition: cgraph.h:38