-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Hey there, thanks for making fontimize. I attempted to run this on my website, and I had some trouble getting it to work. I've made patches directly to the package in my virtual environment, which made it work, and I'm opening issues to maybe help upstream these patches. Unfortunately I haven't convinced myself the patches are appropriate, nor have I gotten to the bottom of why issues occur.
This issue is about the parsing of CSS. Specifically, when I ran fontimize on my static site, it reported not being able to find any fonts. This was surprising, but I observed that it was finding CSS files and opening them. However -- modifying the script to add extra output -- I noticed that, whereas the script expects a "function call" with with the name "url", the output of CSS parsing was a URLToken, which had a distinct token type, and a "value" rather than "arguments" attribute. The following patch made it work:
206,207c206,207
< if token.type == 'function' and token.lower_name == 'url':
< urls.append(token.arguments[0].value)
---
> if token.type == 'url':
> urls.append(token.value)I'm not sure what changed (TinyCSS? my Python version? Something about my CSS compared to yours?
Next, I ran into a very peculiar issue in which fontimize was clearly looking in the wrong folder for font files referenced in the CSS. It turned out that the script was mistakenly using the last CSS file's path as a reference when searching for font files, instead of using each CSS files as a reference to find fonts within that file. The patch was the following, which I am much more happy with:
305c307,309
< adjusted_font_path = _get_path(adjusted_css_path, font_url) # Relative to the CSS file
---
> adjusted_font_path = _get_path(css_file, font_url) # Relative to the CSS fileI also had to make the following change to _get_path. I don't recall why it was needed, but it has something to do with the use of relative paths ../../..etc in my CSS, I suspect.
220c220
< full_path = path.join(base_dir, relative_path)
---
> full_path = path.normpath(path.join(base_dir, relative_path))My pip freeze output is as follows:
beautifulsoup4==4.13.3
bs4==0.0.2
fontimize==0.8.9
fonttools==4.56.0
pathlib==1.0.1
pathvalidate==3.2.3
soupsieve==2.6
tinycss2==1.4.0
ttf2web==0.9.3
typing==3.7.4.3
typing_extensions==4.12.2
webencodings==0.5.1
And my font declarations (if it helps) look like this:
@font-face {
font-family: 'Lora';
font-display: swap;
src: local('Lora'),
url('../fonts/gen/Lora-Regular.woff2'),
url('../fonts/gen/Lora-Italic.woff2');
}