1 /*
2 * FTGL - OpenGL font library
3 *
4 * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
5 * Copyright (c) 2008 Sam Hocevar <sam@hocevar.net>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #include "FTFace.h"
30 #include "FTCharmap.h"
31
32
FTCharmap(FTFace * face)33 FTCharmap::FTCharmap(FTFace* face)
34 : ftFace(*(face->Face())),
35 err(0)
36 {
37 if(!ftFace->charmap)
38 {
39 if(!ftFace->num_charmaps)
40 {
41 // This face doesn't even have one charmap!
42 err = 0x96; // Invalid_CharMap_Format
43 return;
44 }
45
46 err = FT_Set_Charmap(ftFace, ftFace->charmaps[0]);
47 }
48
49 ftEncoding = ftFace->charmap->encoding;
50
51 for(unsigned int i = 0; i < FTCharmap::MAX_PRECOMPUTED; i++)
52 {
53 charIndexCache[i] = FT_Get_Char_Index(ftFace, i);
54 }
55 }
56
57
~FTCharmap()58 FTCharmap::~FTCharmap()
59 {
60 charMap.clear();
61 }
62
63
CharMap(FT_Encoding encoding)64 bool FTCharmap::CharMap(FT_Encoding encoding)
65 {
66 if(ftEncoding == encoding)
67 {
68 err = 0;
69 return true;
70 }
71
72 err = FT_Select_Charmap(ftFace, encoding);
73
74 if(!err)
75 {
76 ftEncoding = encoding;
77 charMap.clear();
78 }
79
80 return !err;
81 }
82
83
GlyphListIndex(const unsigned int characterCode)84 unsigned int FTCharmap::GlyphListIndex(const unsigned int characterCode)
85 {
86 return charMap.find(characterCode);
87 }
88
89
FontIndex(const unsigned int characterCode)90 unsigned int FTCharmap::FontIndex(const unsigned int characterCode)
91 {
92 if(characterCode < FTCharmap::MAX_PRECOMPUTED)
93 {
94 return charIndexCache[characterCode];
95 }
96
97 return FT_Get_Char_Index(ftFace, characterCode);
98 }
99
100
InsertIndex(const unsigned int characterCode,const size_t containerIndex)101 void FTCharmap::InsertIndex(const unsigned int characterCode,
102 const size_t containerIndex)
103 {
104 charMap.insert(characterCode, static_cast<FTCharToGlyphIndexMap::GlyphIndex>(containerIndex));
105 }
106
107