reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 1 | /* |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | ** Copyright 2006, The Android Open Source Project |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
reed@android.com | f13c6e1 | 2009-01-19 19:10:24 +0000 | [diff] [blame] | 17 | #include <carbon/carbon.h> |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 18 | #include "SkFontHost.h" |
| 19 | #include "SkDescriptor.h" |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 20 | #include "SkEndian.h" |
reed@android.com | f13c6e1 | 2009-01-19 19:10:24 +0000 | [diff] [blame] | 21 | #include "SkPoint.h" |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 22 | |
| 23 | // Give 1MB font cache budget |
| 24 | #define FONT_CACHE_MEMORY_BUDGET (1024 * 1024) |
| 25 | |
| 26 | const char* gDefaultfont = "Arial"; // hard code for now |
| 27 | static SkMutex gFTMutex; |
| 28 | |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 29 | static inline SkPoint F32PtToSkPoint(const Float32Point p) { |
| 30 | SkPoint sp = { SkFloatToScalar(p.x), SkFloatToScalar(p.y) }; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 31 | return sp; |
| 32 | } |
| 33 | |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 34 | static inline uint32_t _rotl(uint32_t v, uint32_t r) { |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 35 | return (v << r | v >> (32 - r)); |
| 36 | } |
| 37 | |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 38 | class SkTypeface_Mac : public SkTypeface { |
| 39 | public: |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 40 | SkTypeface_Mac(SkTypeface::Style style, uint32_t id) |
| 41 | : SkTypeface(style, id) {} |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #pragma mark - |
| 45 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 46 | static uint32_t find_from_name(const char name[]) { |
| 47 | CFStringRef str = CFStringCreateWithCString(NULL, name, |
| 48 | kCFStringEncodingUTF8); |
| 49 | uint32_t fontID = ::ATSFontFindFromName(str, kATSOptionFlagsDefault); |
| 50 | CFRelease(str); |
| 51 | return fontID; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 52 | } |
| 53 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 54 | static uint32_t find_default_fontID() { |
| 55 | static const char* gDefaultNames[] = { "Arial", "Tahoma", "Helvetica" }; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 56 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 57 | uint32_t fontID; |
| 58 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); i++) { |
| 59 | fontID = find_from_name(gDefaultNames[i]); |
| 60 | if (fontID) { |
| 61 | return fontID; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 62 | } |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 63 | } |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 64 | sk_throw(); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static SkTypeface* CreateTypeface_(const char name[], SkTypeface::Style style) { |
| 69 | uint32_t fontID = 0; |
| 70 | if (NULL != name) { |
| 71 | fontID = find_from_name(name); |
| 72 | } |
| 73 | if (0 == fontID) { |
| 74 | fontID = find_default_fontID(); |
| 75 | } |
| 76 | // we lie (for now) and report that we found the exact style bits |
| 77 | return new SkTypeface_Mac(style, fontID); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | #pragma mark - |
| 81 | |
| 82 | class SkScalerContext_Mac : public SkScalerContext { |
| 83 | public: |
| 84 | SkScalerContext_Mac(const SkDescriptor* desc); |
| 85 | virtual ~SkScalerContext_Mac(); |
| 86 | |
| 87 | protected: |
| 88 | virtual unsigned generateGlyphCount() const; |
| 89 | virtual uint16_t generateCharToGlyph(SkUnichar uni); |
| 90 | virtual void generateAdvance(SkGlyph* glyph); |
| 91 | virtual void generateMetrics(SkGlyph* glyph); |
| 92 | virtual void generateImage(const SkGlyph& glyph); |
| 93 | virtual void generatePath(const SkGlyph& glyph, SkPath* path); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 94 | virtual void generateFontMetrics(SkPaint::FontMetrics* mX, SkPaint::FontMetrics* mY); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | ATSUTextLayout fLayout; |
| 98 | ATSUStyle fStyle; |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 99 | CGColorSpaceRef fGrayColorSpace; |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 100 | CGAffineTransform fTransform; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 101 | |
| 102 | static OSStatus MoveTo(const Float32Point *pt, void *cb); |
| 103 | static OSStatus Line(const Float32Point *pt, void *cb); |
| 104 | static OSStatus Curve(const Float32Point *pt1, const Float32Point *pt2, const Float32Point *pt3, void *cb); |
| 105 | static OSStatus Close(void *cb); |
| 106 | }; |
| 107 | |
| 108 | SkScalerContext_Mac::SkScalerContext_Mac(const SkDescriptor* desc) |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 109 | : SkScalerContext(desc), fLayout(0), fStyle(0) |
| 110 | { |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 111 | SkAutoMutexAcquire ac(gFTMutex); |
| 112 | OSStatus err; |
| 113 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 114 | err = ::ATSUCreateStyle(&fStyle); |
| 115 | SkASSERT(0 == err); |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 116 | |
| 117 | SkMatrix m; |
| 118 | fRec.getSingleMatrix(&m); |
| 119 | |
| 120 | fTransform = CGAffineTransformMake(SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 121 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 122 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 123 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 124 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 125 | SkScalarToFloat(m[SkMatrix::kMTransY])); |
| 126 | |
reed@android.com | 590ef3f | 2009-03-10 04:02:30 +0000 | [diff] [blame] | 127 | ATSStyleRenderingOptions renderOpts = kATSStyleApplyAntiAliasing; |
| 128 | switch (fRec.fHints) { |
| 129 | case kNo_Hints: |
| 130 | renderOpts |= kATSStyleNoHinting; |
| 131 | break; |
| 132 | case kSubpixel_Hints: |
| 133 | // hmmm, need to support subpixel... from path? |
| 134 | renderOpts |= kATSStyleNoHinting; |
| 135 | break; |
| 136 | case kNormal_Hints: |
| 137 | renderOpts |= kATSStyleApplyHints; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | ATSUFontID fontID = FMGetFontFromATSFontRef(fRec.fFontID); |
| 142 | // we put everything in the matrix, so our pt size is just 1.0 |
| 143 | Fixed fixedSize = SK_Fixed1; |
| 144 | static const ATSUAttributeTag tags[] = { |
| 145 | kATSUFontTag, kATSUSizeTag, kATSUFontMatrixTag, kATSUStyleRenderingOptionsTag |
| 146 | }; |
| 147 | static const ByteCount sizes[] = { |
| 148 | sizeof(fontID), sizeof(fixedSize), sizeof(fTransform), sizeof(renderOpts) |
| 149 | }; |
| 150 | const ATSUAttributeValuePtr values[] = { |
| 151 | &fontID, &fixedSize, &fTransform, &renderOpts |
| 152 | }; |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 153 | err = ::ATSUSetAttributes(fStyle, SK_ARRAY_COUNT(tags), |
| 154 | tags, sizes, values); |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 155 | SkASSERT(0 == err); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 156 | |
| 157 | err = ::ATSUCreateTextLayout(&fLayout); |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 158 | SkASSERT(0 == err); |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 159 | |
| 160 | fGrayColorSpace = ::CGColorSpaceCreateDeviceGray(); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 163 | SkScalerContext_Mac::~SkScalerContext_Mac() { |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 164 | ::CGColorSpaceRelease(fGrayColorSpace); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 165 | ::ATSUDisposeTextLayout(fLayout); |
| 166 | ::ATSUDisposeStyle(fStyle); |
| 167 | } |
| 168 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 169 | unsigned SkScalerContext_Mac::generateGlyphCount() const { |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 170 | return 0xFFFF; |
| 171 | } |
| 172 | |
| 173 | uint16_t SkScalerContext_Mac::generateCharToGlyph(SkUnichar uni) |
| 174 | { |
| 175 | SkAutoMutexAcquire ac(gFTMutex); |
| 176 | |
| 177 | OSStatus err; |
| 178 | UniChar achar = uni; |
| 179 | err = ::ATSUSetTextPointerLocation(fLayout,&achar,0,1,1); |
| 180 | err = ::ATSUSetRunStyle(fLayout,fStyle,kATSUFromTextBeginning,kATSUToTextEnd); |
| 181 | |
| 182 | ATSLayoutRecord *layoutPtr; |
| 183 | ItemCount count; |
| 184 | ATSGlyphRef glyph; |
| 185 | |
| 186 | err = ::ATSUDirectGetLayoutDataArrayPtrFromTextLayout(fLayout,0,kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,(void**)&layoutPtr,&count); |
| 187 | glyph = layoutPtr->glyphID; |
| 188 | ::ATSUDirectReleaseLayoutDataArrayPtr(NULL,kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,(void**)&layoutPtr); |
| 189 | return glyph; |
| 190 | } |
| 191 | |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 192 | static void set_glyph_metrics_on_error(SkGlyph* glyph) { |
| 193 | glyph->fRsbDelta = 0; |
| 194 | glyph->fLsbDelta = 0; |
| 195 | glyph->fWidth = 0; |
| 196 | glyph->fHeight = 0; |
| 197 | glyph->fTop = 0; |
| 198 | glyph->fLeft = 0; |
| 199 | glyph->fAdvanceX = 0; |
| 200 | glyph->fAdvanceY = 0; |
| 201 | } |
| 202 | |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 203 | void SkScalerContext_Mac::generateAdvance(SkGlyph* glyph) { |
| 204 | this->generateMetrics(glyph); |
| 205 | } |
| 206 | |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 207 | void SkScalerContext_Mac::generateMetrics(SkGlyph* glyph) { |
| 208 | GlyphID glyphID = glyph->getGlyphID(fBaseGlyphCount); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 209 | ATSGlyphScreenMetrics screenMetrics; |
| 210 | ATSGlyphIdealMetrics idealMetrics; |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 211 | |
| 212 | OSStatus err = ATSUGlyphGetScreenMetrics(fStyle, 1, &glyphID, 0, true, true, |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 213 | &screenMetrics); |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 214 | if (noErr != err) { |
| 215 | set_glyph_metrics_on_error(glyph); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 216 | return; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 217 | } |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 218 | err = ATSUGlyphGetIdealMetrics(fStyle, 1, &glyphID, 0, &idealMetrics); |
| 219 | if (noErr != err) { |
| 220 | set_glyph_metrics_on_error(glyph); |
| 221 | return; |
| 222 | } |
reed@android.com | 590ef3f | 2009-03-10 04:02:30 +0000 | [diff] [blame] | 223 | |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 224 | if (kNormal_Hints == fRec.fHints) { |
| 225 | glyph->fAdvanceX = SkFloatToFixed(screenMetrics.deviceAdvance.x); |
| 226 | glyph->fAdvanceY = -SkFloatToFixed(screenMetrics.deviceAdvance.y); |
| 227 | } else { |
| 228 | glyph->fAdvanceX = SkFloatToFixed(idealMetrics.advance.x); |
| 229 | glyph->fAdvanceY = -SkFloatToFixed(idealMetrics.advance.y); |
| 230 | } |
| 231 | |
| 232 | // specify an extra 1-pixel border, go tive CG room for its antialiasing |
| 233 | // i.e. without this, I was seeing some edges chopped off! |
| 234 | glyph->fWidth = screenMetrics.width + 2; |
| 235 | glyph->fHeight = screenMetrics.height + 2; |
| 236 | glyph->fLeft = sk_float_round2int(screenMetrics.topLeft.x) - 1; |
| 237 | glyph->fTop = -sk_float_round2int(screenMetrics.topLeft.y) - 1; |
| 238 | } |
| 239 | |
| 240 | void SkScalerContext_Mac::generateImage(const SkGlyph& glyph) |
| 241 | { |
| 242 | SkAutoMutexAcquire ac(gFTMutex); |
| 243 | SkASSERT(fLayout); |
| 244 | |
| 245 | bzero(glyph.fImage, glyph.fHeight * glyph.rowBytes()); |
| 246 | CGContextRef contextRef = ::CGBitmapContextCreate(glyph.fImage, |
| 247 | glyph.fWidth, glyph.fHeight, 8, |
| 248 | glyph.rowBytes(), fGrayColorSpace, |
| 249 | kCGImageAlphaNone); |
| 250 | if (!contextRef) { |
| 251 | SkASSERT(false); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | ::CGContextSetGrayFillColor(contextRef, 1.0, 1.0); |
| 256 | ::CGContextSetTextDrawingMode(contextRef, kCGTextFill); |
| 257 | |
reed@android.com | 590ef3f | 2009-03-10 04:02:30 +0000 | [diff] [blame] | 258 | CGGlyph glyphID = glyph.getGlyphID(fBaseGlyphCount); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 259 | CGFontRef fontRef = CGFontCreateWithPlatformFont(&fRec.fFontID); |
| 260 | CGContextSetFont(contextRef, fontRef); |
| 261 | CGContextSetFontSize(contextRef, 1); |
| 262 | CGContextSetTextMatrix(contextRef, fTransform); |
| 263 | CGContextShowGlyphsAtPoint(contextRef, -glyph.fLeft, |
| 264 | glyph.fTop + glyph.fHeight, &glyphID, 1); |
| 265 | |
| 266 | ::CGContextRelease(contextRef); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 267 | } |
| 268 | |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 269 | static void convert_metrics(SkPaint::FontMetrics* dst, |
| 270 | const ATSFontMetrics& src) { |
| 271 | dst->fTop = -SkFloatToScalar(src.ascent); |
| 272 | dst->fAscent = -SkFloatToScalar(src.ascent); |
| 273 | dst->fDescent = SkFloatToScalar(src.descent); |
| 274 | dst->fBottom = SkFloatToScalar(src.descent); |
| 275 | dst->fLeading = SkFloatToScalar(src.leading); |
| 276 | } |
| 277 | |
| 278 | static void* get_font_table(ATSFontRef fontID, uint32_t tag) { |
| 279 | ByteCount size; |
| 280 | OSStatus err = ATSFontGetTable(fontID, tag, 0, 0, NULL, &size); |
| 281 | if (err) { |
| 282 | return NULL; |
| 283 | } |
| 284 | void* data = sk_malloc_throw(size); |
| 285 | err = ATSFontGetTable(fontID, tag, 0, size, data, &size); |
| 286 | if (err) { |
| 287 | sk_free(data); |
| 288 | data = NULL; |
| 289 | } |
| 290 | return data; |
| 291 | } |
| 292 | |
| 293 | static int get_be16(const void* data, size_t offset) { |
| 294 | const char* ptr = reinterpret_cast<const char*>(data); |
| 295 | uint16_t value = *reinterpret_cast<const uint16_t*>(ptr + offset); |
| 296 | int n = SkEndian_SwapBE16(value); |
| 297 | // now force it to be signed |
| 298 | return n << 16 >> 16; |
| 299 | } |
| 300 | |
| 301 | #define SFNT_HEAD_UPEM_OFFSET 18 |
| 302 | #define SFNT_HEAD_YMIN_OFFSET 38 |
| 303 | #define SFNT_HEAD_YMAX_OFFSET 42 |
| 304 | #define SFNT_HEAD_STYLE_OFFSET 44 |
| 305 | |
| 306 | #define SFNT_HHEA_ASCENT_OFFSET 4 |
| 307 | #define SFNT_HHEA_DESCENT_OFFSET 6 |
| 308 | #define SFNT_HHEA_LEADING_OFFSET 8 |
| 309 | |
| 310 | static bool init_vertical_metrics(ATSFontRef font, SkPoint pts[5]) { |
| 311 | void* head = get_font_table(font, 'head'); |
| 312 | if (NULL == head) { |
| 313 | return false; |
| 314 | } |
| 315 | void* hhea = get_font_table(font, 'hhea'); |
| 316 | if (NULL == hhea) { |
| 317 | sk_free(head); |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | int upem = get_be16(head, SFNT_HEAD_UPEM_OFFSET); |
| 322 | int ys[5]; |
| 323 | |
| 324 | ys[0] = -get_be16(head, SFNT_HEAD_YMAX_OFFSET); |
| 325 | ys[1] = -get_be16(hhea, SFNT_HHEA_ASCENT_OFFSET); |
| 326 | ys[2] = -get_be16(hhea, SFNT_HHEA_DESCENT_OFFSET); |
| 327 | ys[3] = -get_be16(head, SFNT_HEAD_YMIN_OFFSET); |
| 328 | ys[4] = get_be16(hhea, SFNT_HHEA_LEADING_OFFSET); |
| 329 | |
| 330 | // now do some cleanup, to ensure y[max,min] are really that |
| 331 | if (ys[0] > ys[1]) { |
| 332 | ys[0] = ys[1]; |
| 333 | } |
| 334 | if (ys[3] < ys[2]) { |
| 335 | ys[3] = ys[2]; |
| 336 | } |
| 337 | |
| 338 | for (int i = 0; i < 5; i++) { |
| 339 | pts[i].set(0, SkIntToScalar(ys[i]) / upem); |
| 340 | } |
| 341 | |
| 342 | sk_free(hhea); |
| 343 | sk_free(head); |
| 344 | return true; |
| 345 | } |
| 346 | |
reed@android.com | 76aa34b | 2008-12-23 01:27:39 +0000 | [diff] [blame] | 347 | void SkScalerContext_Mac::generateFontMetrics(SkPaint::FontMetrics* mx, |
| 348 | SkPaint::FontMetrics* my) { |
reed@android.com | 0bf64d4 | 2009-03-09 17:22:22 +0000 | [diff] [blame] | 349 | SkPoint pts[5]; |
| 350 | |
| 351 | if (!init_vertical_metrics(fRec.fFontID, pts)) { |
| 352 | // these are not as accurate as init_vertical_metrics :( |
| 353 | ATSFontMetrics metrics; |
| 354 | ATSFontGetVerticalMetrics(fRec.fFontID, kATSOptionFlagsDefault, |
| 355 | &metrics); |
| 356 | pts[0].set(0, -SkFloatToScalar(metrics.ascent)); |
| 357 | pts[1].set(0, -SkFloatToScalar(metrics.ascent)); |
| 358 | pts[2].set(0, -SkFloatToScalar(metrics.descent)); |
| 359 | pts[3].set(0, -SkFloatToScalar(metrics.descent)); |
| 360 | pts[4].set(0, SkFloatToScalar(metrics.leading)); //+ or -? |
| 361 | } |
| 362 | |
| 363 | SkMatrix m; |
| 364 | fRec.getSingleMatrix(&m); |
| 365 | m.mapPoints(pts, 5); |
| 366 | |
| 367 | if (mx) { |
| 368 | mx->fTop = pts[0].fX; |
| 369 | mx->fAscent = pts[1].fX; |
| 370 | mx->fDescent = pts[2].fX; |
| 371 | mx->fBottom = pts[3].fX; |
| 372 | mx->fLeading = pts[4].fX; |
| 373 | } |
| 374 | if (my) { |
| 375 | my->fTop = pts[0].fY; |
| 376 | my->fAscent = pts[1].fY; |
| 377 | my->fDescent = pts[2].fY; |
| 378 | my->fBottom = pts[3].fY; |
| 379 | my->fLeading = pts[4].fY; |
| 380 | } |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 381 | } |
| 382 | |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 383 | void SkScalerContext_Mac::generatePath(const SkGlyph& glyph, SkPath* path) |
| 384 | { |
| 385 | SkAutoMutexAcquire ac(gFTMutex); |
| 386 | OSStatus err,result; |
| 387 | |
| 388 | err = ::ATSUGlyphGetCubicPaths( |
| 389 | fStyle,glyph.fID, |
| 390 | &SkScalerContext_Mac::MoveTo, |
| 391 | &SkScalerContext_Mac::Line, |
| 392 | &SkScalerContext_Mac::Curve, |
| 393 | &SkScalerContext_Mac::Close, |
| 394 | path,&result); |
| 395 | SkASSERT(err == noErr); |
| 396 | } |
| 397 | |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 398 | OSStatus SkScalerContext_Mac::MoveTo(const Float32Point *pt, void *cb) |
| 399 | { |
| 400 | reinterpret_cast<SkPath*>(cb)->moveTo(F32PtToSkPoint(*pt)); |
| 401 | return noErr; |
| 402 | } |
| 403 | |
| 404 | OSStatus SkScalerContext_Mac::Line(const Float32Point *pt, void *cb) |
| 405 | { |
| 406 | reinterpret_cast<SkPath*>(cb)->lineTo(F32PtToSkPoint(*pt)); |
| 407 | return noErr; |
| 408 | } |
| 409 | |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 410 | OSStatus SkScalerContext_Mac::Curve(const Float32Point *pt1, |
| 411 | const Float32Point *pt2, |
| 412 | const Float32Point *pt3, void *cb) |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 413 | { |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 414 | reinterpret_cast<SkPath*>(cb)->cubicTo(F32PtToSkPoint(*pt1), |
| 415 | F32PtToSkPoint(*pt2), |
| 416 | F32PtToSkPoint(*pt3)); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 417 | return noErr; |
| 418 | } |
| 419 | |
| 420 | OSStatus SkScalerContext_Mac::Close(void *cb) |
| 421 | { |
| 422 | reinterpret_cast<SkPath*>(cb)->close(); |
| 423 | return noErr; |
| 424 | } |
| 425 | |
| 426 | #pragma mark - |
| 427 | |
| 428 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
| 429 | SkASSERT(!"SkFontHost::Serialize unimplemented"); |
| 430 | } |
| 431 | |
| 432 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
| 433 | SkASSERT(!"SkFontHost::Deserialize unimplemented"); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 437 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 438 | return NULL; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 439 | } |
| 440 | |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 441 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 442 | return NULL; |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 443 | } |
| 444 | |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 445 | SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) { |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 446 | return new SkScalerContext_Mac(desc); |
| 447 | } |
| 448 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame^] | 449 | uint32_t SkFontHost::NextLogicalFont(uint32_t fontID) { |
| 450 | uint32_t newFontID = find_default_fontID(); |
| 451 | if (newFontID == fontID) { |
| 452 | newFontID = 0; |
| 453 | } |
| 454 | return newFontID; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 455 | } |
| 456 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 457 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 458 | const char familyName[], SkTypeface::Style style) { |
reed@android.com | ea446b9 | 2009-03-09 15:25:11 +0000 | [diff] [blame] | 459 | // todo: we don't know how to respect style bits |
| 460 | if (NULL == familyName && NULL != familyFace) { |
| 461 | familyFace->ref(); |
| 462 | return const_cast<SkTypeface*>(familyFace); |
| 463 | } else { |
| 464 | return CreateTypeface_(familyName, style); |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 465 | } |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { |
| 469 | if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) |
| 470 | return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; |
| 471 | else |
| 472 | return 0; // nothing to do |
| 473 | } |
| 474 | |
| 475 | int SkFontHost::ComputeGammaFlag(const SkPaint& paint) { |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | void SkFontHost::GetGammaTables(const uint8_t* tables[2]) { |
| 480 | tables[0] = NULL; // black gamma (e.g. exp=1.4) |
| 481 | tables[1] = NULL; // white gamma (e.g. exp= 1/1.4) |
| 482 | } |
| 483 | |