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 34 #include <fontconfig/fontconfig.h> 35 #include <string.h> 36 #include <unicode/utf16.h> 37 38 namespace WebKit { 39 40 WebCString WebFontInfo::familyForChars(const WebUChar* characters, size_t numCharacters) 41 { 42 FcCharSet* cset = FcCharSetCreate(); 43 for (size_t i = 0; i < numCharacters; ++i) { 44 if (U16_IS_SURROGATE(characters[i]) 45 && U16_IS_SURROGATE_LEAD(characters[i]) 46 && i != numCharacters - 1 47 && U16_IS_TRAIL(characters[i + 1])) { 48 FcCharSetAddChar(cset, U16_GET_SUPPLEMENTARY(characters[i], characters[i+1])); 49 i++; 50 } else 51 FcCharSetAddChar(cset, characters[i]); 52 } 53 FcPattern* pattern = FcPatternCreate(); 54 55 FcValue fcvalue; 56 fcvalue.type = FcTypeCharSet; 57 fcvalue.u.c = cset; 58 FcPatternAdd(pattern, FC_CHARSET, fcvalue, 0); 59 60 fcvalue.type = FcTypeBool; 61 fcvalue.u.b = FcTrue; 62 FcPatternAdd(pattern, FC_SCALABLE, fcvalue, 0); 63 64 FcConfigSubstitute(0, pattern, FcMatchPattern); 65 FcDefaultSubstitute(pattern); 66 67 FcResult result; 68 FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result); 69 FcPatternDestroy(pattern); 70 FcCharSetDestroy(cset); 71 72 if (!fontSet) 73 return WebCString(); 74 75 // Older versions of fontconfig have a bug where they cannot select 76 // only scalable fonts so we have to manually filter the results. 77 for (int i = 0; i < fontSet->nfont; ++i) { 78 FcPattern* current = fontSet->fonts[i]; 79 FcBool isScalable; 80 81 if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch 82 || !isScalable) 83 continue; 84 85 // fontconfig can also return fonts which are unreadable 86 FcChar8* cFilename; 87 if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch) 88 continue; 89 90 if (access(reinterpret_cast<char*>(cFilename), R_OK)) 91 continue; 92 93 FcChar8* family; 94 WebCString result; 95 if (FcPatternGetString(current, FC_FAMILY, 0, &family) == FcResultMatch) { 96 const char* charFamily = reinterpret_cast<char*>(family); 97 result = WebCString(charFamily, strlen(charFamily)); 98 } 99 FcFontSetDestroy(fontSet); 100 return result; 101 } 102 103 FcFontSetDestroy(fontSet); 104 return WebCString(); 105 } 106 107 } // namespace WebKit 108