gtk/gl: Play with layer translucency
[geda-pcb/pcjc2.git] / src / fontmode.c
blobb9abcd428159d1ef5b6e9975dbcd78d1ef85b322
1 /*
2 * COPYRIGHT
4 * PCB, interactive printed circuit board design
5 * Copyright (C) 2006 DJ Delorie
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Contact addresses for paper mail and Email:
22 * DJ Delorie, 334 North Road, Deerfield NH 03037-1110, USA
23 * dj@delorie.com
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include "global.h"
33 #include <math.h>
34 #include <memory.h>
35 #include <limits.h>
38 #include "create.h"
39 #include "data.h"
40 #include "draw.h"
41 #include "misc.h"
42 #include "move.h"
43 #include "remove.h"
44 #include "rtree.h"
45 #include "strflags.h"
46 #include "undo.h"
47 #include "pcb-printf.h"
49 #ifdef HAVE_LIBDMALLOC
50 #include <dmalloc.h>
51 #endif
53 /* FIXME - we currently hardcode the grid and PCB size. What we
54 should do in the future is scan the font for its extents, and size
55 the grid appropriately. Also, when we convert back to a font, we
56 should search the grid for the gridlines and use them to figure out
57 where the symbols are. */
59 #define CELL_SIZE MIL_TO_COORD (100)
60 #define CELL_OFFSET MIL_TO_COORD (10)
62 #define XYtoSym(x,y) ((x1 + CELL_OFFSET) / CELL_SIZE - 1 \
63 + 16 * ((y1 + CELL_OFFSET) / CELL_SIZE - 1))
65 static const char fontedit_syntax[] = "FontEdit()";
67 static const char fontedit_help[] =
68 "Convert the current font to a PCB for editing.";
70 /* %start-doc actions FontEdit
72 This command only allows a font to be edited if the layout being edited contains
73 font symbols. The existing font symbols are displayed on a layer with an overlaid
74 grid, with the new version of the font on another layer which can be modified.
75 Font symbols consist only of lines. The blue lines next to the font symbols
76 indicate the space required after the symbol when it is used.
78 %end-doc */
80 static int
81 FontEdit (int argc, char **argv, Coord Ux, Coord Uy)
83 FontType *font;
84 SymbolType *symbol;
85 LayerType *lfont, *lorig, *lwidth, *lgrid;
86 int s, l;
88 if (hid_actionl ("New", "Font", 0))
89 return 1;
91 Settings.grid_unit = get_unit_struct("mil");
92 Settings.Bloat = PCB->Bloat = 1;
93 Settings.Shrink = PCB->Shrink = 1;
94 Settings.minWid = PCB->minWid = 1;
95 Settings.minSlk = PCB->minSlk = 1;
97 MoveLayerToGroup (max_copper_layer + TOP_SILK_LAYER, 0);
98 MoveLayerToGroup (max_copper_layer + BOTTOM_SILK_LAYER, 1);
100 while (PCB->Data->LayerN > 4)
101 MoveLayer (4, -1);
102 for (l = 0; l < 4; l++)
104 MoveLayerToGroup (l, l);
106 PCB->MaxWidth = CELL_SIZE * 18;
107 PCB->MaxHeight = CELL_SIZE * ((MAX_FONTPOSITION + 15) / 16 + 2);
108 PCB->Grid = MIL_TO_COORD (5);
109 PCB->Data->Layer[0].Name = strdup ("Font");
110 PCB->Data->Layer[1].Name = strdup ("OrigFont");
111 PCB->Data->Layer[2].Name = strdup ("Width");
112 PCB->Data->Layer[3].Name = strdup ("Grid");
113 hid_action ("PCBChanged");
114 hid_action ("LayersChanged");
116 lfont = PCB->Data->Layer + 0;
117 lorig = PCB->Data->Layer + 1;
118 lwidth = PCB->Data->Layer + 2;
119 lgrid = PCB->Data->Layer + 3;
121 font = &PCB->Font;
122 for (s = 0; s <= MAX_FONTPOSITION; s++)
124 Coord ox = (s % 16 + 1) * CELL_SIZE;
125 Coord oy = (s / 16 + 1) * CELL_SIZE;
126 Coord w, miny, maxy, maxx = 0;
128 symbol = &font->Symbol[s];
130 miny = MIL_TO_COORD (5);
131 maxy = font->MaxHeight;
133 for (l = 0; l < symbol->LineN; l++)
135 CreateDrawnLineOnLayer (lfont,
136 symbol->Line[l].Point1.X + ox,
137 symbol->Line[l].Point1.Y + oy,
138 symbol->Line[l].Point2.X + ox,
139 symbol->Line[l].Point2.Y + oy,
140 symbol->Line[l].Thickness,
141 symbol->Line[l].Thickness, NoFlags ());
142 CreateDrawnLineOnLayer (lorig, symbol->Line[l].Point1.X + ox,
143 symbol->Line[l].Point1.Y + oy,
144 symbol->Line[l].Point2.X + ox,
145 symbol->Line[l].Point2.Y + oy,
146 symbol->Line[l].Thickness,
147 symbol->Line[l].Thickness, NoFlags ());
148 if (maxx < symbol->Line[l].Point1.X)
149 maxx = symbol->Line[l].Point1.X;
150 if (maxx < symbol->Line[l].Point2.X)
151 maxx = symbol->Line[l].Point2.X;
153 w = maxx + symbol->Delta + ox;
154 CreateDrawnLineOnLayer (lwidth,
155 w, miny + oy,
156 w, maxy + oy, MIL_TO_COORD (1), MIL_TO_COORD (1), NoFlags ());
159 for (l = 0; l < 16; l++)
161 int x = (l + 1) * CELL_SIZE;
162 CreateDrawnLineOnLayer (lgrid, x, 0, x, PCB->MaxHeight, MIL_TO_COORD (1),
163 MIL_TO_COORD (1), NoFlags ());
165 for (l = 0; l <= MAX_FONTPOSITION / 16 + 1; l++)
167 int y = (l + 1) * CELL_SIZE;
168 CreateDrawnLineOnLayer (lgrid, 0, y, PCB->MaxWidth, y, MIL_TO_COORD (1),
169 MIL_TO_COORD (1), NoFlags ());
171 return 0;
174 static const char fontsave_syntax[] = "FontSave()";
176 static const char fontsave_help[] = "Convert the current PCB back to a font.";
178 /* %start-doc actions FontSave
180 Once a font has been modified with the FontEdit command, the layout can be saved
181 as a new PCB layout. The new PCB layout can then be opened with a text editor so
182 that the font section can be removed and saved as a new "default_font" file for
183 use in other PCB layouts.
185 %end-doc */
187 static int
188 FontSave (int argc, char **argv, Coord Ux, Coord Uy)
190 FontType *font;
191 SymbolType *symbol;
192 int i;
193 GList *ii;
194 LayerType *lfont, *lwidth;
196 font = &PCB->Font;
197 lfont = PCB->Data->Layer + 0;
198 lwidth = PCB->Data->Layer + 2;
200 for (i = 0; i <= MAX_FONTPOSITION; i++)
202 font->Symbol[i].LineN = 0;
203 font->Symbol[i].Valid = 0;
204 font->Symbol[i].Width = 0;
207 for (ii = lfont->Line; ii != NULL; ii = g_list_next (ii))
209 LineType *l = ii->data;
210 int x1 = l->Point1.X;
211 int y1 = l->Point1.Y;
212 int x2 = l->Point2.X;
213 int y2 = l->Point2.Y;
214 int ox, oy, s;
216 s = XYtoSym (x1, y1);
217 ox = (s % 16 + 1) * CELL_SIZE;
218 oy = (s / 16 + 1) * CELL_SIZE;
219 symbol = &PCB->Font.Symbol[s];
221 x1 -= ox;
222 y1 -= oy;
223 x2 -= ox;
224 y2 -= oy;
226 if (symbol->Width < x1)
227 symbol->Width = x1;
228 if (symbol->Width < x2)
229 symbol->Width = x2;
230 symbol->Valid = 1;
232 CreateNewLineInSymbol (symbol, x1, y1, x2, y2, l->Thickness);
235 for (ii = lwidth->Line; ii != NULL; ii = g_list_next (ii))
237 LineType *l = ii->data;
238 Coord x1 = l->Point1.X;
239 Coord y1 = l->Point1.Y;
240 Coord ox, s;
242 s = XYtoSym (x1, y1);
243 ox = (s % 16 + 1) * CELL_SIZE;
244 symbol = &PCB->Font.Symbol[s];
246 x1 -= ox;
248 symbol->Delta = x1 - symbol->Width;
251 SetFontInfo (font);
253 return 0;
256 HID_Action fontmode_action_list[] = {
257 {"FontEdit", 0, FontEdit,
258 fontedit_help, fontedit_syntax},
259 {"FontSave", 0, FontSave,
260 fontsave_help, fontsave_syntax}
263 REGISTER_ACTIONS (fontmode_action_list)