WebKit/chromium/src/gtk/WebFontInfo.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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)
       
    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     FcConfigSubstitute(0, pattern, FcMatchPattern);
       
    66     FcDefaultSubstitute(pattern);
       
    67 
       
    68     FcResult result;
       
    69     FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result);
       
    70     FcPatternDestroy(pattern);
       
    71     FcCharSetDestroy(cset);
       
    72 
       
    73     if (!fontSet)
       
    74         return WebCString();
       
    75 
       
    76     // Older versions of fontconfig have a bug where they cannot select
       
    77     // only scalable fonts so we have to manually filter the results.
       
    78     for (int i = 0; i < fontSet->nfont; ++i) {
       
    79         FcPattern* current = fontSet->fonts[i];
       
    80         FcBool isScalable;
       
    81 
       
    82         if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch
       
    83             || !isScalable)
       
    84             continue;
       
    85 
       
    86         // fontconfig can also return fonts which are unreadable
       
    87         FcChar8* cFilename;
       
    88         if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch)
       
    89             continue;
       
    90 
       
    91         if (access(reinterpret_cast<char*>(cFilename), R_OK))
       
    92             continue;
       
    93 
       
    94         FcChar8* family;
       
    95         WebCString result;
       
    96         if (FcPatternGetString(current, FC_FAMILY, 0, &family) == FcResultMatch) {
       
    97             const char* charFamily = reinterpret_cast<char*>(family);
       
    98             result = WebCString(charFamily, strlen(charFamily));
       
    99         }
       
   100         FcFontSetDestroy(fontSet);
       
   101         return result;
       
   102     }
       
   103 
       
   104     FcFontSetDestroy(fontSet);
       
   105     return WebCString();
       
   106 }
       
   107 
       
   108 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, WebFontRenderStyle* out)
       
   109 {
       
   110     bool isBold = sizeAndStyle & 1;
       
   111     bool isItalic = sizeAndStyle & 2;
       
   112     int pixelSize = sizeAndStyle >> 2;
       
   113 
       
   114     FcPattern* pattern = FcPatternCreate();
       
   115     FcValue fcvalue;
       
   116 
       
   117     fcvalue.type = FcTypeString;
       
   118     fcvalue.u.s = reinterpret_cast<const FcChar8 *>(family);
       
   119     FcPatternAdd(pattern, FC_FAMILY, fcvalue, FcFalse);
       
   120 
       
   121     fcvalue.type = FcTypeInteger;
       
   122     fcvalue.u.i = isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
       
   123     FcPatternAdd(pattern, FC_WEIGHT, fcvalue, FcFalse);
       
   124 
       
   125     fcvalue.type = FcTypeInteger;
       
   126     fcvalue.u.i = isItalic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
       
   127     FcPatternAdd(pattern, FC_SLANT, fcvalue, FcFalse);
       
   128 
       
   129     fcvalue.type = FcTypeBool;
       
   130     fcvalue.u.b = FcTrue;
       
   131     FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
       
   132 
       
   133     fcvalue.type = FcTypeDouble;
       
   134     fcvalue.u.d = pixelSize;
       
   135     FcPatternAdd(pattern, FC_SIZE, fcvalue, FcFalse);
       
   136 
       
   137     FcConfigSubstitute(0, pattern, FcMatchPattern);
       
   138     FcDefaultSubstitute(pattern);
       
   139 
       
   140     FcResult result;
       
   141     // Some versions of fontconfig don't actually write a value into result.
       
   142     // However, it's not clear from the documentation if result should be a
       
   143     // non-0 pointer: future versions might expect to be able to write to
       
   144     // it. So we pass in a valid pointer and ignore it.
       
   145     FcPattern* match = FcFontMatch(0, pattern, &result);
       
   146     FcPatternDestroy(pattern);
       
   147 
       
   148     out->setDefaults();
       
   149 
       
   150     if (!match) {
       
   151         FcPatternDestroy(match);
       
   152         return;
       
   153     }
       
   154 
       
   155     FcBool b;
       
   156     int i;
       
   157 
       
   158     if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &b) == FcResultMatch)
       
   159         out->useAntiAlias = b;
       
   160     if (FcPatternGetBool(match, FC_EMBEDDED_BITMAP, 0, &b) == FcResultMatch)
       
   161         out->useBitmaps = b;
       
   162     if (FcPatternGetBool(match, FC_AUTOHINT, 0, &b) == FcResultMatch)
       
   163         out->useAutoHint = b;
       
   164     if (FcPatternGetBool(match, FC_HINTING, 0, &b) == FcResultMatch)
       
   165         out->useHinting = b;
       
   166     if (FcPatternGetInteger(match, FC_HINT_STYLE, 0, &i) == FcResultMatch)
       
   167         out->hintStyle = i;
       
   168     if (FcPatternGetInteger(match, FC_RGBA, 0, &i) == FcResultMatch) {
       
   169         switch (i) {
       
   170         case FC_RGBA_NONE:
       
   171             out->useSubpixel = 0;
       
   172             break;
       
   173         case FC_RGBA_RGB:
       
   174         case FC_RGBA_BGR:
       
   175         case FC_RGBA_VRGB:
       
   176         case FC_RGBA_VBGR:
       
   177             out->useSubpixel = 1;
       
   178             break;
       
   179         default:
       
   180             // This includes FC_RGBA_UNKNOWN.
       
   181             out->useSubpixel = 2;
       
   182             break;
       
   183         }
       
   184     }
       
   185 
       
   186     FcPatternDestroy(match);
       
   187 }
       
   188 
       
   189 } // namespace WebKit