Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::text::FreeTypeFont Class Reference

This derived Font class represents a FreeType2 font. More...

#include "freetypefont.h"

Inheritance diagram for ion::text::FreeTypeFont:
Collaboration diagram for ion::text::FreeTypeFont:

Classes

struct  GlyphMetrics
 This struct represents the metrics for a single glyph. More...
 

Public Member Functions

 FreeTypeFont (const std::string &name, size_t size_in_pixels, size_t sdf_padding, const void *data, size_t data_size)
 Constructs an instance using the given name. More...
 
const GlyphMetricsGetGlyphMetrics (GlyphIndex glyph_index) const
 Returns the GlyphMetrics for a glyph. More...
 
const math::Vector2f GetKerning (CharIndex char_index0, CharIndex char_index1) const
 Returns the delta that should be made to relative positioning of characters beyond the metrics above. More...
 
GlyphIndex GetDefaultGlyphForChar (CharIndex char_index) const override
 Font overrides. More...
 
const Layout BuildLayout (const std::string &text, const LayoutOptions &options) const override
 Creates a layout as specified by options for a given single- or multi- line string text. More...
 
void AddFallbackFont (const FontPtr &fallback) override
 Causes this font to use the font fallback as a fallback if a requested glyph is not found. More...
 
const std::string & GetName () const
 Returns the name of the font. More...
 
size_t GetSizeInPixels () const
 Returns the size of the font in pixels. More...
 
size_t GetSdfPadding () const
 Returns the padding value used when generating SDF glyphs from the font. More...
 
const FontMetricsGetFontMetrics () const
 Returns the FontMetrics for the font. More...
 
const GlyphGridGetGlyphGrid (GlyphIndex glyph_index) const
 Returns the GlyphGrid for the indexed character. More...
 
void FilterGlyphs (GlyphSet *glyph_set)
 Filter zero-size glyphs from glyph_set. More...
 
void AddGlyphsForAsciiCharacterRange (ion::text::CharIndex start, ion::text::CharIndex finish, ion::text::GlyphSet *glyphs)
 For each character in [start,finish] adds the default glyph from font to glyphs. More...
 
void CacheSdfGrids (const GlyphSet &glyph_set)
 Makes sure that the GlyphData for each glyph in glyph_set has an SDF grid cached inside the font. More...
 
const AllocatorPtr & GetAllocator () const
 Returns the Allocator that was used for the instance. More...
 
const AllocatorPtr & GetNonNullAllocator () const
 Return our allocator, or the default allocator if the instance was declared on the stack. More...
 
const AllocatorPtr & GetAllocatorForLifetime (AllocationLifetime lifetime) const
 Convenience function that returns the Allocator to use to allocate an object with a specific lifetime. More...
 
void * operator new (size_t size)
 The standard no-parameter new operator uses the default Allocator. More...
 
void * operator new (size_t size, AllocationLifetime lifetime)
 This overloaded version of the new operator uses the AllocationManager's default Allocator for the specified lifetime. More...
 
void * operator new (size_t size, const AllocatorPtr &allocator)
 This overloaded version of the new operator takes the Allocator to use directly as a parameter. More...
 
void * operator new (size_t size, const AllocatorPtr &allocator, void *ptr)
 Special operator new for using placement new with Allocatables. More...
 
void * operator new (size_t size, void *ptr)
 The placement new operator is defined conventionally. More...
 
void operator delete (void *ptr)
 Define the delete operator to use specialized functions dealing with an Allocator. More...
 
void operator delete (void *ptr, AllocationLifetime lifetime)
 Windows requires these (or it issues C4291 warnings). More...
 
void operator delete (void *ptr, const AllocatorPtr &allocator)
 
void operator delete (void *ptr, void *ptr2)
 The placement delete operator does nothing, as usual. More...
 
int GetRefCount () const
 GetRefCount() is part of the interface necessary for SharedPtr. More...
 

Protected Types

typedef base::AllocMap
< GlyphIndex, GlyphGrid
GlyphMap
 Convenience typedef for the map storing GlyphGrid instances. More...
 

