OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkFont.h" | 8 #include "SkFont.h" |
9 #include "SkTypeface.h" | 9 #include "SkTypeface.h" |
10 #include "SkUtils.h" | 10 #include "SkUtils.h" |
11 | 11 |
12 SkFont::SkFont(sk_sp<SkTypeface> face, SkScalar size, SkScalar scaleX, SkScalar
skewX, MaskType mt, | 12 static SkTypeface* ref_or_default(SkTypeface* face) { |
| 13 return face ? SkRef(face) : SkTypeface::RefDefault(); |
| 14 } |
| 15 |
| 16 SkFont::SkFont(SkTypeface* face, SkScalar size, SkScalar scaleX, SkScalar skewX,
MaskType mt, |
13 uint32_t flags) | 17 uint32_t flags) |
14 : fTypeface(face ? std::move(face) : SkTypeface::MakeDefault()) | 18 : fTypeface(ref_or_default(face)) |
15 , fSize(size) | 19 , fSize(size) |
16 , fScaleX(scaleX) | 20 , fScaleX(scaleX) |
17 , fSkewX(skewX) | 21 , fSkewX(skewX) |
18 , fFlags(flags) | 22 , fFlags(flags) |
19 , fMaskType(SkToU8(mt)) | 23 , fMaskType(SkToU8(mt)) |
20 { | 24 { |
21 SkASSERT(size > 0); | 25 SkASSERT(size > 0); |
22 SkASSERT(scaleX > 0); | 26 SkASSERT(scaleX > 0); |
23 SkASSERT(SkScalarIsFinite(skewX)); | 27 SkASSERT(SkScalarIsFinite(skewX)); |
24 SkASSERT(0 == (flags & ~kAllFlags)); | 28 SkASSERT(0 == (flags & ~kAllFlags)); |
25 } | 29 } |
26 | 30 |
27 sk_sp<SkFont> SkFont::Make(sk_sp<SkTypeface> face, SkScalar size, SkScalar scale
X, SkScalar skewX, | 31 SkFont* SkFont::Create(SkTypeface* face, SkScalar size, SkScalar scaleX, SkScala
r skewX, |
28 MaskType mt, uint32_t flags) { | 32 MaskType mt, uint32_t flags) { |
29 if (size <= 0 || !SkScalarIsFinite(size)) { | 33 if (size <= 0 || !SkScalarIsFinite(size)) { |
30 return nullptr; | 34 return nullptr; |
31 } | 35 } |
32 if (scaleX <= 0 || !SkScalarIsFinite(scaleX)) { | 36 if (scaleX <= 0 || !SkScalarIsFinite(scaleX)) { |
33 return nullptr; | 37 return nullptr; |
34 } | 38 } |
35 if (!SkScalarIsFinite(skewX)) { | 39 if (!SkScalarIsFinite(skewX)) { |
36 return nullptr; | 40 return nullptr; |
37 } | 41 } |
38 flags &= kAllFlags; | 42 flags &= kAllFlags; |
39 return sk_sp<SkFont>(new SkFont(std::move(face), size, scaleX, skewX, mt, fl
ags)); | 43 return new SkFont(face, size, scaleX, skewX, mt, flags); |
40 } | 44 } |
41 | 45 |
42 sk_sp<SkFont> SkFont::Make(sk_sp<SkTypeface> face, SkScalar size, MaskType mt, u
int32_t flags) { | 46 SkFont* SkFont::Create(SkTypeface* face, SkScalar size, MaskType mt, uint32_t fl
ags) { |
43 return SkFont::Make(std::move(face), size, 1, 0, mt, flags); | 47 return SkFont::Create(face, size, 1, 0, mt, flags); |
44 } | 48 } |
45 | 49 |
46 sk_sp<SkFont> SkFont::makeWithSize(SkScalar newSize) const { | 50 SkFont* SkFont::cloneWithSize(SkScalar newSize) const { |
47 return SkFont::Make(sk_ref_sp(this->getTypeface()), newSize, this->getScaleX
(), | 51 return SkFont::Create(this->getTypeface(), newSize, this->getScaleX(), this-
>getSkewX(), |
48 this->getSkewX(), this->getMaskType(), this->getFlags())
; | 52 this->getMaskType(), this->getFlags()); |
49 } | 53 } |
50 | 54 |
51 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 55 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
52 | 56 |
| 57 SkFont::~SkFont() { |
| 58 SkSafeUnref(fTypeface); |
| 59 } |
| 60 |
53 int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding enc
oding, | 61 int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding enc
oding, |
54 uint16_t glyphs[], int maxGlyphCount) const { | 62 uint16_t glyphs[], int maxGlyphCount) const { |
55 if (0 == byteLength) { | 63 if (0 == byteLength) { |
56 return 0; | 64 return 0; |
57 } | 65 } |
58 | 66 |
59 SkASSERT(text); | 67 SkASSERT(text); |
60 | 68 |
61 int count = 0; // fix uninitialized warning (even though the switch is comp
lete!) | 69 int count = 0; // fix uninitialized warning (even though the switch is comp
lete!) |
62 | 70 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 111 |
104 SkScalar SkFont::measureText(const void* text, size_t byteLength, SkTextEncoding
encoding) const { | 112 SkScalar SkFont::measureText(const void* text, size_t byteLength, SkTextEncoding
encoding) const { |
105 // TODO: need access to the cache | 113 // TODO: need access to the cache |
106 return -1; | 114 return -1; |
107 } | 115 } |
108 | 116 |
109 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 117 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
110 | 118 |
111 #include "SkPaint.h" | 119 #include "SkPaint.h" |
112 | 120 |
113 sk_sp<SkFont> SkFont::Testing_CreateFromPaint(const SkPaint& paint) { | 121 SkFont* SkFont::Testing_CreateFromPaint(const SkPaint& paint) { |
114 uint32_t flags = 0; | 122 uint32_t flags = 0; |
115 if (paint.isVerticalText()) { | 123 if (paint.isVerticalText()) { |
116 flags |= kVertical_Flag; | 124 flags |= kVertical_Flag; |
117 } | 125 } |
118 if (paint.isEmbeddedBitmapText()) { | 126 if (paint.isEmbeddedBitmapText()) { |
119 flags |= kEmbeddedBitmaps_Flag; | 127 flags |= kEmbeddedBitmaps_Flag; |
120 } | 128 } |
121 if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) { | 129 if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) { |
122 flags |= kGenA8FromLCD_Flag; | 130 flags |= kGenA8FromLCD_Flag; |
123 } | 131 } |
(...skipping 11 matching lines...) Expand all Loading... |
135 // this is our default | 143 // this is our default |
136 } else { | 144 } else { |
137 flags |= kUseNonlinearMetrics_Flag; | 145 flags |= kUseNonlinearMetrics_Flag; |
138 } | 146 } |
139 | 147 |
140 MaskType maskType = SkFont::kBW_MaskType; | 148 MaskType maskType = SkFont::kBW_MaskType; |
141 if (paint.isAntiAlias()) { | 149 if (paint.isAntiAlias()) { |
142 maskType = paint.isLCDRenderText() ? kLCD_MaskType : kA8_MaskType; | 150 maskType = paint.isLCDRenderText() ? kLCD_MaskType : kA8_MaskType; |
143 } | 151 } |
144 | 152 |
145 return Make(sk_ref_sp(paint.getTypeface()), paint.getTextSize(), paint.getTe
xtScaleX(), | 153 return Create(paint.getTypeface(), |
146 paint.getTextSkewX(), maskType, flags); | 154 paint.getTextSize(), paint.getTextScaleX(), paint.getTextSkewX
(), |
| 155 maskType, flags); |
147 } | 156 } |
OLD | NEW |