aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/tools/IrrFontTool/newFontTool/main.cpp
blob: 9e9e87f1307426c803f7ab063569014cee21418f (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
	Tool for creating Irrlicht bitmap+vector fonts,
	started by Gaz Davidson in December 2006

	Due to my laziness and Microsoft's unituitive API, surragate pairs and
	nonspacing diacritical marks are not supported!

	Linux bitmap font support added by Neil Burlock Oct 2008
	Note: Xft/Freetype2 is used to render the fonts under X11.  Anti-aliasing
	is controlled by the system and cannot be overriden by an application,
	so fonts that are rendered will be aliased or anti-aliased depending
	on the system that they are created on.

*/


#include <irrlicht.h>
#include <iostream>

#include "CFontTool.h"
#include "CVectorFontTool.h"
#include "ITexture.h"

using namespace irr;
using namespace gui;

#pragma comment(lib, "Irrlicht.lib")

const s32 texturesizes[] = {128, 256, 512, 1024, 2048, 4096, 0};

const wchar_t *fileformats[]		 =  { L"bmp", L"ppm", 0 };  // bitmap font formats
const wchar_t *alphafileformats[]  =  { L"png", L"tga", 0 };  // bitmap font formats which support alpha channels
const wchar_t *vectorfileformats[] =  { L"xml", L"bin", 0 };  // file formats for vector fonts

const wchar_t *warntext = L"Legal Notice\n"
					L"------------\n\n"
					L"When making bitmap and vector fonts, you should consider the potential legal "
					L"issues with redistributing the fonts with your software; this tool basically "
					L"copies font data and some authors might not like this!\n"
					L"If you purchased fonts or they came with an application or your OS, you'll have"
					L"to check the license to see what restrictions are placed on making derivative works.\n\n"
					L"PD and the OFL\n"
					L"--------------\n\n"
					L"You'll find lots of fonts on the web listed as Public Domain, you can do what you like "
					L"with these.\n"
					L"Many fonts are released under the Open Font License, which is a 'viral' open source "
					L"license like the GPL. It's worth reading the license here: http://scripts.sil.org/OFL\n"
					L"The most important restrictions are- you must include the original font's license, you "
					L"can't stop your users from using or distributing the font, the font must have a "
					L"different name the original.\n\n"
					L"Some free fonts can be found here- www.openfontlibrary.org\n"
					L"http://savannah.nongnu.org/projects/freefont/";

const wchar_t *helptext = L"This tool creates bitmap fonts for the Irrlicht Engine\n\n"

					L"First select a character encoding from the list, then choose the font, "
					L"size, and whether you'd like bold, italic, antialiasing and an alpha channel. "
					L"In Windows, antialiasing will be ignored for small fonts\n\n"

					L"Then select a texture width and height. If the output exceeds this then more than "
					L"one image will be created. You can use the scrollbar at the top of the screen to "
					L"preview them. Most modern graphics cards will support up to 2048x2048, "
					L"keep in mind that more images means worse performance when drawing text!\n\n"

					L"If you want a vector font rather than a bitmap font, check the vector box. "
					L"Vector fonts are stored in system memory while bitmap fonts are in video ram\n\n"

					L"Click create to preview your font, this may take lots of time and memory "
					L"when making a font with a lot of characters, please be patient!\n\n"

					L"Now you're ready to give your font a name, select a format and click save.\n\n"
					L"To load your font in Irrlicht, simply use env->getFont(\"Myfont.xml\");\n\n"

					L"That's all, have fun :-)";

#ifdef _IRR_WINDOWS_
					const wchar_t *completeText = L"Font created";
#else
					const wchar_t *completeText = L"Font created\n\n"
							L"Please note that anti-aliasing under X11 is controlled by the system "
							L"configuration, so if your system is set to use anti-aliasing, then so "
							L"will any fonts you create with FontTool";
#endif

enum MYGUI
{
	MYGUI_CHARSET  = 100,
	MYGUI_FONTNAME,
	MYGUI_SIZE,
	MYGUI_TEXWIDTH,
	MYGUI_TEXHEIGHT,
	MYGUI_BOLD,
	MYGUI_ITALIC,
	MYGUI_ANTIALIAS,
	MYGUI_ALPHA,
	MYGUI_VECTOR,
	MYGUI_FILENAME,
	MYGUI_FORMAT,
	MYGUI_CREATE,
	MYGUI_SAVE,
	MYGUI_IMAGE,
	MYGUI_CURRENTIMAGE,
	MYGUI_HELPBUTTON
};


// event reciever
class MyEventReceiver : public IEventReceiver
{
public:

	MyEventReceiver(IrrlichtDevice* device, CFontTool*& fonttool, CVectorFontTool* &vectool) :
		Device(device), FontTool(fonttool), VecTool(vectool)
	{
		device->setEventReceiver(this);
	}

	virtual bool OnEvent(const SEvent &event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = Device->getGUIEnvironment();

			switch(event.GUIEvent.EventType)
			{
			case EGET_SCROLL_BAR_CHANGED:
				if (id == MYGUI_CURRENTIMAGE)
				{
					IGUIImage* img = (IGUIImage*)env->getRootGUIElement()->getElementFromId(MYGUI_IMAGE,true);
					s32 i = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
					img->setImage(FontTool->currentTextures[i]);

					return true;
				}
				break;
			case EGET_COMBO_BOX_CHANGED:
				if (id == MYGUI_CHARSET)
				{
					IGUIComboBox* cbo = (IGUIComboBox*)event.GUIEvent.Caller;
					FontTool->selectCharSet(cbo->getSelected());
					// rebuild font list
					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FONTNAME,true);
					cbo->clear();
					for (u32 i=0; i < FontTool->FontNames.size(); ++i)
						cbo->addItem(FontTool->FontNames[i].c_str());
					return true;
				}
				break;
			case EGET_CHECKBOX_CHANGED:
				if (id == MYGUI_VECTOR)
				{
					IGUICheckBox* chk = (IGUICheckBox*)event.GUIEvent.Caller;

					IGUIComboBox *cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					cbo->clear();

					if (chk->isChecked() && VecTool)
					{
						// vector formats
						for (s32 i=0; fileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(vectorfileformats[i]).c_str());

					}
					else
					{

						// bitmap formats
						if (!FontTool->UseAlphaChannel)
						{
							// add non-alpha formats
							for (s32 i=0; fileformats[i] != 0; ++i)
								cbo->addItem( core::stringw(fileformats[i]).c_str());
						}
						// add formats which support alpha
						for (s32 i=0; alphafileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(alphafileformats[i]).c_str());
					}

				}
				break;

			case EGET_BUTTON_CLICKED:

				if (id == MYGUI_CREATE)
				{
					// create the font with the params
					IGUIComboBox* cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_CHARSET, true);
					int charset = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FONTNAME,true);
					int fontname = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_SIZE,true);
					int fontsize = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_TEXWIDTH,true);
					int texwidth = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_TEXHEIGHT,true);
					int texheight = cbo->getSelected();

					IGUICheckBox* chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_BOLD,true);
					bool bold = chk->isChecked();
					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ITALIC,true);
					bool italic = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ALPHA,true);
					bool alpha = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ANTIALIAS,true);
					bool aa = chk->isChecked();

					// vector fonts disabled
					//chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_VECTOR,true);
					bool vec = false;//chk->isChecked();

					FontTool->makeBitmapFont(fontname, charset, FontTool->FontSizes[fontsize], texturesizes[texwidth], texturesizes[texheight], bold, italic, aa, alpha);

					IGUIScrollBar* scrl = (IGUIScrollBar*)env->getRootGUIElement()->getElementFromId(MYGUI_CURRENTIMAGE,true);
					scrl->setMax(FontTool->currentTextures.size() == 0 ? 0 : FontTool->currentTextures.size()-1);

					if (FontTool->currentTextures.size() > 0)
					{
						IGUIImage* img = (IGUIImage*)env->getRootGUIElement()->getElementFromId(MYGUI_IMAGE,true);
						img->setImage(FontTool->currentTextures[0]);
						scrl->setPos(0);
					}

					// make sure users pick a file format that supports alpha channel
					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					cbo->clear();

					if (vec)
					{
						// add vector formats
						for (s32 i=0; fileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(vectorfileformats[i]).c_str());
					}
					else
					{
						if (!alpha)
						{
							// add non-alpha formats
							for (s32 i=0; fileformats[i] != 0; ++i)
								cbo->addItem( core::stringw(fileformats[i]).c_str());
						}
						// add formats which support alpha
						for (s32 i=0; alphafileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(alphafileformats[i]).c_str());
					}
					if (VecTool)
					{
						delete VecTool;
						VecTool = 0;
					}
					if (vec)
					{
						VecTool = new CVectorFontTool(FontTool);
					}

					/* Message box letting the user know the process is complete */
					env->addMessageBox(L"Create", completeText);

					return true;
				}

				if (id == MYGUI_SAVE)
				{
					IGUIEditBox *edt  = (IGUIEditBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FILENAME,true);
					core::stringc name = edt->getText();

					IGUIComboBox *fmt  = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					core::stringc format = fmt->getItem(fmt->getSelected());

					// vector fonts disabled
					IGUICheckBox *chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_VECTOR,true);
					bool vec = false; // chk->isChecked();

					if (vec && VecTool)
						VecTool->saveVectorFont(name.c_str(), format.c_str());
					else
						FontTool->saveBitmapFont(name.c_str(), format.c_str());

					return true;
				}

				if (id == MYGUI_HELPBUTTON)
				{
					env->addMessageBox(L"Irrlicht Unicode Font Tool", helptext);
					return true;
				}

				break;
			}
		}

		return false;
	}

	IrrlichtDevice*	 Device;
	CFontTool*       FontTool;
	CVectorFontTool* VecTool;

};

