]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/XPM.cxx
36e7994f46c4dd9b562523c48df0db82de9f5363
1 // Scintilla source code edit control
3 ** Define a class that holds data in the X Pixmap (XPM) format.
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.
15 static const char *NextField(const char *s
) {
16 // In case there are leading spaces in the string
17 while (*s
&& *s
== ' ') {
20 while (*s
&& *s
!= ' ') {
23 while (*s
&& *s
== ' ') {
29 // Data lines in XPM can be terminated either with NUL or "
30 static size_t MeasureLength(const char *s
) {
32 while (s
[i
] && (s
[i
] != '\"'))
37 ColourAllocated
XPM::ColourFromCode(int ch
) {
38 return colourCodeTable
[ch
]->allocated
;
40 for (int i
=0; i
<nColours
; i
++) {
42 return colours
[i
].allocated
;
45 return colours
[0].allocated
;
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) {
61 XPM::XPM(const char * const *linesForm
) :
62 data(0), codes(0), colours(0), lines(0) {
70 void XPM::Init(const char *textForm
) {
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
);
82 // It is really in line form
83 Init(reinterpret_cast<const char * const *>(textForm
));
87 void XPM::Init(const char * const *linesForm
) {
93 codeTransparent
= ' ';
100 const char *line0
= linesForm
[0];
102 line0
= NextField(line0
);
103 height
= atoi(line0
);
104 line0
= NextField(line0
);
105 nColours
= atoi(line0
);
106 line0
= NextField(line0
);
107 if (atoi(line0
) != 1) {
108 // Only one char per pixel is supported
111 codes
= new char[nColours
];
112 colours
= new ColourPair
[nColours
];
114 int strings
= 1+height
+nColours
;
115 lines
= new char *[strings
];
116 size_t allocation
= 0;
117 for (int i
=0; i
<strings
; i
++) {
118 allocation
+= MeasureLength(linesForm
[i
]) + 1;
120 data
= new char[allocation
];
121 char *nextBit
= data
;
122 for (int j
=0; j
<strings
; j
++) {
124 size_t len
= MeasureLength(linesForm
[j
]);
125 memcpy(nextBit
, linesForm
[j
], len
);
130 for (int code
=0; code
<256; code
++) {
131 colourCodeTable
[code
] = 0;
134 for (int c
=0; c
<nColours
; c
++) {
135 const char *colourDef
= linesForm
[c
+1];
136 codes
[c
] = colourDef
[0];
138 if (*colourDef
== '#') {
139 colours
[c
].desired
.Set(colourDef
);
141 colours
[c
].desired
= ColourDesired(0xff, 0xff, 0xff);
142 codeTransparent
= codes
[c
];
144 colourCodeTable
[static_cast<unsigned char>(codes
[c
])] = &(colours
[c
]);
159 void XPM::RefreshColourPalette(Palette
&pal
, bool want
) {
160 if (!data
|| !codes
|| !colours
|| !lines
) {
163 for (int i
=0; i
<nColours
; i
++) {
164 pal
.WantFind(colours
[i
], want
);
168 void XPM::CopyDesiredColours() {
169 if (!data
|| !codes
|| !colours
|| !lines
) {
172 for (int i
=0; i
<nColours
; i
++) {
177 void XPM::Draw(Surface
*surface
, PRectangle
&rc
) {
178 if (!data
|| !codes
|| !colours
|| !lines
) {
182 int startY
= rc
.top
+ (rc
.Height() - height
) / 2;
183 int startX
= rc
.left
+ (rc
.Width() - width
) / 2;
184 for (int y
=0;y
<height
;y
++) {
187 for (int x
=0; x
<width
; x
++) {
188 int code
= lines
[y
+nColours
+1][x
];
189 if (code
!= prevCode
) {
190 FillRun(surface
, prevCode
, startX
+ xStartRun
, startY
+ y
, startX
+ x
);
195 FillRun(surface
, prevCode
, startX
+ xStartRun
, startY
+ y
, startX
+ width
);
199 const char **XPM::LinesFormFromTextForm(const char *textForm
) {
200 // Build the lines form out of the text form
201 const char **linesForm
= 0;
205 for (; countQuotes
< (2*strings
) && textForm
[j
] != '\0'; j
++) {
206 if (textForm
[j
] == '\"') {
207 if (countQuotes
== 0) {
208 // First field: width, height, number of colors, chars per pixel
209 const char *line0
= textForm
+ j
+ 1;
211 line0
= NextField(line0
);
212 // Add 1 line for each pixel of height
213 strings
+= atoi(line0
);
214 line0
= NextField(line0
);
215 // Add 1 line for each colour
216 strings
+= atoi(line0
);
217 linesForm
= new const char *[strings
];
218 if (linesForm
== 0) {
219 break; // Memory error!
222 if (countQuotes
/ 2 >= strings
) {
223 break; // Bad height or number of colors!
225 if ((countQuotes
& 1) == 0) {
226 linesForm
[countQuotes
/ 2] = textForm
+ j
+ 1;
231 if (textForm
[j
] == '\0' || countQuotes
/ 2 > strings
) {
232 // Malformed XPM! Height + number of colors too high or too low
239 // In future, may want to minimize search time by sorting and using a binary search.
241 XPMSet::XPMSet() : set(0), len(0), maximum(0), height(-1), width(-1) {
248 void XPMSet::Clear() {
249 for (int i
= 0; i
< len
; i
++) {
260 void XPMSet::Add(int id
, const char *textForm
) {
261 // Invalidate cached dimensions
265 // Replace if this id already present
266 for (int i
= 0; i
< len
; i
++) {
267 if (set
[i
]->GetId() == id
) {
268 set
[i
]->Init(textForm
);
269 set
[i
]->CopyDesiredColours();
274 // Not present, so add to end
275 XPM
*pxpm
= new XPM(textForm
);
278 pxpm
->CopyDesiredColours();
279 if (len
== maximum
) {
281 XPM
**setNew
= new XPM
*[maximum
];
282 for (int i
= 0; i
< len
; i
++) {
293 XPM
*XPMSet::Get(int id
) {
294 for (int i
= 0; i
< len
; i
++) {
295 if (set
[i
]->GetId() == id
) {
302 int XPMSet::GetHeight() {
304 for (int i
= 0; i
< len
; i
++) {
305 if (height
< set
[i
]->GetHeight()) {
306 height
= set
[i
]->GetHeight();
310 return (height
> 0) ? height
: 0;
313 int XPMSet::GetWidth() {
315 for (int i
= 0; i
< len
; i
++) {
316 if (width
< set
[i
]->GetWidth()) {
317 width
= set
[i
]->GetWidth();
321 return (width
> 0) ? width
: 0;