0

Add a font API to Pepper and implement on Linux based on agl's code from http://codereview.chromium.org/2673003.

Review URL: http://codereview.chromium.org/2794004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49599 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
jam@chromium.org
2010-06-11 22:25:54 +00:00
parent d4d1b878fd
commit adb072f92d
10 changed files with 494 additions and 1 deletions

@ -32,6 +32,9 @@ include_rules = [
# Allow inclusion of Mozilla interface headers.
"+third_party/mozilla",
# Allow inclusion of NPAPI interface headers.
"+third_party/npapi",
# Allow inclusion of tcmalloc header.
"+third_party/tcmalloc",

@ -5,6 +5,7 @@
#include "chrome/browser/renderer_host/render_sandbox_host_linux.h"
#include <fcntl.h>
#include <fontconfig/fontconfig.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/uio.h>
@ -25,6 +26,7 @@
#include "base/string_util.h"
#include "base/unix_domain_socket_posix.h"
#include "chrome/common/sandbox_methods_linux.h"
#include "third_party/npapi/bindings/npapi_extensions.h"
#include "third_party/WebKit/WebKit/chromium/public/gtk/WebFontInfo.h"
#include "SkFontHost_fontconfig_direct.h"
@ -141,6 +143,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:
@ -360,6 +364,225 @@ 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 face;
bool is_bold, is_italic;
uint32 charset;
if (!pickle.ReadString(&iter, &face) ||
face.empty() ||
!pickle.ReadBool(&iter, &is_bold) ||
!pickle.ReadBool(&iter, &is_italic) ||
!pickle.ReadUInt32(&iter, &charset)) {
return;
}
FcLangSet* langset = FcLangSetCreate();
MSCharSetToFontconfig(langset, charset);
FcPattern* pattern = FcPatternCreate();
// TODO(agl): FC_FAMILy needs to change
FcPatternAddString(pattern, FC_FAMILY, (FcChar8*) face.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 charset identifier to 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.
switch (fdwCharSet) {
case NPCharsetAnsi:
// These values I don't really know what to do with, so I'm going to map
// them to English also.
case NPCharsetDefault:
case NPCharsetMac:
case NPCharsetOEM:
case NPCharsetSymbol:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("en"));
break;
case NPCharsetBaltic:
// 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 NPCharsetChineseBIG5:
case NPCharsetGB2312:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("zh"));
break;
case NPCharsetEastEurope:
// 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 NPCharsetGreek:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("el"));
break;
case NPCharsetHangul:
case NPCharsetJohab:
// Korean
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ko"));
break;
case NPCharsetRussian:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ru"));
break;
case NPCharsetShiftJIS:
// Japanese
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("jp"));
break;
case NPCharsetTurkish:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("tr"));
break;
case NPCharsetVietnamese:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("vi"));
break;
case NPCharsetArabic:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ar"));
break;
case NPCharsetHebrew:
FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("he"));
break;
case NPCharsetThai:
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;

@ -17,6 +17,7 @@ class LinuxSandbox {
METHOD_GET_CHILD_WITH_INODE = 34,
METHOD_GET_STYLE_FOR_STRIKE = 35,
METHOD_MAKE_SHARED_MEMORY_SEGMENT = 36,
METHOD_MATCH_WITH_FALLBACK = 37,
};
};

