summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_font_proxy.cc
blob: 49d38923caac88504aa3b96865369e835ce38622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2011 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 "ppapi/proxy/ppb_font_proxy.h"

#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "ppapi/c/dev/ppb_font_dev.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/plugin_proxy_delegate.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppb_image_data_proxy.h"
#include "ppapi/proxy/serialized_var.h"
#include "ppapi/shared_impl/ppapi_preferences.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_image_data_api.h"
#include "ppapi/thunk/thunk.h"

using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_Font_API;
using ppapi::thunk::PPB_Font_FunctionAPI;
using ppapi::thunk::PPB_ImageData_API;

namespace ppapi {
namespace proxy {

namespace {

bool PPTextRunToTextRun(const PP_TextRun_Dev* run,
                        WebKitForwarding::Font::TextRun* output) {
  StringVar* str = StringVar::FromPPVar(run->text);
  if (!str)
    return false;

  output->text = str->value();
  output->rtl = PP_ToBool(run->rtl);
  output->override_direction = PP_ToBool(run->override_direction);
  return true;
}

}  // namespace

PPB_Font_Proxy::PPB_Font_Proxy(Dispatcher* dispatcher)
    : InterfaceProxy(dispatcher) {
}

PPB_Font_Proxy::~PPB_Font_Proxy() {
}

PPB_Font_FunctionAPI* PPB_Font_Proxy::AsPPB_Font_FunctionAPI() {
  return this;
}

PP_Var PPB_Font_Proxy::GetFontFamilies(PP_Instance instance) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return PP_MakeUndefined();

  // Assume the font families don't change, so we can cache the result globally.
  CR_DEFINE_STATIC_LOCAL(std::string, families, ());
  if (families.empty()) {
    PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(
        new PpapiHostMsg_PPBFont_GetFontFamilies(&families));
  }

  return StringVar::StringToPPVar(families);
}

bool PPB_Font_Proxy::OnMessageReceived(const IPC::Message& msg) {
  // There aren't any font messages.
  NOTREACHED();
  return false;
}

Font::Font(const HostResource& resource,
           const PP_FontDescription_Dev& desc)
    : Resource(resource),
      webkit_event_(false, false),
      font_forwarding_(NULL) {
  TRACE_EVENT0("ppapi proxy", "Font::Font");
  StringVar* face = StringVar::FromPPVar(desc.face);

  PluginDispatcher* dispatcher = PluginDispatcher::GetForResource(this);
  if (!dispatcher)
    return;
  WebKitForwarding* forwarding =
      PluginGlobals::Get()->plugin_proxy_delegate()->GetWebKitForwarding();

  RunOnWebKitThread(true,
                    base::Bind(&WebKitForwarding::CreateFontForwarding,
                               base::Unretained(forwarding),
                               &webkit_event_, desc,
                               face ? face->value() : std::string(),
                               dispatcher->preferences(),
                               &font_forwarding_));
}

Font::~Font() {
  if (font_forwarding_) {
    RunOnWebKitThread(false,
                      base::Bind(&DeleteFontForwarding, font_forwarding_));
  }
}

PPB_Font_API* Font::AsPPB_Font_API() {
  return this;
}

PP_Bool Font::Describe(PP_FontDescription_Dev* description,
                       PP_FontMetrics_Dev* metrics) {
  TRACE_EVENT0("ppapi proxy", "Font::Describe");
  std::string face;
  PP_Bool result = PP_FALSE;
  if (font_forwarding_) {
    RunOnWebKitThread(true,
                      base::Bind(&WebKitForwarding::Font::Describe,
                                 base::Unretained(font_forwarding_),
                                 &webkit_event_, description, &face, metrics,
                                 &result));
  }

  if (PP_ToBool(result))
    description->face = StringVar::StringToPPVar(face);
  else
    description->face = PP_MakeUndefined();
  return result;
}

PP_Bool Font::DrawTextAt(PP_Resource pp_image_data,
                         const PP_TextRun_Dev* text,
                         const PP_Point* position,
                         uint32_t color,
                         const PP_Rect* clip,
                         PP_Bool image_data_is_opaque) {
  TRACE_EVENT0("ppapi proxy", "Font::DrawTextAt");
  if (!font_forwarding_)
    return PP_FALSE;

  // Convert to an ImageData object.
  EnterResourceNoLock<PPB_ImageData_API> enter(pp_image_data, true);
  if (enter.failed())
    return PP_FALSE;
  ImageData* image_data = static_cast<ImageData*>(enter.object());

  skia::PlatformCanvas* canvas = image_data->mapped_canvas();
  bool needs_unmapping = false;
  if (!canvas) {
    needs_unmapping = true;
    image_data->Map();
    canvas = image_data->mapped_canvas();
    if (!canvas)
      return PP_FALSE;  // Failure mapping.
  }

  WebKitForwarding::Font::TextRun run;
  if (!PPTextRunToTextRun(text, &run)) {
    if (needs_unmapping)
      image_data->Unmap();
    return PP_FALSE;
  }
  RunOnWebKitThread(
      true,
      base::Bind(&WebKitForwarding::Font::DrawTextAt,
                 base::Unretained(font_forwarding_), &webkit_event_,
                 WebKitForwarding::Font::DrawTextParams(canvas, run, position,
                                                        color, clip,
                                                        image_data_is_opaque)));

  if (needs_unmapping)
    image_data->Unmap();
  return PP_TRUE;
}

int32_t Font::MeasureText(const PP_TextRun_Dev* text) {
  TRACE_EVENT0("ppapi proxy", "Font::MeasureText");
  WebKitForwarding::Font::TextRun run;
  if (!font_forwarding_ || !PPTextRunToTextRun(text, &run))
    return -1;
  int32_t result = -1;
  RunOnWebKitThread(true,
                    base::Bind(&WebKitForwarding::Font::MeasureText,
                               base::Unretained(font_forwarding_),
                               &webkit_event_, run, &result));
  return result;
}

uint32_t Font::CharacterOffsetForPixel(const PP_TextRun_Dev* text,
                                       int32_t pixel_position) {
  TRACE_EVENT0("ppapi proxy", "Font::CharacterOffsetForPixel");
  WebKitForwarding::Font::TextRun run;
  if (!font_forwarding_ || !PPTextRunToTextRun(text, &run))
    return -1;
  uint32_t result = -1;
  RunOnWebKitThread(true,
                    base::Bind(&WebKitForwarding::Font::CharacterOffsetForPixel,
                               base::Unretained(font_forwarding_),
                               &webkit_event_, run, pixel_position, &result));
  return result;
}

int32_t Font::PixelOffsetForCharacter(const PP_TextRun_Dev* text,
                                      uint32_t char_offset) {
  TRACE_EVENT0("ppapi proxy", "Font::PixelOffsetForCharacter");
  WebKitForwarding::Font::TextRun run;
  if (!font_forwarding_ || !PPTextRunToTextRun(text, &run))
    return -1;
  int32_t result = -1;
  RunOnWebKitThread(true,
                    base::Bind(&WebKitForwarding::Font::PixelOffsetForCharacter,
                               base::Unretained(font_forwarding_),
                               &webkit_event_, run, char_offset, &result));
  return result;
}

void Font::RunOnWebKitThread(bool blocking, const base::Closure& task) {
  PluginGlobals::Get()->plugin_proxy_delegate()->PostToWebKitThread(
      FROM_HERE, task);
  if (blocking)
    webkit_event_.Wait();
}

// static
void Font::DeleteFontForwarding(WebKitForwarding::Font* font_forwarding) {
  delete font_forwarding;
}

}  // namespace proxy
}  // namespace ppapi