~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Typeset/Boxes/Basic/basic_boxes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : basic.hpp
 
4
* DESCRIPTION: Basic boxes are the most elementary boxes.
 
5
*              We have implemented the following ones:
 
6
*                - a test box
 
7
*                - line boxes
 
8
* COPYRIGHT  : (C) 1999  Joris van der Hoeven
 
9
*******************************************************************************
 
10
* This software falls under the GNU general public license and comes WITHOUT
 
11
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
 
12
* If you don't have this file, write to the Free Software Foundation, Inc.,
 
13
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
14
******************************************************************************/
 
15
 
 
16
#include "boxes.hpp"
 
17
#include "formatter.hpp"
 
18
 
 
19
#include <math.h>
 
20
 
 
21
/******************************************************************************
 
22
* The test box
 
23
******************************************************************************/
 
24
 
 
25
struct test_box_rep: public box_rep {
 
26
  test_box_rep (path ip): box_rep (ip) {
 
27
    x1=x3=0; x2=x4=50<<8; y1=y3=0; y2=y4= 25<<8; }
 
28
  operator tree () { return "test"; }
 
29
  void display (ps_device dev);
 
30
};
 
31
 
 
32
void
 
33
test_box_rep::display (ps_device dev) {
 
34
  dev->set_color (dev->green);
 
35
  dev->set_line_style (PIXEL);
 
36
  dev->line (x1, y1, x2, y2);
 
37
  dev->line (x1, y2, x2, y1);
 
38
  // dev->arc (x1, y1, x2, y2, 45*64, 90*64);
 
39
}
 
40
 
 
41
/******************************************************************************
 
42
* Line boxes
 
43
******************************************************************************/
 
44
 
 
45
struct line_box_rep: public box_rep {
 
46
  SI    X1, Y1, X2, Y2;
 
47
  SI    width;
 
48
  color col;
 
49
 
 
50
  line_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b, SI w, color c);
 
51
  operator tree () { return "line"; }
 
52
  void display (ps_device dev);
 
53
};
 
54
 
 
55
line_box_rep::line_box_rep (
 
56
  path ip, SI X1b, SI Y1b, SI X2b, SI Y2b, SI w, color c):
 
57
    box_rep (ip)
 
58
{
 
59
  X1     = X1b;
 
60
  Y1     = Y1b;
 
61
  X2     = X2b;
 
62
  Y2     = Y2b;
 
63
  width  = w;
 
64
  col    = c;
 
65
  x1= min (X1, X2); y1= min (Y1, Y2);
 
66
  x2= max (X1, X2); y2= max (Y1, Y2);
 
67
  x3= x1-(w>>1);    y3= y1-(w>>1); 
 
68
  x4= x2+(w>>1);    y4= y2+(w>>1);
 
69
}
 
70
 
 
71
void
 
72
line_box_rep::display (ps_device dev) {
 
73
  dev->set_line_style (width);
 
74
  dev->set_color (col);
 
75
  dev->line (X1, Y1, X2, Y2);
 
76
}
 
77
 
 
78
/******************************************************************************
 
79
* Polygon boxes
 
80
******************************************************************************/
 
81
 
 
82
struct polygon_box_rep: public box_rep {
 
83
  array<SI> x, y;
 
84
  color fill, outline;
 
85
  SI w;
 
86
 
 
87
  polygon_box_rep (path ip, array<SI> x, array<SI> y, SI w, color f, color o);
 
88
  operator tree () { return "polygon"; }
 
89
  void display (ps_device dev);
 
90
};
 
91
 
 
92
polygon_box_rep::polygon_box_rep (
 
93
  path ip, array<SI> X, array<SI> Y, SI W, color f, color o):
 
94
    box_rep (ip), x (X), y (Y), fill (f), outline (o), w (W)
 
95
{
 
96
  int i, n= N(x);
 
97
  x1= x2= x[0]; y1= y2= y[0];
 
98
  for (i=1; i<n; i++) {
 
99
    x1= min (x1, x[i]); y1= min (y1, y[i]);
 
100
    x2= max (x2, x[i]); y2= max (y2, y[i]);
 
101
  }
 
102
  x3= x1-(w>>1); y3= y1-(w>>1); 
 
103
  x4= x2+(w>>1); y4= y2+(w>>1);
 
104
}
 
105
 
 
106
void
 
107
polygon_box_rep::display (ps_device dev) {
 
108
  dev->set_color (fill);
 
109
  dev->polygon (x, y);
 
110
  if (w>0) {
 
111
    int i, n= N(x);
 
112
    dev->set_line_style (w);
 
113
    dev->set_color (outline);
 
114
    for (i=0; i<n; i++)
 
115
      dev->line (x[i], y[i], x[(i+1)%n], y[(i+1)%n]);
 
116
  }
 
117
}
 
118
 
 
119
/******************************************************************************
 
120
* Arc boxes
 
121
******************************************************************************/
 
122
 
 
123
struct arc_box_rep: public box_rep {
 
124
  SI    X1, Y1, X2, Y2;
 
125
  int   a1, a2;
 
126
  SI    width;
 
127
  color col;
 
128
 
 
129
  arc_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b,
 
130
               int A1, int A2, SI w, color c);
 
131
  operator tree () { return "arc"; }
 
132
  void display (ps_device dev);
 
133
};
 
134
 
 
135
arc_box_rep::arc_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b,
 
136
                          int a1b, int a2b, SI w, color c): box_rep (ip)
 
137
{
 
138
  X1     = X1b;
 
139
  Y1     = Y1b;
 
140
  X2     = X2b;
 
141
  Y2     = Y2b;
 
142
  a1     = a1b;
 
143
  a2     = a2b;
 
144
  width  = w;
 
145
  col    = c;
 
146
 
 
147
  double scale= 3.1415927 / (180<<6);
 
148
  SI P1= ((X1+X2)/2) + (SI) (((X2-X1)/2) * cos (((double) a1) * scale));
 
149
  SI Q1= ((Y1+Y2)/2) + (SI) (((Y2-Y1)/2) * sin (((double) a1) * scale));
 
150
  SI P2= ((X1+X2)/2) + (SI) (((X2-X1)/2) * cos (((double) a2) * scale));
 
151
  SI Q2= ((Y1+Y2)/2) + (SI) (((Y2-Y1)/2) * sin (((double) a2) * scale));
 
152
  SI p1= min (P1, P2);
 
153
  SI q1= min (Q1, Q2);
 
154
  SI p2= max (P1, P2);
 
155
  SI q2= max (Q1, Q2);
 
156
 
 
157
  int s= (a1>>6)%360;
 
158
  int d= ((a2-a1)>>6);
 
159
  if ((s< 90) && ((s+d)> 90)) q2= Y2;
 
160
  if ((s<180) && ((s+d)>180)) p1= X1;
 
161
  if ((s<270) && ((s+d)>270)) q1= Y1;
 
162
  if ((s<360) && ((s+d)>360)) p2= X2;
 
163
  if ((s<450) && ((s+d)>450)) q2= Y2;
 
164
  if ((s<540) && ((s+d)>540)) p1= X1;
 
165
  if ((s<630) && ((s+d)>630)) q1= Y1;
 
166
 
 
167
  x1= min (p1, p2); y1= min (q1, q2);
 
168
  x2= max (p1, p2); y2= max (q1, q2);
 
169
  x3= x1-(w>>1);    y3= y1-(w>>1); 
 
170
  x4= x2+(w>>1);    y4= y2+(w>>1);
 
171
}
 
172
 
 
173
void
 
174
arc_box_rep::display (ps_device dev) {
 
175
  dev->set_line_style (width);
 
176
  dev->set_color (col);
 
177
  dev->arc (X1, Y1, X2, Y2, a1, a2-a1);
 
178
  // dev->line (x1, y1, x2, y2);
 
179
}
 
180
 
 
181
/******************************************************************************
 
182
* Postscript boxes
 
183
******************************************************************************/
 
