gvloadimage_lasi.cpp 2.6 KB

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