OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontConfigInterface.h" | 8 #include "SkFontConfigInterface.h" |
9 #include "SkFontConfigTypeface.h" | 9 #include "SkFontConfigTypeface.h" |
10 #include "SkFontDescriptor.h" | 10 #include "SkFontDescriptor.h" |
11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
12 #include "SkFontStyle.h" | 12 #include "SkFontStyle.h" |
| 13 #include "SkMakeUnique.h" |
13 #include "SkMutex.h" | 14 #include "SkMutex.h" |
14 #include "SkString.h" | 15 #include "SkString.h" |
15 #include "SkTypeface.h" | 16 #include "SkTypeface.h" |
16 #include "SkTypefaceCache.h" | 17 #include "SkTypefaceCache.h" |
17 #include "SkResourceCache.h" | 18 #include "SkResourceCache.h" |
18 | 19 |
19 SkStreamAsset* SkTypeface_FCI::onOpenStream(int* ttcIndex) const { | 20 SkStreamAsset* SkTypeface_FCI::onOpenStream(int* ttcIndex) const { |
20 *ttcIndex = this->getIdentity().fTTCIndex; | 21 *ttcIndex = this->getIdentity().fTTCIndex; |
21 | 22 |
22 if (fFontData) { | 23 if (fFontData) { |
23 SkStreamAsset* stream = fFontData->getStream(); | 24 SkStreamAsset* stream = fFontData->getStream(); |
24 if (!stream) { | 25 if (!stream) { |
25 return nullptr; | 26 return nullptr; |
26 } | 27 } |
27 return stream->duplicate(); | 28 return stream->duplicate(); |
28 } | 29 } |
29 | 30 |
30 return fFCI->openStream(this->getIdentity()); | 31 return fFCI->openStream(this->getIdentity()); |
31 } | 32 } |
32 | 33 |
33 SkFontData* SkTypeface_FCI::onCreateFontData() const { | 34 std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const { |
34 if (fFontData) { | 35 if (fFontData) { |
35 return new SkFontData(*fFontData.get()); | 36 return skstd::make_unique<SkFontData>(*fFontData); |
36 } | 37 } |
37 | 38 |
38 const SkFontConfigInterface::FontIdentity& id = this->getIdentity(); | 39 const SkFontConfigInterface::FontIdentity& id = this->getIdentity(); |
39 return new SkFontData( fFCI->openStream(id), id.fTTCIndex, nullptr, 0); | 40 return skstd::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->o
penStream(id)), |
| 41 id.fTTCIndex, nullptr, 0); |
40 } | 42 } |
41 | 43 |
42 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalSt
ream) const { | 44 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalSt
ream) const { |
43 SkString name; | 45 SkString name; |
44 this->getFamilyName(&name); | 46 this->getFamilyName(&name); |
45 desc->setFamilyName(name.c_str()); | 47 desc->setFamilyName(name.c_str()); |
46 desc->setStyle(this->fontStyle()); | 48 desc->setStyle(this->fontStyle()); |
47 *isLocalStream = SkToBool(fFontData); | 49 *isLocalStream = SkToBool(fFontData); |
48 } | 50 } |
49 | 51 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 const char* bcp47[], int bcp47Count, | 194 const char* bcp47[], int bcp47Count, |
193 SkUnichar character) const override
{ | 195 SkUnichar character) const override
{ |
194 return nullptr; | 196 return nullptr; |
195 } | 197 } |
196 SkTypeface* onMatchFaceStyle(const SkTypeface*, | 198 SkTypeface* onMatchFaceStyle(const SkTypeface*, |
197 const SkFontStyle&) const override { return nul
lptr; } | 199 const SkFontStyle&) const override { return nul
lptr; } |
198 | 200 |
199 SkTypeface* onCreateFromData(SkData*, int ttcIndex) const override { return
nullptr; } | 201 SkTypeface* onCreateFromData(SkData*, int ttcIndex) const override { return
nullptr; } |
200 | 202 |
201 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 203 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
202 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 204 std::unique_ptr<SkStreamAsset> stream(bareStream); |
203 const size_t length = stream->getLength(); | 205 const size_t length = stream->getLength(); |
204 if (!length) { | 206 if (!length) { |
205 return nullptr; | 207 return nullptr; |
206 } | 208 } |
207 if (length >= 1024 * 1024 * 1024) { | 209 if (length >= 1024 * 1024 * 1024) { |
208 return nullptr; // don't accept too large fonts (>= 1GB) for safety
. | 210 return nullptr; // don't accept too large fonts (>= 1GB) for safety
. |
209 } | 211 } |
210 | 212 |
211 // TODO should the caller give us the style or should we get it from fre
etype? | 213 // TODO should the caller give us the style or should we get it from fre
etype? |
212 SkFontStyle style; | 214 SkFontStyle style; |
213 bool isFixedPitch = false; | 215 bool isFixedPitch = false; |
214 if (!fScanner.scanFont(stream, 0, nullptr, &style, &isFixedPitch, nullpt
r)) { | 216 if (!fScanner.scanFont(stream.get(), 0, nullptr, &style, &isFixedPitch,
nullptr)) { |
215 return nullptr; | 217 return nullptr; |
216 } | 218 } |
217 | 219 |
218 std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(), tt
cIndex, | 220 auto fontData = skstd::make_unique<SkFontData>(std::move(stream), ttcInd
ex, nullptr, 0); |
219 nullptr, 0)); | |
220 return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch); | 221 return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch); |
221 } | 222 } |
222 | 223 |
223 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { | 224 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { |
224 using Scanner = SkTypeface_FreeType::Scanner; | 225 using Scanner = SkTypeface_FreeType::Scanner; |
225 SkAutoTDelete<SkStreamAsset> stream(s); | 226 std::unique_ptr<SkStreamAsset> stream(s); |
226 const size_t length = stream->getLength(); | 227 const size_t length = stream->getLength(); |
227 if (!length) { | 228 if (!length) { |
228 return nullptr; | 229 return nullptr; |
229 } | 230 } |
230 if (length >= 1024 * 1024 * 1024) { | 231 if (length >= 1024 * 1024 * 1024) { |
231 return nullptr; // don't accept too large fonts (>= 1GB) for safety
. | 232 return nullptr; // don't accept too large fonts (>= 1GB) for safety
. |
232 } | 233 } |
233 | 234 |
234 bool isFixedPitch; | 235 bool isFixedPitch; |
235 SkFontStyle style; | 236 SkFontStyle style; |
236 SkString name; | 237 SkString name; |
237 Scanner::AxisDefinitions axisDefinitions; | 238 Scanner::AxisDefinitions axisDefinitions; |
238 if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &styl
e, &isFixedPitch, | 239 if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(), |
239 &axisDefinitions)) | 240 &name, &style, &isFixedPitch, &axisDefinitions)) |
240 { | 241 { |
241 return nullptr; | 242 return nullptr; |
242 } | 243 } |
243 | 244 |
244 int paramAxisCount; | 245 int paramAxisCount; |
245 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); | 246 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); |
246 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); | 247 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
247 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); | 248 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); |
248 | 249 |
249 std::unique_ptr<SkFontData> fontData(new SkFontData(stream.release(), | 250 auto fontData = skstd::make_unique<SkFontData>(std::move(stream), |
250 params.getCollection
Index(), | 251 params.getCollectionIndex
(), |
251 axisValues.get(), | 252 axisValues.get(), |
252 axisDefinitions.coun
t())); | 253 axisDefinitions.count()); |
253 return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch); | 254 return SkTypeface_FCI::Create(std::move(fontData), style, isFixedPitch); |
254 } | 255 } |
255 | 256 |
256 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ | 257 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
257 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); | 258 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path); |
258 return stream.get() ? this->createFromStream(stream.release(), ttcIndex)
: nullptr; | 259 return stream.get() ? this->createFromStream(stream.release(), ttcIndex)
: nullptr; |
259 } | 260 } |
260 | 261 |
261 SkTypeface* onLegacyCreateTypeface(const char requestedFamilyName[], | 262 SkTypeface* onLegacyCreateTypeface(const char requestedFamilyName[], |
262 SkFontStyle requestedStyle) const overrid
e | 263 SkFontStyle requestedStyle) const overrid
e |
263 { | 264 { |
264 SkAutoMutexAcquire ama(fMutex); | 265 SkAutoMutexAcquire ama(fMutex); |
265 | 266 |
266 // Check if this request is already in the request cache. | 267 // Check if this request is already in the request cache. |
267 using Request = SkFontRequestCache::Request; | 268 using Request = SkFontRequestCache::Request; |
(...skipping 23 matching lines...) Expand all Loading... |
291 fCache.add(face, request.release()); | 292 fCache.add(face, request.release()); |
292 | 293 |
293 return face; | 294 return face; |
294 } | 295 } |
295 }; | 296 }; |
296 | 297 |
297 SK_API SkFontMgr* SkFontMgr_New_FCI(SkFontConfigInterface* fci) { | 298 SK_API SkFontMgr* SkFontMgr_New_FCI(SkFontConfigInterface* fci) { |
298 SkASSERT(fci); | 299 SkASSERT(fci); |
299 return new SkFontMgr_FCI(fci); | 300 return new SkFontMgr_FCI(fci); |
300 } | 301 } |
OLD | NEW |