I have a bunch of files in a folder. They are formatted in the following ways:
Font1-Regular.ttf
Font1-Bold.ttf
Font2-SomeString-Regular.ttf
Font3.ttf
My goal is to create CSS representation of each font and a JSON object for each font type. For the fonts above it would look like this:
@font-face {
font-family: "Font1-Regular";
src: url("../assets/fonts/Font1-Regular.ttf");
}
@font-face {
font-family: "Font1-Bold";
src: url("../assets/fonts/Font1-Bold.ttf");
}
@font-face {
font-family: "Font2-SomeString-Regular";
src: url("../assets/fonts/Font2-SomeString-Regular.ttf");
}
@font-face {
font-family: "Font3-Regular";
src: url("../assets/fonts/Font3-Regular.ttf");
}
And JSON representation of the fonts:
[
{
fontName: "Font1",
fontTypes: ["Regular", "Bold"]
},
{
fontName: "Font2-SomeString",
fontTypes: ["Regular"]
},
{
fontName: "Font3",
fontTypes: ["Regular"]
}
]
This is the logic diagram for @font-faces creation:
- check if it contain
-
character - if it does, than get the string after the last
-
and remove.ttf
. Also get the string before the last-
. If it doesn't, rename file to{previusFontName}-Regular.ttf
and repeat step 2. - crete new
@font-face
tag and append it to the file I am saving the results.
How do you check if a file contains character and then get the substring before and after the character?
I think I can handle the JSON part, once I learn how to create @font-faces.
Code so far (which returns some syntax errors)
find . -type f "*.ttf" -exec if [[{} =~ "-"]]; then echo {} else echo "FAIL" fi \;
fc-query
command may help you to extract and format all the relevant information from those ttf files.