1 // Created on: 2013-09-16
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <Font_BRepFont.hxx>
17 #include <BRep_Tool.hxx>
18 #include <BRepBuilderAPI_MakeFace.hxx>
19 #include <BRepBuilderAPI_MakeWire.hxx>
20 #include <BRepLib_MakeEdge.hxx>
21 #include <GCE2d_MakeSegment.hxx>
22 #include <GC_MakeSegment.hxx>
23 #include <Geom_BezierCurve.hxx>
24 #include <Geom_BSplineCurve.hxx>
25 #include <Geom2d_TrimmedCurve.hxx>
26 #include <Geom_Plane.hxx>
27 #include <Geom2d_BezierCurve.hxx>
28 #include <Geom2d_BSplineCurve.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <Geom2d_Line.hxx>
31 #include <GeomAPI.hxx>
32 #include <GeomAdaptor_HSurface.hxx>
33 #include <GeomLib.hxx>
35 #include <ShapeBuild_ReShape.hxx>
36 #include <ShapeFix_Edge.hxx>
37 #include <ShapeFix_Wire.hxx>
38 #include <TColGeom2d_HSequenceOfBoundedCurve.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <TCollection_HAsciiString.hxx>
42 #include <TopExp_Explorer.hxx>
44 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Vertex.hxx>
52 // pre-defined font rendering options
53 static const unsigned int THE_FONT_SIZE = 72;
54 static const unsigned int THE_RESOLUTION_DPI = 4800;
56 // compute scaling factor for specified font size
57 inline Standard_Real getScale (const Standard_Real theSize)
59 return theSize / Standard_Real(THE_FONT_SIZE) * 72.0 / Standard_Real(THE_RESOLUTION_DPI);
64 // =======================================================================
65 // function : Constructor
67 // =======================================================================
68 Font_BRepFont::Font_BRepFont ()
69 : myPrecision (Precision::Confusion()),
71 myIsCompositeCurve (Standard_False),
78 // =======================================================================
81 // =======================================================================
82 void Font_BRepFont::init()
84 mySurface = new Geom_Plane (gp_Pln (gp::XOY()));
85 myCurve2dAdaptor = new Geom2dAdaptor_HCurve();
86 Handle(Adaptor3d_HSurface) aSurfAdaptor = new GeomAdaptor_HSurface (mySurface);
87 myCurvOnSurf.Load (aSurfAdaptor);
89 myFixer.FixWireMode() = 1;
90 myFixer.FixOrientationMode() = 1;
91 myFixer.FixSplitFaceMode() = 1; // some glyphs might be composed from several faces
92 Handle(ShapeFix_Wire) aWireFixer = myFixer.FixWireTool();
93 aWireFixer->FixConnectedMode() = 1;
94 aWireFixer->ClosedWireMode() = Standard_True;
95 Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape();
96 myFixer.SetContext (aContext);
99 // =======================================================================
100 // function : Constructor
102 // =======================================================================
103 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontPath,
104 const Standard_Real theSize)
105 : myPrecision (Precision::Confusion()),
107 myIsCompositeCurve (Standard_False),
112 if (theSize <= myPrecision * 100.0)
117 myScaleUnits = getScale (theSize);
118 Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
121 // =======================================================================
122 // function : Constructor
124 // =======================================================================
125 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontName,
126 const Font_FontAspect theFontAspect,
127 const Standard_Real theSize)
128 : myPrecision (Precision::Confusion()),
130 myIsCompositeCurve (Standard_False),
135 if (theSize <= myPrecision * 100.0)
140 myScaleUnits = getScale (theSize);
141 Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
144 // =======================================================================
145 // function : Release
147 // =======================================================================
148 void Font_BRepFont::Release()
151 Font_FTFont::Release();
154 // =======================================================================
155 // function : SetCompositeCurveMode
157 // =======================================================================
158 void Font_BRepFont::SetCompositeCurveMode (const Standard_Boolean theToConcatenate)
160 if (myIsCompositeCurve != theToConcatenate)
162 myIsCompositeCurve = theToConcatenate;
167 // =======================================================================
170 // =======================================================================
171 bool Font_BRepFont::Init (const NCollection_String& theFontPath,
172 const Standard_Real theSize)
174 if (theSize <= myPrecision * 100.0)
179 myScaleUnits = getScale (theSize);
180 return Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
183 // =======================================================================
186 // =======================================================================
187 bool Font_BRepFont::Init (const NCollection_String& theFontName,
188 const Font_FontAspect theFontAspect,
189 const Standard_Real theSize)
191 if (theSize <= myPrecision * 100.0)
196 myScaleUnits = getScale (theSize);
197 return Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
200 // =======================================================================
201 // function : RenderGlyph
203 // =======================================================================
204 TopoDS_Shape Font_BRepFont::RenderGlyph (const Standard_Utf32Char& theChar)
207 Standard_Mutex::Sentry aSentry (myMutex);
208 renderGlyph (theChar, aShape);
212 // =======================================================================
215 // =======================================================================
216 bool Font_BRepFont::to3d (const Handle(Geom2d_Curve)& theCurve2d,
217 const GeomAbs_Shape theContinuity,
218 Handle(Geom_Curve)& theCurve3d)
220 Standard_Real aMaxDeviation = 0.0;
221 Standard_Real anAverDeviation = 0.0;
222 myCurve2dAdaptor->ChangeCurve2d().Load (theCurve2d);
223 const Handle(Adaptor2d_HCurve2d)& aCurve = myCurve2dAdaptor; // to avoid ambiguity
224 myCurvOnSurf.Load (aCurve);
225 GeomLib::BuildCurve3d (myPrecision, myCurvOnSurf,
226 myCurve2dAdaptor->FirstParameter(), myCurve2dAdaptor->LastParameter(),
227 theCurve3d, aMaxDeviation, anAverDeviation, theContinuity);
228 return !theCurve3d.IsNull();
231 // =======================================================================
232 // function : renderGlyph
234 // =======================================================================
235 Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
236 TopoDS_Shape& theShape)
239 if (!loadGlyph (theChar)
240 || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
242 return Standard_False;
244 else if (myCache.Find (theChar, theShape))
246 return !theShape.IsNull();
249 TopLoc_Location aLoc;
250 TopoDS_Face aFaceDraft;
251 myBuilder.MakeFace (aFaceDraft, mySurface, myPrecision);
252 FT_Outline& anOutline = myFTFace->glyph->outline;
253 // Get orientation is useless since it doesn't retrieve any in-font information and just computes orientation.
254 // Because it fails in some cases - leave this to ShapeFix.
255 //const FT_Orientation anOrient = FT_Outline_Get_Orientation (&anOutline);
256 for (short aContour = 0, aStartIndex = 0; aContour < anOutline.n_contours; ++aContour)
258 const FT_Vector* aPntList = &anOutline.points[aStartIndex];
259 const char* aTags = &anOutline.tags[aStartIndex];
260 const short anEndIndex = anOutline.contours[aContour];
261 const short aPntsNb = (anEndIndex - aStartIndex) + 1;
262 aStartIndex = anEndIndex + 1;
265 // closed contour can not be constructed from < 3 points
269 BRepBuilderAPI_MakeWire aWireMaker;
272 gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1]);
273 gp_XY aPntNext = readFTVec (aPntList[0]);
275 Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0;
276 gp_XY aPntLine1 = aPntCurr;
278 // see http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html
279 // for a full description of FreeType tags.
280 for (short aPntId = 0; aPntId < aPntsNb; ++aPntId)
284 aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb]);
287 if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On)
291 aPntLine1 = aPntCurr;
296 const gp_XY aDirVec = aPntCurr - aPntLine1;
297 const Standard_Real aLen = aDirVec.Modulus();
298 if (aLen <= myPrecision)
300 aPntLine1 = aPntCurr;
305 if (myIsCompositeCurve)
307 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr));
308 myConcatMaker.Add (aLine, myPrecision);
312 Handle(Geom_Curve) aCurve3d;
313 Handle(Geom2d_Line) aCurve2d = new Geom2d_Line (gp_Pnt2d (aPntLine1), gp_Dir2d (aDirVec));
314 if (to3d (aCurve2d, GeomAbs_C1, aCurve3d))
316 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen);
317 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
318 aWireMaker.Add (anEdge);
321 aPntLine1 = aPntCurr;
323 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic)
326 gp_XY aPntPrev2 = aPntPrev;
327 gp_XY aPntNext2 = aPntNext;
329 // previous point is either the real previous point (an "on" point),
330 // or the midpoint between the current one and the previous "conic off" point
331 if (FT_CURVE_TAG(aTags[(aPntId - 1 + aPntsNb) % aPntsNb]) == FT_Curve_Tag_Conic)
333 aPntPrev2 = (aPntCurr + aPntPrev) * 0.5;
336 // next point is either the real next point or the midpoint
337 if (FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Conic)
339 aPntNext2 = (aPntCurr + aPntNext) * 0.5;
342 my3Poles.SetValue (1, aPntPrev2);
343 my3Poles.SetValue (2, aPntCurr);
344 my3Poles.SetValue (3, aPntNext2);
345 Handle(Geom2d_BezierCurve) aBezierArc = new Geom2d_BezierCurve (my3Poles);
346 if (myIsCompositeCurve)
348 myConcatMaker.Add (aBezierArc, myPrecision);
352 Handle(Geom_Curve) aCurve3d;
353 if (to3d (aBezierArc, GeomAbs_C1, aCurve3d))
355 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
356 myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision);
357 aWireMaker.Add (anEdge);
361 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Cubic
362 && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic)
365 my4Poles.SetValue (1, aPntPrev);
366 my4Poles.SetValue (2, aPntCurr);
367 my4Poles.SetValue (3, aPntNext);
368 my4Poles.SetValue (4, gp_Pnt2d(readFTVec (aPntList[(aPntId + 2) % aPntsNb])));
369 Handle(Geom2d_BezierCurve) aBezier = new Geom2d_BezierCurve (my4Poles);
370 if (myIsCompositeCurve)
372 myConcatMaker.Add (aBezier, myPrecision);
376 Handle(Geom_Curve) aCurve3d;
377 if (to3d (aBezier, GeomAbs_C1, aCurve3d))
379 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
380 myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision);
381 aWireMaker.Add (anEdge);
387 if (myIsCompositeCurve)
389 Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve();
390 if (aDraft2d.IsNull())
395 const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint();
396 const gp_Pnt2d aLastPnt = aDraft2d->EndPoint();
397 if (!aFirstPnt.IsEqual (aLastPnt, myPrecision))
399 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt);
400 myConcatMaker.Add (aLine, myPrecision);
403 Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve();
404 Handle(Geom_Curve) aCurve3d;
405 if (to3d (aCurve2d, GeomAbs_C0, aCurve3d))
407 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
408 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
409 aWireMaker.Add (anEdge);
411 myConcatMaker.Clear();
415 if (!aWireMaker.IsDone())
420 TopoDS_Vertex aFirstV, aLastV;
421 TopExp::Vertices (aWireMaker.Wire(), aFirstV, aLastV);
422 gp_Pnt aFirstPoint = BRep_Tool::Pnt (aFirstV);
423 gp_Pnt aLastPoint = BRep_Tool::Pnt (aLastV);
424 if (!aFirstPoint.IsEqual (aLastPoint, myPrecision))
426 aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV));
430 if (!aWireMaker.IsDone())
435 TopoDS_Wire aWireDraft = aWireMaker.Wire();
436 //if (anOrient == FT_ORIENTATION_FILL_LEFT)
438 // According to the TrueType specification, clockwise contours must be filled
439 aWireDraft.Reverse();
441 myBuilder.Add (aFaceDraft, aWireDraft);
444 myFixer.Init (aFaceDraft);
446 theShape = myFixer.Result();
447 if (!theShape.IsNull()
448 && theShape.ShapeType() != TopAbs_FACE)
450 // shape fix can not fix orientation within the single call
451 TopoDS_Compound aComp;
452 myBuilder.MakeCompound (aComp);
453 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
455 TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current());
456 myFixer.Init (aFace);
458 myBuilder.Add (aComp, myFixer.Result());
463 myCache.Bind (theChar, theShape);
464 return !theShape.IsNull();
467 // =======================================================================
468 // function : RenderText
470 // =======================================================================
471 TopoDS_Shape Font_BRepFont::RenderText (const NCollection_String& theString)
473 if (theString.IsEmpty())
475 return TopoDS_Shape();
480 Standard_Integer aLine = 0;
481 TopoDS_Shape aGlyphShape;
482 TopoDS_Compound aResult;
483 myBuilder.MakeCompound (aResult);
484 Standard_Mutex::Sentry aSentry (myMutex);
485 for (NCollection_Utf8Iter anIter = theString.Iterator(); *anIter != 0;)
487 const Standard_Utf32Char aCharCurr = *anIter;
488 const Standard_Utf32Char aCharNext = *++anIter;
489 if (aCharCurr == '\x0D' // CR (carriage return)
490 || aCharCurr == '\a' // BEL (alarm)
491 || aCharCurr == '\f' // FF (form feed) NP (new page)
492 || aCharCurr == '\b' // BS (backspace)
493 || aCharCurr == '\v') // VT (vertical tab)
495 continue; // skip unsupported carriage control codes
497 else if (aCharCurr == ' ' || aCharCurr == '\t')
499 aPen.SetX (aPen.X() + AdvanceX (aCharCurr, aCharNext));
502 else if (aCharCurr == '\n')
506 aPen.SetY (-Standard_Real(aLine) * LineSpacing());
510 if (renderGlyph (aCharCurr, aGlyphShape))
512 aTrsf.SetTranslation (gp_Vec (aPen));
513 aGlyphShape.Move (aTrsf);
514 myBuilder.Add (aResult, aGlyphShape);
516 aPen.SetX (aPen.X() + AdvanceX (aCharCurr, aCharNext));