summaryrefslogtreecommitdiff
path: root/BitmapChar.cs
blob: e1cf53241e8e68553af51bb087016e67fe8bdb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// BitmapFontEd
// Copyright (C) 2005  Ian Cowburn
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// $Id$
//
using System;
using System.Drawing;
using System.IO;
using System.Collections;

namespace BitmapFontEd
{
	/// <summary>
	/// Description of BitmapChar.
	/// </summary>
	public class BitmapChar
	{
		public BitmapChar(uint width, uint height)
		{
			m_width=width;
			m_height=height;
			m_data=new Color[m_width,m_height];
			Clear(Color.Black);
			m_changed=false;
		}
		
		public BitmapChar() : this(16,16)
		{
		}
		
		public BitmapChar(BitmapChar old)
		{
			m_width=old.m_width;
			m_height=old.m_height;
			m_data=new Color[m_width,m_height];
			
			for(int x=0;x<m_width;x++)
				for(int y=0;y<m_height;y++)
					m_data[x,y]=old.m_data[x,y];
			
			m_changed=old.m_changed;
		}
		
		public uint Width
		{
			get {return m_width;}
		}
		
		public uint Height
		{
			get {return m_height;}
		}
		
		public bool Changed
		{
			get {return m_changed;}
		}
		
		public void Resize(uint width, uint height)
		{
			if (width==m_width && height==m_height)
			{
				return;
			}
			
			Color[,] data=new Color[width,height];

			for(int y=0;y<height;y++)
				for(int x=0;x<width;x++)
					data[x,y]=Color.Black;

			uint mx=Math.Min(width,m_width);
			uint my=Math.Min(height,m_height);
			
			for(int x=0;x<mx;x++)
				for(int y=0;y<my;y++)
					data[x,y]=m_data[x,y];
			
			m_width=width;
			m_height=height;
			m_data=data;
			m_changed=true;
		}
		
		public Color this [uint x, uint y]
		{
			set
			{
				m_data[x,y]=value;
				m_changed=true;
			}
			get {return m_data[x,y];}
		}
		
		public void Clear(Color c)
		{
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
					m_data[x,y]=c;

			m_changed=true;
		}
		
		public void MirrorHorizontal()
		{
			Color[,] d=new Color[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
					d[x,y]=m_data[m_width-x-1,y];
			
			m_data=d;
			m_changed=true;
		}

		public void MirrorVertical()
		{
			Color[,] d=new Color[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
					d[x,y]=m_data[x,m_height-y-1];
			
			m_data=d;
			m_changed=true;
		}
		
		public void Scroll(int dx, int dy)
		{
			Color[,] d=new Color[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
					d[Mod(x+dx,m_width),Mod(y+dy,m_height)]=m_data[x,y];
			
			m_data=d;
			m_changed=true;
		}
		
		public void RotateRight()
		{
			Color[,] d=new Color[m_width,m_height];
			uint min=Math.Min(m_width,m_height);
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				{
					long nx=min-y-1;
					long ny=x;
					
					if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
					{
						d[nx,ny]=m_data[x,y];
					}
				}
			
			m_data=d;
			m_changed=true;
		}

		public void RotateLeft()
		{
			Color[,] d=new Color[m_width,m_height];
			uint min=Math.Min(m_width,m_height);
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				{
					long nx=y;
					long ny=min-x-1;
					
					if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
					{
						d[nx,ny]=m_data[x,y];
					}
				}
			
			m_data=d;
			m_changed=true;
		}

		public void Output(Stream stream)
		{
			Util.WriteUint(stream,m_width);
			Util.WriteUint(stream,m_height);

			for(uint x=0;x<m_width;x++)
			{
				for(uint y=0;y<m_height;y++)
				{
					if (m_data[x,y]!=Color.Black)
					{
						Util.WriteInt(stream,m_data[x,y].ToArgb());
					}
					else
					{
						Util.WriteInt(stream,0);
					}
				}
			}
		}
		
		public static BitmapChar Input(Stream stream)
		{
			uint width=Util.ReadUint(stream);
			uint height=Util.ReadUint(stream);
			
			BitmapChar s=new BitmapChar(width,height);
			
			for(uint x=0;x<width;x++)
			{
				for(uint y=0;y<height;y++)
				{
					int col=Util.ReadInt(stream);
					
					if (col==0)
					{
						s[x,y]=Color.Black;
					}
					else
					{
						s[x,y]=Color.FromArgb(255,(col&0xff0000)>>16,(col&0xff00)>>8,col&0xff);
						
						if (s[x,y].R==0 && s[x,y].G==0 && s[x,y].B==0)
						{
							s[x,y]=Color.Black;
						}
					}
				}
			}
			
			s.m_changed=false;
			
			return s;
		}
		
		// ------------------------------------------------
		// PRIVATE
		//
		private uint		m_width;
		private uint		m_height;
		private Color[,]	m_data;
		private bool		m_changed;
		
		private int Mod(int v, uint max)
		{
			while(v<0)
				v+=(int)max;
			
			v=v%(int)max;
			
			return v;
		}
	}
	
	/// <summary>
	/// Describes a list of bitmap characters
	/// </summary>
	public class BitmapCharList : IEnumerable
	{
		private const int CHARS=95;
		
		public BitmapCharList()
		{
			m_list=new BitmapChar[CHARS];
			
			for(int f=0;f<CHARS;f++)
			{
				m_list[f]=new BitmapChar();
			}
			
			m_changed=false;
		}
		
		public BitmapChar this [int i]
		{
			get {return m_list[i];}
			set
			{
				m_list[i]=value;
				m_changed=true;
			}
		}
		
		public bool Changed
		{
			get {return m_changed;}
			set {m_changed=value;}
		}
		
		public bool IsFixedWidth
		{
			get
			{
				uint w=m_list[0].Width;
				
				foreach (BitmapChar c in m_list)
				{
					if (c.Width!=w)
					{
						return false;
					}
				}
				
				return true;
			}
		}
		
		public IEnumerator GetEnumerator()
		{
			return m_list.GetEnumerator();
		}
		
		public void Output(Stream stream)
		{
			Util.WriteString(stream,MAGIC);
			
			if (IsFixedWidth)
			{
				Util.WriteInt(stream,1);
			}
			else
			{
				Util.WriteInt(stream,0);
			}
			
			foreach (BitmapChar c in m_list)
			{
				c.Output(stream);
			}
		}
		
		public static BitmapCharList Input(Stream stream)
		{
			BitmapCharList l=new BitmapCharList();
			
			string magic=Util.ReadString(stream,4);
			
			if (magic!=MAGIC)
			{
				throw new Exception("Not a recognised BMF file");
			}
			
			Util.ReadInt(stream);
			
			for(int f=0;f<CHARS;f++)
			{
				l.m_list[f]=BitmapChar.Input(stream);
			}
			
			l.Changed=false;
			
			return l;
		}
		
		// ------------------------------------------------
		// PRIVATE
		//
		private const string	MAGIC="BMF1";
		
		private BitmapChar[]	m_list;
		private bool			m_changed;
	}
}