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

0023457: Slow text rendering

Added class Font_FTFont wrapper over FreeType face

Unify collections methods NCollection_Array1, NCollection_Sequence, NCollection_Vector:
declare Upper, Lower, First, Last, ChangeFirst, ChangeLast methods for all these collections.

Added method NCollection_DataMap::Find() with check key is bound + retrieve value within single call interface.

OpenGl_Context::ReleaseResource() method now supports lazy release of shared resources.

Added class OpenGl_Font which implements textured fonts support.
Added class OpenGl_TextFormatter for text formatting using OpenGl_Font.

OpenGl_Text was redesigned to use OpenGl_FontFormatter.

OpenGl_FontMgr class was removed.
All methods related to text rendered removed from OpenGl_Display class.

OpenGl_Trihedron and OpenGl_GraduatedTrihedron classes were redesigned
to use OpenGl_Text.

OpenGl_PrinterContext instance was moved to OpenGl_GraphicDriver fields
(eliminated usage of global instance).

Added test cases into 3rdparty/fonts grid to check different font styles
and perform FPS tests (no automated results - requires manual analysis
or snapshots comparisons).

Removed unused CSF_FTGL dependency.
OpenGl_Text::setupMatrix - do not apply floor for myWinZ
This commit is contained in:
kgv
2013-02-08 15:05:16 +04:00
parent 163ef25006
commit a174a3c54f
72 changed files with 5397 additions and 2606 deletions

View File

@@ -1,3 +1,7 @@
EXTERNLIB
Font_FTFont.hxx
Font_FTFont.cxx
Font_FTLibrary.hxx
Font_FTLibrary.cxx
Font_NListOfSystemFont.hxx
Font_NameOfFont.hxx

View File

@@ -18,8 +18,8 @@
package Font
uses Standard ,
Quantity ,
uses Standard,
Quantity,
TCollection,
OSD,
TColStd
@@ -31,7 +31,9 @@ is
class SystemFont;
imported NListOfSystemFont;
imported FTFont;
imported FTLibrary;
class FontMgr;
end Font;
end Font;

243
src/Font/Font_FTFont.cxx Normal file
View File

