pdfmm 0.9.20
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
mm::PdfFont Class Referenceabstract

#include <PdfFont.h>

Inheritance diagram for mm::PdfFont:
mm::PdfFontCIDTrueType mm::PdfFontSimple mm::PdfFontStandard14 mm::PdfFontTrueType mm::PdfFontType1 mm::PdfFontType3

Public Member Functions

void WriteStringToStream (PdfStream &stream, const std::string_view &str) const
 
void WriteStringToStream (std::ostream &stream, const std::string_view &str) const
 
double GetStringWidth (const std::string_view &view, const PdfTextState &state) const
 
bool TryGetStringWidth (const std::string_view &view, const PdfTextState &state, double &width) const
 
double GetStringWidth (const PdfString &encodedStr, const PdfTextState &state) const
 
bool TryGetStringWidth (const PdfString &encodedStr, const PdfTextState &state, double &width) const
 
double GetCharWidth (char32_t codePoint, const PdfTextState &state, bool ignoreCharSpacing=false) const
 
double GetLineSpacing (const PdfTextState &state) const
 
double GetUnderlineThickness (const PdfTextState &state) const
 
double GetUnderlinePosition (const PdfTextState &state) const
 
double GetStrikeOutPosition (const PdfTextState &state) const
 
double GetStrikeOutThickness (const PdfTextState &state) const
 
double GetAscent (const PdfTextState &state) const
 
double GetDescent (const PdfTextState &state) const
 
bool IsLoaded () const
 
bool IsSubsettingEnabled () const
 
const std::string & GetSubsetPrefix () const
 
const PdfNameGetIdentifier () const
 
const PdfEncodingGetEncoding () const
 
const PdfFontMetricsGetMetrics () const
 
const std::string & GetName () const
 

Static Public Member Functions

static std::unique_ptr< PdfFontCreate (PdfDocument &doc, const PdfFontMetricsConstPtr &metrics, const PdfEncoding &encoding, PdfFontInitOptions flags)
 
static std::unique_ptr< PdfFontCreateStandard14 (PdfDocument &doc, PdfStandard14FontType baseFont, const PdfEncoding &encoding, PdfFontInitOptions flags)
 
static bool TryCreateFromObject (PdfObject &obj, std::unique_ptr< PdfFont > &font)
 
static std::string ExtractBaseName (const std::string_view &fontName, bool &isBold, bool &isItalic)
 
static std::string ExtractBaseName (const std::string_view &fontName)
 

Protected Member Functions

 PdfFont (PdfDocument &doc, const PdfFontMetricsConstPtr &metrics, const PdfEncoding &encoding)
 
virtual bool TryMapCIDToGID (unsigned cid, unsigned &gid) const
 
virtual bool TryMapGIDToCID (unsigned gid, unsigned &cid) const
 
double GetCIDWidthRaw (unsigned cid) const
 
void FillDescriptor (PdfDictionary &dict) const
 
virtual void initImported ()
 

Detailed Description

Before you can draw text on a PDF document, you have to create a font object first. You can reuse this font object as often as you want.

Use PdfDocument::CreateFont to create a new font object. It will choose a correct subclass using PdfFontFactory.

This is only an abstract base class which is implemented for different font formats.

Constructor & Destructor Documentation

◆ PdfFont()

PdfFont::PdfFont ( PdfDocument doc,
const PdfFontMetricsConstPtr metrics,
const PdfEncoding encoding 
)
protected

Create a new PdfFont object which will introduce itself automatically to every page object it is used on.

Parameters
parentparent of the font object
metricspointer to a font metrics object. The font in the PDF file will match this fontmetrics object. The metrics object is deleted along with the font.
encodingthe encoding of this font

Member Function Documentation

◆ Create()

unique_ptr< PdfFont > PdfFont::Create ( PdfDocument doc,
const PdfFontMetricsConstPtr metrics,
const PdfEncoding encoding,
PdfFontInitOptions  flags 
)
static

Create a new PdfFont object.

