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 "FTGL/ftgl.h"
30
31 #include "FTGlyphContainer.h"
32 #include "FTFace.h"
33 #include "FTCharmap.h"
34
35
FTGlyphContainer(FTFace * f)36 FTGlyphContainer::FTGlyphContainer(FTFace* f)
37 : face(f),
38 err(0)
39 {
40 glyphs.push_back(NULL);
41 charMap = new FTCharmap(face);
42 }
43
44
~FTGlyphContainer()45 FTGlyphContainer::~FTGlyphContainer()
46 {
47 GlyphVector::iterator it;
48 for(it = glyphs.begin(); it != glyphs.end(); ++it)
49 {
50 delete *it;
51 }
52
53 glyphs.clear();
54 delete charMap;
55 }
56
57
CharMap(FT_Encoding encoding)58 bool FTGlyphContainer::CharMap(FT_Encoding encoding)
59 {
60 bool result = charMap->CharMap(encoding);
61 err = charMap->Error();
62 return result;
63 }
64
65
FontIndex(const unsigned int charCode) const66 unsigned int FTGlyphContainer::FontIndex(const unsigned int charCode) const
67 {
68 return charMap->FontIndex(charCode);
69 }
70
71
Add(FTGlyph * tempGlyph,const unsigned int charCode)72 void FTGlyphContainer::Add(FTGlyph* tempGlyph, const unsigned int charCode)
73 {
74 charMap->InsertIndex(charCode, glyphs.size());
75 glyphs.push_back(tempGlyph);
76 }
77
78
Glyph(const unsigned int charCode) const79 const FTGlyph* FTGlyphContainer::Glyph(const unsigned int charCode) const
80 {
81 unsigned int index = charMap->GlyphListIndex(charCode);
82
83 return (index < glyphs.size()) ? glyphs[index] : NULL;
84 }
85
86
BBox(const unsigned int charCode) const87 FTBBox FTGlyphContainer::BBox(const unsigned int charCode) const
88 {
89 return Glyph(charCode)->BBox();
90 }
91
92
Advance(const unsigned int charCode,const unsigned int nextCharCode)93 float FTGlyphContainer::Advance(const unsigned int charCode,
94 const unsigned int nextCharCode)
95 {
96 unsigned int left = charMap->FontIndex(charCode);
97 unsigned int right = charMap->FontIndex(nextCharCode);
98 const FTGlyph *glyph = Glyph(charCode);
99
100 if (!glyph)
101 return 0.0f;
102
103 return face->KernAdvance(left, right).Xf() + glyph->Advance();
104 }
105
106
Render(const unsigned int charCode,const unsigned int nextCharCode,FTPoint penPosition,int renderMode)107 FTPoint FTGlyphContainer::Render(const unsigned int charCode,
108 const unsigned int nextCharCode,
109 FTPoint penPosition, int renderMode)
110 {
111 unsigned int left = charMap->FontIndex(charCode);
112 unsigned int right = charMap->FontIndex(nextCharCode);
113
114 FTPoint kernAdvance = face->KernAdvance(left, right);
115
116 if(!face->Error())
117 {
118 unsigned int index = charMap->GlyphListIndex(charCode);
119 if (index < glyphs.size())
120 kernAdvance += glyphs[index]->Render(penPosition, renderMode);
121 }
122
123 return kernAdvance;
124 }
125
126