-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathFont.cpp
669 lines (552 loc) · 16.2 KB
/
Font.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008-2013, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of Image Engine Design nor the names of any
// other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////
#include "IECoreScene/Font.h"
#include "IECoreScene/Group.h"
#include "IECoreScene/MatrixTransform.h"
#include "IECoreScene/MeshAlgo.h"
#include "IECoreScene/MeshPrimitive.h"
#include "IECoreScene/TransformOp.h"
#include "IECoreScene/Triangulator.h"
#include "IECore/BezierAlgo.h"
#include "IECore/BoxOps.h"
#include "IECore/PolygonAlgo.h"
#include "tbb/spin_mutex.h"
#include "ft2build.h"
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include <algorithm>
using namespace IECore;
using namespace IECoreScene;
using namespace Imath;
using namespace std;
IE_CORE_DEFINERUNTIMETYPED( Font );
////////////////////////////////////////////////////////////////////////////////
// Font::Mesher class implementation
////////////////////////////////////////////////////////////////////////////////
class Font::Mesher
{
public :
Mesher( FT_Pos curveTolerance );
MeshPrimitivePtr mesh( FT_Outline *outline );
private :
FT_Pos m_curveTolerance;
typedef vector<V3f> PointVector;
typedef std::pair<PointVector::const_iterator, PointVector::const_iterator> Loop;
typedef std::vector<Loop> LoopVector;
typedef std::vector<Box2f> BoundVector;
PointVector ¤tPointVector();
void addPoint( const V3f &p );
// used in the subdivision of bezier curves
class BezierCallback;
// functions to be used as part of an FT_Outline_Funcs_ struct
static int moveTo( const FT_Vector *to, void *that );
static int lineTo( const FT_Vector *to, void *that );
static int conicTo( const FT_Vector *control, const FT_Vector *to, void *that );
static int cubicTo( const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *that );
vector<PointVector> m_pointVectors;
};
// a functor used in subdividing bezier curves in the mesher
class Font::Mesher::BezierCallback
{
public :
BezierCallback( Font::Mesher *m )
: m_ignoredFirst( false ), m_mesher( m )
{
}
void operator()( const V3f &p )
{
if( !m_ignoredFirst )
{
m_ignoredFirst = true;
}
else
{
m_mesher->addPoint( p );
}
}
private :
bool m_ignoredFirst;
Font::Mesher *m_mesher;
};
Font::Mesher::Mesher( FT_Pos curveTolerance )
: m_curveTolerance( curveTolerance )
{
}
MeshPrimitivePtr Font::Mesher::mesh( FT_Outline *outline )
{
// break the contours down into our m_pointVectors
// structure
FT_Outline_Funcs_ funcs;
funcs.move_to = moveTo;
funcs.line_to = lineTo;
funcs.conic_to = conicTo;
funcs.cubic_to = cubicTo;
funcs.shift = 0;
funcs.delta = 0;
FT_Outline_Decompose( outline, &funcs, this );
// reverse the contours if necessary
if( !(outline->flags & FT_OUTLINE_REVERSE_FILL) )
{
for( unsigned i=0; i<m_pointVectors.size(); i++ )
{
std::reverse( m_pointVectors[i].begin(), m_pointVectors[i].end() );
}
}
// sort into outlines and holes
LoopVector outlines;
BoundVector outlineBounds;
LoopVector holes;
BoundVector holeBounds;
vector<bool> holeUsed;
for( unsigned i=0; i<m_pointVectors.size(); i++ )
{
if( m_pointVectors[i].size() )
{
// freetype explicitly joins the last segment to the beginning
// of the first, which gives us two coincident points. we don't
// want that as it confuses the triangulator.
m_pointVectors[i].resize( m_pointVectors[i].size() - 1 );
}
if( !m_pointVectors[i].size() )
{
continue;
}
if( polygonWinding( m_pointVectors[i].begin(), m_pointVectors[i].end() )==ClockwiseWinding )
{
holes.push_back( Loop( m_pointVectors[i].begin(), m_pointVectors[i].end() ) );
Box3f b = polygonBound( m_pointVectors[i].begin(), m_pointVectors[i].end() );
holeBounds.push_back( Box2f( V2f( b.min.x, b.min.y ), V2f( b.max.x, b.max.y ) ) );
holeUsed.push_back( false );
}
else
{
outlines.push_back( Loop( m_pointVectors[i].begin(), m_pointVectors[i].end() ) );
Box3f b = polygonBound( m_pointVectors[i].begin(), m_pointVectors[i].end() );
outlineBounds.push_back( Box2f( V2f( b.min.x, b.min.y ), V2f( b.max.x, b.max.y ) ) );
}
}
// triangulate each outline, along with any holes which should be associated with it
MeshPrimitiveBuilder::Ptr b = new MeshPrimitiveBuilder;
V3fTriangulator::Ptr t = new V3fTriangulator( b );
for( unsigned i=0; i<outlines.size(); i++ )
{
LoopVector loops;
loops.push_back( outlines[i] );
for( unsigned j=0; j<holes.size(); j++ )
{
if( !holeUsed[j] )
{
if( boxContains( outlineBounds[i], holeBounds[j] ) )
{
// the containment test is a bit weak - we might need to check
// that the edges of the outline and hole don't intersect either.
loops.push_back( holes[j] );
holeUsed[j] = true;
}
}
}
t->triangulate( loops.begin(), loops.end() );
}
return b->mesh();
}
Font::Mesher::PointVector &Font::Mesher::currentPointVector()
{
return *(m_pointVectors.rbegin());
}
void Font::Mesher::addPoint( const V3f &p )
{
PointVector &points = currentPointVector();
if( !points.size() || !(*(points.rbegin())).equalWithAbsError( p, 1e-6 ) )
{
points.push_back( p );
}
}
int Font::Mesher::moveTo( const FT_Vector *to, void *that )
{
Mesher *m = (Mesher *)( that );
m->m_pointVectors.push_back( PointVector() );
m->addPoint( V3f( to->x, to->y, 0 ) );
return 0;
}
int Font::Mesher::lineTo( const FT_Vector *to, void *that )
{
Mesher *m = (Mesher *)( that );
m->addPoint( V3f( to->x, to->y, 0 ) );
return 0;
}
int Font::Mesher::conicTo( const FT_Vector *control, const FT_Vector *to, void *that )
{
Mesher *m = (Mesher *)( that );
BezierCallback c( m );
bezierSubdivide(
*(m->currentPointVector().rbegin()),
V3f( control->x, control->y, 0 ),
V3f( to->x, to->y, 0 ),
m->m_curveTolerance,
c
);
return 0;
}
int Font::Mesher::cubicTo( const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *that )
{
Mesher *m = (Mesher *)( that );
BezierCallback c( m );
bezierSubdivide(
*(m->currentPointVector().rbegin()),
V3f( control1->x, control1->y, 0 ),
V3f( control2->x, control2->y, 0 ),
V3f( to->x, to->y, 0 ),
m->m_curveTolerance,
c
);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Font::Implementation
////////////////////////////////////////////////////////////////////////////////
class Font::Implementation : public IECore::RefCounted
{
public :
Implementation( const std::string &fontFile )
: m_fileName( fontFile ), m_kerning( 1.0f ), m_lineSpacing( 1.2f ), m_curveTolerance( 0.01 )
{
FreeTypeMutex::scoped_lock lock( g_freeTypeMutex );
FT_Error e = FT_New_Face( library(), fontFile.c_str(), 0, &m_face );
if( e )
{
throw Exception( "Error creating new FreeType face." );
}
m_meshes.resize( 128 );
}
~Implementation() override
{
FreeTypeMutex::scoped_lock lock( g_freeTypeMutex );
FT_Done_Face( m_face );
}
const std::string &fileName() const
{
return m_fileName;
}
void setKerning( float kerning )
{
m_kerning = kerning;
}
float getKerning() const
{
return m_kerning;
}
void setLineSpacing( float lineSpacing )
{
m_lineSpacing = lineSpacing;
}
float getLineSpacing() const
{
return m_lineSpacing;
}
void setCurveTolerance( float tolerance )
{
m_curveTolerance = tolerance;
m_meshes.clear();
}
float getCurveTolerance() const
{
return m_curveTolerance;
}
const MeshPrimitive *mesh( char c ) const
{
return cachedMesh( c )->primitive.get();
}
MeshPrimitivePtr mesh( const std::string &text ) const
{
MeshPrimitivePtr result = new MeshPrimitive;
V3fVectorDataPtr pData = new V3fVectorData;
pData->setInterpretation( GeometricData::Point );
result->variables["P"] = PrimitiveVariable( PrimitiveVariable::Vertex, pData );
if( !text.size() )
{
return result;
}
std::vector<MeshPrimitivePtr> characters;
std::vector<const MeshPrimitive *> meshes( { result.get() } );
TransformOpPtr transformOp = new TransformOp;
transformOp->copyParameter()->setTypedValue( false );
M44fDataPtr matrixData = new M44fData;
transformOp->matrixParameter()->setValue( matrixData );
V3f translate( 0.0f );
for( unsigned i=0; i<text.size(); i++ )
{
if( text[i] == '\n' )
{
translate.x = 0;
translate.y -= bound().size().y * m_lineSpacing;
continue;
}
const Mesh *character = cachedMesh( text[i] );
MeshPrimitivePtr primitive = character->primitive->copy();
transformOp->inputParameter()->setValue( primitive );
matrixData->writable() = M44f().setTranslation( translate );
transformOp->operate();
characters.push_back( primitive );
meshes.push_back( primitive.get() );
if( i<text.size()-1 )
{
const V2f a = advance( text[i], text[i+1] );
translate += V3f( a.x, a.y, 0 );
}
}
return MeshAlgo::merge( meshes );
}
GroupPtr meshGroup( const std::string &text ) const
{
GroupPtr result = new Group;
if( !text.size() )
{
return result;
}
V3f translate( 0.0f );
for( unsigned i=0; i<text.size(); i++ )
{
if( text[i] == '\n' )
{
translate.x = 0;
translate.y -= bound().size().y * m_lineSpacing;
continue;
}
const Mesh *character = cachedMesh( text[i] );
if( character->primitive->variableSize( PrimitiveVariable::Uniform ) )
{
GroupPtr g = new Group;
g->addChild( character->primitive->copy() );
g->setTransform( new MatrixTransform( M44f().setTranslation( translate ) ) );
result->addChild( g );
}
if( i<text.size()-1 )
{
const V2f a = advance( text[i], text[i+1] );
translate += V3f( a.x, a.y, 0 );
}
}
return result;
}
Imath::V2f advance( char first, char second ) const
{
V2f a = cachedMesh( first )->advance;
if( m_kerning!=0.0f )
{
FreeTypeMutex::scoped_lock lock( g_freeTypeMutex );
FT_UInt left = FT_Get_Char_Index( m_face, first );
FT_UInt right = FT_Get_Char_Index( m_face, second );
FT_Vector kerning;
FT_Error e = FT_Get_Kerning( m_face, left, right, FT_KERNING_UNSCALED, &kerning );
if( !e )
{
a += m_kerning * V2f( kerning.x, kerning.y ) / m_face->units_per_EM;
}
}
return a;
}
Imath::Box2f bound() const
{
float scale = 1.0f / (float)(m_face->units_per_EM);
return Box2f(
V2f( (float)m_face->bbox.xMin * scale, (float)m_face->bbox.yMin * scale ),
V2f( (float)m_face->bbox.xMax * scale, (float)m_face->bbox.yMax * scale )
);
}
Imath::Box2f bound( char c ) const
{
Imath::Box3f b = cachedMesh( c )->bound;
return Imath::Box2f( Imath::V2f( b.min.x, b.min.y ), Imath::V2f( b.max.x, b.max.y ) );
}
Imath::Box2f bound( const std::string &text ) const
{
Imath::Box2f result;
if( !text.size() )
{
return result;
}
V2f translate( 0 );
for( unsigned i=0; i<text.size(); i++ )
{
if( text[i] == '\n' )
{
translate.x = 0;
translate.y -= bound().size().y * m_lineSpacing;
continue;
}
Box2f b = bound( text[i] );
b.min += translate;
b.max += translate;
result.extendBy( b );
if( i<text.size()-1 )
{
translate += advance( text[i], text[i+1] );
}
}
return result;
}
private :
std::string m_fileName;
FT_Face m_face;
float m_kerning;
float m_lineSpacing;
float m_curveTolerance;
struct Mesh
{
ConstMeshPrimitivePtr primitive;
Box3f bound;
V2f advance;
};
typedef boost::shared_ptr<Mesh> MeshPtr;
typedef boost::shared_ptr<const Mesh> ConstMeshPtr;
mutable std::vector<MeshPtr> m_meshes;
const Mesh *cachedMesh( char c ) const
{
if( c < 0 )
{
// Map all invalid characters to capital X
c = 'X';
}
FreeTypeMutex::scoped_lock lock( g_freeTypeMutex );
// see if we have it cached
if( m_meshes[c] )
{
return m_meshes[c].get();
}
// not in cache, so load it
FT_Load_Char( m_face, c, FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE );
// get the mesh
Mesher m( (FT_Pos)(m_curveTolerance * m_face->units_per_EM) );
MeshPrimitivePtr primitive = m.mesh( &(m_face->glyph->outline) );
// transform it so an EM is 1 unit
M44f transform; transform.scale( V3f( 1.0f / m_face->units_per_EM ) );
TransformOpPtr transformOp = new TransformOp;
transformOp->inputParameter()->setValue( primitive );
transformOp->matrixParameter()->setValue( new M44fData( transform ) );
transformOp->copyParameter()->setTypedValue( false );
transformOp->operate();
// put it in the cache
MeshPtr mesh( new Mesh );
mesh->primitive = primitive;
mesh->bound = primitive->bound();
mesh->advance = V2f( m_face->glyph->advance.x, m_face->glyph->advance.y ) / m_face->units_per_EM;
m_meshes[c] = mesh;
// return it
return mesh.get();
}
typedef tbb::spin_mutex FreeTypeMutex;
static FreeTypeMutex g_freeTypeMutex;
static FT_Library library()
{
static FT_Library l;
static bool init = false;
if( !init )
{
FT_Error e = FT_Init_FreeType( &l );
if( e )
{
throw Exception( "Error initialising FreeType library." );
}
init = true;
}
return l;
}
};
Font::Implementation::FreeTypeMutex Font::Implementation::g_freeTypeMutex;
//////////////////////////////////////////////////////////////////////////////////////////
// Font
//////////////////////////////////////////////////////////////////////////////////////////
Font::Font( const std::string &fontFile )
: m_implementation( new Implementation( fontFile ) )
{
}
Font::~Font()
{
}
const std::string &Font::fileName() const
{
return m_implementation->fileName();
}
void Font::setKerning( float kerning )
{
m_implementation->setKerning( kerning );
}
float Font::getKerning() const
{
return m_implementation->getKerning();
}
void Font::setLineSpacing( float lineSpacing )
{
m_implementation->setLineSpacing( lineSpacing );
}
float Font::getLineSpacing() const
{
return m_implementation->getLineSpacing();
}
void Font::setCurveTolerance( float tolerance )
{
m_implementation->setCurveTolerance( tolerance );
}
float Font::getCurveTolerance() const
{
return m_implementation->getCurveTolerance();
}
const MeshPrimitive *Font::mesh( char c ) const
{
return m_implementation->mesh( c );
}
MeshPrimitivePtr Font::mesh( const std::string &text ) const
{
return m_implementation->mesh( text );
}
GroupPtr Font::meshGroup( const std::string &text ) const
{
return m_implementation->meshGroup( text );
}
Imath::V2f Font::advance( char first, char second ) const
{
return m_implementation->advance( first, second );
}
Imath::Box2f Font::bound() const
{
return m_implementation->bound();
}
Imath::Box2f Font::bound( char c ) const
{
return m_implementation->bound( c );
}
Imath::Box2f Font::bound( const std::string &text ) const
{
return m_implementation->bound( text );
}