Files @ r20718:cc912091a170
Branch filter:

Location: cpp/openttd-patchpack/source/src/fontcache.cpp - annotation

translators
(svn r25756) -Update from WebTranslator v3.0:
indonesian - 2 changes by UseYourIllusion
  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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
r5584:545d748cc681
r5584:545d748cc681
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6179:c0508e7aefec
r5584:545d748cc681
r5584:545d748cc681
r20421:689b79ec796e
r6937:bea726150bf3
r8131:7a50db7be0ff
r20454:35a010f0c655
r18413:e892ae4942df
r18462:a1ba75a7f741
r20543:89cc7ba70ae8
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r20429:8ee12cd4d75d
r8264:d493cb51fe8a
r12188:1956794c2b1b
r20179:3d62e5438cc2
r12188:1956794c2b1b
r20179:3d62e5438cc2
r20448:799c881fb514
r20448:799c881fb514
r11485:42dece12eaa2
r18422:ab780a7de508
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20448:799c881fb514
r20448:799c881fb514
r20448:799c881fb514
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20543:89cc7ba70ae8
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20543:89cc7ba70ae8
r20423:f73b67868582
r20423:f73b67868582
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20423:f73b67868582
r20423:f73b67868582
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20423:f73b67868582
r20424:dc165f5888d8
r20424:dc165f5888d8
r20447:a030e602a1ba
r20447:a030e602a1ba
r20423:f73b67868582
r20454:35a010f0c655
r20447:a030e602a1ba
r20447:a030e602a1ba
r20423:f73b67868582
r20490:fd8166bd400c
r20478:8c1e52fed405
r20423:f73b67868582
r20423:f73b67868582
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20447:a030e602a1ba
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20447:a030e602a1ba
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20429:8ee12cd4d75d
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20424:dc165f5888d8
r20447:a030e602a1ba
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20447:a030e602a1ba
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20179:3d62e5438cc2
r5584:545d748cc681
r20423:f73b67868582
r20423:f73b67868582
r20426:c68b26c88f35
r20426:c68b26c88f35
r20428:f2a9c31f2342
r20482:888d761be878
r20478:8c1e52fed405
r20454:35a010f0c655
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20447:a030e602a1ba
r20447:a030e602a1ba
r20428:f2a9c31f2342
r20423:f73b67868582
r20426:c68b26c88f35
r20426:c68b26c88f35
r20447:a030e602a1ba
r20447:a030e602a1ba
r20423:f73b67868582
r20423:f73b67868582
r20447:a030e602a1ba
r20447:a030e602a1ba
r20423:f73b67868582
r20447:a030e602a1ba
r20478:8c1e52fed405
r20423:f73b67868582
r20423:f73b67868582
r20421:689b79ec796e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15175:66e0817dc450
r15175:66e0817dc450
r5584:545d748cc681
r20426:c68b26c88f35
r20426:c68b26c88f35
r20426:c68b26c88f35
r20426:c68b26c88f35
r20426:c68b26c88f35
r20426:c68b26c88f35
r20428:f2a9c31f2342
r13564:81d26c235297
r20431:b0c3436dae0d
r20431:b0c3436dae0d
r20179:3d62e5438cc2
r20179:3d62e5438cc2
r20426:c68b26c88f35
r20179:3d62e5438cc2
r20426:c68b26c88f35
r20179:3d62e5438cc2
r20179:3d62e5438cc2
r20179:3d62e5438cc2
r20426:c68b26c88f35
r20426:c68b26c88f35
r20179:3d62e5438cc2
r20179:3d62e5438cc2
r20179:3d62e5438cc2
r20426:c68b26c88f35
r18199:2f6c1f5f3085
r13564:81d26c235297
r18199:2f6c1f5f3085
r20426:c68b26c88f35
r20426:c68b26c88f35
r18199:2f6c1f5f3085
r18199:2f6c1f5f3085
r18199:2f6c1f5f3085
r18199:2f6c1f5f3085
r18199:2f6c1f5f3085
r20426:c68b26c88f35
r13564:81d26c235297
r18199:2f6c1f5f3085
r20448:799c881fb514
r20448:799c881fb514
r20448:799c881fb514
r20448:799c881fb514
r13564:81d26c235297
r13564:81d26c235297
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r5584:545d748cc681
r20430:6112801da287
r5584:545d748cc681
r20436:15b556d8019b
r20430:6112801da287
r20430:6112801da287
r20436:15b556d8019b
r20436:15b556d8019b
r20436:15b556d8019b
r20436:15b556d8019b
r20430:6112801da287
r5584:545d748cc681
r20436:15b556d8019b
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r5584:545d748cc681
r20430:6112801da287
r20430:6112801da287
r5584:545d748cc681
r20430:6112801da287
r20436:15b556d8019b
r20430:6112801da287
r20436:15b556d8019b
r5584:545d748cc681
r5584:545d748cc681
r20436:15b556d8019b
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r20430:6112801da287
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r20430:6112801da287
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r20430:6112801da287
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20430:6112801da287
r5584:545d748cc681
r20430:6112801da287
r20436:15b556d8019b
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20436:15b556d8019b
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18419:79e272adb960
r20428:f2a9c31f2342
r18419:79e272adb960
r20426:c68b26c88f35
r18419:79e272adb960
r20431:b0c3436dae0d
r20428:f2a9c31f2342
r20454:35a010f0c655
r20478:8c1e52fed405
r20478:8c1e52fed405
r20454:35a010f0c655
r18419:79e272adb960
r18419:79e272adb960
r18419:79e272adb960
r19116:609e37882a23
r19116:609e37882a23
r20423:f73b67868582
r19116:609e37882a23
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r19116:609e37882a23
r5584:545d748cc681
r20447:a030e602a1ba
r10367:2521c24ffcf6
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20447:a030e602a1ba
r5584:545d748cc681
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r5584:545d748cc681
r5584:545d748cc681
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r5584:545d748cc681
r5584:545d748cc681
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r20428:f2a9c31f2342
r5584:545d748cc681
r5584:545d748cc681
r13499:0af2aac7f5aa
r6856:7afc1ae9c263
r8037:0f84abbbbbf6
r6856:7afc1ae9c263
r6856:7afc1ae9c263
r5584:545d748cc681
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r20422:8dfeb5e16301
r20422:8dfeb5e16301
r20422:8dfeb5e16301
r20422:8dfeb5e16301
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r6913:a28cda7a5f13
r20447:a030e602a1ba
r5584:545d748cc681
r20447:a030e602a1ba
r5584:545d748cc681
r5584:545d748cc681
r20431:b0c3436dae0d
r5584:545d748cc681
r5584:545d748cc681
r20431:b0c3436dae0d
r5584:545d748cc681
r20423:f73b67868582
r6913:a28cda7a5f13
r20431:b0c3436dae0d
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r17042:c3e42890454e
r20423:f73b67868582
r17042:c3e42890454e
r17042:c3e42890454e
r17042:c3e42890454e
r20423:f73b67868582
r20428:f2a9c31f2342
r17042:c3e42890454e
r17042:c3e42890454e
r17042:c3e42890454e
r20447:a030e602a1ba
r20447:a030e602a1ba
r20428:f2a9c31f2342
r17042:c3e42890454e
r17042:c3e42890454e
r16560:73269894b72b
r20447:a030e602a1ba
r20426:c68b26c88f35
r5584:545d748cc681
r9716:c3d234f3d583
r12179:250cb251ae70
r9716:c3d234f3d583
r5584:545d748cc681
r20431:b0c3436dae0d
r20431:b0c3436dae0d
r5584:545d748cc681
r18049:d6aadab953b4
r18049:d6aadab953b4
r18049:d6aadab953b4
r5584:545d748cc681
r20431:b0c3436dae0d
r19022:c323225b1609
r18461:b013cc5efff8
r6856:7afc1ae9c263
r6856:7afc1ae9c263
r6856:7afc1ae9c263
r20427:30afb67f2d28
r5584:545d748cc681
r5584:545d748cc681
r20423:f73b67868582
r20431:b0c3436dae0d
r20431:b0c3436dae0d
r7928:a80e7e05d6c5
r6856:7afc1ae9c263
r6913:a28cda7a5f13
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20431:b0c3436dae0d
r20431:b0c3436dae0d
r7928:a80e7e05d6c5
r6856:7afc1ae9c263
r6913:a28cda7a5f13
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6856:7afc1ae9c263
r14154:7e8e7c9f319a
r5584:545d748cc681
r20428:f2a9c31f2342
r5584:545d748cc681
r6856:7afc1ae9c263
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20423:f73b67868582
r18170:a06c23d1985d
r20447:a030e602a1ba
r18170:a06c23d1985d
r18170:a06c23d1985d
r18170:a06c23d1985d
r20447:a030e602a1ba
r5584:545d748cc681
r20447:a030e602a1ba
r5584:545d748cc681
r20431:b0c3436dae0d
r5584:545d748cc681
r20423:f73b67868582
r20428:f2a9c31f2342
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20447:a030e602a1ba
r20478:8c1e52fed405
r20454:35a010f0c655
r20478:8c1e52fed405
r20478:8c1e52fed405
r20478:8c1e52fed405
r20478:8c1e52fed405
r20478:8c1e52fed405
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r20478:8c1e52fed405
r20454:35a010f0c655
r20478:8c1e52fed405
r20454:35a010f0c655
r20454:35a010f0c655
r20454:35a010f0c655
r5584:545d748cc681
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file fontcache.cpp Cache for characters from fonts. */

