src/fontcache.cpp
author rubidium
Tue, 25 Dec 2007 11:26:07 +0000
changeset 8131 160939e24ed3
parent 8130 d2eb7d04f6e1
child 8214 971f861d5543
permissions -rw-r--r--
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     1
/* $Id$ */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     2
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5937
diff changeset
     3
/** @file fontcache.cpp */
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5937
diff changeset
     4
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     6
#include "openttd.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     7
#include "debug.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     8
#include "table/sprites.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
     9
#include "table/control_codes.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    10
#include "spritecache.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    11
#include "string.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    12
#include "fontcache.h"
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
    13
#include "spriteloader/spriteloader.hpp"
6937
40c760fcf1f6 (svn r10190) -Codechange: merged renderer and blitter to one single class API: blitter
truelight
parents: 6913
diff changeset
    14
#include "blitter/factory.hpp"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8121
diff changeset
    15
#include "gfx_func.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
    16
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    17
#include "core/math_func.hpp"
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    18
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    19
#ifdef WITH_FREETYPE
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    20
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    21
#include <ft2build.h>
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    22
#include FT_FREETYPE_H
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    23
#include FT_GLYPH_H
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    24
5117
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
    25
#ifdef WITH_FONTCONFIG
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
    26
#include <fontconfig/fontconfig.h>
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
    27
#endif
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
    28
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    29
static FT_Library _library = NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    30
static FT_Face _face_small = NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    31
static FT_Face _face_medium = NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    32
static FT_Face _face_large = NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    33
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    34
FreeTypeSettings _freetype;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    35
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    36
enum {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    37
	FACE_COLOUR = 1,
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    38
	SHADOW_COLOUR = 2,
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    39
};
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
    40
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    41
/** Get the font loaded into a Freetype face by using a font-name.
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    42
 * If no appropiate font is found, the function returns an error */
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    43
#ifdef WIN32
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    44
#include <windows.h>
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    45
#include <tchar.h>
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    46
#include <shlobj.h> // SHGetFolderPath
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    47
#include "win32.h"
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    48
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    49
/* Get the font file to be loaded into Freetype by looping the registry
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    50
 * location where windows lists all installed fonts. Not very nice, will
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    51
 * surely break if the registry path changes, but it works. Much better
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    52
 * solution would be to use CreateFont, and extract the font data from it
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    53
 * by GetFontData. The problem with this is that the font file needs to be
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    54
 * kept in memory then until the font is no longer needed. This could mean
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    55
 * an additional memory usage of 30MB (just for fonts!) when using an eastern
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    56
 * font for all font sizes */
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    57
#define FONT_DIR_NT "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    58
#define FONT_DIR_9X "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts"
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    59
static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    60
{
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    61
	FT_Error err = FT_Err_Cannot_Open_Resource;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    62
	HKEY hKey;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    63
	LONG ret;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    64
	TCHAR vbuffer[MAX_PATH], dbuffer[256];
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    65
	TCHAR *font_namep;
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    66
	char *font_path;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    67
	uint index;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    68
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    69
	/* On windows NT (2000, NT3.5, XP, etc.) the fonts are stored in the
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    70
	 * "Windows NT" key, on Windows 9x in the Windows key. To save us having
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    71
	 * to retrieve the windows version, we'll just query both */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    72
	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_NT), 0, KEY_READ, &hKey);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    73
	if (ret != ERROR_SUCCESS) ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_9X), 0, KEY_READ, &hKey);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    74
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    75
	if (ret != ERROR_SUCCESS) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
    76
		DEBUG(freetype, 0, "Cannot open registry key HKLM\\SOFTWARE\\Microsoft\\Windows (NT)\\CurrentVersion\\Fonts");
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    77
		return err;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    78
	}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    79
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    80
	/* For Unicode we need some conversion between widechar and
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    81
	 * normal char to match the data returned by RegEnumValue,
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    82
	 * otherwise just use parameter */
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    83
#if defined(UNICODE)
5937
27e781a9554d (svn r8576) -Fix: wrong type given to MallocT (VC2003, UNICODE build)
KUDr
parents: 5638
diff changeset
    84
	font_namep = MallocT<TCHAR>(MAX_PATH);
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    85
	MB_TO_WIDE_BUFFER(font_name, font_namep, MAX_PATH * sizeof(TCHAR));
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    86
#else
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    87
	font_namep = (char*)font_name; // only cast because in unicode pointer is not const
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    88
#endif
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    89
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    90
	for (index = 0;; index++) {
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    91
		TCHAR *s;
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    92
		DWORD vbuflen = lengthof(vbuffer);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    93
		DWORD dbuflen = lengthof(dbuffer);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    94
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
    95
		ret = RegEnumValue(hKey, index, vbuffer, &vbuflen, NULL, NULL, (byte*)dbuffer, &dbuflen);
5240
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
    96
		if (ret != ERROR_SUCCESS) goto registry_no_font_found;
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    97
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    98
		/* The font names in the registry are of the following 3 forms:
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
    99
		 * - ADMUI3.fon
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   100
		 * - Book Antiqua Bold (TrueType)
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   101
		 * - Batang & BatangChe & Gungsuh & GungsuhChe (TrueType)
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   102
		 * We will strip the font-type '()' if any and work with the font name
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   103
		 * itself, which must match exactly; if...
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   104
		 * TTC files, font files which contain more than one font are seperated
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   105
		 * byt '&'. Our best bet will be to do substr match for the fontname
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   106
		 * and then let FreeType figure out which index to load */
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   107
		s = _tcschr(vbuffer, _T('('));
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   108
		if (s != NULL) s[-1] = '\0';
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   109
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   110
		if (_tcschr(vbuffer, _T('&')) == NULL) {
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   111
			if (_tcsicmp(vbuffer, font_namep) == 0) break;
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   112
		} else {
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   113
			if (_tcsstr(vbuffer, font_namep) != NULL) break;
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   114
		}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   115
	}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   116
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   117
	if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, vbuffer))) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   118
		DEBUG(freetype, 0, "SHGetFolderPath cannot return fonts directory");
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   119
		goto folder_error;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   120
	}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   121
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   122
	/* Some fonts are contained in .ttc files, TrueType Collection fonts. These
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   123
	 * contain multiple fonts inside this single file. GetFontData however
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   124
	 * returns the whole file, so we need to check each font inside to get the
5240
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
   125
	 * proper font.
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   126
	 * Also note that FreeType does not support UNICODE filesnames! */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   127