@@ -0,0 +1,243 @@
// Created on: 2013-01-28
// Created by: Kirill GAVRILOV
// Copyright (c) 2013 OPEN CASCADE SAS
//
// The content of this file is subject to the Open CASCADE Technology Public
// License Version 6.5 (the "License"). You may not use the content of this file
// except in compliance with the License. Please obtain a copy of the License
// at http://www.opencascade.org and read it completely before using this file.
//
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
//
// The Original Code and all software distributed under the License is
// distributed on an "AS IS" basis, without warranty of any kind, and the
// Initial Developer hereby disclaims all such warranties, including without
// limitation, any warranties of merchantability, fitness for a particular
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#include <Font_FTFont.hxx>
IMPLEMENT_STANDARD_HANDLE (Font_FTFont, Standard_Transient)
IMPLEMENT_STANDARD_RTTIEXT(Font_FTFont, Standard_Transient)
// =======================================================================
// function : Font_FTFont
// purpose :
// =======================================================================
Font_FTFont::Font_FTFont (const Handle(Font_FTLibrary)& theFTLib)
: myFTLib (theFTLib),
myFTFace (NULL),
myPointSize (0),
myUChar (0)
{
if (myFTLib.IsNull())
{
myFTLib = new Font_FTLibrary();
}
}
// =======================================================================
// function : Font_FTFont
// purpose :
// =======================================================================
Font_FTFont::~Font_FTFont()
{
Release();
}
// =======================================================================
// function : Font_FTFont
// purpose :
// =======================================================================
void Font_FTFont::Release()
{
myGlyphImg.Clear();
myFontPath.Clear();
myUChar = 0;
if (myFTFace != NULL)
{
FT_Done_Face (myFTFace);
myFTFace = NULL;
}
}
// =======================================================================
// function : Font_FTFont
// purpose :
// =======================================================================
bool Font_FTFont::Init (const NCollection_String& theFontPath,
const unsigned int thePointSize,
const unsigned int theResolution)
{
Release();
myFontPath = theFontPath;
myPointSize = thePointSize;
if (!myFTLib->IsValid())
{
std::cerr << "FreeType library is unavailable!\n";
Release();
return false;
}
if (FT_New_Face (myFTLib->Instance(), myFontPath.ToCString(), 0, &myFTFace) != 0)
{
//std::cerr << "Font '" << myFontPath << "' fail to load!\n";
Release();
return false;
}
else if (FT_Select_Charmap (myFTFace, ft_encoding_unicode) != 0)
{
//std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
Release();
return false;
}
else if (FT_Set_Char_Size (myFTFace, 0L, toFTPoints (thePointSize), theResolution, theResolution) != 0)
{
//std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
Release();
return false;
}
return true;
}
// =======================================================================
// function : loadGlyph
// purpose :
// =======================================================================
bool Font_FTFont::loadGlyph (const Standard_Utf32Char theUChar)
{
if (myUChar == theUChar)
{
return true;
}
myGlyphImg.Clear();
myUChar = 0;
if (theUChar == 0
|| FT_Load_Char (myFTFace, theUChar, FT_LOAD_TARGET_NORMAL) != 0
|| myFTFace->glyph == NULL)
{
return false;
}
myUChar = theUChar;
return true;
}
// =======================================================================
// function : RenderGlyph
// purpose :
// =======================================================================
bool Font_FTFont::RenderGlyph (const Standard_Utf32Char theUChar)
{
myGlyphImg.Clear();
myUChar = 0;
if (theUChar == 0
|| FT_Load_Char (myFTFace, theUChar, FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL) != 0
|| myFTFace->glyph == NULL
|| myFTFace->glyph->format != FT_GLYPH_FORMAT_BITMAP)
{
return false;
}
FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
if (aBitmap.pixel_mode != FT_PIXEL_MODE_GRAY
|| aBitmap.buffer == NULL || aBitmap.width <= 0 || aBitmap.rows <= 0)
{
return false;
}
if (!myGlyphImg.InitWrapper (Image_PixMap::ImgGray, aBitmap.buffer,
aBitmap.width, aBitmap.rows, Abs (aBitmap.pitch)))
{
return false;
}
myGlyphImg.SetTopDown (aBitmap.pitch > 0);
myUChar = theUChar;
return true;
}
// =======================================================================
// function : GlyphMaxSizeX
// purpose :
// =======================================================================
unsigned int Font_FTFont::GlyphMaxSizeX() const
{
float aWidth = (FT_IS_SCALABLE(myFTFace) != 0)
? float(myFTFace->bbox.xMax - myFTFace->bbox.xMin) * (float(myFTFace->size->metrics.x_ppem) / float(myFTFace->units_per_EM))
: fromFTPoints<float> (myFTFace->size->metrics.max_advance);
return (unsigned int)(aWidth + 0.5f);
}
// =======================================================================
// function : GlyphMaxSizeY
// purpose :
// =======================================================================
unsigned int Font_FTFont::GlyphMaxSizeY() const
{
float aHeight = (FT_IS_SCALABLE(myFTFace) != 0)
? float(myFTFace->bbox.yMax - myFTFace->bbox.yMin) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM))
: fromFTPoints<float> (myFTFace->size->metrics.height);
return (unsigned int)(aHeight + 0.5f);
}
// =======================================================================
// function : AdvanceX
// purpose :
// =======================================================================
float Font_FTFont::AdvanceX (const Standard_Utf32Char theUChar,
const Standard_Utf32Char theUCharNext)
{
loadGlyph (theUChar);
return AdvanceX (theUCharNext);
}
// =======================================================================
// function : AdvanceY
// purpose :
// =======================================================================
float Font_FTFont::AdvanceY (const Standard_Utf32Char theUChar,
const Standard_Utf32Char theUCharNext)
{
loadGlyph (theUChar);
return AdvanceY (theUCharNext);
}
// =======================================================================
// function : AdvanceX
// purpose :
// =======================================================================
float Font_FTFont::AdvanceX (const Standard_Utf32Char theUCharNext)
{
if (myUChar == 0)
{
return 0.0f;
}
if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
|| FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
{
return fromFTPoints<float> (myFTFace->glyph->advance.x);
}
return fromFTPoints<float> (myKernAdvance.x + myFTFace->glyph->advance.x);
}
// =======================================================================
// function : AdvanceY
// purpose :
// =======================================================================
float Font_FTFont::AdvanceY (const Standard_Utf32Char theUCharNext)
{
if (myUChar == 0)
{
return 0.0f;
}
if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
|| FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
{
return fromFTPoints<float> (myFTFace->glyph->advance.y);
}
return fromFTPoints<float> (myKernAdvance.y + myFTFace->glyph->advance.y);
}

213
src/Font/Font_FTFont.hxx Normal file
View File

