1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
#include "config.h"
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <common/render.h>
#include <common/utils.h>
#include <gvc/gvio.h>
#include <gvc/gvplugin_loadimage.h>
#include <util/agxbuf.h>
enum {
FORMAT_PS_PS,
};
static void ps_freeimage(usershape_t *us) {
#ifdef HAVE_SYS_MMAN_H
munmap(us->data, us->datasize);
#else
delete[] us->data;
#endif
}
extern "C" {
/// usershape described by a postscript file
static void lasi_loadimage_ps(GVJ_t *job, usershape_t *us, boxf b, bool) {
assert(job);
assert(us);
assert(us->name);
if (us->data) {
if (us->datafree != ps_freeimage) {
us->datafree(us); // free incompatible cache data
us->data = nullptr;
us->datafree = nullptr;
us->datasize = 0;
}
}
if (!us->data) { // read file into cache
int fd;
struct stat statbuf;
if (!gvusershape_file_access(us))
return;
fd = fileno(us->f);
switch (us->type) {
case FT_PS:
case FT_EPS:
fstat(fd, &statbuf);
us->datasize = statbuf.st_size;
#ifdef HAVE_SYS_MMAN_H
us->data = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (us->data == MAP_FAILED)
us->data = nullptr;
#else
us->data = new char[statbuf.st_size];
fread(us->data, 1, (size_t)statbuf.st_size, us->f);
#endif
us->must_inline = true;
break;
default:
break;
}
if (us->data)
us->datafree = ps_freeimage;
gvusershape_file_release(us);
}
if (us->data) {
gvprintf(job, "gsave %g %g translate newpath\n", b.LL.x - (double)(us->x),
b.LL.y - (double)(us->y));
if (us->must_inline)
epsf_emit_body(job, us);
else
gvprintf(job, "user_shape_%d\n", us->macro_id);
gvprintf(job, "grestore\n");
}
}
}
static gvloadimage_engine_t engine_ps = {lasi_loadimage_ps};
extern "C" {
gvplugin_installed_t gvloadimage_lasi_types[] = {
{FORMAT_PS_PS, "eps:lasi", -5, &engine_ps, nullptr},
{FORMAT_PS_PS, "ps:lasi", -5, &engine_ps, nullptr},
{0, nullptr, 0, nullptr, nullptr}};
}
|