Skip to content

Commit 9e15f7e

Browse files
authored
Merge pull request #9 from saltcandy123/develop
Add npm-publish automation and remove webapp
2 parents 7366c94 + 1667365 commit 9e15f7e

28 files changed

Lines changed: 238 additions & 3802 deletions

.github/workflows/font-image.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
name: Font image
3+
4+
on:
5+
push:
6+
branches: [ main ]
7+
8+
jobs:
9+
build-font:
10+
name: Update font image
11+
runs-on: ubuntu-20.04
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Install FontForge
15+
run: sudo apt-get install fontforge python3-fontforge
16+
- name: Build fonts
17+
run: python3 build_font.py ${{ github.sha }}
18+
- name: Update font image
19+
run: bash update-fontimage.sh
20+
- name: Push the update to GitHub
21+
run: |
22+
git config user.email "saltcandy123@gmail.com"
23+
git config user.name "saltcandy123 (GitHub Actions)"
24+
git add fontimage.png
25+
if git commit -m 'Update font image'; then
26+
git push origin
27+
else
28+
echo No change on fontimage.png
29+
fi

.github/workflows/github-pages.yml

Lines changed: 0 additions & 43 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@ on:
77

88
jobs:
99
build-font:
10-
name: Build fonts
10+
name: Build font
1111
runs-on: ubuntu-20.04
1212
steps:
1313
- uses: actions/checkout@v2
1414
- name: Install FontForge
1515
run: sudo apt-get install fontforge python3-fontforge
1616
- name: Build fonts
17-
run: python3 build_fonts.py --add-version ${GITHUB_REF#refs/tags/}
17+
run: python3 build_font.py ${GITHUB_REF#refs/tags/v}
18+
- name: Upload artifact
19+
uses: actions/upload-artifact@v2
20+
with:
21+
name: dist
22+
path: dist
23+
create-release:
24+
name: Create release
25+
needs: build-font
26+
runs-on: ubuntu-20.04
27+
steps:
28+
- name: Download artifact
29+
uses: actions/download-artifact@v2
30+
with:
31+
name: dist
32+
path: dist
1833
- name: Create release
1934
id: create_release
2035
uses: actions/create-release@v1
@@ -29,7 +44,7 @@ jobs:
2944
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3045
with:
3146
upload_url: ${{ steps.create_release.outputs.upload_url }}
32-
asset_path: font-dist/saltcandy123font.ttf
47+
asset_path: dist/saltcandy123font.ttf
3348
asset_name: saltcandy123font.ttf
3449
asset_content_type: font/ttf
3550
- name: Upload saltcandy123font.woff
@@ -38,6 +53,50 @@ jobs:
3853
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3954
with:
4055
upload_url: ${{ steps.create_release.outputs.upload_url }}
41-
asset_path: font-dist/saltcandy123font.woff
56+
asset_path: dist/saltcandy123font.woff
4257
asset_name: saltcandy123font.woff
4358
asset_content_type: font/woff
59+
publish-to-npm-registry:
60+
name: Publish npm package to npm registry
61+
needs: build-font
62+
runs-on: ubuntu-20.04
63+
steps:
64+
- uses: actions/setup-node@v2
65+
with:
66+
node-version: '14.x'
67+
registry-url: 'https://registry.npmjs.org'
68+
- name: Download artifact
69+
uses: actions/download-artifact@v2
70+
with:
71+
name: dist
72+
path: dist
73+
- name: Publish package
74+
run: |
75+
cd dist
76+
yarn publish
77+
env:
78+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
79+
publish-to-github-registry:
80+
name: Publish npm package to GitHub Packages registry
81+
needs: build-font
82+
runs-on: ubuntu-20.04
83+
permissions:
84+
contents: read
85+
packages: write
86+
steps:
87+
- uses: actions/setup-node@v2
88+
with:
89+
node-version: '14.x'
90+
registry-url: 'https://npm.pkg.github.com'
91+
scope: '@saltcandy123'
92+
- name: Download artifact
93+
uses: actions/download-artifact@v2
94+
with:
95+
name: dist
96+
path: dist
97+
- name: Publish package
98+
run: |
99+
cd dist
100+
yarn publish
101+
env:
102+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/verification.yml

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,27 @@ on:
88

99
jobs:
1010
build:
11-
name: Build fonts and demo pages
12-
# Ubuntu 20.04 or later is required for python3-fontforge
11+
name: Build font
1312
runs-on: ubuntu-20.04
1413
steps:
1514
- uses: actions/checkout@v2
16-
- uses: actions/setup-node@v2
17-
with:
18-
node-version: '14'
1915
- name: Lint glyph SVG files
2016
run: |
21-
python3 utils/clean_glyphs.py glyphs/
17+
python3 clean_glyphs.py glyphs/
2218
git diff --numstat --exit-code
2319
- name: Install FontForge
2420
run: sudo apt-get install fontforge python3-fontforge
25-
- name: Build fonts
26-
run: python3 build_fonts.py --add-version ${{ github.sha }}
27-
- name: Archive font-dist
28-
uses: actions/upload-artifact@v2
29-
with:
30-
name: font-dist
31-
path: font-dist
32-
- name: Build webapp
21+
- name: Build font
22+
run: python3 build_font.py ${{ github.sha }}
23+
- name: Update font image
3324
run: |
34-
cd webapp
35-
yarn
36-
yarn run lint
37-
yarn run build
38-
- name: Archive webapp-dist
25+
bash update-fontimage.sh
26+
cp fontimage.png dist/
27+
- name: Archive dist
3928
uses: actions/upload-artifact@v2
4029
with:
41-
name: webapp-dist
42-
path: webapp-dist
30+
path: dist
31+
name: dist
4332
super-linter:
4433
name: Run Super-Linter
4534
runs-on: ubuntu-latest
@@ -49,4 +38,4 @@ jobs:
4938
uses: github/super-linter@v3
5039
env:
5140
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52-
FILTER_REGEX_EXCLUDE: .*/glyphs/.*|.*/webapp/.*\.(jsx?|tsx?)
41+
FILTER_REGEX_EXCLUDE: .*/glyphs/.*

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
node_modules
2-
.next
3-
/font-dist
4-
/webapp-dist
5-
yarn-error.log
1+
/dist

README-npm.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# saltcandy123font
2+
3+
This is a simple handwritten font created by @saltcandy123.
4+
5+
![Font image](https://raw.githubusercontent.com/saltcandy123/saltcandy123font/main/fontimage.png)
6+
7+
## Usage
8+
9+
Install the package.
10+
11+
```bash
12+
# npm
13+
npm i @saltcandy123/saltcandy123font
14+
15+
# yarn
16+
yarn add @saltcandy123/saltcandy123font
17+
```
18+
19+
Then, use `saltcandy123font.ttf` or `saltcandy123font.woff` in your frontend code.
20+
21+
```javascript
22+
import saltcandy123font from '@saltcandy123/saltcandy123font/saltcandy123font.woff';
23+
```

README.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,14 @@
44

55
This is a simple handwritten font created by @saltcandy123.
66

7-
* Download font from <https://github.com/saltcandy123/saltcandy123font/releases>
8-
* Try out demo on <https://saltcandy123.github.io/saltcandy123font/>
7+
- Download the `ttf` file and the `woff` from [the release page](https://github.com/saltcandy123/saltcandy123font/releases).
8+
- An npm package ([@saltcandy123/saltcandy123font](https://www.npmjs.com/package/@saltcandy123/saltcandy123font)) is also available.
99

10-
![Sample image](https://saltcandy123.github.io/saltcandy123font/sample.png)
10+
![Font image](./fontimage.png)
1111

12-
## How it works
12+
## Source code
1313

14-
All the glyph data is stored as SVG files in [`/glyphs`](./glyphs) directory.
15-
Each SVG file is named after the corresponding 4-digit hex Unicode.
16-
For instance, the filename for "A" glyph should be [`u0041.svg`](./glyphs/u0041.svg) because the Unicode of "A" is 41 in hex.
17-
See also [Unicode Character Code Charts](https://unicode.org/charts/).
18-
19-
A Python script [`/build_fonts.py`](./build_fonts.py) converts the SVG files to fonts in `/font-dist` directory.
20-
It requires [FontForge](https://fontforge.org/) and [its Python library](https://fontforge.org/docs/scripting/python.html).
21-
22-
[`/webapp`](./webapp) is the source code directory for <https://saltcandy123.github.io/saltcandy123font/>.
23-
`yarn && yarn build` builds static pages in `/webapp-dist` directory.
24-
`yarn build` requires `/font-dist` directory with font files.
25-
26-
Releasing is handled by GitHub Actions.
27-
By pushing a tag to this repository, GitHub Actions will automatically [create a new release](./.github/workflows/release.yml) and [update GitHub Pages](./.github/workflows/github-pages.yml).
14+
- **`glyphs/uXXXX.svg`** defines the shape of each character. For example, [`u0041.svg`](glyphs/u0041.svg) is the image of "A".
15+
- **[`build_font.py`](build_font.py)** is a script to convert the glyph SVG files to font files in `dist` directory. It also adds a few additional files for the npm package. The Python script requires [FontForge](https://fontforge.org/) and [its Python library](https://fontforge.org/docs/scripting/python.html).
16+
- **[`clean_glyphs.py`](clean_glyphs.py)** is a script to clean and normalize SVG files. `--add-template` flag draws template lines on each SVG, which is useful for working on individual glyphs.
17+
- **[`update-fontimage.sh`](update-fontimage.sh)** is a script to update [`fontimage.png`](fontimage.png) with the latest font. It's mainly for GitHub Actions to update the image automatically.

build_font.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
3+
"""Build saltcandy123font from SVG files"""
4+
5+
import argparse
6+
import json
7+
import pathlib
8+
import re
9+
import xml.dom.minidom
10+
11+
import fontforge
12+
13+
BASE_DIR = pathlib.Path(__file__).parent
14+
15+
16+
def build_saltcandy123font(*, version: str) -> fontforge.font:
17+
font = fontforge.font()
18+
font.fontname = "saltcandy123font"
19+
font.fullname = font.fontname
20+
font.familyname = font.fontname
21+
font.copyright = "Copyright (C) saltcandy123"
22+
font.weight = "Regular"
23+
font.os2_weight = 400
24+
font.version = version
25+
26+
for svg_path in BASE_DIR.joinpath("glyphs").iterdir():
27+
match = re.search("^u([0-9a-f]{4}).svg$", svg_path.name)
28+
if not match:
29+
continue
30+
code = int(match.group(1), 16)
31+
glyph = font.createChar(code)
32+
glyph.importOutlines(str(svg_path))
33+
with xml.dom.minidom.parse(str(svg_path)) as doc:
34+
glyph.width = int(doc.childNodes[0].getAttribute("width"))
35+
36+
return font
37+
38+
39+
def generate_npm_package_metadata(*, version=str) -> dict:
40+
return {
41+
"name": "@saltcandy123/saltcandy123font",
42+
"version": version,
43+
"description": "A simple handwritten font created by @saltcandy123",
44+
"repository": "https://github.com/saltcandy123/saltcandy123font",
45+
"author": "saltcandy123",
46+
}
47+
48+
49+
def main():
50+
parser = argparse.ArgumentParser(description=__doc__)
51+
parser.add_argument("font_version", help="font version (e.g. 0.1.2)")
52+
args = parser.parse_args()
53+
54+
dist_dir = BASE_DIR.joinpath("dist")
55+
dist_dir.mkdir(exist_ok=True)
56+
57+
font = build_saltcandy123font(version=args.font_version)
58+
59+
for ext in ["ttf", "woff"]:
60+
font.generate(str(dist_dir.joinpath(f"saltcandy123font.{ext}")))
61+
62+
package_metadata = generate_npm_package_metadata(version=args.font_version)
63+
with open(dist_dir.joinpath("package.json"), "w") as f:
64+
json.dump(package_metadata, f, indent=2)
65+
66+
with open(dist_dir.joinpath("README.md"), "w") as f:
67+
with open(BASE_DIR.joinpath("README-npm.md")) as f_src:
68+
f.write(f_src.read())
69+
70+
71+
if __name__ == "__main__":
72+
main()

0 commit comments

Comments
 (0)