w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ppmlabel.c
Go to the documentation of this file.
1 /*
2 
3  Include text labels in a portable pixmap
4 
5  by John Walker -- kelvin@fourmilab.ch
6  WWW home page: http://www.fourmilab.ch/
7  June 1995
8 */
9 
10 #include <math.h>
11 
12 #include "ppm.h"
13 #include "ppmdraw.h"
14 
15 #define TRUE 1
16 #define FALSE 0
17 
18 #ifndef M_PI
19 #define M_PI 3.14159265358979323846
20 #endif
21 
22 #define dtr(x) (((x) * M_PI) / 180.0)
23 
24 static int argn, rows, cols, x, y, size, angle, transparent;
25 static pixel **pixels;
26 static pixval maxval;
28 
29 /* DRAWTEXT -- Draw text at current location and advance to
30  start of next line. */
31 
32 static void drawtext(text)
33  char *text;
34 {
35  if (!transparent && strlen(text) > 0) {
36  char *handle = ppmd_fill_init();
37  int left, top, right, bottom,
38  lx, ly,
39  p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
40  double sina, cosa;
41 
43  &left, &top, &right, &bottom);
44 
45  /* Displacement vector */
46 
47  lx = right;
48  ly = -(top - bottom);
49 
50  /* Sine and cosine */
51 
52  sina = sin(dtr(angle));
53  cosa = cos(dtr(angle));
54 
55  /* Rotated extent box corners */
56 
57  p1x = (int) ((x + left * cosa + bottom * sina) + 0.5);
58  p1y = (int) ((y + bottom * cosa + -left * sina) + 0.5);
59 
60 #define WERF ppmd_fill_drawproc, handle
61 
62  p2x = (int) (p1x - sina * ly + 0.5);
63  p2y = (int) ((p1y - cosa * ly) + 0.5);
64 
65  p3x = (int) (p1x + cosa * lx + -sina * ly + 0.5);
66  p3y = (int) ((p1y - (cosa * ly + sina * lx)) + 0.5);
67 
68  p4x = (int) (p1x + cosa * lx + 0.5);
69  p4y = (int) ((p1y - sina * lx) + 0.5);
70 
72  p1x, p1y, p2x, p2y,
73  WERF);
75  p2x, p2y, p3x, p3y,
76  WERF);
78  p3x, p3y, p4x, p4y,
79  WERF);
81  p4x, p4y, p1x, p1y,
82  WERF);
83 
84 
86  handle, PPMD_NULLDRAWPROC, (char *) &backcolour);
87  }
89  x, y, size, angle, text,
90  PPMD_NULLDRAWPROC, (char *) &rgbcolour);
91 
92  /* For convenience, simulate a carriage return to the next line.
93  This allows multiple "-text" specifications or multiple lines
94  in a -file input to write consecutive lines of text in a
95  generally reasonable fashion. */
96 
97  x += (int) ((cos(dtr(angle + 270)) * size * 1.75) + 0.5);
98  y -= (int) ((sin(dtr(angle + 270)) * size * 1.75) + 0.5);
99 }
100 
101 /* Main program. */
102 
104  int argc;
105  char *argv[];
106 {
107  FILE *ifp;
108  char *usage = "[-x <x>] [-y <y>] [-size <size>] [-angle <degrees>]\n\
109  [-colo[u]r <colourspec>] [-background transparent|<colourspec>]\n\
110  [-text \"<text string>\"] [-file <filename>]\n\
111  [ppmfile]\n";
112 
113  /* Process standard command line arguments */
114 
115  ppm_init(&argc, argv);
116 
117  argn = 1;
118 
119  /* Check for explicit input file specification, Note that
120  we count on the fact that every command line switch
121  takes a single argument. If this becomes untrue due
122  to a change in the future, you'll have to make this
123  test smarter. */
124 
125  if ((argn != argc) && (argc == 2 || argv[argc - 2][0] != '-')) {
126  ifp = pm_openr(argv[argc - 1]);
127  argc--;
128  } else {
129  ifp = stdin;
130  }
131 
132  /* Load input image */
133 
135  pm_close(ifp);
136 
137  /* Set initial defaults */
138 
139  x = 0;
140  y = rows / 2;
141  size = 12;
142  angle = 0;
144  PPM_ASSIGN(backcolour, 0, 0, 0);
145  transparent = TRUE;
146 
147  while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
148 
149  if (pm_keymatch(argv[argn], "-angle", 1)) {
150  argn++;
151  if ((argn == argc) || (sscanf(argv[argn], "%d", &angle) != 1))
152  pm_usage(usage);
153 
154  } else if (pm_keymatch(argv[argn], "-background", 1)) {
155  argn++;
156  if (strcmp(argv[argn], "transparent") == 0) {
157  transparent = TRUE;
158  } else {
159  transparent = FALSE;
161  }
162 
163  } else if (pm_keymatch(argv[argn], "-color", 1)
164  || pm_keymatch(argv[argn], "-colour", 1)) {
165  argn++;
167 
168  } else if (pm_keymatch(argv[argn], "-file", 1)) {
169  char s[512];
170 
171  argn++;
172  ifp = pm_openr(argv[argn]);
173  while (fgets(s, sizeof s, ifp) != NULL) {
174  while (s[0] != 0 && s[strlen(s) - 1] < ' ') {
175  s[strlen(s) - 1] = 0;
176  }
177  drawtext(s);
178  }
179  pm_close(ifp);
180 
181  } else if (pm_keymatch(argv[argn], "-size", 1)) {
182  argn++;
183  if ((argn == argc) || (sscanf(argv[argn], "%d", &size) != 1))
184  pm_usage(usage);
185  } else if (pm_keymatch(argv[argn], "-text", 1)) {
186  argn++;
187  drawtext(argv[argn]);
188 
189  } else if (pm_keymatch(argv[argn], "-u", 1)) {
190  pm_usage(usage);
191 
192  } else if (pm_keymatch(argv[argn], "-x", 1)) {
193  argn++;
194  if ((argn == argc) || (sscanf(argv[argn], "%d", &x) != 1))
195  pm_usage(usage);
196 
197  } else if (pm_keymatch(argv[argn], "-y", 1)) {
198  argn++;
199  if ((argn == argc) || (sscanf(argv[argn], "%d", &y) != 1))
200  pm_usage(usage);
201 
202  } else {
203  pm_usage(usage);
204  }
205  argn++;
206  }
207 
208  if (argn != argc) { /* Extra bogus arguments ? */
209  pm_usage(usage);
210  }
211 
212  /* Output annotated image */
213 
215 
217 
218  return 0;
219 }
#define text(a)
Definition: aptex-macros.h:925
#define fgets
Definition: xxstdio.h:29
double cos()
double sin()
int strcmp()
Definition: coll.cpp:143
int sscanf()
int pixels
Definition: dvipng.h:106
#define s
Definition: afcover.h:80
static char usage[]
Definition: giftopnm.c:59
#define NULL
Definition: ftobjs.h:61
@ right
Definition: annotate.c:15
int int double double double char double char * top
Definition: gdfx.h:19
int int double double double char double char char * bottom
Definition: gdfx.h:20
void pm_usage(char *usage)
Definition: libpbm1.c:343
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
#define pm_keymatch(stra, strb, _x)
Definition: png22pnm.c:121
#define pm_close(file)
Definition: png22pnm.c:120
void ppm_init(int *argcP, argv)
Definition: libppm1.c:21
pixel ** ppm_readppm(FILE *file, int *colsP, int *rowsP, pixval *maxvalP)
Definition: libppm1.c:178
void ppm_writeppm(FILE *file, pixel **pixels, int cols, int rows, pixval maxval, int forceplain)
Definition: libppm2.c:148
pixel ppm_parsecolor(char *colorname, pixval maxval)
Definition: libppm4.c:139
void ppmd_line(pixel **pixels, int cols, int rows, pixval maxval, int x0, int y0, int x1, int y1, void *drawprocP, char *clientdata)
Definition: libppm5.c:121
char * ppmd_fill_init()
Definition: libppm5.c:435
void ppmd_fill(pixel **pixels, int cols, int rows, pixval maxval, char *fillhandle, void *drawprocP, char *clientdata)
Definition: libppm5.c:573
void ppmd_text(pixel **pixels, int cols, int rows, pixval maxval, int x, int y, int height, int angle, char *s, void *drawprocP, char *clientdata)
Definition: libppm5.c:1043
void ppmd_text_box(int height, int angle, char *s, int *left, int *top, int *right, int *bottom)
Definition: libppm5.c:1152
#define PPM_ASSIGN(p, red, grn, blu)
Definition: ppm.h:46
#define ppm_freearray(pixels, rows)
Definition: ppm.h:73
gray pixval
Definition: ppm.h:9
#define PPMD_NULLDRAWPROC
Definition: ppmdraw.h:26
#define WERF
static int rows
Definition: ppmlabel.c:24
static int y
Definition: ppmlabel.c:24
static void drawtext(char *text)
Definition: ppmlabel.c:32
static pixel ** pixels
Definition: ppmlabel.c:25
static int size
Definition: ppmlabel.c:24
static int cols
Definition: ppmlabel.c:24
static int x
Definition: ppmlabel.c:24
static int angle
Definition: ppmlabel.c:24
static int transparent
Definition: ppmlabel.c:24
#define dtr(x)
Definition: ppmlabel.c:22
static int argn
Definition: ppmlabel.c:24
#define TRUE
Definition: ppmlabel.c:15
#define FALSE
Definition: ppmlabel.c:16
static pixel rgbcolour
Definition: ppmlabel.c:27
static pixel backcolour
Definition: ppmlabel.c:27
static pixval maxval
Definition: ppmlabel.c:26
int main(int argc, argv)
Definition: ppmlabel.c:103
lft_cell * left
Definition: routines.h:73
Definition: ppm.h:33
#define FILE
Definition: t1stdio.h:34
*job_name strlen((char *) job_name) - 4)
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
FILE * ifp
Definition: t1asm.c:88
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269