File indexing completed on 2025-01-18 10:03:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef Font_TextFormatter_Header
0017 #define Font_TextFormatter_Header
0018
0019 #include <Font_Rect.hxx>
0020 #include <Graphic3d_HorizontalTextAlignment.hxx>
0021 #include <Graphic3d_VerticalTextAlignment.hxx>
0022 #include <NCollection_DataMap.hxx>
0023 #include <NCollection_Vector.hxx>
0024 #include <NCollection_String.hxx>
0025
0026 class Font_FTFont;
0027
0028 DEFINE_STANDARD_HANDLE(Font_TextFormatter, Standard_Transient)
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 class Font_TextFormatter : public Standard_Transient
0055 {
0056 public:
0057
0058 enum IterationFilter
0059 {
0060 IterationFilter_None = 0x0000,
0061 IterationFilter_ExcludeInvisible = 0x0002,
0062 };
0063
0064
0065
0066 class Iterator
0067 {
0068 public:
0069
0070 Iterator (const Font_TextFormatter& theFormatter,
0071 IterationFilter theFilter = IterationFilter_None)
0072 : myFilter (theFilter), myIter (theFormatter.myString.Iterator()), mySymbolChar (0), mySymbolCharNext (0)
0073 {
0074 mySymbolPosition = readNextSymbol (-1, mySymbolChar);
0075 mySymbolNext = readNextSymbol (mySymbolPosition, mySymbolCharNext);
0076 }
0077
0078
0079 Standard_Boolean More() const { return mySymbolPosition >= 0; }
0080
0081
0082 Standard_Boolean HasNext() const { return mySymbolNext >= 0; }
0083
0084
0085 Standard_Utf32Char Symbol() const { return mySymbolChar; }
0086
0087
0088 Standard_Utf32Char SymbolNext() const { return mySymbolCharNext; }
0089
0090
0091 Standard_Integer SymbolPosition() const { return mySymbolPosition; }
0092
0093
0094 Standard_Integer SymbolPositionNext() const { return mySymbolNext; }
0095
0096
0097 void Next()
0098 {
0099 mySymbolPosition = mySymbolNext;
0100 mySymbolChar = mySymbolCharNext;
0101 mySymbolNext = readNextSymbol (mySymbolPosition, mySymbolCharNext);
0102 }
0103
0104 protected:
0105
0106 Standard_Integer readNextSymbol (const Standard_Integer theSymbolStartingFrom,
0107 Standard_Utf32Char& theSymbolChar)
0108 {
0109 Standard_Integer aNextSymbol = theSymbolStartingFrom;
0110 for (; *myIter != 0; ++myIter)
0111 {
0112 const Standard_Utf32Char aCharCurr = *myIter;
0113 if (Font_TextFormatter::IsCommandSymbol (aCharCurr))
0114 {
0115 continue;
0116 }
0117 aNextSymbol++;
0118 if ((myFilter & IterationFilter_ExcludeInvisible) != 0)
0119 {
0120 if (aCharCurr == '\x0A'||
0121 aCharCurr == ' ' ||
0122 aCharCurr == '\t')
0123 {
0124 continue;
0125 }
0126 }
0127 ++myIter;
0128 theSymbolChar = aCharCurr;
0129 return aNextSymbol;
0130 }
0131 return -1;
0132 }
0133
0134 protected:
0135 IterationFilter myFilter;
0136 NCollection_Utf8Iter myIter;
0137 Standard_Integer mySymbolPosition;
0138 Standard_Utf32Char mySymbolChar;
0139 Standard_Integer mySymbolNext;
0140 Standard_Utf32Char mySymbolCharNext;
0141 };
0142
0143
0144 Standard_EXPORT Font_TextFormatter();
0145
0146
0147 Standard_EXPORT void SetupAlignment (const Graphic3d_HorizontalTextAlignment theAlignX,
0148 const Graphic3d_VerticalTextAlignment theAlignY);
0149
0150
0151 Standard_EXPORT void Reset();
0152
0153
0154 Standard_EXPORT void Append (const NCollection_String& theString,
0155 Font_FTFont& theFont);
0156
0157
0158
0159 Standard_EXPORT void Format();
0160
0161 Standard_DEPRECATED("BottomLeft should be used instead")
0162 const NCollection_Vec2<Standard_ShortReal>& TopLeft (const Standard_Integer theIndex) const
0163 {
0164 return BottomLeft (theIndex);
0165 }
0166
0167
0168 const NCollection_Vec2<Standard_ShortReal>& BottomLeft (const Standard_Integer theIndex) const
0169 { return myCorners.Value (theIndex); }
0170
0171
0172 inline const NCollection_String& String() const
0173 {
0174 return myString;
0175 }
0176
0177
0178
0179 Standard_EXPORT Standard_Boolean GlyphBoundingBox (const Standard_Integer theIndex,
0180 Font_Rect& theBndBox) const;
0181
0182
0183
0184 Standard_ShortReal LineHeight (const Standard_Integer theIndex) const
0185 { return theIndex == 0 ? myAscender : myLineSpacing; }
0186
0187
0188 Standard_EXPORT Standard_ShortReal LineWidth (const Standard_Integer theIndex) const;
0189
0190
0191 Standard_EXPORT Standard_Boolean IsLFSymbol (const Standard_Integer theIndex) const;
0192
0193
0194 Standard_EXPORT Standard_ShortReal FirstPosition() const;
0195
0196
0197 Standard_EXPORT Standard_Integer LinePositionIndex (const Standard_Integer theIndex) const;
0198
0199
0200 Standard_EXPORT Standard_Integer LineIndex (const Standard_Integer theIndex) const;
0201
0202
0203 inline Standard_Integer TabSize() const
0204 {
0205 return myTabSize;
0206 }
0207
0208
0209 Graphic3d_HorizontalTextAlignment HorizontalTextAlignment() const { return myAlignX; }
0210
0211
0212 Graphic3d_VerticalTextAlignment VerticalTextAlignment() const { return myAlignY; }
0213
0214
0215 void SetWrapping (const Standard_ShortReal theWidth) { myWrappingWidth = theWidth; }
0216
0217
0218 Standard_Boolean HasWrapping() const { return myWrappingWidth > 0; }
0219
0220
0221 Standard_ShortReal Wrapping() const { return myWrappingWidth; }
0222
0223
0224 Standard_Boolean WordWrapping () const { return myIsWordWrapping; }
0225
0226
0227 void SetWordWrapping (const Standard_Boolean theIsWordWrapping) { myIsWordWrapping = theIsWordWrapping; }
0228
0229
0230 inline Standard_ShortReal ResultWidth() const
0231 {
0232 return myBndWidth;
0233 }
0234
0235
0236 inline Standard_ShortReal ResultHeight() const
0237 {
0238 return myLineSpacing * Standard_ShortReal(myLinesNb);
0239 }
0240
0241
0242 Standard_ShortReal MaximumSymbolWidth() const { return myMaxSymbolWidth; }
0243
0244
0245 inline void BndBox (Font_Rect& theBndBox) const
0246 {
0247 theBndBox.Left = 0.0f;
0248 switch (myAlignX)
0249 {
0250 default:
0251 case Graphic3d_HTA_LEFT: theBndBox.Right = myBndWidth; break;
0252 case Graphic3d_HTA_RIGHT: theBndBox.Right = -myBndWidth; break;
0253 case Graphic3d_HTA_CENTER:
0254 {
0255 theBndBox.Left = -0.5f * myBndWidth;
0256 theBndBox.Right = 0.5f * myBndWidth;
0257 break;
0258 }
0259 }
0260 theBndBox.Top = myBndTop;
0261 theBndBox.Bottom = theBndBox.Top - myLineSpacing * Standard_ShortReal(myLinesNb);
0262 }
0263
0264
0265 const NCollection_Vector < NCollection_Vec2<Standard_ShortReal> >& Corners() const { return myCorners; }
0266
0267
0268 const NCollection_Vector<Standard_ShortReal>& NewLines() const { return myNewLines; }
0269
0270
0271 static inline Standard_Boolean IsCommandSymbol (const Standard_Utf32Char& theSymbol)
0272 {
0273 if (theSymbol == '\x0D'
0274 || theSymbol == '\a'
0275 || theSymbol == '\f'
0276 || theSymbol == '\b'
0277 || theSymbol == '\v')
0278 return Standard_True;
0279
0280 return Standard_False;
0281 }
0282
0283
0284 static Standard_Boolean IsSeparatorSymbol (const Standard_Utf32Char& theSymbol)
0285 {
0286 return theSymbol == '\x0A'
0287 || theSymbol == ' '
0288 || theSymbol == '\x09';
0289 }
0290
0291 DEFINE_STANDARD_RTTIEXT (Font_TextFormatter, Standard_Transient)
0292
0293 protected:
0294
0295
0296 Standard_EXPORT void newLine (const Standard_Integer theLastRect,
0297 const Standard_ShortReal theMaxLineWidth);
0298
0299 protected:
0300
0301 Graphic3d_HorizontalTextAlignment myAlignX;
0302 Graphic3d_VerticalTextAlignment myAlignY;
0303 Standard_Integer myTabSize;
0304 Standard_ShortReal myWrappingWidth;
0305 Standard_Boolean myIsWordWrapping;
0306 Standard_ShortReal myLastSymbolWidth;
0307 Standard_ShortReal myMaxSymbolWidth;
0308
0309 protected:
0310
0311 NCollection_String myString;
0312 NCollection_Vec2<Standard_ShortReal>
0313 myPen;
0314 NCollection_Vector < NCollection_Vec2<Standard_ShortReal> >
0315 myCorners;
0316 NCollection_Vector<Standard_ShortReal>
0317 myNewLines;
0318 Standard_ShortReal myLineSpacing;
0319 Standard_ShortReal myAscender;
0320 bool myIsFormatted;
0321
0322 protected:
0323
0324 Standard_Integer myLinesNb;
0325 Standard_Integer myRectLineStart;
0326 Standard_Integer myNewLineNb;
0327
0328 Standard_ShortReal myPenCurrLine;
0329 Standard_ShortReal myBndTop;
0330 Standard_ShortReal myBndWidth;
0331 NCollection_Vec2<Standard_ShortReal>
0332 myMoveVec;
0333 };
0334
0335 #endif