@@ -0,0 +1,213 @@
// Created on: 2013-01-28
// Created by: Kirill GAVRILOV
// Copyright (c) 2013 OPEN CASCADE SAS
//
// The content of this file is subject to the Open CASCADE Technology Public
// License Version 6.5 (the "License"). You may not use the content of this file
// except in compliance with the License. Please obtain a copy of the License
// at http://www.opencascade.org and read it completely before using this file.
//
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
//
// The Original Code and all software distributed under the License is
// distributed on an "AS IS" basis, without warranty of any kind, and the
// Initial Developer hereby disclaims all such warranties, including without
// limitation, any warranties of merchantability, fitness for a particular
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#ifndef _Font_FTFont_H__
#define _Font_FTFont_H__
#include <NCollection_Vec2.hxx>
#include <NCollection_String.hxx>
#include <Font_FTLibrary.hxx>
#include <Image_PixMap.hxx>
//! Wrapper over FreeType font.
//! Notice that this class uses internal buffers for loaded glyphs
//! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
class Font_FTFont : public Standard_Transient
{
public:
//! Auxiliary structure - rectangle definition
struct Rect
{
float Left;
float Right;
float Top;
float Bottom;
NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
{
theVec.x() = Left;
theVec.y() = Top;
return theVec;
}
NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
{
theVec.x() = Right;
theVec.y() = Top;
return theVec;
}
NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
{
theVec.x() = Left;
theVec.y() = Bottom;
return theVec;
}
NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
{
theVec.x() = Right;
theVec.y() = Bottom;
return theVec;
}
};
public:
//! Create uninitialized instance.
Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
//! Destructor.
Standard_EXPORT virtual ~Font_FTFont();
//! @return true if font is loaded
inline bool IsValid() const
{
return myFTFace != NULL;
}
//! @return image plane for currently rendered glyph
inline const Image_PixMap& GlyphImage() const
{
return myGlyphImg;
}
//! Initialize the font.
//! @param theFontPath path to the font
//! @param thePointSize the face size in points (1/72 inch)
//! @param theResolution the resolution of the target device in dpi
//! @return true on success
Standard_EXPORT bool Init (const NCollection_String& theFontPath,
const unsigned int thePointSize,
const unsigned int theResolution = 72);
//! Release currently loaded font.
Standard_EXPORT void Release();
//! Render specified glyph into internal buffer (bitmap).
Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
//! @return maximal glyph width in pixels (rendered to bitmap).
Standard_EXPORT unsigned int GlyphMaxSizeX() const;
//! @return maximal glyph height in pixels (rendered to bitmap).
Standard_EXPORT unsigned int GlyphMaxSizeY() const;
//! @return vertical distance from the horizontal baseline to the highest character coordinate.
inline float Ascender() const
{
return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
}
//! @return vertical distance from the horizontal baseline to the lowest character coordinate.
inline float Descender() const
{
return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
}
//! @return default line spacing (the baseline-to-baseline distance).
inline float LineSpacing() const
{
return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
}
//! Configured point size
unsigned int PointSize() const
{
return myPointSize;
}
//! Compute advance to the next character with kerning applied when applicable.
//! Assuming text rendered horizontally.
Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
//! Compute advance to the next character with kerning applied when applicable.
//! Assuming text rendered horizontally.
Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUChar,
const Standard_Utf32Char theUCharNext);
//! Compute advance to the next character with kerning applied when applicable.
//! Assuming text rendered vertically.
Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUCharNext);
//! Compute advance to the next character with kerning applied when applicable.
//! Assuming text rendered vertically.
Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUChar,
const Standard_Utf32Char theUCharNext);
//! @return glyphs number in this font.
inline Standard_Integer GlyphsNumber() const
{
return myFTFace->num_glyphs;
}
//! Retrieve glyph bitmap rectangle
inline void GlyphRect (Font_FTFont::Rect& theRect) const
{
FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
theRect.Left = float(myFTFace->glyph->bitmap_left);
theRect.Top = float(myFTFace->glyph->bitmap_top);
theRect.Right = float(myFTFace->glyph->bitmap_left + aBitmap.width);
theRect.Bottom = float(myFTFace->glyph->bitmap_top - aBitmap.rows);
}
protected:
//! Convert value to 26.6 fixed-point format for FT library API.
template <typename theInput_t>
inline FT_F26Dot6 toFTPoints (const theInput_t thePointSize) const
{
return (FT_F26Dot6)thePointSize * 64;
}
//! Convert value from 26.6 fixed-point format for FT library API.
template <typename theReturn_t, typename theFTUnits_t>
inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
{
return (theReturn_t)theFTUnits / 64.0f;
}
protected:
//! Load glyph without rendering it.
Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
protected:
Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object
FT_Face myFTFace; //!< FT face object
NCollection_String myFontPath; //!< font path
unsigned int myPointSize; //!< point size set by FT_Set_Char_Size
Image_PixMap myGlyphImg; //!< cached glyph plane
FT_Vector myKernAdvance; //!< buffer variable
Standard_Utf32Char myUChar; //!< currently loaded unicode character
public:
DEFINE_STANDARD_RTTI(Font_FTFont) // Type definition
};
DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
#endif // _Font_FTFont_H__

View File

