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_FTLibrary.hxx>
22 #include <Font_TextFormatter.hxx>
23 #include <GCE2d_MakeSegment.hxx>
24 #include <GC_MakeSegment.hxx>
25 #include <Geom_BezierCurve.hxx>
26 #include <Geom_BSplineCurve.hxx>
27 #include <Geom2d_TrimmedCurve.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom2d_BezierCurve.hxx>
30 #include <Geom2d_BSplineCurve.hxx>
31 #include <Geom2d_TrimmedCurve.hxx>
32 #include <Geom2d_Line.hxx>
33 #include <GeomAPI.hxx>
34 #include <GeomAdaptor_HSurface.hxx>
35 #include <GeomLib.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>
44 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Vertex.hxx>
50 #include FT_FREETYPE_H
53 IMPLEMENT_STANDARD_RTTIEXT(Font_BRepFont,Font_FTFont)
57 // pre-defined font rendering options
58 static const unsigned int THE_FONT_SIZE = 72;
59 static const unsigned int THE_RESOLUTION_DPI = 4800;
61 // compute scaling factor for specified font size
62 inline Standard_Real getScale (const Standard_Real theSize)
64 return theSize / Standard_Real(THE_FONT_SIZE) * 72.0 / Standard_Real(THE_RESOLUTION_DPI);
67 //! Auxiliary method to convert FT_Vector to gp_XY
68 static gp_XY readFTVec (const FT_Vector& theVec,
69 const Standard_Real theScaleUnits)
71 return gp_XY (theScaleUnits * Standard_Real(theVec.x) / 64.0, theScaleUnits * Standard_Real(theVec.y) / 64.0);
76 // =======================================================================
77 // function : Constructor
79 // =======================================================================
80 Font_BRepFont::Font_BRepFont ()
81 : myPrecision (Precision::Confusion()),
83 myIsCompositeCurve (Standard_False),
90 // =======================================================================
93 // =======================================================================
94 void Font_BRepFont::init()
96 mySurface = new Geom_Plane (gp_Pln (gp::XOY()));
97 myCurve2dAdaptor = new Geom2dAdaptor_HCurve();
98 Handle(Adaptor3d_HSurface) aSurfAdaptor = new GeomAdaptor_HSurface (mySurface);
99 myCurvOnSurf.Load (aSurfAdaptor);
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);
111 // =======================================================================
112 // function : Constructor
114 // =======================================================================
115 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontPath,
116 const Standard_Real theSize)
117 : myPrecision (Precision::Confusion()),
119 myIsCompositeCurve (Standard_False),
124 if (theSize <= myPrecision * 100.0)
129 myScaleUnits = getScale (theSize);
130 Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
133 // =======================================================================
134 // function : Constructor
136 // =======================================================================
137 Font_BRepFont::Font_BRepFont (const NCollection_String& theFontName,
138 const Font_FontAspect theFontAspect,
139 const Standard_Real theSize)
140 : myPrecision (Precision::Confusion()),
142 myIsCompositeCurve (Standard_False),
147 if (theSize <= myPrecision * 100.0)
152 myScaleUnits = getScale (theSize);
153 Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
156 // =======================================================================
157 // function : Release
159 // =======================================================================
160 void Font_BRepFont::Release()
163 Font_FTFont::Release();
166 // =======================================================================
167 // function : SetCompositeCurveMode
169 // =======================================================================
170 void Font_BRepFont::SetCompositeCurveMode (const Standard_Boolean theToConcatenate)
172 if (myIsCompositeCurve != theToConcatenate)
174 myIsCompositeCurve = theToConcatenate;
179 // =======================================================================
182 // =======================================================================
183 bool Font_BRepFont::Init (const NCollection_String& theFontPath,
184 const Standard_Real theSize)
186 if (theSize <= myPrecision * 100.0)
191 myScaleUnits = getScale (theSize);
192 return Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
195 // =======================================================================
198 // =======================================================================
199 bool Font_BRepFont::Init (const NCollection_String& theFontName,
200 const Font_FontAspect theFontAspect,
201 const Standard_Real theSize)
203 if (theSize <= myPrecision * 100.0)
208 myScaleUnits = getScale (theSize);
209 return Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
212 // =======================================================================
213 // function : RenderGlyph
215 // =======================================================================
216 TopoDS_Shape Font_BRepFont::RenderGlyph (const Standard_Utf32Char& theChar)
219 Standard_Mutex::Sentry aSentry (myMutex);
220 renderGlyph (theChar, aShape);
224 // =======================================================================
227 // =======================================================================
228 bool Font_BRepFont::to3d (const Handle(Geom2d_Curve)& theCurve2d,
229 const GeomAbs_Shape theContinuity,
230 Handle(Geom_Curve)& theCurve3d)
232 Standard_Real aMaxDeviation = 0.0;
233 Standard_Real anAverDeviation = 0.0;
234 myCurve2dAdaptor->ChangeCurve2d().Load (theCurve2d);
235 const Handle(Adaptor2d_HCurve2d)& aCurve = myCurve2dAdaptor; // to avoid ambiguity
236 myCurvOnSurf.Load (aCurve);
237 GeomLib::BuildCurve3d (myPrecision, myCurvOnSurf,
238 myCurve2dAdaptor->FirstParameter(), myCurve2dAdaptor->LastParameter(),
239 theCurve3d, aMaxDeviation, anAverDeviation, theContinuity);
240 return !theCurve3d.IsNull();
243 // =======================================================================
244 // function : renderGlyph
246 // =======================================================================
247 Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
248 TopoDS_Shape& theShape)
251 if (!loadGlyph (theChar)
252 || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
254 return Standard_False;
256 else if (myCache.Find (theChar, theShape))
258 return !theShape.IsNull();
261 FT_Outline& anOutline = myFTFace->glyph->outline;
263 if (!anOutline.n_contours)
264 return Standard_False;
266 TopLoc_Location aLoc;
267 TopoDS_Face aFaceDraft;
268 myBuilder.MakeFace (aFaceDraft, mySurface, myPrecision);
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)
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;
282 // closed contour can not be constructed from < 3 points
286 BRepBuilderAPI_MakeWire aWireMaker;
289 gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1], myScaleUnits);
290 gp_XY aPntNext = readFTVec (aPntList[0], myScaleUnits);
292 Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0;
293 gp_XY aPntLine1 = aPntCurr;
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)
301 aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb], myScaleUnits);
304 if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On)
308 aPntLine1 = aPntCurr;
313 const gp_XY aDirVec = aPntCurr - aPntLine1;
314 const Standard_Real aLen = aDirVec.Modulus();
315 if (aLen <= myPrecision)
317 aPntLine1 = aPntCurr;
322 if (myIsCompositeCurve)
324 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr));
325 myConcatMaker.Add (aLine, myPrecision);
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))
333 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen);
334 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
335 aWireMaker.Add (anEdge);
338 aPntLine1 = aPntCurr;
340 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic)
343 gp_XY aPntPrev2 = aPntPrev;
344 gp_XY aPntNext2 = aPntNext;
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)
350 aPntPrev2 = (aPntCurr + aPntPrev) * 0.5;
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)
356 aPntNext2 = (aPntCurr + aPntNext) * 0.5;
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)
365 myConcatMaker.Add (aBezierArc, myPrecision);
369 Handle(Geom_Curve) aCurve3d;
370 if (to3d (aBezierArc, GeomAbs_C1, aCurve3d))
372 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
373 myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision);
374 aWireMaker.Add (anEdge);
378 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Cubic
379 && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic)
382 my4Poles.SetValue (1, aPntPrev);
383 my4Poles.SetValue (2, aPntCurr);
384 my4Poles.SetValue (3, aPntNext);
385 my4Poles.SetValue (4, gp_Pnt2d(readFTVec (aPntList[(aPntId + 2) % aPntsNb], myScaleUnits)));
386 Handle(Geom2d_BezierCurve) aBezier = new Geom2d_BezierCurve (my4Poles);
387 if (myIsCompositeCurve)
389 myConcatMaker.Add (aBezier, myPrecision);
393 Handle(Geom_Curve) aCurve3d;
394 if (to3d (aBezier, GeomAbs_C1, aCurve3d))
396 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
397 myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision);
398 aWireMaker.Add (anEdge);
404 if (myIsCompositeCurve)
406 Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve();
407 if (aDraft2d.IsNull())
412 const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint();
413 const gp_Pnt2d aLastPnt = aDraft2d->EndPoint();
414 if (!aFirstPnt.IsEqual (aLastPnt, myPrecision))
416 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt);
417 myConcatMaker.Add (aLine, myPrecision);
420 Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve();
421 Handle(Geom_Curve) aCurve3d;
422 if (to3d (aCurve2d, GeomAbs_C0, aCurve3d))
424 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
425 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
426 aWireMaker.Add (anEdge);
428 myConcatMaker.Clear();
432 if (!aWireMaker.IsDone())
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))
443 aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV));
447 if (!aWireMaker.IsDone())
452 TopoDS_Wire aWireDraft = aWireMaker.Wire();
453 //if (anOrient == FT_ORIENTATION_FILL_LEFT)
455 // According to the TrueType specification, clockwise contours must be filled
456 aWireDraft.Reverse();
458 myBuilder.Add (aFaceDraft, aWireDraft);
461 myFixer.Init (aFaceDraft);
463 theShape = myFixer.Result();
464 if (!theShape.IsNull()
465 && theShape.ShapeType() != TopAbs_FACE)
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())
472 TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current());
473 myFixer.Init (aFace);
475 myBuilder.Add (aComp, myFixer.Result());
480 myCache.Bind (theChar, theShape);
481 return !theShape.IsNull();