truetype_font_dev.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/cpp/dev/truetype_font_dev.h"
6
7#include <stdio.h>
8#include <string.h>  // memcpy
9
10#include "ppapi/c/dev/ppb_truetype_font_dev.h"
11#include "ppapi/c/pp_errors.h"
12#include "ppapi/c/ppb_var.h"
13#include "ppapi/cpp/completion_callback.h"
14#include "ppapi/cpp/instance_handle.h"
15#include "ppapi/cpp/module.h"
16#include "ppapi/cpp/module_impl.h"
17#include "ppapi/cpp/var.h"
18
19namespace pp {
20
21namespace {
22
23template <> const char* interface_name<PPB_TrueTypeFont_Dev_0_1>() {
24  return PPB_TRUETYPEFONT_DEV_INTERFACE_0_1;
25}
26
27}  // namespace
28
29// TrueTypeFontDesc_Dev --------------------------------------------------------
30
31TrueTypeFontDesc_Dev::TrueTypeFontDesc_Dev() {
32  desc_.family = family_.pp_var();
33  set_generic_family(PP_TRUETYPEFONTFAMILY_SERIF);
34  set_style(PP_TRUETYPEFONTSTYLE_NORMAL);
35  set_weight(PP_TRUETYPEFONTWEIGHT_NORMAL);
36  set_width(PP_TRUETYPEFONTWIDTH_NORMAL);
37  set_charset(PP_TRUETYPEFONTCHARSET_DEFAULT);
38}
39
40TrueTypeFontDesc_Dev::TrueTypeFontDesc_Dev(
41    PassRef,
42    const PP_TrueTypeFontDesc_Dev& pp_desc) {
43  desc_ = pp_desc;
44  family_ = Var(PASS_REF, pp_desc.family);
45}
46
47TrueTypeFontDesc_Dev::TrueTypeFontDesc_Dev(const TrueTypeFontDesc_Dev& other) {
48  set_family(other.family());
49  set_generic_family(other.generic_family());
50  set_style(other.style());
51  set_weight(other.weight());
52  set_width(other.width());
53  set_charset(other.charset());
54}
55
56TrueTypeFontDesc_Dev::~TrueTypeFontDesc_Dev() {
57}
58
59TrueTypeFontDesc_Dev& TrueTypeFontDesc_Dev::operator=(
60    const TrueTypeFontDesc_Dev& other) {
61  if (this == &other)
62    return *this;
63
64  desc_ = other.desc_;
65  // Be careful about the refcount of the string, the assignment above doesn't
66  // copy a ref. The assignments below take a ref to the new name and copy the
67  // PP_Var into the wrapped descriptor.
68  family_ = other.family();
69  desc_.family = family_.pp_var();
70
71  return *this;
72}
73
74// TrueTypeFont_Dev ------------------------------------------------------------
75
76TrueTypeFont_Dev::TrueTypeFont_Dev() {
77}
78
79TrueTypeFont_Dev::TrueTypeFont_Dev(const InstanceHandle& instance,
80                                   const TrueTypeFontDesc_Dev& desc) {
81  if (!has_interface<PPB_TrueTypeFont_Dev_0_1>())
82    return;
83  PassRefFromConstructor(get_interface<PPB_TrueTypeFont_Dev_0_1>()->Create(
84      instance.pp_instance(), &desc.pp_desc()));
85}
86
87TrueTypeFont_Dev::TrueTypeFont_Dev(const TrueTypeFont_Dev& other)
88    : Resource(other) {
89}
90
91TrueTypeFont_Dev::TrueTypeFont_Dev(PassRef, PP_Resource resource)
92    : Resource(PASS_REF, resource) {
93}
94
95// static
96int32_t TrueTypeFont_Dev::GetFontFamilies(
97    const InstanceHandle& instance,
98    const CompletionCallbackWithOutput<std::vector<Var> >& cc) {
99  if (has_interface<PPB_TrueTypeFont_Dev_0_1>()) {
100    return get_interface<PPB_TrueTypeFont_Dev_0_1>()->GetFontFamilies(
101        instance.pp_instance(),
102        cc.output(), cc.pp_completion_callback());
103  }
104  return cc.MayForce(PP_ERROR_NOINTERFACE);
105}
106
107int32_t TrueTypeFont_Dev::Describe(
108    const CompletionCallbackWithOutput<TrueTypeFontDesc_Dev>& cc) {
109  if (has_interface<PPB_TrueTypeFont_Dev_0_1>()) {
110    int32_t result =
111        get_interface<PPB_TrueTypeFont_Dev_0_1>()->Describe(
112            pp_resource(), cc.output(), cc.pp_completion_callback());
113    return result;
114  }
115  return cc.MayForce(PP_ERROR_NOINTERFACE);
116}
117
118int32_t TrueTypeFont_Dev::GetTableTags(
119    const CompletionCallbackWithOutput<std::vector<uint32_t> >& cc) {
120  if (has_interface<PPB_TrueTypeFont_Dev_0_1>()) {
121    return get_interface<PPB_TrueTypeFont_Dev_0_1>()->GetTableTags(
122        pp_resource(),
123        cc.output(), cc.pp_completion_callback());
124  }
125  return cc.MayForce(PP_ERROR_NOINTERFACE);
126}
127
128int32_t TrueTypeFont_Dev::GetTable(
129    uint32_t table,
130    int32_t offset,
131    int32_t max_data_length,
132    const CompletionCallbackWithOutput<std::vector<char> >& cc) {
133  if (has_interface<PPB_TrueTypeFont_Dev_0_1>()) {
134    return get_interface<PPB_TrueTypeFont_Dev_0_1>()->GetTable(
135        pp_resource(),
136        table, offset, max_data_length,
137        cc.output(), cc.pp_completion_callback());
138  }
139  return cc.MayForce(PP_ERROR_NOINTERFACE);
140}
141
142}  // namespace pp
143