Back to index

texmacs  1.0.7.15
basic_boxes.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : basic.hpp
00004 * DESCRIPTION: Basic boxes are the most elementary boxes.
00005 *              We have implemented the following ones:
00006 *                - a test box
00007 *                - line boxes
00008 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00009 *******************************************************************************
00010 * This software falls under the GNU general public license version 3 or later.
00011 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00012 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00013 ******************************************************************************/
00014 
00015 #include "boxes.hpp"
00016 #include "formatter.hpp"
00017 
00018 /******************************************************************************
00019 * The test box
00020 ******************************************************************************/
00021 
00022 struct test_box_rep: public box_rep {
00023   test_box_rep (path ip): box_rep (ip) {
00024     x1=x3=0; x2=x4=50<<8; y1=y3=0; y2=y4= 25<<8; }
00025   operator tree () { return "test"; }
00026   void display (renderer ren);
00027 };
00028 
00029 void
00030 test_box_rep::display (renderer ren) {
00031   ren->set_color (green);
00032   ren->set_line_style (PIXEL);
00033   ren->line (x1, y1, x2, y2);
00034   ren->line (x1, y2, x2, y1);
00035   // ren->arc (x1, y1, x2, y2, 45*64, 90*64);
00036 }
00037 
00038 /******************************************************************************
00039 * Line boxes
00040 ******************************************************************************/
00041 
00042 struct line_box_rep: public box_rep {
00043   SI    X1, Y1, X2, Y2;
00044   SI    width;
00045   color col;
00046 
00047   line_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b, SI w, color c);
00048   operator tree () { return "line"; }
00049   void display (renderer ren);
00050 };
00051 
00052 line_box_rep::line_box_rep (
00053   path ip, SI X1b, SI Y1b, SI X2b, SI Y2b, SI w, color c):
00054     box_rep (ip)
00055 {
00056   X1     = X1b;
00057   Y1     = Y1b;
00058   X2     = X2b;
00059   Y2     = Y2b;
00060   width  = w;
00061   col    = c;
00062   x1= min (X1, X2); y1= min (Y1, Y2);
00063   x2= max (X1, X2); y2= max (Y1, Y2);
00064   x3= x1-(w>>1);    y3= y1-(w>>1); 
00065   x4= x2+(w>>1);    y4= y2+(w>>1);
00066 }
00067 
00068 void
00069 line_box_rep::display (renderer ren) {
00070   ren->set_line_style (width);
00071   ren->set_color (col);
00072   ren->line (X1, Y1, X2, Y2);
00073 }
00074 
00075 /******************************************************************************
00076 * Polygon boxes
00077 ******************************************************************************/
00078 
00079 struct polygon_box_rep: public box_rep {
00080   array<SI> x, y;
00081   color fill, outline;
00082   SI w;
00083 
00084   polygon_box_rep (path ip, array<SI> x, array<SI> y, SI w, color f, color o);
00085   operator tree () { return "polygon"; }
00086   void display (renderer ren);
00087 };
00088 
00089 polygon_box_rep::polygon_box_rep (
00090   path ip, array<SI> X, array<SI> Y, SI W, color f, color o):
00091     box_rep (ip), x (X), y (Y), fill (f), outline (o), w (W)
00092 {
00093   int i, n= N(x);
00094   x1= x2= x[0]; y1= y2= y[0];
00095   for (i=1; i<n; i++) {
00096     x1= min (x1, x[i]); y1= min (y1, y[i]);
00097     x2= max (x2, x[i]); y2= max (y2, y[i]);
00098   }
00099   x3= x1-(w>>1); y3= y1-(w>>1); 
00100   x4= x2+(w>>1); y4= y2+(w>>1);
00101 }
00102 
00103 void
00104 polygon_box_rep::display (renderer ren) {
00105   ren->set_color (fill);
00106   ren->polygon (x, y);
00107   if (w>0) {
00108     int i, n= N(x);
00109     ren->set_line_style (w);
00110     ren->set_color (outline);
00111     for (i=0; i<n; i++)
00112       ren->line (x[i], y[i], x[(i+1)%n], y[(i+1)%n]);
00113   }
00114 }
00115 
00116 /******************************************************************************
00117 * Arc boxes
00118 ******************************************************************************/
00119 
00120 struct arc_box_rep: public box_rep {
00121   SI    X1, Y1, X2, Y2;
00122   int   a1, a2;
00123   SI    width;
00124   color col;
00125 
00126   arc_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b,
00127               int A1, int A2, SI w, color c);
00128   operator tree () { return "arc"; }
00129   void display (renderer ren);
00130 };
00131 
00132 arc_box_rep::arc_box_rep (path ip, SI X1b, SI Y1b, SI X2b, SI Y2b,
00133                        int a1b, int a2b, SI w, color c): box_rep (ip)
00134 {
00135   X1     = X1b;
00136   Y1     = Y1b;
00137   X2     = X2b;
00138   Y2     = Y2b;
00139   a1     = a1b;
00140   a2     = a2b;
00141   width  = w;
00142   col    = c;
00143 
00144   double scale= 3.1415927 / (180<<6);
00145   SI P1= ((X1+X2)/2) + (SI) (((X2-X1)/2) * cos (((double) a1) * scale));
00146   SI Q1= ((Y1+Y2)/2) + (SI) (((Y2-Y1)/2) * sin (((double) a1) * scale));
00147   SI P2= ((X1+X2)/2) + (SI) (((X2-X1)/2) * cos (((double) a2) * scale));
00148   SI Q2= ((Y1+Y2)/2) + (SI) (((Y2-Y1)/2) * sin (((double) a2) * scale));
00149   SI p1= min (P1, P2);
00150   SI q1= min (Q1, Q2);
00151   SI p2= max (P1, P2);
00152   SI q2= max (Q1, Q2);
00153 
00154   int s= (a1>>6)%360;
00155   int d= ((a2-a1)>>6);
00156   if ((s< 90) && ((s+d)> 90)) q2= Y2;
00157   if ((s<180) && ((s+d)>180)) p1= X1;
00158   if ((s<270) && ((s+d)>270)) q1= Y1;
00159   if ((s<360) && ((s+d)>360)) p2= X2;
00160   if ((s<450) && ((s+d)>450)) q2= Y2;
00161   if ((s<540) && ((s+d)>540)) p1= X1;
00162   if ((s<630) && ((s+d)>630)) q1= Y1;
00163 
00164   x1= min (p1, p2); y1= min (q1, q2);
00165   x2= max (p1, p2); y2= max (q1, q2);
00166   x3= x1-(w>>1);    y3= y1-(w>>1); 
00167   x4= x2+(w>>1);    y4= y2+(w>>1);
00168 }
00169 
00170 void
00171 arc_box_rep::display (renderer ren) {
00172   ren->set_line_style (width);
00173   ren->set_color (col);
00174   ren->arc (X1, Y1, X2, Y2, a1, a2-a1);
00175   // ren->line (x1, y1, x2, y2);
00176 }
00177 
00178 /******************************************************************************
00179 * Image boxes
00180 ******************************************************************************/
00181 
00182 struct image_box_rep: public box_rep {
00183   url u;
00184   int alpha;
00185   image_box_rep (path ip, url u2, SI w, SI h, int alpha);
00186   operator tree () { return "image"; }
00187   void display (renderer ren);
00188 };
00189 
00190 image_box_rep::image_box_rep (path ip, url u2, SI w, SI h, int a):
00191   box_rep (ip), u (u2), alpha (a)
00192 {
00193   x1= x3= 0; y1= y3= 0;
00194   x2= x4= w; y2= y4= h;
00195 }
00196 
00197 void
00198 image_box_rep::display (renderer ren) {
00199   ren->image (u, x2, y2, 0, 0, 0.0, 0.0, 1.0, 1.0, alpha);
00200 }
00201 
00202 /******************************************************************************
00203 * Control boxes
00204 ******************************************************************************/
00205 
00206 struct control_tree_box_rep: public box_rep {
00207   tree t;
00208   control_tree_box_rep (path ip, tree t2, font fn): box_rep (ip), t (t2) {
00209     x1=x2=x3=x4=y1=y3=y4=0; y2=fn->yx; }
00210   operator tree () { return tuple ("control tree", (tree) t); }
00211   void display (renderer ren) { (void) ren; }
00212   tree get_leaf_tree () { return t; }
00213 };
00214 
00215 struct control_lazy_box_rep: public box_rep {
00216   lazy lz;
00217   control_lazy_box_rep (path ip, lazy lz2, font fn): box_rep (ip), lz (lz2) {
00218     x1=x2=x3=x4=y1=y3=y4=0; y2=fn->yx; }
00219   operator tree () { return tuple ("control lazy", (tree) lz); }
00220   void display (renderer ren) { (void) ren; }
00221   lazy get_leaf_lazy () { return lz; }
00222 };
00223 
00224 /******************************************************************************
00225 * box construction routines
00226 ******************************************************************************/
00227 
00228 box
00229 test_box (path ip) {
00230   return tm_new<test_box_rep> (ip);
00231 }
00232 
00233 box
00234 line_box (path ip, SI x1, SI y1, SI x2, SI y2, SI w, color c) {
00235   return tm_new<line_box_rep> (ip, x1, y1, x2, y2, w, c);
00236 }
00237 
00238 box
00239 arc_box (path ip, SI x1, SI y1, SI x2, SI y2, int a1, int a2, SI w, color c) {
00240   return tm_new<arc_box_rep> (ip, x1, y1, x2, y2, a1, a2, w, c);
00241 }
00242 
00243 box
00244 polygon_box (path ip, array<SI> x, array<SI> y, SI w, color cf, color cl) {
00245   return tm_new<polygon_box_rep> (ip, x, y, w, cf, cl);
00246 }
00247 
00248 box
00249 polygon_box (path ip, array<SI> x, array<SI> y, color c) {
00250   return tm_new<polygon_box_rep> (ip, x, y, 0, c, c);
00251 }
00252 
00253 box
00254 image_box (path ip, url u, SI w, SI h, int alpha) {
00255   return tm_new<image_box_rep> (ip, u, w, h, alpha);
00256 }
00257 
00258 box
00259 control_box (path ip, tree t, font fn) {
00260   return tm_new<control_tree_box_rep> (ip, t, fn);
00261 }
00262 
00263 box
00264 control_box (path ip, lazy lz, font fn) {
00265   return tm_new<control_lazy_box_rep> (ip, lz, fn);
00266 }