File indexing completed on 2026-06-27 08:39:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef Font_FTFont_HeaderFile
0017 #define Font_FTFont_HeaderFile
0018
0019 #include <Font_FontAspect.hxx>
0020 #include <Font_Hinting.hxx>
0021 #include <Font_Rect.hxx>
0022 #include <Font_StrictLevel.hxx>
0023 #include <Font_UnicodeSubset.hxx>
0024 #include <Graphic3d_HorizontalTextAlignment.hxx>
0025 #include <Graphic3d_VerticalTextAlignment.hxx>
0026 #include <Image_PixMap.hxx>
0027 #include <NCollection_String.hxx>
0028 #include <TCollection_AsciiString.hxx>
0029
0030
0031 typedef struct FT_FaceRec_* FT_Face;
0032 typedef struct FT_Vector_ FT_Vector;
0033 typedef struct FT_Outline_ FT_Outline;
0034 class Font_FTLibrary;
0035
0036
0037 struct Font_FTFontParams
0038 {
0039 unsigned int PointSize;
0040
0041 unsigned int Resolution;
0042 Font_Hinting FontHinting;
0043
0044
0045 bool ToSynthesizeItalic;
0046
0047 bool IsSingleStrokeFont;
0048
0049
0050 Font_FTFontParams()
0051 : PointSize(0),
0052 Resolution(72u),
0053 FontHinting(Font_Hinting_Off),
0054 ToSynthesizeItalic(false),
0055 IsSingleStrokeFont(false)
0056 {
0057 }
0058
0059
0060 Font_FTFontParams(unsigned int thePointSize, unsigned int theResolution)
0061 : PointSize(thePointSize),
0062 Resolution(theResolution),
0063 FontHinting(Font_Hinting_Off),
0064 ToSynthesizeItalic(false),
0065 IsSingleStrokeFont(false)
0066 {
0067 }
0068 };
0069
0070 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
0071
0072
0073
0074
0075 class Font_FTFont : public Standard_Transient
0076 {
0077 DEFINE_STANDARD_RTTIEXT(Font_FTFont, Standard_Transient)
0078 public:
0079
0080
0081
0082
0083
0084
0085 Standard_EXPORT static Handle(Font_FTFont) FindAndCreate(
0086 const TCollection_AsciiString& theFontName,
0087 const Font_FontAspect theFontAspect,
0088 const Font_FTFontParams& theParams,
0089 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
0090
0091
0092 static bool IsCharFromCJK(Standard_Utf32Char theUChar)
0093 {
0094 return (theUChar >= 0x03400 && theUChar <= 0x04DFF)
0095 || (theUChar >= 0x04E00 && theUChar <= 0x09FFF)
0096 || (theUChar >= 0x0F900 && theUChar <= 0x0FAFF)
0097 || (theUChar >= 0x20000 && theUChar <= 0x2A6DF)
0098 || (theUChar >= 0x2F800 && theUChar <= 0x2FA1F)
0099
0100
0101 || IsCharFromHiragana(theUChar) || IsCharFromKatakana(theUChar);
0102 }
0103
0104
0105 static bool IsCharFromHiragana(Standard_Utf32Char theUChar)
0106 {
0107 return (theUChar >= 0x03040 && theUChar <= 0x0309F);
0108 }
0109
0110
0111 static bool IsCharFromKatakana(Standard_Utf32Char theUChar)
0112 {
0113 return (theUChar >= 0x030A0 && theUChar <= 0x030FF);
0114 }
0115
0116
0117 static bool IsCharFromKorean(Standard_Utf32Char theUChar)
0118 {
0119 return (theUChar >= 0x01100 && theUChar <= 0x011FF)
0120 || (theUChar >= 0x03130 && theUChar <= 0x0318F)
0121 || (theUChar >= 0x0AC00 && theUChar <= 0x0D7A3);
0122 }
0123
0124
0125 static bool IsCharFromArabic(Standard_Utf32Char theUChar)
0126 {
0127 return (theUChar >= 0x00600 && theUChar <= 0x006FF);
0128 }
0129
0130
0131 static bool IsCharRightToLeft(Standard_Utf32Char theUChar) { return IsCharFromArabic(theUChar); }
0132
0133
0134 static Font_UnicodeSubset CharSubset(Standard_Utf32Char theUChar)
0135 {
0136 if (IsCharFromCJK(theUChar))
0137 {
0138 return Font_UnicodeSubset_CJK;
0139 }
0140 else if (IsCharFromKorean(theUChar))
0141 {
0142 return Font_UnicodeSubset_Korean;
0143 }
0144 else if (IsCharFromArabic(theUChar))
0145 {
0146 return Font_UnicodeSubset_Arabic;
0147 }
0148 return Font_UnicodeSubset_Western;
0149 }
0150
0151 public:
0152
0153 Standard_EXPORT Font_FTFont(const Handle(Font_FTLibrary)& theFTLib = Handle(Font_FTLibrary)());
0154
0155
0156 Standard_EXPORT virtual ~Font_FTFont();
0157
0158
0159 inline bool IsValid() const { return myFTFace != NULL; }
0160
0161
0162 inline const Image_PixMap& GlyphImage() const { return myGlyphImg; }
0163
0164
0165
0166
0167
0168
0169 bool Init(const TCollection_AsciiString& theFontPath,
0170 const Font_FTFontParams& theParams,
0171 const Standard_Integer theFaceId = 0)
0172 {
0173 return Init(Handle(NCollection_Buffer)(), theFontPath, theParams, theFaceId);
0174 }
0175
0176
0177
0178
0179
0180
0181
0182
0183 Standard_EXPORT bool Init(const Handle(NCollection_Buffer)& theData,
0184 const TCollection_AsciiString& theFileName,
0185 const Font_FTFontParams& theParams,
0186 const Standard_Integer theFaceId = 0);
0187
0188
0189
0190
0191
0192
0193
0194 Standard_EXPORT bool FindAndInit(const TCollection_AsciiString& theFontName,
0195 Font_FontAspect theFontAspect,
0196 const Font_FTFontParams& theParams,
0197 Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
0198
0199
0200
0201
0202 Standard_Boolean ToUseUnicodeSubsetFallback() const { return myToUseUnicodeSubsetFallback; }
0203
0204
0205
0206 void SetUseUnicodeSubsetFallback(Standard_Boolean theToFallback)
0207 {
0208 myToUseUnicodeSubsetFallback = theToFallback;
0209 }
0210
0211
0212
0213
0214 bool IsSingleStrokeFont() const { return myFontParams.IsSingleStrokeFont; }
0215
0216
0217 void SetSingleStrokeFont(bool theIsSingleLine)
0218 {
0219 myFontParams.IsSingleStrokeFont = theIsSingleLine;
0220 }
0221
0222
0223 bool ToSynthesizeItalic() const { return myFontParams.ToSynthesizeItalic; }
0224
0225
0226 Standard_EXPORT virtual void Release();
0227
0228
0229 Standard_EXPORT bool RenderGlyph(const Standard_Utf32Char theChar);
0230
0231
0232 Standard_EXPORT unsigned int GlyphMaxSizeX(bool theToIncludeFallback = false) const;
0233
0234
0235 Standard_EXPORT unsigned int GlyphMaxSizeY(bool theToIncludeFallback = false) const;
0236
0237
0238 Standard_EXPORT float Ascender() const;
0239
0240
0241 Standard_EXPORT float Descender() const;
0242
0243
0244 Standard_EXPORT float LineSpacing() const;
0245
0246
0247 unsigned int PointSize() const { return myFontParams.PointSize; }
0248
0249
0250 float WidthScaling() const { return myWidthScaling; }
0251
0252
0253
0254 void SetWidthScaling(const float theScaleFactor) { myWidthScaling = theScaleFactor; }
0255
0256
0257 Standard_EXPORT bool HasSymbol(Standard_Utf32Char theUChar) const;
0258
0259
0260
0261
0262 Standard_EXPORT float AdvanceX(Standard_Utf32Char theUCharNext) const;
0263
0264
0265
0266
0267
0268 Standard_EXPORT float AdvanceX(Standard_Utf32Char theUChar, Standard_Utf32Char theUCharNext);
0269
0270
0271
0272
0273 Standard_EXPORT float AdvanceY(Standard_Utf32Char theUCharNext) const;
0274
0275
0276
0277
0278
0279 Standard_EXPORT float AdvanceY(Standard_Utf32Char theUChar, Standard_Utf32Char theUCharNext);
0280
0281
0282
0283 Standard_EXPORT Standard_Integer GlyphsNumber(bool theToIncludeFallback = false) const;
0284
0285
0286 Standard_EXPORT void GlyphRect(Font_Rect& theRect) const;
0287
0288
0289
0290
0291
0292 Standard_EXPORT Font_Rect BoundingBox(const NCollection_String& theString,
0293 const Graphic3d_HorizontalTextAlignment theAlignX,
0294 const Graphic3d_VerticalTextAlignment theAlignY);
0295
0296 public:
0297
0298
0299
0300
0301 Standard_EXPORT const FT_Outline* renderGlyphOutline(const Standard_Utf32Char theChar);
0302
0303 public:
0304
0305
0306
0307
0308
0309 Standard_DEPRECATED("Deprecated method, Font_FTFontParams should be used for passing "
0310 "parameters")
0311
0312 bool Init(const NCollection_String& theFontPath,
0313 unsigned int thePointSize,
0314 unsigned int theResolution)
0315 {
0316 Font_FTFontParams aParams;
0317 aParams.PointSize = thePointSize;
0318 aParams.Resolution = theResolution;
0319 return Init(theFontPath.ToCString(), aParams, 0);
0320 }
0321
0322
0323
0324
0325
0326
0327
0328 Standard_DEPRECATED("Deprecated method, Font_FTFontParams should be used for passing "
0329 "parameters")
0330
0331 bool Init(const NCollection_String& theFontName,
0332 Font_FontAspect theFontAspect,
0333 unsigned int thePointSize,
0334 unsigned int theResolution)
0335 {
0336 Font_FTFontParams aParams;
0337 aParams.PointSize = thePointSize;
0338 aParams.Resolution = theResolution;
0339 return FindAndInit(theFontName.ToCString(), theFontAspect, aParams);
0340 }
0341
0342 protected:
0343
0344 template <typename theInput_t>
0345 int32_t toFTPoints(const theInput_t thePointSize) const
0346 {
0347 return (int32_t)thePointSize * 64;
0348 }
0349
0350
0351 template <typename theReturn_t, typename theFTUnits_t>
0352 inline theReturn_t fromFTPoints(const theFTUnits_t theFTUnits) const
0353 {
0354 return (theReturn_t)theFTUnits / 64.0f;
0355 }
0356
0357 protected:
0358
0359 Standard_EXPORT bool loadGlyph(const Standard_Utf32Char theUChar);
0360
0361
0362 Standard_EXPORT bool getKerning(FT_Vector& theKern,
0363 Standard_Utf32Char theUCharCurr,
0364 Standard_Utf32Char theUCharNext) const;
0365
0366
0367 Standard_EXPORT bool findAndInitFallback(Font_UnicodeSubset theSubset);
0368
0369
0370 void setLoadFlag(int32_t theFlag, bool theToEnable)
0371 {
0372 if (theToEnable)
0373 {
0374 myLoadFlags |= theFlag;
0375 }
0376 else
0377 {
0378 myLoadFlags &= ~theFlag;
0379 }
0380 }
0381
0382 protected:
0383 Handle(Font_FTLibrary) myFTLib;
0384 Handle(NCollection_Buffer) myBuffer;
0385 Handle(Font_FTFont) myFallbackFaces[Font_UnicodeSubset_NB];
0386 FT_Face myFTFace;
0387 FT_Face myActiveFTFace;
0388 TCollection_AsciiString myFontPath;
0389 Font_FTFontParams myFontParams;
0390 Font_FontAspect myFontAspect;
0391 float myWidthScaling;
0392 int32_t myLoadFlags;
0393
0394 Image_PixMap myGlyphImg;
0395 Standard_Utf32Char myUChar;
0396
0397 Standard_Boolean myToUseUnicodeSubsetFallback;
0398
0399 };
0400
0401 #endif