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

Font is a base class for implementation-specific representations of fonts. More...

#include "font.h"

Inheritance diagram for ion::text::Font:
Collaboration diagram for ion::text::Font:

Classes

struct  FontMetrics
 This struct represents the cumulative metrics for the font. More...
 
struct  GlyphGrid
 A grid representing a rendered glyph, with each grid pixel representing pixel coverage in the range (0,1). More...
 

Public Member Functions

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...
 
virtual GlyphIndex GetDefaultGlyphForChar (CharIndex char_index) const =0
 Returns the index of a glyph corresponding to the given character in the default ("unicode", in practice) charmap of the font. 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...
 
virtual const Layout BuildLayout (const std::string &text, const LayoutOptions &options) const =0
 Creates a layout as specified by options for a given single- or multi- line string text. 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...
 
virtual void AddFallbackFont (const FontPtr &fallback)=0
 Causes this font to use the font fallback as a fallback if a requested glyph is not found. 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

 Font (const std::string &name, size_t size_in_pixels, size_t sdf_padding)
 The constructor is protected because this is an abstract base class. More...
 
 ~Font () override
 The destructor is protected because all base::Referent classes must have protected or private destructors. More...
 
GlyphGridGetMutableGlyphGrid (GlyphIndex glyph_index) const
 Non-const version of GetGlyphGrid. More...
 
GlyphGridGetMutableGlyphGridLocked (GlyphIndex glyph_index) const
 Prelocked version of GetMutableGlyphGrid. More...
 
virtual bool LoadGlyphGrid (GlyphIndex glyph_index, GlyphGrid *glyph_grid) const
 Called by GetGlyphGrid() for missing glyphs. 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

Font is a base class for implementation-specific representations of fonts.

It contains font metrics, glyph metrics, and rendered glyph grids.

Definition at line 43 of file font.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::Font::Font ( const std::string &  name,
size_t  size_in_pixels,
size_t  sdf_padding 
)
protected

The constructor is protected because this is an abstract base class.

Definition at line 41 of file font.cc.

ion::text::Font::~Font ( )
overrideprotected

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

Definition at line 47 of file font.cc.

Member Function Documentation

virtual void ion::text::Font::AddFallbackFont ( const FontPtr fallback)
pure virtual

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.

Implemented in ion::text::FreeTypeFont, and ion::text::CoreTextFont.

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

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 
)

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 GetDefaultGlyphForChar().

virtual const Layout ion::text::Font::BuildLayout ( const std::string &  text,
const LayoutOptions options 
) const
pure virtual

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

Implemented in ion::text::FreeTypeFont, and ion::text::CoreTextFont.

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

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, GetMutableGlyphGrid(), ion::text::Font::GlyphGrid::is_sdf, LOG, and ion::text::Font::GlyphGrid::pixels.

Referenced by CacheSdfGrids().

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

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 CacheSdfGrid(), ion::text::ComputeSdfGrid(), DCHECK, GetMutableGlyphGrid(), GetSdfPadding(), ion::text::Font::GlyphGrid::is_sdf, ion::base::IsInvalidReference(), and ion::text::Font::GlyphGrid::pixels.

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

Filter zero-size glyphs from glyph_set.

Definition at line 125 of file font.cc.

References 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
virtual GlyphIndex ion::text::Font::GetDefaultGlyphForChar ( CharIndex  char_index) const
pure virtual

Returns the index of a glyph corresponding to the given character in the default ("unicode", in practice) charmap of the font.

Note that this is an ill-defined concept, as a character may well require multiple glyphs to render, or require different glyphs in different contexts, and so on. This and AddGlyphsForAsciiCharacterRange are therefore intended as a quick and dirty way to prepopulate a font with glyps so that a static FontImage can be used, and cannot be assumed to work on non-trivial (i.e., non-latin) characters. Returns zero if no glyph available.

Implemented in ion::text::FreeTypeFont, and ion::text::CoreTextFont.

Referenced by AddGlyphsForAsciiCharacterRange().

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

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

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 GetMutableGlyphGrid().

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

Non-const version of GetGlyphGrid.

Definition at line 55 of file font.cc.

References GetMutableGlyphGridLocked().

Referenced by CacheSdfGrid(), CacheSdfGrids(), and GetGlyphGrid().

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

Prelocked version of GetMutableGlyphGrid.

Definition at line 60 of file font.cc.

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

Referenced by FilterGlyphs(), and GetMutableGlyphGrid().

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

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
inline

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 CacheSdfGrids(), ion::text::BasicBuilder::UpdateUniforms(), and ion::text::OutlineBuilder::UpdateUniforms().

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

Returns the size of the font in pixels.

Definition at line 78 of file font.h.

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

bool ion::text::Font::LoadGlyphGrid ( GlyphIndex  glyph_index,
GlyphGrid glyph_grid 
) const
protectedvirtual

Called by GetGlyphGrid() for missing glyphs.

Child classes that load glyphs on-demand should override this method to load the result into glyph_grid. Returns true if a glyph was loaded.

Reimplemented in ion::text::FreeTypeFont, and ion::text::CoreTextFont.

Definition at line 77 of file font.cc.

Referenced by GetMutableGlyphGridLocked().

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)
protected

Member Data Documentation

const size_t ion::text::Font::size_in_pixels_
protected

Size in pixels.

Definition at line 169 of file font.h.


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