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