@@ -0,0 +1,48 @@
// Created on: 2013-01-28
// Created by: Kirill GAVRILOV
// Copyright (c) 2013 OPEN CASCADE SAS
//
// The content of this file is subject to the Open CASCADE Technology Public
// License Version 6.5 (the "License"). You may not use the content of this file
// except in compliance with the License. Please obtain a copy of the License
// at http://www.opencascade.org and read it completely before using this file.
//
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
//
// The Original Code and all software distributed under the License is
// distributed on an "AS IS" basis, without warranty of any kind, and the
// Initial Developer hereby disclaims all such warranties, including without
// limitation, any warranties of merchantability, fitness for a particular
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#include <Font_FTLibrary.hxx>
IMPLEMENT_STANDARD_HANDLE (Font_FTLibrary, Standard_Transient)
IMPLEMENT_STANDARD_RTTIEXT(Font_FTLibrary, Standard_Transient)
// =======================================================================
// function : Font_FTLibrary
// purpose :
// =======================================================================
Font_FTLibrary::Font_FTLibrary()
: myFTLib (NULL)
{
if (FT_Init_FreeType (&myFTLib) != 0)
{
myFTLib = NULL;
}
}
// =======================================================================
// function : ~Font_FTLibrary
// purpose :
// =======================================================================
Font_FTLibrary::~Font_FTLibrary()
{
if (IsValid())
{
FT_Done_FreeType (myFTLib);
}
}

View File

@@ -0,0 +1,73 @@
// Created on: 2013-01-28
// Created by: Kirill GAVRILOV
// Copyright (c) 2013 OPEN CASCADE SAS
//
// The content of this file is subject to the Open CASCADE Technology Public
// License Version 6.5 (the "License"). You may not use the content of this file
// except in compliance with the License. Please obtain a copy of the License
// at http://www.opencascade.org and read it completely before using this file.
//
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
//
// The Original Code and all software distributed under the License is
// distributed on an "AS IS" basis, without warranty of any kind, and the
// Initial Developer hereby disclaims all such warranties, including without
// limitation, any warranties of merchantability, fitness for a particular
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#ifndef _Font_FTLibrary_H__
#define _Font_FTLibrary_H__
#include <Standard_DefineHandle.hxx>
#include <Standard_Transient.hxx>
#include <Handle_Standard_Transient.hxx>
// inclusion template for FreeType
#include <ft2build.h>
#include FT_FREETYPE_H
//! Wrapper over FT_Library. Provides access to FreeType library.
class Font_FTLibrary : public Standard_Transient
{
public:
//! Initialize new FT_Library instance.
Standard_EXPORT Font_FTLibrary();
//! Release FT_Library instance.
Standard_EXPORT ~Font_FTLibrary();
//! This method should always return true.
//! @return true if FT_Library instance is valid.
bool IsValid() const
{
return myFTLib != NULL;
}
//! Access FT_Library instance.
FT_Library Instance() const
{
return myFTLib;
}
private:
FT_Library myFTLib;
private:
Font_FTLibrary (const Font_FTLibrary& );
Font_FTLibrary& operator= (const Font_FTLibrary& );
public:
DEFINE_STANDARD_RTTI(Font_FTLibrary) // Type definition
};
DEFINE_STANDARD_HANDLE(Font_FTLibrary, Standard_Transient)
#endif // _Font_FTLibrary_H__

View File

@@ -38,8 +38,8 @@ Font_SystemFont::Font_SystemFont( const Handle(TCollection_HAsciiString)& FontNa
const Handle(TCollection_HAsciiString)& FilePath ):
MyFontName(FontName),
MyFontAspect(FontAspect),
MyFilePath(FilePath),
MyFaceSize(-1),
MyFilePath(FilePath),
MyVerification(Standard_True)
{
@@ -47,8 +47,9 @@ MyVerification(Standard_True)
Font_SystemFont::Font_SystemFont (const Handle(TCollection_HAsciiString)& theXLFD,
const Handle(TCollection_HAsciiString)& theFilePath) :
MyFilePath(theFilePath),
MyFontAspect(Font_FA_Regular)
MyFontAspect(Font_FA_Regular),
MyFaceSize(-1),
MyFilePath(theFilePath)
{
MyVerification = Standard_True;
if (theXLFD.IsNull())
@@ -73,7 +74,7 @@ MyFontAspect(Font_FA_Regular)
MyFaceSize = aXLFD.Token ("-", 7).IntegerValue();
// Detect font aspect
if (aXLFD.Token ("-", 3).IsEqual ("bold") &&
if (aXLFD.Token ("-", 3).IsEqual ("bold") &&
(aXLFD.Token ("-", 4).IsEqual ("i") || aXLFD.Token ("-", 4).IsEqual ("o")))
{
MyFontAspect = Font_FA_BoldItalic;
@@ -99,7 +100,7 @@ Standard_Boolean Font_SystemFont::IsValid() const{
if ( MyFontName->IsEmpty() || !MyFontName->IsAscii() )
return Standard_False;
OSD_Path path;
OSD_Path path;
return path.IsValid( MyFilePath->String() );
}