Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: src/fonts/SkGScalerContext.cpp

Issue 14890016: start a wrapper for color fonts (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« include/core/SkTypeface.h ('K') | « src/fonts/SkGScalerContext.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkGScalerContext.h"
9 #include "SkGlyph.h"
10 #include "SkPath.h"
11 #include "SkCanvas.h"
12
13 class SkGScalerContext : public SkScalerContext {
14 public:
15 SkGScalerContext(SkGTypeface*, const SkDescriptor*);
16 virtual ~SkGScalerContext();
17
18 protected:
19 virtual unsigned generateGlyphCount() SK_OVERRIDE;
20 virtual uint16_t generateCharToGlyph(SkUnichar) SK_OVERRIDE;
21 virtual void generateAdvance(SkGlyph*) SK_OVERRIDE;
22 virtual void generateMetrics(SkGlyph*) SK_OVERRIDE;
23 virtual void generateImage(const SkGlyph&) SK_OVERRIDE;
24 virtual void generatePath(const SkGlyph&, SkPath*) SK_OVERRIDE;
25 virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
26 SkPaint::FontMetrics* mY) SK_OVERRIDE;
27
28 private:
29 SkGTypeface* fFace;
30 SkScalerContext* fProxy;
31 SkMatrix fMatrix;
32 };
33
34 #define STD_SIZE 1
35
36 #include "SkDescriptor.h"
37
38 SkGScalerContext::SkGScalerContext(SkGTypeface* face, const SkDescriptor* desc)
39 : SkScalerContext(face, desc)
40 , fFace(face)
41 {
42
43 size_t descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext ::Rec);
44 SkAutoDescriptor ad(descSize);
45 SkDescriptor* newDesc = ad.getDesc();
46
47 newDesc->init();
48 void* entry = newDesc->addEntry(kRec_SkDescriptorTag,
49 sizeof(SkScalerContext::Rec), &fRec);
50 {
51 SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry;
52 rec->fTextSize = STD_SIZE;
53 rec->fPreScaleX = SK_Scalar1;
54 rec->fPreSkewX = 0;
55 rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1;
56 rec->fPost2x2[1][0] = rec->fPost2x2[0][1] = 0;
57 }
58 SkASSERT(descSize == newDesc->getLength());
59 newDesc->computeChecksum();
60
61 fProxy = face->proxy()->createScalerContext(newDesc);
62
63 fRec.getSingleMatrix(&fMatrix);
64 fMatrix.preScale(SK_Scalar1 / STD_SIZE, SK_Scalar1 / STD_SIZE);
65 }
66
67 SkGScalerContext::~SkGScalerContext() {
68 SkDELETE(fProxy);
69 }
70
71 unsigned SkGScalerContext::generateGlyphCount() {
72 return fProxy->getGlyphCount();
73 }
74
75 uint16_t SkGScalerContext::generateCharToGlyph(SkUnichar uni) {
76 return fProxy->charToGlyphID(uni);
77 }
78
79 void SkGScalerContext::generateAdvance(SkGlyph* glyph) {
80 fProxy->getAdvance(glyph);
81
82 SkVector advance;
83 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
84 SkFixedToScalar(glyph->fAdvanceY), &advance);
85 glyph->fAdvanceX = SkScalarToFixed(advance.fX);
86 glyph->fAdvanceY = SkScalarToFixed(advance.fY);
87 }
88
89 void SkGScalerContext::generateMetrics(SkGlyph* glyph) {
90 fProxy->getMetrics(glyph);
91
92 SkVector advance;
93 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
94 SkFixedToScalar(glyph->fAdvanceY), &advance);
95 glyph->fAdvanceX = SkScalarToFixed(advance.fX);
96 glyph->fAdvanceY = SkScalarToFixed(advance.fY);
97
98 SkPath path;
99 fProxy->getPath(*glyph, &path);
100 path.transform(fMatrix);
101
102 SkRect storage;
103 const SkPaint& paint = fFace->paint();
104 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
105 &storage,
106 SkPaint::kFill_Style);
107 SkIRect ibounds;
108 newBounds.roundOut(&ibounds);
109 glyph->fLeft = ibounds.fLeft;
110 glyph->fTop = ibounds.fTop;
111 glyph->fWidth = ibounds.width();
112 glyph->fHeight = ibounds.height();
113 glyph->fMaskFormat = SkMask::kARGB32_Format;
114 }
115
116 void SkGScalerContext::generateImage(const SkGlyph& glyph) {
117 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
118 SkPath path;
119 fProxy->getPath(glyph, &path);
120
121 SkBitmap bm;
122 bm.setConfig(SkBitmap::kARGB_8888_Config, glyph.fWidth, glyph.fHeight,
123 glyph.rowBytes());
124 bm.setPixels(glyph.fImage);
125 bm.eraseColor(0);
126
127 SkCanvas canvas(bm);
128 canvas.translate(-glyph.fLeft, -glyph.fTop);
129 canvas.concat(fMatrix);
130 canvas.drawPath(path, fFace->paint());
131 } else {
132 fProxy->getImage(glyph);
133 }
134 }
135
136 void SkGScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
137 fProxy->getPath(glyph, path);
138 path->transform(fMatrix);
139 }
140
141 void SkGScalerContext::generateFontMetrics(SkPaint::FontMetrics*,
142 SkPaint::FontMetrics* metrics) {
143 fProxy->getFontMetrics(metrics);
144 if (metrics) {
145 SkScalar scale = fMatrix.getScaleY();
146 metrics->fTop = SkScalarMul(metrics->fTop, scale);
147 metrics->fAscent = SkScalarMul(metrics->fAscent, scale);
148 metrics->fDescent = SkScalarMul(metrics->fDescent, scale);
149 metrics->fBottom = SkScalarMul(metrics->fBottom, scale);
150 metrics->fLeading = SkScalarMul(metrics->fLeading, scale);
151 metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale);
152 metrics->fXMin = SkScalarMul(metrics->fXMin, scale);
153 metrics->fXMax = SkScalarMul(metrics->fXMax, scale);
154 metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale);
155 }
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////
159
160 #include "SkTypefaceCache.h"
161
162 SkGTypeface::SkGTypeface(SkTypeface* proxy, const SkPaint& paint)
163 : SkTypeface(proxy->style(), SkTypefaceCache::NewFontID(), false)
164 , fProxy(SkRef(proxy))
165 , fPaint(paint) {}
166
167 SkGTypeface::~SkGTypeface() {
168 fProxy->unref();
169 }
170
171 SkScalerContext* SkGTypeface::onCreateScalerContext(
172 const SkDescriptor* desc) const {
173 return SkNEW_ARGS(SkGScalerContext, (const_cast<SkGTypeface*>(this), desc));
174 }
175
176 void SkGTypeface::onFilterRec(SkScalerContextRec* rec) const {
177 fProxy->filterRec(rec);
178 }
179
180 SkAdvancedTypefaceMetrics* SkGTypeface::onGetAdvancedTypefaceMetrics(
181 SkAdvancedTypefaceMetrics::PerGlyphInfo info,
182 const uint32_t* glyphIDs,
183 uint32_t glyphIDsCount) const {
184 return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
185 }
186
187 SkStream* SkGTypeface::onOpenStream(int* ttcIndex) const {
188 return fProxy->openStream(ttcIndex);
189 }
190
191 void SkGTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
192 bool* isLocal) const {
193 fProxy->getFontDescriptor(desc, isLocal);
194 }
195
196 int SkGTypeface::onGetUPEM() const {
197 return fProxy->getUnitsPerEm();
198 }
199
200 int SkGTypeface::onGetTableTags(SkFontTableTag tags[]) const {
201 return fProxy->getTableTags(tags);
202 }
203
204 size_t SkGTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
205 size_t length, void* data) const {
206 return fProxy->getTableData(tag, offset, length, data);
207 }
208
209 ///////////////////////////////////////////////////////////////////////////////
210
211 #if 0
212 // under construction -- defining a font purely in terms of skia primitives
213 // ala an SVG-font.
214 class SkGFont : public SkRefCnt {
215 public:
216 virtual ~SkGFont();
217
218 int unicharToGlyph(SkUnichar) const;
219
220 int countGlyphs() const { return fCount; }
221
222 float getAdvance(int index) const {
223 SkASSERT((unsigned)index < (unsigned)fCount);
224 return fGlyphs[index].fAdvance;
225 }
226
227 const SkPath& getPath(int index) const {
228 SkASSERT((unsigned)index < (unsigned)fCount);
229 return fGlyphs[index].fPath;
230 }
231
232 private:
233 struct Glyph {
234 SkUnichar fUni;
235 float fAdvance;
236 SkPath fPath;
237 };
238 int fCount;
239 Glyph* fGlyphs;
240
241 friend class SkGFontBuilder;
242 SkGFont(int count, Glyph* array);
243 };
244
245 class SkGFontBuilder {
246 public:
247
248 };
249 #endif
OLDNEW
« include/core/SkTypeface.h ('K') | « src/fonts/SkGScalerContext.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698