Parameters
docthe parent of the created font.
metricspointer to a font metrics object. The font in the PDF file will match this fontmetrics object. The metrics object is deleted along with the created font. In case of an error, it is deleted here.
encodingthe encoding of this font.
flagsflags for font init
Returns
a new PdfFont object or nullptr

◆ CreateStandard14()

unique_ptr< PdfFont > PdfFont::CreateStandard14 ( PdfDocument doc,
PdfStandard14FontType  baseFont,
const PdfEncoding encoding,
PdfFontInitOptions  flags 
)
static

Creates a new standard 14 font object (of class PdfFontStandard14) if the font name (has to include variant) is one of the standard 14 fonts. The font name is to be given as specified (with an ASCII hyphen).

Parameters
docthe parent of the created font
baseFontstandard14 font type
encodingan encoding compatible with the font
flagsflags for font init

◆ ExtractBaseName() [1/2]

static std::string mm::PdfFont::ExtractBaseName ( const std::string_view &  fontName)
static

Extract base font name, removing known bold/italic/subset prefixes/suffixes

◆ ExtractBaseName() [2/2]

static std::string mm::PdfFont::ExtractBaseName ( const std::string_view &  fontName,
bool &  isBold,
bool &  isItalic 
)
static

Extract base font name, removing known bold/italic/subset prefixes/suffixes

◆ FillDescriptor()

void PdfFont::FillDescriptor ( PdfDictionary dict) const
protected

Fill the /FontDescriptor object dictionary

◆ GetAscent()

double PdfFont::GetAscent ( const PdfTextState &  state) const

Get the ascent of this font in PDF units for the current font size.

Returns
the ascender for this font
See also
GetAscent

◆ GetCharWidth()

double PdfFont::GetCharWidth ( char32_t  codePoint,
const PdfTextState &  state,
bool  ignoreCharSpacing = false 
) const
Remarks
Doesn't throw if characater glyph could not be found

◆ GetCIDWidthRaw()

double PdfFont::GetCIDWidthRaw ( unsigned  cid) const
protected

Get the raw width of a CID identifier

◆ GetDescent()

double PdfFont::GetDescent ( const PdfTextState &  state) const

Get the descent of this font in PDF units for the current font size. This value is usually negative!

Returns
the descender for this font
See also
GetDescent

◆ GetEncoding()

const PdfEncoding & mm::PdfFont::GetEncoding ( ) const
inline

Returns a reference to the fonts encoding

Returns
a PdfEncoding object.

◆ GetIdentifier()

const PdfName & mm::PdfFont::GetIdentifier ( ) const
inline

Returns the identifier of this font how it is known in the pages resource dictionary.

Returns
PdfName containing the identifier (e.g. /Ft13)

◆ GetLineSpacing()

double PdfFont::GetLineSpacing ( const PdfTextState &  state) const

Retrieve the line spacing for this font

Returns
the linespacing in PDF units

◆ GetMetrics()

const PdfFontMetrics & mm::PdfFont::GetMetrics ( ) const
inline

Returns a handle to the fontmetrics object of this font. This can be used for size calculations of text strings when drawn using this font.

Returns
a handle to the font metrics object

◆ GetName()

const std::string & mm::PdfFont::GetName ( ) const
inline

Get the base font name of this font

Returns
the font name, usually the /BaseFont key of a type1 or type0 font, or the /FontName in a font descriptor

◆ GetStrikeOutPosition()

double PdfFont::GetStrikeOutPosition ( const PdfTextState &  state) const

Return the position of the strikeout for the current font size in PDF units

Returns
the underline position in PDF units

◆ GetStrikeOutThickness()

double PdfFont::GetStrikeOutThickness ( const PdfTextState &  state) const

Get the width of the strikeout for the current font size in PDF units

Returns
the thickness of the strikeout in PDF units

◆ GetStringWidth() [1/2]

double PdfFont::GetStringWidth ( const PdfString encodedStr,
const PdfTextState &  state 
) const

Retrieve the width of a given encoded PdfString in PDF units when drawn with the current font

Parameters
viewa text string of which the width should be calculated
Returns
the width in PDF units
Remarks
Doesn't throw if string glyphs could not be partially or totally found