#include "stdafx.h"
#include "fontcache.h"
#include "fontdetection.h"
#include "blitter/factory.hpp"
#include "core/math_func.hpp"
#include "core/smallmap_type.hpp"
#include "strings_func.h"
#include "zoom_type.h"
#include "gfx_layout.h"

#include "table/sprites.h"
#include "table/control_codes.h"
#include "table/unicode.h"

static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter.
static const int MAX_FONT_SIZE     = 72; ///< Maximum font size.

/** Default heights for the different sizes of fonts. */
static const int _default_font_height[FS_END]   = {10, 6, 18, 10};
static const int _default_font_ascender[FS_END] = { 8, 5, 15,  8};

/**
 * Create a new font cache.
 * @param fs The size of the font.
 */
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
		ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
		units_per_em(1)
{
	assert(parent == NULL || this->fs == parent->fs);
	FontCache::caches[this->fs] = this;
	Layouter::ResetFontCache(this->fs);
}

/** Clean everything up. */
FontCache::~FontCache()
{
	assert(this->fs == parent->fs);
	FontCache::caches[this->fs] = this->parent;
	Layouter::ResetFontCache(this->fs);
}


/**
 * Get height of a character for a given font size.
 * @param size Font size to get height of
 * @return     Height of characters in the given font (pixels)
 */
