SimpleFontDataChromiumWin.cpp [plain text]
#include "config.h"
#include "SimpleFontData.h"
#include "ChromiumBridge.h"
#include "Font.h"
#include "FontCache.h"
#include "FloatRect.h"
#include "FontDescription.h"
#include <wtf/MathExtras.h>
#include <unicode/uchar.h>
#include <unicode/unorm.h>
#include <objidl.h>
#include <mlang.h>
namespace WebCore {
static inline float scaleEmToUnits(float x, int unitsPerEm)
{
return unitsPerEm ? x / static_cast<float>(unitsPerEm) : x;
}
void SimpleFontData::platformInit()
{
HDC dc = GetDC(0);
HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
TEXTMETRIC textMetric = {0};
if (!GetTextMetrics(dc, &textMetric)) {
if (ChromiumBridge::ensureFontLoaded(m_platformData.hfont())) {
if (!GetTextMetrics(dc, &textMetric))
LOG_ERROR("Unable to get the text metrics after second attempt");
}
}
m_avgCharWidth = textMetric.tmAveCharWidth;
m_maxCharWidth = textMetric.tmMaxCharWidth;
m_ascent = textMetric.tmAscent;
m_descent = textMetric.tmDescent;
m_lineGap = textMetric.tmExternalLeading;
m_xHeight = m_ascent * 0.56f;
OUTLINETEXTMETRIC outlineTextMetric;
if (GetOutlineTextMetrics(dc, sizeof(outlineTextMetric), &outlineTextMetric) > 0) {
GLYPHMETRICS glyphMetrics = {0};
MAT2 identityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
DWORD len = GetGlyphOutlineW(dc, 'x', GGO_METRICS, &glyphMetrics, 0, 0, &identityMatrix);
if (len != GDI_ERROR && glyphMetrics.gmBlackBoxY > 0)
m_xHeight = static_cast<float>(glyphMetrics.gmBlackBoxY);
}
m_lineSpacing = m_ascent + m_descent + m_lineGap;
SelectObject(dc, oldFont);
ReleaseDC(0, dc);
}
void SimpleFontData::platformCharWidthInit()
{
}
void SimpleFontData::platformDestroy()
{
delete m_smallCapsFontData;
m_smallCapsFontData = 0;
}
SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
{
if (!m_smallCapsFontData) {
LOGFONT winFont;
GetObject(m_platformData.hfont(), sizeof(LOGFONT), &winFont);
float smallCapsSize = 0.70f * fontDescription.computedSize();
winFont.lfHeight = -lroundf(smallCapsSize);
HFONT hfont = CreateFontIndirect(&winFont);
m_smallCapsFontData =
new SimpleFontData(FontPlatformData(hfont, smallCapsSize));
}
return m_smallCapsFontData;
}
bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
{
return false;
}
void SimpleFontData::determinePitch()
{
HDC dc = GetDC(0);
HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
TEXTMETRIC textMetric = {0};
if (!GetTextMetrics(dc, &textMetric)) {
if (ChromiumBridge::ensureFontLoaded(m_platformData.hfont())) {
if (!GetTextMetrics(dc, &textMetric))
LOG_ERROR("Unable to get the text metrics after second attempt");
}
}
m_treatAsFixedPitch = ((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0);
SelectObject(dc, oldFont);
ReleaseDC(0, dc);
}
FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
{
return FloatRect();
}
float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
{
HDC dc = GetDC(0);
HGDIOBJ oldFont = SelectObject(dc, m_platformData.hfont());
int width = 0;
if (!GetCharWidthI(dc, glyph, 1, 0, &width)) {
if (ChromiumBridge::ensureFontLoaded(m_platformData.hfont())) {
if (!GetCharWidthI(dc, glyph, 1, 0, &width))
LOG_ERROR("Unable to get the char width after second attempt");
}
}
SelectObject(dc, oldFont);
ReleaseDC(0, dc);
return static_cast<float>(width);
}
}