pcb 4.1.1
An interactive printed circuit board layout editor.

extents.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <math.h>
00009 
00010 #include "global.h"
00011 #include "data.h"
00012 
00013 #include "hid.h"
00014 #include "hid_draw.h"
00015 #include "../hidint.h"
00016 #include "hid/common/draw_helpers.h"
00017 
00018 #ifdef HAVE_LIBDMALLOC
00019 #include <dmalloc.h>
00020 #endif
00021 
00022 static BoxType box;
00023 
00024 typedef struct hid_gc_struct
00025 {
00026   int width;
00027 } hid_gc_struct;
00028 
00029 static int
00030 extents_set_layer (const char *name, int group, int empty)
00031 {
00032   int idx = group;
00033   if (idx >= 0 && idx < max_group)
00034     {
00035       idx = PCB->LayerGroups.Entries[idx][0];
00036     }
00037   if (idx >= 0 && idx < max_copper_layer + SILK_LAYER)
00038     return 1;
00039   if (idx < 0)
00040     {
00041       switch (SL_TYPE (idx))
00042         {
00043         case SL_INVISIBLE:
00044         case SL_MASK:
00045         case SL_ASSY:
00046           return 0;
00047         case SL_SILK:
00048         case SL_PDRILL:
00049         case SL_UDRILL:
00050           return 1;
00051         }
00052     }
00053   return 0;
00054 }
00055 
00056 static hidGC
00057 extents_make_gc (void)
00058 {
00059   hidGC rv = (hidGC)malloc (sizeof (hid_gc_struct));
00060   memset (rv, 0, sizeof (hid_gc_struct));
00061   return rv;
00062 }
00063 
00064 static void
00065 extents_destroy_gc (hidGC gc)
00066 {
00067   free (gc);
00068 }
00069 
00070 static void
00071 extents_use_mask (enum mask_mode mode)
00072 {
00073 }
00074 
00075 static void
00076 extents_set_color (hidGC gc, const char *name)
00077 {
00078 }
00079 
00080 static void
00081 extents_set_line_cap (hidGC gc, EndCapStyle style)
00082 {
00083 }
00084 
00085 static void
00086 extents_set_line_width (hidGC gc, Coord width)
00087 {
00088   gc->width = width;
00089 }
00090 
00091 static void
00092 extents_set_draw_xor (hidGC gc, int xor_)
00093 {
00094 }
00095 
00096 #define PEX(x,w) if (box.X1 > (x)-(w)) box.X1 = (x)-(w); \
00097         if (box.X2 < (x)+(w)) box.X2 = (x)+(w)
00098 #define PEY(y,w) if (box.Y1 > (y)-(w)) box.Y1 = (y)-(w); \
00099         if (box.Y2 < (y)+(w)) box.Y2 = (y)+(w)
00100 
00101 static void
00102 extents_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00103 {
00104   PEX (x1, gc->width);
00105   PEY (y1, gc->width);
00106   PEX (x2, gc->width);
00107   PEY (y2, gc->width);
00108 }
00109 
00110 static void
00111 extents_draw_arc (hidGC gc, Coord cx, Coord cy, Coord width, Coord height,
00112                   Angle start_angle, Angle end_angle)
00113 {
00114   /* Naive but good enough.  */
00115   PEX (cx, width + gc->width);
00116   PEY (cy, height + gc->width);
00117 }
00118 
00119 static void
00120 extents_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00121 {
00122   PEX (x1, gc->width);
00123   PEY (y1, gc->width);
00124   PEX (x2, gc->width);
00125   PEY (y2, gc->width);
00126 }
00127 
00128 static void
00129 extents_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
00130 {
00131   PEX (cx, radius);
00132   PEY (cy, radius);
00133 }
00134 
00135 static void
00136 extents_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
00137 {
00138   int i;
00139   for (i = 0; i < n_coords; i++)
00140     {
00141       PEX (x[i], 0);
00142       PEY (y[i], 0);
00143     }
00144 }
00145 
00146 static void
00147 extents_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00148 {
00149   PEX (x1, 0);
00150   PEY (y1, 0);
00151   PEX (x2, 0);
00152   PEY (y2, 0);
00153 }
00154 
00155 static HID extents_hid;
00156 static HID_DRAW extents_graphics;
00157 
00158 void
00159 hid_extents_init (void)
00160 {
00161   static bool initialised = false;
00162 
00163   if (initialised)
00164     return;
00165 
00166   memset (&extents_hid, 0, sizeof (HID));
00167   memset (&extents_graphics, 0, sizeof (HID_DRAW));
00168 
00169   common_draw_helpers_init (&extents_graphics);
00170 
00171   extents_hid.struct_size         = sizeof (HID);
00172   extents_hid.name                = "extents-extents";
00173   extents_hid.description         = "used to calculate extents";
00174   extents_hid.poly_before         = 1;
00175 
00176   extents_hid.set_layer           = extents_set_layer;
00177 
00178   extents_hid.graphics            = &extents_graphics;
00179 
00180   extents_graphics.make_gc        = extents_make_gc;
00181   extents_graphics.destroy_gc     = extents_destroy_gc;
00182   extents_graphics.use_mask       = extents_use_mask;
00183   extents_graphics.set_color      = extents_set_color;
00184   extents_graphics.set_line_cap   = extents_set_line_cap;
00185   extents_graphics.set_line_width = extents_set_line_width;
00186   extents_graphics.set_draw_xor   = extents_set_draw_xor;
00187   extents_graphics.draw_line      = extents_draw_line;
00188   extents_graphics.draw_arc       = extents_draw_arc;
00189   extents_graphics.draw_rect      = extents_draw_rect;
00190   extents_graphics.fill_circle    = extents_fill_circle;
00191   extents_graphics.fill_polygon   = extents_fill_polygon;
00192   extents_graphics.fill_rect      = extents_fill_rect;
00193 
00194   initialised = true;
00195 }
00196 
00197 
00198 BoxType *
00199 hid_get_extents (void *item)
00200 {
00201   BoxType region;
00202 
00203   /* As this isn't a real "HID", we need to ensure we are initialised. */
00204   hid_extents_init ();
00205 
00206   box.X1 = COORD_MAX;
00207   box.Y1 = COORD_MAX;
00208   box.X2 = -COORD_MAX - 1;
00209   box.Y2 = -COORD_MAX - 1;
00210 
00211   region.X1 = -COORD_MAX - 1;
00212   region.Y1 = -COORD_MAX - 1;
00213   region.X2 = COORD_MAX;
00214   region.Y2 = COORD_MAX;
00215   hid_expose_callback (&extents_hid, &region, item);
00216 
00217   return &box;
00218 }