epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 The Android Open Source Project |
| 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 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 8 | #include "SkFontLCDConfig.h" |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 9 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 10 | static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation; |
| 11 | static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 12 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 13 | SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 14 | return gLCDOrientation; |
| 15 | } |
| 16 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 17 | void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 18 | gLCDOrientation = orientation; |
| 19 | } |
| 20 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 21 | SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 22 | return gLCDOrder; |
| 23 | } |
| 24 | |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 25 | void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 26 | gLCDOrder = order; |
| 27 | } |
reed@google.com | 1a64a54 | 2013-03-12 15:41:18 +0000 | [diff] [blame] | 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Legacy wrappers : remove from SkFontHost when webkit switches to new API |
| 31 | |
| 32 | #include "SkFontHost.h" |
| 33 | |
| 34 | SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() { |
| 35 | return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation(); |
| 36 | } |
| 37 | |
| 38 | void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) { |
| 39 | SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation); |
| 40 | } |
| 41 | |
| 42 | SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() { |
| 43 | return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder(); |
| 44 | } |
| 45 | |
| 46 | void SkFontHost::SetSubpixelOrder(LCDOrder order) { |
| 47 | SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order); |
| 48 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 53 | #include "SkFontStyle.h" |
| 54 | |
| 55 | SkFontStyle::SkFontStyle() { |
| 56 | fUnion.fU32 = 0; |
| 57 | fUnion.fR.fWeight = kNormal_Weight; |
| 58 | fUnion.fR.fWidth = kNormal_Width; |
| 59 | fUnion.fR.fSlant = kUpright_Slant; |
| 60 | } |
| 61 | |
| 62 | SkFontStyle::SkFontStyle(int weight, int width, Slant slant) { |
| 63 | fUnion.fU32 = 0; |
| 64 | fUnion.fR.fWeight = SkPin32(weight, kThin_Weight, kBlack_Weight); |
| 65 | fUnion.fR.fWidth = SkPin32(width, kUltraCondensed_Width, kUltaExpanded_Width); |
| 66 | fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant); |
| 67 | } |
| 68 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 69 | #include "SkFontMgr.h" |
| 70 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 71 | SK_DEFINE_INST_COUNT(SkFontStyleSet) |
| 72 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 73 | class SkEmptyFontStyleSet : public SkFontStyleSet { |
| 74 | public: |
| 75 | virtual int count() SK_OVERRIDE { return 0; } |
| 76 | virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE { |
| 77 | SkASSERT(!"SkFontStyleSet::getStyle called on empty set"); |
| 78 | } |
| 79 | virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { |
| 80 | SkASSERT(!"SkFontStyleSet::createTypeface called on empty set"); |
| 81 | return NULL; |
| 82 | } |
| 83 | virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE { |
| 84 | return NULL; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | SkFontStyleSet* SkFontStyleSet::CreateEmpty() { |
| 89 | return SkNEW(SkEmptyFontStyleSet); |
| 90 | } |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 94 | SK_DEFINE_INST_COUNT(SkFontMgr) |
| 95 | |
reed@google.com | db23509 | 2013-03-27 20:12:59 +0000 | [diff] [blame] | 96 | class SkEmptyFontMgr : public SkFontMgr { |
| 97 | protected: |
| 98 | virtual int onCountFamilies() SK_OVERRIDE { |
| 99 | return 0; |
| 100 | } |
| 101 | virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE { |
| 102 | SkASSERT(!"onGetFamilyName called with bad index"); |
| 103 | } |
| 104 | virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE { |
| 105 | SkASSERT(!"onCreateStyleSet called with bad index"); |
| 106 | return NULL; |
| 107 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 108 | virtual SkFontStyleSet* onMatchFamily(const char[]) SK_OVERRIDE { |
| 109 | return SkFontStyleSet::CreateEmpty(); |
| 110 | } |
| 111 | |
reed@google.com | db23509 | 2013-03-27 20:12:59 +0000 | [diff] [blame] | 112 | virtual SkTypeface* onMatchFamilyStyle(const char[], |
| 113 | const SkFontStyle&) SK_OVERRIDE { |
| 114 | return NULL; |
| 115 | } |
| 116 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, |
| 117 | const SkFontStyle&) SK_OVERRIDE { |
| 118 | return NULL; |
| 119 | } |
| 120 | virtual SkTypeface* onCreateFromData(SkData*, int) SK_OVERRIDE { |
| 121 | return NULL; |
| 122 | } |
| 123 | virtual SkTypeface* onCreateFromStream(SkStream*, int) SK_OVERRIDE { |
| 124 | return NULL; |
| 125 | } |
| 126 | virtual SkTypeface* onCreateFromFile(const char[], int) SK_OVERRIDE { |
| 127 | return NULL; |
| 128 | } |
| 129 | }; |
| 130 | |
reed@google.com | c1ccda3 | 2013-04-19 14:28:54 +0000 | [diff] [blame] | 131 | static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) { |
| 132 | if (NULL == fsset) { |
| 133 | fsset = SkFontStyleSet::CreateEmpty(); |
| 134 | } |
| 135 | return fsset; |
| 136 | } |
| 137 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 138 | int SkFontMgr::countFamilies() { |
| 139 | return this->onCountFamilies(); |
| 140 | } |
| 141 | |
| 142 | void SkFontMgr::getFamilyName(int index, SkString* familyName) { |
| 143 | this->onGetFamilyName(index, familyName); |
| 144 | } |
| 145 | |
| 146 | SkFontStyleSet* SkFontMgr::createStyleSet(int index) { |
reed@google.com | c1ccda3 | 2013-04-19 14:28:54 +0000 | [diff] [blame] | 147 | return emptyOnNull(this->onCreateStyleSet(index)); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 148 | } |
| 149 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 150 | SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) { |
reed@google.com | c1ccda3 | 2013-04-19 14:28:54 +0000 | [diff] [blame] | 151 | return emptyOnNull(this->onMatchFamily(familyName)); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 154 | SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[], |
| 155 | const SkFontStyle& fs) { |
| 156 | return this->onMatchFamilyStyle(familyName, fs); |
| 157 | } |
| 158 | |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 159 | SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face, |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 160 | const SkFontStyle& fs) { |
reed@google.com | 919e9c3 | 2013-03-26 19:29:55 +0000 | [diff] [blame] | 161 | return this->onMatchFaceStyle(face, fs); |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) { |
reed@google.com | b6bd24d | 2013-07-31 18:49:33 +0000 | [diff] [blame^] | 165 | if (NULL == data) { |
| 166 | return NULL; |
| 167 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 168 | return this->onCreateFromData(data, ttcIndex); |
| 169 | } |
| 170 | |
| 171 | SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) { |
reed@google.com | b6bd24d | 2013-07-31 18:49:33 +0000 | [diff] [blame^] | 172 | if (NULL == stream) { |
| 173 | return NULL; |
| 174 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 175 | return this->onCreateFromStream(stream, ttcIndex); |
| 176 | } |
| 177 | |
| 178 | SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) { |
reed@google.com | b6bd24d | 2013-07-31 18:49:33 +0000 | [diff] [blame^] | 179 | if (NULL == path) { |
| 180 | return NULL; |
| 181 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 182 | return this->onCreateFromFile(path, ttcIndex); |
| 183 | } |
| 184 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 185 | SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], |
| 186 | unsigned styleBits) { |
| 187 | return this->onLegacyCreateTypeface(familyName, styleBits); |
| 188 | } |
| 189 | |
| 190 | SkTypeface* SkFontMgr::onLegacyCreateTypeface(const char familyName[], |
| 191 | unsigned styleBits) { |
| 192 | SkASSERT(!"unimplemented"); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 196 | SkFontMgr* SkFontMgr::RefDefault() { |
| 197 | static SkFontMgr* gFM; |
| 198 | if (NULL == gFM) { |
| 199 | gFM = SkFontMgr::Factory(); |
reed@google.com | db23509 | 2013-03-27 20:12:59 +0000 | [diff] [blame] | 200 | // we never want to return NULL |
| 201 | if (NULL == gFM) { |
| 202 | gFM = SkNEW(SkEmptyFontMgr); |
| 203 | } |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 204 | } |
| 205 | return SkRef(gFM); |
| 206 | } |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 207 | |
| 208 | ////////////////////////////////////////////////////////////////////////// |
| 209 | |
| 210 | #ifdef SK_FONTHOST_USES_FONTMGR |
| 211 | |
| 212 | #if 0 |
| 213 | static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) { |
| 214 | SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ? |
| 215 | SkFontStyle::kBold_Weight : |
| 216 | SkFontStyle::kNormal_Weight; |
| 217 | SkFontStyle::Width width = SkFontStyle::kNormal_Width; |
| 218 | SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ? |
| 219 | SkFontStyle::kUpright_Slant : |
| 220 | SkFontStyle::kItalic_Slant; |
| 221 | return SkFontStyle(weight, width, slant); |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 226 | const char familyName[], |
| 227 | SkTypeface::Style style) { |
| 228 | if (familyFace) { |
| 229 | return familyFace->refMatchingStyle(style); |
| 230 | } else { |
| 231 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 232 | return fm->legacyCreateTypeface(familyName, style); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 237 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 238 | return fm->createFromFile(path); |
| 239 | } |
| 240 | |
| 241 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
| 242 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 243 | return fm->createFromStream(stream); |
| 244 | } |
| 245 | |
| 246 | #endif |