#include "config.h"
#include "fontprops.h"
#include <ApplicationServices/ApplicationServices.h>
#include <wx/defs.h>
#include <wx/gdicmn.h>
#include <wx/graphics.h>
#ifdef BUILDING_ON_TIGER
void (*wkGetFontMetrics)(CGFontRef, int* ascent, int* descent, int* lineGap, unsigned* unitsPerEm);
#endif
const float smallCapsFontSizeMultiplier = 0.7f;
const float contextDPI = 72.0f;
static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return x * (contextDPI / (contextDPI * unitsPerEm)); }
wxFontProperties::wxFontProperties(wxFont* font):
m_ascent(0), m_descent(0), m_lineGap(0), m_lineSpacing(0), m_xHeight(0)
{
CGFontRef cgFont;
#ifdef wxOSX_USE_CORE_TEXT && wxOSX_USE_CORE_TEXT
cgFont = CTFontCopyGraphicsFont((CTFontRef)font->OSXGetCTFont(), NULL);
#else
ATSFontRef fontRef;
fontRef = FMGetATSFontRefFromFont(font->MacGetATSUFontID());
if (fontRef)
cgFont = CGFontCreateWithPlatformFont((void*)&fontRef);
#endif
if (cgFont) {
int iAscent;
int iDescent;
int iLineGap;
unsigned unitsPerEm;
#ifdef BUILDING_ON_TIGER
wkGetFontMetrics(cgFont, &iAscent, &iDescent, &iLineGap, &unitsPerEm);
#else
iAscent = CGFontGetAscent(cgFont);
iDescent = CGFontGetDescent(cgFont);
iLineGap = CGFontGetLeading(cgFont);
unitsPerEm = CGFontGetUnitsPerEm(cgFont);
#endif
float pointSize = font->GetPointSize();
float fAscent = scaleEmToUnits(iAscent, unitsPerEm) * pointSize;
float fDescent = -scaleEmToUnits(iDescent, unitsPerEm) * pointSize;
float fLineGap = scaleEmToUnits(iLineGap, unitsPerEm) * pointSize;
m_ascent = lroundf(fAscent);
m_descent = lroundf(fDescent);
m_lineGap = lroundf(fLineGap);
wxCoord xHeight = 0;
GetTextExtent(*font, wxT("x"), NULL, &xHeight, NULL, NULL);
m_xHeight = lroundf(xHeight);
m_lineSpacing = m_ascent + m_descent + m_lineGap;
}
if (cgFont)
CGFontRelease(cgFont);
}
void GetTextExtent( const wxFont& font, const wxString& str, wxCoord *width, wxCoord *height,
wxCoord *descent, wxCoord *externalLeading )
{
wxGraphicsContext * const gc = wxGraphicsContext::Create();
gc->SetFont(font, *wxBLACK); struct GCTextExtent
{
wxDouble width, height, descent, externalLeading;
} e;
gc->GetTextExtent(str, &e.width, &e.height, &e.descent, &e.externalLeading);
if ( width )
*width = wxCoord(e.width + .5);
if ( height )
*height = wxCoord(e.height + .5);
if ( descent )
*descent = wxCoord(e.descent + .5);
if ( externalLeading )
*externalLeading = wxCoord(e.externalLeading + .5);
delete gc;
}