Home | History | Annotate | Download | only in gtk
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2006 Michael Emmel mike.emmel (at) gmail.com
      4  * Copyright (C) 2007, 2008 Alp Toker <alp (at) atoker.com>
      5  * Copyright (C) 2007 Holger Hans Peter Freyther
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1.  Redistributions of source code must retain the above copyright
     13  *     notice, this list of conditions and the following disclaimer.
     14  * 2.  Redistributions in binary form must reproduce the above copyright
     15  *     notice, this list of conditions and the following disclaimer in the
     16  *     documentation and/or other materials provided with the distribution.
     17  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     18  *     its contributors may be used to endorse or promote products derived
     19  *     from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include "config.h"
     34 #include "SimpleFontData.h"
     35 
     36 #include "FloatRect.h"
     37 #include "Font.h"
     38 #include "FontCache.h"
     39 #include "FontDescription.h"
     40 #include "GlyphBuffer.h"
     41 #include <cairo.h>
     42 #include <wtf/MathExtras.h>
     43 
     44 namespace WebCore {
     45 
     46 void SimpleFontData::platformInit()
     47 {
     48     cairo_font_extents_t font_extents;
     49     cairo_text_extents_t text_extents;
     50     cairo_scaled_font_extents(m_platformData.m_scaledFont, &font_extents);
     51     m_ascent = static_cast<int>(lroundf(font_extents.ascent));
     52     m_descent = static_cast<int>(lroundf(font_extents.descent));
     53     m_lineSpacing = static_cast<int>(lroundf(font_extents.height));
     54     // There seems to be some rounding error in cairo (or in how we
     55     // use cairo) with some fonts, like DejaVu Sans Mono, which makes
     56     // cairo report a height smaller than ascent + descent, which is
     57     // wrong and confuses WebCore's layout system. Workaround this
     58     // while we figure out what's going on.
     59     if (m_lineSpacing < m_ascent + m_descent)
     60         m_lineSpacing = m_ascent + m_descent;
     61     cairo_scaled_font_text_extents(m_platformData.m_scaledFont, "x", &text_extents);
     62     m_xHeight = text_extents.height;
     63     cairo_scaled_font_text_extents(m_platformData.m_scaledFont, " ", &text_extents);
     64     m_spaceWidth = static_cast<float>(text_extents.x_advance);
     65     m_lineGap = m_lineSpacing - m_ascent - m_descent;
     66     m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
     67 }
     68 
     69 void SimpleFontData::platformCharWidthInit()
     70 {
     71     m_avgCharWidth = 0.f;
     72     m_maxCharWidth = 0.f;
     73     initCharWidths();
     74 }
     75 
     76 void SimpleFontData::platformDestroy()
     77 {
     78     delete m_smallCapsFontData;
     79     m_smallCapsFontData = 0;
     80 }
     81 
     82 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
     83 {
     84     if (!m_smallCapsFontData) {
     85         FontDescription desc = FontDescription(fontDescription);
     86         desc.setComputedSize(0.70f*fontDescription.computedSize());
     87         const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family());
     88         m_smallCapsFontData = new SimpleFontData(*pdata);
     89     }
     90     return m_smallCapsFontData;
     91 }
     92 
     93 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
     94 {
     95     FT_Face face = cairo_ft_scaled_font_lock_face(m_platformData.m_scaledFont);
     96 
     97     if (!face)
     98         return false;
     99 
    100     for (int i = 0; i < length; i++) {
    101         if (FcFreeTypeCharIndex(face, characters[i]) == 0) {
    102             cairo_ft_scaled_font_unlock_face(m_platformData.m_scaledFont);
    103             return false;
    104         }
    105     }
    106 
    107     cairo_ft_scaled_font_unlock_face(m_platformData.m_scaledFont);
    108 
    109     return true;
    110 }
    111 
    112 void SimpleFontData::determinePitch()
    113 {
    114     m_treatAsFixedPitch = m_platformData.isFixedPitch();
    115 }
    116 
    117 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
    118 {
    119     ASSERT(m_platformData.m_scaledFont);
    120 
    121     cairo_glyph_t cglyph = { glyph, 0, 0 };
    122     cairo_text_extents_t extents;
    123     cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents);
    124 
    125     float w = (float)m_spaceWidth;
    126     if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0)
    127         w = (float)extents.x_advance;
    128     return w;
    129 }
    130 
    131 }
    132