int GetCharacterHeight(FontSize size)
{
	return FontCache::Get(size)->GetHeight();
}


/** Font cache for fonts that are based on a freetype font. */
class SpriteFontCache : public FontCache {
private:
	SpriteID **glyph_to_spriteid_map; ///< Mapping of glyphs to sprite IDs.

	void ClearGlyphToSpriteMap();
public:
	SpriteFontCache(FontSize fs);
	~SpriteFontCache();
	virtual SpriteID GetUnicodeGlyph(WChar key);
	virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
	virtual void InitializeUnicodeGlyphMap();
	virtual void ClearFontCache() {}
	virtual const Sprite *GetGlyph(GlyphID key);
	virtual uint GetGlyphWidth(GlyphID key);
	virtual bool GetDrawGlyphShadow();
	virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
	virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return NULL; }
};

/**
 * Create a new sprite font cache.
 * @param fs The font size to create the cache for.
 */
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(NULL)
{
	this->InitializeUnicodeGlyphMap();
}

/**
 * Free everything we allocated.
 */
SpriteFontCache::~SpriteFontCache()
{
	this->ClearGlyphToSpriteMap();
}

SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key)
{
	if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) return 0;
	return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
}

void SpriteFontCache::SetUnicodeGlyph(GlyphID key, SpriteID sprite)
{
	if (this->glyph_to_spriteid_map == NULL) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
	if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
	this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
}

void SpriteFontCache::InitializeUnicodeGlyphMap()
{
	/* Clear out existing glyph map if it exists */
	this->ClearGlyphToSpriteMap();

	SpriteID base;
	switch (this->fs) {
		default: NOT_REACHED();
		case FS_MONO:   // Use normal as default for mono spaced font, i.e. FALL THROUGH
		case FS_NORMAL: base = SPR_ASCII_SPACE;       break;
		case FS_SMALL:  base = SPR_ASCII_SPACE_SMALL; break;
		case FS_LARGE:  base = SPR_ASCII_SPACE_BIG;   break;
	}

	for (uint i = ASCII_LETTERSTART; i < 256; i++) {
		SpriteID sprite = base + i - ASCII_LETTERSTART;
		if (!SpriteExists(sprite)) continue;
		this->SetUnicodeGlyph(i, sprite);
		this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
	}

	for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
		byte key = _default_unicode_map[i].key;
		if (key == CLRA) {
			/* Clear the glyph. This happens if the glyph at this code point
				* is non-standard and should be accessed by an SCC_xxx enum
				* entry only. */
			this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
		} else {
			SpriteID sprite = base + key - ASCII_LETTERSTART;
			this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
		}
	}
}

