BRL-CAD
ps.c
Go to the documentation of this file.
1 /* P S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/ps.c
21  *
22  * The ps command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 
33 #include "bu/getopt.h"
34 #include "vmath.h"
35 #include "bn.h"
36 
37 
38 #include "./ged_private.h"
39 
40 
41 static fastf_t ps_default_ppi = 72.0;
42 static fastf_t ps_default_scale = 4.5 * 72.0 / 4096.0;
43 
44 int
45 ged_ps(struct ged *gedp, int argc, const char *argv[])
46 {
47  FILE *fp;
48  struct bu_vls creator = BU_VLS_INIT_ZERO;
49  struct bu_vls title = BU_VLS_INIT_ZERO;
50  struct bu_vls font = BU_VLS_INIT_ZERO;
51  fastf_t scale = ps_default_scale;
52  int linewidth = 4;
53  int xoffset = 0;
54  int yoffset = 0;
55  int border = 0;
56  int k;
57  int r, g, b;
58 
59  float border_red = 0.0;
60  float border_green = 0.0;
61  float border_blue = 0.0;
62 
63  static const char *usage = "[-a author] [-b] [-c r/g/b] [-f font] [-s size] [-t title] [-x offset] [-y offset] file";
64 
68  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
69 
70  /* initialize result */
71  bu_vls_trunc(gedp->ged_result_str, 0);
72 
73  /* must be wanting help */
74  if (argc == 1) {
75  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
76  return GED_HELP;
77  }
78 
79  /* Initialize var defaults */
80  bu_vls_printf(&font, "Courier");
81  bu_vls_printf(&title, "No Title");
82  bu_vls_printf(&creator, "LIBGED ps");
83 
84  /* Process options */
85  bu_optind = 1;
86  while ((k = bu_getopt(argc, (char * const *)argv, "a:bc:f:s:t:x:y:")) != -1) {
87  double tmp_f;
88 
89  switch (k) {
90  case 'a':
91  bu_vls_trunc(&creator, 0);
92  bu_vls_printf(&creator, "%s", bu_optarg);
93 
94  break;
95  case 'b':
96  border = 1;
97  break;
98  case 'c':
99  if (sscanf(bu_optarg, "%d%*c%d%*c%d", &r, &g, &b) != 3) {
100  bu_vls_printf(gedp->ged_result_str, "%s: bad color - %s", argv[0], bu_optarg);
101  return GED_ERROR;
102  }
103 
104  /* Clamp color values */
105  if (r < 0)
106  r = 0;
107  else if (r > 255)
108  r = 255;
109 
110  if (g < 0)
111  g = 0;
112  else if (g > 255)
113  g = 255;
114 
115  if (b < 0)
116  b = 0;
117  else if (b > 255)
118  b = 255;
119 
120  border_red = PS_COLOR(r);
121  border_green = PS_COLOR(g);
122  border_blue = PS_COLOR(b);
123 
124  break;
125  case 'f':
126  bu_vls_trunc(&font, 0);
127  bu_vls_printf(&font, "%s", bu_optarg);
128 
129  break;
130  case 's':
131  if (sscanf(bu_optarg, "%lf", &tmp_f) != 1) {
132  bu_vls_printf(gedp->ged_result_str, "%s: bad size - %s", argv[0], bu_optarg);
133  goto bad;
134  }
135 
136  if (tmp_f < 0.0 || NEAR_ZERO(tmp_f, 0.1)) {
137  bu_vls_printf(gedp->ged_result_str, "%s: bad size - %s, must be greater than 0.1 inches\n", argv[0], bu_optarg);
138  goto bad;
139  }
140 
141  scale = tmp_f * ps_default_ppi / 4096.0;
142 
143  break;
144  case 't':
145  bu_vls_trunc(&title, 0);
146  bu_vls_printf(&title, "%s", bu_optarg);
147 
148  break;
149  case 'x':
150  if (sscanf(bu_optarg, "%lf", &tmp_f) != 1) {
151  bu_vls_printf(gedp->ged_result_str, "%s: bad x offset - %s", argv[0], bu_optarg);
152  goto bad;
153  }
154  xoffset = (int)(tmp_f * ps_default_ppi);
155 
156  break;
157  case 'y':
158  if (sscanf(bu_optarg, "%lf", &tmp_f) != 1) {
159  bu_vls_printf(gedp->ged_result_str, "%s: bad y offset - %s", argv[0], bu_optarg);
160  goto bad;
161  }
162  yoffset = (int)(tmp_f * ps_default_ppi);
163 
164  break;
165  default:
166  bu_vls_printf(gedp->ged_result_str, "%s: Unrecognized option - %s", argv[0], argv[bu_optind-1]);
167  goto bad;
168  }
169  }
170 
171  if ((argc - bu_optind) != 1) {
172  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
173  goto bad;
174  }
175 
176  if ((fp = fopen(argv[bu_optind], "wb")) == NULL) {
177  bu_vls_printf(gedp->ged_result_str, "%s: Error opening file - %s\n", argv[0], argv[bu_optind]);
178  goto bad;
179  }
180 
181  dl_ps(gedp->ged_gdp->gd_headDisplay, fp, border, bu_vls_addr(&font), bu_vls_addr(&title), bu_vls_addr(&creator), linewidth, scale, xoffset, yoffset, gedp->ged_gvp->gv_model2view, gedp->ged_gvp->gv_perspective, gedp->ged_gvp->gv_eye_pos, border_red, border_green, border_blue);
182 
183  fclose(fp);
184 
185  bu_vls_free(&font);
186  bu_vls_free(&title);
187  bu_vls_free(&creator);
188 
189  return GED_OK;
190 
191 bad:
192  bu_vls_free(&font);
193  bu_vls_free(&title);
194  bu_vls_free(&creator);
195 
196  return GED_ERROR;
197 }
198 
199 
200 /*
201  * Local Variables:
202  * tab-width: 8
203  * mode: C
204  * indent-tabs-mode: t
205  * c-file-style: "stroustrup"
206  * End:
207  * ex: shiftwidth=4 tabstop=8
208  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
void dl_ps(struct bu_list *hdlp, FILE *fp, int border, char *font, char *title, char *creator, int linewidth, fastf_t scale, int xoffset, int yoffset, mat_t model2view, fastf_t perspective, vect_t eye_pos, float red, float green, float blue)
Definition: ged.h:338
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
fastf_t gv_perspective
perspective angle
Definition: bview.h:214
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#define GED_ERROR
Definition: ged.h:61
struct bview * ged_gvp
Definition: ged.h:361
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define NEAR_ZERO(val, epsilon)
Definition: color.c:55
int ged_ps(struct ged *gedp, int argc, const char *argv[])
Definition: ps.c:45
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
struct ged_drawable * ged_gdp
Definition: ged.h:360
mat_t gv_model2view
Definition: bview.h:222
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
vect_t gv_eye_pos
eye position
Definition: bview.h:216
#define GED_HELP
Definition: ged.h:62
#define PS_COLOR(_c)
Definition: ged_private.h:200
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
double fastf_t
Definition: defines.h:300