]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/XPM.cxx
Cast to void* before casting to Node** to make the compiler do no strict-aliasing...
[wxWidgets.git] / contrib / src / stc / scintilla / src / XPM.cxx
1 // Scintilla source code edit control
2 /** @file XPM.cxx
3 ** Define a class that holds data in the X Pixmap (XPM) format.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include "Platform.h"
12
13 #include "XPM.h"
14
15 static const char *NextField(const char *s) {
16 // In case there are leading spaces in the string
17 while (*s && *s == ' ') {
18 s++;
19 }
20 while (*s && *s != ' ') {
21 s++;
22 }
23 while (*s && *s == ' ') {
24 s++;
25 }
26 return s;
27 }
28
29 // Data lines in XPM can be terminated either with NUL or "
30 static size_t MeasureLength(const char *s) {
31 size_t i = 0;
32 while (s[i] && (s[i] != '\"'))
33 i++;
34 return i;
35 }
36
37 ColourAllocated XPM::ColourFromCode(int ch) {
38 return colourCodeTable[ch]->allocated;
39 #ifdef SLOW
40 for (int i=0; i<nColours; i++) {
41 if (codes[i] == ch) {
42 return colours[i].allocated;
43 }
44 }
45 return colours[0].allocated;
46 #endif
47 }
48
49 void XPM::FillRun(Surface *surface, int code, int startX, int y, int x) {
50 if ((code != codeTransparent) && (startX != x)) {
51 PRectangle rc(startX, y, x, y+1);
52 surface->FillRectangle(rc, ColourFromCode(code));
53 }
54 }
55
56 XPM::XPM(const char *textForm) :
57 data(0), codes(0), colours(0), lines(0) {
58 Init(textForm);
59 }
60
61 XPM::XPM(const char * const *linesForm) :
62 data(0), codes(0), colours(0), lines(0) {
63 Init(linesForm);
64 }
65
66 XPM::~XPM() {
67 Clear();
68 }
69
70 void XPM::Init(const char *textForm) {
71 Clear();
72 // Test done is two parts to avoid possibility of overstepping the memory
73 // if memcmp implemented strangely. Must be 4 bytes at least at destination.
74 if ((0 == memcmp(textForm, "/* X", 4)) && (0 == memcmp(textForm, "/* XPM */", 9))) {
75 // Build the lines form out of the text form
76 const char **linesForm = LinesFormFromTextForm(textForm);
77 if (linesForm != 0) {
78 Init(linesForm);
79 delete []linesForm;
80 }
81 } else {
82 // It is really in line form
83 Init(reinterpret_cast<const char * const *>(textForm));
84 }
85 }
86
87 void XPM::Init(const char * const *linesForm) {
88 Clear();
89 height = 1;
90 width = 1;
91 nColours = 1;
92 data = NULL;
93 codeTransparent = ' ';
94 codes = NULL;
95 colours = NULL;
96 lines = NULL;
97 if (!linesForm)
98 return;
99
100 const char *line0 = linesForm[0];
101 width = atoi(line0);
102 line0 = NextField(line0);
103 height = atoi(line0);
104 line0 = NextField(line0);
105 nColours = atoi(line0);
106 codes = new char[nColours];
107 colours = new ColourPair[nColours];
108
109 int strings = 1+height+nColours;
110 lines = new char *[strings];
111 size_t allocation = 0;
112 for (int i=0; i<strings; i++) {
113 allocation += MeasureLength(linesForm[i]) + 1;
114 }
115 data = new char[allocation];
116 char *nextBit = data;
117 for (int j=0; j<strings; j++) {
118 lines[j] = nextBit;
119 size_t len = MeasureLength(linesForm[j]);
120 memcpy(nextBit, linesForm[j], len);
121 nextBit += len;
122 *nextBit++ = '\0';
123 }
124
125 for (int code=0; code<256; code++) {
126 colourCodeTable[code] = 0;
127 }
128
129 for (int c=0; c<nColours; c++) {
130 const char *colourDef = linesForm[c+1];
131 codes[c] = colourDef[0];
132 colourDef += 4;
133 if (*colourDef == '#') {
134 colours[c].desired.Set(colourDef);
135 } else {
136 colours[c].desired = ColourDesired(0xff, 0xff, 0xff);
137 codeTransparent = codes[c];
138 }
139 colourCodeTable[static_cast<unsigned char>(codes[c])] = &(colours[c]);
140 }
141 }
142
143 void XPM::Clear() {
144 delete []data;
145 data = 0;
146 delete []codes;
147 codes = 0;
148 delete []colours;
149 colours = 0;
150 delete []lines;
151 lines = 0;
152 }
153
154 void XPM::RefreshColourPalette(Palette &pal, bool want) {
155 if (!data || !codes || !colours || !lines) {
156 return;
157 }
158 for (int i=0; i<nColours; i++) {
159 pal.WantFind(colours[i], want);
160 }
161 }
162
163 void XPM::CopyDesiredColours() {
164 if (!data || !codes || !colours || !lines) {
165 return;
166 }
167 for (int i=0; i<nColours; i++) {
168 colours[i].Copy();
169 }
170 }
171
172 void XPM::Draw(Surface *surface, PRectangle &rc) {
173 if (!data || !codes || !colours || !lines) {
174 return;
175 }
176 // Centre the pixmap
177 int startY = rc.top + (rc.Height() - height) / 2;
178 int startX = rc.left + (rc.Width() - width) / 2;
179 for (int y=0;y<height;y++) {
180 int prevCode = 0;
181 int xStartRun = 0;
182 for (int x=0; x<width; x++) {
183 int code = lines[y+nColours+1][x];
184 if (code != prevCode) {
185 FillRun(surface, prevCode, startX + xStartRun, startY + y, startX + x);
186 xStartRun = x;
187 prevCode = code;
188 }
189 }
190 FillRun(surface, prevCode, startX + xStartRun, startY + y, startX + width);
191 }
192 }
193
194 const char **XPM::LinesFormFromTextForm(const char *textForm) {
195 // Build the lines form out of the text form
196 const char **linesForm = 0;
197 int countQuotes = 0;
198 int strings=1;
199 int j=0;
200 for (; countQuotes < (2*strings) && textForm[j] != '\0'; j++) {
201 if (textForm[j] == '\"') {
202 if (countQuotes == 0) {
203 // First field: width, height, number of colors, chars per pixel
204 const char *line0 = textForm + j + 1;
205 // Skip width
206 line0 = NextField(line0);
207 // Add 1 line for each pixel of height
208 strings += atoi(line0);
209 line0 = NextField(line0);
210 // Add 1 line for each colour
211 strings += atoi(line0);
212 linesForm = new const char *[strings];
213 if (linesForm == 0) {
214 break; // Memory error!
215 }
216 }
217 if (countQuotes / 2 >= strings) {
218 break; // Bad height or number of colors!
219 }
220 if ((countQuotes & 1) == 0) {
221 linesForm[countQuotes / 2] = textForm + j + 1;
222 }
223 countQuotes++;
224 }
225 }
226 if (textForm[j] == '\0' || countQuotes / 2 > strings) {
227 // Malformed XPM! Height + number of colors too high or too low
228 delete []linesForm;
229 linesForm = 0;
230 }
231 return linesForm;
232 }
233
234 // In future, may want to minimize search time by sorting and using a binary search.
235
236 XPMSet::XPMSet() : set(0), len(0), maximum(0), height(-1), width(-1) {
237 }
238
239 XPMSet::~XPMSet() {
240 Clear();
241 }
242
243 void XPMSet::Clear() {
244 for (int i = 0; i < len; i++) {
245 delete set[i];
246 }
247 delete []set;
248 set = 0;
249 len = 0;
250 maximum = 0;
251 height = -1;
252 width = -1;
253 }
254
255 void XPMSet::Add(int id, const char *textForm) {
256 // Invalidate cached dimensions
257 height = -1;
258 width = -1;
259
260 // Replace if this id already present
261 for (int i = 0; i < len; i++) {
262 if (set[i]->GetId() == id) {
263 set[i]->Init(textForm);
264 return;
265 }
266 }
267
268 // Not present, so add to end
269 XPM *pxpm = new XPM(textForm);
270 if (pxpm) {
271 pxpm->SetId(id);
272 pxpm->CopyDesiredColours();
273 if (len == maximum) {
274 maximum += 64;
275 XPM **setNew = new XPM *[maximum];
276 for (int i = 0; i < len; i++) {
277 setNew[i] = set[i];
278 }
279 delete []set;
280 set = setNew;
281 }
282 set[len] = pxpm;
283 len++;
284 }
285 }
286
287 XPM *XPMSet::Get(int id) {
288 for (int i = 0; i < len; i++) {
289 if (set[i]->GetId() == id) {
290 return set[i];
291 }
292 }
293 return 0;
294 }
295
296 int XPMSet::GetHeight() {
297 if (height < 0) {
298 for (int i = 0; i < len; i++) {
299 if (height < set[i]->GetHeight()) {
300 height = set[i]->GetHeight();
301 }
302 }
303 }
304 return (height > 0) ? height : 0;
305 }
306
307 int XPMSet::GetWidth() {
308 if (width < 0) {
309 for (int i = 0; i < len; i++) {
310 if (width < set[i]->GetWidth()) {
311 width = set[i]->GetWidth();
312 }
313 }
314 }
315 return (width > 0) ? width : 0;
316 }