#if defined(UNICODE)
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   128
	/* We need a cast here back from wide because FreeType doesn't support
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   129
	 * widechar filenames. Just use the buffer we allocated before for the
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   130
	 * font_name search */
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   131
	font_path = (char*)font_namep;
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   132
	WIDE_TO_MB_BUFFER(vbuffer, font_path, MAX_PATH * sizeof(TCHAR));
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   133
#else
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   134
	font_path = vbuffer;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   135
#endif
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   136
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   137
	ttd_strlcat(font_path, "\\", MAX_PATH * sizeof(TCHAR));
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   138
	ttd_strlcat(font_path, WIDE_TO_MB(dbuffer), MAX_PATH * sizeof(TCHAR));
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   139
	index = 0;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   140
	do {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   141
		err = FT_New_Face(_library, font_path, index, face);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   142
		if (err != FT_Err_Ok) break;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   143
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   144
		if (strncasecmp(font_name, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   145
		err = FT_Err_Cannot_Open_Resource;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   146
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   147
	} while ((FT_Long)++index != (*face)->num_faces);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   148
5267
3ebadb629742 (svn r7406) -Fix (r7377): [win32] Fontcache now also compiles in UNICODE mode; sorry, should've tested.
Darkvater
parents: 5240
diff changeset
   149
6388
278514553a20 (svn r9518) -Fix [Windows]: do not free an uninitialized pointer.
rubidium
parents: 6248
diff changeset
   150
folder_error:
5240
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
   151
registry_no_font_found:
6389
e1331b03c6ce (svn r9519) -Fix r7406 (revert r9518): free the malloced variable, not a pointer to that variable. Also free it if there's any error.
glx
parents: 6388
diff changeset
   152
#if defined(UNICODE)
e1331b03c6ce (svn r9519) -Fix r7406 (revert r9518): free the malloced variable, not a pointer to that variable. Also free it if there's any error.
glx
parents: 6388
diff changeset
   153
	free(font_namep);
e1331b03c6ce (svn r9519) -Fix r7406 (revert r9518): free the malloced variable, not a pointer to that variable. Also free it if there's any error.
glx
parents: 6388
diff changeset
   154
#endif
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   155
	RegCloseKey(hKey);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   156
	return err;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   157
}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   158
#else
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   159
# ifdef WITH_FONTCONFIG
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   160
static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   161
{
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   162
	FT_Error err = FT_Err_Cannot_Open_Resource;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   163
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   164
	if (!FcInit()) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   165
		ShowInfoF("Unable to load font configuration");
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   166
	} else {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   167
		FcPattern *match;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   168
		FcPattern *pat;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   169
		FcFontSet *fs;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   170
		FcResult  result;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   171
		char *font_style;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   172
		char *font_family;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   173
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   174
		/* Split & strip the font's style */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   175
		font_family = strdup(font_name);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   176
		font_style = strchr(font_family, ',');
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   177
		if (font_style != NULL) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   178
			font_style[0] = '\0';
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   179
			font_style++;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   180
			while (*font_style == ' ' || *font_style == '\t') font_style++;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   181
		}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   182
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   183
		/* Resolve the name and populate the information structure */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   184
		pat = FcNameParse((FcChar8*)font_family);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   185
		if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   186
		FcConfigSubstitute(0, pat, FcMatchPattern);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   187
		FcDefaultSubstitute(pat);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   188
		fs = FcFontSetCreate();
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   189
		match = FcFontMatch(0, pat, &result);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   190
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   191
		if (fs != NULL && match != NULL) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   192
			int i;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   193
			FcChar8 *family;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   194
			FcChar8 *style;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   195
			FcChar8 *file;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   196
			FcFontSetAdd(fs, match);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   197
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   198
			for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   199
				/* Try the new filename */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   200
				if (FcPatternGetString(fs->fonts[i], FC_FILE,   0, &file)   == FcResultMatch &&
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   201
						FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   202
						FcPatternGetString(fs->fonts[i], FC_STYLE,  0, &style)  == FcResultMatch) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   203
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   204
					/* The correct style? */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   205
					if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   206
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   207
					/* Font config takes the best shot, which, if the family name is spelled
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   208
					* wrongly a 'random' font, so check whether the family name is the
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   209
					* same as the supplied name */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   210
					if (strcasecmp(font_family, (char*)family) == 0) {
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   211
						err = FT_New_Face(_library, (char *)file, 0, face);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   212
					}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   213
				}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   214
			}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   215
		}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   216
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   217
		free(font_family);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   218
		FcPatternDestroy(pat);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   219
		FcFontSetDestroy(fs);
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   220
		FcFini();
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   221
	}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   223
	return err;
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   224
}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   225
# else
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   226
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   227
# endif /* WITH_FONTCONFIG */
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   228
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   229
#endif
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   230
5117
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   231
/**
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   232
 * Loads the freetype font.
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   233
 * First type to load the fontname as if it were a path. If that fails,
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   234
 * try to resolve the filename of the font using fontconfig, where the
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   235
 * format is 'font family name' or 'font family name, font style'.
d64bf0093ff4 (svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
rubidium
parents: 5108
diff changeset
   236
 */
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   237
static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   238
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   239
	FT_Error error;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   240
