FontPlatformDataCairo.cpp [plain text]
#include "config.h"
#include "FontPlatformData.h"
#include "PlatformString.h"
#include "FontDescription.h"
#include <wtf/text/CString.h>
#include <cairo-ft.h>
#include <cairo.h>
#include <fontconfig/fcfreetype.h>
#include <gdk/gdk.h>
namespace WebCore {
FontPlatformData::FontPlatformData(const FontDescription& fontDescription, const AtomicString& familyName)
: m_pattern(0)
, m_fallbacks(0)
, m_size(fontDescription.computedPixelSize())
, m_syntheticBold(false)
, m_syntheticOblique(false)
, m_scaledFont(0)
{
FontPlatformData::init();
CString familyNameString = familyName.string().utf8();
const char* fcfamily = familyNameString.data();
int fcslant = FC_SLANT_ROMAN;
int fcweight = FC_WEIGHT_NORMAL;
double fcsize = fontDescription.computedPixelSize();
if (fontDescription.italic())
fcslant = FC_SLANT_ITALIC;
if (fontDescription.weight() >= FontWeight600)
fcweight = FC_WEIGHT_BOLD;
int type = fontDescription.genericFamily();
FcPattern* pattern = FcPatternCreate();
cairo_font_face_t* fontFace;
static const cairo_font_options_t* defaultOptions = cairo_font_options_create();
const cairo_font_options_t* options = NULL;
cairo_matrix_t fontMatrix;
if (!FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(fcfamily)))
goto freePattern;
switch (type) {
case FontDescription::SerifFamily:
fcfamily = "serif";
break;
case FontDescription::SansSerifFamily:
fcfamily = "sans-serif";
break;
case FontDescription::MonospaceFamily:
fcfamily = "monospace";
break;
case FontDescription::StandardFamily:
fcfamily = "sans-serif";
break;
case FontDescription::NoFamily:
default:
fcfamily = NULL;
break;
}
if (fcfamily && !FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(fcfamily)))
goto freePattern;
if (!FcPatternAddInteger(pattern, FC_WEIGHT, fcweight))
goto freePattern;
if (!FcPatternAddInteger(pattern, FC_SLANT, fcslant))
goto freePattern;
if (!FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fcsize))
goto freePattern;
FcConfigSubstitute(NULL, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
FcResult fcresult;
m_pattern = FcFontMatch(NULL, pattern, &fcresult);
if (!m_pattern)
goto freePattern;
fontFace = cairo_ft_font_face_create_for_pattern(m_pattern);
cairo_matrix_t ctm;
cairo_matrix_init_scale(&fontMatrix, fontDescription.computedPixelSize(), fontDescription.computedPixelSize());
cairo_matrix_init_identity(&ctm);
if (GdkScreen* screen = gdk_screen_get_default())
options = gdk_screen_get_font_options(screen);
if (!options)
options = defaultOptions;
m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options);
cairo_font_face_destroy(fontFace);
freePattern:
FcPatternDestroy(pattern);
}
FontPlatformData::FontPlatformData(float size, bool bold, bool italic)
: m_pattern(0)
, m_fallbacks(0)
, m_size(size)
, m_syntheticBold(bold)
, m_syntheticOblique(italic)
, m_scaledFont(0)
{
}
FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, float size, bool bold, bool italic)
: m_pattern(0)
, m_fallbacks(0)
, m_size(size)
, m_syntheticBold(bold)
, m_syntheticOblique(italic)
, m_scaledFont(0)
{
cairo_matrix_t fontMatrix;
cairo_matrix_init_scale(&fontMatrix, size, size);
cairo_matrix_t ctm;
cairo_matrix_init_identity(&ctm);
static const cairo_font_options_t* defaultOptions = cairo_font_options_create();
const cairo_font_options_t* options = NULL;
if (GdkScreen* screen = gdk_screen_get_default())
options = gdk_screen_get_font_options(screen);
if (!options)
options = defaultOptions;
m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options);
}
FontPlatformData& FontPlatformData::operator=(const FontPlatformData& other)
{
if (this == &other)
return *this;
m_size = other.m_size;
m_syntheticBold = other.m_syntheticBold;
m_syntheticOblique = other.m_syntheticOblique;
if (other.m_scaledFont)
cairo_scaled_font_reference(other.m_scaledFont);
if (m_scaledFont)
cairo_scaled_font_destroy(m_scaledFont);
m_scaledFont = other.m_scaledFont;
if (other.m_pattern)
FcPatternReference(other.m_pattern);
if (m_pattern)
FcPatternDestroy(m_pattern);
m_pattern = other.m_pattern;
if (m_fallbacks) {
FcFontSetDestroy(m_fallbacks);
m_fallbacks = 0;
}
return *this;
}
FontPlatformData::FontPlatformData(const FontPlatformData& other)
: m_pattern(0)
, m_fallbacks(0)
, m_scaledFont(0)
{
*this = other;
}
bool FontPlatformData::init()
{
static bool initialized = false;
if (initialized)
return true;
if (!FcInit()) {
fprintf(stderr, "Can't init font config library\n");
return false;
}
initialized = true;
return true;
}
FontPlatformData::~FontPlatformData()
{
if (m_pattern && ((FcPattern*)-1 != m_pattern)) {
FcPatternDestroy(m_pattern);
m_pattern = 0;
}
if (m_fallbacks) {
FcFontSetDestroy(m_fallbacks);
m_fallbacks = 0;
}
if (m_scaledFont)
cairo_scaled_font_destroy(m_scaledFont);
}
bool FontPlatformData::isFixedPitch()
{
if (!m_pattern)
return false;
int spacing;
if (FcPatternGetInteger(m_pattern, FC_SPACING, 0, &spacing) == FcResultMatch)
return spacing == FC_MONO;
return false;
}
bool FontPlatformData::operator==(const FontPlatformData& other) const
{
if (m_pattern == other.m_pattern)
return true;
if (m_pattern == 0 || m_pattern == reinterpret_cast<FcPattern*>(-1)
|| other.m_pattern == 0 || other.m_pattern == reinterpret_cast<FcPattern*>(-1))
return false;
return FcPatternEqual(m_pattern, other.m_pattern);
}
#ifndef NDEBUG
String FontPlatformData::description() const
{
return String();
}
#endif
}