184
 
 
185
struct postscript_box_rep: public box_rep {
 
186
  url image;
 
187
  int X1, Y1, X2, Y2;
 
188
 
 
189
  postscript_box_rep (path ip, url image2, SI w, SI h,
 
190
                      int X1, int Y1, int X2, int Y2);
 
191
  operator tree () { return "postscript"; }
 
192
  void display (ps_device dev);
 
193
};
 
194
 
 
195
postscript_box_rep::postscript_box_rep (
 
196
  path ip, url image2,
 
197
  SI w, SI h, int X1b, int Y1b, int X2b, int Y2b):
 
198
    box_rep (ip), image (image2)
 
199
{
 
200
  X1= X1b; Y1= Y1b;
 
201
  X2= X2b; Y2= Y2b;
 
202
  x1= x3= 0; y1= y3= 0;
 
203
  x2= x4= w; y2= y4= h;
 
204
}
 
205
 
 
206
void
 
207
postscript_box_rep::display (ps_device dev) {
 
208
  dev->postscript (image, x2, y2, 0, 0, X1, Y1, X2, Y2);
 
209
}
 
210
 
 
211
/******************************************************************************
 
212
* Control boxes
 
213
******************************************************************************/
 
214
 
 
215
struct control_tree_box_rep: public box_rep {
 
216
  tree t;
 
217
  control_tree_box_rep (path ip, tree t2, font fn): box_rep (ip), t (t2) {
 
218
    x1=x2=x3=x4=y1=y3=y4=0; y2=fn->yx; }
 
219
  operator tree () { return tuple ("control tree", (tree) t); }
 
220
  void display (ps_device dev) { (void) dev; }
 
221
  tree get_leaf_tree () { return t; }
 
222
};
 
223
 
 
224
struct control_lazy_box_rep: public box_rep {
 
225
  lazy lz;
 
226
  control_lazy_box_rep (path ip, lazy lz2, font fn): box_rep (ip), lz (lz2) {
 
227
    x1=x2=x3=x4=y1=y3=y4=0; y2=fn->yx; }
 
228
  operator tree () { return tuple ("control lazy", (tree) lz); }
 
229
  void display (ps_device dev) { (void) dev; }
 
230
  lazy get_leaf_lazy () { return lz; }
 
231
};
 
232
 
 
233
/******************************************************************************
 
234
* box construction routines
 
235
******************************************************************************/
 
236
 
 
237
box
 
238
test_box (path ip) {
 
239
  return new test_box_rep (ip);
 
240
}
 
241
 
 
242
box
 
243
line_box (path ip, SI x1, SI y1, SI x2, SI y2, SI w, SI c) {
 
244
  return new line_box_rep (ip, x1, y1, x2, y2, w, c);
 
245
}
 
246
 
 
247
box
 
248
arc_box (path ip, SI x1, SI y1, SI x2, SI y2, int a1, int a2, SI w, SI c) {
 
249
  return new arc_box_rep (ip, x1, y1, x2, y2, a1, a2, w, c);
 
250
}
 
251
 
 
252
box
 
253
polygon_box (path ip, array<SI> x, array<SI> y, SI w, color cf, color cl) {
 
254
  return new polygon_box_rep (ip, x, y, w, cf, cl);
 
255
}
 
256
 
 
257
box
 
258
polygon_box (path ip, array<SI> x, array<SI> y, color c) {
 
259
  return new polygon_box_rep (ip, x, y, 0, c, c);
 
260
}
 
261
 
 
262
box
 
263
postscript_box (path ip, url image, SI w, SI h,
 
264
                int x1, int y1, int x2, int y2)
 
265
{
 
266
  return new postscript_box_rep (ip, image, w, h, x1, y1, x2, y2);
 
267
}
 
268
 
 
269
box
 
270
control_box (path ip, tree t, font fn) {
 
271
  return new control_tree_box_rep (ip, t, fn);
 
272
}
 
273
 
 
274
box
 
275
control_box (path ip, lazy lz, font fn) {
 
276
  return new control_lazy_box_rep (ip, lz, fn);
 
277
}