1 /* 2 * Copyright 2009, The Android Open Source Project 3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 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 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 29 #include "EmojiFont.h" 30 #include "Font.h" 31 #include "FontCache.h" 32 #include "SimpleFontData.h" 33 #include "FloatRect.h" 34 #include "FontDescription.h" 35 #include "SkPaint.h" 36 #include "SkTypeface.h" 37 #include "SkTime.h" 38 39 using namespace android; 40 41 namespace WebCore { 42 43 void SimpleFontData::platformInit() 44 { 45 SkPaint paint; 46 SkPaint::FontMetrics metrics; 47 48 m_platformData.setupPaint(&paint); 49 (void)paint.getFontMetrics(&metrics); 50 51 // use ceil instead of round to favor descent, given a lot of accidental 52 // clipping of descenders (e.g. 14pt 'g') in textedit fields 53 int d = SkScalarCeil(metrics.fDescent); 54 int s = SkScalarRound(metrics.fDescent - metrics.fAscent); 55 int a = s - d; 56 57 m_ascent = a; 58 m_descent = d; 59 m_xHeight = SkScalarToFloat(-metrics.fAscent) * 0.56f; // hack I stole from the window's port 60 m_lineSpacing = a + d; 61 m_lineGap = SkScalarRound(metrics.fLeading); 62 } 63 64 void SimpleFontData::platformCharWidthInit() 65 { 66 m_avgCharWidth = 0.f; 67 m_maxCharWidth = 0.f; 68 initCharWidths(); 69 } 70 71 void SimpleFontData::platformDestroy() 72 { 73 delete m_smallCapsFontData; 74 } 75 76 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const 77 { 78 if (!m_smallCapsFontData) { 79 m_smallCapsFontData = new SimpleFontData(FontPlatformData(m_platformData, fontDescription.computedSize() * 0.7f)); 80 } 81 return m_smallCapsFontData; 82 } 83 84 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const 85 { 86 SkPaint paint; 87 88 m_platformData.setupPaint(&paint); 89 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); 90 return paint.containsText(characters, length << 1); 91 } 92 93 void SimpleFontData::determinePitch() 94 { 95 m_treatAsFixedPitch = false; 96 } 97 98 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 99 { 100 SkASSERT(sizeof(glyph) == 2); // compile-time assert 101 102 SkPaint paint; 103 104 m_platformData.setupPaint(&paint); 105 106 if (EmojiFont::IsEmojiGlyph(glyph)) 107 return EmojiFont::GetAdvanceWidth(glyph, paint); 108 else { 109 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 110 return SkScalarToFloat(paint.measureText(&glyph, 2)); 111 } 112 } 113 114 } 115