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 <Font_TextFormatter.hxx>
22 #include <GCE2d_MakeSegment.hxx>
23 #include <GC_MakeSegment.hxx>
24 #include <Geom_BezierCurve.hxx>
25 #include <Geom_BSplineCurve.hxx>
26 #include <Geom2d_TrimmedCurve.hxx>
27 #include <Geom_Plane.hxx>
28 #include <Geom2d_BezierCurve.hxx>
29 #include <Geom2d_BSplineCurve.hxx>
30 #include <Geom2d_TrimmedCurve.hxx>
31 #include <Geom2d_Line.hxx>
32 #include <GeomAPI.hxx>
33 #include <GeomAdaptor_HSurface.hxx>
34 #include <GeomLib.hxx>
36 #include <ShapeBuild_ReShape.hxx>
37 #include <ShapeFix_Edge.hxx>
38 #include <ShapeFix_Wire.hxx>
39 #include <TColGeom2d_HSequenceOfBoundedCurve.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TCollection_HAsciiString.hxx>
43 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <TopoDS_Vertex.hxx>
53 // pre-defined font rendering options
54 static const unsigned int THE_FONT_SIZE = 72;
55 static const unsigned int THE_RESOLUTION_DPI = 4800;
57 // compute scaling factor for specified font size
58 inline Standard_Real getScale (const Standard_Real theSize)
60 return theSize / Standard_Real(THE_FONT_SIZE) * 72.0 / Standard_Real(THE_RESOLUTION_DPI);
65 // =======================================================================
66 // function : Constructor
68 // =======================================================================
69 Font_BRepFont::Font_BRepFont ()
70 : myPrecision (Precision::Confusion()),
72 myIsCompositeCurve (Standard_False),
79 // =======================================================================
82 // =======================================================================
83 void Font_BRepFont::init()
85 mySurface = new Geom_Plane (gp_Pln (gp::XOY()));
86 myCurve2dAdaptor = new Geom2dAdaptor_HCurve();
87 Handle(Adaptor3d_HSurface) aSurfAdaptor = new GeomAdaptor_HSurface (mySurface);
88 myCurvOnSurf.Load (aSurfAdaptor);
90 myFixer.FixWireMode() = 1;
91 myFixer.FixOrientationMode() = 1;
92 myFixer.FixSplitFaceMode() = 1; // some glyphs might be composed from several faces
93 Handle(ShapeFix_Wire) aWireFixer = myFixer.FixWireTool();
94 aWireFixer->FixConnectedMode() = 1;
95 aWireFixer->ClosedWireMode() = Standard_True;
96 Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape();
97 myFixer.SetContext (aContext);
100 // =======================================================================
101 // function : Constructor
103 // =======================================================================
104 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontPath,
105 const Standard_Real theSize)
106 : myPrecision (Precision::Confusion()),
108 myIsCompositeCurve (Standard_False),
113 if (theSize <= myPrecision * 100.0)
118 myScaleUnits = getScale (theSize);
119 Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
122 // =======================================================================
123 // function : Constructor
125 // =======================================================================
126 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontName,
127 const Font_FontAspect theFontAspect,
128 const Standard_Real theSize)
129 : myPrecision (Precision::Confusion()),
131 myIsCompositeCurve (Standard_False),
136 if (theSize <= myPrecision * 100.0)
141 myScaleUnits = getScale (theSize);
142 Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
145 // =======================================================================
146 // function : Release
148 // =======================================================================
149 void Font_BRepFont::Release()
152 Font_FTFont::Release();
155 // =======================================================================
156 // function : SetCompositeCurveMode
158 // =======================================================================
159 void Font_BRepFont::SetCompositeCurveMode (const Standard_Boolean theToConcatenate)
161 if (myIsCompositeCurve != theToConcatenate)
163 myIsCompositeCurve = theToConcatenate;
168 // =======================================================================
171 // =======================================================================
172 bool Font_BRepFont::Init (const NCollection_String& theFontPath,
173 const Standard_Real theSize)
175 if (theSize <= myPrecision * 100.0)
180 myScaleUnits = getScale (theSize);
181 return Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
184 // =======================================================================
187 // =======================================================================
188 bool Font_BRepFont::Init (const NCollection_String& theFontName,
189 const Font_FontAspect theFontAspect,
190 const Standard_Real theSize)
192 if (theSize <= myPrecision * 100.0)
197 myScaleUnits = getScale (theSize);
198 return Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
201 // =======================================================================
202 // function : RenderGlyph
204 // =======================================================================
205 TopoDS_Shape Font_BRepFont::RenderGlyph (const Standard_Utf32Char& theChar)
208 Standard_Mutex::Sentry aSentry (myMutex);
209 renderGlyph (theChar, aShape);
213 // =======================================================================
216 // =======================================================================
217 bool Font_BRepFont::to3d (const Handle(Geom2d_Curve)& theCurve2d,
218 const GeomAbs_Shape theContinuity,
219 Handle(Geom_Curve)& theCurve3d)
221 Standard_Real aMaxDeviation = 0.0;
222 Standard_Real anAverDeviation = 0.0;
223 myCurve2dAdaptor->ChangeCurve2d().Load (theCurve2d);
224 const Handle(Adaptor2d_HCurve2d)& aCurve = myCurve2dAdaptor; // to avoid ambiguity
225 myCurvOnSurf.Load (aCurve);
226 GeomLib::BuildCurve3d (myPrecision, myCurvOnSurf,
227 myCurve2dAdaptor->FirstParameter(), myCurve2dAdaptor->LastParameter(),
228 theCurve3d, aMaxDeviation, anAverDeviation, theContinuity);
229 return !theCurve3d.IsNull();
232 // =======================================================================
233 // function : renderGlyph
235 // =======================================================================
236 Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
237 TopoDS_Shape& theShape)
240 if (!loadGlyph (theChar)
241 || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
243 return Standard_False;
245 else if (myCache.Find (theChar, theShape))
247 return !theShape.IsNull();
250 TopLoc_Location aLoc;
251 TopoDS_Face aFaceDraft;
252 myBuilder.MakeFace (aFaceDraft, mySurface, myPrecision);
253 FT_Outline& anOutline = myFTFace->glyph->outline;
254 // Get orientation is useless since it doesn't retrieve any in-font information and just computes orientation.
255 // Because it fails in some cases - leave this to ShapeFix.
256 //const FT_Orientation anOrient = FT_Outline_Get_Orientation (&anOutline);
257 for (short aContour = 0, aStartIndex = 0; aContour < anOutline.n_contours; ++aContour)
259 const FT_Vector* aPntList = &anOutline.points[aStartIndex];
260 const char* aTags = &anOutline.tags[aStartIndex];
261 const short anEndIndex = anOutline.contours[aContour];
262 const short aPntsNb = (anEndIndex - aStartIndex) + 1;
263 aStartIndex = anEndIndex + 1;
266 // closed contour can not be constructed from < 3 points
270 BRepBuilderAPI_MakeWire aWireMaker;
273 gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1]);
274 gp_XY aPntNext = readFTVec (aPntList[0]);
276 Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0;
277 gp_XY aPntLine1 = aPntCurr;
279 // see http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html
280 // for a full description of FreeType tags.
281 for (short aPntId = 0; aPntId < aPntsNb; ++aPntId)
285 aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb]);
288 if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On)
292 aPntLine1 = aPntCurr;
297 const gp_XY aDirVec = aPntCurr - aPntLine1;
298 const Standard_Real aLen = aDirVec.Modulus();
299 if (aLen <= myPrecision)
301 aPntLine1 = aPntCurr;
306 if (myIsCompositeCurve)
308 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr));
309 myConcatMaker.Add (aLine, myPrecision);
313 Handle(Geom_Curve) aCurve3d;
314 Handle(Geom2d_Line) aCurve2d = new Geom2d_Line (gp_Pnt2d (aPntLine1), gp_Dir2d (aDirVec));
315 if (to3d (aCurve2d, GeomAbs_C1, aCurve3d))
317 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen);
318 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
319 aWireMaker.Add (anEdge);
322 aPntLine1 = aPntCurr;
324 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic)
327 gp_XY aPntPrev2 = aPntPrev;
328 gp_XY aPntNext2 = aPntNext;
330 // previous point is either the real previous point (an "on" point),
331 // or the midpoint between the current one and the previous "conic off" point
332 if (FT_CURVE_TAG(aTags[(aPntId - 1 + aPntsNb) % aPntsNb]) == FT_Curve_Tag_Conic)
334 aPntPrev2 = (aPntCurr + aPntPrev) * 0.5;
337 // next point is either the real next point or the midpoint
338 if (FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Conic)
340 aPntNext2 = (aPntCurr + aPntNext) * 0.5;
343 my3Poles.SetValue (1, aPntPrev2);
344 my3Poles.SetValue (2, aPntCurr);
345 my3Poles.SetValue (3, aPntNext2);
346 Handle(Geom2d_BezierCurve) aBezierArc = new Geom2d_BezierCurve (my3Poles);
347 if (myIsCompositeCurve)
349 myConcatMaker.Add (aBezierArc, myPrecision);
353 Handle(Geom_Curve) aCurve3d;
354 if (to3d (aBezierArc, GeomAbs_C1, aCurve3d))
356 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
357 myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision);
358 aWireMaker.Add (anEdge);
362 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Cubic
363 && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic)
366 my4Poles.SetValue (1, aPntPrev);
367 my4Poles.SetValue (2, aPntCurr);
368 my4Poles.SetValue (3, aPntNext);
369 my4Poles.SetValue (4, gp_Pnt2d(readFTVec (aPntList[(aPntId + 2) % aPntsNb])));
370 Handle(Geom2d_BezierCurve) aBezier = new Geom2d_BezierCurve (my4Poles);
371 if (myIsCompositeCurve)
373 myConcatMaker.Add (aBezier, myPrecision);
377 Handle(Geom_Curve) aCurve3d;
378 if (to3d (aBezier, GeomAbs_C1, aCurve3d))
380 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
381 myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision);
382 aWireMaker.Add (anEdge);
388 if (myIsCompositeCurve)
390 Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve();
391 if (aDraft2d.IsNull())
396 const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint();
397 const gp_Pnt2d aLastPnt = aDraft2d->EndPoint();
398 if (!aFirstPnt.IsEqual (aLastPnt, myPrecision))
400 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt);
401 myConcatMaker.Add (aLine, myPrecision);
404 Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve();
405 Handle(Geom_Curve) aCurve3d;
406 if (to3d (aCurve2d, GeomAbs_C0, aCurve3d))
408 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
409 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
410 aWireMaker.Add (anEdge);
412 myConcatMaker.Clear();
416 if (!aWireMaker.IsDone())
421 TopoDS_Vertex aFirstV, aLastV;
422 TopExp::Vertices (aWireMaker.Wire(), aFirstV, aLastV);
423 gp_Pnt aFirstPoint = BRep_Tool::Pnt (aFirstV);
424 gp_Pnt aLastPoint = BRep_Tool::Pnt (aLastV);
425 if (!aFirstPoint.IsEqual (aLastPoint, myPrecision))
427 aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV));
431 if (!aWireMaker.IsDone())
436 TopoDS_Wire aWireDraft = aWireMaker.Wire();
437 //if (anOrient == FT_ORIENTATION_FILL_LEFT)
439 // According to the TrueType specification, clockwise contours must be filled
440 aWireDraft.Reverse();
442 myBuilder.Add (aFaceDraft, aWireDraft);
445 myFixer.Init (aFaceDraft);
447 theShape = myFixer.Result();
448 if (!theShape.IsNull()
449 && theShape.ShapeType() != TopAbs_FACE)
451 // shape fix can not fix orientation within the single call
452 TopoDS_Compound aComp;
453 myBuilder.MakeCompound (aComp);
454 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
456 TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current());
457 myFixer.Init (aFace);
459 myBuilder.Add (aComp, myFixer.Result());
464 myCache.Bind (theChar, theShape);
465 return !theShape.IsNull();