Skip to content

Commit

Permalink
Add strict fonts rule (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes authored Feb 5, 2020
1 parent 372a454 commit 2e4b2bf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ in the .xib or .storyboard file.

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

- `strict_fonts`

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).

- `strict_font_names`

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).
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.

- `strict_font_sizes`

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).
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.

- `unavailable_custom_classes`

Expand Down
2 changes: 1 addition & 1 deletion xiblint/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.9.11'
__version__ = '0.9.12'
53 changes: 53 additions & 0 deletions xiblint/rules/strict_fonts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from xiblint.rules import Rule
from xiblint.xibcontext import XibContext


class StrictFonts(Rule):
"""
Ensures font name and size combinations are in the allowed set. This would generally be used instead
of strict_font_names and strict_font_sizes.
Example configuration:
{
"allowed_fonts": [
{
"name": "ComicSans-Regular",
"size": 14
},
{
"name": "ComicSans-Bold",
"size": 14
}
]
}
"""
def check(self, context): # type: (XibContext) -> None
allowed_fonts = [(d["name"], d["size"]) for d in self.config.get('allowed_fonts', [])]

for element in context.tree.findall('.//font') + context.tree.findall('.//fontDescription'):
size_attribute_name = None

if element.tag == 'font':
# Skip <font> tags nested in a localization comment
container = element.parent.parent.parent
if container.tag == 'attributedString' and container.get('key') == 'userComments':
continue

size_attribute_name = 'size'
else:
size_attribute_name = 'pointSize'

raw_size = element.get(size_attribute_name)
if raw_size is None:
context.error(element, 'Invalid <{}> found. Must have a {}.'.format(element.tag, size_attribute_name))
continue

size = int(raw_size)

name = element.get('name')
if name is None:
context.error(element, 'Invalid <{}> found. Must have a name.'.format(element.tag))
continue

if (name, size) not in allowed_fonts:
context.error(element, 'Invalid font found {} {}. Please use an allowed font.'.format(name, size))

0 comments on commit 2e4b2bf

Please sign in to comment.