1
0
mirror of https://github.com/tabler/tabler-icons.git synced 2025-08-24 14:43:04 +02:00

Merge pull request #120 from tijmenvangulik/master

Make it easier to compile a font with sub set of icons
This commit is contained in:
Michał Wolny
2021-04-11 08:48:38 +02:00
committed by GitHub
4 changed files with 114 additions and 23 deletions

1
.gitignore vendored
View File

@@ -14,3 +14,4 @@ src/test*.svg
.jekyll-metadata
icons-react/dist/
/_import.csv
compile-options.json

View File

@@ -127,6 +127,45 @@ To load a specific version replace `latest` with the desired version number.
<script src="https://unpkg.com/@tabler/icons@1.36.0/icons-react/dist/index.umd.js"></script>
```
Compiling fonts:
To compile fonts first install [fontforge](https://fontforge.org/en-US/)
When compiling the font it will look for a json file compile-options.json in root folder (same folder as the package.json) In this file you can define extra options:
The default settings if you have not defined the file will be:
{
"includeIcons": [],
"fontForge": "fontforge",
"strokeWidth": 2
}
The fontforge executable needs to be in the path or you can set set the path to the downloaded fontforge executable in the. If you installed in on a mac in your application directory it will be "/Applications/FontForge.app/Contents/MacOS/FontForge". You can set this value in the compile-options.json file.
```JSON
{
"fontForge":"/Applications/FontForge.app/Contents/MacOS/FontForge"
}
```
To compile the fonts run:
npm run build-iconfont
By default the stroke width is 2. You can change the stroke width in the compile-options.json
```JSON
{
"strokeWidth": 1.5,
}
```
To reduce the font file size you can choose to compile a sub set of icons. When you leave the array empty it will compile all the fonts. To compile only two icons you can set for example the folowing option in the compile-options.json:
```JSON
{
"includeIcons":["alert-octagon","alert-triangle"]
}
```
### Svelte
You can use [`tabler-icons-svelte`](https://github.com/benflap/tabler-icons-svelte) to use icons in your Svelte projects (see [example](https://svelte.dev/repl/e80dc63d7019431692b10a77525e7f99?version=3.31.0)):

View File

@@ -18,6 +18,44 @@ const gulp = require('gulp'),
svgpath = require('svgpath'),
svgr = require('@svgr/core').default;
let compileOptions = {
includeIcons: [],
strokeWidth: null,
fontForge: "fontforge"
};
if (fs.existsSync('./compile-options.json')) {
try {
let tempOptions = require('./compile-options');
if (typeof tempOptions!="object") {
throw "Compile options file does not contain an json object";
}
if (typeof tempOptions.includeIcons!="undefined") {
if (!Array.isArray(tempOptions.includeIcons)) {
throw "property inludeIcons is not an array";
}
compileOptions.includeIcons= tempOptions.includeIcons;
}
if (typeof tempOptions.strokeWidth!="undefined") {
if (typeof tempOptions.strokeWidth!="string" && typeof tempOptions.strokeWidth!="number") {
throw "property strokeWidth is not a string or number";
}
compileOptions.strokeWidth=tempOptions.strokeWidth.toString();
}
if (typeof tempOptions.fontForge!="undefined") {
if (typeof tempOptions.fontForge!="string") {
throw "property fontForge is not a string";
}
compileOptions.fontForge=tempOptions.fontForge;
}
} catch (error) {
throw `Error reading compile-options.json: ${error}`
}
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
@@ -204,7 +242,11 @@ gulp.task('iconfont-svg-outline', function (cb) {
}
await asyncForEach(files, async function (file) {
const name = path.basename(file, '.svg'),
const name = path.basename(file, '.svg');
if (compileOptions.includeIcons.length==0 || compileOptions.includeIcons.indexOf(name)>=0) {
unicode = iconfontUnicode[name];
await console.log('Stroke for:', file, unicode);
@@ -214,6 +256,9 @@ gulp.task('iconfont-svg-outline', function (cb) {
strokedSVG = strokedSVG
.replace('width="24"', 'width="1000"')
.replace('height="24"', 'height="1000"');
if (compileOptions.strokeWidth) {
strokedSVG = strokedSVG.replace('stroke-width="2"', `stroke-width="${compileOptions.strokeWidth}"`);
}
await outlineStroke(strokedSVG, {
optCurve: false,
@@ -229,6 +274,8 @@ gulp.task('iconfont-svg-outline', function (cb) {
fs.writeFileSync(`icons-outlined/${name}.svg`, outlined);
}
}).catch(error => console.log(error));
}
});
cb();
@@ -242,8 +289,10 @@ gulp.task('iconfont-optimize', function() {
});
gulp.task('iconfont-fix-outline', function(cb) {
var fontForge= compileOptions.fontForge;
// correct svg outline directions in a child process using fontforge
const generate = cp.spawn("fontforge", ["-lang=py", "-script", "./fix-outline.py"], { stdio: 'inherit' });
const generate = cp.spawn(fontForge, ["-lang=py", "-script", "./fix-outline.py"], { stdio: 'inherit' });
generate.on("close", function (code) {
console.log(`Correcting svg outline directions exited with code ${code}`);
if (!code) {

View File

@@ -40,7 +40,9 @@
"prebuild-react": "rm -rf ./icons-react/dist/",
"build-react": "rollup -c",
"optimize": "gulp optimize",
"release": "release-it"
"release": "release-it",
"build": "gulp build",
"build-iconfont": "gulp build-iconfont"
},
"description": "A set of free MIT-licensed high-quality SVG icons for you to use in your web projects.",
"keywords": [