Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:21

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_Vector.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 
0036   //! Simple structure stores tile rectangle.
0037   struct Tile
0038   {
0039     Font_Rect uv;      //!< UV coordinates in texture
0040     Font_Rect px;      //!< pixel displacement coordinates
0041     GLuint    texture; //!< GL texture ID
0042   };
0043 
0044   struct RectI
0045   {
0046     Standard_Integer Left;
0047     Standard_Integer Right;
0048     Standard_Integer Top;
0049     Standard_Integer Bottom;
0050   };
0051 
0052 public:
0053 
0054   //! Main constructor.
0055   Standard_EXPORT OpenGl_Font (const Handle(Font_FTFont)&     theFont,
0056                                const TCollection_AsciiString& theKey = "");
0057 
0058   //! Destroy object.
0059   Standard_EXPORT virtual ~OpenGl_Font();
0060 
0061   //! Destroy object - will release GPU memory if any
0062   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
0063 
0064   //! Returns estimated GPU memory usage.
0065   Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
0066 
0067   //! @return key of shared resource
0068   inline const TCollection_AsciiString& ResourceKey() const
0069   {
0070     return myKey;
0071   }
0072 
0073   //! @return FreeType font instance specified on construction.
0074   inline const Handle(Font_FTFont)& FTFont() const
0075   {
0076     return myFont;
0077   }
0078 
0079   //! @return true if font was loaded successfully.
0080   inline bool IsValid() const
0081   {
0082     return !myTextures.IsEmpty() && myTextures.First()->IsValid();
0083   }
0084 
0085   //! Notice that this method doesn't return initialization success state.
0086   //! Use IsValid() instead.
0087   //! @return true if initialization was already called.
0088   inline bool WasInitialized() const
0089   {
0090     return !myTextures.IsEmpty();
0091   }
0092 
0093   //! Initialize GL resources.
0094   //! FreeType font instance should be already initialized!
0095   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx);
0096 
0097   //! @return vertical distance from the horizontal baseline to the highest character coordinate
0098   inline float Ascender() const
0099   {
0100     return myAscender;
0101   }
0102 
0103   //! @return vertical distance from the horizontal baseline to the lowest character coordinate
0104   inline float Descender() const
0105   {
0106     return myDescender;
0107   }
0108 
0109   //! Render glyph to texture if not already.
0110   //! @param theCtx       active context
0111   //! @param theUChar     unicode symbol to render
0112   //! @param theGlyph     computed glyph position rectangle, texture ID and UV coordinates
0113   Standard_EXPORT bool RenderGlyph (const Handle(OpenGl_Context)& theCtx,
0114                                     const Standard_Utf32Char      theUChar,
0115                                     Tile&                         theGlyph);
0116 
0117   //! @return first texture.
0118   const Handle(OpenGl_Texture)& Texture() const
0119   {
0120     return myTextures.First();
0121   }
0122 
0123 protected:
0124 
0125   //! Render new glyph to the texture.
0126   bool renderGlyph (const Handle(OpenGl_Context)& theCtx,
0127                     const Standard_Utf32Char      theChar);
0128 
0129   //! Allocate new texture.
0130   bool createTexture (const Handle(OpenGl_Context)& theCtx);
0131 
0132 protected:
0133 
0134   TCollection_AsciiString myKey;           //!< key of shared resource
0135   Handle(Font_FTFont)     myFont;          //!< FreeType font instance
0136   Standard_ShortReal      myAscender;      //!< ascender     provided my FT font
0137   Standard_ShortReal      myDescender;     //!< descender    provided my FT font
0138   Standard_Integer        myTileSizeY;     //!< tile height
0139   Standard_Integer        myLastTileId;    //!< id of last tile
0140   RectI                   myLastTilePx;
0141   Standard_Integer        myTextureFormat; //!< texture format
0142 
0143   NCollection_Vector<Handle(OpenGl_Texture)> myTextures; //!< array of textures
0144   NCollection_Vector<Tile>                   myTiles;    //!< array of loaded tiles
0145 
0146   NCollection_DataMap<Standard_Utf32Char, Standard_Integer> myGlyphMap;
0147 
0148 public:
0149 
0150   DEFINE_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource) // Type definition
0151 
0152 };
0153 
0154 DEFINE_STANDARD_HANDLE(OpenGl_Font, OpenGl_Resource)
0155 
0156 #endif // _OpenGl_Font_H__