Protected Member Functions

 FreeTypeFont (const std::string &name, size_t size_in_pixels, size_t sdf_padding)
 This constructor is used only for testing. More...
 
 ~FreeTypeFont () override
 The destructor is private because all base::Referent classes must have protected or private destructors. More...
 
bool LoadGlyphGrid (GlyphIndex glyph_index, GlyphGrid *glyph_grid) const override
 Override Font::LoadGlyphGrid to load glyphs on demand. More...
 
GlyphGridGetMutableGlyphGrid (GlyphIndex glyph_index) const
 Non-const version of GetGlyphGrid. More...
 
GlyphGridGetMutableGlyphGridLocked (GlyphIndex glyph_index) const
 Prelocked version of GetMutableGlyphGrid. More...
 
const GlyphGridAddGlyph (GlyphIndex glyph_index, const GlyphGrid &glyph) const
 Adds a GlyphGrid to the GlyphMap. More...
 
void SetFontMetrics (const FontMetrics &metrics)
 Sets FontMetrics. SetFontMetrics() should only ever be called once. More...
 
bool CacheSdfGrid (GlyphIndex glyph_index, const base::Array2< double > &sdf_pixels)
 Replaces the grid in a glyph with an SDF grid. More...
 

Protected Attributes

const size_t size_in_pixels_
 Size in pixels. More...
 

Detailed Description

This derived Font class represents a FreeType2 font.

Definition at line 35 of file freetypefont.h.

Member Typedef Documentation

Convenience typedef for the map storing GlyphGrid instances.

Definition at line 134 of file font.h.

Constructor & Destructor Documentation

ion::text::FreeTypeFont::FreeTypeFont ( const std::string &  name,
size_t  size_in_pixels,
size_t  sdf_padding,
const void *  data,
size_t  data_size 
)

Constructs an instance using the given name.

FreeTypeFont functions.

The supplied font data may be in any format that FreeType2's FT_New_Memory_Face() can handle. The data must not be deallocated before destruction of the FreeTypeFont. The font size will be as close as possible to the specified size.

Definition at line 785 of file freetypefont.cc.

References ion::text::Font::SetFontMetrics().

ion::text::FreeTypeFont::FreeTypeFont ( const std::string &  name,
size_t  size_in_pixels,
size_t  sdf_padding 
)
protected

This constructor is used only for testing.

It simulates the failure of the FreeType2 library initialization, which is otherwise extremely difficult to test.

Definition at line 795 of file freetypefont.cc.

ion::text::FreeTypeFont::~FreeTypeFont ( )
overrideprotected

The destructor is private because all base::Referent classes must have protected or private destructors.

Definition at line 802 of file freetypefont.cc.

Member Function Documentation

void ion::text::FreeTypeFont::AddFallbackFont ( const FontPtr fallback)
overridevirtual

Causes this font to use the font fallback as a fallback if a requested glyph is not found.

This is useful in internationalization cases, as few fonts contain glyphs for enough unicode codepoints to satisfy most languages.

Implements ion::text::Font.

Definition at line 863 of file freetypefont.cc.

References ion::base::SharedPtr< T >::Get().

const Font::GlyphGrid & ion::text::Font::AddGlyph ( GlyphIndex  glyph_index,
const GlyphGrid glyph 
) const
protectedinherited

Adds a GlyphGrid to the GlyphMap.

Definition at line 81 of file font.cc.

References ion::base::IsInvalidReference().

void ion::text::Font::AddGlyphsForAsciiCharacterRange ( ion::text::CharIndex  start,
ion::text::CharIndex  finish,
ion::text::GlyphSet glyphs 
)
inherited

For each character in [start,finish] adds the default glyph from font to glyphs.

Since this is not well-defined for all of Unicode, enforces that [start,finish] lies within [1,127], where the character->glyph mapping is simple enough.

Definition at line 137 of file font.cc.

References DCHECK_GE, DCHECK_LE, and ion::text::Font::GetDefaultGlyphForChar().

const Layout ion::text::FreeTypeFont::BuildLayout ( const std::string &  text,
const LayoutOptions options 
) const
overridevirtual

Creates a layout as specified by options for a given single- or multi- line string text.

Implements ion::text::Font.

Definition at line 839 of file freetypefont.cc.

