• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package noto_fonts
2import (
3  "android/soong/android"
4  "fmt"
5  prebuilt_etc "android/soong/etc"
6  "os"
7)
8
9type fontProperties struct {
10  // An optional font file of the versioned font files.
11  // If not provided, the module name is used instead.
12  FontFile string
13
14  // An optional directory that contains versioned files.
15  // If not provided, "font" is used instead.
16  VersionDir string
17
18  // A mandatory version flag used for controlling versioning.
19  VersionFlag string
20
21  // A mandatory version string used for environment that does not have build flags, e.g. SDK build.
22  DefaultVersion string
23}
24
25type configProperties struct {
26  // An optional font file of the versioned font files.
27  // If not provided, "font_config.json" is used instead.
28  ConfigFile string
29
30  // An optional directory that contains versioned font files.
31  // If not provided, "font" is used instead.
32  VersionDir string
33
34  // A mandatory version flag used for controlling versioning.
35  VersionFlag string
36
37  // A mandatory version string used for environment that does not have build flags, e.g. SDK build.
38  DefaultVersion string
39}
40
41func init() {
42  android.RegisterModuleType("prebuilt_versioned_font", prebuiltVersionedFontFactory)
43  android.RegisterModuleType("versioned_font_config", versionedFontConfigFactory)
44}
45
46///////////////////////////////////////////////////////////////////////////////
47// prebuilt_versioned_font definition
48///////////////////////////////////////////////////////////////////////////////
49func prebuiltVersionedFontFactory() (android.Module) {
50  // Inherit prebuilt_font module
51  module := prebuilt_etc.PrebuiltFontFactory()
52  module.AddProperties(&fontProperties{})
53  android.AddLoadHook(module, prebuiltVersionFont)
54  return module
55}
56
57func prebuiltVersionFont(ctx android.LoadHookContext) {
58  // Find fontProperties propery from the property list.
59  var fontProp *fontProperties
60  for _, p := range ctx.Module().GetProperties() {
61      _fontProp, ok := p.(*fontProperties)
62      if ok {
63        fontProp = _fontProp
64      }
65  }
66
67  // Check if the default version is specified.
68  defaultVersion := fontProp.DefaultVersion
69  if len(defaultVersion) == 0 {
70    fmt.Printf("defaultVersion is required for SDK build")
71    os.Exit(1)
72  }
73
74  // Read the version number from build flag.
75  // The build flag may not be present, in such case, use default value instead.
76  version, ok := ctx.Config().GetBuildFlag(fontProp.VersionFlag)
77  if !ok {
78    fmt.Printf("No value set to Build Flag %s. Use default version instead.", fontProp.VersionFlag)
79    version = defaultVersion
80  }
81
82  // If no VersionDir is specified, use "font" as a default.
83  if len(fontProp.VersionDir) == 0 {
84    fontProp.VersionDir = "font"
85  }
86
87  // If no FontFile is specified, use module name as a default.
88  if len(fontProp.FontFile) == 0 {
89    fontProp.FontFile = ctx.Module().Name()
90  }
91
92  // Override the src property with newly generated one.
93  type props struct {
94    Src string
95  }
96  p := &props{}
97  p.Src = fontProp.VersionDir + "/" + version + "/" + fontProp.FontFile
98  ctx.AppendProperties(p)
99}
100
101
102///////////////////////////////////////////////////////////////////////////////
103// versioned_font_config definition
104///////////////////////////////////////////////////////////////////////////////
105func versionedFontConfigFactory() (android.Module) {
106  // Inherit filegroup module
107  module := android.FileGroupFactory()
108  module.AddProperties(&configProperties{})
109  android.AddLoadHook(module, versionedFontConfig)
110  return module
111}
112
113func versionedFontConfig(ctx android.LoadHookContext) {
114  // Find fontProperties propery from the property list.
115  var configProp *configProperties
116  for _, p := range ctx.Module().GetProperties() {
117      _configProp, ok := p.(*configProperties)
118      if ok {
119        configProp = _configProp
120      }
121  }
122
123  // Check if the default version is specified.
124  defaultVersion := configProp.DefaultVersion
125  if len(defaultVersion) == 0 {
126    fmt.Printf("defaultVersion is required for SDK build")
127    os.Exit(1)
128  }
129
130  // Read the version number from build flag.
131  // The build flag may not be present, in such case, use default value instead.
132  version, ok := ctx.Config().GetBuildFlag(configProp.VersionFlag)
133  if !ok {
134    fmt.Printf("No value set to Build Flag %s. Use default version instead.", configProp.VersionFlag)
135    version = defaultVersion
136  }
137
138  // If no VersionDir is specified, use "font" as a default.
139  if len(configProp.VersionDir) == 0 {
140    configProp.VersionDir = "font"
141  }
142
143  // If no ConfigFile is specified, use font_config.json instead
144  if len(configProp.ConfigFile) == 0 {
145    configProp.ConfigFile = "font_config.json"
146  }
147
148  // Override the src property with newly generated one.
149  type props struct {
150    Srcs []string
151  }
152  p := &props{}
153  p.Srcs = make([]string, 1)
154  p.Srcs[0] = configProp.VersionDir + "/" + version + "/" + configProp.ConfigFile
155  ctx.AppendProperties(p)
156}
157
158