reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 "SkFont.h" |
| 9 | #include "SkTypeface.h" |
| 10 | #include "SkUtils.h" |
| 11 | |
| 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, |
| 17 | uint32_t flags) |
| 18 | : fTypeface(ref_or_default(face)) |
| 19 | , fSize(size) |
| 20 | , fScaleX(scaleX) |
| 21 | , fSkewX(skewX) |
| 22 | , fFlags(flags) |
| 23 | , fMaskType(SkToU8(mt)) |
| 24 | , fPad(0) |
| 25 | { |
| 26 | SkASSERT(size > 0); |
| 27 | SkASSERT(scaleX > 0); |
| 28 | SkASSERT(SkScalarIsFinite(skewX)); |
| 29 | SkASSERT(0 == (flags & ~kAllFlags)); |
| 30 | } |
| 31 | |
| 32 | SkFont* SkFont::Create(SkTypeface* face, SkScalar size, SkScalar scaleX, SkScalar skewX, |
| 33 | MaskType mt, uint32_t flags) { |
| 34 | if (size <= 0 || !SkScalarIsFinite(size)) { |
| 35 | return NULL; |
| 36 | } |
| 37 | if (scaleX <= 0 || !SkScalarIsFinite(scaleX)) { |
| 38 | return NULL; |
| 39 | } |
| 40 | if (!SkScalarIsFinite(skewX)) { |
| 41 | return NULL; |
| 42 | } |
| 43 | flags &= kAllFlags; |
| 44 | return SkNEW_ARGS(SkFont, (face, size, scaleX, skewX, mt, flags)); |
| 45 | } |
| 46 | |
| 47 | SkFont* SkFont::Create(SkTypeface* face, SkScalar size, MaskType mt, uint32_t flags) { |
| 48 | return SkFont::Create(face, size, 1, 0, mt, flags); |
| 49 | } |
| 50 | |
| 51 | SkFont* SkFont::cloneWithSize(SkScalar newSize) const { |
| 52 | return SkFont::Create(this->getTypeface(), newSize, this->getScaleX(), this->getSkewX(), |
| 53 | this->getMaskType(), this->getFlags()); |
| 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | SkFont::~SkFont() { |
| 59 | SkSafeUnref(fTypeface); |
| 60 | } |
| 61 | |
| 62 | int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, |
| 63 | uint16_t glyphs[], int maxGlyphCount) const { |
| 64 | if (0 == byteLength) { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | SkASSERT(text); |
| 69 | |
| 70 | int count; |
| 71 | switch (encoding) { |
| 72 | case kUTF8_SkTextEncoding: |
| 73 | count = SkUTF8_CountUnichars((const char*)text, byteLength); |
| 74 | break; |
| 75 | case kUTF16_SkTextEncoding: |
| 76 | count = SkUTF16_CountUnichars((const uint16_t*)text, SkToInt(byteLength >> 1)); |
| 77 | break; |
| 78 | case kUTF32_SkTextEncoding: |
| 79 | count = SkToInt(byteLength >> 2); |
| 80 | break; |
| 81 | case kGlyphID_SkTextEncoding: |
| 82 | count = SkToInt(byteLength >> 1); |
| 83 | break; |
| 84 | } |
| 85 | if (NULL == glyphs) { |
| 86 | return count; |
| 87 | } |
| 88 | |
| 89 | if (kGlyphID_SkTextEncoding == encoding) { |
| 90 | memcpy(glyphs, text, count << 1); |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | // TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding |
| 95 | SkTypeface::Encoding typeface_encoding; |
| 96 | switch (encoding) { |
| 97 | case kUTF8_SkTextEncoding: |
| 98 | typeface_encoding = SkTypeface::kUTF8_Encoding; |
| 99 | break; |
| 100 | case kUTF16_SkTextEncoding: |
| 101 | typeface_encoding = SkTypeface::kUTF16_Encoding; |
| 102 | break; |
| 103 | case kUTF32_SkTextEncoding: |
| 104 | typeface_encoding = SkTypeface::kUTF32_Encoding; |
| 105 | break; |
| 106 | case kGlyphID_SkTextEncoding: |
| 107 | SkASSERT(0); // can't get here |
| 108 | } |
| 109 | |
| 110 | (void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count); |
| 111 | return count; |
| 112 | } |
| 113 | |
| 114 | SkScalar SkFont::measureText(const void* text, size_t byteLength, SkTextEncoding encoding) const { |
| 115 | // TODO: need access to the cache |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 120 | |
| 121 | #include "SkPaint.h" |
| 122 | |
| 123 | SkFont* SkFont::Testing_CreateFromPaint(const SkPaint& paint) { |
| 124 | uint32_t flags = 0; |
| 125 | if (paint.isVerticalText()) { |
| 126 | flags |= kVertical_Flag; |
| 127 | } |
| 128 | if (paint.isEmbeddedBitmapText()) { |
| 129 | flags |= kEmbeddedBitmaps_Flag; |
| 130 | } |
| 131 | if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) { |
| 132 | flags |= kGenA8FromLCD_Flag; |
| 133 | } |
| 134 | if (paint.isFakeBoldText()) { |
| 135 | flags |= kEmbolden_Flag; |
| 136 | } |
| 137 | |
| 138 | if (SkPaint::kFull_Hinting == paint.getHinting()) { |
| 139 | flags |= kEnableByteCodeHints_Flag; |
| 140 | } |
| 141 | if (paint.isAutohinted()) { |
| 142 | flags |= kEnableAutoHints_Flag; |
| 143 | } |
| 144 | if (paint.isSubpixelText() || paint.isLinearText()) { |
| 145 | // this is our default |
| 146 | } else { |
| 147 | flags |= kUseNonlinearMetrics_Flag; |
| 148 | } |
| 149 | |
| 150 | MaskType maskType = SkFont::kBW_MaskType; |
| 151 | if (paint.isAntiAlias()) { |
| 152 | maskType = paint.isLCDRenderText() ? kLCD_MaskType : kA8_MaskType; |
| 153 | } |
| 154 | |
| 155 | return Create(paint.getTypeface(), |
| 156 | paint.getTextSize(), paint.getTextScaleX(), paint.getTextSkewX(), |
| 157 | maskType, flags); |
| 158 | } |