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 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 12 | SkFont::SkFont(sk_sp<SkTypeface> face, SkScalar size, SkScalar scaleX, SkScalar skewX, MaskType mt, |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 13 | uint32_t flags) |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 14 | : fTypeface(face ? std::move(face) : SkTypeface::MakeDefault()) |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 15 | , fSize(size) |
| 16 | , fScaleX(scaleX) |
| 17 | , fSkewX(skewX) |
| 18 | , fFlags(flags) |
| 19 | , fMaskType(SkToU8(mt)) |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 20 | { |
| 21 | SkASSERT(size > 0); |
| 22 | SkASSERT(scaleX > 0); |
| 23 | SkASSERT(SkScalarIsFinite(skewX)); |
| 24 | SkASSERT(0 == (flags & ~kAllFlags)); |
| 25 | } |
| 26 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 27 | sk_sp<SkFont> SkFont::Make(sk_sp<SkTypeface> face, SkScalar size, SkScalar scaleX, SkScalar skewX, |
| 28 | MaskType mt, uint32_t flags) { |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 29 | if (size <= 0 || !SkScalarIsFinite(size)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | return nullptr; |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 31 | } |
| 32 | if (scaleX <= 0 || !SkScalarIsFinite(scaleX)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | return nullptr; |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 34 | } |
| 35 | if (!SkScalarIsFinite(skewX)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | return nullptr; |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 37 | } |
| 38 | flags &= kAllFlags; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 39 | return sk_sp<SkFont>(new SkFont(std::move(face), size, scaleX, skewX, mt, flags)); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 40 | } |
| 41 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 42 | sk_sp<SkFont> SkFont::Make(sk_sp<SkTypeface> face, SkScalar size, MaskType mt, uint32_t flags) { |
| 43 | return SkFont::Make(std::move(face), size, 1, 0, mt, flags); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 46 | sk_sp<SkFont> SkFont::makeWithSize(SkScalar newSize) const { |
| 47 | return SkFont::Make(sk_ref_sp(this->getTypeface()), newSize, this->getScaleX(), |
| 48 | this->getSkewX(), this->getMaskType(), this->getFlags()); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 52 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 53 | int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, |
| 54 | uint16_t glyphs[], int maxGlyphCount) const { |
| 55 | if (0 == byteLength) { |
| 56 | return 0; |
| 57 | } |
skia.committer@gmail.com | e1d9443 | 2014-04-09 03:04:11 +0000 | [diff] [blame] | 58 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 59 | SkASSERT(text); |
| 60 | |
reed@google.com | 43d6d80 | 2014-04-08 16:02:45 +0000 | [diff] [blame] | 61 | int count = 0; // fix uninitialized warning (even though the switch is complete!) |
| 62 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 63 | switch (encoding) { |
| 64 | case kUTF8_SkTextEncoding: |
| 65 | count = SkUTF8_CountUnichars((const char*)text, byteLength); |
| 66 | break; |
| 67 | case kUTF16_SkTextEncoding: |
| 68 | count = SkUTF16_CountUnichars((const uint16_t*)text, SkToInt(byteLength >> 1)); |
| 69 | break; |
| 70 | case kUTF32_SkTextEncoding: |
| 71 | count = SkToInt(byteLength >> 2); |
| 72 | break; |
| 73 | case kGlyphID_SkTextEncoding: |
| 74 | count = SkToInt(byteLength >> 1); |
| 75 | break; |
| 76 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | if (nullptr == glyphs) { |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 78 | return count; |
| 79 | } |
| 80 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 81 | // TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding |
| 82 | SkTypeface::Encoding typeface_encoding; |
| 83 | switch (encoding) { |
| 84 | case kUTF8_SkTextEncoding: |
| 85 | typeface_encoding = SkTypeface::kUTF8_Encoding; |
| 86 | break; |
| 87 | case kUTF16_SkTextEncoding: |
| 88 | typeface_encoding = SkTypeface::kUTF16_Encoding; |
| 89 | break; |
| 90 | case kUTF32_SkTextEncoding: |
| 91 | typeface_encoding = SkTypeface::kUTF32_Encoding; |
| 92 | break; |
reed@google.com | 6719d60 | 2014-04-08 16:57:05 +0000 | [diff] [blame] | 93 | default: |
| 94 | SkASSERT(kGlyphID_SkTextEncoding == encoding); |
reed@google.com | efadb4b | 2014-04-08 15:48:26 +0000 | [diff] [blame] | 95 | // we can early exit, since we already have glyphIDs |
| 96 | memcpy(glyphs, text, count << 1); |
| 97 | return count; |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | (void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count); |
| 101 | return count; |
| 102 | } |
| 103 | |
| 104 | SkScalar SkFont::measureText(const void* text, size_t byteLength, SkTextEncoding encoding) const { |
| 105 | // TODO: need access to the cache |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | #include "SkPaint.h" |
| 112 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 113 | sk_sp<SkFont> SkFont::Testing_CreateFromPaint(const SkPaint& paint) { |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 114 | uint32_t flags = 0; |
| 115 | if (paint.isVerticalText()) { |
| 116 | flags |= kVertical_Flag; |
| 117 | } |
| 118 | if (paint.isEmbeddedBitmapText()) { |
| 119 | flags |= kEmbeddedBitmaps_Flag; |
| 120 | } |
| 121 | if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) { |
| 122 | flags |= kGenA8FromLCD_Flag; |
| 123 | } |
| 124 | if (paint.isFakeBoldText()) { |
| 125 | flags |= kEmbolden_Flag; |
| 126 | } |
| 127 | |
| 128 | if (SkPaint::kFull_Hinting == paint.getHinting()) { |
| 129 | flags |= kEnableByteCodeHints_Flag; |
| 130 | } |
| 131 | if (paint.isAutohinted()) { |
| 132 | flags |= kEnableAutoHints_Flag; |
| 133 | } |
| 134 | if (paint.isSubpixelText() || paint.isLinearText()) { |
| 135 | // this is our default |
| 136 | } else { |
| 137 | flags |= kUseNonlinearMetrics_Flag; |
| 138 | } |
| 139 | |
| 140 | MaskType maskType = SkFont::kBW_MaskType; |
| 141 | if (paint.isAntiAlias()) { |
| 142 | maskType = paint.isLCDRenderText() ? kLCD_MaskType : kA8_MaskType; |
| 143 | } |
| 144 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame^] | 145 | return Make(sk_ref_sp(paint.getTypeface()), paint.getTextSize(), paint.getTextScaleX(), |
| 146 | paint.getTextSkewX(), maskType, flags); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 147 | } |