◆ GetStringWidth() [2/2]

double mm::PdfFont::GetStringWidth ( const std::string_view &  view,
const PdfTextState &  state 
) const

Retrieve the width of a given text string in PDF units when drawn with the current font

Parameters
viewa text string of which the width should be calculated
Returns
the width in PDF units
Remarks
Doesn't throw if string glyphs could not be partially or totally found

◆ GetSubsetPrefix()

const std::string & mm::PdfFont::GetSubsetPrefix ( ) const
inline
Returns
empty string or a 6 uppercase letter and "+" sign prefix used for font subsets

◆ GetUnderlinePosition()

double PdfFont::GetUnderlinePosition ( const PdfTextState &  state) const

Return the position of the underline for the current font size in PDF units

Returns
the underline position in PDF units

◆ GetUnderlineThickness()

double PdfFont::GetUnderlineThickness ( const PdfTextState &  state) const

Get the width of the underline for the current font size in PDF units

Returns
the thickness of the underline in PDF units

◆ initImported()

void PdfFont::initImported ( )
protectedvirtual

Inititialization tasks for imported/created from scratch fonts

Reimplemented in mm::PdfFontCIDTrueType, mm::PdfFontStandard14, mm::PdfFontTrueType, mm::PdfFontType1, and mm::PdfFontType3.

◆ IsLoaded()

bool mm::PdfFont::IsLoaded ( ) const
inline

True if the font is loaded from a PdfObject

◆ IsSubsettingEnabled()

bool mm::PdfFont::IsSubsettingEnabled ( ) const
inline

Check if this is a subsetting font.

Returns
true if this is a subsetting font

◆ TryCreateFromObject()

bool PdfFont::TryCreateFromObject ( PdfObject obj,
std::unique_ptr< PdfFont > &  font 
)
static

Create a new PdfFont from an existing font in a PDF file.

Parameters
obja PDF font object
fontthe created font object

◆ TryGetStringWidth() [1/2]

bool PdfFont::TryGetStringWidth ( const PdfString encodedStr,
const PdfTextState &  state,
double &  width 
) const
Remarks
Produces a partial result also in case of failures

◆ TryGetStringWidth() [2/2]

bool mm::PdfFont::TryGetStringWidth ( const std::string_view &  view,
const PdfTextState &  state,
double &  width 
) const
Remarks
Produces a partial result also in case of failures

◆ TryMapCIDToGID()

bool PdfFont::TryMapCIDToGID ( unsigned  cid,
unsigned &  gid 
) const
protectedvirtual

Optional function to map a CID to a GID

Example for /Type2 CID fonts may have a /CIDToGIDMap For Standard14 fonts we have to convert CID to unicode then we retrieve the glyph index

Reimplemented in mm::PdfFontStandard14.

◆ TryMapGIDToCID()

bool PdfFont::TryMapGIDToCID ( unsigned  gid,
unsigned &  cid 
) const
protectedvirtual

Optional function to map a CID to a GID

Example for /Type2 CID fonts may have a /CIDToGIDMap For Standard14 fonts we have to convert CID to unicode then we retrieve the glyph index

Reimplemented in mm::PdfFontStandard14.

◆ WriteStringToStream() [1/2]

void mm::PdfFont::WriteStringToStream ( PdfStream stream,
const std::string_view &  str 
) const

Write a string to a PdfStream in a format so that it can be used with this font. This is used by PdfPainter::DrawText to display a text string. The following PDF operator will be Tj

Parameters
streamthe string will be appended to stream without any leading or following whitespaces.
stra unicode or ansi string which will be displayed

◆ WriteStringToStream() [2/2]

void mm::PdfFont::WriteStringToStream ( std::ostream &  stream,
const std::string_view &  str 
) const

Write a string to a PdfStream in a format so that it can be used with this font. This is used by PdfPainter::DrawText to display a text string. The following PDF operator will be Tj

Parameters
streamthe string will be appended to the stream without any leading or following whitespaces.
stra unicode or ansi string which will be displayed