OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "components/font_service/font_service_app.h" | |
6 | |
7 #include "base/files/file.h" | |
8 #include "base/files/file_path.h" | |
9 #include "mojo/application/public/cpp/application_connection.h" | |
10 #include "mojo/platform_handle/platform_handle_functions.h" | |
11 | |
12 COMPILE_ASSERT(static_cast<uint32>(SkTypeface::kNormal) == | |
13 static_cast<uint32>(font_service::TYPEFACE_STYLE_NORMAL), | |
14 typeface_flags_should_match); | |
15 COMPILE_ASSERT(static_cast<uint32>(SkTypeface::kBold) == | |
16 static_cast<uint32>(font_service::TYPEFACE_STYLE_BOLD), | |
17 typeface_flags_should_match); | |
18 COMPILE_ASSERT(static_cast<uint32>(SkTypeface::kItalic) == | |
19 static_cast<uint32>(font_service::TYPEFACE_STYLE_ITALIC), | |
20 typeface_flags_should_match); | |
21 COMPILE_ASSERT( | |
22 static_cast<uint32>(SkTypeface::kBoldItalic) == | |
23 static_cast<uint32>(font_service::TYPEFACE_STYLE_BOLD_ITALIC), | |
24 typeface_flags_should_match); | |
25 | |
26 namespace { | |
27 | |
28 mojo::ScopedHandle GetHandleForPath(const base::FilePath& path) { | |
29 if (path.empty()) | |
30 return mojo::ScopedHandle(); | |
31 | |
32 mojo::ScopedHandle to_pass; | |
33 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
34 if (!file.IsValid()) { | |
35 LOG(WARNING) << "file not valid, path=" << path.value(); | |
36 return mojo::ScopedHandle(); | |
37 } | |
38 | |
39 MojoHandle mojo_handle; | |
40 MojoResult create_result = | |
41 MojoCreatePlatformHandleWrapper(file.TakePlatformFile(), &mojo_handle); | |
42 if (create_result != MOJO_RESULT_OK) { | |
43 LOG(WARNING) << "unable to create wrapper, path=" << path.value() | |
44 << "result=" << create_result; | |
45 return mojo::ScopedHandle(); | |
46 } | |
47 | |
48 return mojo::ScopedHandle(mojo::Handle(mojo_handle)).Pass(); | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 namespace font_service { | |
54 | |
55 FontServiceApp::FontServiceApp() {} | |
56 | |
57 FontServiceApp::~FontServiceApp() {} | |
58 | |
59 void FontServiceApp::Initialize(mojo::ApplicationImpl* app) {} | |
60 | |
61 bool FontServiceApp::ConfigureIncomingConnection( | |
62 mojo::ApplicationConnection* connection) { | |
63 connection->AddService(this); | |
64 return true; | |
65 } | |
66 | |
67 void FontServiceApp::Create(mojo::ApplicationConnection* connection, | |
68 mojo::InterfaceRequest<FontService> request) { | |
69 bindings_.AddBinding(this, request.Pass()); | |
70 } | |
71 | |
72 void FontServiceApp::MatchFamilyName(const mojo::String& family_name, | |
73 TypefaceStyle requested_style, | |
74 const MatchFamilyNameCallback& callback) { | |
75 SkFontConfigInterface::FontIdentity result_identity; | |
76 SkString result_family; | |
77 SkTypeface::Style result_style; | |
78 SkFontConfigInterface* fc = | |
79 SkFontConfigInterface::GetSingletonDirectInterface(); | |
80 const bool r = fc->matchFamilyName( | |
81 family_name.data(), static_cast<SkTypeface::Style>(requested_style), | |
82 &result_identity, &result_family, &result_style); | |
83 | |
84 if (!r) { | |
85 callback.Run(nullptr, "", TYPEFACE_STYLE_NORMAL); | |
86 return; | |
87 } | |
88 | |
89 // Stash away the returned path, so we can give it an ID (index) | |
90 // which will later be given to us in a request to open the file. | |
91 int index = FindOrAddPath(result_identity.fString); | |
92 | |
93 FontIdentityPtr identity(FontIdentity::New()); | |
94 identity->id = static_cast<uint32_t>(index); | |
95 identity->ttc_index = result_identity.fTTCIndex; | |
96 identity->str_representation = result_identity.fString.c_str(); | |
97 | |
98 callback.Run(identity.Pass(), result_family.c_str(), | |
99 static_cast<TypefaceStyle>(result_style)); | |
100 } | |
101 | |
102 void FontServiceApp::OpenStream(uint32_t id_number, | |
103 const OpenStreamCallback& callback) { | |
104 mojo::ScopedHandle handle; | |
105 if (id_number < static_cast<uint32_t>(paths_.count())) { | |
106 handle = | |
107 GetHandleForPath(base::FilePath(paths_[id_number]->c_str())).Pass(); | |
108 } | |
109 | |
110 callback.Run(handle.Pass()); | |
111 } | |
112 | |
113 int FontServiceApp::FindOrAddPath(const SkString& path) { | |
114 int count = paths_.count(); | |
115 for (int i = 0; i < count; ++i) { | |
116 if (path == *paths_[i]) | |
117 return i; | |
118 } | |
119 *paths_.append() = new SkString(path); | |
120 return count; | |
121 } | |
122 | |
123 } // namespace font_service | |
OLD | NEW |