Home | History | Annotate | Download | only in gtk
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      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
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebFontInfo.h"
     33 #include "WebFontRenderStyle.h"
     34 
     35 #include <fontconfig/fontconfig.h>
     36 #include <string.h>
     37 #include <unicode/utf16.h>
     38 
     39 namespace WebKit {
     40 
     41 WebCString WebFontInfo::familyForChars(const WebUChar* characters, size_t numCharacters, const char* preferredLocale)
     42 {
     43     FcCharSet* cset = FcCharSetCreate();
     44     for (size_t i = 0; i < numCharacters; ++i) {
     45         if (U16_IS_SURROGATE(characters[i])
     46          && U16_IS_SURROGATE_LEAD(characters[i])
     47          && i != numCharacters - 1
     48          && U16_IS_TRAIL(characters[i + 1])) {
     49               FcCharSetAddChar(cset, U16_GET_SUPPLEMENTARY(characters[i], characters[i+1]));
     50           i++;
     51         } else
     52               FcCharSetAddChar(cset, characters[i]);
     53     }
     54     FcPattern* pattern = FcPatternCreate();
     55 
     56     FcValue fcvalue;
     57     fcvalue.type = FcTypeCharSet;
     58     fcvalue.u.c = cset;
     59     FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse);
     60 
     61     fcvalue.type = FcTypeBool;
     62     fcvalue.u.b = FcTrue;
     63     FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
     64 
     65     if (preferredLocale) {
     66         FcLangSet* langset = FcLangSetCreate();
     67         FcLangSetAdd(langset, reinterpret_cast<const FcChar8 *>(preferredLocale));
     68         FcPatternAddLangSet(pattern, FC_LANG, langset);
     69         FcLangSetDestroy(langset);
     70     }
     71 
     72     FcConfigSubstitute(0, pattern, FcMatchPattern);
     73     FcDefaultSubstitute(pattern);
     74 
     75     FcResult result;
     76     FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result);
     77     FcPatternDestroy(pattern);
     78     FcCharSetDestroy(cset);
     79 
     80     if (!fontSet)
     81         return WebCString();
     82 
     83     // Older versions of fontconfig have a bug where they cannot select
     84     // only scalable fonts so we have to manually filter the results.
     85     for (int i = 0; i < fontSet->nfont; ++i) {
     86         FcPattern* current = fontSet->fonts[i];
     87         FcBool isScalable;
     88 
     89         if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch
     90             || !isScalable)
     91             continue;
     92 
     93         // fontconfig can also return fonts which are unreadable
     94         FcChar8* cFilename;
     95         if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch)
     96             continue;
     97 
     98         if (access(reinterpret_cast<char*>(cFilename), R_OK))
     99             continue;
    100 
    101         FcChar8* family;
    102         WebCString result;
    103         if (FcPatternGetString(current, FC_FAMILY, 0, &family) == FcResultMatch) {
    104             const char* charFamily = reinterpret_cast<char*>(family);
    105             result = WebCString(charFamily, strlen(charFamily));
    106         }
    107         FcFontSetDestroy(fontSet);
    108         return result;
    109     }
    110 
    111     FcFontSetDestroy(fontSet);
    112     return WebCString();
    113 }
    114 
    115 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, WebFontRenderStyle* out)
    116 {
    117     bool isBold = sizeAndStyle & 1;
    118     bool isItalic = sizeAndStyle & 2;
    119     int pixelSize = sizeAndStyle >> 2;
    120 
    121     FcPattern* pattern = FcPatternCreate();
    122     FcValue fcvalue;
    123 
    124     fcvalue.type = FcTypeString;
    125     fcvalue.u.s = reinterpret_cast<const FcChar8 *>(family);
    126     FcPatternAdd(pattern, FC_FAMILY, fcvalue, FcFalse);
    127 
    128     fcvalue.type = FcTypeInteger;
    129     fcvalue.u.i = isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
    130     FcPatternAdd(pattern, FC_WEIGHT, fcvalue, FcFalse);
    131 
    132     fcvalue.type = FcTypeInteger;
    133     fcvalue.u.i = isItalic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
    134     FcPatternAdd(pattern, FC_SLANT, fcvalue, FcFalse);
    135 
    136     fcvalue.type = FcTypeBool;
    137     fcvalue.u.b = FcTrue;
    138     FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
    139 
    140     fcvalue.type = FcTypeDouble;
    141     fcvalue.u.d = pixelSize;
    142     FcPatternAdd(pattern, FC_SIZE, fcvalue, FcFalse);
    143 
    144     FcConfigSubstitute(0, pattern, FcMatchPattern);
    145     FcDefaultSubstitute(pattern);
    146 
    147     FcResult result;
    148     // Some versions of fontconfig don't actually write a value into result.
    149     // However, it's not clear from the documentation if result should be a
    150     // non-0 pointer: future versions might expect to be able to write to
    151     // it. So we pass in a valid pointer and ignore it.
    152     FcPattern* match = FcFontMatch(0, pattern, &result);
    153     FcPatternDestroy(pattern);
    154 
    155     out->setDefaults();
    156 
    157     if (!match) {
    158         FcPatternDestroy(match);
    159         return;
    160     }
    161 
    162     FcBool b;
    163     int i;
    164 
    165     if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &b) == FcResultMatch)
    166         out->useAntiAlias = b;
    167     if (FcPatternGetBool(match, FC_EMBEDDED_BITMAP, 0, &b) == FcResultMatch)
    168         out->useBitmaps = b;
    169     if (FcPatternGetBool(match, FC_AUTOHINT, 0, &b) == FcResultMatch)
    170         out->useAutoHint = b;
    171     if (FcPatternGetBool(match, FC_HINTING, 0, &b) == FcResultMatch)
    172         out->useHinting = b;
    173     if (FcPatternGetInteger(match, FC_HINT_STYLE, 0, &i) == FcResultMatch)
    174         out->hintStyle = i;
    175     if (FcPatternGetInteger(match, FC_RGBA, 0, &i) == FcResultMatch) {
    176         switch (i) {
    177         case FC_RGBA_NONE:
    178             out->useSubpixel = 0;
    179             break;
    180         case FC_RGBA_RGB:
    181         case FC_RGBA_BGR:
    182         case FC_RGBA_VRGB:
    183         case FC_RGBA_VBGR:
    184             out->useSubpixel = 1;
    185             break;
    186         default:
    187             // This includes FC_RGBA_UNKNOWN.
    188             out->useSubpixel = 2;
    189             break;
    190         }
    191     }
    192 
    193     FcPatternDestroy(match);
    194 }
    195 
    196 } // namespace WebKit
    197