/**
 * Clear the glyph to sprite mapping.
 */
void SpriteFontCache::ClearGlyphToSpriteMap()
{
	if (this->glyph_to_spriteid_map == NULL) return;

	for (uint i = 0; i < 256; i++) {
		free(this->glyph_to_spriteid_map[i]);
	}
	free(this->glyph_to_spriteid_map);
	this->glyph_to_spriteid_map = NULL;
}

const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
{
	SpriteID sprite = this->GetUnicodeGlyph(key);
	if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
	return GetSprite(sprite, ST_FONT);
}

uint SpriteFontCache::GetGlyphWidth(GlyphID key)
{
	SpriteID sprite = this->GetUnicodeGlyph(key);
	if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
	return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (this->fs != FS_NORMAL) : 0;
}

bool SpriteFontCache::GetDrawGlyphShadow()
{
	return false;
}

/*static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };

#ifdef WITH_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_TRUETYPE_TABLES_H

/** Font cache for fonts that are based on a freetype font. */
class FreeTypeFontCache : public FontCache {
private:
	FT_Face face;  ///< The font face associated with this font.

	typedef SmallMap<uint32, SmallPair<size_t, const void*> > FontTable; ///< Table with font table cache
	FontTable font_tables; ///< Cached font tables.

	/** Container for information about a glyph. */
	struct GlyphEntry {
		Sprite *sprite; ///< The loaded sprite.
		byte width;     ///< The width of the glyph.
		bool duplicate; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
	};

	/**
	 * The glyph cache. This is structured to reduce memory consumption.
	 * 1) There is a 'segment' table for each font size.
	 * 2) Each segment table is a discrete block of characters.
	 * 3) Each block contains 256 (aligned) characters sequential characters.
	 *
	 * The cache is accessed in the following way:
	 * For character 0x0041  ('A'): glyph_to_sprite[0x00][0x41]
	 * For character 0x20AC (Euro): glyph_to_sprite[0x20][0xAC]
	 *
	 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
	 * This can be simply changed in the two functions Get & SetGlyphPtr.
	 */
	GlyphEntry **glyph_to_sprite;

	GlyphEntry *GetGlyphPtr(GlyphID key);
	void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);

public:
	FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
	~FreeTypeFontCache();
	virtual SpriteID GetUnicodeGlyph(WChar key) { return this->parent->GetUnicodeGlyph(key); }
	virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
	virtual void InitializeUnicodeGlyphMap() { this->parent->InitializeUnicodeGlyphMap(); }
	virtual void ClearFontCache();
	virtual const Sprite *GetGlyph(GlyphID key);
	virtual uint GetGlyphWidth(GlyphID key);
	virtual bool GetDrawGlyphShadow();
	virtual GlyphID MapCharToGlyph(WChar key);
	virtual const void *GetFontTable(uint32 tag, size_t &length);
};

FT_Library _library = NULL;

FreeTypeSettings _freetype;

static const byte FACE_COLOUR   = 1;
static const byte SHADOW_COLOUR = 2;

/**
 * Create a new FreeTypeFontCache.
 * @param fs     The font size that is going to be cached.
 * @param face   The font that has to be loaded.
 * @param pixels The number of pixels this font should be high.
 */
FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : FontCache(fs), face(face), glyph_to_sprite(NULL)
{
	assert(face != NULL);

	if (pixels == 0) {
		/* Try to determine a good height based on the minimal height recommended by the font. */
		pixels = _default_font_height[this->fs];

		TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
		if (head != NULL) {
			/* Font height is minimum height plus the difference between the default
			 * height for this font size and the small size. */
			int diff = _default_font_height[this->fs] - _default_font_height[FS_SMALL];
			pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[this->fs], MAX_FONT_SIZE);
		}
	}

	FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
	if (err == FT_Err_Invalid_Pixel_Size) {

		/* Find nearest size to that requested */
		FT_Bitmap_Size *bs = this->face->available_sizes;
		int i = this->face->num_fixed_sizes;
		int n = bs->height;
		for (; --i; bs++) {
			if (abs(pixels - bs->height) < abs(pixels - n)) n = bs->height;
		}

		FT_Set_Pixel_Sizes(this->face, 0, n);
	}

	this->units_per_em = this->face->units_per_EM;
	this->ascender     = this->face->size->metrics.ascender >> 6;
	this->descender    = this->face->size->metrics.descender >> 6;
	this->height       = this->ascender - this->descender;
}

