]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/XPM.cxx
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 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
++) {
119 size_t len
= MeasureLength(linesForm
[j
]);
120 memcpy(nextBit
, linesForm
[j
], len
);
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];
133 if (*colourDef
== '#') {
134 colours
[c
].desired
.Set(colourDef
);
136 colours
[c
].desired
= ColourDesired(0xff, 0xff, 0xff);
137 codeTransparent
= codes
[c
];
139 colourCodeTable
[static_cast<unsigned char>(codes
[c
])] = &(colours
[c
]);
154 void XPM::RefreshColourPalette(Palette
&pal
, bool want
) {
155 if (!data
|| !codes
|| !colours
|| !lines
) {
158 for (int i
=0; i
<nColours
; i
++) {
159 pal
.WantFind(colours
[i
], want
);
163 void XPM::CopyDesiredColours() {
164 if (!data
|| !codes
|| !colours
|| !lines
) {
167 for (int i
=0; i
<nColours
; i
++) {
172 void XPM::Draw(Surface
*surface
, PRectangle
&rc
) {
173 if (!data
|| !codes
|| !colours
|| !lines
) {
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
++) {
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
);
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;
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;
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;
226 if (textForm
[j
] == '\0' || countQuotes
/ 2 > strings
) {
227 // Malformed XPM! Height + number of colors too high or too low
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) {
243 void XPMSet::Clear() {
244 for (int i
= 0; i
< len
; i
++) {
255 void XPMSet::Add(int id
, const char *textForm
) {
256 // Invalidate cached dimensions
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
);
268 // Not present, so add to end
269 XPM
*pxpm
= new XPM(textForm
);
272 pxpm
->CopyDesiredColours();
273 if (len
== maximum
) {
275 XPM
**setNew
= new XPM
*[maximum
];
276 for (int i
= 0; i
< len
; i
++) {
287 XPM
*XPMSet::Get(int id
) {
288 for (int i
= 0; i
< len
; i
++) {
289 if (set
[i
]->GetId() == id
) {
296 int XPMSet::GetHeight() {
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() {
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;