hid/common: Fix extents computation in nm units
[geda-pcb/pcjc2.git] / src / hid / common / extents.c
blob432e3742b81f87fa373c0bc87d77ee5a306ebbac
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
10 #include "global.h"
11 #include "data.h"
13 #include "hid.h"
14 #include "hid_draw.h"
15 #include "../hidint.h"
16 #include "hid/common/draw_helpers.h"
18 #ifdef HAVE_LIBDMALLOC
19 #include <dmalloc.h>
20 #endif
22 static BoxType box;
24 typedef struct hid_gc_struct
26 int width;
27 } hid_gc_struct;
29 static int
30 extents_set_layer (const char *name, int group, int empty)
32 int idx = group;
33 if (idx >= 0 && idx < max_group)
35 idx = PCB->LayerGroups.Entries[idx][0];
37 if (idx >= 0 && idx < max_copper_layer + 2)
38 return 1;
39 if (idx < 0)
41 switch (SL_TYPE (idx))
43 case SL_INVISIBLE:
44 case SL_MASK:
45 case SL_ASSY:
46 return 0;
47 case SL_SILK:
48 case SL_PDRILL:
49 case SL_UDRILL:
50 return 1;
53 return 0;
56 static hidGC
57 extents_make_gc (void)
59 hidGC rv = (hidGC)malloc (sizeof (hid_gc_struct));
60 memset (rv, 0, sizeof (hid_gc_struct));
61 return rv;
64 static void
65 extents_destroy_gc (hidGC gc)
67 free (gc);
70 static void
71 extents_use_mask (enum mask_mode mode)
75 static void
76 extents_set_color (hidGC gc, const char *name)
80 static void
81 extents_set_line_cap (hidGC gc, EndCapStyle style)
85 static void
86 extents_set_line_width (hidGC gc, Coord width)
88 gc->width = width;
91 static void
92 extents_set_draw_xor (hidGC gc, int xor_)
96 #define PEX(x,w) if (box.X1 > (x)-(w)) box.X1 = (x)-(w); \
97 if (box.X2 < (x)+(w)) box.X2 = (x)+(w)
98 #define PEY(y,w) if (box.Y1 > (y)-(w)) box.Y1 = (y)-(w); \
99 if (box.Y2 < (y)+(w)) box.Y2 = (y)+(w)
101 static void
102 extents_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
104 PEX (x1, gc->width);
105 PEY (y1, gc->width);
106 PEX (x2, gc->width);
107 PEY (y2, gc->width);
110 static void
111 extents_draw_arc (hidGC gc, Coord cx, Coord cy, Coord width, Coord height,
112 Angle start_angle, Angle end_angle)
114 /* Naive but good enough. */
115 PEX (cx, width + gc->width);
116 PEY (cy, height + gc->width);
119 static void
120 extents_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
122 PEX (x1, gc->width);
123 PEY (y1, gc->width);
124 PEX (x2, gc->width);
125 PEY (y2, gc->width);
128 static void
129 extents_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
131 PEX (cx, radius);
132 PEY (cy, radius);
135 static void
136 extents_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
138 int i;
139 for (i = 0; i < n_coords; i++)
141 PEX (x[i], 0);
142 PEY (y[i], 0);
146 static void
147 extents_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
149 PEX (x1, 0);
150 PEY (y1, 0);
151 PEX (x2, 0);
152 PEY (y2, 0);
155 static HID extents_hid;
156 static HID_DRAW extents_graphics;
158 void
159 hid_extents_init (void)
161 static bool initialised = false;
163 if (initialised)
164 return;
166 memset (&extents_hid, 0, sizeof (HID));
167 memset (&extents_graphics, 0, sizeof (HID_DRAW));
169 common_draw_helpers_init (&extents_graphics);
171 extents_hid.struct_size = sizeof (HID);
172 extents_hid.name = "extents-extents";
173 extents_hid.description = "used to calculate extents";
174 extents_hid.poly_before = 1;
176 extents_hid.set_layer = extents_set_layer;
178 extents_hid.graphics = &extents_graphics;
180 extents_graphics.make_gc = extents_make_gc;
181 extents_graphics.destroy_gc = extents_destroy_gc;
182 extents_graphics.use_mask = extents_use_mask;
183 extents_graphics.set_color = extents_set_color;
184 extents_graphics.set_line_cap = extents_set_line_cap;
185 extents_graphics.set_line_width = extents_set_line_width;
186 extents_graphics.set_draw_xor = extents_set_draw_xor;
187 extents_graphics.draw_line = extents_draw_line;
188 extents_graphics.draw_arc = extents_draw_arc;
189 extents_graphics.draw_rect = extents_draw_rect;
190 extents_graphics.fill_circle = extents_fill_circle;
191 extents_graphics.fill_polygon = extents_fill_polygon;
192 extents_graphics.fill_rect = extents_fill_rect;
194 initialised = true;
198 BoxType *
199 hid_get_extents (void *item)
201 BoxType region;
203 /* As this isn't a real "HID", we need to ensure we are initialised. */
204 hid_extents_init ();
206 box.X1 = COORD_MAX;
207 box.Y1 = COORD_MAX;
208 box.X2 = -COORD_MAX - 1;
209 box.Y2 = -COORD_MAX - 1;
211 region.X1 = -COORD_MAX - 1;
212 region.Y1 = -COORD_MAX - 1;
213 region.X2 = COORD_MAX;
214 region.Y2 = COORD_MAX;
215 hid_expose_callback (&extents_hid, &region, item);
217 return &box;