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     static const Typeface* resolveDefault(const 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, Style desiredStyle);
72     static Typeface* createAbsolute(Typeface* base, int weight, bool italic);
73 
74     static Typeface* createFromTypefaceWithVariation(
75             Typeface* src, const std::vector<minikin::FontVariation>& variations);
76 
77     static Typeface* createFromFamilies(
78             std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic);
79 
80     static void setDefault(const Typeface* face);
81 
82     // Sets roboto font as the default typeface for testing purpose.
83     static void setRobotoTypefaceForTest();
84 
85 private:
86     // base weight in CSS-style units, 1..1000
87     int fBaseWeight;
88 };
89 }
90 
91 #endif  // _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
92