-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProceduralMultiShapeGenerators.cs
331 lines (294 loc) · 11.1 KB
/
ProceduralMultiShapeGenerators.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//#define PROCEDURAL_USE_FREETYPE
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math=Mogre.Math;
namespace Mogre_Procedural
{
//*
// * \ingroup shapegrp
// * @{
//
#if PROCEDURAL_USE_FREETYPE
//-----------------------------------------------------------------------
//*
// * Produces a multishape from a given text
// * \image html shape_text.png
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport TextShape
public class TextShape
{
private String mText = "";
private String mFontName = "";
private byte mFontSize = 0;
// *
// Default constructor.
//
public TextShape()
{
mText = "OgreProcedural";
mFontName = "Arial";
mFontSize = 12;
}
// *
// Set the text content.
// \param text New text for processing (default "OgreProcedural")
// \exception Ogre::InvalidParametersException Empty text
//
public TextShape setText(string text)
{
if (text.empty())
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INVALIDPARAMS>(), "There must be more than 0 characters in text", "Procedural::TextShape::setText(Ogre::String text)", __FILE__, __LINE__);
;
mText = text;
return this;
}
// *
// Set the font for the text.
// \param fontName Filenpath of a font or name of font (only on windows desktops)
// \param fontSize Size of font [px] (default 12)
// \exception Ogre::InvalidParametersException Empty font name
// \exception Ogre::InvalidParametersException Font size is below 4
// \todo Add search for font names on non windows systems.
//
public TextShape setFont(string fontName, byte fontSize)
{
if (fontName.empty())
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INVALIDPARAMS>(), "There must be more than 0 characters in font name", "Procedural::TextShape::setFont(Ogre::String fontName, Ogre::byte fontSize)", __FILE__, __LINE__);
;
if (fontSize < 4)
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INVALIDPARAMS>(), "Minimum font size is 4", "Procedural::TextShape::setFont(Ogre::String fontName, Ogre::byte fontSize)", __FILE__, __LINE__);
;
mFontName = fontName;
mFontSize = fontSize;
return this;
}
// *
// * Build a MultiShape from chars (one Shape per character)
// * \exception Ogre::InternalErrorException Freetype error
// * \todo Need to split shapes of multi region chars. For example the letter \c O
// * has two shapes, but they are connected to one shape.
//
public MultiShape realizeShapes()
{
MultiShape retVal = new MultiShape();
FT_Library ftlib = new FT_Library();
FT_Face face = new FT_Face();
FT_GlyphSlot slot = new FT_GlyphSlot();
FT_Error error = FT_Init_FreeType(ftlib);
if (error == 0)
{
error = FT_New_Face(ftlib, getFontFileByName().c_str(), 0, face);
if (error == FT_Err_Unknown_File_Format)
{
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_Err_Unknown_File_Format", "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
;
}
else if (error != null)
{
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_New_Face - " + StringConverter.toString(error), "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
;
}
else
{
FT_Set_Pixel_Sizes(face, 0, mFontSize);
int px = 0;
int py = 0;
slot = face.glyph;
for (int n = 0; n < mText.length(); n++)
{
error = FT_Load_Char(face, mText[n], FT_LOAD_NO_BITMAP);
if (error != null)
continue;
Shape s = new Shape();
int nContours = face.glyph.outline.n_contours;
int startPos = 0;
string tags = face.glyph.outline.tags;
FT_Vector[] vec = face.glyph.outline.points;
for (int k = 0; k < nContours; k++)
{
if (k > 0)
startPos = face.glyph.outline.contours[k-1]+1;
int endPos = face.glyph.outline.contours[k]+1;
Vector2 lastPoint = Vector2.ZERO;
for (int j = startPos; j < endPos; j++)
{
if (FT_CURVE_TAG(tags[j]) == FT_CURVE_TAG_ON)
{
lastPoint = Vector2((float)vec[j].x, (float)vec[j].y);
s.addPoint(lastPoint / 64.0f);
}
else
{
if (FT_CURVE_TAG(tags[j]) == FT_CURVE_TAG_CUBIC)
{
int prevPoint = j - 1;
if (j == 0)
prevPoint = endPos - 1;
int nextIndex = j + 1;
if (nextIndex >= endPos)
nextIndex = startPos;
Vector2[] nextPoint = new Vector2[nextIndex]((float)vec.x, (float)vec[nextIndex].y);
if ((FT_CURVE_TAG(tags[prevPoint]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[prevPoint]) == FT_CURVE_TAG_CUBIC))
{
BezierCurve2 bc = new BezierCurve2();
bc.addPoint(Vector2((float)vec[prevPoint].x, (float)vec[prevPoint].y) / 64.0f);
bc.addPoint(Vector2((float)vec[j].x, (float)vec[j].y) / 64.0f);
bc.addPoint(Vector2((float)vec[nextIndex].x, (float)vec[nextIndex].y) / 64.0f);
s.appendShape(bc.realizeShape());
}
}
else
{
Vector2[] conicPoint = new Vector2[j]((float)vec.x, (float)vec[j].y);
if (j == startPos)
{
if ((FT_CURVE_TAG(tags[endPos-1]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[endPos-1]) != FT_CURVE_TAG_CUBIC))
{
Vector2[] lastConnic = new Vector2[endPos - 1]((float)vec.x, (float)vec[endPos - 1].y);
lastPoint = (conicPoint + lastConnic) / 2;
}
}
int nextIndex = j + 1;
if (nextIndex >= endPos)
nextIndex = startPos;
Vector2[] nextPoint = new Vector2[nextIndex]((float)vec.x, (float)vec[nextIndex].y);
bool nextIsConnic = (FT_CURVE_TAG(tags[nextIndex]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[nextIndex]) != FT_CURVE_TAG_CUBIC);
if (nextIsConnic)
nextPoint = (conicPoint + nextPoint) / 2;
int pc = s.getPointCount();
BezierCurve2 bc = new BezierCurve2();
if (pc == 0)
bc.addPoint(Vector2.ZERO);
else
bc.addPoint(s.getPoint(pc - 1));
bc.addPoint(lastPoint / 64.0f);
bc.addPoint(conicPoint / 64.0f);
bc.addPoint(nextPoint / 64.0f);
if (pc == 0)
s.appendShape(bc.realizeShape());
else
{
List<Vector2> subShape = bc.realizeShape().getPoints();
for (List<Vector2>.Enumerator iter = subShape.GetEnumerator(); iter.MoveNext(); iter++)
{
if (iter != subShape.GetEnumerator())
s.addPoint(iter.Current);
}
}
if (nextIsConnic)
//
//ORIGINAL LINE: lastPoint = nextPoint;
lastPoint=(nextPoint);
}
}
}
}
s.close();
s.translate((float)px, (float)py);
retVal.addShape(s);
px += slot.advance.x >> 6;
py += slot.advance.y >> 6;
}
FT_Done_Face(face);
}
FT_Done_FreeType(ftlib);
}
else
{
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
//C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
throw ExceptionFactory.create(Mogre.ExceptionCodeType<Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_Init_FreeTyp", "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
;
}
return retVal;
}
private String getFontFileByName()
{
string ff = "";
string tmp = "";
//C++ TO C# CONVERTER TODO TASK: C# does not allow setting or comparing #define constants:
#if PROCEDURAL_PLATFORM == PROCEDURAL_PLATFORM_WIN32
string windows = new string(new char[MAX_PATH]);
GetWindowsDirectory(windows, MAX_PATH);
bool result = getFontFile(mFontName, ref tmp, ref ff);
if (result == null)
return mFontName;
if (!(ff[0] == '\\' && ff[1] == '\\') && !(ff[1] == ':' && ff[2] == '\\'))
return String(windows) + "\\fonts\\" + ff;
else
return ff;
#else
return mFontName;
#endif
}
//C++ TO C# CONVERTER TODO TASK: C# does not allow setting or comparing #define constants:
#if PROCEDURAL_PLATFORM == PROCEDURAL_PLATFORM_WIN32
private bool getFontFile(string fontName, ref String displayName, ref String filePath)
{
if (fontName.empty())
return false;
if ((fontName[0] == '\\' && fontName[1] == '\\') || (fontName[1] == ':' && fontName[2] == '\\'))
{
displayName = fontName;
filePath = fontName;
return true;
}
string name = new string(new char[2 * MAX_PATH]);
string data = new string(new char[2 * MAX_PATH]);
filePath.empty();
bool retVal = false;
IntPtr hkFont;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", 0, KEY_READ, hkFont) == ERROR_SUCCESS)
{
string cname = new string(new char[MAX_PATH]);
uint icname = 0;
uint isubkeys = 0;
uint imaxsubkey = 0;
uint imaxclass = 0;
uint ivalues = 0;
uint imaxvalues = 0;
uint imaxnamevalues = 0;
uint isecurity = 0;
FILETIME dtlast = new FILETIME();
uint retCode = RegQueryInfoKey(hkFont, cname, icname, null, isubkeys, imaxsubkey, imaxclass, ivalues, imaxnamevalues, imaxvalues, isecurity, dtlast);
if (ivalues != 0)
{
for (uint i = 0; i < ivalues; i++)
{
retCode = ERROR_SUCCESS;
uint nsize = MAX_PATH - 1;
uint dsize = MAX_PATH - 1;
name = null;
data = null;
retCode = RegEnumValue(hkFont, i, name, nsize, null, null, (byte)data, dsize);
if (retCode == ERROR_SUCCESS)
if (strnicmp(name, fontName.c_str(), System.Math.Min(name.Length, fontName.length())) == 0)
{
displayName = name;
filePath = data;
retVal = true;
break;
}
}
}
}
RegCloseKey(hkFont);
return retVal;
}
#endif
}
#endif
}