Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: chrome/browser/renderer_host/render_sandbox_host_linux.cc

Issue 2673003: [prelim] Additional renderer fontconfig support. (Closed)
Patch Set: ... Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/common/sandbox_methods_linux.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/renderer_host/render_sandbox_host_linux.cc
diff --git a/chrome/browser/renderer_host/render_sandbox_host_linux.cc b/chrome/browser/renderer_host/render_sandbox_host_linux.cc
index 453f0c753dce0b4311f134c4ae449f8f9cf07192..b4bf0234b990c046994a5a3fc179384cd3531811 100644
--- a/chrome/browser/renderer_host/render_sandbox_host_linux.cc
+++ b/chrome/browser/renderer_host/render_sandbox_host_linux.cc
@@ -6,6 +6,7 @@
#include <stdint.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/poll.h>
@@ -28,6 +29,8 @@
#include "SkFontHost_fontconfig_direct.h"
#include "SkFontHost_fontconfig_ipc.h"
+#include <fontconfig/fontconfig.h>
+
using WebKit::WebCString;
using WebKit::WebFontInfo;
using WebKit::WebUChar;
@@ -139,6 +142,8 @@ class SandboxIPCProcess {
HandleGetStyleForStrike(fd, pickle, iter, fds);
} else if (kind == LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT) {
HandleMakeSharedMemorySegment(fd, pickle, iter, fds);
+ } else if (kind == LinuxSandbox::METHOD_MATCH_WITH_FALLBACK) {
+ HandleMatchWithFallback(fd, pickle, iter, fds);
}
error:
@@ -358,6 +363,248 @@ class SandboxIPCProcess {
SendRendererReply(fds, reply, shm_fd);
}
+ void HandleMatchWithFallback(int fd, const Pickle& pickle, void* iter,
+ std::vector<int>& fds) {
+ // Unlike the other calls, for which we are an indirection in front of
+ // WebKit or Skia, this call is always made via this sandbox helper
+ // process. Therefore the fontconfig code goes in here directly.
+
+ std::string family;
+ bool is_bold, is_italic;
+ uint32 microsoft_charset;
+
+ if (!pickle.ReadString(&iter, &family) ||
+ family.empty() ||
+ !pickle.ReadBool(&iter, &is_bold) ||
+ !pickle.ReadBool(&iter, &is_italic) ||
+ !pickle.ReadUInt32(&iter, &microsoft_charset)) {
+ return;
+ }
+
+ FcLangSet* langset = FcLangSetCreate();
+ MSCharSetToFontconfig(langset, microsoft_charset);
+
+ FcPattern* pattern = FcPatternCreate();
+ FcPatternAddString(pattern, FC_FAMILY, (FcChar8*) family.c_str());
+ if (is_bold)
+ FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+ if (is_italic)
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+ FcPatternAddLangSet(pattern, FC_LANG, langset);
+ FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
+ FcConfigSubstitute(NULL, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ FcResult result;
+ FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
+ int font_fd = -1;
+ int good_enough_index = -1;
+ bool good_enough_index_set = false;
+
+ if (font_set) {
+ for (int i = 0; i < font_set->nfont; ++i) {
+ FcPattern* current = font_set->fonts[i];
+
+ // Older versions of fontconfig have a bug where they cannot select
+ // only scalable fonts so we have to manually filter the results.
+ FcBool is_scalable;
+ if (FcPatternGetBool(current, FC_SCALABLE, 0,
+ &is_scalable) != FcResultMatch ||
+ !is_scalable) {
+ continue;
+ }
+
+ FcChar8* c_filename;
+ if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
+ continue;
+
+ // We only want to return sfnt (TrueType) based fonts. We don't have a
+ // very good way of detecting this so we'll filter based on the
+ // filename.
+ bool is_sfnt = false;
+ static const char kSFNTExtensions[][5] = {
+ ".ttf", ".otc", ".TTF", ".ttc", ""
+ };
+ const size_t filename_len = strlen(reinterpret_cast<char*>(c_filename));
+ for (unsigned j = 0; ; j++) {
+ if (kSFNTExtensions[j][0] == 0) {
+ // None of the extensions matched.
+ break;
+ }
+ const size_t ext_len = strlen(kSFNTExtensions[j]);
+ if (filename_len > ext_len &&
+ memcmp(c_filename + filename_len - ext_len,
+ kSFNTExtensions[j], ext_len) == 0) {
+ is_sfnt = true;
+ break;
+ }
+ }
+
+ if (!is_sfnt)
+ continue;
+
+ // This font is good enough to pass muster, but we might be able to do
+ // better with subsequent ones.
+ if (!good_enough_index_set) {
+ good_enough_index = i;
+ good_enough_index_set = true;
+ }
+
+ FcValue matrix;
+ bool have_matrix = FcPatternGet(current, FC_MATRIX, 0, &matrix) == 0;
+
+ if (is_italic && have_matrix) {
+ // we asked for an italic font, but fontconfig is giving us a
+ // non-italic font with a transformation matrix.
+ continue;
+ }
+
+ FcValue embolden;
+ const bool have_embolden =
+ FcPatternGet(current, FC_EMBOLDEN, 0, &embolden) == 0;
+
+ if (is_bold && have_embolden) {
+ // we asked for a bold font, but fontconfig gave us a non-bold font
+ // and asked us to apply fake bolding.
+ continue;
+ }
+
+ font_fd = open(reinterpret_cast<char*>(c_filename), O_RDONLY);
+ if (font_fd >= 0)
+ break;
+ }
+ }
+
+ if (font_fd == -1 && good_enough_index_set) {
+ // We didn't find a font that we liked, so we fallback to something
+ // acceptable.
+ FcPattern* current = font_set->fonts[good_enough_index];
+ FcChar8* c_filename;
+ FcPatternGetString(current, FC_FILE, 0, &c_filename);
+ font_fd = open(reinterpret_cast<char*>(c_filename), O_RDONLY);
+ }
+
+ if (font_set)
+ FcFontSetDestroy(font_set);
+ FcPatternDestroy(pattern);
+
+ Pickle reply;
+ SendRendererReply(fds, reply, font_fd);
+
+ if (font_fd >= 0)
+ HANDLE_EINTR(close(font_fd));
+ }
+
+ // MSCharSetToFontconfig translates a Microsoft fdwCharSet value into a
+ // fontconfig language set by appending to |langset|.
+ static void MSCharSetToFontconfig(FcLangSet* langset, unsigned fdwCharSet) {
+ // We have need to translate raw fdwCharSet values into terms that fontconfig
+ // can understand. (See the description of fdwCharSet in the MSDN
+ // documentation for CreateFont:
+ // http://msdn.microsoft.com/en-us/library/dd183499(VS.85).aspx )
+ //
+ // Although the argument is /called/ 'charset', the actual values conflate
+ // character sets (which are sets of Unicode code points) and character
+ // encodings (which are algorithms for turning a series of bits into a
+ // series of code points.) Sometimes the values will name a language,
+ // sometimes they'll name an encoding. In the latter case I'm assuming that
+ // they mean the set of code points in the domain of that encoding.
+ //
+ // fontconfig deals with ISO 639-1 language codes:
+ // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
+ //
+ // So, for each of the documented fdwCharSet values I've had to take a
+ // guess at the set of ISO 639-1 languages intended.
+ //
+ // Firstly, we need to know the raw integer values for the Microsoft
+ // constants, which I'm taking from
+ // http://www.microsoft.com/mspress/books/sampchap/2344b.aspx
+
+ enum {
+ ANSI_CHARSET = 0,
+ DEFAULT_CHARSET = 1,
+ SYMBOL_CHARSET = 2,
+ MAC_CHARSET = 77,
+ SHIFTJIS_CHARSET = 128,
+ HANGUL_CHARSET = 129,
+ JOHAB_CHARSET = 130,
+ GB2312_CHARSET = 134,
+ CHINESEBIG5_CHARSET = 136,
+ GREEK_CHARSET = 161,
+ TURKISH_CHARSET = 162,
+ VIETNAMESE_CHARSET = 163,
+ HEBREW_CHARSET = 177,
+ ARABIC_CHARSET = 178,
+ BALTIC_CHARSET = 186,
+ RUSSIAN_CHARSET = 204,
+ THAI_CHARSET = 222,
+ EASTEUROPE_CHARSET = 238,
+ OEM_CHARSET = 255,
+ };
+
+ switch (fdwCharSet) {
+ case ANSI_CHARSET:
+ // These values I don't really know what to do with, so I'm going to map
+ // them to English also.
+ case DEFAULT_CHARSET:
+ case MAC_CHARSET:
+ case OEM_CHARSET:
+ case SYMBOL_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("en"));
+ break;
+ case BALTIC_CHARSET:
+ // The three baltic languages.
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("et"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lv"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lt"));
+ break;
+ case CHINESEBIG5_CHARSET:
+ case GB2312_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("zh"));
+ break;
+ case EASTEUROPE_CHARSET:
+ // A scattering of eastern European languages.
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("pl"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("cs"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("sk"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hu"));
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hr"));
+ case GREEK_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("el"));
+ break;
+ case HANGUL_CHARSET:
+ case JOHAB_CHARSET:
+ // Korean
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ko"));
+ break;
+ case RUSSIAN_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ru"));
+ break;
+ case SHIFTJIS_CHARSET:
+ // Japanese
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("jp"));
+ break;
+ case TURKISH_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("tr"));
+ break;
+ case VIETNAMESE_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("vi"));
+ break;
+ case ARABIC_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ar"));
+ break;
+ case HEBREW_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("he"));
+ break;
+ case THAI_CHARSET:
+ FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("th"));
+ break;
+ // default:
+ // Don't add any languages in that case that we don't recognise the
+ // constant.
+ }
+ }
+
void SendRendererReply(const std::vector<int>& fds, const Pickle& reply,
int reply_fd) {
struct msghdr msg;
« no previous file with comments | « no previous file | chrome/common/sandbox_methods_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698