Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2008 Apple 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 <windows.h>
     33 
     34 #include "Font.h"
     35 #include "FontCache.h"
     36 #include "FontDescription.h"
     37 #include <cairo.h>
     38 #include <cairo-win32.h>
     39 #include <mlang.h>
     40 #include <tchar.h>
     41 #include <wtf/MathExtras.h>
     42 
     43 namespace WebCore {
     44 
     45 void SimpleFontData::platformInit()
     46 {
     47     m_scriptCache = 0;
     48     m_scriptFontProperties = 0;
     49     m_isSystemFont = false;
     50 
     51     m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
     52 
     53     if (m_platformData.useGDI())
     54        return initGDIFont();
     55 
     56     HDC hdc = GetDC(0);
     57     SaveDC(hdc);
     58 
     59     cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
     60     const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
     61 
     62     cairo_win32_scaled_font_select_font(scaledFont, hdc);
     63 
     64     TEXTMETRIC textMetrics;
     65     GetTextMetrics(hdc, &textMetrics);
     66     m_ascent = lroundf(textMetrics.tmAscent * metricsMultiplier);
     67     m_descent = lroundf(textMetrics.tmDescent * metricsMultiplier);
     68     m_xHeight = m_ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts.
     69     m_lineGap = lroundf(textMetrics.tmExternalLeading * metricsMultiplier);
     70     m_lineSpacing = m_ascent + m_descent + m_lineGap;
     71     m_avgCharWidth = lroundf(textMetrics.tmAveCharWidth * metricsMultiplier);
     72     m_maxCharWidth = lroundf(textMetrics.tmMaxCharWidth * metricsMultiplier);
     73 
     74     OUTLINETEXTMETRIC metrics;
     75     if (GetOutlineTextMetrics(hdc, sizeof(metrics), &metrics) > 0) {
     76         // This is a TrueType font.  We might be able to get an accurate xHeight
     77         GLYPHMETRICS gm;
     78         MAT2 mat = { 1, 0, 0, 1 };
     79         DWORD len = GetGlyphOutline(hdc, 'x', GGO_METRICS, &gm, 0, 0, &mat);
     80         if (len != GDI_ERROR && gm.gmptGlyphOrigin.y > 0)
     81             m_xHeight = gm.gmptGlyphOrigin.y * metricsMultiplier;
     82     }
     83 
     84     cairo_win32_scaled_font_done_font(scaledFont);
     85 
     86     m_isSystemFont = false;
     87     m_scriptCache = 0;
     88     m_scriptFontProperties = 0;
     89 
     90     RestoreDC(hdc, -1);
     91     ReleaseDC(0, hdc);
     92 }
     93 
     94 void SimpleFontData::platformCharWidthInit()
     95 {
     96     // charwidths are set in platformInit.
     97 }
     98 
     99 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
    100 {
    101     if (m_platformData.useGDI())
    102        return widthForGDIGlyph(glyph);
    103 
    104     HDC hdc = GetDC(0);
    105     SaveDC(hdc);
    106 
    107     cairo_scaled_font_t* scaledFont = m_platformData.scaledFont();
    108     cairo_win32_scaled_font_select_font(scaledFont, hdc);
    109 
    110     int width;
    111     GetCharWidthI(hdc, glyph, 1, 0, &width);
    112 
    113     cairo_win32_scaled_font_done_font(scaledFont);
    114 
    115     RestoreDC(hdc, -1);
    116     ReleaseDC(0, hdc);
    117 
    118     const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size();
    119     return width * metricsMultiplier;
    120 }
    121 
    122 }
    123