5638
ee1871005c80 (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5609
diff changeset
   241
	if (StrEmpty(font_name)) return;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   242
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   243
	error = FT_New_Face(_library, font_name, 0, face);
5219
f8f37c282c2d (svn r7334) -Revert accidentally committed file in r7333
Darkvater
parents: 5218
diff changeset
   244
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   245
	if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
5219
f8f37c282c2d (svn r7334) -Revert accidentally committed file in r7333
Darkvater
parents: 5218
diff changeset
   246
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   247
	if (error == FT_Err_Ok) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   248
		DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
5219
f8f37c282c2d (svn r7334) -Revert accidentally committed file in r7333
Darkvater
parents: 5218
diff changeset
   249
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   250
		/* Attempt to select the unicode character map */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   251
		error = FT_Select_Charmap(*face, ft_encoding_unicode);
5222
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   252
		if (error == FT_Err_Ok) return; // Success
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   253
66a48f112460 (svn r7337) -Feature: [win32] Add font selection by specifying a font-name, ala Fontconfig. Pretty
Darkvater
parents: 5219
diff changeset
   254
		if (error == FT_Err_Invalid_CharMap_Handle) {
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   255
			/* Try to pick a different character map instead. We default to
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   256
			 * the first map, but platform_id 0 encoding_id 0 should also
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   257
			 * be unicode (strange system...) */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   258
			FT_CharMap found = (*face)->charmaps[0];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   259
			int i;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   260
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   261
			for (i = 0; i < (*face)->num_charmaps; i++) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   262
				FT_CharMap charmap = (*face)->charmaps[i];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   263
				if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   264
					found = charmap;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   265
				}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   266
			}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   267
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   268
			if (found != NULL) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   269
				error = FT_Set_Charmap(*face, found);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   270
				if (error == FT_Err_Ok) return;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   271
			}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   272
		}
