OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_font.h" |
| 6 |
| 7 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 8 #include "native_client/src/include/portability.h" |
| 9 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" |
| 10 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h" |
| 11 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 12 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 13 #include "ppapi/c/dev/ppb_font_dev.h" |
| 14 #include "ppapi/c/pp_completion_callback.h" |
| 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/c/pp_point.h" |
| 17 #include "ppapi/c/pp_rect.h" |
| 18 #include "srpcgen/ppb_rpc.h" |
| 19 |
| 20 namespace ppapi_proxy { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const nacl_abi_size_t kPPPointBytes = |
| 25 static_cast<nacl_abi_size_t>(sizeof(struct PP_Point)); |
| 26 const nacl_abi_size_t kPPRectBytes = |
| 27 static_cast<nacl_abi_size_t>(sizeof(struct PP_Rect)); |
| 28 const nacl_abi_size_t kPPTextRunBytes = |
| 29 static_cast<nacl_abi_size_t>(sizeof(struct PP_TextRun_Dev)); |
| 30 const nacl_abi_size_t kPPFontMetricsBytes = |
| 31 static_cast<nacl_abi_size_t>(sizeof(struct PP_FontMetrics_Dev)); |
| 32 const nacl_abi_size_t kPPFontDescriptionBytes = |
| 33 static_cast<nacl_abi_size_t>(sizeof(struct PP_FontDescription_Dev)); |
| 34 const int32_t kInvalidOffset = -1; |
| 35 |
| 36 PP_Var GetFontFamilies(PP_Instance instance) { |
| 37 DebugPrintf("PPB_Font::GetFontFamilies: instance=%"NACL_PRIu32"\n", |
| 38 instance); |
| 39 NaClSrpcChannel* channel = GetMainSrpcChannel(); |
| 40 |
| 41 PP_Var font_families = PP_MakeUndefined(); |
| 42 nacl_abi_size_t var_size = kMaxVarSize; |
| 43 nacl::scoped_array<char> var_bytes(new char[kMaxVarSize]); |
| 44 |
| 45 NaClSrpcError srpc_result = |
| 46 PpbFontRpcClient::PPB_Font_GetFontFamilies( |
| 47 channel, |
| 48 instance, |
| 49 &var_size, |
| 50 var_bytes.get()); |
| 51 |
| 52 DebugPrintf("PPB_Font::GetFontFamilies: %s\n", |
| 53 NaClSrpcErrorString(srpc_result)); |
| 54 |
| 55 if (srpc_result == NACL_SRPC_RESULT_OK) |
| 56 (void) DeserializeTo( |
| 57 channel, var_bytes.get(), var_size, 1, &font_families); |
| 58 return font_families; |
| 59 } |
| 60 |
| 61 PP_Resource Create(PP_Instance instance, |
| 62 const struct PP_FontDescription_Dev* description) { |
| 63 DebugPrintf("PPB_Font::Create: instance=%"NACL_PRIu32"\n", instance); |
| 64 |
| 65 nacl_abi_size_t face_size = kMaxVarSize; |
| 66 nacl::scoped_array<char> face_bytes( |
| 67 Serialize(&description->face, 1, &face_size)); |
| 68 |
| 69 PP_Resource resource = kInvalidResourceId; |
| 70 NaClSrpcError srpc_result = |
| 71 PpbFontRpcClient::PPB_Font_Create( |
| 72 GetMainSrpcChannel(), |
| 73 instance, |
| 74 kPPFontDescriptionBytes, |
| 75 reinterpret_cast<char*>( |
| 76 const_cast<struct PP_FontDescription_Dev*>(description)), |
| 77 face_size, |
| 78 face_bytes.get(), |
| 79 &resource); |
| 80 |
| 81 DebugPrintf("PPB_Font::Create: %s\n", NaClSrpcErrorString(srpc_result)); |
| 82 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 83 scoped_refptr<PluginFont> font = |
| 84 PluginResource::AdoptAs<PluginFont>(resource); |
| 85 if (font.get()) |
| 86 return resource; |
| 87 } |
| 88 return kInvalidResourceId; |
| 89 } |
| 90 |
| 91 PP_Bool IsFont(PP_Resource resource) { |
| 92 DebugPrintf("PPB_Font::IsFont: resource=%"NACL_PRIu32"\n", resource); |
| 93 |
| 94 int32_t is_font = 0; |
| 95 NaClSrpcError srpc_result = |
| 96 PpbFontRpcClient::PPB_Font_IsFont(GetMainSrpcChannel(), |
| 97 resource, |
| 98 &is_font); |
| 99 |
| 100 DebugPrintf("PPB_Font::IsFont: %s\n", NaClSrpcErrorString(srpc_result)); |
| 101 if (srpc_result == NACL_SRPC_RESULT_OK && is_font) |
| 102 return PP_TRUE; |
| 103 return PP_FALSE; |
| 104 } |
| 105 |
| 106 PP_Bool Describe(PP_Resource font, |
| 107 struct PP_FontDescription_Dev* description, |
| 108 struct PP_FontMetrics_Dev* metrics) { |
| 109 DebugPrintf("PPB_Font::Describe: font=%"NACL_PRIu32"\n", font); |
| 110 NaClSrpcChannel* channel = GetMainSrpcChannel(); |
| 111 |
| 112 int32_t success = 0; |
| 113 nacl_abi_size_t description_size = kPPFontDescriptionBytes; |
| 114 nacl_abi_size_t face_size = kMaxVarSize; |
| 115 nacl::scoped_array<char> face_bytes(new char[kMaxVarSize]); |
| 116 nacl_abi_size_t metrics_size = kPPFontMetricsBytes; |
| 117 NaClSrpcError srpc_result = |
| 118 PpbFontRpcClient::PPB_Font_Describe( |
| 119 channel, |
| 120 font, |
| 121 &description_size, |
| 122 reinterpret_cast<char*>(description), |
| 123 &face_size, |
| 124 face_bytes.get(), |
| 125 &metrics_size, |
| 126 reinterpret_cast<char*>(metrics), |
| 127 &success); |
| 128 |
| 129 DebugPrintf("PPB_Font::Describe: %s\n", NaClSrpcErrorString(srpc_result)); |
| 130 |
| 131 description->face = PP_MakeUndefined(); |
| 132 if (srpc_result == NACL_SRPC_RESULT_OK && success) { |
| 133 (void) DeserializeTo( |
| 134 channel, face_bytes.get(), face_size, 1, &description->face); |
| 135 return PP_TRUE; |
| 136 } |
| 137 return PP_FALSE; |
| 138 } |
| 139 |
| 140 PP_Bool DrawTextAt(PP_Resource font, |
| 141 PP_Resource image_data, |
| 142 const struct PP_TextRun_Dev* text_run, |
| 143 const struct PP_Point* position, |
| 144 uint32_t color, |
| 145 const struct PP_Rect* clip, |
| 146 PP_Bool image_data_is_opaque) { |
| 147 DebugPrintf("PPB_Font::DrawTextAt: font=%"NACL_PRIu32"\n", font); |
| 148 |
| 149 nacl_abi_size_t text_size = kMaxVarSize; |
| 150 nacl::scoped_array<char> text_bytes( |
| 151 Serialize(&text_run->text, 1, &text_size)); |
| 152 |
| 153 int32_t success = 0; |
| 154 NaClSrpcError srpc_result = |
| 155 PpbFontRpcClient::PPB_Font_DrawTextAt( |
| 156 GetMainSrpcChannel(), |
| 157 font, |
| 158 image_data, |
| 159 kPPTextRunBytes, |
| 160 reinterpret_cast<char*>( |
| 161 const_cast<struct PP_TextRun_Dev*>(text_run)), |
| 162 text_size, |
| 163 text_bytes.get(), |
| 164 kPPPointBytes, |
| 165 reinterpret_cast<char*>(const_cast<struct PP_Point*>(position)), |
| 166 color, |
| 167 kPPRectBytes, |
| 168 reinterpret_cast<char*>(const_cast<struct PP_Rect*>(clip)), |
| 169 image_data_is_opaque, |
| 170 &success); |
| 171 |
| 172 DebugPrintf("PPB_Font::DrawTextAt: %s\n", NaClSrpcErrorString(srpc_result)); |
| 173 if (srpc_result == NACL_SRPC_RESULT_OK && success) |
| 174 return PP_TRUE; |
| 175 return PP_FALSE; |
| 176 } |
| 177 |
| 178 int32_t MeasureText(PP_Resource font, |
| 179 const struct PP_TextRun_Dev* text_run) { |
| 180 DebugPrintf("PPB_Font::MeasureText: font=%"NACL_PRIu32"\n", font); |
| 181 |
| 182 nacl_abi_size_t text_size = kMaxVarSize; |
| 183 nacl::scoped_array<char> text_bytes( |
| 184 Serialize(&text_run->text, 1, &text_size)); |
| 185 int32_t width = 0; |
| 186 NaClSrpcError srpc_result = |
| 187 PpbFontRpcClient::PPB_Font_MeasureText( |
| 188 GetMainSrpcChannel(), |
| 189 font, |
| 190 kPPTextRunBytes, |
| 191 reinterpret_cast<char*>( |
| 192 const_cast<struct PP_TextRun_Dev*>(text_run)), |
| 193 text_size, |
| 194 text_bytes.get(), |
| 195 &width); |
| 196 |
| 197 DebugPrintf("PPB_Font::MeasureText: %s\n", NaClSrpcErrorString(srpc_result)); |
| 198 return width; |
| 199 } |
| 200 |
| 201 uint32_t CharacterOffsetForPixel(PP_Resource font, |
| 202 const struct PP_TextRun_Dev* text_run, |
| 203 int32_t pixel_position) { |
| 204 DebugPrintf("PPB_Font::CharacterOffsetForPixel: font=%"NACL_PRIu32"\n", font); |
| 205 |
| 206 nacl_abi_size_t text_size = kMaxVarSize; |
| 207 nacl::scoped_array<char> text_bytes( |
| 208 Serialize(&text_run->text, 1, &text_size)); |
| 209 int32_t offset = kInvalidOffset; |
| 210 NaClSrpcError srpc_result = |
| 211 PpbFontRpcClient::PPB_Font_CharacterOffsetForPixel( |
| 212 GetMainSrpcChannel(), |
| 213 font, |
| 214 kPPTextRunBytes, |
| 215 reinterpret_cast<char*>( |
| 216 const_cast<struct PP_TextRun_Dev*>(text_run)), |
| 217 text_size, |
| 218 text_bytes.get(), |
| 219 pixel_position, |
| 220 &offset); |
| 221 |
| 222 DebugPrintf("PPB_Font::CharacterOffsetForPixel: %s\n", |
| 223 NaClSrpcErrorString(srpc_result)); |
| 224 return offset; |
| 225 } |
| 226 |
| 227 int32_t PixelOffsetForCharacter(PP_Resource font, |
| 228 const struct PP_TextRun_Dev* text_run, |
| 229 uint32_t char_offset) { |
| 230 DebugPrintf("PPB_Font::PixelOffsetForCharacter: font=%"NACL_PRIu32"\n", font); |
| 231 |
| 232 nacl_abi_size_t text_size = kMaxVarSize; |
| 233 nacl::scoped_array<char> text_bytes( |
| 234 Serialize(&text_run->text, 1, &text_size)); |
| 235 int32_t offset = kInvalidOffset; |
| 236 NaClSrpcError srpc_result = |
| 237 PpbFontRpcClient::PPB_Font_PixelOffsetForCharacter( |
| 238 GetMainSrpcChannel(), |
| 239 font, |
| 240 kPPTextRunBytes, |
| 241 reinterpret_cast<char*>( |
| 242 const_cast<struct PP_TextRun_Dev*>(text_run)), |
| 243 text_size, |
| 244 text_bytes.get(), |
| 245 char_offset, |
| 246 &offset); |
| 247 |
| 248 DebugPrintf("PPB_Font::PixelOffsetForCharacter: %s\n", |
| 249 NaClSrpcErrorString(srpc_result)); |
| 250 return offset; |
| 251 } |
| 252 |
| 253 } // namespace |
| 254 |
| 255 const PPB_Font_Dev* PluginFont::GetInterface() { |
| 256 static const PPB_Font_Dev font_interface = { |
| 257 GetFontFamilies, |
| 258 Create, |
| 259 IsFont, |
| 260 Describe, |
| 261 DrawTextAt, |
| 262 MeasureText, |
| 263 CharacterOffsetForPixel, |
| 264 PixelOffsetForCharacter |
| 265 }; |
| 266 return &font_interface; |
| 267 } |
| 268 |
| 269 } // namespace ppapi_proxy |
| 270 |
OLD | NEW |