References ion::text::ComputeTextSize(), ion::text::ComputeTransformData(), ion::text::LayOutText(), ion::base::SplitString(), and ion::text::LayoutOptions::target_size.

bool ion::text::Font::CacheSdfGrid ( GlyphIndex  glyph_index,
const base::Array2< double > &  sdf_pixels 
)
protectedinherited

Replaces the grid in a glyph with an SDF grid.

This is used to cache the SDF grid, since it is relatively expensive to compute. Logs an error and returns false on error.

Definition at line 110 of file font.cc.

References ion::port::ERROR, ion::text::Font::GetMutableGlyphGrid(), ion::text::Font::GlyphGrid::is_sdf, LOG, and ion::text::Font::GlyphGrid::pixels.

Referenced by ion::text::Font::CacheSdfGrids().

void ion::text::Font::CacheSdfGrids ( const GlyphSet glyph_set)
inherited

Makes sure that the GlyphData for each glyph in glyph_set has an SDF grid cached inside the font.

This assumes that the requested glyphs are available in the font. There is no real need to call this outside of Ion's internal code.

Definition at line 95 of file font.cc.

References ion::text::Font::CacheSdfGrid(), ion::text::ComputeSdfGrid(), DCHECK, ion::text::Font::GetMutableGlyphGrid(), ion::text::Font::GetSdfPadding(), ion::text::Font::GlyphGrid::is_sdf, ion::base::IsInvalidReference(), and ion::text::Font::GlyphGrid::pixels.

void ion::text::Font::FilterGlyphs ( GlyphSet glyph_set)
inherited

Filter zero-size glyphs from glyph_set.

Definition at line 125 of file font.cc.

References ion::text::Font::GetMutableGlyphGridLocked(), and ion::text::Font::GlyphGrid::IsZeroSize().

const AllocatorPtr& ion::base::Allocatable::GetAllocator ( ) const
inlineinherited

Returns the Allocator that was used for the instance.

This will be NULL if the instance was declared on the stack or created with normal placement new.

Definition at line 68 of file allocatable.h.

References allocator_.

Referenced by ion::base::DataContainer::CreateAndCopy(), ion::text::DynamicFontImage::FindContainingImageDataIndex(), and ion::text::DynamicFontImage::FindImageDataIndex().

const AllocatorPtr& ion::base::Allocatable::GetAllocatorForLifetime ( AllocationLifetime  lifetime) const
inlineinherited
GlyphIndex ion::text::FreeTypeFont::GetDefaultGlyphForChar ( CharIndex  char_index) const
overridevirtual

Font overrides.

Implements ion::text::Font.

Definition at line 835 of file freetypefont.cc.

Referenced by ion::text::ComputeTextSize().

const FontMetrics& ion::text::Font::GetFontMetrics ( ) const
inlineinherited

Returns the FontMetrics for the font.

Definition at line 87 of file font.h.

Referenced by ion::text::ComputeTextSize().

const Font::GlyphGrid & ion::text::Font::GetGlyphGrid ( GlyphIndex  glyph_index) const
inherited

Returns the GlyphGrid for the indexed character.

Returns an invalid reference if the index does not refer to a glyph in the font.

Definition at line 49 of file font.cc.

References ion::text::Font::GetMutableGlyphGrid().

const FreeTypeFont::GlyphMetrics & ion::text::FreeTypeFont::GetGlyphMetrics ( GlyphIndex  glyph_index) const

Returns the GlyphMetrics for a glyph.

This defines the basic layout of a given glyph.

Definition at line 868 of file freetypefont.cc.

References ion::base::IsInvalidReference().

Referenced by ion::text::ComputeTextSize().

const math::Vector2f ion::text::FreeTypeFont::GetKerning ( CharIndex  char_index0,
CharIndex  char_index1 
) const

Returns the delta that should be made to relative positioning of characters beyond the metrics above.

Definition at line 809 of file freetypefont.cc.

Font::GlyphGrid * ion::text::Font::GetMutableGlyphGrid ( GlyphIndex  glyph_index) const
protectedinherited

Non-const version of GetGlyphGrid.

Definition at line 55 of file font.cc.

References ion::text::Font::GetMutableGlyphGridLocked().

