1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0024272: Provide basic text formatting routines for Font_BRepFont

Add new class Font_BRepTextBuilder for generation of formatted BRep text.
Delete method Font_BRepFont::RenderText(), which should be replaced by Font_BRepTextBuilder::Perform().
This commit is contained in:
isk
2015-08-24 14:10:22 +03:00
committed by ski
parent 2eea6525af
commit ac84fcf602
22 changed files with 487 additions and 149 deletions

View File

@@ -1,5 +1,7 @@
Font_BRepFont.cxx
Font_BRepFont.hxx
Font_BRepTextBuilder.cxx
Font_BRepTextBuilder.hxx
Font_FontAspect.hxx
Font_FontMgr.cxx
Font_FontMgr.hxx

View File

@@ -18,6 +18,7 @@
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepLib_MakeEdge.hxx>
#include <Font_TextFormatter.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <GC_MakeSegment.hxx>
#include <Geom_BezierCurve.hxx>
@@ -463,57 +464,3 @@ Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
myCache.Bind (theChar, theShape);
return !theShape.IsNull();
}
// =======================================================================
// function : RenderText
// purpose :
// =======================================================================
TopoDS_Shape Font_BRepFont::RenderText (const NCollection_String& theString)
{
if (theString.IsEmpty())
{
return TopoDS_Shape();
}
gp_Trsf aTrsf;
gp_XYZ aPen;
Standard_Integer aLine = 0;
TopoDS_Shape aGlyphShape;
TopoDS_Compound aResult;
myBuilder.MakeCompound (aResult);
Standard_Mutex::Sentry aSentry (myMutex);
for (NCollection_Utf8Iter anIter = theString.Iterator(); *anIter != 0;)
{
const Standard_Utf32Char aCharCurr = *anIter;
const Standard_Utf32Char aCharNext = *++anIter;
if (aCharCurr == '\x0D' // CR (carriage return)
|| aCharCurr == '\a' // BEL (alarm)
|| aCharCurr == '\f' // FF (form feed) NP (new page)
|| aCharCurr == '\b' // BS (backspace)
|| aCharCurr == '\v') // VT (vertical tab)
{
continue; // skip unsupported carriage control codes
}
else if (aCharCurr == ' ' || aCharCurr == '\t')
{
aPen.SetX (aPen.X() + AdvanceX (aCharCurr, aCharNext));
continue;
}
else if (aCharCurr == '\n')
{
++aLine;
aPen.SetX (0.0);
aPen.SetY (-Standard_Real(aLine) * LineSpacing());
continue;
}
if (renderGlyph (aCharCurr, aGlyphShape))
{
aTrsf.SetTranslation (gp_Vec (aPen));
aGlyphShape.Move (aTrsf);
myBuilder.Add (aResult, aGlyphShape);
}
aPen.SetX (aPen.X() + AdvanceX (aCharCurr, aCharNext));
}
return aResult;
}

View File

@@ -18,6 +18,7 @@
#include <Adaptor3d_CurveOnSurface.hxx>
#include <BRep_Builder.hxx>
#include <Font_FTFont.hxx>
#include <Font_TextFormatter.hxx>
#include <Geom2dAdaptor_HCurve.hxx>
#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
#include <gp_Ax3.hxx>
@@ -95,27 +96,6 @@ public:
//! Notice that altering this flag clears currently accumulated cache!
Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate);
public:
//! Render text as BRep shape.
//! @param theString text in UTF-8 encoding
//! @return result shape within XOY plane and start position (0,0,0) on the baseline
Standard_EXPORT TopoDS_Shape RenderText (const NCollection_String& theString);
//! Render text as BRep shape.
//! @param theString text in UTF-8 encoding
//! @param thePenLoc start position and orientation on the baseline
//! @return result shape with pen transformation applied as shape location
TopoDS_Shape RenderText (const NCollection_String& theString,
const gp_Ax3& thePenLoc)
{
TopoDS_Shape aResult = RenderText (theString);
gp_Trsf aTrsf;
aTrsf.SetTransformation (thePenLoc, gp_Ax3 (gp::XOY()));
aResult.Move (aTrsf);
return aResult;
}
public:
//! @return vertical distance from the horizontal baseline to the highest character coordinate.
@@ -172,6 +152,18 @@ public:
return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUChar, theUCharNext));
}
//! Returns scaling factor for current font size.
Standard_Real Scale() const
{
return myScaleUnits;
}
//! Returns mutex.
Standard_Mutex& Mutex()
{
return myMutex;
}
protected:
//! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
@@ -205,7 +197,7 @@ protected: //! @name Protected fields
Handle(Geom_Surface) mySurface; //!< surface to place glyphs on to
Standard_Real myPrecision; //!< algorithm precision
Standard_Real myScaleUnits; //!< scale font rendering units into model units
Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, ON by default
Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
protected: //! @name Shared temporary variables for glyph construction

View File

