1/* 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2007-2009 Torch Mobile, Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include "config.h" 31#include "SimpleFontData.h" 32 33#include "FloatRect.h" 34#include "Font.h" 35#include "FontCache.h" 36#include "FontDescription.h" 37#include <mlang.h> 38#include <wtf/MathExtras.h> 39 40namespace WebCore { 41 42extern HDC g_screenDC; 43 44void SimpleFontData::platformInit() 45{ 46 if (!m_platformData.isValid()) 47 return; 48 49 const TEXTMETRIC& tm = m_platformData.metrics(); 50 m_isSystemFont = m_platformData.isSystemFont(); 51 52 float ascent = (tm.tmAscent * m_platformData.size() + 36) / 72.0f; 53 float descent = (tm.tmDescent * m_platformData.size() + 36) / 72.0f; 54 float lineGap = (tm.tmExternalLeading * m_platformData.size() + 36) / 72.0f; 55 m_fontMetrics.setAscent(ascent); 56 m_fontMetrics.setDescent(descent); 57 m_fontMetrics.setLineGap(lineGap); 58 m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(lineGap)); 59 m_fontMetrics.setXHeight(ascent * 0.56f); 60} 61 62void SimpleFontData::platformDestroy() 63{ 64} 65 66PassRefPtr<SimpleFontData> SimpleFontData::platformCreateScaledFontData(const FontDescription& fontDescription, float scaleFactor) const 67{ 68 FontDescription fontDesc(fontDescription); 69 fontDesc.setComputedSize(lroundf(scaleFactor * fontDesc.computedSize())); 70 fontDesc.setSpecifiedSize(lroundf(scaleFactor * fontDesc.specifiedSize())); 71 fontDesc.setKeywordSize(lroundf(scaleFactor * fontDesc.keywordSize())); 72 FontPlatformData* result = fontCache().getCachedFontPlatformData(fontDesc, m_platformData.family()); 73 if (!result) 74 return 0; 75 return SimpleFontData::create(*result); 76} 77 78DWORD getKnownFontCodePages(const wchar_t* family); 79 80bool SimpleFontData::containsCharacters(const UChar* characters, int length) const 81{ 82 if (m_platformData.isDisabled()) 83 return true; 84 85 // FIXME: Microsoft documentation seems to imply that characters can be output using a given font and DC 86 // merely by testing code page intersection. This seems suspect though. Can't a font only partially 87 // cover a given code page? 88 89 // FIXME: in the case that we failed to get the interface, still use the font. 90 IMLangFontLinkType* langFontLink = fontCache().getFontLinkInterface(); 91 if (!langFontLink) 92 return true; 93 94 DWORD fontCodePages = m_platformData.codePages(); 95 if (!fontCodePages) 96 return false; 97 98 DWORD acpCodePages = 0; 99 langFontLink->CodePageToCodePages(CP_ACP, &acpCodePages); 100 101 DWORD actualCodePages; 102 long numCharactersProcessed; 103 while (length) { 104 langFontLink->GetStrCodePages(characters, length, acpCodePages, &actualCodePages, &numCharactersProcessed); 105 if (actualCodePages && !(actualCodePages & fontCodePages)) 106 return false; 107 108 length -= numCharactersProcessed; 109 characters += numCharactersProcessed; 110 } 111 112 return true; 113} 114 115void SimpleFontData::determinePitch() 116{ 117 if (!m_platformData.isValid()) 118 return; 119 120 const TEXTMETRIC& tm = m_platformData.metrics(); 121 122 // Yes, this looks backwards, but the fixed pitch bit is actually set if the font 123 // is *not* fixed pitch. Unbelievable but true. 124 m_treatAsFixedPitch = !(tm.tmPitchAndFamily & TMPF_FIXED_PITCH); 125} 126 127FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const 128{ 129 return FloatRect(); 130} 131 132float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 133{ 134 if (m_platformData.isDisabled()) 135 return 0; 136 137 HGDIOBJ hOldFont = SelectObject(g_screenDC, m_platformData.hfont()); 138 139 SIZE fontSize; 140 wchar_t c = glyph; 141 GetTextExtentPoint32(g_screenDC, &c, 1, &fontSize); 142 143 SelectObject(g_screenDC, hOldFont); 144 145 return (float)fontSize.cx * (float)m_platformData.size() / 72.f; 146} 147 148 149void SimpleFontData::platformCharWidthInit() 150{ 151 if (!m_platformData.isValid()) 152 return; 153 154 const TEXTMETRIC& tm = m_platformData.metrics(); 155 m_avgCharWidth = (tm.tmAveCharWidth * m_platformData.size() + 36) / 72; 156 m_maxCharWidth = (tm.tmMaxCharWidth * m_platformData.size() + 36) / 72; 157} 158 159} // namespace WebCore 160