Open ramnathv opened 10 years ago
The idea would be to create a payload and expand it to a css file using a mustache template (which you can see below. Let me illustrate with an example. The make_dataURI
function encodes a font file in base64. The make_fonts
function takes a fonts list and generates a css file.
make_dataURI <- function(font){
font$dataURI = base64enc::dataURI(font$file,
mime = sprintf('data:application/x-font-', font$format)
)
return(font)
}
make_fonts <- function(fonts, template, destfile = 'fonts.css'){
fonts <- lapply(fonts, make_dataURI)
css = paste(capture.output(cat(whisker.render(template, list(fonts = fonts)))), collapse = "\n")
cat(css, file = destfile)
return(css)
}
fonts = list(
list(name = 'Open Sans', weight = 400, style = normal, format = 'wott', file = 'Open Sans.wott'),
list(name = 'PT Sans', ...)
)
make_fonts(fonts)
Here is the mustache template to use.
{{# fonts }}
@font-face {
font-family: '{{{ name }}}';
font-style: {{{ style }}};
font-weight: {{{ weight }}};
src: url({{{ dataURI }}}) format('{{{ format }}}');
}
{{/ fonts }}
Thanks for the outline! I'll get to work on this.
I hope to have something viable tomorrow but you can track the progress here: https://github.com/seankross/cosmofont
This is looking good. A few ideas
style.css
that links to the fonts locally, or one with fonts encoded in base64. This would require minimal changes to the code.By using the Google Fonts API and including httr as a dependency I believe I can include functionality where a user can specify the name, weight, and style of a Google Font so that cosmofont can GET
the font from Google and either create the appropriate data uri or include the corresponding .woff
file with style.css
.
Oh that would be brilliant! Nice idea.
And another note. Make yourself the author and maintainer, since you are doing all the hard work. You can add me as a contributor.
Will do, but this couldn't have happened without you!
Let me know if this works for you: https://github.com/seankross/cosmofont
You can install cosmofont with:
library(devtools)
install_github("cosmofont", "seankross")
Any comments or questions about the code, API, documentation, or anything else are welcome!
Here is a rough sketch of how I think this could work