From e43988e4adac46b78753c90f49b92645d08d118d Mon Sep 17 00:00:00 2001 From: tijmenvangulik Date: Sun, 21 Mar 2021 18:29:46 +0100 Subject: Make it easier to compile a font with sub set of icons --- README.md | 29 +++++++++++++++++++++++++++++ gulpfile.js | 17 +++++++++++++++-- package.json | 9 ++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c3d6acc2..8f50edd2 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,35 @@ To load a specific version replace `latest` with the desired version number. ``` +Compiling fonts: + +To compile fonts first install [fontforge](https://fontforge.org/en-US/) + +the fontforge executable needs to be in the path or you can set set the path to the downloaded fontforge executable in the package.json. If you installed in on a mac in your application directory it will be: +```JSON + "compileFonts": { + "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 by setting the package property: + +```JSON + "compileFonts": { + "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. For example: + +```JSON + "includeIcons": { + "include":["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)): diff --git a/gulpfile.js b/gulpfile.js index 9f541ab7..5d57298d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -191,6 +191,7 @@ gulp.task('iconfont-clean', function (cb) { cb(); }); }); +const includeIcons=typeof p.compileFonts=='object'? p.compileFonts.include:[]; gulp.task('iconfont-svg-outline', function (cb) { @@ -204,7 +205,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 (!Array.isArray( includeIcons) || includeIcons.length==0 || includeIcons.indexOf(name)>=0) { + unicode = iconfontUnicode[name]; await console.log('Stroke for:', file, unicode); @@ -214,6 +219,8 @@ gulp.task('iconfont-svg-outline', function (cb) { strokedSVG = strokedSVG .replace('width="24"', 'width="1000"') .replace('height="24"', 'height="1000"'); + if (typeof p.compileFonts=='object' && typeof p.compileFonts.strokeWidth=="string") + strokedSVG = strokedSVG.replace('stroke-width="2"', `stroke-width="${p.compileFonts.strokeWidth}"`); await outlineStroke(strokedSVG, { optCurve: false, @@ -229,6 +236,8 @@ gulp.task('iconfont-svg-outline', function (cb) { fs.writeFileSync(`icons-outlined/${name}.svg`, outlined); } }).catch(error => console.log(error)); + } + }); cb(); @@ -242,8 +251,12 @@ gulp.task('iconfont-optimize', function() { }); gulp.task('iconfont-fix-outline', function(cb) { + var fontForge= 'fontforge'; + if (typeof p.compileFonts=='object' && typeof p.compileFonts.fontForge=='string') + fontForge=p.compileFonts.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) { diff --git a/package.json b/package.json index 56f9c6cf..d8ff5c53 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,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": [ @@ -99,5 +101,10 @@ "peerDependencies": { "react": "^16.13.1", "react-dom": "^16.13.1" + }, + "compileFonts": { + "include":[], + "strokeWidth":"2", + "fontForge":"fontforge" } } -- cgit v1.2.1 From 6d046c56ef8e15678d01368bb4032eb1796d7170 Mon Sep 17 00:00:00 2001 From: tijmenvangulik Date: Mon, 22 Mar 2021 18:46:43 +0100 Subject: Corrected wrong encoded unicode characters in css --- .build/iconfont.scss | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.build/iconfont.scss b/.build/iconfont.scss index d09a7ac1..c74e1a7e 100644 --- a/.build/iconfont.scss +++ b/.build/iconfont.scss @@ -40,9 +40,12 @@ $ti-prefix: 'ti' !default; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } +@function unicode($str) { + @return unquote("\"")+unquote(str-insert($str, "\\", 1))+unquote("\"") +} <% glyphs.forEach(function(glyph) { %> -$ti-icon-<%= glyph.name %>: '\<%= glyph.unicode[0].codePointAt(0).toString(16) %>';<% }); %> +$ti-icon-<%= glyph.name %>: unicode('<%= glyph.unicode[0].codePointAt(0).toString(16) %>');<% }); %> <% glyphs.forEach(function(glyph) { %> .#{$ti-prefix}-<%= glyph.name %>:before { content: $ti-icon-<%= glyph.name %>; }<% }); %> -- cgit v1.2.1 From c0f30068c2f46e7015cea7e45e08bf902a2815d9 Mon Sep 17 00:00:00 2001 From: tijmenvangulik Date: Sat, 10 Apr 2021 10:34:00 +0200 Subject: Moved compileFonts properties from package to separate file --- .gitignore | 1 + README.md | 26 +++++++++++------ gulpfile.js | 91 ++++++++++++++++++++++++++++++++++++++++-------------------- package.json | 8 ++---- 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 4aa836dc..85e11470 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ src/test*.svg .jekyll-metadata icons-react/dist/ /_import.csv +compile-options.json diff --git a/README.md b/README.md index 8f50edd2..27b0f87c 100644 --- a/README.md +++ b/README.md @@ -131,28 +131,38 @@ Compiling fonts: To compile fonts first install [fontforge](https://fontforge.org/en-US/) -the fontforge executable needs to be in the path or you can set set the path to the downloaded fontforge executable in the package.json. If you installed in on a mac in your application directory it will be: +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 - "compileFonts": { + { "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 by setting the package property: +By default the stroke width is 2. You can change the stroke width in the compile-options.json ```JSON - "compileFonts": { - "strokeWidth":"1.5", + { + "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. For example: +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": { - "include":["alert-octagon","alert-triangle"] + { + "includeIcons":["alert-octagon","alert-triangle"] } ``` diff --git a/gulpfile.js b/gulpfile.js index 5d57298d..9468d785 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -18,6 +18,40 @@ 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); @@ -191,7 +225,6 @@ gulp.task('iconfont-clean', function (cb) { cb(); }); }); -const includeIcons=typeof p.compileFonts=='object'? p.compileFonts.include:[]; gulp.task('iconfont-svg-outline', function (cb) { @@ -203,39 +236,39 @@ gulp.task('iconfont-svg-outline', function (cb) { if (fs.existsSync('./.build/iconfont-unicode.json')) { iconfontUnicode = require('./.build/iconfont-unicode'); } - + await asyncForEach(files, async function (file) { const name = path.basename(file, '.svg'); - if (!Array.isArray( includeIcons) || includeIcons.length==0 || includeIcons.indexOf(name)>=0) { + if (compileOptions.includeIcons.length==0 || compileOptions.includeIcons.indexOf(name)>=0) { unicode = iconfontUnicode[name]; - await console.log('Stroke for:', file, unicode); - - let strokedSVG = fs.readFileSync(file).toString(); - - strokedSVG = strokedSVG - .replace('width="24"', 'width="1000"') - .replace('height="24"', 'height="1000"'); - if (typeof p.compileFonts=='object' && typeof p.compileFonts.strokeWidth=="string") - strokedSVG = strokedSVG.replace('stroke-width="2"', `stroke-width="${p.compileFonts.strokeWidth}"`); - - await outlineStroke(strokedSVG, { - optCurve: false, - steps: 4, - round: 0, - centerHorizontally: true, - fixedWidth: true, - color: 'black' - }).then(outlined => { - if (unicode) { - fs.writeFileSync(`icons-outlined/u${unicode.toUpperCase()}-${name}.svg`, outlined); - } else { - fs.writeFileSync(`icons-outlined/${name}.svg`, outlined); - } - }).catch(error => console.log(error)); + await console.log('Stroke for:', file, unicode); + + let strokedSVG = fs.readFileSync(file).toString(); + + 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, + steps: 4, + round: 0, + centerHorizontally: true, + fixedWidth: true, + color: 'black' + }).then(outlined => { + if (unicode) { + fs.writeFileSync(`icons-outlined/u${unicode.toUpperCase()}-${name}.svg`, outlined); + } else { + fs.writeFileSync(`icons-outlined/${name}.svg`, outlined); + } + }).catch(error => console.log(error)); } }); @@ -251,9 +284,7 @@ gulp.task('iconfont-optimize', function() { }); gulp.task('iconfont-fix-outline', function(cb) { - var fontForge= 'fontforge'; - if (typeof p.compileFonts=='object' && typeof p.compileFonts.fontForge=='string') - fontForge=p.compileFonts.fontForge; + 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' }); diff --git a/package.json b/package.json index d8ff5c53..985fb8fb 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "optimize": "gulp optimize", "release": "release-it", "build": "gulp build", - "build-iconfont": "gulp build-iconfont" + "build-iconfont": "gulp build-iconfont", + "copy":"cp -r -n -v -f ./iconfont/fonts/* ../WebErgometer/WebApp/html/font" }, "description": "A set of free MIT-licensed high-quality SVG icons for you to use in your web projects.", "keywords": [ @@ -101,10 +102,5 @@ "peerDependencies": { "react": "^16.13.1", "react-dom": "^16.13.1" - }, - "compileFonts": { - "include":[], - "strokeWidth":"2", - "fontForge":"fontforge" } } -- cgit v1.2.1 From ad20982ece66803a7d78d17ad69925c20b38ccde Mon Sep 17 00:00:00 2001 From: tijmenvangulik Date: Sun, 11 Apr 2021 08:42:30 +0200 Subject: Added brackets and fixed ident --- gulpfile.js | 25 +++++++++++++++---------- package.json | 3 +-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 9468d785..597b513a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -27,22 +27,26 @@ let compileOptions = { 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!="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"; + 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"; + 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"; + if (typeof tempOptions.fontForge!="string") { + throw "property fontForge is not a string"; + } compileOptions.fontForge=tempOptions.fontForge; } @@ -252,8 +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}"`); + if (compileOptions.strokeWidth) { + strokedSVG = strokedSVG.replace('stroke-width="2"', `stroke-width="${compileOptions.strokeWidth}"`); + } await outlineStroke(strokedSVG, { optCurve: false, diff --git a/package.json b/package.json index 985fb8fb..df9ce7ea 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,7 @@ "optimize": "gulp optimize", "release": "release-it", "build": "gulp build", - "build-iconfont": "gulp build-iconfont", - "copy":"cp -r -n -v -f ./iconfont/fonts/* ../WebErgometer/WebApp/html/font" + "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": [ -- cgit v1.2.1