@ -4,8 +4,12 @@
#include "chrome/renderer/renderer_sandbox_support_linux.h"
#include <sys/stat.h>
#include "base/eintr_wrapper.h"
#include "base/global_descriptors_posix.h"
#include "base/pickle.h"
#include "base/scoped_ptr.h"
#include "base/unix_domain_socket_posix.h"
#include "chrome/common/chrome_descriptors.h"
#include "chrome/common/sandbox_methods_linux.h"
@ -18,7 +22,8 @@ static int GetSandboxFD() {
namespace renderer_sandbox_support {
std::string getFontFamilyForCharacters(const uint16_t* utf16, size_t num_utf16) {
std::string getFontFamilyForCharacters(const uint16_t* utf16,
size_t num_utf16) {
Pickle request;
request.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS);
request.WriteInt(num_utf16);
@ -87,4 +92,96 @@ int MakeSharedMemorySegmentViaIPC(size_t length) {
return result_fd;
}
int MatchFontWithFallback(const std::string& face, bool bold,
bool italic, NPCharset charset) {
Pickle request;
request.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK);
request.WriteString(face);
request.WriteBool(bold);
request.WriteBool(italic);
request.WriteUInt32(charset);
uint8_t reply_buf[64];
int fd = -1;
base::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf),
&fd, request);
return fd;
}
bool GetFontTable(int fd, uint32_t table, uint8_t* output,
size_t* output_length) {
if (table == 0) {
struct stat st;
if (fstat(fd, &st) < 0)
return false;
size_t length = st.st_size;
if (!output) {
*output_length = length;
return true;
}
if (*output_length < length)
return false;
*output_length = length;
ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0));
if (n != static_cast<ssize_t>(length))
return false;
return true;
}
unsigned num_tables;
uint8_t num_tables_buf[2];
ssize_t n = HANDLE_EINTR(pread(fd, &num_tables_buf, sizeof(num_tables_buf),
4 /* skip the font type */));
if (n != sizeof(num_tables_buf))
return false;
num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 |
num_tables_buf[1];
// The size in bytes of an entry in the table directory.
static const unsigned kTableEntrySize = 16;
scoped_array<uint8_t> table_entries(
new uint8_t[num_tables * kTableEntrySize]);
n = HANDLE_EINTR(pread(fd, table_entries.get(), num_tables * kTableEntrySize,
12 /* skip the SFNT header */));
if (n != static_cast<ssize_t>(num_tables * kTableEntrySize))
return false;
size_t offset;
size_t length = 0;
for (unsigned i = 0; i < num_tables; i++) {
const uint8_t* entry = table_entries.get() + i * kTableEntrySize;
if (memcmp(entry, &table, sizeof(table)) == 0) {
offset = static_cast<size_t>(entry[8]) << 24 |
static_cast<size_t>(entry[9]) << 16 |
static_cast<size_t>(entry[10]) << 8 |
static_cast<size_t>(entry[11]);
length = static_cast<size_t>(entry[12]) << 24 |
static_cast<size_t>(entry[13]) << 16 |
static_cast<size_t>(entry[14]) << 8 |
static_cast<size_t>(entry[15]);
break;
}
}
if (!length)
return false;
if (!output) {
*output_length = length;
return true;
}
if (*output_length < length)
return false;
*output_length = length;
n = HANDLE_EINTR(pread(fd, output, length, offset));
if (n != static_cast<ssize_t>(length))
return false;
return true;
}
} // namespace render_sandbox_support

@ -9,6 +9,8 @@
#include <string>
#include "third_party/npapi/bindings/npapi_extensions.h"
namespace WebKit {
struct WebFontRenderStyle;
}
@ -30,6 +32,24 @@ void getRenderStyleForStrike(const char* family, int sizeAndStyle,
// Returns a file descriptor for a shared memory segment.
int MakeSharedMemorySegmentViaIPC(size_t length);
// Return a read-only file descriptor to the font which best matches the given
// properties or -1 on failure.
// charset: specifies the language(s) that the font must cover. See
// render_sandbox_host_linux.cc for more information.
int MatchFontWithFallback(const std::string& face, bool bold,
bool italic, NPCharset charset);
// GetFontTable loads a specified font table from an open SFNT file.
// fd: a file descriptor to the SFNT file. The position doesn't matter.
// table: the table in *big-endian* format, or 0 for the whole font file.
// output: a buffer of size output_length that gets the data. can be 0, in
// which case output_length will be set to the required size in bytes.
// output_length: size of output, if it's not 0.
//
// returns: true on success.
bool GetFontTable(int fd, uint32_t table, uint8_t* output,
size_t* output_length);
}; // namespace render_sandbox_support
#endif // CHROME_RENDERER_RENDERER_SANDBOX_SUPPORT_LINUX_H_

@ -9,6 +9,10 @@
#include <string>
#include <vector>
#if defined(OS_LINUX)
#include <unistd.h>
#endif
#include "base/file_util.h"
#include "base/keyboard_codes.h"
#if defined(OS_MACOSX)
@ -27,6 +31,9 @@
#include "chrome/common/render_messages.h"
#include "chrome/renderer/pepper_widget.h"
#include "chrome/renderer/render_thread.h"
#if defined(OS_LINUX)
#include "chrome/renderer/renderer_sandbox_support_linux.h"
#endif
#include "chrome/renderer/webplugin_delegate_proxy.h"
#include "gfx/blit.h"
#if defined(OS_WIN)
@ -426,6 +433,58 @@ bool WebPluginDelegatePepper::SetCursor(NPCursorType type) {
return true;
}
NPError NPMatchFontWithFallback(NPP instance,
const NPFontDescription* description,
NPFontID* id) {
#if defined(OS_LINUX)
int fd = renderer_sandbox_support::MatchFontWithFallback(
description->face, description->weight >= 700, description->italic,
description->charset);
if (fd == -1)
return NPERR_GENERIC_ERROR;
*id = fd;
return NPERR_NO_ERROR;
#else
NOTIMPLEMENTED();
return NPERR_GENERIC_ERROR;
#endif
}
NPError GetFontTable(NPP instance,
NPFontID id,
uint32_t table,
void* output,
size_t* output_length) {
#if defined(OS_LINUX)
bool rv = renderer_sandbox_support::GetFontTable(
id, table, static_cast<uint8_t*>(output), output_length);
return rv ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
#else
NOTIMPLEMENTED();
return NPERR_GENERIC_ERROR;
#endif
}
NPError NPDestroyFont(NPP instance, NPFontID id) {
#if defined(OS_LINUX)
close(id);
return NPERR_NO_ERROR;
#else
NOTIMPLEMENTED();
return NPERR_GENERIC_ERROR;
#endif
}
NPFontExtensions g_font_extensions = {
NPMatchFontWithFallback,
GetFontTable,
NPDestroyFont
};
NPFontExtensions* WebPluginDelegatePepper::GetFontExtensions() {
return &g_font_extensions;
}
void WebPluginDelegatePepper::Zoom(int factor) {
NPPExtensions* extensions = NULL;
instance()->NPP_GetValue(NPPVPepperExtensions, &extensions);

@ -94,6 +94,7 @@ class WebPluginDelegatePepper : public webkit_glue::WebPluginDelegate,
void* user_data);
virtual NPWidgetExtensions* GetWidgetExtensions();
virtual bool SetCursor(NPCursorType type);
virtual NPFontExtensions* GetFontExtensions();
virtual void Zoom(int factor);
virtual void Copy();