Referenced by ion::text::Font::CacheSdfGrid(), ion::text::Font::CacheSdfGrids(), and ion::text::Font::GetGlyphGrid().

Font::GlyphGrid * ion::text::Font::GetMutableGlyphGridLocked ( GlyphIndex  glyph_index) const
protectedinherited

Prelocked version of GetMutableGlyphGrid.

Definition at line 60 of file font.cc.

References DCHECK, ion::port::Mutex::IsLocked(), and ion::text::Font::LoadGlyphGrid().

Referenced by ion::text::Font::FilterGlyphs(), and ion::text::Font::GetMutableGlyphGrid().

const std::string& ion::text::Font::GetName ( ) const
inlineinherited

Returns the name of the font.

Definition at line 75 of file font.h.

const AllocatorPtr& ion::base::Allocatable::GetNonNullAllocator ( ) const
inlineinherited

Return our allocator, or the default allocator if the instance was declared on the stack.

Definition at line 72 of file allocatable.h.

References allocator_, and ion::base::AllocationManager::GetNonNullAllocator().

int ion::base::Shareable::GetRefCount ( ) const
inlineinherited

GetRefCount() is part of the interface necessary for SharedPtr.

Definition at line 34 of file shareable.h.

Referenced by ion::base::Notifier::RemoveReceiver().

size_t ion::text::Font::GetSdfPadding ( ) const
inlineinherited

Returns the padding value used when generating SDF glyphs from the font.

Most SDF glyphs are larger than the original glyph so that the outer edges have a nice distance fall-off. The SDF glyph grids are padded by this many pixels on all sides.

Definition at line 84 of file font.h.

Referenced by ion::text::Font::CacheSdfGrids(), ion::text::BasicBuilder::UpdateUniforms(), and ion::text::OutlineBuilder::UpdateUniforms().

size_t ion::text::Font::GetSizeInPixels ( ) const
inlineinherited

Returns the size of the font in pixels.

Definition at line 78 of file font.h.

Referenced by ion::text::ComputeTextSize().

bool ion::text::FreeTypeFont::LoadGlyphGrid ( GlyphIndex  glyph_index,
GlyphGrid glyph_grid 
) const
overrideprotectedvirtual

Override Font::LoadGlyphGrid to load glyphs on demand.

Reimplemented from ion::text::Font.

Definition at line 804 of file freetypefont.cc.

void ion::base::Allocatable::operator delete ( void *  ptr)
inlineinherited

Define the delete operator to use specialized functions dealing with an Allocator.

Definition at line 109 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
AllocationLifetime  lifetime 
)
inlineinherited

Windows requires these (or it issues C4291 warnings).

Definition at line 112 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
const AllocatorPtr allocator 
)
inlineinherited

Definition at line 113 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
void *  ptr2 
)
inlineinherited

The placement delete operator does nothing, as usual.

Definition at line 118 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size)
inlineinherited

The standard no-parameter new operator uses the default Allocator.

Definition at line 84 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
AllocationLifetime  lifetime 
)
inlineinherited

This overloaded version of the new operator uses the AllocationManager's default Allocator for the specified lifetime.

Definition at line 88 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
const AllocatorPtr allocator 
)
inlineinherited

This overloaded version of the new operator takes the Allocator to use directly as a parameter.

If the Allocator pointer is NULL, this uses the default Allocator.

Definition at line 95 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
const AllocatorPtr allocator,
void *  ptr 
)
inlineinherited

Special operator new for using placement new with Allocatables.

Definition at line 100 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
void *  ptr 
)
inlineinherited

The placement new operator is defined conventionally.

Definition at line 105 of file allocatable.h.

void ion::text::Font::SetFontMetrics ( const FontMetrics metrics)
protectedinherited

Sets FontMetrics. SetFontMetrics() should only ever be called once.

Definition at line 89 of file font.cc.

References DCHECK_EQ, and ion::text::Font::FontMetrics::line_advance_height.

Referenced by ion::text::CoreTextFont::CoreTextFont(), and FreeTypeFont().

Member Data Documentation

const size_t ion::text::Font::size_in_pixels_
protectedinherited

Size in pixels.

Definition at line 169 of file font.h.


The documentation for this class was generated from the following files: