• 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     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     static const Typeface* resolveDefault(const Typeface* src);
48 
49     // The following three functions create new Typeface from an existing Typeface with a different
50     // style. There is a base weight concept which is used for calculating relative style from an
51     // existing Typeface.
52     // The createRelative method creates a new Typeface with a style relative to the base Typeface.
53     // For example, if the base Typeface has a base weight of 400 and the desired style is bold, the
54     // resulting Typeface renders the text with a weight of 700. This function doesn't change the
55     // base weight, so even if you create a new Typeface from the bold Typeface specifying bold on
56     // it again, the text is still rendered with a weight of 700.
57     // You can create another base weight Typeface from an existing Typeface with
58     // createWithDifferentBaseWeight. The Typeface created with this function renders the text with
59     // a specified base weight.
60     // The createAbsolute method creates a new Typeface ignoring the base weight.
61     // Here is an example:
62     //   Typeface* base = resolveDefault(nullptr);  // Usually this has a weight of 400.
63     //   Typeface* bold = createRelative(base, Bold);  // Rendered with a weight of 700.
64     //   Typeface* bold2 = createRelative(bold, Bold); // Rendered with a weight of 700.
65     //
66     //   Typeface* boldBase = createWithDifferentBaseWeight(base, 700);  // With a weight of 700.
67     //   Typeface* boldBold = createRelative(boldBase, Bold);  // Rendered with a weight of 1000.
68     //
69     //   Typeface* lightBase = createWithDifferentBaseWeight(base, 300);  // With a weight of 300.
70     //   Typeface* lightBold = createRelative(lightBase, Bold);  // Rendered with a weight of 600.
71     //
72     //   Typeface* black = createAbsolute(base, 900, false);  // Rendered with a weight of 900.
73     static Typeface* createWithDifferentBaseWeight(Typeface* src, int baseweight);
74     static Typeface* createRelative(Typeface* src, Style desiredStyle);
75     static Typeface* createAbsolute(Typeface* base, int weight, bool italic);
76 
77     static Typeface* createFromTypefaceWithVariation(
78             Typeface* src, const std::vector<minikin::FontVariation>& variations);
79 
80     static Typeface* createFromFamilies(
81             std::vector<std::shared_ptr<minikin::FontFamily>>&& families, int weight, int italic);
82 
83     static void setDefault(const Typeface* face);
84 
85     // Sets roboto font as the default typeface for testing purpose.
86     static void setRobotoTypefaceForTest();
87 };
88 }
89 
90 #endif  // _ANDROID_GRAPHICS_TYPEFACE_IMPL_H_
91