File: pdf.c

package info (click to toggle)
gnuastro 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 176,016; sh: 14,784; makefile: 1,298; cpp: 9
file content (168 lines) | stat: -rw-r--r-- 4,827 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*********************************************************************
pdf -- functions to write PDF files.
This is part of GNU Astronomy Utilities (Gnuastro) package.

Original author:
     Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2015-2024 Free Software Foundation, Inc.

Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Gnuastro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>

#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <gnuastro/eps.h>
#include <gnuastro/pdf.h>
#include <gnuastro/jpeg.h>
#include <gnuastro/pointer.h>

#include <gnuastro-internal/checkset.h>




/*************************************************************
 **************      Acceptable PDF names      ***************
 *************************************************************/
int
gal_pdf_name_is_pdf(char *name)
{
  size_t len;

  if(name)
    {
      len=strlen(name);
      if (strcmp(&name[len-3], "pdf") == 0
          || strcmp(&name[len-3], "PDF") == 0)
        return 1;
      else
        return 0;
    }
  else return 0;
}





int
gal_pdf_suffix_is_pdf(char *name)
{
  if(name)
    {
      if (strcmp(name, "pdf") == 0 || strcmp(name, ".pdf") == 0
          || strcmp(name, "PDF") == 0 || strcmp(name, ".PDF") == 0)
        return 1;
      else
        return 0;
    }
  else return 0;
}




















/*************************************************************
 **************        Write a PDF image       ***************
 *************************************************************/
void
gal_pdf_write(gal_data_t *in, char *filename, float widthincm,
              uint32_t borderwidth, uint8_t bordercolor,
              int dontoptimize, gal_data_t *marks)
{
  int execstat=0;
  size_t w_h_in_pt[2];
  gal_list_str_t *command=NULL;
  char *device, *devwp, *devhp, *devopt;
  char *epsname=gal_checkset_malloc_cat(filename, ".ps");

  /* Write the EPS file. */
  gal_eps_write(in, epsname, widthincm, borderwidth,
                bordercolor, 0, dontoptimize, 0, marks);

  /* Get the size of the image in 'pt' units. */
  gal_eps_to_pt(widthincm, in->dsize, w_h_in_pt);

  /* Set the device from the file name. */
  if(gal_jpeg_name_is_jpeg(filename)) device="jpeg";
  else                                device="pdfwrite";

  /* Build the necessary strings. */
  if( asprintf(&devwp, "-dDEVICEWIDTHPOINTS=%zu",
               w_h_in_pt[0]+2*borderwidth)<0 )
    error(EXIT_FAILURE, 0, "%s: asprintf allocation error", __func__);
  if( asprintf(&devhp, "-dDEVICEHEIGHTPOINTS=%zu",
               w_h_in_pt[1]+2*borderwidth)<0 )
    error(EXIT_FAILURE, 0, "%s: asprintf allocation error", __func__);
  if( asprintf(&devopt, "-sDEVICE=%s", device)<0 )
    error(EXIT_FAILURE, 0, "%s: asprintf allocation error", __func__);

  /* Run Ghostscript (if the command changes, also change the command in
     the error message).  */
  gal_list_str_add(&command, PATH_GHOSTSCRIPT, 0);
  gal_list_str_add(&command, "-q", 0);
  gal_list_str_add(&command, "-o", 0);
  gal_list_str_add(&command, filename, 0);
  gal_list_str_add(&command, devopt, 0);
  gal_list_str_add(&command, devwp, 0);
  gal_list_str_add(&command, devhp, 0);
  gal_list_str_add(&command, "-dPDFFitPage", 0);
  gal_list_str_add(&command, epsname, 0);
  gal_list_str_reverse(&command);
  execstat=gal_checkset_exec(PATH_GHOSTSCRIPT, command);
  if(execstat)
    error(EXIT_FAILURE, 0, "the Ghostscript command (printed at the "
          "end of this message) to convert/compile the EPS file (made "
          "by Gnuastro) to PDF was not successful (Ghostscript returned "
          "with status %d; its error message is shown above)! The EPS "
          "file ('%s') is left if you want to convert it through any "
          "other means (for example the 'epspdf' program). The "
          "Ghostscript command was: %s", execstat, epsname,
          gal_list_str_cat(command, ' '));

  /* Delete the EPS file. */
  errno=0;
  if(unlink(epsname))
    error(EXIT_FAILURE, errno, "%s", epsname);

  /* Clean up the command list and delete the EPS file. */
  free(devhp);
  free(devwp);
  free(devopt);
  free(epsname);
  gal_list_str_free(command, 0);
}