1 /* 2 * Copyright (C) 2013 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 18 #ifndef _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_ 19 #define _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_ 20 21 #include "SkTypeface.h" 22 23 #include <cutils/compiler.h> 24 #include <minikin/FontCollection.h> 25 #include <vector> 26 #include <memory> 27 28 namespace android { 29 30 // This indicates that the weight or italic information should be resolved by OS/2 table. 31 // This value must be the same as the android.graphics.Typeface$Builder.RESOLVE_BY_FONT_TABLE. 32 constexpr int RESOLVE_BY_FONT_TABLE = -1; 33 34 struct ANDROID_API Typeface { 35 public: 36 std::shared_ptr<minikin::FontCollection> fFontCollection; 37 38 // resolved style actually used for rendering 39 minikin::FontStyle fStyle; 40 41 // style used for constructing and querying Typeface objects 42 SkTypeface::Style fSkiaStyle; 43 44 static Typeface* resolveDefault(Typeface* src); 45 46 // The following three functions create new Typeface from an existing Typeface with a different 47 // style. There is a base weight concept which is used for calculating relative style from an 48 // existing Typeface. 49 // The createRelative method creates a new Typeface with a style relative to the base Typeface. 50 // For example, if the base Typeface has a base weight of 400 and the desired style is bold, the 51 // resulting Typeface renders the text with a weight of 700. This function doesn't change the 52 // base weight, so even if you create a new Typeface from the bold Typeface specifying bold on 53 // it again, the text is still rendered with a weight of 700. 54 // You can create another base weight Typeface from an existing Typeface with 55 // createWithDifferentBaseWeight. The Typeface created with this function renders the text with 56 // a specified base weight. 57 // The createAbsolute method creates a new Typeface ignoring the base weight. 58 // Here is an example: 59 // Typeface* base = resolveDefault(nullptr); // Usually this has a weight of 400. 60 // Typeface* bold = createRelative(base, Bold); // Rendered with a weight of 700. 61 // Typeface* bold2 = createRelative(bold, Bold); // Rendered with a weight of 700. 62 // 63 // Typeface* boldBase = createWithDifferentBaseWeight(base, 700); // With a weight of 700. 64 // Typeface* boldBold = createRelative(boldBase, Bold); // Rendered with a weight of 1000. 65 // 66 // Typeface* lightBase = createWithDifferentBaseWeight(base, 300); // With a weight of 300. 67 // Typeface* lightBold = createRelative(lightBase, Bold); // Rendered with a weight of 600. 68 // 69 // Typeface* black = createAbsolute(base, 900, false); // Rendered with a weight of 900. 70 static Typeface* createWithDifferentBaseWeight(Typeface* src, int baseweight); 71 static Typeface* createRelative(Typeface* src, SkTypeface::Style desiredStyle); 72 static Typeface* createAbsolute(Typeface* base, int weight, bool italic); 73 74 static Typeface* createFromTypefaceWithVariation(Typeface* src, 75 const std::vector<minikin::FontVariation>& variations); 76 77 static Typeface* createFromFamilies( 78 std::vector<std::shared_ptr<minikin::FontFamily>>&& families, 79 int weight, int italic); 80 81 static void setDefault(Typeface* face); 82 83 // Sets roboto font as the default typeface for testing purpose. 84 static void setRobotoTypefaceForTest(); 85 private: 86 // base weight in CSS-style units, 1..1000 87 int fBaseWeight; 88 89 }; 90 91 } 92 93 #endif // _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_ 94