This source file includes following definitions.
- IsValid
- Describe
- GetTableTags
- GetTable
- Create
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/sys_byteorder.h"
#include "content/public/common/child_process_sandbox_support_linux.h"
#include "content/renderer/pepper/pepper_truetype_font.h"
#include "ppapi/c/dev/ppb_truetype_font_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/trusted/ppb_browser_font_trusted.h"
namespace content {
namespace {
class PepperTrueTypeFontLinux : public PepperTrueTypeFont {
public:
explicit PepperTrueTypeFontLinux(
const ppapi::proxy::SerializedTrueTypeFontDesc& desc);
virtual ~PepperTrueTypeFontLinux() OVERRIDE;
virtual bool IsValid() OVERRIDE;
virtual int32_t Describe(
ppapi::proxy::SerializedTrueTypeFontDesc* desc) OVERRIDE;
virtual int32_t GetTableTags(std::vector<uint32_t>* tags) OVERRIDE;
virtual int32_t GetTable(uint32_t table_tag,
int32_t offset,
int32_t max_data_length,
std::string* data) OVERRIDE;
private:
ppapi::proxy::SerializedTrueTypeFontDesc desc_;
int fd_;
DISALLOW_COPY_AND_ASSIGN(PepperTrueTypeFontLinux);
};
PepperTrueTypeFontLinux::PepperTrueTypeFontLinux(
const ppapi::proxy::SerializedTrueTypeFontDesc& desc) :
desc_(desc) {
if (desc_.family.empty()) {
switch (desc_.generic_family) {
case PP_TRUETYPEFONTFAMILY_SERIF:
desc_.family = "serif";
break;
case PP_TRUETYPEFONTFAMILY_SANSSERIF:
desc_.family = "sans-serif";
break;
case PP_TRUETYPEFONTFAMILY_CURSIVE:
desc_.family = "cursive";
break;
case PP_TRUETYPEFONTFAMILY_FANTASY:
desc_.family = "fantasy";
break;
case PP_TRUETYPEFONTFAMILY_MONOSPACE:
desc_.family = "monospace";
break;
}
}
fd_ = MatchFontWithFallback(
desc_.family.c_str(),
desc_.weight >= PP_TRUETYPEFONTWEIGHT_BOLD,
desc_.style & PP_TRUETYPEFONTSTYLE_ITALIC,
desc_.charset,
PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT);
}
PepperTrueTypeFontLinux::~PepperTrueTypeFontLinux() {
}
bool PepperTrueTypeFontLinux::IsValid() {
return fd_ != -1;
}
int32_t PepperTrueTypeFontLinux::Describe(
ppapi::proxy::SerializedTrueTypeFontDesc* desc) {
*desc = desc_;
return PP_OK;
}
int32_t PepperTrueTypeFontLinux::GetTableTags(std::vector<uint32_t>* tags) {
uint8_t num_tables_buf[2];
size_t output_length = sizeof(num_tables_buf);
if (!GetFontTable(fd_,
0 ,
4 ,
reinterpret_cast<uint8_t*>(&num_tables_buf),
&output_length))
return PP_ERROR_FAILED;
DCHECK(output_length == sizeof(num_tables_buf));
uint16_t num_tables = (num_tables_buf[0] << 8) | num_tables_buf[1];
static const size_t kFontHeaderSize = 12;
static const size_t kTableEntrySize = 16;
output_length = num_tables * kTableEntrySize;
scoped_ptr<uint8_t[]> table_entries(new uint8_t[output_length]);
if (!GetFontTable(fd_,
0 ,
kFontHeaderSize ,
table_entries.get(),
&output_length))
return PP_ERROR_FAILED;
DCHECK(output_length == num_tables * kTableEntrySize);
tags->resize(num_tables);
for (uint16_t i = 0; i < num_tables; i++) {
uint8_t* entry = table_entries.get() + i * kTableEntrySize;
uint32_t tag = static_cast<uint32_t>(entry[0]) << 24 |
static_cast<uint32_t>(entry[1]) << 16 |
static_cast<uint32_t>(entry[2]) << 8 |
static_cast<uint32_t>(entry[3]);
(*tags)[i] = tag;
}
return num_tables;
}
int32_t PepperTrueTypeFontLinux::GetTable(uint32_t table_tag,
int32_t offset,
int32_t max_data_length,
std::string* data) {
size_t table_size = 0;
table_tag = base::ByteSwap(table_tag);
if (!GetFontTable(fd_, table_tag, offset, NULL, &table_size))
return PP_ERROR_FAILED;
table_size = std::min(table_size, static_cast<size_t>(max_data_length));
data->resize(table_size);
if (!GetFontTable(fd_, table_tag, offset,
reinterpret_cast<uint8_t*>(&(*data)[0]),
&table_size))
return PP_ERROR_FAILED;
return base::checked_cast<int32_t>(table_size);
}
}
PepperTrueTypeFont* PepperTrueTypeFont::Create(
const ppapi::proxy::SerializedTrueTypeFontDesc& desc) {
return new PepperTrueTypeFontLinux(desc);
}
}