*** empty log message ***
[anjuta-git-plugin.git] / scintilla / XPM.cxx
blobd3bbb4dcc12176b8a797a424cb8674f7ddfafb6d
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.
8 #include <string.h>
9 #include <stdlib.h>
11 #include "Platform.h"
13 #include "XPM.h"
15 static const char *NextField(const char *s) {
16 // In case there are leading spaces in the string
17 while (*s && *s == ' ') {
18 s++;
20 while (*s && *s != ' ') {
21 s++;
23 while (*s && *s == ' ') {
24 s++;
26 return s;
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;
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;
45 return colours[0].allocated;
46 #endif
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));
56 XPM::XPM(const char *textForm) :
57 data(0), codes(0), colours(0), lines(0) {
58 Init(textForm);
61 XPM::XPM(const char * const *linesForm) :
62 data(0), codes(0), colours(0), lines(0) {
63 Init(linesForm);
66 XPM::~XPM() {
67 Clear();
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;
81 } else {
82 // It is really in line form
83 Init(reinterpret_cast<const char * const *>(textForm));
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;
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];
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;
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';
125 for (int code=0; code<256; code++) {
126 colourCodeTable[code] = 0;
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];
139 colourCodeTable[static_cast<unsigned char>(codes[c])] = &(colours[c]);
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;
154 void XPM::RefreshColourPalette(Palette &pal, bool want) {
155 if (!data || !codes || !colours || !lines) {
156 return;
158 for (int i=0; i<nColours; i++) {
159 pal.WantFind(colours[i], want);
163 void XPM::CopyDesiredColours() {
164 if (!data || !codes || !colours || !lines) {
165 return;
167 for (int i=0; i<nColours; i++) {
168 colours[i].Copy();
172 void XPM::Draw(Surface *surface, PRectangle &rc) {
173 if (!data || !codes || !colours || !lines) {
174 return;
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;
190 FillRun(surface, prevCode, startX + xStartRun, startY + y, startX + width);
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!
217 if (countQuotes / 2 >= strings) {
218 break; // Bad height or number of colors!
220 if ((countQuotes & 1) == 0) {
221 linesForm[countQuotes / 2] = textForm + j + 1;
223 countQuotes++;
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;
231 return linesForm;
234 // In future, may want to minimize search time by sorting and using a binary search.
236 XPMSet::XPMSet() : set(0), len(0), maximum(0), height(-1), width(-1) {
239 XPMSet::~XPMSet() {
240 Clear();
243 void XPMSet::Clear() {
244 for (int i = 0; i < len; i++) {
245 delete set[i];
247 delete []set;
248 set = 0;
249 len = 0;
250 maximum = 0;
251 height = -1;
252 width = -1;
255 void XPMSet::Add(int id, const char *textForm) {
256 // Invalidate cached dimensions
257 height = -1;
258 width = -1;
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;
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];
279 delete []set;
280 set = setNew;
282 set[len] = pxpm;
283 len++;
287 XPM *XPMSet::Get(int id) {
288 for (int i = 0; i < len; i++) {
289 if (set[i]->GetId() == id) {
290 return set[i];
293 return 0;
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();
304 return (height > 0) ? height : 0;
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();
315 return (width > 0) ? width : 0;