• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
18 #define _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
19 
20 #include "SkTypeface.h"
21 
22 #include <cutils/compiler.h>
23 #include <minikin/FontCollection.h>
24 #include <memory>
25 #include <vector>
26 
27 namespace android {
28 
29 // This indicates that the weight or italic information should be resolved by OS/2 table.
30 // This value must be the same as the android.graphics.Typeface$Builder.RESOLVE_BY_FONT_TABLE.
31 constexpr int RESOLVE_BY_FONT_TABLE = -1;
32 
33 struct ANDROID_API Typeface {
34 public:
35     enum Style : uint8_t { kNormal = 0, kBold = 0x01, kItalic = 0x02, kBoldItalic = 0x03 };
TypefaceTypeface36     Typeface(const std::shared_ptr<minikin::FontCollection> fc, minikin::FontStyle style,
37              Style apiStyle, int baseWeight, bool isVariationInstance)
38             : fFontCollection(fc)
39             , fStyle(style)
40             , fAPIStyle(apiStyle)
41             , fBaseWeight(baseWeight)
42             , fIsVariationInstance(isVariationInstance) {}
43 
getFontCollectionTypeface44     const std::shared_ptr<minikin::FontCollection>& getFontCollection() const {
45         return fFontCollection;
46     }
47 
48     // resolved style actually used for rendering
getFontStyleTypeface49     minikin::FontStyle getFontStyle() const { return fStyle; }
50 
51     // style used in the API
getAPIStyleTypeface52     Style getAPIStyle() const { return fAPIStyle; }
53 
54     // base weight in CSS-style units, 1..1000
getBaseWeightTypeface55     int getBaseWeight() const { return fBaseWeight; }
56 
57     // True if the Typeface is already created for variation settings.
isVariationInstanceTypeface58     bool isVariationInstance() const { return fIsVariationInstance; }
59 
60 private:
61     std::shared_ptr<minikin::FontCollection> fFontCollection;
62     minikin::FontStyle fStyle;
63     Style fAPIStyle;
64     int fBaseWeight;
65     bool fIsVariationInstance = false;
66 
67 public:
68     static const Typeface* resolveDefault(const Typeface* src);
69 
70     // The following three functions create new Typeface from an existing Typeface with a different
71     // style. There is a base weight concept which is used for calculating relative style from an
72     // existing Typeface.
73     // The createRelative method creates a new Typeface with a style relative to the base Typeface.
74     // For example, if the base Typeface has a base weight of 400 and the desired style is bold, the
75     // resulting Typeface renders the text with a weight of 700. This function doesn't change the
76     // base weight, so even if you create a new Typeface from the bold Typeface specifying bold on
77     // it again, the text is still rendered with a weight of 700.
78     // You can create another base weight Typeface from an existing Typeface with
79     // createWithDifferentBaseWeight. The Typeface created with this function renders the text with
80     // a specified base weight.
81     // The createAbsolute method creates a new Typeface ignoring the base weight.
82     // Here is an example:
83     //   Typeface* base = resolveDefault(nullptr);  // Usually this has a weight of 400.
84     //   Typeface* bold = createRelative(base, Bold);  // Rendered with a weight of 700.
85     //   Typeface* bold2 = createRelative(bold, Bold); // Rendered with a weight of 700.
86     //
87     //   Typeface* boldBase = createWithDifferentBaseWeight(base, 700);  // With a weight of 700.
88     //   Typeface* boldBold = createRelative(boldBase, Bold);  // Rendered with a weight of 1000.
89     //
90     //   Typeface* lightBase = createWithDifferentBaseWeight(base, 300);  // With a weight of 300.
91     //   Typeface* lightBold = createRelative(lightBase, Bold);  // Rendered with a weight of 600.
92     //
93     //   Typeface* black = createAbsolute(base, 900, false);  // Rendered with a weight of 900.
94     static Typeface* createWithDifferentBaseWeight(Typeface* src, int baseweight);
95     static Typeface* createRelative(Typeface* src, Style desiredStyle);
96     static Typeface* createAbsolute(Typeface* base, int weight, bool italic);
97 
98     static Typeface* createFromTypefaceWithVariation(Typeface* src,
99                                                      const minikin::VariationSettings& variations);
100 
101     static Typeface* createFromFamilies(
102             std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic,
103             const Typeface* fallback);
104 
105     static void setDefault(const Typeface* face);
106 
107     // Sets roboto font as the default typeface for testing purpose.
108     static void setRobotoTypefaceForTest();
109 };
110 }
111 
112 #endif  // _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
113