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
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkFont.h"
#include "SkTypeface.h"
#include "SkUtils.h"
static SkTypeface* ref_or_default(SkTypeface* face) {
return face ? SkRef(face) : SkTypeface::RefDefault();
}
SkFont::SkFont(SkTypeface* face, SkScalar size, SkScalar scaleX, SkScalar skewX, MaskType mt,
uint32_t flags)
: fTypeface(ref_or_default(face))
, fSize(size)
, fScaleX(scaleX)
, fSkewX(skewX)
, fFlags(flags)
, fMaskType(SkToU8(mt))
{
SkASSERT(size > 0);
SkASSERT(scaleX > 0);
SkASSERT(SkScalarIsFinite(skewX));
SkASSERT(0 == (flags & ~kAllFlags));
}
SkFont* SkFont::Create(SkTypeface* face, SkScalar size, SkScalar scaleX, SkScalar skewX,
MaskType mt, uint32_t flags) {
if (size <= 0 || !SkScalarIsFinite(size)) {
return nullptr;
}
if (scaleX <= 0 || !SkScalarIsFinite(scaleX)) {
return nullptr;
}
if (!SkScalarIsFinite(skewX)) {
return nullptr;
}
flags &= kAllFlags;
return new SkFont(face, size, scaleX, skewX, mt, flags);
}
SkFont* SkFont::Create(SkTypeface* face, SkScalar size, MaskType mt, uint32_t flags) {
return SkFont::Create(face, size, 1, 0, mt, flags);
}
SkFont* SkFont::cloneWithSize(SkScalar newSize) const {
return SkFont::Create(this->getTypeface(), newSize, this->getScaleX(), this->getSkewX(),
this->getMaskType(), this->getFlags());
}
///////////////////////////////////////////////////////////////////////////////////////////////////
SkFont::~SkFont() {
SkSafeUnref(fTypeface);
}
int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
uint16_t glyphs[], int maxGlyphCount) const {
if (0 == byteLength) {
return 0;
}
SkASSERT(text);
int count = 0; // fix uninitialized warning (even though the switch is complete!)
switch (encoding) {
case kUTF8_SkTextEncoding:
count = SkUTF8_CountUnichars((const char*)text, byteLength);
break;
case kUTF16_SkTextEncoding:
count = SkUTF16_CountUnichars((const uint16_t*)text, SkToInt(byteLength >> 1));
break;
case kUTF32_SkTextEncoding:
count = SkToInt(byteLength >> 2);
break;
case kGlyphID_SkTextEncoding:
count = SkToInt(byteLength >> 1);
break;
}
if (nullptr == glyphs) {
return count;
}
// TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding
SkTypeface::Encoding typeface_encoding;
switch (encoding) {
case kUTF8_SkTextEncoding:
typeface_encoding = SkTypeface::kUTF8_Encoding;
break;
case kUTF16_SkTextEncoding:
typeface_encoding = SkTypeface::kUTF16_Encoding;
break;
case kUTF32_SkTextEncoding:
typeface_encoding = SkTypeface::kUTF32_Encoding;
break;
default:
SkASSERT(kGlyphID_SkTextEncoding == encoding);
// we can early exit, since we already have glyphIDs
memcpy(glyphs, text, count << 1);
return count;
}
(void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count);
return count;
}
SkScalar SkFont::measureText(const void* text, size_t byteLength, SkTextEncoding encoding) const {
// TODO: need access to the cache
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "SkPaint.h"
SkFont* SkFont::Testing_CreateFromPaint(const SkPaint& paint) {
uint32_t flags = 0;
if (paint.isVerticalText()) {
flags |= kVertical_Flag;
}
if (paint.isEmbeddedBitmapText()) {
flags |= kEmbeddedBitmaps_Flag;
}
if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) {
flags |= kGenA8FromLCD_Flag;
}
if (paint.isFakeBoldText()) {
flags |= kEmbolden_Flag;
}
if (SkPaint::kFull_Hinting == paint.getHinting()) {
flags |= kEnableByteCodeHints_Flag;
}
if (paint.isAutohinted()) {
flags |= kEnableAutoHints_Flag;
}
if (paint.isSubpixelText() || paint.isLinearText()) {
// this is our default
} else {
flags |= kUseNonlinearMetrics_Flag;
}
MaskType maskType = SkFont::kBW_MaskType;
if (paint.isAntiAlias()) {
maskType = paint.isLCDRenderText() ? kLCD_MaskType : kA8_MaskType;
}
return Create(paint.getTypeface(),
paint.getTextSize(), paint.getTextScaleX(), paint.getTextSkewX(),
maskType, flags);
}
|