Home | History | Annotate | Download | only in android
      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 "SkFontHost.h"
     36 #include "SkPaint.h"
     37 #include "SkTypeface.h"
     38 #include "SkTime.h"
     39 
     40 using namespace android;
     41 
     42 namespace WebCore {
     43 
     44 void SimpleFontData::platformInit()
     45 {
     46     SkPaint  paint;
     47     SkPaint::FontMetrics skiaFontMetrics;
     48 
     49     m_platformData.setupPaint(&paint);
     50     paint.getFontMetrics(&skiaFontMetrics);
     51 
     52     float d = SkScalarToFloat(skiaFontMetrics.fDescent);
     53     float s = SkScalarToFloat(skiaFontMetrics.fDescent - skiaFontMetrics.fAscent);
     54     float a = s - d;
     55 
     56     m_fontMetrics.setAscent(a);
     57     m_fontMetrics.setDescent(d);
     58     m_fontMetrics.setXHeight(SkScalarToFloat(-skiaFontMetrics.fAscent) * 0.56f);   // hack I stole from the window's port
     59     m_fontMetrics.setLineSpacing(a + d);
     60     m_fontMetrics.setLineGap(SkScalarToFloat(skiaFontMetrics.fLeading));
     61 
     62     if (platformData().orientation() == Vertical && !isTextOrientationFallback()) {
     63         static const uint32_t vheaTag = SkSetFourByteTag('v', 'h', 'e', 'a');
     64         static const uint32_t vorgTag = SkSetFourByteTag('V', 'O', 'R', 'G');
     65         const SkFontID fontID = m_platformData.uniqueID();
     66         size_t vheaSize = SkFontHost::GetTableSize(fontID, vheaTag);
     67         size_t vorgSize = SkFontHost::GetTableSize(fontID, vorgTag);
     68         if ((vheaSize > 0) || (vorgSize > 0))
     69             m_hasVerticalGlyphs = true;
     70     }
     71 }
     72 
     73 void SimpleFontData::platformCharWidthInit()
     74 {
     75     m_avgCharWidth = 0.f;
     76     m_maxCharWidth = 0.f;
     77     initCharWidths();
     78 }
     79 
     80 void SimpleFontData::platformDestroy()
     81 {
     82 }
     83 
     84 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
     85 {
     86     if (!m_derivedFontData)
     87         m_derivedFontData = DerivedFontData::create(isCustomFont());
     88     if (!m_derivedFontData->smallCaps)
     89         m_derivedFontData->smallCaps = new SimpleFontData(FontPlatformData(m_platformData, fontDescription.computedSize() * 0.7f));
     90 
     91     return m_derivedFontData->smallCaps.get();
     92 }
     93 
     94 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
     95 {
     96     if (!m_derivedFontData)
     97         m_derivedFontData = DerivedFontData::create(isCustomFont());
     98     if (!m_derivedFontData->emphasisMark)
     99         m_derivedFontData->emphasisMark = new SimpleFontData(FontPlatformData(m_platformData, fontDescription.computedSize() * 0.5f));
    100 
    101     return m_derivedFontData->emphasisMark.get();
    102 }
    103 
    104 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
    105 {
    106     SkPaint     paint;
    107 
    108     m_platformData.setupPaint(&paint);
    109     paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
    110     return paint.containsText(characters, length << 1);
    111 }
    112 
    113 void SimpleFontData::determinePitch()
    114 {
    115     m_treatAsFixedPitch = m_platformData.isFixedPitch();
    116 }
    117 
    118 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
    119 {
    120     return FloatRect();
    121 }
    122 
    123 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
    124 {
    125     SkASSERT(sizeof(glyph) == 2);   // compile-time assert
    126 
    127     SkPaint  paint;
    128 
    129     m_platformData.setupPaint(&paint);
    130 
    131     float advanceWidth;
    132     if (EmojiFont::IsEmojiGlyph(glyph))
    133         advanceWidth = EmojiFont::GetAdvanceWidth(glyph, paint);
    134     else {
    135         paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
    136         advanceWidth = SkScalarToFloat(paint.measureText(&glyph, 2));
    137     }
    138     return advanceWidth;
    139 }
    140 
    141 
    142 }
    143