@ -630,6 +630,83 @@ typedef NPError (*NPSetCursorPtr)(
NPP instance,
NPCursorType type);
/* unique id for each font */
typedef int NPFontID;
typedef enum {
NPCharsetAnsi = 0,
NPCharsetDefault = 1,
NPCharsetSymbol = 2,
NPCharsetMac = 77,
NPCharsetShiftJIS = 128,
NPCharsetHangul = 129,
NPCharsetJohab = 130,
NPCharsetGB2312 =134,
NPCharsetChineseBIG5 = 136,
NPCharsetGreek = 161,
NPCharsetTurkish = 162,
NPCharsetVietnamese = 163,
NPCharsetHebrew = 177,
NPCharsetArabic = 178,
NPCharsetBaltic = 186,
NPCharsetRussian = 204,
NPCharsetThai = 222,
NPCharsetEastEurope = 238,
NPCharsetOEM = 255
} NPCharset;
typedef enum {
NPPitchDefault,
NPPitchFixed
} NPPitch;
typedef enum {
NPFamilyDefault,
NPFamilyRoman,
NPFamilyScript
} NPFamily;
typedef struct _NPFontDescription {
const char* face;
int weight;
bool italic;
NPPitch pitch;
NPFamily family;
NPCharset charset;
} NPFontDescription;
// Return a font which best matches the given properties.
typedef NPError (*NPMatchFontWithFallbackPtr) (
NPP instance,
const NPFontDescription* description,
NPFontID* id);
// Loads a specified font table for the given font.
// table: the table in *big-endian* format, or 0 for the whole font file.
// output: a buffer of size output_length that gets the data. can be 0, in
// which case output_length will be set to the required size in bytes.
// output_length: size of output, if it's not 0.
typedef NPError (*GetFontTablePtr) (
NPP instance,
NPFontID id,
uint32_t table,
void* output,
size_t* output_length);
// Destroys a font.
typedef NPError (*NPDestroyFontPtr) (
NPP instance,
NPFontID id);
typedef struct _NPFontExtensions {
NPMatchFontWithFallbackPtr matchFontWithFallback;
GetFontTablePtr getFontTable;
NPDestroyFontPtr destroyFont;
} NPFontExtensions;
typedef NPFontExtensions* (*NPGetFontExtensionsPtr)(
NPP instance);
/* Pepper extensions */
struct NPNExtensions {
/* Device interface acquisition */
@ -645,6 +722,8 @@ struct NPNExtensions {
NPGetWidgetExtensionsPtr getWidgetExtensions;
/* Cursor */
NPSetCursorPtr setCursor;
/* Font */
NPGetFontExtensionsPtr getFontExtensions;
};
/* 3D -----------------------------------------------------------------------*/

@ -526,6 +526,14 @@ static NPError NPSetCursor(NPP id, NPCursorType type) {
NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
}
static NPFontExtensions* GetFontExtensions(NPP id) {
scoped_refptr<NPAPI::PluginInstance> plugin = FindInstance(id);
if (!plugin)
return NULL;
return plugin->webplugin()->delegate()->GetFontExtensions();
}
namespace NPAPI {
NPError GetPepperExtensionsFunctions(void* value) {
@ -537,6 +545,7 @@ NPError GetPepperExtensionsFunctions(void* value) {
&ChooseFile,
&GetWidgetExtensions,
&NPSetCursor,
&GetFontExtensions,
};
// Return a pointer to the canonical function table.

@ -152,6 +152,7 @@ class WebPluginDelegate : public WebPlugin2DDeviceDelegate,
virtual void SelectedFindResultChanged(int index) {}
virtual NPWidgetExtensions* GetWidgetExtensions() { return NULL; }
virtual bool SetCursor(NPCursorType type) { return false; }
virtual NPFontExtensions* GetFontExtensions() { return NULL; }
// Used for zooming of full page plugins. 0 means reset, while -1 means zoom
// out and +1 means zoom in.