gfx/skia/trunk/src/fonts/SkFontMgr_fontconfig.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/fonts/SkFontMgr_fontconfig.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkFontMgr.h"
    1.12 +#include "SkFontStyle.h"
    1.13 +#include "SkFontConfigInterface.h"
    1.14 +#include "SkFontConfigTypeface.h"
    1.15 +#include "SkMath.h"
    1.16 +#include "SkString.h"
    1.17 +#include "SkTDArray.h"
    1.18 +
    1.19 +// for now we pull these in directly. eventually we will solely rely on the
    1.20 +// SkFontConfigInterface instance.
    1.21 +#include <fontconfig/fontconfig.h>
    1.22 +#include <unistd.h>
    1.23 +
    1.24 +// Defined in SkFontHost_FreeType.cpp
    1.25 +bool find_name_and_attributes(SkStream* stream, SkString* name,
    1.26 +                              SkTypeface::Style* style, bool* isFixedWidth);
    1.27 +
    1.28 +// borrow this global from SkFontHost_fontconfig. eventually that file should
    1.29 +// go away, and be replaced with this one.
    1.30 +extern SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
    1.31 +static SkFontConfigInterface* RefFCI() {
    1.32 +    return SkFontHost_fontconfig_ref_global();
    1.33 +}
    1.34 +
    1.35 +// look for the last substring after a '/' and return that, or return null.
    1.36 +static const char* find_just_name(const char* str) {
    1.37 +    const char* last = strrchr(str, '/');
    1.38 +    return last ? last + 1 : NULL;
    1.39 +}
    1.40 +
    1.41 +static bool is_lower(char c) {
    1.42 +    return c >= 'a' && c <= 'z';
    1.43 +}
    1.44 +
    1.45 +static int get_int(FcPattern* pattern, const char field[]) {
    1.46 +    int value;
    1.47 +    if (FcPatternGetInteger(pattern, field, 0, &value) != FcResultMatch) {
    1.48 +        value = SK_MinS32;
    1.49 +    }
    1.50 +    return value;
    1.51 +}
    1.52 +
    1.53 +static const char* get_name(FcPattern* pattern, const char field[]) {
    1.54 +    const char* name;
    1.55 +    if (FcPatternGetString(pattern, field, 0, (FcChar8**)&name) != FcResultMatch) {
    1.56 +        name = "";
    1.57 +    }
    1.58 +    return name;
    1.59 +}
    1.60 +
    1.61 +static bool valid_pattern(FcPattern* pattern) {
    1.62 +    FcBool is_scalable;
    1.63 +    if (FcPatternGetBool(pattern, FC_SCALABLE, 0, &is_scalable) != FcResultMatch || !is_scalable) {
    1.64 +        return false;
    1.65 +    }
    1.66 +
    1.67 +    // fontconfig can also return fonts which are unreadable
    1.68 +    const char* c_filename = get_name(pattern, FC_FILE);
    1.69 +    if (0 == *c_filename) {
    1.70 +        return false;
    1.71 +    }
    1.72 +    if (access(c_filename, R_OK) != 0) {
    1.73 +        return false;
    1.74 +    }
    1.75 +    return true;
    1.76 +}
    1.77 +
    1.78 +static bool match_name(FcPattern* pattern, const char family_name[]) {
    1.79 +    return !strcasecmp(family_name, get_name(pattern, FC_FAMILY));
    1.80 +}
    1.81 +
    1.82 +static FcPattern** MatchFont(FcFontSet* font_set,
    1.83 +                             const char post_config_family[],
    1.84 +                             int* count) {
    1.85 +  // Older versions of fontconfig have a bug where they cannot select
    1.86 +  // only scalable fonts so we have to manually filter the results.
    1.87 +
    1.88 +    FcPattern** iter = font_set->fonts;
    1.89 +    FcPattern** stop = iter + font_set->nfont;
    1.90 +    // find the first good match
    1.91 +    for (; iter < stop; ++iter) {
    1.92 +        if (valid_pattern(*iter)) {
    1.93 +            break;
    1.94 +        }
    1.95 +    }
    1.96 +
    1.97 +    if (iter == stop || !match_name(*iter, post_config_family)) {
    1.98 +        return NULL;
    1.99 +    }
   1.100 +
   1.101 +    FcPattern** firstIter = iter++;
   1.102 +    for (; iter < stop; ++iter) {
   1.103 +        if (!valid_pattern(*iter) || !match_name(*iter, post_config_family)) {
   1.104 +            break;
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    *count = iter - firstIter;
   1.109 +    return firstIter;
   1.110 +}
   1.111 +
   1.112 +class SkFontStyleSet_FC : public SkFontStyleSet {
   1.113 +public:
   1.114 +    SkFontStyleSet_FC(FcPattern** matches, int count);
   1.115 +    virtual ~SkFontStyleSet_FC();
   1.116 +
   1.117 +    virtual int count() SK_OVERRIDE { return fRecCount; }
   1.118 +    virtual void getStyle(int index, SkFontStyle*, SkString* style) SK_OVERRIDE;
   1.119 +    virtual SkTypeface* createTypeface(int index) SK_OVERRIDE;
   1.120 +    virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE;
   1.121 +
   1.122 +private:
   1.123 +    struct Rec {
   1.124 +        SkString    fStyleName;
   1.125 +        SkString    fFileName;
   1.126 +        SkFontStyle fStyle;
   1.127 +    };
   1.128 +    Rec* fRecs;
   1.129 +    int  fRecCount;
   1.130 +};
   1.131 +
   1.132 +static int map_range(int value,
   1.133 +                     int old_min, int old_max, int new_min, int new_max) {
   1.134 +    SkASSERT(old_min < old_max);
   1.135 +    SkASSERT(new_min < new_max);
   1.136 +    return new_min + SkMulDiv(value - old_min,
   1.137 +                              new_max - new_min, old_max - old_min);
   1.138 +}
   1.139 +
   1.140 +static SkFontStyle make_fontconfig_style(FcPattern* match) {
   1.141 +    int weight = get_int(match, FC_WEIGHT);
   1.142 +    int width = get_int(match, FC_WIDTH);
   1.143 +    int slant = get_int(match, FC_SLANT);
   1.144 +//    SkDebugf("old weight %d new weight %d\n", weight, map_range(weight, 0, 80, 0, 400));
   1.145 +
   1.146 +    // fontconfig weight seems to be 0..200 or so, so we remap it here
   1.147 +    weight = map_range(weight, 0, 80, 0, 400);
   1.148 +    width = map_range(width, 0, 200, 0, 9);
   1.149 +    return SkFontStyle(weight, width, slant > 0 ? SkFontStyle::kItalic_Slant
   1.150 +                                                : SkFontStyle::kUpright_Slant);
   1.151 +}
   1.152 +
   1.153 +SkFontStyleSet_FC::SkFontStyleSet_FC(FcPattern** matches, int count) {
   1.154 +    fRecCount = count;
   1.155 +    fRecs = SkNEW_ARRAY(Rec, count);
   1.156 +    for (int i = 0; i < count; ++i) {
   1.157 +        fRecs[i].fStyleName.set(get_name(matches[i], FC_STYLE));
   1.158 +        fRecs[i].fFileName.set(get_name(matches[i], FC_FILE));
   1.159 +        fRecs[i].fStyle = make_fontconfig_style(matches[i]);
   1.160 +    }
   1.161 +}
   1.162 +
   1.163 +SkFontStyleSet_FC::~SkFontStyleSet_FC() {
   1.164 +    SkDELETE_ARRAY(fRecs);
   1.165 +}
   1.166 +
   1.167 +void SkFontStyleSet_FC::getStyle(int index, SkFontStyle* style,
   1.168 +                                 SkString* styleName) {
   1.169 +    SkASSERT((unsigned)index < (unsigned)fRecCount);
   1.170 +    if (style) {
   1.171 +        *style = fRecs[index].fStyle;
   1.172 +    }
   1.173 +    if (styleName) {
   1.174 +        *styleName = fRecs[index].fStyleName;
   1.175 +    }
   1.176 +}
   1.177 +
   1.178 +SkTypeface* SkFontStyleSet_FC::createTypeface(int index) {
   1.179 +    return NULL;
   1.180 +}
   1.181 +
   1.182 +SkTypeface* SkFontStyleSet_FC::matchStyle(const SkFontStyle& pattern) {
   1.183 +    return NULL;
   1.184 +}
   1.185 +
   1.186 +class SkFontMgr_fontconfig : public SkFontMgr {
   1.187 +    SkAutoTUnref<SkFontConfigInterface> fFCI;
   1.188 +    SkDataTable* fFamilyNames;
   1.189 +
   1.190 +
   1.191 +public:
   1.192 +    SkFontMgr_fontconfig(SkFontConfigInterface* fci)
   1.193 +        : fFCI(fci)
   1.194 +        , fFamilyNames(fFCI->getFamilyNames()) {}
   1.195 +
   1.196 +    virtual ~SkFontMgr_fontconfig() {
   1.197 +        SkSafeUnref(fFamilyNames);
   1.198 +    }
   1.199 +
   1.200 +protected:
   1.201 +    virtual int onCountFamilies() const SK_OVERRIDE {
   1.202 +        return fFamilyNames->count();
   1.203 +    }
   1.204 +
   1.205 +    virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
   1.206 +        familyName->set(fFamilyNames->atStr(index));
   1.207 +    }
   1.208 +
   1.209 +    virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
   1.210 +        return this->onMatchFamily(fFamilyNames->atStr(index));
   1.211 +    }
   1.212 +
   1.213 +    virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
   1.214 +        FcPattern* pattern = FcPatternCreate();
   1.215 +
   1.216 +        FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
   1.217 +#if 0
   1.218 +        FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
   1.219 +#endif
   1.220 +        FcConfigSubstitute(NULL, pattern, FcMatchPattern);
   1.221 +        FcDefaultSubstitute(pattern);
   1.222 +
   1.223 +        const char* post_config_family = get_name(pattern, FC_FAMILY);
   1.224 +
   1.225 +        FcResult result;
   1.226 +        FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
   1.227 +        if (!font_set) {
   1.228 +            FcPatternDestroy(pattern);
   1.229 +            return NULL;
   1.230 +        }
   1.231 +
   1.232 +        int count;
   1.233 +        FcPattern** match = MatchFont(font_set, post_config_family, &count);
   1.234 +        if (!match) {
   1.235 +            FcPatternDestroy(pattern);
   1.236 +            FcFontSetDestroy(font_set);
   1.237 +            return NULL;
   1.238 +        }
   1.239 +
   1.240 +        FcPatternDestroy(pattern);
   1.241 +
   1.242 +        SkTDArray<FcPattern*> trimmedMatches;
   1.243 +        for (int i = 0; i < count; ++i) {
   1.244 +            const char* justName = find_just_name(get_name(match[i], FC_FILE));
   1.245 +            if (!is_lower(*justName)) {
   1.246 +                *trimmedMatches.append() = match[i];
   1.247 +            }
   1.248 +        }
   1.249 +
   1.250 +        SkFontStyleSet_FC* sset = SkNEW_ARGS(SkFontStyleSet_FC,
   1.251 +                                             (trimmedMatches.begin(),
   1.252 +                                              trimmedMatches.count()));
   1.253 +        return sset;
   1.254 +    }
   1.255 +
   1.256 +    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
   1.257 +                                           const SkFontStyle&) const SK_OVERRIDE { return NULL; }
   1.258 +    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
   1.259 +                                         const SkFontStyle&) const SK_OVERRIDE { return NULL; }
   1.260 +
   1.261 +    virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const SK_OVERRIDE { return NULL; }
   1.262 +
   1.263 +    virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
   1.264 +        const size_t length = stream->getLength();
   1.265 +        if (!length) {
   1.266 +            return NULL;
   1.267 +        }
   1.268 +        if (length >= 1024 * 1024 * 1024) {
   1.269 +            return NULL;  // don't accept too large fonts (>= 1GB) for safety.
   1.270 +        }
   1.271 +
   1.272 +        // TODO should the caller give us the style or should we get it from freetype?
   1.273 +        SkTypeface::Style style = SkTypeface::kNormal;
   1.274 +        bool isFixedWidth = false;
   1.275 +        if (!find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
   1.276 +            return NULL;
   1.277 +        }
   1.278 +
   1.279 +        SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream));
   1.280 +        return face;
   1.281 +    }
   1.282 +
   1.283 +    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
   1.284 +        SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
   1.285 +        return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
   1.286 +    }
   1.287 +
   1.288 +    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
   1.289 +                                               unsigned styleBits) const SK_OVERRIDE {
   1.290 +        return FontConfigTypeface::LegacyCreateTypeface(NULL, familyName,
   1.291 +                                                  (SkTypeface::Style)styleBits);
   1.292 +    }
   1.293 +};
   1.294 +
   1.295 +SkFontMgr* SkFontMgr::Factory() {
   1.296 +    SkFontConfigInterface* fci = RefFCI();
   1.297 +    return fci ? SkNEW_ARGS(SkFontMgr_fontconfig, (fci)) : NULL;
   1.298 +}

mercurial