summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--content/browser/renderer_host/render_sandbox_host_linux.cc88
-rw-r--r--content/common/font_config_ipc_linux.cc70
-rw-r--r--content/common/font_config_ipc_linux.h25
-rw-r--r--content/zygote/zygote_main_linux.cc10
-rw-r--r--skia/ext/SkFontHost_fontconfig.cpp369
-rw-r--r--skia/ext/SkFontHost_fontconfig_control.h38
-rw-r--r--skia/ext/SkFontHost_fontconfig_direct.cpp504
-rw-r--r--skia/ext/SkFontHost_fontconfig_direct.h59
-rw-r--r--skia/ext/SkFontHost_fontconfig_impl.h73
-rw-r--r--skia/ext/skia_utils_base.cc53
-rw-r--r--skia/ext/skia_utils_base.h33
-rw-r--r--skia/skia.gyp9
12 files changed, 1151 insertions, 180 deletions
diff --git a/content/browser/renderer_host/render_sandbox_host_linux.cc b/content/browser/renderer_host/render_sandbox_host_linux.cc
index 48a9912..bfd8e28 100644
--- a/content/browser/renderer_host/render_sandbox_host_linux.cc
+++ b/content/browser/renderer_host/render_sandbox_host_linux.cc
@@ -30,11 +30,10 @@
#include "content/common/font_config_ipc_linux.h"
#include "content/common/sandbox_linux.h"
#include "content/common/webkitplatformsupport_impl.h"
-#include "skia/ext/skia_utils_base.h"
+#include "skia/ext/SkFontHost_fontconfig_direct.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/linux/WebFontInfo.h"
#include "third_party/npapi/bindings/npapi_extensions.h"
-#include "third_party/skia/include/ports/SkFontConfigInterface.h"
#include "ui/gfx/font_render_params_linux.h"
using WebKit::WebCString;
@@ -60,7 +59,8 @@ class SandboxIPCProcess {
SandboxIPCProcess(int lifeline_fd, int browser_socket,
std::string sandbox_cmd)
: lifeline_fd_(lifeline_fd),
- browser_socket_(browser_socket) {
+ browser_socket_(browser_socket),
+ font_config_(new FontConfigDirect()) {
if (!sandbox_cmd.empty()) {
sandbox_cmd_.push_back(sandbox_cmd);
sandbox_cmd_.push_back(base::kFindInodeSwitch);
@@ -119,7 +119,7 @@ class SandboxIPCProcess {
// bytes long (this is the largest message type).
// 128 bytes padding are necessary so recvmsg() does not return MSG_TRUNC
// error for a maximum length message.
- char buf[FontConfigIPC::kMaxFontFamilyLength + 128];
+ char buf[FontConfigInterface::kMaxFontFamilyLength + 128];
const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
if (len == -1) {
@@ -163,58 +163,64 @@ class SandboxIPCProcess {
}
}
- int FindOrAddPath(const SkString& path) {
- int count = paths_.count();
- for (int i = 0; i < count; ++i) {
- if (path == *paths_[i])
- return i;
- }
- *paths_.append() = new SkString(path);
- return count;
- }
-
void HandleFontMatchRequest(int fd, const Pickle& pickle, PickleIterator iter,
std::vector<int>& fds) {
- uint32_t requested_style;
+ bool filefaceid_valid;
+ uint32_t filefaceid = 0;
+
+ if (!pickle.ReadBool(&iter, &filefaceid_valid))
+ return;
+ if (filefaceid_valid) {
+ if (!pickle.ReadUInt32(&iter, &filefaceid))
+ return;
+ }
+ bool is_bold, is_italic;
+ if (!pickle.ReadBool(&iter, &is_bold) ||
+ !pickle.ReadBool(&iter, &is_italic)) {
+ return;
+ }
+
+ uint32_t characters_bytes;
+ if (!pickle.ReadUInt32(&iter, &characters_bytes))
+ return;
+ const char* characters = NULL;
+ if (characters_bytes > 0) {
+ const uint32_t kMaxCharactersBytes = 1 << 10;
+ if (characters_bytes % 2 != 0 || // We expect UTF-16.
+ characters_bytes > kMaxCharactersBytes ||
+ !pickle.ReadBytes(&iter, &characters, characters_bytes))
+ return;
+ }
+
std::string family;
- if (!pickle.ReadString(&iter, &family) ||
- !pickle.ReadUInt32(&iter, &requested_style))
+ if (!pickle.ReadString(&iter, &family))
return;
- SkFontConfigInterface::FontIdentity result_identity;
- SkString result_family;
- SkTypeface::Style result_style;
- SkFontConfigInterface* fc =
- SkFontConfigInterface::GetSingletonDirectInterface();
- const bool r = fc->matchFamilyName(
- family.c_str(), static_cast<SkTypeface::Style>(requested_style),
- &result_identity, &result_family, &result_style);
+ std::string result_family;
+ unsigned result_filefaceid;
+ const bool r = font_config_->Match(
+ &result_family, &result_filefaceid, filefaceid_valid, filefaceid,
+ family, characters, characters_bytes, &is_bold, &is_italic);
Pickle reply;
if (!r) {
reply.WriteBool(false);
} else {
- // Stash away the returned path, so we can give it an ID (index)
- // which will later be given to us in a request to open the file.
- int index = FindOrAddPath(result_identity.fString);
- result_identity.fID = static_cast<uint32_t>(index);
-
reply.WriteBool(true);
- skia::WriteSkString(&reply, result_family);
- skia::WriteSkFontIdentity(&reply, result_identity);
- reply.WriteUInt32(result_style);
+ reply.WriteUInt32(result_filefaceid);
+ reply.WriteString(result_family);
+ reply.WriteBool(is_bold);
+ reply.WriteBool(is_italic);
}
SendRendererReply(fds, reply, -1);
}
void HandleFontOpenRequest(int fd, const Pickle& pickle, PickleIterator iter,
std::vector<int>& fds) {
- uint32_t index;
- if (!pickle.ReadUInt32(&iter, &index))
+ uint32_t filefaceid;
+ if (!pickle.ReadUInt32(&iter, &filefaceid))
return;
- if (index >= static_cast<uint32_t>(paths_.count()))
- return;
- const int result_fd = open(paths_[index]->c_str(), O_RDONLY);
+ const int result_fd = font_config_->Open(filefaceid);
Pickle reply;
if (result_fd == -1) {
@@ -224,6 +230,9 @@ class SandboxIPCProcess {
}
SendRendererReply(fds, reply, result_fd);
+
+ if (result_fd >= 0)
+ close(result_fd);
}
void HandleGetFontFamilyForChars(int fd, const Pickle& pickle,
@@ -650,13 +659,12 @@ class SandboxIPCProcess {
const int lifeline_fd_;
const int browser_socket_;
+ scoped_ptr<FontConfigDirect> font_config_;
std::vector<std::string> sandbox_cmd_;
scoped_ptr<WebKitPlatformSupportImpl> webkit_platform_support_;
- SkTDArray<SkString*> paths_;
};
SandboxIPCProcess::~SandboxIPCProcess() {
- paths_.deleteAll();
if (webkit_platform_support_.get())
WebKit::shutdown();
}
diff --git a/content/common/font_config_ipc_linux.cc b/content/common/font_config_ipc_linux.cc
index a5efe1b..8dd45a8 100644
--- a/content/common/font_config_ipc_linux.cc
+++ b/content/common/font_config_ipc_linux.cc
@@ -12,8 +12,6 @@
#include "base/pickle.h"
#include "base/posix/unix_domain_socket_linux.h"
-#include "skia/ext/skia_utils_base.h"
-#include "third_party/skia/include/core/SkStream.h"
namespace content {
@@ -25,21 +23,31 @@ FontConfigIPC::~FontConfigIPC() {
close(fd_);
}
-bool FontConfigIPC::matchFamilyName(const char familyName[],
- SkTypeface::Style requestedStyle,
- FontIdentity* outFontIdentity,
- SkString* outFamilyName,
- SkTypeface::Style* outStyle) {
- size_t familyNameLen = familyName ? strlen(familyName) : 0;
- if (familyNameLen > kMaxFontFamilyLength)
+bool FontConfigIPC::Match(std::string* result_family,
+ unsigned* result_filefaceid,
+ bool filefaceid_valid, unsigned filefaceid,
+ const std::string& family,
+ const void* characters, size_t characters_bytes,
+ bool* is_bold, bool* is_italic) {
+ if (family.length() > kMaxFontFamilyLength)
return false;
Pickle request;
request.WriteInt(METHOD_MATCH);
- request.WriteData(familyName, familyNameLen);
- request.WriteUInt32(requestedStyle);
+ request.WriteBool(filefaceid_valid);
+ if (filefaceid_valid)
+ request.WriteUInt32(filefaceid);
- uint8_t reply_buf[2048];
+ request.WriteBool(is_bold && *is_bold);
+ request.WriteBool(is_bold && *is_italic);
+
+ request.WriteUInt32(characters_bytes);
+ if (characters_bytes)
+ request.WriteBytes(characters, characters_bytes);
+
+ request.WriteString(family);
+
+ uint8_t reply_buf[512];
const ssize_t r = UnixDomainSocket::SendRecvMsg(fd_, reply_buf,
sizeof(reply_buf), NULL,
request);
@@ -54,29 +62,33 @@ bool FontConfigIPC::matchFamilyName(const char familyName[],
if (!result)
return false;
- SkString reply_family;
- FontIdentity reply_identity;
- uint32_t reply_style;
- if (!skia::ReadSkString(reply, &iter, &reply_family) ||
- !skia::ReadSkFontIdentity(reply, &iter, &reply_identity) ||
- !reply.ReadUInt32(&iter, &reply_style)) {
+ uint32_t reply_filefaceid;
+ std::string reply_family;
+ bool resulting_bold, resulting_italic;
+ if (!reply.ReadUInt32(&iter, &reply_filefaceid) ||
+ !reply.ReadString(&iter, &reply_family) ||
+ !reply.ReadBool(&iter, &resulting_bold) ||
+ !reply.ReadBool(&iter, &resulting_italic)) {
return false;
}
- if (outFontIdentity)
- *outFontIdentity = reply_identity;
- if (outFamilyName)
- *outFamilyName = reply_family;
- if (outStyle)
- *outStyle = static_cast<SkTypeface::Style>(reply_style);
+ if (result_filefaceid)
+ *result_filefaceid = reply_filefaceid;
+ if (result_family)
+ *result_family = reply_family;
+
+ if (is_bold)
+ *is_bold = resulting_bold;
+ if (is_italic)
+ *is_italic = resulting_italic;
return true;
}
-SkStream* FontConfigIPC::openStream(const FontIdentity& identity) {
+int FontConfigIPC::Open(unsigned filefaceid) {
Pickle request;
request.WriteInt(METHOD_OPEN);
- request.WriteUInt32(identity.fID);
+ request.WriteUInt32(filefaceid);
int result_fd = -1;
uint8_t reply_buf[256];
@@ -85,7 +97,7 @@ SkStream* FontConfigIPC::openStream(const FontIdentity& identity) {
&result_fd, request);
if (r == -1)
- return NULL;
+ return -1;
Pickle reply(reinterpret_cast<char*>(reply_buf), r);
bool result;
@@ -94,10 +106,10 @@ SkStream* FontConfigIPC::openStream(const FontIdentity& identity) {
!result) {
if (result_fd)
close(result_fd);
- return NULL;
+ return -1;
}
- return new SkFDStream(result_fd, true);
+ return result_fd;
}
} // namespace content
diff --git a/content/common/font_config_ipc_linux.h b/content/common/font_config_ipc_linux.h
index 0325dc4..2ba3375 100644
--- a/content/common/font_config_ipc_linux.h
+++ b/content/common/font_config_ipc_linux.h
@@ -6,7 +6,7 @@
#define CONTENT_COMMON_FONT_CONFIG_IPC_LINUX_H_
#include "base/compiler_specific.h"
-#include "third_party/skia/include/ports/SkFontConfigInterface.h"
+#include "skia/ext/SkFontHost_fontconfig_impl.h"
#include <string>
@@ -14,28 +14,27 @@ namespace content {
// FontConfig implementation for Skia that proxies out of process to get out
// of the sandbox. See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
-class FontConfigIPC : public SkFontConfigInterface {
+class FontConfigIPC : public FontConfigInterface {
public:
explicit FontConfigIPC(int fd);
virtual ~FontConfigIPC();
- virtual bool matchFamilyName(const char familyName[],
- SkTypeface::Style requested,
- FontIdentity* outFontIdentifier,
- SkString* outFamilyName,
- SkTypeface::Style* outStyle) OVERRIDE;
-
- virtual SkStream* openStream(const FontIdentity&) OVERRIDE;
+ // FontConfigInterface implementation.
+ virtual bool Match(std::string* result_family,
+ unsigned* result_filefaceid,
+ bool filefaceid_valid,
+ unsigned filefaceid,
+ const std::string& family,
+ const void* characters,
+ size_t characters_bytes,
+ bool* is_bold, bool* is_italic) OVERRIDE;
+ virtual int Open(unsigned filefaceid) OVERRIDE;
enum Method {
METHOD_MATCH = 0,
METHOD_OPEN = 1,
};
- enum {
- kMaxFontFamilyLength = 2048
- };
-
private:
const int fd_;
};
diff --git a/content/zygote/zygote_main_linux.cc b/content/zygote/zygote_main_linux.cc
index fd4a709..4ef3eefc 100644
--- a/content/zygote/zygote_main_linux.cc
+++ b/content/zygote/zygote_main_linux.cc
@@ -37,8 +37,8 @@
#include "crypto/nss_util.h"
#include "sandbox/linux/services/libc_urandom_override.h"
#include "sandbox/linux/suid/client/setuid_sandbox_client.h"
+#include "skia/ext/SkFontHost_fontconfig_control.h"
#include "third_party/icu/public/i18n/unicode/timezone.h"
-#include "third_party/skia/include/ports/SkFontConfigInterface.h"
#if defined(OS_LINUX)
#include <sys/epoll.h>
@@ -368,8 +368,8 @@ static bool EnterSandbox(sandbox::SetuidSandboxClient* setuid_sandbox,
return false;
PreSandboxInit();
- SkFontConfigInterface::SetGlobal(
- new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor))->unref();
+ SkiaFontConfigSetImplementation(
+ new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor));
if (setuid_sandbox->IsSuidSandboxChild()) {
// Use the SUID sandbox. This still allows the seccomp sandbox to
@@ -438,8 +438,8 @@ static bool EnterSandbox(sandbox::SetuidSandboxClient* setuid_sandbox,
return false;
PreSandboxInit();
- SkFontConfigInterface::SetGlobal(
- new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor)))->unref();
+ SkiaFontConfigSetImplementation(
+ new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor));
return true;
}
diff --git a/skia/ext/SkFontHost_fontconfig.cpp b/skia/ext/SkFontHost_fontconfig.cpp
new file mode 100644
index 0000000..a209869
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig.cpp
@@ -0,0 +1,369 @@
+/* libs/graphics/ports/SkFontHost_fontconfig.cpp
+**
+** Copyright 2008, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+// -----------------------------------------------------------------------------
+// This file provides implementations of the font resolution members of
+// SkFontHost by using the fontconfig[1] library. Fontconfig is usually found
+// on Linux systems and handles configuration, parsing and caching issues
+// involved with enumerating and matching fonts.
+//
+// [1] http://fontconfig.org
+// -----------------------------------------------------------------------------
+
+#include <map>
+#include <string>
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "base/compiler_specific.h"
+#include "third_party/skia/src/core/SkFontDescriptor.h"
+#include "SkFontHost.h"
+#include "SkStream.h"
+#include "SkFontHost_fontconfig_control.h"
+#include "SkFontHost_fontconfig_impl.h"
+#include "SkFontHost_fontconfig_direct.h"
+
+static FontConfigInterface* global_fc_impl = NULL;
+
+void SkiaFontConfigUseDirectImplementation() {
+ if (global_fc_impl)
+ delete global_fc_impl;
+ global_fc_impl = new FontConfigDirect;
+}
+
+void SkiaFontConfigSetImplementation(FontConfigInterface* font_config) {
+ if (global_fc_impl)
+ delete global_fc_impl;
+ global_fc_impl = font_config;
+}
+
+static FontConfigInterface* GetFcImpl() {
+ if (!global_fc_impl)
+ global_fc_impl = new FontConfigDirect;
+ return global_fc_impl;
+}
+
+SK_DECLARE_STATIC_MUTEX(global_remote_font_map_lock);
+static std::map<uint32_t, std::pair<uint8_t*, size_t> >* global_remote_fonts;
+
+// Initialize the map declared above. Note that its corresponding mutex must be
+// locked before calling this function.
+static void AllocateGlobalRemoteFontsMapOnce() {
+ if (!global_remote_fonts) {
+ global_remote_fonts =
+ new std::map<uint32_t, std::pair<uint8_t*, size_t> >();
+ }
+}
+
+static unsigned global_next_remote_font_id;
+
+// This is the maximum size of the font cache.
+static const unsigned kFontCacheMemoryBudget = 2 * 1024 * 1024; // 2MB
+
+// UniqueIds are encoded as (filefaceid << 8) | style
+// For system fonts, filefaceid = (fileid << 4) | face_index.
+// For remote fonts, filefaceid = fileid.
+
+static unsigned UniqueIdToFileFaceId(unsigned uniqueid)
+{
+ return uniqueid >> 8;
+}
+
+static SkTypeface::Style UniqueIdToStyle(unsigned uniqueid)
+{
+ return static_cast<SkTypeface::Style>(uniqueid & 0xff);
+}
+
+static unsigned FileFaceIdAndStyleToUniqueId(unsigned filefaceid,
+ SkTypeface::Style style)
+{
+ SkASSERT((style & 0xff) == style);
+ return (filefaceid << 8) | static_cast<int>(style);
+}
+
+static const unsigned kRemoteFontMask = 0x00800000u;
+
+static bool IsRemoteFont(unsigned filefaceid)
+{
+ return filefaceid & kRemoteFontMask;
+}
+
+class FontConfigTypeface : public SkTypeface {
+public:
+ FontConfigTypeface(Style style, uint32_t id)
+ : SkTypeface(style, id)
+ { }
+
+ virtual ~FontConfigTypeface()
+ {
+ const uint32_t id = uniqueID();
+ if (IsRemoteFont(UniqueIdToFileFaceId(id))) {
+ SkAutoMutexAcquire ac(global_remote_font_map_lock);
+ AllocateGlobalRemoteFontsMapOnce();
+ std::map<uint32_t, std::pair<uint8_t*, size_t> >::iterator iter
+ = global_remote_fonts->find(id);
+ if (iter != global_remote_fonts->end()) {
+ sk_free(iter->second.first); // remove the font on memory.
+ global_remote_fonts->erase(iter);
+ }
+ }
+ }
+};
+
+// static
+SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
+ const char familyName[],
+ SkTypeface::Style style)
+{
+ std::string resolved_family_name;
+
+ if (familyFace) {
+ // Given the fileid we can ask fontconfig for the familyname of the
+ // font.
+ const unsigned filefaceid = UniqueIdToFileFaceId(familyFace->uniqueID());
+ if (!GetFcImpl()->Match(&resolved_family_name, NULL,
+ true /* filefaceid valid */, filefaceid, "",
+ NULL, 0, NULL, NULL)) {
+ return NULL;
+ }
+ } else if (familyName) {
+ resolved_family_name = familyName;
+ }
+
+ bool bold = style & SkTypeface::kBold;
+ bool italic = style & SkTypeface::kItalic;
+ unsigned filefaceid;
+ if (!GetFcImpl()->Match(NULL, &filefaceid,
+ false, -1, /* no filefaceid */
+ resolved_family_name, NULL, 0,
+ &bold, &italic)) {
+ return NULL;
+ }
+ const SkTypeface::Style resulting_style = static_cast<SkTypeface::Style>(
+ (bold ? SkTypeface::kBold : 0) |
+ (italic ? SkTypeface::kItalic : 0));
+
+ const unsigned id = FileFaceIdAndStyleToUniqueId(filefaceid,
+ resulting_style);
+ SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (resulting_style, id));
+ return typeface;
+}
+
+// static
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream)
+{
+ if (!stream)
+ return NULL;
+
+ const size_t length = stream->read(0, 0);
+ if (!length)
+ return NULL;
+ if (length >= 1024 * 1024 * 1024)
+ return NULL; // don't accept too large fonts (>= 1GB) for safety.
+
+ uint8_t* font = (uint8_t*)sk_malloc_throw(length);
+ if (stream->read(font, length) != length) {
+ sk_free(font);
+ return NULL;
+ }
+
+ SkTypeface::Style style = static_cast<SkTypeface::Style>(0);
+ unsigned id = 0;
+ {
+ SkAutoMutexAcquire ac(global_remote_font_map_lock);
+ AllocateGlobalRemoteFontsMapOnce();
+ id = FileFaceIdAndStyleToUniqueId(
+ global_next_remote_font_id | kRemoteFontMask, style);
+
+ if (++global_next_remote_font_id >= kRemoteFontMask)
+ global_next_remote_font_id = 0;
+
+ if (!global_remote_fonts->insert(
+ std::make_pair(id, std::make_pair(font, length))).second) {
+ sk_free(font);
+ return NULL;
+ }
+ }
+
+ SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (style, id));
+ return typeface;
+}
+
+// static
+SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[])
+{
+ SkASSERT(!"SkFontHost::CreateTypefaceFromFile unimplemented");
+ return NULL;
+}
+
+uint32_t SkFontHost::NextLogicalFont(SkFontID curr, SkFontID orig) {
+ // We don't handle font fallback, WebKit does.
+ return 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+// Serialize, Deserialize need to be compatible across platforms, hence the use
+// of SkFontDescriptor.
+
+void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
+ SkFontDescriptor desc(face->style());
+
+ std::string resolved_family_name;
+
+ const unsigned filefaceid = UniqueIdToFileFaceId(face->uniqueID());
+ if (GetFcImpl()->Match(&resolved_family_name, NULL,
+ true /* filefaceid valid */, filefaceid, "", NULL, 0, NULL, NULL))
+ desc.setFamilyName(resolved_family_name.c_str());
+ else
+ desc.setFamilyName("sans-serif");
+
+ // would also like other names (see SkFontDescriptor.h)
+
+ desc.serialize(stream);
+
+ // by convention, we also write out the actual sfnt data, preceeded by
+ // a packed-length. For now we skip that, so we just write the zero.
+ stream->writePackedUInt(0);
+}
+
+SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
+ SkFontDescriptor desc(stream);
+
+ // by convention, Serialize will have also written the actual sfnt data.
+ // for now, we just want to skip it.
+ size_t size = stream->readPackedUInt();
+ stream->skip(size);
+
+ return SkFontHost::CreateTypeface(NULL, desc.getFamilyName(),
+ desc.getStyle());
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+class SkFileDescriptorStream : public SkStream {
+ public:
+ SkFileDescriptorStream(int fd) {
+ memory_ = NULL;
+ offset_ = 0;
+
+ // this ensures that if we fail in the constructor, we will safely
+ // ignore all subsequent calls to read() because we will always trim
+ // the requested size down to 0
+ length_ = 0;
+
+ struct stat st;
+ if (fstat(fd, &st))
+ return;
+
+ void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ close(fd);
+ if (memory == MAP_FAILED)
+ return;
+
+ memory_ = reinterpret_cast<uint8_t*>(memory);
+ length_ = st.st_size;
+ }
+
+ virtual ~SkFileDescriptorStream() {
+ munmap(const_cast<uint8_t*>(memory_), length_);
+ }
+
+ virtual bool rewind() OVERRIDE {
+ offset_ = 0;
+ return true;
+ }
+
+ // SkStream implementation.
+ virtual size_t read(void* buffer, size_t size) OVERRIDE {
+ if (!buffer && !size) {
+ // This is request for the length of the stream.
+ return length_;
+ }
+
+ size_t remaining = length_ - offset_;
+ if (size > remaining)
+ size = remaining;
+ if (buffer)
+ memcpy(buffer, memory_ + offset_, size);
+
+ offset_ += size;
+ return size;
+ }
+
+ virtual const void* getMemoryBase() OVERRIDE {
+ return memory_;
+ }
+
+ private:
+ const uint8_t* memory_;
+ size_t offset_, length_;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+// static
+SkStream* SkFontHost::OpenStream(uint32_t id)
+{
+ const unsigned filefaceid = UniqueIdToFileFaceId(id);
+
+ if (IsRemoteFont(filefaceid)) {
+ // remote font
+ SkAutoMutexAcquire ac(global_remote_font_map_lock);
+ AllocateGlobalRemoteFontsMapOnce();
+ std::map<uint32_t, std::pair<uint8_t*, size_t> >::const_iterator iter
+ = global_remote_fonts->find(id);
+ if (iter == global_remote_fonts->end())
+ return NULL;
+ return SkNEW_ARGS(
+ SkMemoryStream, (iter->second.first, iter->second.second));
+ }
+
+ // system font
+ const int fd = GetFcImpl()->Open(filefaceid);
+ if (fd < 0)
+ return NULL;
+
+ return SkNEW_ARGS(SkFileDescriptorStream, (fd));
+}
+
+// static
+size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
+ int32_t* index) {
+ const unsigned filefaceid = UniqueIdToFileFaceId(fontID);
+
+ if (IsRemoteFont(filefaceid))
+ return 0;
+
+ if (index) {
+ *index = filefaceid & 0xfu;
+ // 1 is a bogus return value.
+ // We had better change the signature of this function in Skia
+ // to return bool to indicate success/failure and have another
+ // out param for fileName length.
+ if (!path)
+ return 1;
+ }
+
+ if (path)
+ SkASSERT(!"SkFontHost::GetFileName does not support the font path "
+ "retrieval.");
+
+ return 0;
+}
diff --git a/skia/ext/SkFontHost_fontconfig_control.h b/skia/ext/SkFontHost_fontconfig_control.h
new file mode 100644
index 0000000..336634b
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig_control.h
@@ -0,0 +1,38 @@
+/* libs/graphics/ports/SkFontHost_fontconfig_control.h
+**
+** Copyright 2009, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef FontConfigControl_DEFINED
+#define FontConfigControl_DEFINED
+
+#include "SkPreConfig.h"
+
+class FontConfigInterface;
+
+// Sets the Skia FontHost to use the direct (non-IPC, requires filesystem
+// access) FontConfig implementation. Any previously-set FontConfigInterface
+// will be freed.
+SK_API void SkiaFontConfigUseDirectImplementation();
+
+// Sets the Skia FontHost to use the given implementation of FontConfig. This
+// is normally used to configure the IPC-based implementation to get out of
+// the sandbox.
+//
+// Ownership of the given pointer is transferred, and any previously-set
+// FontConfigInterface will be freed.
+SK_API void SkiaFontConfigSetImplementation(FontConfigInterface* font_config);
+
+#endif // FontConfigControl_DEFINED
diff --git a/skia/ext/SkFontHost_fontconfig_direct.cpp b/skia/ext/SkFontHost_fontconfig_direct.cpp
new file mode 100644
index 0000000..fb22106
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig_direct.cpp
@@ -0,0 +1,504 @@
+/* libs/graphics/ports/SkFontHost_fontconfig_direct.cpp
+**
+** Copyright 2009, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#include "skia/ext/SkFontHost_fontconfig_direct.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <fontconfig/fontconfig.h>
+
+#include "third_party/skia/include/core/SkTypeface.h"
+#include "third_party/skia/include/core/SkUtils.h"
+
+namespace {
+
+// Equivalence classes, used to match the Liberation and other fonts
+// with their metric-compatible replacements. See the discussion in
+// GetFontEquivClass().
+enum FontEquivClass
+{
+ OTHER,
+ SANS,
+ SERIF,
+ MONO,
+ SYMBOL,
+ PGOTHIC,
+ GOTHIC,
+ PMINCHO,
+ MINCHO,
+ SIMSUN,
+ NSIMSUN,
+ SIMHEI,
+ PMINGLIU,
+ MINGLIU,
+ PMINGLIUHK,
+ MINGLIUHK,
+};
+
+// Match the font name against a whilelist of fonts, returning the equivalence
+// class.
+FontEquivClass GetFontEquivClass(const char* fontname)
+{
+ // It would be nice for fontconfig to tell us whether a given suggested
+ // replacement is a "strong" match (that is, an equivalent font) or
+ // a "weak" match (that is, fontconfig's next-best attempt at finding a
+ // substitute). However, I played around with the fontconfig API for
+ // a good few hours and could not make it reveal this information.
+ //
+ // So instead, we hardcode. Initially this function emulated
+ // /etc/fonts/conf.d/30-metric-aliases.conf
+ // from my Ubuntu system, but we're better off being very conservative.
+
+ // Arimo, Tinos and Cousine are a set of fonts metric-compatible with
+ // Arial, Times New Roman and Courier New with a character repertoire
+ // much larger than Liberation. Note that Cousine is metrically
+ // compatible with Courier New, but the former is sans-serif while
+ // the latter is serif.
+
+
+ struct FontEquivMap {
+ FontEquivClass clazz;
+ const char name[40];
+ };
+
+ static const FontEquivMap kFontEquivMap[] = {
+ { SANS, "Arial" },
+ { SANS, "Arimo" },
+ { SANS, "Liberation Sans" },
+
+ { SERIF, "Times New Roman" },
+ { SERIF, "Tinos" },
+ { SERIF, "Liberation Serif" },
+
+ { MONO, "Courier New" },
+ { MONO, "Cousine" },
+ { MONO, "Liberation Mono" },
+
+ { SYMBOL, "Symbol" },
+ { SYMBOL, "Symbol Neu" },
+
+ // MS Pゴシック
+ { PGOTHIC, "MS PGothic" },
+ { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
+ "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
+ { PGOTHIC, "IPAPGothic" },
+ { PGOTHIC, "MotoyaG04Gothic" },
+
+ // MS ゴシック
+ { GOTHIC, "MS Gothic" },
+ { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 "
+ "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
+ { GOTHIC, "IPAGothic" },
+ { GOTHIC, "MotoyaG04GothicMono" },
+
+ // MS P明朝
+ { PMINCHO, "MS PMincho" },
+ { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
+ "\xe6\x98\x8e\xe6\x9c\x9d"},
+ { PMINCHO, "IPAPMincho" },
+ { PMINCHO, "MotoyaG04Mincho" },
+
+ // MS 明朝
+ { MINCHO, "MS Mincho" },
+ { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" },
+ { MINCHO, "IPAMincho" },
+ { MINCHO, "MotoyaG04MinchoMono" },
+
+ // 宋体
+ { SIMSUN, "Simsun" },
+ { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" },
+ { SIMSUN, "MSung GB18030" },
+ { SIMSUN, "Song ASC" },
+
+ // 新宋体
+ { NSIMSUN, "NSimsun" },
+ { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" },
+ { NSIMSUN, "MSung GB18030" },
+ { NSIMSUN, "N Song ASC" },
+
+ // 黑体
+ { SIMHEI, "Simhei" },
+ { SIMHEI, "\xe9\xbb\x91\xe4\xbd\x93" },
+ { SIMHEI, "MYingHeiGB18030" },
+ { SIMHEI, "MYingHeiB5HK" },
+
+ // 新細明體
+ { PMINGLIU, "PMingLiU"},
+ { PMINGLIU, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
+ { PMINGLIU, "MSung B5HK"},
+
+ // 細明體
+ { MINGLIU, "MingLiU"},
+ { MINGLIU, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
+ { MINGLIU, "MSung B5HK"},
+
+ // 新細明體
+ { PMINGLIUHK, "PMingLiU_HKSCS"},
+ { PMINGLIUHK, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
+ { PMINGLIUHK, "MSung B5HK"},
+
+ // 細明體
+ { MINGLIUHK, "MingLiU_HKSCS"},
+ { MINGLIUHK, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
+ { MINGLIUHK, "MSung B5HK"},
+ };
+
+ static const size_t kFontCount =
+ sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]);
+
+ // TODO(jungshik): If this loop turns out to be hot, turn
+ // the array to a static (hash)map to speed it up.
+ for (size_t i = 0; i < kFontCount; ++i) {
+ if (strcasecmp(kFontEquivMap[i].name, fontname) == 0)
+ return kFontEquivMap[i].clazz;
+ }
+ return OTHER;
+}
+
+
+// Return true if |font_a| and |font_b| are visually and at the metrics
+// level interchangeable.
+bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b)
+{
+ FontEquivClass class_a = GetFontEquivClass(font_a);
+ FontEquivClass class_b = GetFontEquivClass(font_b);
+
+ return class_a != OTHER && class_a == class_b;
+}
+
+inline unsigned FileFaceIdToFileId(unsigned filefaceid)
+{
+ return filefaceid >> 4;
+}
+
+inline unsigned FileIdAndFaceIndexToFileFaceId(unsigned fileid, int face_index)
+{
+ SkASSERT((face_index & 0xfu) == face_index);
+ return (fileid << 4) | face_index;
+}
+
+// Normally we only return exactly the font asked for. In last-resort
+// cases, the request either doesn't specify a font or is one of the
+// basic font names like "Sans", "Serif" or "Monospace". This function
+// tells you whether a given request is for such a fallback.
+bool IsFallbackFontAllowed(const std::string& family) {
+ const char* family_cstr = family.c_str();
+ return family.empty() ||
+ strcasecmp(family_cstr, "sans") == 0 ||
+ strcasecmp(family_cstr, "serif") == 0 ||
+ strcasecmp(family_cstr, "monospace") == 0;
+}
+
+// Find matching font from |font_set| for the given font family.
+FcPattern* MatchFont(FcFontSet* font_set,
+ FcChar8* post_config_family,
+ const std::string& family) {
+ // Older versions of fontconfig have a bug where they cannot select
+ // only scalable fonts so we have to manually filter the results.
+ FcPattern* match = NULL;
+ for (int i = 0; i < font_set->nfont; ++i) {
+ FcPattern* current = font_set->fonts[i];
+ FcBool is_scalable;
+
+ if (FcPatternGetBool(current, FC_SCALABLE, 0,
+ &is_scalable) != FcResultMatch ||
+ !is_scalable) {
+ continue;
+ }
+
+ // fontconfig can also return fonts which are unreadable
+ FcChar8* c_filename;
+ if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
+ continue;
+
+ if (access(reinterpret_cast<char*>(c_filename), R_OK) != 0)
+ continue;
+
+ match = current;
+ break;
+ }
+
+ if (match && !IsFallbackFontAllowed(family)) {
+ bool acceptable_substitute = false;
+ for (int id = 0; id < 255; ++id) {
+ FcChar8* post_match_family;
+ if (FcPatternGetString(match, FC_FAMILY, id, &post_match_family) !=
+ FcResultMatch)
+ break;
+ acceptable_substitute =
+ (strcasecmp(reinterpret_cast<char*>(post_config_family),
+ reinterpret_cast<char*>(post_match_family)) == 0 ||
+ // Workaround for Issue 12530:
+ // requested family: "Bitstream Vera Sans"
+ // post_config_family: "Arial"
+ // post_match_family: "Bitstream Vera Sans"
+ // -> We should treat this case as a good match.
+ strcasecmp(family.c_str(),
+ reinterpret_cast<char*>(post_match_family)) == 0) ||
+ IsMetricCompatibleReplacement(family.c_str(),
+ reinterpret_cast<char*>(post_match_family));
+ if (acceptable_substitute)
+ break;
+ }
+ if (!acceptable_substitute)
+ return NULL;
+ }
+
+ return match;
+}
+
+// Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
+bool GetFontProperties(FcPattern* font,
+ std::string* font_family,
+ bool* is_bold,
+ bool* is_italic) {
+ FcChar8* c_family;
+ if (FcPatternGetString(font, FC_FAMILY, 0, &c_family))
+ return false;
+
+ int resulting_bold;
+ if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
+ resulting_bold = FC_WEIGHT_NORMAL;
+
+ int resulting_italic;
+ if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
+ resulting_italic = FC_SLANT_ROMAN;
+
+ // If we ask for an italic font, fontconfig might take a roman font and set
+ // the undocumented property FC_MATRIX to a skew matrix. It'll then say
+ // that the font is italic or oblique. So, if we see a matrix, we don't
+ // believe that it's italic.
+ FcValue matrix;
+ const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0;
+
+ // If we ask for an italic font, fontconfig might take a roman font and set
+ // FC_EMBOLDEN.
+ FcValue embolden;
+ const bool have_embolden =
+ FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) == 0;
+
+ *is_bold = resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden;
+ *is_italic = resulting_italic > FC_SLANT_ROMAN && !have_matrix;
+ *font_family = reinterpret_cast<char*>(c_family);
+
+ return true;
+}
+
+} // anonymous namespace
+
+FontConfigDirect::FontConfigDirect()
+ : next_file_id_(0) {
+ FcInit();
+}
+
+FontConfigDirect::~FontConfigDirect() {
+}
+
+bool FontConfigDirect::Match(std::string* result_family,
+ unsigned* result_filefaceid,
+ bool filefaceid_valid, unsigned filefaceid,
+ const std::string& family,
+ const void* data, size_t characters_bytes,
+ bool* is_bold, bool* is_italic) {
+ if (family.length() > kMaxFontFamilyLength)
+ return false;
+
+ SkAutoMutexAcquire ac(mutex_);
+
+ // Given |family|, |is_bold| and |is_italic| but not |data|, the result will
+ // be a function of these three parameters, and thus eligible for caching.
+ // This is the fast path for |SkTypeface::CreateFromName()|.
+ bool eligible_for_cache = !family.empty() && is_bold && is_italic && !data;
+ if (eligible_for_cache) {
+ int style = (*is_bold ? SkTypeface::kBold : 0 ) |
+ (*is_italic ? SkTypeface::kItalic : 0);
+ FontMatchKey key = FontMatchKey(family, style);
+ const std::map<FontMatchKey, FontMatch>::const_iterator i =
+ font_match_cache_.find(key);
+ if (i != font_match_cache_.end()) {
+ *is_bold = i->second.is_bold;
+ *is_italic = i->second.is_italic;
+ if (result_family)
+ *result_family = i->second.family;
+ if (result_filefaceid)
+ *result_filefaceid = i->second.filefaceid;
+ return true;
+ }
+ }
+
+ FcPattern* pattern = FcPatternCreate();
+
+ if (filefaceid_valid) {
+ const std::map<unsigned, std::string>::const_iterator
+ i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid));
+ if (i == fileid_to_filename_.end()) {
+ FcPatternDestroy(pattern);
+ return false;
+ }
+ int face_index = filefaceid & 0xfu;
+ FcPatternAddString(pattern, FC_FILE,
+ reinterpret_cast<const FcChar8*>(i->second.c_str()));
+ // face_index is added only when family is empty because it is not
+ // necessary to uniquiely identify a font if both file and
+ // family are given.
+ if (family.empty())
+ FcPatternAddInteger(pattern, FC_INDEX, face_index);
+ }
+ if (!family.empty()) {
+ FcPatternAddString(pattern, FC_FAMILY, (FcChar8*) family.c_str());
+ }
+
+ FcCharSet* charset = NULL;
+ if (data) {
+ charset = FcCharSetCreate();
+ const uint16_t* chars = (const uint16_t*)data;
+ const uint16_t* stop = chars + characters_bytes/2;
+ while (chars < stop) {
+ FcCharSetAddChar(charset, SkUTF16_NextUnichar(&chars));
+ }
+ FcPatternAddCharSet(pattern, FC_CHARSET, charset);
+ FcCharSetDestroy(charset); // pattern now owns it.
+ }
+
+ FcPatternAddInteger(pattern, FC_WEIGHT,
+ is_bold && *is_bold ? FC_WEIGHT_BOLD
+ : FC_WEIGHT_NORMAL);
+ FcPatternAddInteger(pattern, FC_SLANT,
+ is_italic && *is_italic ? FC_SLANT_ITALIC
+ : FC_SLANT_ROMAN);
+ FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
+
+ FcConfigSubstitute(NULL, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ // Font matching:
+ // CSS often specifies a fallback list of families:
+ // font-family: a, b, c, serif;
+ // However, fontconfig will always do its best to find *a* font when asked
+ // for something so we need a way to tell if the match which it has found is
+ // "good enough" for us. Otherwise, we can return NULL which gets piped up
+ // and lets WebKit know to try the next CSS family name. However, fontconfig
+ // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we
+ // wish to support that.
+ //
+ // Thus, if a specific family is requested we set @family_requested. Then we
+ // record two strings: the family name after config processing and the
+ // family name after resolving. If the two are equal, it's a good match.
+ //
+ // So consider the case where a user has mapped Arial to Helvetica in their
+ // config.
+ // requested family: "Arial"
+ // post_config_family: "Helvetica"
+ // post_match_family: "Helvetica"
+ // -> good match
+ //
+ // and for a missing font:
+ // requested family: "Monaco"
+ // post_config_family: "Monaco"
+ // post_match_family: "Times New Roman"
+ // -> BAD match
+ //
+ // However, we special-case fallback fonts; see IsFallbackFontAllowed().
+ FcChar8* post_config_family;
+ FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family);
+
+ FcResult result;
+ FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
+ if (!font_set) {
+ FcPatternDestroy(pattern);
+ return false;
+ }
+
+ FcPattern* match = MatchFont(font_set, post_config_family, family);
+ if (!match) {
+ FcPatternDestroy(pattern);
+ FcFontSetDestroy(font_set);
+ return false;
+ }
+
+ FcPatternDestroy(pattern);
+
+ FcChar8* c_filename;
+ if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) {
+ FcFontSetDestroy(font_set);
+ return false;
+ }
+ int face_index;
+ if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) {
+ FcFontSetDestroy(font_set);
+ return false;
+ }
+
+ FontMatch font_match;
+ if (filefaceid_valid) {
+ font_match.filefaceid = filefaceid;
+ } else {
+ unsigned out_fileid;
+ const std::string filename(reinterpret_cast<char*>(c_filename));
+ const std::map<std::string, unsigned>::const_iterator
+ i = filename_to_fileid_.find(filename);
+ if (i == filename_to_fileid_.end()) {
+ out_fileid = next_file_id_++;
+ filename_to_fileid_[filename] = out_fileid;
+ fileid_to_filename_[out_fileid] = filename;
+ } else {
+ out_fileid = i->second;
+ }
+ // fileid stored in filename_to_fileid_ and fileid_to_filename_ is
+ // unique only up to the font file. We have to encode face_index for
+ // the out param.
+ font_match.filefaceid =
+ FileIdAndFaceIndexToFileFaceId(out_fileid, face_index);
+ }
+
+ bool success = GetFontProperties(match,
+ &font_match.family,
+ &font_match.is_bold,
+ &font_match.is_italic);
+ FcFontSetDestroy(font_set);
+
+ if (success) {
+ // If eligible, cache the result of the matching.
+ if (eligible_for_cache) {
+ int style = (*is_bold ? SkTypeface::kBold : 0 ) |
+ (*is_italic ? SkTypeface::kItalic : 0);
+ font_match_cache_[FontMatchKey(family, style)] = font_match;
+ }
+
+ if (result_family)
+ *result_family = font_match.family;
+ if (result_filefaceid)
+ *result_filefaceid = font_match.filefaceid;
+ if (is_bold)
+ *is_bold = font_match.is_bold;
+ if (is_italic)
+ *is_italic = font_match.is_italic;
+ }
+
+ return success;
+}
+
+int FontConfigDirect::Open(unsigned filefaceid) {
+ SkAutoMutexAcquire ac(mutex_);
+ const std::map<unsigned, std::string>::const_iterator
+ i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid));
+ if (i == fileid_to_filename_.end())
+ return -1;
+
+ return open(i->second.c_str(), O_RDONLY);
+}
diff --git a/skia/ext/SkFontHost_fontconfig_direct.h b/skia/ext/SkFontHost_fontconfig_direct.h
new file mode 100644
index 0000000..ae97f3d
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig_direct.h
@@ -0,0 +1,59 @@
+/* libs/graphics/ports/SkFontHost_fontconfig_direct.h
+**
+** Copyright 2009, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef FontConfigDirect_DEFINED
+#define FontConfigDirect_DEFINED
+
+#include <map>
+#include <string>
+
+#include "SkThread.h"
+#include "SkFontHost_fontconfig_impl.h"
+
+class SK_API FontConfigDirect : public FontConfigInterface {
+ public:
+ FontConfigDirect();
+ virtual ~FontConfigDirect();
+
+ // FontConfigInterface implementation. Thread safe.
+ virtual bool Match(std::string* result_family, unsigned* result_filefaceid,
+ bool filefaceid_valid, unsigned filefaceid,
+ const std::string& family,
+ const void* characters, size_t characters_bytes,
+ bool* is_bold, bool* is_italic) SK_OVERRIDE;
+ virtual int Open(unsigned filefaceid) SK_OVERRIDE;
+
+ private:
+ SkMutex mutex_;
+ // fileid stored in two maps below are unique per font file.
+ std::map<unsigned, std::string> fileid_to_filename_;
+ std::map<std::string, unsigned> filename_to_fileid_;
+
+ // Cache of |family,style| to |FontMatch| to minimize querying FontConfig.
+ typedef std::pair<std::string, int> FontMatchKey;
+ struct FontMatch {
+ std::string family;
+ bool is_bold;
+ bool is_italic;
+ unsigned filefaceid;
+ };
+ std::map<FontMatchKey, FontMatch> font_match_cache_;
+
+ unsigned next_file_id_;
+};
+
+#endif // FontConfigDirect_DEFINED
diff --git a/skia/ext/SkFontHost_fontconfig_impl.h b/skia/ext/SkFontHost_fontconfig_impl.h
new file mode 100644
index 0000000..d6f81796
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig_impl.h
@@ -0,0 +1,73 @@
+/* libs/graphics/ports/SkFontHost_fontconfig_impl.h
+**
+** Copyright 2009, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+/* The SkFontHost_fontconfig code requires an implementation of an abstact
+ * fontconfig interface. We do this because sometimes fontconfig is not
+ * directly availible and this provides an ability to change the fontconfig
+ * implementation at run-time.
+ */
+
+#ifndef FontConfigInterface_DEFINED
+#define FontConfigInterface_DEFINED
+
+#include <string>
+
+class FontConfigInterface {
+ public:
+ virtual ~FontConfigInterface() { }
+
+ /** Performs config match
+ *
+ * @param result_family (optional, set to NULL to ignore, output)
+ * on success, set to the resulting family name.
+ * @param result_filefaceid (optional, set to NULL to ignore, output)
+ * on success, set to the resulting fileface id.
+ * @param filefaceid_valid if true, then |filefaceid| is valid
+ * @param filefaceid the filefaceid (as returned by this function)
+ * which we are trying to match.
+ * @param family (optional) the family of the font that we are trying to
+ * match. If the length of the |family| is greater then
+ * kMaxFontFamilyLength, this function should immediately return false.
+ * @param characters (optional) UTF-16 characters the font must cover.
+ * @param characters_bytes (optional) number of bytes in |characters|
+ * @param is_bold (optional, set to NULL to ignore, in/out)
+ * @param is_italic (optional, set to NULL to ignore, in/out)
+ * @return true iff successful.
+ * Note that |filefaceid| uniquely identifies <font file, face_index) :
+ * system font: filefaceid =
+ * (fileid(unique per font file) << 4 | face_index)
+ * remote font: filefaceid = fileid
+ */
+ virtual bool Match(
+ std::string* result_family,
+ unsigned* result_filefaceid,
+ bool filefaceid_valid,
+ unsigned filefaceid,
+ const std::string& family,
+ const void* characters,
+ size_t characters_bytes,
+ bool* is_bold,
+ bool* is_italic) = 0;
+
+ /** Open a font file given the filefaceid as returned by Match.
+ */
+ virtual int Open(unsigned filefaceid) = 0;
+
+ static const unsigned kMaxFontFamilyLength = 2048;
+};
+
+#endif // FontConfigInterface_DEFINED
diff --git a/skia/ext/skia_utils_base.cc b/skia/ext/skia_utils_base.cc
deleted file mode 100644
index 07c06bb..0000000
--- a/skia/ext/skia_utils_base.cc
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "skia/ext/skia_utils_base.h"
-
-namespace skia {
-
-bool ReadSkString(const Pickle& pickle, PickleIterator* iter, SkString* str) {
- int reply_length;
- const char* reply_text;
-
- if (!pickle.ReadData(iter, &reply_text, &reply_length))
- return false;
-
- if (str)
- str->set(reply_text, reply_length);
- return true;
-}
-
-bool ReadSkFontIdentity(const Pickle& pickle, PickleIterator* iter,
- SkFontConfigInterface::FontIdentity* identity) {
- uint32_t reply_id;
- uint32_t reply_ttcIndex;
- int reply_length;
- const char* reply_text;
-
- if (!pickle.ReadUInt32(iter, &reply_id) ||
- !pickle.ReadUInt32(iter, &reply_ttcIndex) ||
- !pickle.ReadData(iter, &reply_text, &reply_length))
- return false;
-
- if (identity) {
- identity->fID = reply_id;
- identity->fTTCIndex = reply_ttcIndex;
- identity->fString.set(reply_text, reply_length);
- }
- return true;
-}
-
-bool WriteSkString(Pickle* pickle, const SkString& str) {
- return pickle->WriteData(str.c_str(), str.size());
-}
-
-bool WriteSkFontIdentity(Pickle* pickle,
- const SkFontConfigInterface::FontIdentity& identity) {
- return pickle->WriteUInt32(identity.fID) &&
- pickle->WriteUInt32(identity.fTTCIndex) &&
- WriteSkString(pickle, identity.fString);
-}
-
-} // namespace skia
-
diff --git a/skia/ext/skia_utils_base.h b/skia/ext/skia_utils_base.h
deleted file mode 100644
index 2f94e40..0000000
--- a/skia/ext/skia_utils_base.h
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef SKIA_EXT_SKIA_UTILS_BASE_H_
-#define SKIA_EXT_SKIA_UTILS_BASE_H_
-
-#include "base/pickle.h"
-#include "third_party/skia/include/ports/SkFontConfigInterface.h"
-
-namespace skia {
-
-// Return true if the pickle/iterator contains a string. If so, and if str
-// is not null, copy that string into str.
-SK_API bool ReadSkString(const Pickle& pickle, PickleIterator* iter,
- SkString* str);
-
-// Return true if the pickle/iterator contains a FontIdentity. If so, and if
-// identity is not null, copy it into identity.
-SK_API bool ReadSkFontIdentity(const Pickle& pickle, PickleIterator* iter,
- SkFontConfigInterface::FontIdentity* identity);
-
-// Return true if str can be written into the request pickle.
-SK_API bool WriteSkString(Pickle* pickle, const SkString& str);
-
-// Return true if identity can be written into the request pickle.
-SK_API bool WriteSkFontIdentity(Pickle* pickle,
- const SkFontConfigInterface::FontIdentity& identity);
-
-} // namespace skia
-
-#endif // SKIA_EXT_SKIA_UTILS_WIN_H_
-
diff --git a/skia/skia.gyp b/skia/skia.gyp
index 71b34f2..340e64e0 100644
--- a/skia/skia.gyp
+++ b/skia/skia.gyp
@@ -182,8 +182,6 @@
'ext/skia_sandbox_support_win.h',
'ext/skia_sandbox_support_win.cc',
'ext/skia_trace_shim.h',
- 'ext/skia_utils_base.cc',
- 'ext/skia_utils_base.h',
'ext/skia_utils_ios.mm',
'ext/skia_utils_ios.h',
'ext/skia_utils_mac.mm',
@@ -376,11 +374,8 @@
'-Wno-unused-function',
],
'sources': [
- '../third_party/skia/src/ports/SkFontHost_fontconfig.cpp',
- '../third_party/skia/src/ports/SkFontConfigInterface_direct.cpp',
- ],
- 'sources!': [
- '../third_party/skia/src/ports/SkFontHost_tables.cpp',
+ 'ext/SkFontHost_fontconfig.cpp',
+ 'ext/SkFontHost_fontconfig_direct.cpp',
],
'defines': [
# 'SK_USE_COLOR_LUMINANCE',