Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy-custom-fonts parameter to HtmlConfig to enable custom fonts #1807

Closed

Conversation

emirror-de
Copy link

The first PR #1799 has been accidentially closed.

This PR introduces a new parameter to the HtmlConfig struct named copy_custom_fonts. When set to true, the fonts/fonts.css file is scanned for @font-face rules that contain a src: url(font-file.name) attribute. The files that have been found are copied into the book folder on build/serve.

If copy_fonts is also enabled, the build in fonts/fonts.css is extended by the custom one. In addition to that, the build in font files are also copied to the output folder.

Example usage:

Create a fonts/ folder in your book root. Then copy the fonts you want to use into this folder (in this example we are assuming Lato-Regular.ttf)

Create a custom fonts file fonts/fonts.css

@font-face {
     font-family: "Lato";
     font-style: normal;
     font-weight: normal;
     src: url('Lato-Regular.ttf');
}

Setup your book.toml that it contains the following parameters:

[output.html]
copy-fonts = true
copy-custom-fonts = true

Adjust your theme/css/general.css file according to your needs, for example:

html {
    font-family: "Lato", sans-serif;
}

It adds a new dependency lewp-css to Cargo.toml for simple parsing of the fonts/fonts.css file.
It also updates the msrv to 1.56.0 because lewp-css requires edition 2021.
Furthermore, the clap dependency gets updated.

Only applies, when copy-fonts parameter is set to true as well.

When set to true, the fonts/fonts.css file is scanned for @font-face rules

that contain a src: url(font-file.name) attribute. The files that have been

found are copied into the book folder on build.
If both are enabled, the build in fonts.css is extended by the custom one.

Also both, the build in as well as the custom files are copied to the output

folder.
@ISSOtm
Copy link
Contributor

ISSOtm commented Jun 26, 2022

AIUI, non-processed files under src/ get copied as-is, so couldn't you simply create src/fonts/Lato-Regular.ttf and reference that (fonts/Lato-Regular.ttf)?

@emirror-de
Copy link
Author

Thanks for your response! You are right, this works indeed.
But if it comes to project structure, the font files in my opinion do not belong to the source code of the project. As a result, they should go into a separate folder to have clean separation. Would that make sense?

@ehuss
Copy link
Contributor

ehuss commented Jan 15, 2023

Thanks for the PR! I think I would like to go with a different solution posted in #1987. My goal is to keep the config as simple as possible. Any feedback on that approach is welcome, though.

@ehuss ehuss closed this Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants