Lomse library. API documentation  0.30.0
RequestFont Class Reference

#include <lomse_events.h>

Inheritance diagram for RequestFont:
Request

Detailed Description

When a Document uses a font that Lomse can not find it sends this request to the user application, asking for the file path for the font to use. The user application has to answer this request by invoking method RequestFont::set_font_fullname() passing as argument the path to the font to use.

Lomse manages fonts in different ways depending on the platform:

  • For Linux (and macOS) Lomse uses system installed fonts and makes substitutions when requested font is not available. The RequestFont callback is only used when Lomse cannot find the "Bravura.otf" music font needed for rendering musical symbols, when it is not installed in the system (e.g. missing dependency in installation page, local installation from sources, etc.).
  • For Windows, Lomse also uses system installed fonts and makes substitutions when requested font is not available, but the substitutions table is small and thus, the RequestFont callback is used when Lomse cannot find the the requested font or an appropriate substitution.
  • For other platforms Lomse only uses the fonts distributed with Lomse package and the RequestFont callback is used when requesting any other font not present in Lomse package.

Example:

class MyApp
{
public:
...
//callbacks
static void lomse_request_handler_method(void* pThis, Request* pRequest);
}
void MyApp::initialize_lomse()
{
...
//initialize the library with these values
m_lomse.init_library(pixel_format, resolution, reverse_y_axis, m_lomseReporter);
//set callback for requests
m_lomse.set_request_callback(this, lomse_request_handler_method);
}
void MyApp::lomse_request_handler_method(void* pThis, Request* pRequest)
{
static_cast<MyApp*>(pThis)->on_lomse_request(pRequest);
}
void MyApp::on_lomse_request(Request* pRequest)
{
int type = pRequest->get_request_type();
switch (type)
{
case k_dynamic_content_request:
generate_dynamic_content( dynamic_cast<RequestDynamic*>(pRequest) );
break;
get_font_filename( dynamic_cast<RequestFont*>(pRequest) );
break;
default:
LogError("Unknown request %d", type);
}
}
void MyApp::get_font_filename(RequestFont* pRequest)
{
//This is just a trivial example. In real applications you should
//use operating system services to find a suitable font
//notes on parameters received:
// - fontname can be either the face name (e.g., "Book Antiqua") or
// the familly name (e.g., "Liberation sans")
const string& fontname = pRequest->get_fontname();
bool bold = pRequest->get_bold();
bool italic = pRequest->get_italic();
string path = "/usr/share/fonts/truetype/";
//if family name, choose a font name
string name = fontname;
if (name == "serif")
name = "Times New Roman";
else if (name == "sans-serif")
name = "Tahoma";
else if (name == "handwritten" || name == "cursive")
name = "Monotype Corsiva";
else if (name == "monospaced")
name = "Courier New";
//choose a suitable font file
string fontfile;
if (name == "Times New Roman")
{
if (italic && bold)
fontfile = "freefont/FreeSerifBoldItalic.ttf";
else if (italic)
fontfile = "freefont/FreeSerifItalic.ttf";
else if (bold)
fontfile = "freefont/FreeSerifBold.ttf";
else
fontfile = "freefont/FreeSerif.ttf";
}
else if (name == "Tahoma")
{
if (bold)
fontfile = "freefont/FreeSansOblique.ttf";
else
fontfile = "freefont/FreeSans.ttf";
}
else if (name == "Monotype Corsiva")
{
fontfile = "ttf-dejavu/DejaVuSans-Oblique.ttf";
}
else if (name == "Courier New")
{
if (italic && bold)
fontfile = "freefont/FreeMonoBoldOblique.ttf";
else if (italic)
fontfile = "freefont/FreeMonoOblique.ttf";
else if (bold)
fontfile = "freefont/FreeMonoBold.ttf";
else
fontfile = "freefont/FreeMono.ttf";
}
else
fontfile = "freefont/FreeSerif.ttf";
pRequest->set_font_fullname( path + fontfile );
}

Public Member Functions

 RequestFont (const string &fontname, bool bold, bool italic)
 
const string & get_fontname ()
 
bool get_bold ()
 
bool get_italic ()
 
void set_font_fullname (const string &name)
 
- Public Member Functions inherited from Request
virtual ~Request ()
 
int get_request_type ()
 
bool is_dynamic_content_request ()
 
bool is_get_font_filename ()
 

Constructor & Destructor Documentation

◆ RequestFont()

RequestFont::RequestFont ( const string &  fontname,
bool  bold,
bool  italic 
)
inline

Constructor.

Member Function Documentation

◆ get_bold()

bool RequestFont::get_bold ( )
inline

Returns true is the font is requested in bold face. */.

◆ get_fontname()

const string& RequestFont::get_fontname ( )
inline

Returns the fontname. It can be either the face name (i.e. "Book Antiqua") or the familly name (e.g., "Liberation sans").

◆ get_italic()

bool RequestFont::get_italic ( )
inline

Returns true is the font is requested in italic style. */.

◆ set_font_fullname()

void RequestFont::set_font_fullname ( const string &  name)
inline

User application should use this method for setting the requested data