0024272: Provide basic text formatting routines for Font_BRepFont
[occt.git] / src / Font / Font_BRepFont.cxx
CommitLineData
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>
ac84fcf6 21#include <Font_TextFormatter.hxx>
b514beda 22#include <GCE2d_MakeSegment.hxx>
23#include <GC_MakeSegment.hxx>
24#include <Geom_BezierCurve.hxx>
25#include <Geom_BSplineCurve.hxx>
c04c30b3 26#include <Geom2d_TrimmedCurve.hxx>
b514beda 27#include <Geom_Plane.hxx>
28#include <Geom2d_BezierCurve.hxx>
29#include <Geom2d_BSplineCurve.hxx>
543a9964 30#include <Geom2d_TrimmedCurve.hxx>
b514beda 31#include <Geom2d_Line.hxx>
32#include <GeomAPI.hxx>
33#include <GeomAdaptor_HSurface.hxx>
34#include <GeomLib.hxx>
35#include <gp_Pln.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>
42#include <TopExp.hxx>
43#include <TopExp_Explorer.hxx>
44#include <TopoDS.hxx>
45#include <TopoDS_Compound.hxx>
46#include <TopoDS_Vertex.hxx>
47
48#include FT_OUTLINE_H
49
b514beda 50
51namespace
52{
53 // pre-defined font rendering options
54 static const unsigned int THE_FONT_SIZE = 72;
55 static const unsigned int THE_RESOLUTION_DPI = 4800;
56
57 // compute scaling factor for specified font size
58 inline Standard_Real getScale (const Standard_Real theSize)
59 {
60 return theSize / Standard_Real(THE_FONT_SIZE) * 72.0 / Standard_Real(THE_RESOLUTION_DPI);
61 }
62
63};
64
65// =======================================================================
66// function : Constructor
67// purpose :
68// =======================================================================
69Font_BRepFont::Font_BRepFont ()
70: myPrecision (Precision::Confusion()),
71 myScaleUnits (1.0),
72 myIsCompositeCurve (Standard_False),
73 my3Poles (1, 3),
74 my4Poles (1, 4)
75{
76 init();
77}
78
79// =======================================================================
80// function : init
81// purpose :
82// =======================================================================
83void Font_BRepFont::init()
84{
85 mySurface = new Geom_Plane (gp_Pln (gp::XOY()));
86 myCurve2dAdaptor = new Geom2dAdaptor_HCurve();
543a9964 87 Handle(Adaptor3d_HSurface) aSurfAdaptor = new GeomAdaptor_HSurface (mySurface);
b514beda 88 myCurvOnSurf.Load (aSurfAdaptor);
89
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);
98}
99
100// =======================================================================
101// function : Constructor
102// purpose :
103// =======================================================================
104Font_BRepFont::Font_BRepFont (const NCollection_String& theFontPath,
105 const Standard_Real theSize)
106: myPrecision (Precision::Confusion()),
107 myScaleUnits (1.0),
108 myIsCompositeCurve (Standard_False),
109 my3Poles (1, 3),
110 my4Poles (1, 4)
111{
112 init();
113 if (theSize <= myPrecision * 100.0)
114 {
115 return;
116 }
117
118 myScaleUnits = getScale (theSize);
119 Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
120}
121
122// =======================================================================
123// function : Constructor
124// purpose :
125// =======================================================================
126Font_BRepFont::Font_BRepFont (const NCollection_String& theFontName,
127 const Font_FontAspect theFontAspect,
128 const Standard_Real theSize)
129: myPrecision (Precision::Confusion()),
130 myScaleUnits (1.0),
131 myIsCompositeCurve (Standard_False),
132 my3Poles (1, 3),
133 my4Poles (1, 4)
134{
135 init();
136 if (theSize <= myPrecision * 100.0)
137 {
138 return;
139 }
140
141 myScaleUnits = getScale (theSize);
142 Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
143}
144
145// =======================================================================
146// function : Release
147// purpose :
148// =======================================================================
149void Font_BRepFont::Release()
150{
151 myCache.Clear();
152 Font_FTFont::Release();
153}
154
155// =======================================================================
156// function : SetCompositeCurveMode
157// purpose :
158// =======================================================================
159void Font_BRepFont::SetCompositeCurveMode (const Standard_Boolean theToConcatenate)
160{
161 if (myIsCompositeCurve != theToConcatenate)
162 {
163 myIsCompositeCurve = theToConcatenate;
164 myCache.Clear();
165 }
166}
167
168// =======================================================================
169// function : Init
170// purpose :
171// =======================================================================
172bool Font_BRepFont::Init (const NCollection_String& theFontPath,
173 const Standard_Real theSize)
174{
175 if (theSize <= myPrecision * 100.0)
176 {
177 return false;
178 }
179
180 myScaleUnits = getScale (theSize);
181 return Font_FTFont::Init (theFontPath, THE_FONT_SIZE, THE_RESOLUTION_DPI);
182}
183
184// =======================================================================
185// function : Init
186// purpose :
187// =======================================================================
188bool Font_BRepFont::Init (const NCollection_String& theFontName,
189 const Font_FontAspect theFontAspect,
190 const Standard_Real theSize)
191{
192 if (theSize <= myPrecision * 100.0)
193 {
194 return false;
195 }
196
197 myScaleUnits = getScale (theSize);
198 return Font_FTFont::Init (theFontName, theFontAspect, THE_FONT_SIZE, THE_RESOLUTION_DPI);
199}
200
201// =======================================================================
202// function : RenderGlyph
203// purpose :
204// =======================================================================
205TopoDS_Shape Font_BRepFont::RenderGlyph (const Standard_Utf32Char& theChar)
206{
207 TopoDS_Shape aShape;
208 Standard_Mutex::Sentry aSentry (myMutex);
209 renderGlyph (theChar, aShape);
210 return aShape;
211}
212
213// =======================================================================
214// function : to3d
215// purpose :
216// =======================================================================
543a9964 217bool Font_BRepFont::to3d (const Handle(Geom2d_Curve)& theCurve2d,
b514beda 218 const GeomAbs_Shape theContinuity,
219 Handle(Geom_Curve)& theCurve3d)
220{
221 Standard_Real aMaxDeviation = 0.0;
222 Standard_Real anAverDeviation = 0.0;
223 myCurve2dAdaptor->ChangeCurve2d().Load (theCurve2d);
543a9964 224 const Handle(Adaptor2d_HCurve2d)& aCurve = myCurve2dAdaptor; // to avoid ambiguity
225 myCurvOnSurf.Load (aCurve);
b514beda 226 GeomLib::BuildCurve3d (myPrecision, myCurvOnSurf,
227 myCurve2dAdaptor->FirstParameter(), myCurve2dAdaptor->LastParameter(),
228 theCurve3d, aMaxDeviation, anAverDeviation, theContinuity);
229 return !theCurve3d.IsNull();
230}
231
232// =======================================================================
233// function : renderGlyph
234// purpose :
235// =======================================================================
236Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
237 TopoDS_Shape& theShape)
238{
239 theShape.Nullify();
240 if (!loadGlyph (theChar)
241 || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
242 {
243 return Standard_False;
244 }
245 else if (myCache.Find (theChar, theShape))
246 {
247 return !theShape.IsNull();
248 }
249
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)
258 {
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;
264 if (aPntsNb < 3)
265 {
266 // closed contour can not be constructed from < 3 points
267 continue;
268 }
269
270 BRepBuilderAPI_MakeWire aWireMaker;
271
272 gp_XY aPntPrev;
273 gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1]);
274 gp_XY aPntNext = readFTVec (aPntList[0]);
275
276 Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0;
277 gp_XY aPntLine1 = aPntCurr;
278
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)
282 {
283 aPntPrev = aPntCurr;
284 aPntCurr = aPntNext;
285 aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb]);
286
287 // process tags
288 if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On)
289 {
290 if (aLinePnts < 1)
291 {
292 aPntLine1 = aPntCurr;
293 aLinePnts = 1;
294 continue;
295 }
296
297 const gp_XY aDirVec = aPntCurr - aPntLine1;
298 const Standard_Real aLen = aDirVec.Modulus();
299 if (aLen <= myPrecision)
300 {
301 aPntLine1 = aPntCurr;
302 aLinePnts = 1;
303 continue;
304 }
305
306 if (myIsCompositeCurve)
307 {
308 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr));
309 myConcatMaker.Add (aLine, myPrecision);
310 }
311 else
312 {
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))
316 {
317 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen);
318 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
319 aWireMaker.Add (anEdge);
320 }
321 }
322 aPntLine1 = aPntCurr;
323 }
324 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic)
325 {
326 aLinePnts = 0;
327 gp_XY aPntPrev2 = aPntPrev;
328 gp_XY aPntNext2 = aPntNext;
329
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)
333 {
334 aPntPrev2 = (aPntCurr + aPntPrev) * 0.5;
335 }
336
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)
339 {
340 aPntNext2 = (aPntCurr + aPntNext) * 0.5;
341 }
342
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)
348 {
349 myConcatMaker.Add (aBezierArc, myPrecision);
350 }
351 else
352 {
353 Handle(Geom_Curve) aCurve3d;
354 if (to3d (aBezierArc, GeomAbs_C1, aCurve3d))
355 {
356 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
357 myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision);
358 aWireMaker.Add (anEdge);
359 }
360 }
361 }
362 else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Cubic
363 && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic)
364 {
365 aLinePnts = 0;
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)
372 {
373 myConcatMaker.Add (aBezier, myPrecision);
374 }
375 else
376 {
377 Handle(Geom_Curve) aCurve3d;
378 if (to3d (aBezier, GeomAbs_C1, aCurve3d))
379 {
380 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
381 myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision);
382 aWireMaker.Add (anEdge);
383 }
384 }
385 }
386 }
387
388 if (myIsCompositeCurve)
389 {
390 Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve();
391 if (aDraft2d.IsNull())
392 {
393 continue;
394 }
395
396 const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint();
397 const gp_Pnt2d aLastPnt = aDraft2d->EndPoint();
398 if (!aFirstPnt.IsEqual (aLastPnt, myPrecision))
399 {
400 Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt);
401 myConcatMaker.Add (aLine, myPrecision);
402 }
403
404 Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve();
405 Handle(Geom_Curve) aCurve3d;
406 if (to3d (aCurve2d, GeomAbs_C0, aCurve3d))
407 {
408 TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
409 myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
410 aWireMaker.Add (anEdge);
411 }
412 myConcatMaker.Clear();
413 }
414 else
415 {
416 if (!aWireMaker.IsDone())
417 {
418 continue;
419 }
420
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))
426 {
427 aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV));
428 }
429 }
430
431 if (!aWireMaker.IsDone())
432 {
433 continue;
434 }
435
436 TopoDS_Wire aWireDraft = aWireMaker.Wire();
437 //if (anOrient == FT_ORIENTATION_FILL_LEFT)
438 //{
439 // According to the TrueType specification, clockwise contours must be filled
440 aWireDraft.Reverse();
441 //}
442 myBuilder.Add (aFaceDraft, aWireDraft);
443 }
444
445 myFixer.Init (aFaceDraft);
446 myFixer.Perform();
447 theShape = myFixer.Result();
448 if (!theShape.IsNull()
449 && theShape.ShapeType() != TopAbs_FACE)
450 {
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())
455 {
456 TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current());
457 myFixer.Init (aFace);
458 myFixer.Perform();
459 myBuilder.Add (aComp, myFixer.Result());
460 }
461 theShape = aComp;
462 }
463
464 myCache.Bind (theChar, theShape);
465 return !theShape.IsNull();
466}