Skip to content

Commit 2e4b2bf

Browse files
authored
Add strict fonts rule (#53)
1 parent 372a454 commit 2e4b2bf

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ in the .xib or .storyboard file.
5656

5757
Ensures there are no links to other storyboards in different bundles.
5858

59+
- `strict_fonts`
60+
61+
Ensures all font names and sizes are in an allowed set. Configure `allowed_fonts` with an array of dictionaries containing a `name` and `size` in a custom rule configuration using `rules_config` (see below).
62+
5963
- `strict_font_names`
6064

61-
Ensures all fonts are in an allowed set. Configure `allowed_fonts` and `allow_system_fonts` (default is `true`) in a custom rule configuration using `rules_config` (see below).
65+
Ensures all font namess are in an allowed set. Configure `allowed_fonts` and `allow_system_fonts` (default is `true`) in a custom rule configuration using `rules_config` (see below). This is a good option if `strict_fonts` is too strict.
6266

6367
- `strict_font_sizes`
6468

65-
Ensures all fonts are above a minimum font size. Configure `minimum_size` (default is `0`) and/or `maximum_size` (default is `1000`) in a custom rule configuration using `rules_config` (see below).
69+
Ensures all font sizes are above a minimum font size. Configure `minimum_size` (default is `0`) and/or `maximum_size` (default is `1000`) in a custom rule configuration using `rules_config` (see below). This is a good option if `strict_fonts` is too strict.
6670

6771
- `unavailable_custom_classes`
6872

xiblint/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.9.11'
1+
__version__ = '0.9.12'

xiblint/rules/strict_fonts.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from xiblint.rules import Rule
2+
from xiblint.xibcontext import XibContext
3+
4+
5+
class StrictFonts(Rule):
6+
"""
7+
Ensures font name and size combinations are in the allowed set. This would generally be used instead
8+
of strict_font_names and strict_font_sizes.
9+
10+
Example configuration:
11+
{
12+
"allowed_fonts": [
13+
{
14+
"name": "ComicSans-Regular",
15+
"size": 14
16+
},
17+
{
18+
"name": "ComicSans-Bold",
19+
"size": 14
20+
}
21+
]
22+
}
23+
"""
24+
def check(self, context): # type: (XibContext) -> None
25+
allowed_fonts = [(d["name"], d["size"]) for d in self.config.get('allowed_fonts', [])]
26+
27+
for element in context.tree.findall('.//font') + context.tree.findall('.//fontDescription'):
28+
size_attribute_name = None
29+
30+
if element.tag == 'font':
31+
# Skip <font> tags nested in a localization comment
32+
container = element.parent.parent.parent
33+
if container.tag == 'attributedString' and container.get('key') == 'userComments':
34+
continue
35+
36+
size_attribute_name = 'size'
37+
else:
38+
size_attribute_name = 'pointSize'
39+
40+
raw_size = element.get(size_attribute_name)
41+
if raw_size is None:
42+
context.error(element, 'Invalid <{}> found. Must have a {}.'.format(element.tag, size_attribute_name))
43+
continue
44+
45+
size = int(raw_size)
46+
47+
name = element.get('name')
48+
if name is None:
49+
context.error(element, 'Invalid <{}> found. Must have a name.'.format(element.tag))
50+
continue
51+
52+
if (name, size) not in allowed_fonts:
53+
context.error(element, 'Invalid font found {} {}. Please use an allowed font.'.format(name, size))

0 commit comments

Comments
 (0)