1 /*------------------------------------------------------------------------ |
|
2 * |
|
3 * OpenVG 1.1 Reference Implementation |
|
4 * ----------------------------------- |
|
5 * |
|
6 * Copyright (c) 2007 The Khronos Group Inc. |
|
7 * |
|
8 * Permission is hereby granted, free of charge, to any person obtaining a |
|
9 * copy of this software and /or associated documentation files |
|
10 * (the "Materials "), to deal in the Materials without restriction, |
|
11 * including without limitation the rights to use, copy, modify, merge, |
|
12 * publish, distribute, sublicense, and/or sell copies of the Materials, |
|
13 * and to permit persons to whom the Materials are furnished to do so, |
|
14 * subject to the following conditions: |
|
15 * |
|
16 * The above copyright notice and this permission notice shall be included |
|
17 * in all copies or substantial portions of the Materials. |
|
18 * |
|
19 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
|
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR |
|
25 * THE USE OR OTHER DEALINGS IN THE MATERIALS. |
|
26 * |
|
27 *//** |
|
28 * \file |
|
29 * \brief Implementation of Font class. |
|
30 * \note |
|
31 *//*-------------------------------------------------------------------*/ |
|
32 |
|
33 #include "riFont.h" |
|
34 |
|
35 //============================================================================================== |
|
36 |
|
37 namespace OpenVGRI |
|
38 { |
|
39 |
|
40 /*-------------------------------------------------------------------*//*! |
|
41 * \brief Font constructor. |
|
42 * \param |
|
43 * \return |
|
44 * \note |
|
45 *//*-------------------------------------------------------------------*/ |
|
46 |
|
47 Font::Font(int capacityHint) : |
|
48 m_referenceCount(0), |
|
49 m_glyphs() |
|
50 { |
|
51 RI_ASSERT(capacityHint >= 0); |
|
52 m_glyphs.reserve(capacityHint); |
|
53 } |
|
54 |
|
55 /*-------------------------------------------------------------------*//*! |
|
56 * \brief Font destructor. |
|
57 * \param |
|
58 * \return |
|
59 * \note |
|
60 *//*-------------------------------------------------------------------*/ |
|
61 |
|
62 Font::~Font() |
|
63 { |
|
64 //remove references to paths and images |
|
65 for(int i=0;i<m_glyphs.size();i++) |
|
66 clearGlyph(&m_glyphs[i]); |
|
67 RI_ASSERT(m_referenceCount == 0); |
|
68 } |
|
69 |
|
70 /*-------------------------------------------------------------------*//*! |
|
71 * \brief Find a glyph based on glyphIndex. |
|
72 * \param |
|
73 * \return |
|
74 * \note |
|
75 *//*-------------------------------------------------------------------*/ |
|
76 |
|
77 Font::Glyph* Font::findGlyph(unsigned int index) |
|
78 { |
|
79 for(int i=0;i<m_glyphs.size();i++) |
|
80 { |
|
81 if(m_glyphs[i].m_state != Glyph::GLYPH_UNINITIALIZED && m_glyphs[i].m_index == index) |
|
82 return &m_glyphs[i]; |
|
83 } |
|
84 return NULL; |
|
85 } |
|
86 |
|
87 /*-------------------------------------------------------------------*//*! |
|
88 * \brief Find a free glyph or allocate a new one. |
|
89 * \param |
|
90 * \return |
|
91 * \note |
|
92 *//*-------------------------------------------------------------------*/ |
|
93 |
|
94 Font::Glyph* Font::newGlyph() |
|
95 { |
|
96 for(int i=0;i<m_glyphs.size();i++) |
|
97 { |
|
98 if(m_glyphs[i].m_state == Glyph::GLYPH_UNINITIALIZED) |
|
99 return &m_glyphs[i]; |
|
100 } |
|
101 m_glyphs.resize(m_glyphs.size()+1); |
|
102 return &m_glyphs[m_glyphs.size()-1]; |
|
103 } |
|
104 |
|
105 /*-------------------------------------------------------------------*//*! |
|
106 * \brief Free glyph and its data. |
|
107 * \param |
|
108 * \return |
|
109 * \note |
|
110 *//*-------------------------------------------------------------------*/ |
|
111 |
|
112 void Font::clearGlyph(Glyph* g) |
|
113 { |
|
114 RI_ASSERT(g); |
|
115 if(g->m_path != VG_INVALID_HANDLE) |
|
116 { |
|
117 Path* p = (Path*)g->m_path; |
|
118 if(!p->removeReference()) |
|
119 RI_DELETE(p); |
|
120 } |
|
121 if(g->m_image != VG_INVALID_HANDLE) |
|
122 { |
|
123 Image* p = (Image*)g->m_image; |
|
124 p->removeInUse(); |
|
125 if(!p->removeReference()) |
|
126 RI_DELETE(p); |
|
127 } |
|
128 Glyph a; |
|
129 *g = a; |
|
130 } |
|
131 |
|
132 /*-------------------------------------------------------------------*//*! |
|
133 * \brief |
|
134 * \param |
|
135 * \return |
|
136 * \note |
|
137 *//*-------------------------------------------------------------------*/ |
|
138 |
|
139 void Font::setGlyphToPath(unsigned int index, VGPath path, bool isHinted, const Vector2& origin, const Vector2& escapement) |
|
140 { |
|
141 Glyph* g = findGlyph(index); |
|
142 if(g) |
|
143 { //glyph exists, replace |
|
144 clearGlyph(g); |
|
145 } |
|
146 else |
|
147 { //glyph doesn't exist, allocate a new one |
|
148 g = newGlyph(); |
|
149 } |
|
150 |
|
151 g->m_index = index; |
|
152 g->m_state = Glyph::GLYPH_PATH; |
|
153 g->m_path = path; |
|
154 g->m_image = VG_INVALID_HANDLE; |
|
155 g->m_isHinted = isHinted; |
|
156 g->m_origin = origin; |
|
157 g->m_escapement = escapement; |
|
158 |
|
159 if(path != VG_INVALID_HANDLE) |
|
160 { |
|
161 Path* p = (Path*)path; |
|
162 p->addReference(); |
|
163 } |
|
164 } |
|
165 |
|
166 /*-------------------------------------------------------------------*//*! |
|
167 * \brief |
|
168 * \param |
|
169 * \return |
|
170 * \note |
|
171 *//*-------------------------------------------------------------------*/ |
|
172 |
|
173 void Font::setGlyphToImage(unsigned int index, VGImage image, const Vector2& origin, const Vector2& escapement) |
|
174 { |
|
175 Glyph* g = findGlyph(index); |
|
176 if(g) |
|
177 { //glyph exists, replace |
|
178 clearGlyph(g); |
|
179 } |
|
180 else |
|
181 { //glyph doesn't exist, allocate a new one |
|
182 g = newGlyph(); |
|
183 } |
|
184 |
|
185 g->m_index = index; |
|
186 g->m_state = Glyph::GLYPH_IMAGE; |
|
187 g->m_path = VG_INVALID_HANDLE; |
|
188 g->m_image = image; |
|
189 g->m_isHinted = false; |
|
190 g->m_origin = origin; |
|
191 g->m_escapement = escapement; |
|
192 |
|
193 if(image != VG_INVALID_HANDLE) |
|
194 { |
|
195 Image* p = (Image*)image; |
|
196 p->addReference(); |
|
197 p->addInUse(); |
|
198 } |
|
199 } |
|
200 |
|
201 //======================================================================= |
|
202 |
|
203 } //namespace OpenVGRI |
|