xref: /aosp_15_r20/frameworks/base/data/fonts/script/family_builder.py (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1#!/usr/bin/env python
2
3#
4# Copyright (C) 2024 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""Build Family instance with validating JSON contents."""
20
21import dataclasses
22
23from custom_json import _load_json_with_comment
24from font_builder import Font
25from font_builder import parse_fonts
26from validators import check_enum_or_none
27from validators import check_priority_or_none
28from validators import check_str_or_none
29
30_FAMILY_KEYS = set([
31    "id",
32    "lang",
33    "name",
34    "variant",
35    "fallbackFor",
36    "fonts",
37    "target",
38    "priority",
39])
40
41
42@dataclasses.dataclass
43class Family:
44  id: str | None
45  lang: str | None
46  name: str | None
47  priority: int | None
48  variant: str | None
49  fallback_for: str | None
50  target: str | None
51  fonts: [Font]
52
53
54def _validate_family(family):
55  assert not family.lang or not family.name, (
56      "If lang attribute is specified, name attribute must not be specified: %s"
57      % family
58  )
59
60  if family.fallback_for:
61    assert family.target, (
62        "If fallbackFor is specified, must specify target: %s" % family
63    )
64  if family.target:
65    assert family.fallback_for, (
66        "If target is specified, must specify fallbackFor: %s" % family
67    )
68
69
70def _parse_family(obj, for_sanitization_test=False) -> Family:
71  """Create Family object from dictionary."""
72  unknown_keys = obj.keys() - _FAMILY_KEYS
73  assert not unknown_keys, "Unknown keys found: %s in %s" % (unknown_keys, obj)
74
75  if for_sanitization_test:
76    fonts = []
77  else:
78    fonts = parse_fonts(obj.get("fonts"))
79
80  family = Family(
81      id=check_str_or_none(obj, "id"),
82      lang=check_str_or_none(obj, "lang"),
83      name=check_str_or_none(obj, "name"),
84      priority=check_priority_or_none(obj, "priority"),
85      variant=check_enum_or_none(obj, "variant", ["elegant", "compact"]),
86      fallback_for=check_str_or_none(obj, "fallbackFor"),
87      target=check_str_or_none(obj, "target"),
88      fonts=fonts,
89  )
90
91  if not for_sanitization_test:
92    _validate_family(family)
93  return family
94
95
96def parse_family_from_json_for_sanitization_test(json_str) -> Family:
97  """For testing purposes."""
98  return _parse_family(
99      _load_json_with_comment(json_str), for_sanitization_test=True
100  )
101
102
103def parse_family_from_json(json_str) -> Family:
104  """For testing purposes."""
105  return _parse_family(_load_json_with_comment(json_str))
106
107
108def parse_families_from_json(json_str) -> [Family]:
109  objs = _load_json_with_comment(json_str)
110  assert isinstance(objs, list), "families must be list"
111  assert objs, "families must contains at least one family"
112  return [_parse_family(obj) for obj in objs]
113