Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-22 08:58:50

0001 // Created on: 2013-01-29
0002 // Created by: Kirill GAVRILOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef OpenGl_Font_HeaderFile
0017 #define OpenGl_Font_HeaderFile
0018 
0019 #include <OpenGl_Texture.hxx>
0020 #include <OpenGl_Vec.hxx>
0021 
0022 #include <Font_Rect.hxx>
0023 
0024 #include <NCollection_DataMap.hxx>
0025 #include <NCollection_DynamicArray.hxx>
0026 #include <TCollection_AsciiString.hxx>
0027 
0028 class Font_FTFont;
0029 
0030 //! Texture font.
0031 class OpenGl_Font : public OpenGl_Resource
0032 {
0033 
0034 public:
0035   //! Simple structure stores tile rectangle.
0036   struct Tile
0037   {
0038     Font_Rect uv;      //!< UV coordinates in texture
0039     Font_Rect px;      //!< pixel displacement coordinates
0040     GLuint    texture; //!< GL texture ID
0041   };
0042 
0043   struct RectI
0044   {
0045     int Left;
0046     int Right;
0047     int Top;
0048     int Bottom;
0049   };
0050 
0051 public:
0052   //! Main constructor.
0053   Standard_EXPORT OpenGl_Font(const occ::handle<Font_FTFont>& theFont,
0054                               const TCollection_AsciiString&  theKey = "");
0055 
0056   //! Destroy object.
0057   Standard_EXPORT ~OpenGl_Font() override;
0058 
0059   //! Destroy object - will release GPU memory if any
0060   Standard_EXPORT void Release(OpenGl_Context* theCtx) override;
0061 
0062   //! Returns estimated GPU memory usage.
0063   Standard_EXPORT size_t EstimatedDataSize() const override;
0064 
0065   //! @return key of shared resource
0066   inline const TCollection_AsciiString& ResourceKey() const { return myKey; }
0067 
0068   //! @return FreeType font instance specified on construction.
0069   inline const occ::handle<Font_FTFont>& FTFont() const { return myFont; }
0070 
0071   //! @return true if font was loaded successfully.
0072   inline bool IsValid() const { return !myTextures.IsEmpty() && myTextures.First()->IsValid(); }
0073 
0074   //! Notice that this method doesn't return initialization success state.
0075   //! Use IsValid() instead.
0076   //! @return true if initialization was already called.
0077   inline bool WasInitialized() const { return !myTextures.IsEmpty(); }
0078 
0079   //! Initialize GL resources.
0080   //! FreeType font instance should be already initialized!
0081   Standard_EXPORT bool Init(const occ::handle<OpenGl_Context>& theCtx);
0082 
0083   //! @return vertical distance from the horizontal baseline to the highest character coordinate
0084   inline float Ascender() const { return myAscender; }
0085 
0086   //! @return vertical distance from the horizontal baseline to the lowest character coordinate
0087   inline float Descender() const { return myDescender; }
0088 
0089   //! Render glyph to texture if not already.
0090   //! @param theCtx       active context
0091   //! @param theUChar     unicode symbol to render
0092   //! @param theGlyph     computed glyph position rectangle, texture ID and UV coordinates
0093   Standard_EXPORT bool RenderGlyph(const occ::handle<OpenGl_Context>& theCtx,
0094                                    const char32_t                     theUChar,
0095                                    Tile&                              theGlyph);
0096 
0097   //! @return first texture.
0098   const occ::handle<OpenGl_Texture>& Texture() const { return myTextures.First(); }
0099 
0100 protected:
0101   //! Render new glyph to the texture.
0102   bool renderGlyph(const occ::handle<OpenGl_Context>& theCtx, const char32_t theChar);
0103 
0104   //! Allocate new texture.
0105   bool createTexture(const occ::handle<OpenGl_Context>& theCtx);
0106 
0107 protected:
0108   TCollection_AsciiString  myKey;        //!< key of shared resource
0109   occ::handle<Font_FTFont> myFont;       //!< FreeType font instance
0110   float                    myAscender;   //!< ascender     provided my FT font
0111   float                    myDescender;  //!< descender    provided my FT font
0112   int                      myTileSizeY;  //!< tile height
0113   int                      myLastTileId; //!< id of last tile
0114   RectI                    myLastTilePx;
0115   int                      myTextureFormat; //!< texture format
0116 
0117   NCollection_DynamicArray<occ::handle<OpenGl_Texture>> myTextures; //!< array of textures
0118   NCollection_DynamicArray<Tile>                        myTiles;    //!< array of loaded tiles
0119 
0120   NCollection_DataMap<char32_t, int> myGlyphMap;
0121 
0122 public:
0123   DEFINE_STANDARD_RTTIEXT(OpenGl_Font, OpenGl_Resource) // Type definition
0124 };
0125 
0126 #endif // _OpenGl_Font_H__