Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2007 Nicholas Shanks <webkit (at) nickshanks.com>
      4  * Copyright (C) 2013 Google, Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef AlternateFontFamily_h
     32 #define AlternateFontFamily_h
     33 
     34 #include "platform/fonts/FontDescription.h"
     35 #include "wtf/text/AtomicString.h"
     36 
     37 namespace WebCore {
     38 
     39 // We currently do not support bitmap fonts on windows (with GDI_FONTS_ON_WINDOWS enabled).
     40 // Instead of trying to construct a bitmap font and then going down the fallback path map
     41 // certain common bitmap fonts to their truetype equivalent up front. This also allows the
     42 // GDI_FONTS_ON_WINDOWS disabled code path to match our current behavior.
     43 inline const AtomicString& adjustFamilyNameToAvoidUnsupportedFonts(const AtomicString& familyName)
     44 {
     45 #if OS(WIN)
     46     // On Windows, 'Courier New' (truetype font) is always present and
     47     // 'Courier' is a bitmap font. On Mac on the other hand 'Courier' is
     48     // a truetype font. Thus pages asking for Courier are better of
     49     // using 'Courier New' on windows.
     50     DEFINE_STATIC_LOCAL(AtomicString, courier, ("Courier", AtomicString::ConstructFromLiteral));
     51     DEFINE_STATIC_LOCAL(AtomicString, courierNew, ("Courier New", AtomicString::ConstructFromLiteral));
     52     if (equalIgnoringCase(familyName, courier))
     53         return courierNew;
     54 
     55     // Alias 'MS Sans Serif' (bitmap font) -> 'Microsoft Sans Serif'
     56     // (truetype font).
     57     DEFINE_STATIC_LOCAL(AtomicString, msSans, ("MS Sans Serif", AtomicString::ConstructFromLiteral));
     58     DEFINE_STATIC_LOCAL(AtomicString, microsoftSans, ("Microsoft Sans Serif", AtomicString::ConstructFromLiteral));
     59     if (equalIgnoringCase(familyName, msSans))
     60         return microsoftSans;
     61 
     62     // Alias 'MS Serif' (bitmap) -> 'Times New Roman' (truetype font).
     63     // There's no 'Microsoft Sans Serif-equivalent' for Serif.
     64     DEFINE_STATIC_LOCAL(AtomicString, msSerif, ("MS Serif", AtomicString::ConstructFromLiteral));
     65     DEFINE_STATIC_LOCAL(AtomicString, timesNewRoman, ("Times New Roman", AtomicString::ConstructFromLiteral));
     66     if (equalIgnoringCase(familyName, msSerif))
     67         return timesNewRoman;
     68 #endif
     69 
     70     return familyName;
     71 }
     72 
     73 inline const AtomicString& alternateFamilyName(const AtomicString& familyName)
     74 {
     75     // Alias Courier <-> Courier New
     76     DEFINE_STATIC_LOCAL(AtomicString, courier, ("Courier", AtomicString::ConstructFromLiteral));
     77     DEFINE_STATIC_LOCAL(AtomicString, courierNew, ("Courier New", AtomicString::ConstructFromLiteral));
     78     if (equalIgnoringCase(familyName, courier))
     79         return courierNew;
     80 #if !OS(WIN)
     81     // On Windows, Courier New (truetype font) is always present and
     82     // Courier is a bitmap font. So, we don't want to map Courier New to
     83     // Courier.
     84     if (equalIgnoringCase(familyName, courierNew))
     85         return courier;
     86 #endif
     87 
     88     // Alias Times and Times New Roman.
     89     DEFINE_STATIC_LOCAL(AtomicString, times, ("Times", AtomicString::ConstructFromLiteral));
     90     DEFINE_STATIC_LOCAL(AtomicString, timesNewRoman, ("Times New Roman", AtomicString::ConstructFromLiteral));
     91     if (equalIgnoringCase(familyName, times))
     92         return timesNewRoman;
     93     if (equalIgnoringCase(familyName, timesNewRoman))
     94         return times;
     95 
     96     // Alias Arial and Helvetica
     97     DEFINE_STATIC_LOCAL(AtomicString, arial, ("Arial", AtomicString::ConstructFromLiteral));
     98     DEFINE_STATIC_LOCAL(AtomicString, helvetica, ("Helvetica", AtomicString::ConstructFromLiteral));
     99     if (equalIgnoringCase(familyName, arial))
    100         return helvetica;
    101     if (equalIgnoringCase(familyName, helvetica))
    102         return arial;
    103 
    104     return emptyAtom;
    105 }
    106 
    107 
    108 inline const AtomicString getFallbackFontFamily(const FontDescription& description)
    109 {
    110     DEFINE_STATIC_LOCAL(const AtomicString, sansStr, ("sans-serif", AtomicString::ConstructFromLiteral));
    111     DEFINE_STATIC_LOCAL(const AtomicString, serifStr, ("serif", AtomicString::ConstructFromLiteral));
    112     DEFINE_STATIC_LOCAL(const AtomicString, monospaceStr, ("monospace", AtomicString::ConstructFromLiteral));
    113     DEFINE_STATIC_LOCAL(const AtomicString, cursiveStr, ("cursive", AtomicString::ConstructFromLiteral));
    114     DEFINE_STATIC_LOCAL(const AtomicString, fantasyStr, ("fantasy", AtomicString::ConstructFromLiteral));
    115 
    116     switch (description.genericFamily()) {
    117     case FontDescription::SansSerifFamily:
    118         return sansStr;
    119     case FontDescription::SerifFamily:
    120         return serifStr;
    121     case FontDescription::MonospaceFamily:
    122         return monospaceStr;
    123     case FontDescription::CursiveFamily:
    124         return cursiveStr;
    125     case FontDescription::FantasyFamily:
    126         return fantasyStr;
    127     default:
    128         // Let the caller use the system default font.
    129         return emptyAtom;
    130     }
    131 }
    132 
    133 } // namespace WebCore
    134 
    135 #endif // AlternateFontFamily_h
    136