@@ -0,0 +1,89 @@
// Created on: 2015-08-10
// Created by: Ilya SEVRIKOV
// Copyright (c) 2013-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <Font_BRepTextBuilder.hxx>
// =======================================================================
// Function : Perfrom
// Purpose :
// =======================================================================
TopoDS_Shape Font_BRepTextBuilder::Perform (Font_BRepFont& theFont,
const Font_TextFormatter& theFormatter,
const gp_Ax3& thePenLoc)
{
gp_Trsf aTrsf;
gp_XYZ aPen;
TopoDS_Shape aGlyphShape;
TopoDS_Compound aResult;
Standard_Mutex::Sentry aSentry (theFont.Mutex());
myBuilder.MakeCompound (aResult);
Standard_Integer aSymbolCounter = 0;
Standard_Real aScaleUnits = theFont.Scale();
for (NCollection_Utf8Iter anIter = theFormatter.String().Iterator(); *anIter != 0; ++anIter)
{
const Standard_Utf32Char aCharCurr = *anIter;
if (aCharCurr == '\x0D' // CR (carriage return)
|| aCharCurr == '\a' // BEL (alarm)
|| aCharCurr == '\f' // FF (form feed) NP (new page)
|| aCharCurr == '\b' // BS (backspace)
|| aCharCurr == '\v' // VT (vertical tab)
|| aCharCurr == ' '
|| aCharCurr == '\t'
|| aCharCurr == '\n')
{
continue; // skip unsupported carriage control codes
}
const NCollection_Vec2<Standard_ShortReal>& aCorner = theFormatter.TopLeft (aSymbolCounter);
aPen.SetCoord (aCorner.x() * aScaleUnits, aCorner.y() * aScaleUnits, 0.0);
aGlyphShape = theFont.RenderGlyph (aCharCurr);
if (!aGlyphShape.IsNull())
{
aTrsf.SetTranslation (gp_Vec (aPen));
aGlyphShape.Move (aTrsf);
myBuilder.Add (aResult, aGlyphShape);
}
++aSymbolCounter;
}
aTrsf.SetTransformation (thePenLoc, gp_Ax3 (gp::XOY()));
aResult.Move (aTrsf);
return aResult;
}
// =======================================================================
// Function : Perform
// Purpose :
// =======================================================================
TopoDS_Shape Font_BRepTextBuilder::Perform (Font_BRepFont& theFont,
const NCollection_String& theString,
const gp_Ax3& thePenLoc,
const Graphic3d_HorizontalTextAlignment theHAlign,
const Graphic3d_VerticalTextAlignment theVAlign)
{
Font_TextFormatter aFormatter;
aFormatter.Reset();
aFormatter.SetupAlignment (theHAlign, theVAlign);
aFormatter.Append (theString, *(reinterpret_cast<Font_FTFont*> (&theFont)));
aFormatter.Format();
return Perform (theFont, aFormatter, thePenLoc);
}

View File

@@ -0,0 +1,51 @@
// Created on: 2015-08-10
// Created by: Ilya SEVRIKOV
// Copyright (c) 2013-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef Font_BRepTextBuilder_Header
#define Font_BRepTextBuilder_Header
#include <Font_BRepFont.hxx>
#include <Font_TextFormatter.hxx>
#include <gp_Ax3.hxx>
//! Represents class for applying text formatting.
class Font_BRepTextBuilder
{
public:
//! Render text as BRep shape.
//! @param theString text in UTF-8 encoding
//! @param thePenLoc start position and orientation on the baseline
//! @param theFormatter formatter which defines alignment for the text
//! @return result shape with pen transformation applied as shape location
Standard_EXPORT TopoDS_Shape Perform (Font_BRepFont& theFont,
const Font_TextFormatter& theFormatter,
const gp_Ax3& thePenLoc = gp_Ax3());
//! Render text as BRep shape.
//! @param theString text in UTF-8 encoding
//! @param thePenLoc start position and orientation on the baseline
//! @param theHAlign horizontal alignment of the text
//! @param theVAlign vertical alignment of the text
//! @return result shape with pen transformation applied as shape location
Standard_EXPORT TopoDS_Shape Perform (Font_BRepFont& theFont,
const NCollection_String& theString,
const gp_Ax3& thePenLoc = gp_Ax3(),
const Graphic3d_HorizontalTextAlignment theHAlign = Graphic3d_HTA_LEFT,
const Graphic3d_VerticalTextAlignment theVAlign = Graphic3d_VTA_BOTTOM);
protected:
BRep_Builder myBuilder;
};
#endif // Font_BRepTextBuilder_Header

View File

@@ -298,11 +298,18 @@ void Font_TextFormatter::Format()
if (myAlignY == Graphic3d_VTA_BOTTOM)
{
myBndTop = -myLineSpacing - myPenCurrLine;
moveY (myCorners, myBndTop, 0, myRectsNb - 1);
}
else if (myAlignY == Graphic3d_VTA_CENTER)
{
myBndTop = 0.5f * (myLineSpacing * Standard_ShortReal(myLinesNb));
}
else if (myAlignY == Graphic3d_VTA_TOPFIRSTLINE)
{
myBndTop = myAscender;
}
if (myAlignY != Graphic3d_VTA_TOP)
{
moveY (myCorners, myBndTop, 0, myRectsNb - 1);
}
}