1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23 #include "art_misc.h"
24 #include "art_uta.h"
25 #include "art_vpath.h"
26 #include "art_uta_vpath.h"
27 #include "art_rect.h"
28 #include "art_rect_uta.h"
29 #include "art_uta_rect.h"
30 
31 #undef LIBART_COMPILATION
32 #include "libart-features.h"
33 
34 #define TEST_UTA
35 #define noTEST_UTA_SPEED
36 
37 #define XOFF 50
38 #define YOFF 700
39 
40 static void
print_uta_ps(ArtUta * uta)41 print_uta_ps (ArtUta *uta)
42 {
43   int x, y;
44   int x0, y0, x1, y1;
45   int width = uta->width;
46   ArtUtaBbox ub;
47 
48   for (y = 0; y < uta->height; y++)
49     for (x = 0; x < width; x++)
50       {
51 	ub = uta->utiles[y * width + x];
52 	if (ub != 0)
53 	  {
54 	    x0 = (uta->x0 + x) * ART_UTILE_SIZE + ART_UTA_BBOX_X0(ub);
55 	    x1 = (uta->x0 + x) * ART_UTILE_SIZE + ART_UTA_BBOX_X1(ub);
56 	    y0 = (uta->y0 + y) * ART_UTILE_SIZE + ART_UTA_BBOX_Y0(ub);
57 	    y1 = (uta->y0 + y) * ART_UTILE_SIZE + ART_UTA_BBOX_Y1(ub);
58 	    printf ("%% tile %d, %d: %d %d %d %d\n",
59 		    x, y,
60 		    ART_UTA_BBOX_X0(ub),
61 		    ART_UTA_BBOX_Y0(ub),
62 		    ART_UTA_BBOX_X1(ub),
63 		    ART_UTA_BBOX_Y1(ub));
64 	    printf ("%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath fill\n",
65 		    XOFF + x0, YOFF - y0, XOFF + x1, YOFF - y0,
66 		    XOFF + x1, YOFF - y1, XOFF + x0, YOFF - y1);
67 	  }
68       }
69 }
70 
71 static void
print_rbuf_ps(int * rbuf,int width,int height)72 print_rbuf_ps (int *rbuf, int width, int height)
73 {
74   int x, y;
75 
76   for (y = 0; y < height; y++)
77     for (x = 0; x < width; x++)
78       if (1 && rbuf[y * width + x] != 0)
79 	printf ("%d %d moveto (%d) show\n", x * ART_UTILE_SIZE, y * ART_UTILE_SIZE,
80 		rbuf[y * width + x]);
81 }
82 
83 #if 0
84 void
85 randline (ArtUta *uta, int *rbuf, int rbuf_rowstride)
86 {
87   double x0, y0, x1, y1;
88 
89   x0 = rand () * (500.0 / RAND_MAX);
90   y0 = rand () * (500.0 / RAND_MAX);
91   x1 = rand () * (500.0 / RAND_MAX);
92   y1 = rand () * (500.0 / RAND_MAX);
93 
94   printf ("%g %g moveto %g %g lineto stroke\n", x0, y0, x1, y1);
95   art_uta_add_line (uta, x0, y0, x1, y1, rbuf, rbuf_rowstride);
96 }
97 #endif
98 
99 static void
print_ps_vpath(ArtVpath * vpath)100 print_ps_vpath (ArtVpath *vpath)
101 {
102   int i;
103 
104   for (i = 0; vpath[i].code != ART_END; i++)
105     {
106       switch (vpath[i].code)
107 	{
108 	case ART_MOVETO:
109 	  printf ("%g %g moveto\n", XOFF + vpath[i].x, YOFF - vpath[i].y);
110 	  break;
111 	case ART_LINETO:
112 	  printf ("%g %g lineto\n", XOFF + vpath[i].x, YOFF - vpath[i].y);
113 	  break;
114 	default:
115 	  break;
116 	}
117     }
118   printf ("stroke\n");
119 }
120 
121 static ArtVpath *
randstar(int n)122 randstar (int n)
123 {
124   ArtVpath *vec;
125   int i;
126   double r, th;
127 
128   vec = art_new (ArtVpath, n + 2);
129   for (i = 0; i < n; i++)
130     {
131       vec[i].code = i ? ART_LINETO : ART_MOVETO;
132       r = rand () * (250.0 / RAND_MAX);
133       th = i * 2 * M_PI / n;
134       vec[i].x = 250 + r * cos (th);
135       vec[i].y = 250 - r * sin (th);
136     }
137   vec[i].code = ART_LINETO;
138   vec[i].x = vec[0].x;
139   vec[i].y = vec[0].y;
140   i++;
141   vec[i].code = ART_END;
142   vec[i].x = 0;
143   vec[i].y = 0;
144   return vec;
145 }
146 
147 int
main(int argc,char ** argv)148 main (int argc, char **argv)
149 {
150   ArtUta *uta;
151   int i;
152   int *rbuf;
153   ArtVpath *vec;
154   ArtIRect *rects;
155   int n_rects;
156 
157   if (argc == 2)
158     srand (atoi (argv[1]));
159 
160 #ifdef TEST_UTA
161   printf ("%%!PS-Adobe\n");
162   printf ("%% libart version: %d.%d.%d\n",
163 	  libart_major_version, libart_minor_version, libart_micro_version);
164   printf ("/Helvetica findfont 12 scalefont setfont\n");
165   printf ("0.5 setlinewidth\n");
166 
167   printf ("0.5 setgray\n");
168   for (i = 0; i < 500; i += ART_UTILE_SIZE)
169     {
170       printf ("%d %d moveto %d %d lineto stroke\n",
171 	      XOFF, YOFF - i, XOFF + 500, YOFF - i);
172       printf ("%d %d moveto %d %d lineto stroke\n",
173 	      XOFF + i, YOFF, XOFF + i, YOFF - 500);
174     }
175 
176   printf ("/a {\n");
177 
178 #if 1
179   vec = randstar (50);
180   print_ps_vpath (vec);
181   uta = art_uta_from_vpath (vec);
182 #ifdef TEST_UTA_RECT
183   {
184     ArtIRect bbox = {5, 5, 450, 450};
185     uta = art_uta_from_irect (&bbox);
186   }
187 #endif
188   rbuf = NULL;
189 #else
190   uta = art_uta_new_coords (0, 0, 500, 500);
191 
192   rbuf = malloc (sizeof(int) * (500 >> ART_UTILE_SHIFT) * (500 >> ART_UTILE_SHIFT));
193   for (i = 0; i < 10; i++)
194     randline (uta, rbuf, 500 >> ART_UTILE_SHIFT);
195 #endif
196 
197   printf ("} def 1 0.5 0.5 setrgbcolor\n");
198 
199   print_uta_ps (uta);
200 
201   printf ("0 0 0.5 setrgbcolor\n");
202 
203   if (rbuf)
204     print_rbuf_ps (rbuf, 500 >> ART_UTILE_SHIFT, 500 >> ART_UTILE_SHIFT);
205 
206   printf ("0 setgray a\n");
207 
208   rects = art_rect_list_from_uta (uta, 256, 64, &n_rects);
209 
210   printf ("%% %d rectangles:\n0 0 1 setrgbcolor\n", n_rects);
211 
212   for (i = 0; i < n_rects; i++)
213     printf ("%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath stroke\n",
214 	    XOFF + rects[i].x0, YOFF - rects[i].y0,
215 	    XOFF + rects[i].x1, YOFF - rects[i].y0,
216 	    XOFF + rects[i].x1, YOFF - rects[i].y1,
217 	    XOFF + rects[i].x0, YOFF - rects[i].y1);
218 
219   printf ("showpage\n");
220 #endif
221 
222 #ifdef TEST_UTA_SPEED
223   for (i = 0; i < 1000; i++)
224     {
225       vec = randstar (50);
226       uta = art_uta_from_vpath (vec);
227       art_free (vec);
228       art_uta_free (uta);
229     }
230 #endif
231 
232   return 0;
233 }
234