/**
 * Loads the freetype font.
 * First type to load the fontname as if it were a path. If that fails,
 * try to resolve the filename of the font using fontconfig, where the
 * format is 'font family name' or 'font family name, font style'.
 * @param fs The font size to load.
 */
static void LoadFreeTypeFont(FontSize fs)
{
	FreeTypeSubSetting *settings = NULL;
	switch (fs) {
		default: NOT_REACHED();
		case FS_SMALL:  settings = &_freetype.small;  break;
		case FS_NORMAL: settings = &_freetype.medium; break;
		case FS_LARGE:  settings = &_freetype.large;  break;
		case FS_MONO:   settings = &_freetype.mono;   break;
	}

	if (StrEmpty(settings->font)) return;

	if (_library == NULL) {
		if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
			ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
			return;
		}

		DEBUG(freetype, 2, "Initialized");
	}

	FT_Face face = NULL;
	FT_Error error = FT_New_Face(_library, settings->font, 0, &face);

	if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);

	if (error == FT_Err_Ok) {
		DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);

		/* Attempt to select the unicode character map */
		error = FT_Select_Charmap(face, ft_encoding_unicode);
		if (error == FT_Err_Ok) goto found_face; // Success

		if (error == FT_Err_Invalid_CharMap_Handle) {
			/* Try to pick a different character map instead. We default to
			 * the first map, but platform_id 0 encoding_id 0 should also
			 * be unicode (strange system...) */
			FT_CharMap found = face->charmaps[0];
			int i;

			for (i = 0; i < face->num_charmaps; i++) {
				FT_CharMap charmap = face->charmaps[i];
				if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
					found = charmap;
				}
			}

			if (found != NULL) {
				error = FT_Set_Charmap(face, found);
				if (error == FT_Err_Ok) goto found_face;
			}
		}
	}

	FT_Done_Face(face);

	static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
	ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error);
	return;

found_face:
	new FreeTypeFontCache(fs, face, settings->size);
}


/**
 * Free everything that was allocated for this font cache.
 */
FreeTypeFontCache::~FreeTypeFontCache()
{
	FT_Done_Face(this->face);
	this->ClearFontCache();

	for (FontTable::iterator iter = this->font_tables.Begin(); iter != this->font_tables.End(); iter++) {
		free(iter->second.second);
	}
}

/**
 * Reset cached glyphs.
 */
void FreeTypeFontCache::ClearFontCache()
{
	if (this->glyph_to_sprite == NULL) return;

	for (int i = 0; i < 256; i++) {
		if (this->glyph_to_sprite[i] == NULL) continue;

		for (int j = 0; j < 256; j++) {
			if (this->glyph_to_sprite[i][j].duplicate) continue;
			free(this->glyph_to_sprite[i][j].sprite);
		}

		free(this->glyph_to_sprite[i]);
	}

	free(this->glyph_to_sprite);
	this->glyph_to_sprite = NULL;
}

FreeTypeFontCache::GlyphEntry *FreeTypeFontCache::GetGlyphPtr(GlyphID key)
{
	if (this->glyph_to_sprite == NULL) return NULL;
	if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) return NULL;
	return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
}


void FreeTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
{
	if (this->glyph_to_sprite == NULL) {
		DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
		this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
	}

	if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) {
		DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
		this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
	}

	DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite    = glyph->sprite;
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width     = glyph->width;
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
}

static void *AllocateFont(size_t size)
{
	return MallocT<byte>(size);
}


/* Check if a glyph should be rendered with antialiasing */
static bool GetFontAAState(FontSize size)
{
	/* AA is only supported for 32 bpp */
	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;

	switch (size) {
		default: NOT_REACHED();
		case FS_NORMAL: return _freetype.medium.aa;
		case FS_SMALL:  return _freetype.small.aa;
		case FS_LARGE:  return _freetype.large.aa;
		case FS_MONO:   return _freetype.mono.aa;
	}
}


