mike@reedtribe.org | 2bbc0e1 | 2013-03-15 03:15:40 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2013 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 | #ifndef SkFontMgr_DEFINED |
| 9 | #define SkFontMgr_DEFINED |
| 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | |
| 13 | class SkData; |
| 14 | class SkStream; |
| 15 | class SkString; |
| 16 | |
| 17 | class SkFontStyle { |
| 18 | public: |
| 19 | enum Weight { |
| 20 | kThin_Weight = 100, |
| 21 | kExtraLight_Weight = 200, |
| 22 | kLight_Weight = 300, |
| 23 | kNormal_Weight = 400, |
| 24 | kMedium_Weight = 500, |
| 25 | kSemiBold_Weight = 600, |
| 26 | kBold_Weight = 700, |
| 27 | kExtraBold_Weight = 800, |
| 28 | kBlack_Weight = 900 |
| 29 | }; |
| 30 | |
| 31 | enum Width { |
| 32 | kUltraCondensed_Width = 1, |
| 33 | kExtraCondensed_Width = 2, |
| 34 | kCondensed_Width = 3, |
| 35 | kSemiCondensed_Width = 4, |
| 36 | kNormal_Width = 5, |
| 37 | kSemiExpanded_Width = 6, |
| 38 | kExpanded_Width = 7, |
| 39 | kExtraExpanded_Width = 8, |
| 40 | kUltaExpanded_Width = 9 |
| 41 | }; |
| 42 | |
| 43 | enum Flags { |
| 44 | kItalic_Flag = 1 << 0, |
| 45 | }; |
| 46 | |
| 47 | SkFontStyle(); |
| 48 | SkFontStyle(int weight, int width, unsigned flags); |
| 49 | |
| 50 | bool operator==(const SkFontStyle&) const { |
| 51 | return fUnion.fU32 == other.fUnion.fU32; |
| 52 | } |
| 53 | |
| 54 | int weight() const { return fUnion.fR.fWeight; } |
| 55 | int width() const { return fUnion.fR.fWidth; } |
| 56 | unsigned flags() const { return fUnion.fR.fFlags; } |
| 57 | |
| 58 | bool isItalic() const { |
| 59 | return SkToBool(fUnion.fR.fFlags & kItalic_Flag); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | union { |
| 64 | struct { |
| 65 | uint16_t fWeight; // 100 ... 900 |
| 66 | uint8_t fWidth; // 1 .. 9 |
| 67 | uint8_t fFlags; |
| 68 | } fR; |
| 69 | uint32_t fU32; |
| 70 | } fUnion; |
| 71 | }; |
| 72 | |
| 73 | class SkFontMgr : public SkRefCnt { |
| 74 | public: |
| 75 | /** |
| 76 | * SkData contains an array of [const char*] |
| 77 | */ |
| 78 | SkData* refFamilyNames(); |
| 79 | |
| 80 | /** |
| 81 | * Given a familyName, if a corresponding family is found, return |
| 82 | * the array of available styles in SkData (as [SkFontStyle]). |
| 83 | * |
| 84 | * If foundFamilyName is not null, set it to the actual familyName for the |
| 85 | * found family. |
| 86 | */ |
| 87 | SkData* refFamilyStyles(const char familyName[], SkString* foundFamilyName); |
| 88 | |
| 89 | /** |
| 90 | * Find the closest matching typeface to the specified familyName and style |
| 91 | * and return a ref to it. The caller must call unref() on the returned |
| 92 | * object. Will never return NULL. |
| 93 | * |
| 94 | * If foundFamilyName is not null, set it to the actual familyName for the |
| 95 | * returned typeface. |
| 96 | */ |
| 97 | SkTypeface* matchFamilyName(const char familyName[], const Desc&, |
| 98 | SkString* foundFamilyName); |
| 99 | |
| 100 | /** |
| 101 | * Create a typeface for the specified data and TTC index (pass 0 for none) |
| 102 | * or NULL if the data is not recognized. The caller must call unref() on |
| 103 | * the returned object if it is not null. |
| 104 | */ |
| 105 | SkTypeface* createFromData(SkData*, int ttcIndex = 0); |
| 106 | |
| 107 | /** |
| 108 | * Create a typeface for the specified stream and TTC index |
| 109 | * (pass 0 for none) or NULL if the stream is not recognized. The caller |
| 110 | * must call unref() on the returned object if it is not null. |
| 111 | */ |
| 112 | SkTypeface* createFromStream(SkStream*, int ttcIndex = 0); |
| 113 | |
| 114 | /** |
| 115 | * Create a typeface for the specified fileName and TTC index |
| 116 | * (pass 0 for none) or NULL if the file is not found, or its contents are |
| 117 | * not recognized. The caller must call unref() on the returned object |
| 118 | * if it is not null. |
| 119 | */ |
| 120 | SkTypeface* createFromFile(const char path[], int ttcIndex = 0); |
| 121 | |
| 122 | private: |
| 123 | typedef SkRefCnt INHERITED; |
| 124 | }; |
| 125 | |
| 126 | #endif |