reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkTypeface_DEFINED |
| 18 | #define SkTypeface_DEFINED |
| 19 | |
| 20 | #include "SkRefCnt.h" |
| 21 | |
| 22 | class SkStream; |
| 23 | class SkWStream; |
| 24 | |
| 25 | /** \class SkTypeface |
| 26 | |
| 27 | The SkTypeface class specifies the typeface and intrinsic style of a font. |
| 28 | This is used in the paint, along with optionally algorithmic settings like |
| 29 | textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify |
| 30 | how text appears when drawn (and measured). |
| 31 | |
| 32 | Typeface objects are immutable, and so they can be shred between threads. |
| 33 | To enable this, Typeface inherits from the thread-safe version of SkRefCnt. |
| 34 | */ |
| 35 | class SkTypeface : public SkRefCnt { |
| 36 | public: |
| 37 | /** Style specifies the intrinsic style attributes of a given typeface |
| 38 | */ |
| 39 | enum Style { |
| 40 | kNormal = 0, |
| 41 | kBold = 0x01, |
| 42 | kItalic = 0x02, |
| 43 | |
| 44 | // helpers |
| 45 | kBoldItalic = 0x03 |
| 46 | }; |
| 47 | |
| 48 | /** Returns the typeface's intrinsic style attributes |
| 49 | */ |
| 50 | Style style() const { return fStyle; } |
| 51 | |
| 52 | /** DEPRECATED */ |
| 53 | Style getStyle() const { return this->style(); } |
| 54 | |
| 55 | /** Returns true if getStyle() has the kBold bit set. |
| 56 | */ |
| 57 | bool isBold() const { return (fStyle & kBold) != 0; } |
| 58 | |
| 59 | /** Returns true if getStyle() has the kItalic bit set. |
| 60 | */ |
| 61 | bool isItalic() const { return (fStyle & kItalic) != 0; } |
| 62 | |
| 63 | uint32_t uniqueID() const { return fUniqueID; } |
| 64 | |
| 65 | /** Return the uniqueID for the specified typeface. If the face is null, |
| 66 | resolve it to the default font and return its uniqueID. |
| 67 | */ |
| 68 | static uint32_t UniqueID(const SkTypeface* face); |
| 69 | |
| 70 | /** Return a new reference to the typeface that most closely matches the |
| 71 | requested familyName and style. Pass null as the familyName to return |
| 72 | the default font for the requested style. Will never return null |
| 73 | |
| 74 | @param familyName May be NULL. The name of the font family. |
| 75 | @param style The style (normal, bold, italic) of the typeface. |
| 76 | @return reference to the closest-matching typeface. Call must call |
| 77 | unref() when they are done. |
| 78 | */ |
| 79 | static SkTypeface* Create(const char familyName[], Style style = kNormal); |
| 80 | |
| 81 | /** Return a new reference to the typeface that most closely matches the |
| 82 | requested typeface and specified Style. Use this call if you want to |
| 83 | pick a new style from the same family of the existing typeface. |
| 84 | If family is NULL, this selects from the default font's family. |
| 85 | |
| 86 | @param family May be NULL. The name of the existing type face. |
| 87 | @param s The style (normal, bold, italic) of the type face. |
| 88 | @return reference to the closest-matching typeface. Call must call |
| 89 | unref() when they are done. |
| 90 | */ |
| 91 | static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s); |
| 92 | |
| 93 | /** Returns true if the two typefaces reference the same underlying font, |
| 94 | even if one is null (which maps to the default font). |
| 95 | */ |
| 96 | static bool Equal(const SkTypeface* facea, const SkTypeface* faceb); |
| 97 | |
| 98 | /** Returns a 32bit hash value for the typeface. Takes care of mapping null |
| 99 | to the default typeface. |
| 100 | */ |
| 101 | static uint32_t Hash(const SkTypeface* face); |
| 102 | |
| 103 | /** Return a new typeface given a file. If the file does not exist, or is |
| 104 | not a valid font file, returns null. |
| 105 | */ |
| 106 | static SkTypeface* CreateFromFile(const char path[]); |
| 107 | |
| 108 | /** Return a new typeface given a stream. If the stream is |
| 109 | not a valid font file, returns null. Ownership of the stream is |
| 110 | transferred, so the caller must not reference it again. |
| 111 | */ |
| 112 | static SkTypeface* CreateFromStream(SkStream* stream); |
| 113 | |
| 114 | // Serialization |
| 115 | void serialize(SkWStream*) const; |
| 116 | static SkTypeface* Deserialize(SkStream*); |
| 117 | |
| 118 | protected: |
| 119 | /** uniqueID must be unique (please!) and non-zero |
| 120 | */ |
| 121 | SkTypeface(Style style, uint32_t uniqueID) |
| 122 | : fUniqueID(uniqueID), fStyle(style) {} |
| 123 | |
| 124 | private: |
| 125 | uint32_t fUniqueID; |
| 126 | Style fStyle; |
| 127 | |
| 128 | typedef SkRefCnt INHERITED; |
| 129 | }; |
| 130 | |
| 131 | #endif |