const Sprite *FreeTypeFontCache::GetGlyph(GlyphID key)
{
	if ((key & SPRITE_GLYPH) != 0) return parent->GetGlyph(key);

	/* Check for the glyph in our cache */
	GlyphEntry *glyph = this->GetGlyphPtr(key);
	if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;

	FT_GlyphSlot slot = this->face->glyph;

	bool aa = GetFontAAState(this->fs);

	GlyphEntry new_glyph;
	if (key == 0) {
		GlyphID question_glyph = this->MapCharToGlyph('?');
		if (question_glyph == 0) {
			/* The font misses the '?' character. Use sprite font. */
			SpriteID sprite = this->GetUnicodeGlyph(key);
			Sprite *spr = (Sprite*)GetRawSprite(sprite, ST_FONT, AllocateFont);
			assert(spr != NULL);
			new_glyph.sprite = spr;
			new_glyph.width  = spr->width + (this->fs != FS_NORMAL);
			this->SetGlyphPtr(key, &new_glyph, false);
			return new_glyph.sprite;
		} else {
			/* Use '?' for missing characters. */
			this->GetGlyph(question_glyph);
			glyph = this->GetGlyphPtr(question_glyph);
			this->SetGlyphPtr(key, glyph, true);
			return glyph->sprite;
		}
	}
	FT_Load_Glyph(this->face, key, FT_LOAD_DEFAULT);
	FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);

	/* Despite requesting a normal glyph, FreeType may have returned a bitmap */
	aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);

	/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
	int width  = max(1, slot->bitmap.width + (this->fs == FS_NORMAL));
	int height = max(1, slot->bitmap.rows  + (this->fs == FS_NORMAL));

	/* Limit glyph size to prevent overflows later on. */
	if (width > 256 || height > 256) usererror("Font glyph is too large");

	/* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
	SpriteLoader::Sprite sprite;
	sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
	sprite.type = ST_FONT;
	sprite.width = width;
	sprite.height = height;
	sprite.x_offs = slot->bitmap_left;
	sprite.y_offs = this->ascender - slot->bitmap_top;

	/* Draw shadow for medium size */
	if (this->fs == FS_NORMAL && !aa) {
		for (int y = 0; y < slot->bitmap.rows; y++) {
			for (int x = 0; x < slot->bitmap.width; x++) {
				if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
					sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
				}
			}
		}
	}

	for (int y = 0; y < slot->bitmap.rows; y++) {
		for (int x = 0; x < slot->bitmap.width; x++) {
			if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
				sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
			}
		}
	}

	new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
	new_glyph.width  = slot->advance.x >> 6;

	this->SetGlyphPtr(key, &new_glyph);

	return new_glyph.sprite;
}


bool FreeTypeFontCache::GetDrawGlyphShadow()
{
	return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
}


uint FreeTypeFontCache::GetGlyphWidth(GlyphID key)
{
	if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);

	GlyphEntry *glyph = this->GetGlyphPtr(key);
	if (glyph == NULL || glyph->sprite == NULL) {
		this->GetGlyph(key);
		glyph = this->GetGlyphPtr(key);
	}

	return glyph->width;
}

GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
{
	assert(IsPrintable(key));

	if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
		return this->parent->MapCharToGlyph(key);
	}

	return FT_Get_Char_Index(this->face, key);
}

const void *FreeTypeFontCache::GetFontTable(uint32 tag, size_t &length)
{
	const FontTable::iterator iter = this->font_tables.Find(tag);
	if (iter != this->font_tables.End()) {
		length = iter->second.first;
		return iter->second.second;
	}

	FT_ULong len = 0;
	FT_Byte *result = NULL;

	FT_Load_Sfnt_Table(this->face, tag, 0, NULL, &len);

	if (len > 0) {
		result = MallocT<FT_Byte>(len);
		FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
	}
	length = len;

	this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
	return result;
}

#endif /* WITH_FREETYPE */

/**
 * (Re)initialize the freetype related things, i.e. load the non-sprite fonts.
 * @param monospace Whether to initialise the monospace or regular fonts.
 */
void InitFreeType(bool monospace)
{
	for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
		if (monospace != (fs == FS_MONO)) continue;

		FontCache *fc = FontCache::Get(fs);
		if (fc->HasParent()) delete fc;

#ifdef WITH_FREETYPE
		LoadFreeTypeFont(fs);
#endif
	}
}

/**
 * Free everything allocated w.r.t. fonts.
 */
void UninitFreeType()
{
	for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
		FontCache *fc = FontCache::Get(fs);
		if (fc->HasParent()) delete fc;
	}

#ifdef WITH_FREETYPE
	FT_Done_FreeType(_library);
	_library = NULL;
#endif /* WITH_FREETYPE */
}