void createGUI(IrrlichtDevice* device, CFontTool* fc)
{
	gui::IGUIEnvironment *env = device->getGUIEnvironment();

	// change transparency of skin
	for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
	{
		video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
		col.setAlpha(255);
		env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
	}

	IGUIWindow *win = env->addWindow( core::rect<s32>(10,10,200,500), false, L"Font Creator");
	win->getCloseButton()->setVisible(false);

	s32 xs=10,xp=xs, yp=30, h=20;

	env->addStaticText(L"Charset", core::rect<s32>(xp,yp,50,yp+h),false,false, win);

	xp+=60;

	// charset combo
	gui::IGUIComboBox* cbo = env->addComboBox( core::rect<s32>(xp,yp,180,yp+h),win, MYGUI_CHARSET);
	for (u32 i=0; i < fc->CharSets.size(); ++i)
		cbo->addItem(fc->CharSets[i].c_str());

	yp += (s32)(h*1.5f);
	xp = xs;

	env->addStaticText(L"Font", core::rect<s32>(xp,yp,50,yp+h),false,false, win);

	xp+=60;

	// font name combo
	cbo = env->addComboBox( core::rect<s32>(xp,yp,180,yp+h),win, MYGUI_FONTNAME);
	for (u32 i=0; i < fc->FontNames.size(); ++i)
		cbo->addItem(fc->FontNames[i].c_str());

	yp += (s32)(h*1.5f);
	xp = xs;

	env->addStaticText(L"Size", core::rect<s32>(xp,yp,50,yp+h),false,false, win);

	xp += 60;

	// font size combo
	cbo = env->addComboBox( core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_SIZE);
	for (s32 i=0; fc->FontSizes[i] != 0; ++i)
		cbo->addItem( ((core::stringw(fc->FontSizes[i])) + L"px").c_str());

	xp = xs;
	yp += (s32)(h*1.5f);

	// bold checkbox
	env->addCheckBox(false, core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_BOLD, L"Bold");

	xp += 45;

	// italic checkbox
	env->addCheckBox(false, core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_ITALIC, L"Italic");

	xp += 45;

	// AA checkbox
	env->addCheckBox(false, core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_ANTIALIAS, L"AA");

	xp +=40;

	// Alpha checkbox
	env->addCheckBox(false, core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_ALPHA, L"Alpha");

	xp = xs;
	yp += (s32)(h*1.5f);

	/*
	// vector fonts can't be loaded yet
	env->addCheckBox(false, core::rect<s32>(xp,yp,xp+200,yp+h),win, MYGUI_VECTOR, L"Vector Font");
	*/

	yp += (s32)(h*1.5f);

	env->addStaticText(L"Max Width:", core::rect<s32>(xp,yp,50,yp+h),false,false, win);

	xp += 60;

	// texture widths
	cbo = env->addComboBox( core::rect<s32>(xp,yp,xp+70,yp+h),win, MYGUI_TEXWIDTH);
	for (s32 i=0; texturesizes[i] != 0; ++i)
		cbo->addItem( ((core::stringw(texturesizes[i])) + L" wide").c_str());

	xp=xs;
	yp += (s32)(h*1.5f);

	env->addStaticText(L"Max Height:", core::rect<s32>(xp,yp,60,yp+h),false,false, win);

	xp += 60;

	// texture height
	cbo = env->addComboBox( core::rect<s32>(xp,yp,xp+70,yp+h),win, MYGUI_TEXHEIGHT);
	for (s32 i=0; texturesizes[i] != 0; ++i)
		cbo->addItem( ((core::stringw(texturesizes[i])) + L" tall").c_str());

	// file name
	xp = xs;
	yp += (s32)(h*1.5f);
	env->addStaticText(L"Filename", core::rect<s32>(xp,yp,60,yp+h),false,false, win);
	xp += 60;
	env->addEditBox(L"myfont",core::rect<s32>(xp,yp,xp+70,yp+h), true, win, MYGUI_FILENAME);

	// file format
	xp = xs;
	yp += (s32)(h*1.5f);
	env->addStaticText(L"File Format", core::rect<s32>(xp,yp,60,yp+h),false,false, win);
	xp += 60;

	cbo = env->addComboBox( core::rect<s32>(xp,yp,xp+70,yp+h),win, MYGUI_FORMAT);
	for (s32 i=0; fileformats[i] != 0; ++i)
		cbo->addItem( core::stringw(fileformats[i]).c_str());
	for (s32 i=0; alphafileformats[i] != 0; ++i)
		cbo->addItem( core::stringw(alphafileformats[i]).c_str());

	xp = xs;
	yp += h*2;

	// create button
	env->addButton( core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_CREATE, L"Create");

	xp += 60;

	// save button
	env->addButton( core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_SAVE, L"Save");

	xp += 60;

	// help button
	env->addButton( core::rect<s32>(xp,yp,xp+50,yp+h),win, MYGUI_HELPBUTTON, L"Help");

	// font image
	gui::IGUIImage *img = env->addImage(0, core::position2d<s32>(0,0), true,0, MYGUI_IMAGE);
	img->setRelativePosition(core::rect<s32>(0,20,800,600));

	// font scrollbar
	IGUIScrollBar *scrl= env->addScrollBar(true,core::rect<s32>(0,0,800,20),0, MYGUI_CURRENTIMAGE);
	scrl->setMax(0);
	scrl->setSmallStep(1);

	yp += h*3;

	env->getRootGUIElement()->bringToFront(win);
	win->setRelativePosition( core::rect<s32>(10,10,200,yp));
}

int main()
{
	IrrlichtDevice* device =createDevice(video::EDT_OPENGL, core::dimension2du(800, 600));
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	gui::IGUIEnvironment *env = device->getGUIEnvironment();

	// create font tool
	CFontTool *fc = new CFontTool(device);
	CVectorFontTool *vc = 0;

	IEventReceiver *events = new MyEventReceiver(device,fc,vc);

	createGUI(device, fc);

	while(device->run())
	{
		if (device->isWindowActive())
		{

			driver->beginScene(true, true, video::SColor(0,200,200,200));
			smgr->drawAll();
			env->drawAll();
			driver->endScene();
		}
	}

	// drop the font tool and resources
	fc->drop();

	device->drop();

	return 0;
}