xref: /aosp_15_r20/frameworks/base/libs/hwui/hwui/Typeface.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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     std::shared_ptr<minikin::FontCollection> fFontCollection;
36 
37     // resolved style actually used for rendering
38     minikin::FontStyle fStyle;
39 
40     // style used in the API
41     enum Style : uint8_t { kNormal = 0, kBold = 0x01, kItalic = 0x02, kBoldItalic = 0x03 };
42     Style fAPIStyle;
43 
44     // base weight in CSS-style units, 1..1000
45     int fBaseWeight;
46 
47     // True if the Typeface is already created for variation settings.
48     bool fIsVariationInstance;
49 
50     static const Typeface* resolveDefault(const Typeface* src);
51 
52     // The following three functions create new Typeface from an existing Typeface with a different
53     // style. There is a base weight concept which is used for calculating relative style from an
54     // existing Typeface.
55     // The createRelative method creates a new Typeface with a style relative to the base Typeface.
56     // For example, if the base Typeface has a base weight of 400 and the desired style is bold, the
57     // resulting Typeface renders the text with a weight of 700. This function doesn't change the
58     // base weight, so even if you create a new Typeface from the bold Typeface specifying bold on
59     // it again, the text is still rendered with a weight of 700.
60     // You can create another base weight Typeface from an existing Typeface with
61     // createWithDifferentBaseWeight. The Typeface created with this function renders the text with
62     // a specified base weight.
63     // The createAbsolute method creates a new Typeface ignoring the base weight.
64     // Here is an example:
65     //   Typeface* base = resolveDefault(nullptr);  // Usually this has a weight of 400.
66     //   Typeface* bold = createRelative(base, Bold);  // Rendered with a weight of 700.
67     //   Typeface* bold2 = createRelative(bold, Bold); // Rendered with a weight of 700.
68     //
69     //   Typeface* boldBase = createWithDifferentBaseWeight(base, 700);  // With a weight of 700.
70     //   Typeface* boldBold = createRelative(boldBase, Bold);  // Rendered with a weight of 1000.
71     //
72     //   Typeface* lightBase = createWithDifferentBaseWeight(base, 300);  // With a weight of 300.
73     //   Typeface* lightBold = createRelative(lightBase, Bold);  // Rendered with a weight of 600.
74     //
75     //   Typeface* black = createAbsolute(base, 900, false);  // Rendered with a weight of 900.
76     static Typeface* createWithDifferentBaseWeight(Typeface* src, int baseweight);
77     static Typeface* createRelative(Typeface* src, Style desiredStyle);
78     static Typeface* createAbsolute(Typeface* base, int weight, bool italic);
79 
80     static Typeface* createFromTypefaceWithVariation(Typeface* src,
81                                                      const minikin::VariationSettings& variations);
82 
83     static Typeface* createFromFamilies(
84             std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic,
85             const Typeface* fallback);
86 
87     static void setDefault(const Typeface* face);
88 
89     // Sets roboto font as the default typeface for testing purpose.
90     static void setRobotoTypefaceForTest();
91 };
92 }
93 
94 #endif  // _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
95