1 /* 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "SimpleFontData.h" 31 32 #include "FontCache.h" 33 #include "FloatRect.h" 34 #include "FontDescription.h" 35 #include <wtf/MathExtras.h> 36 #include <unicode/uchar.h> 37 #include <unicode/unorm.h> 38 39 #include <wx/defs.h> 40 #include <wx/dcscreen.h> 41 #include "fontprops.h" 42 43 namespace WebCore 44 { 45 46 void SimpleFontData::platformInit() 47 { 48 wxFont *font = m_platformData.font(); 49 if (font && font->IsOk()) { 50 wxFontProperties props = wxFontProperties(font); 51 m_ascent = props.GetAscent(); 52 m_descent = props.GetDescent(); 53 m_lineSpacing = props.GetLineSpacing(); 54 m_xHeight = props.GetXHeight(); 55 m_unitsPerEm = 1; // FIXME! 56 m_lineGap = props.GetLineGap(); 57 } 58 59 #if OS(WINDOWS) 60 m_scriptCache = 0; 61 m_scriptFontProperties = 0; 62 m_isSystemFont = false; 63 m_syntheticBoldOffset = 0.0f; 64 #endif 65 } 66 67 void SimpleFontData::platformCharWidthInit() 68 { 69 m_avgCharWidth = 0.f; 70 m_maxCharWidth = 0.f; 71 initCharWidths(); 72 } 73 74 void SimpleFontData::platformDestroy() 75 { 76 delete m_smallCapsFontData; 77 m_smallCapsFontData = 0; 78 79 #if OS(WINDOWS) 80 if (m_scriptFontProperties) { 81 delete m_scriptFontProperties; 82 m_scriptFontProperties = 0; 83 } 84 85 if (m_scriptCache) 86 ScriptFreeCache(&m_scriptCache); 87 #endif 88 } 89 90 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const 91 { 92 if (!m_smallCapsFontData){ 93 FontDescription desc = FontDescription(fontDescription); 94 desc.setSpecifiedSize(0.70f*fontDescription.computedSize()); 95 const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family()); 96 m_smallCapsFontData = new SimpleFontData(*pdata); 97 } 98 return m_smallCapsFontData; 99 } 100 101 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const 102 { 103 // FIXME: We will need to implement this to load non-ASCII encoding sites 104 return wxFontContainsCharacters(*m_platformData.font(), characters, length); 105 } 106 107 void SimpleFontData::determinePitch() 108 { 109 if (m_platformData.font() && m_platformData.font()->Ok()) 110 m_treatAsFixedPitch = m_platformData.font()->IsFixedWidth(); 111 else 112 m_treatAsFixedPitch = false; 113 } 114 115 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 116 { 117 #if __WXMSW__ 118 // under Windows / wxMSW we currently always use GDI fonts. 119 return widthForGDIGlyph(glyph); 120 #else 121 // TODO: fix this! Make GetTextExtents a method of wxFont in 2.9 122 int width = 10; 123 GetTextExtent(*m_platformData.font(), (wxChar)glyph, &width, NULL); 124 return width; 125 #endif 126 } 127 128 #if OS(WINDOWS) 129 SCRIPT_FONTPROPERTIES* SimpleFontData::scriptFontProperties() const 130 { 131 // AFAICT this is never called even by the Win port anymore. 132 return 0; 133 } 134 135 void SimpleFontData::initGDIFont() 136 { 137 // unused by wx port 138 } 139 140 void SimpleFontData::platformCommonDestroy() 141 { 142 // unused by wx port 143 } 144 145 float SimpleFontData::widthForGDIGlyph(Glyph glyph) const 146 { 147 HDC hdc = GetDC(0); 148 HGDIOBJ oldFont = SelectObject(hdc, m_platformData.hfont()); 149 int width; 150 GetCharWidthI(hdc, glyph, 1, 0, &width); 151 SelectObject(hdc, oldFont); 152 ReleaseDC(0, hdc); 153 return width; 154 } 155 #endif 156 157 } 158