5240
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
   273
	}
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   274
5240
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
   275
	FT_Done_Face(*face);
56a1c6060364 (svn r7362) -Fix (r7337): Free the freetype face after any possible error. For win32 do not use the
Darkvater
parents: 5222
diff changeset
   276
	*face = NULL;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   277
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   278
	ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   279
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   280
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   281
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6179
diff changeset
   282
void InitFreeType()
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   283
{
5638
ee1871005c80 (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5609
diff changeset
   284
	if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   285
		DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   286
		return;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   287
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   288
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   289
	if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   290
		ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   291
		return;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   292
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   293
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   294
	DEBUG(freetype, 2, "Initialized");
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   295
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   296
	/* Load each font */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   297
	LoadFreeTypeFont(_freetype.small_font,  &_face_small,  "small");
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   298
	LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   299
	LoadFreeTypeFont(_freetype.large_font,  &_face_large,  "large");
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   300
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   301
	/* Set each font size */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   302
	if (_face_small  != NULL) FT_Set_Pixel_Sizes(_face_small,  0, _freetype.small_size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   303
	if (_face_medium != NULL) FT_Set_Pixel_Sizes(_face_medium, 0, _freetype.medium_size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   304
	if (_face_large  != NULL) FT_Set_Pixel_Sizes(_face_large,  0, _freetype.large_size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   305
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   306
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   307
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   308
static FT_Face GetFontFace(FontSize size)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   309
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   310
	switch (size) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   311
		default: NOT_REACHED();
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   312
		case FS_NORMAL: return _face_medium;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   313
		case FS_SMALL:  return _face_small;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   314
		case FS_LARGE:  return _face_large;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   315
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   316
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   317
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   318
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   319
struct GlyphEntry {
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   320
	Sprite *sprite;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   321
	byte width;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   322
};
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   323
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   324
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   325
/* The glyph cache. This is structured to reduce memory consumption.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   326
 * 1) There is a 'segment' table for each font size.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   327
 * 2) Each segment table is a discrete block of characters.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   328
 * 3) Each block contains 256 (aligned) characters sequential characters.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   329
 *
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   330
 * The cache is accessed in the following way:
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   331
 * For character 0x0041  ('A'): _glyph_ptr[FS_NORMAL][0x00][0x41]
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   332
 * For character 0x20AC (Euro): _glyph_ptr[FS_NORMAL][0x20][0xAC]
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   333
 *
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   334
 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   335
 * This can be simply changed in the two functions Get & SetGlyphPtr.
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   336
 */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   337
static GlyphEntry **_glyph_ptr[FS_END];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   338
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   339
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   340
static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   341
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   342
	if (_glyph_ptr[size] == NULL) return NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   343
	if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   344
	return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   345
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   346
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   347
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   348
static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   349
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   350
	if (_glyph_ptr[size] == NULL) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   351
		DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   352
		_glyph_ptr[size] = CallocT<GlyphEntry*>(256);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   353
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   354
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   355
	if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   356
		DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   357
		_glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   358
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   359
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   360
	DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   361
	_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   362
	_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width  = glyph->width;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   363
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   364
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   365
void *AllocateFont(size_t size)
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   366
{
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 7928
diff changeset
   367
	return MallocT<byte>(size);
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   368
}
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   369
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   370
6913
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   371
/* Check if a glyph should be rendered with antialiasing */
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   372
static bool GetFontAAState(FontSize size)
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   373
{
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   374
	/* AA is only supported for 32 bpp */
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   375
	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   376
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   377
	switch (size) {
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   378
		default: NOT_REACHED();
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   379
		case FS_NORMAL: return _freetype.medium_aa;
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   380
		case FS_SMALL:  return _freetype.small_aa;
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   381
		case FS_LARGE:  return _freetype.large_aa;
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   382
	}
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   383
}
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   384
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   385
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   386
const Sprite *GetGlyph(FontSize size, WChar key)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   387
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   388
	FT_Face face = GetFontFace(size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   389
	FT_GlyphSlot slot;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   390
	GlyphEntry new_glyph;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   391
	GlyphEntry *glyph;
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   392
	SpriteLoader::Sprite sprite;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   393
	int width;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   394
	int height;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   395
	int x;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   396
	int y;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   397
	int y_adj;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   398
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   399
	assert(IsPrintable(key));
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   400
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   401
	/* Bail out if no face loaded, or for our special characters */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   402
	if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   403
		SpriteID sprite = GetUnicodeGlyph(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   404
		if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   405
		return GetSprite(sprite);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   406
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   407
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   408
	/* Check for the glyph in our cache */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   409
	glyph = GetGlyphPtr(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   410
	if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   411
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   412
	slot = face->glyph;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   413
6913
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   414
	bool aa = GetFontAAState(size);
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   415
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   416
	FT_Load_Char(face, key, FT_LOAD_DEFAULT);
6913
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   417
	FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   418
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   419
	/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   420
	width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   421
	height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   422
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   423
	/* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   424
	sprite.data = CallocT<SpriteLoader::CommonPixel>(width * height);
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   425
	sprite.width = width;
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   426
	sprite.height = height;
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   427
	sprite.x_offs = slot->bitmap_left;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   428
	// XXX 2 should be determined somehow... it's right for the normal face
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   429
	y_adj = (size == FS_NORMAL) ? 2 : 0;
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   430
	sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   431
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   432
	/* Draw shadow for medium size */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   433
	if (size == FS_NORMAL) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   434
		for (y = 0; y < slot->bitmap.rows; y++) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   435
			for (x = 0; x < slot->bitmap.width; x++) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 6937
diff changeset
   436
				if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   437
					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
6913
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   438
					sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   439
				}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   440
			}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   441
		}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   442
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   443
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   444
	for (y = 0; y < slot->bitmap.rows; y++) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   445
		for (x = 0; x < slot->bitmap.width; x++) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 6937
diff changeset
   446
			if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   447
				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
6913
ff5f8a9c1747 (svn r10166) -Feature(tte): Add support for antialiased typefaces via FreeType. This is configurable for each font size in the configuration settings and requires using the 32bpp blitter and suitable fonts.
peter1138
parents: 6895
diff changeset
   448
				sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   449
			}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   450
		}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   451
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   452
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   453
	new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   454
	free(sprite.data);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   455
	new_glyph.width  = (slot->advance.x >> 6) + (size != FS_NORMAL);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   456
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   457
	SetGlyphPtr(size, key, &new_glyph);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   458
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   459
	return new_glyph.sprite;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   460
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   461
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   462
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   463
uint GetGlyphWidth(FontSize size, WChar key)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   464
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   465
	FT_Face face = GetFontFace(size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   466
	GlyphEntry *glyph;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   467
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   468
	if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   469
		SpriteID sprite = GetUnicodeGlyph(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   470
		if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   471
		return SpriteExists(sprite) ? GetSprite(sprite)->width + (size != FS_NORMAL) : 0;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   472
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   473
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   474
	glyph = GetGlyphPtr(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   475
	if (glyph == NULL || glyph->sprite == NULL) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   476
		GetGlyph(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   477
		glyph = GetGlyphPtr(size, key);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   478
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   479
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   480
	return glyph->width;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   481
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   482
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   483
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   484
#endif /* WITH_FREETYPE */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   485
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   486
/* Sprite based glyph mapping */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   487
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   488
#include "table/unicode.h"
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   489
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   490
static SpriteID **_unicode_glyph_map[FS_END];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   491
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   492
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   493
/** Get the SpriteID of the first glyph for the given font size */
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   494
static SpriteID GetFontBase(FontSize size)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   495
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   496
	switch (size) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   497
		default: NOT_REACHED();
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   498
		case FS_NORMAL: return SPR_ASCII_SPACE;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   499
		case FS_SMALL:  return SPR_ASCII_SPACE_SMALL;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   500
		case FS_LARGE:  return SPR_ASCII_SPACE_BIG;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   501
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   502
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   503
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   504
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   505
SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   506
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   507
	if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) return 0;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   508
	return _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   509
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   510
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   511
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   512
void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite)
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   513
{
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   514
	if (_unicode_glyph_map[size] == NULL) _unicode_glyph_map[size] = CallocT<SpriteID*>(256);
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   515
	if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) _unicode_glyph_map[size][GB(key, 8, 8)] = CallocT<SpriteID>(256);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   516
	_unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   517
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   518
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   519
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6179
diff changeset
   520
void InitializeUnicodeGlyphMap()
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   521
{
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   522
	FontSize size;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   523
	SpriteID base;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   524
	SpriteID sprite;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   525
	uint i;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   526
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   527
	for (size = FS_NORMAL; size != FS_END; size++) {
5153
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   528
		/* Clear out existing glyph map if it exists */
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   529
		if (_unicode_glyph_map[size] != NULL) {
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   530
			for (i = 0; i < 256; i++) {
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   531
				if (_unicode_glyph_map[size][i] != NULL) free(_unicode_glyph_map[size][i]);
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   532
			}
5154
d64a7dc0fcdb (svn r7249) -Fix (r7248): Missed a free()...
peter1138
parents: 5153
diff changeset
   533
			free(_unicode_glyph_map[size]);
5153
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   534
			_unicode_glyph_map[size] = NULL;
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   535
		}
5e6b2790264f (svn r7248) -Codechange: Support reinitializing the unicode glyph map.
peter1138
parents: 5117
diff changeset
   536
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   537
		base = GetFontBase(size);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   538
		for (i = ASCII_LETTERSTART; i < 256; i++) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   539
			sprite = base + i - ASCII_LETTERSTART;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   540
			if (!SpriteExists(sprite)) continue;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   541
			SetUnicodeGlyph(size, i, sprite);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   542
			SetUnicodeGlyph(size, i + SCC_SPRITE_START, sprite);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   543
		}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   544
		for (i = 0; i < lengthof(_default_unicode_map); i++) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   545
			sprite = base + _default_unicode_map[i].key - ASCII_LETTERSTART;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   546
			SetUnicodeGlyph(size, _default_unicode_map[i].code, sprite);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   547
		}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   548
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   549
}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents:
diff changeset
   550
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   551
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5270
diff changeset
   552