b514beda |
1 | // Created on: 2013-09-16 |
d5f74e42 |
2 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
b514beda |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b514beda |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b514beda |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b514beda |
14 | |
15 | #include <Font_BRepFont.hxx> |
16 | |
17 | #include <BRep_Tool.hxx> |
18 | #include <BRepBuilderAPI_MakeFace.hxx> |
19 | #include <BRepBuilderAPI_MakeWire.hxx> |
20 | #include <BRepLib_MakeEdge.hxx> |
f9801cf9 |
21 | #include <Font_FTLibrary.hxx> |
ac84fcf6 |
22 | #include <Font_TextFormatter.hxx> |
b514beda |
23 | #include <GCE2d_MakeSegment.hxx> |
24 | #include <GC_MakeSegment.hxx> |
25 | #include <Geom_BezierCurve.hxx> |
26 | #include <Geom_BSplineCurve.hxx> |
c04c30b3 |
27 | #include <Geom2d_TrimmedCurve.hxx> |
b514beda |
28 | #include <Geom_Plane.hxx> |
29 | #include <Geom2d_BezierCurve.hxx> |
30 | #include <Geom2d_BSplineCurve.hxx> |
543a9964 |
31 | #include <Geom2d_TrimmedCurve.hxx> |
b514beda |
32 | #include <Geom2d_Line.hxx> |
33 | #include <GeomAPI.hxx> |
34 | #include <GeomAdaptor_HSurface.hxx> |
35 | #include <GeomLib.hxx> |
36 | #include <gp_Pln.hxx> |
37 | #include <ShapeBuild_ReShape.hxx> |
38 | #include <ShapeFix_Edge.hxx> |
39 | #include <ShapeFix_Wire.hxx> |
40 | #include <TColGeom2d_HSequenceOfBoundedCurve.hxx> |
41 | #include <TCollection_AsciiString.hxx> |
42 | #include <TCollection_HAsciiString.hxx> |
43 | #include <TopExp.hxx> |
44 | #include <TopExp_Explorer.hxx> |
45 | #include <TopoDS.hxx> |
46 | #include <TopoDS_Compound.hxx> |
47 | #include <TopoDS_Vertex.hxx> |
48 | |
f9801cf9 |
49 | #include <ft2build.h> |
50 | #include FT_FREETYPE_H |
b514beda |
51 | #include FT_OUTLINE_H |
52 | |
92efcf78 |
53 | IMPLEMENT_STANDARD_RTTIEXT(Font_BRepFont,Font_FTFont) |
54 | |
b514beda |
55 | namespace |
56 | { |
57 | // pre-defined font rendering options |
58 | static const unsigned int THE_FONT_SIZE = 72; |
59 | static const unsigned int THE_RESOLUTION_DPI = 4800; |
60 | |
61 | // compute scaling factor for specified font size |
62 | inline Standard_Real getScale (const Standard_Real theSize) |
63 | { |
64 | return theSize / Standard_Real(THE_FONT_SIZE) * 72.0 / Standard_Real(THE_RESOLUTION_DPI); |
65 | } |
66 | |
f9801cf9 |
67 | //! Auxiliary method to convert FT_Vector to gp_XY |
68 | static gp_XY readFTVec (const FT_Vector& theVec, |
69 | const Standard_Real theScaleUnits) |
70 | { |
71 | return gp_XY (theScaleUnits * Standard_Real(theVec.x) / 64.0, theScaleUnits * Standard_Real(theVec.y) / 64.0); |
72 | } |
73 | |
a3f6f591 |
74 | } |
b514beda |
75 | |
76 | // ======================================================================= |
77 | // function : Constructor |
78 | // purpose : |
79 | // ======================================================================= |
80 | Font_BRepFont::Font_BRepFont () |
81 | : myPrecision (Precision::Confusion()), |
82 | myScaleUnits (1.0), |
83 | myIsCompositeCurve (Standard_False), |
84 | my3Poles (1, 3), |
85 | my4Poles (1, 4) |
86 | { |
87 | init(); |
88 | } |
89 | |
90 | // ======================================================================= |
91 | // function : init |
92 | // purpose : |
93 | // ======================================================================= |
94 | void Font_BRepFont::init() |
95 | { |
96 | mySurface = new Geom_Plane (gp_Pln (gp::XOY())); |
97 | myCurve2dAdaptor = new Geom2dAdaptor_HCurve(); |
543a9964 |
98 | Handle(Adaptor3d_HSurface) aSurfAdaptor = new GeomAdaptor_HSurface (mySurface); |
b514beda |
99 | myCurvOnSurf.Load (aSurfAdaptor); |
100 | |
101 | myFixer.FixWireMode() = 1; |
102 | myFixer.FixOrientationMode() = 1; |
103 | myFixer.FixSplitFaceMode() = 1; // some glyphs might be composed from several faces |
104 | Handle(ShapeFix_Wire) aWireFixer = myFixer.FixWireTool(); |
105 | aWireFixer->FixConnectedMode() = 1; |
106 | aWireFixer->ClosedWireMode() = Standard_True; |
107 | Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape(); |
108 | myFixer.SetContext (aContext); |
109 | } |
110 | |
111 | // ======================================================================= |
112 | // function : Constructor |
113 | // purpose : |
114 | // ======================================================================= |
115 | Font_BRepFont::Font_BRepFont (const NCollection_String& theFontPath, |
116 | const Standard_Real theSize) |
117 | : myPrecision (Precision::Confusion()), |
118 | myScaleUnits (1.0), |
119 | myIsCompositeCurve (Standard_False), |
120 | my3Poles (1, 3), |
121 | my4Poles (1, 4) |
122 | { |
123 | init(); |
124 | if (theSize <= myPrecision * 100.0) |
125 | { |
126 | return; |
127 | } |
128 | |
129 | myScaleUnits = getScale (theSize); |
130 | Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI); |
131 | } |
132 | |
133 | // ======================================================================= |
134 | // function : Constructor |
135 | // purpose : |
136 | // ======================================================================= |
137 | Font_BRepFont::Font_BRepFont (const NCollection_String& theFontName, |
138 | const Font_FontAspect theFontAspect, |
139 | const Standard_Real theSize) |
140 | : myPrecision (Precision::Confusion()), |
141 | myScaleUnits (1.0), |
142 | myIsCompositeCurve (Standard_False), |
143 | my3Poles (1, 3), |
144 | my4Poles (1, 4) |
145 | { |
146 | init(); |
147 | if (theSize <= myPrecision * 100.0) |
148 | { |
149 | return; |
150 | } |
151 | |
152 | myScaleUnits = getScale (theSize); |
153 | Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI); |
154 | } |
155 | |
156 | // ======================================================================= |
157 | // function : Release |
158 | // purpose : |
159 | // ======================================================================= |
160 | void Font_BRepFont::Release() |
161 | { |
162 | myCache.Clear(); |
163 | Font_FTFont::Release(); |
164 | } |
165 | |
166 | // ======================================================================= |
167 | // function : SetCompositeCurveMode |
168 | // purpose : |
169 | // ======================================================================= |
170 | void Font_BRepFont::SetCompositeCurveMode (const Standard_Boolean theToConcatenate) |
171 | { |
172 | if (myIsCompositeCurve != theToConcatenate) |
173 | { |
174 | myIsCompositeCurve = theToConcatenate; |
175 | myCache.Clear(); |
176 | } |
177 | } |
178 | |
179 | // ======================================================================= |
180 | // function : Init |
181 | // purpose : |
182 | // ======================================================================= |
183 | bool Font_BRepFont::Init (const NCollection_String& theFontPath, |
184 | const Standard_Real theSize) |
185 | { |
186 | if (theSize <= myPrecision * 100.0) |
187 | { |
188 | return false; |
189 | } |
190 | |
191 | myScaleUnits = getScale (theSize); |
192 | return Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI); |
193 | } |
194 | |
195 | // ======================================================================= |
196 | // function : Init |
197 | // purpose : |
198 | // ======================================================================= |
199 | bool Font_BRepFont::Init (const NCollection_String& theFontName, |
200 | const Font_FontAspect theFontAspect, |
201 | const Standard_Real theSize) |
202 | { |
203 | if (theSize <= myPrecision * 100.0) |
204 | { |
205 | return false; |
206 | } |
207 | |
208 | myScaleUnits = getScale (theSize); |
209 | return Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI); |
210 | } |
211 | |
212 | // ======================================================================= |
213 | // function : RenderGlyph |
214 | // purpose : |
215 | // ======================================================================= |
216 | TopoDS_Shape Font_BRepFont::RenderGlyph (const Standard_Utf32Char& theChar) |
217 | { |
218 | TopoDS_Shape aShape; |
219 | Standard_Mutex::Sentry aSentry (myMutex); |
220 | renderGlyph (theChar, aShape); |
221 | return aShape; |
222 | } |
223 | |
224 | // ======================================================================= |
225 | // function : to3d |
226 | // purpose : |
227 | // ======================================================================= |
543a9964 |
228 | bool Font_BRepFont::to3d (const Handle(Geom2d_Curve)& theCurve2d, |
b514beda |
229 | const GeomAbs_Shape theContinuity, |
230 | Handle(Geom_Curve)& theCurve3d) |
231 | { |
232 | Standard_Real aMaxDeviation = 0.0; |
233 | Standard_Real anAverDeviation = 0.0; |
234 | myCurve2dAdaptor->ChangeCurve2d().Load (theCurve2d); |
543a9964 |
235 | const Handle(Adaptor2d_HCurve2d)& aCurve = myCurve2dAdaptor; // to avoid ambiguity |
236 | myCurvOnSurf.Load (aCurve); |
b514beda |
237 | GeomLib::BuildCurve3d (myPrecision, myCurvOnSurf, |
238 | myCurve2dAdaptor->FirstParameter(), myCurve2dAdaptor->LastParameter(), |
239 | theCurve3d, aMaxDeviation, anAverDeviation, theContinuity); |
240 | return !theCurve3d.IsNull(); |
241 | } |
242 | |
243 | // ======================================================================= |
244 | // function : renderGlyph |
245 | // purpose : |
246 | // ======================================================================= |
247 | Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar, |
248 | TopoDS_Shape& theShape) |
249 | { |
250 | theShape.Nullify(); |
251 | if (!loadGlyph (theChar) |
252 | || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE) |
253 | { |
254 | return Standard_False; |
255 | } |
256 | else if (myCache.Find (theChar, theShape)) |
257 | { |
258 | return !theShape.IsNull(); |
259 | } |
260 | |
be7d4aa2 |
261 | FT_Outline& anOutline = myFTFace->glyph->outline; |
262 | |
263 | if (!anOutline.n_contours) |
264 | return Standard_False; |
265 | |
b514beda |
266 | TopLoc_Location aLoc; |
267 | TopoDS_Face aFaceDraft; |
268 | myBuilder.MakeFace (aFaceDraft, mySurface, myPrecision); |
be7d4aa2 |
269 | |
b514beda |
270 | // Get orientation is useless since it doesn't retrieve any in-font information and just computes orientation. |
271 | // Because it fails in some cases - leave this to ShapeFix. |
272 | //const FT_Orientation anOrient = FT_Outline_Get_Orientation (&anOutline); |
273 | for (short aContour = 0, aStartIndex = 0; aContour < anOutline.n_contours; ++aContour) |
274 | { |
275 | const FT_Vector* aPntList = &anOutline.points[aStartIndex]; |
276 | const char* aTags = &anOutline.tags[aStartIndex]; |
277 | const short anEndIndex = anOutline.contours[aContour]; |
278 | const short aPntsNb = (anEndIndex - aStartIndex) + 1; |
279 | aStartIndex = anEndIndex + 1; |
280 | if (aPntsNb < 3) |
281 | { |
282 | // closed contour can not be constructed from < 3 points |
283 | continue; |
284 | } |
285 | |
286 | BRepBuilderAPI_MakeWire aWireMaker; |
287 | |
288 | gp_XY aPntPrev; |
f9801cf9 |
289 | gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1], myScaleUnits); |
290 | gp_XY aPntNext = readFTVec (aPntList[0], myScaleUnits); |
b514beda |
291 | |
292 | Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0; |
293 | gp_XY aPntLine1 = aPntCurr; |
294 | |
295 | // see http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html |
296 | // for a full description of FreeType tags. |
297 | for (short aPntId = 0; aPntId < aPntsNb; ++aPntId) |
298 | { |
299 | aPntPrev = aPntCurr; |
300 | aPntCurr = aPntNext; |
f9801cf9 |
301 | aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb], myScaleUnits); |
b514beda |
302 | |
303 | // process tags |
304 | if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On) |
305 | { |
306 | if (aLinePnts < 1) |
307 | { |
308 | aPntLine1 = aPntCurr; |
309 | aLinePnts = 1; |
310 | continue; |
311 | } |
312 | |
313 | const gp_XY aDirVec = aPntCurr - aPntLine1; |
314 | const Standard_Real aLen = aDirVec.Modulus(); |
315 | if (aLen <= myPrecision) |
316 | { |
317 | aPntLine1 = aPntCurr; |
318 | aLinePnts = 1; |
319 | continue; |
320 | } |
321 | |
322 | if (myIsCompositeCurve) |
323 | { |
324 | Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr)); |
325 | myConcatMaker.Add (aLine, myPrecision); |
326 | } |
327 | else |
328 | { |
329 | Handle(Geom_Curve) aCurve3d; |
330 | Handle(Geom2d_Line) aCurve2d = new Geom2d_Line (gp_Pnt2d (aPntLine1), gp_Dir2d (aDirVec)); |
331 | if (to3d (aCurve2d, GeomAbs_C1, aCurve3d)) |
332 | { |
333 | TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen); |
334 | myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision); |
335 | aWireMaker.Add (anEdge); |
336 | } |
337 | } |
338 | aPntLine1 = aPntCurr; |
339 | } |
340 | else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic) |
341 | { |
342 | aLinePnts = 0; |
343 | gp_XY aPntPrev2 = aPntPrev; |
344 | gp_XY aPntNext2 = aPntNext; |
345 | |
346 | // previous point is either the real previous point (an "on" point), |
347 | // or the midpoint between the current one and the previous "conic off" point |
348 | if (FT_CURVE_TAG(aTags[(aPntId - 1 + aPntsNb) % aPntsNb]) == FT_Curve_Tag_Conic) |
349 | { |
350 | aPntPrev2 = (aPntCurr + aPntPrev) * 0.5; |
351 | } |
352 | |
353 | // next point is either the real next point or the midpoint |
354 | if (FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Conic) |
355 | { |
356 | aPntNext2 = (aPntCurr + aPntNext) * 0.5; |
357 | } |
358 | |
359 | my3Poles.SetValue (1, aPntPrev2); |
360 | my3Poles.SetValue (2, aPntCurr); |
361 | my3Poles.SetValue (3, aPntNext2); |
362 | Handle(Geom2d_BezierCurve) aBezierArc = new Geom2d_BezierCurve (my3Poles); |
363 | if (myIsCompositeCurve) |
364 | { |
365 | myConcatMaker.Add (aBezierArc, myPrecision); |
366 | } |
367 | else |
368 | { |
369 | Handle(Geom_Curve) aCurve3d; |
370 | if (to3d (aBezierArc, GeomAbs_C1, aCurve3d)) |
371 | { |
372 | TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d); |
373 | myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision); |
374 | aWireMaker.Add (anEdge); |
375 | } |
376 | } |
377 | } |
378 | else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Cubic |
379 | && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic) |
380 | { |
381 | aLinePnts = 0; |
382 | my4Poles.SetValue (1, aPntPrev); |
383 | my4Poles.SetValue (2, aPntCurr); |
384 | my4Poles.SetValue (3, aPntNext); |
f9801cf9 |
385 | my4Poles.SetValue (4, gp_Pnt2d(readFTVec (aPntList[(aPntId + 2) % aPntsNb], myScaleUnits))); |
b514beda |
386 | Handle(Geom2d_BezierCurve) aBezier = new Geom2d_BezierCurve (my4Poles); |
387 | if (myIsCompositeCurve) |
388 | { |
389 | myConcatMaker.Add (aBezier, myPrecision); |
390 | } |
391 | else |
392 | { |
393 | Handle(Geom_Curve) aCurve3d; |
394 | if (to3d (aBezier, GeomAbs_C1, aCurve3d)) |
395 | { |
396 | TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d); |
397 | myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision); |
398 | aWireMaker.Add (anEdge); |
399 | } |
400 | } |
401 | } |
402 | } |
403 | |
404 | if (myIsCompositeCurve) |
405 | { |
406 | Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve(); |
407 | if (aDraft2d.IsNull()) |
408 | { |
409 | continue; |
410 | } |
411 | |
412 | const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint(); |
413 | const gp_Pnt2d aLastPnt = aDraft2d->EndPoint(); |
414 | if (!aFirstPnt.IsEqual (aLastPnt, myPrecision)) |
415 | { |
416 | Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt); |
417 | myConcatMaker.Add (aLine, myPrecision); |
418 | } |
419 | |
420 | Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve(); |
421 | Handle(Geom_Curve) aCurve3d; |
422 | if (to3d (aCurve2d, GeomAbs_C0, aCurve3d)) |
423 | { |
424 | TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d); |
425 | myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision); |
426 | aWireMaker.Add (anEdge); |
427 | } |
428 | myConcatMaker.Clear(); |
429 | } |
430 | else |
431 | { |
432 | if (!aWireMaker.IsDone()) |
433 | { |
434 | continue; |
435 | } |
436 | |
437 | TopoDS_Vertex aFirstV, aLastV; |
438 | TopExp::Vertices (aWireMaker.Wire(), aFirstV, aLastV); |
439 | gp_Pnt aFirstPoint = BRep_Tool::Pnt (aFirstV); |
440 | gp_Pnt aLastPoint = BRep_Tool::Pnt (aLastV); |
441 | if (!aFirstPoint.IsEqual (aLastPoint, myPrecision)) |
442 | { |
443 | aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV)); |
444 | } |
445 | } |
446 | |
447 | if (!aWireMaker.IsDone()) |
448 | { |
449 | continue; |
450 | } |
451 | |
452 | TopoDS_Wire aWireDraft = aWireMaker.Wire(); |
453 | //if (anOrient == FT_ORIENTATION_FILL_LEFT) |
454 | //{ |
455 | // According to the TrueType specification, clockwise contours must be filled |
456 | aWireDraft.Reverse(); |
457 | //} |
458 | myBuilder.Add (aFaceDraft, aWireDraft); |
459 | } |
460 | |
461 | myFixer.Init (aFaceDraft); |
462 | myFixer.Perform(); |
463 | theShape = myFixer.Result(); |
464 | if (!theShape.IsNull() |
465 | && theShape.ShapeType() != TopAbs_FACE) |
466 | { |
467 | // shape fix can not fix orientation within the single call |
468 | TopoDS_Compound aComp; |
469 | myBuilder.MakeCompound (aComp); |
470 | for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next()) |
471 | { |
472 | TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current()); |
473 | myFixer.Init (aFace); |
474 | myFixer.Perform(); |
475 | myBuilder.Add (aComp, myFixer.Result()); |
476 | } |
477 | theShape = aComp; |
478 | } |
479 | |
480 | myCache.Bind (theChar, theShape); |
481 | return !theShape.IsNull(); |
482 | } |