├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── ci-cd.yml ├── .gitignore ├── .husky ├── .gitattributes └── commit-msg ├── .nvmrc ├── CHANGELOG.md ├── LICENSE.txt ├── OFL.txt ├── README.md ├── commitlint.config.js ├── package-lock.json ├── package.json ├── release.config.js ├── renovate.json5 ├── src ├── .eslintrc.js ├── Grand9K-Pixel.ttf ├── Griffy-Regular.ttf ├── Knewave.ttf ├── NotoSans-Medium.ttf ├── Scratch.ttf ├── SourceSerifPro-Regular.otf ├── handlee-regular.ttf └── index.js └── webpack.config.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | charset = utf-8 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | 10 | [*.js] 11 | indent_style = space 12 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist/* 2 | node_modules/* 3 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ['scratch', 'scratch/node', 'scratch/es6'] 3 | }; 4 | -------------------------------------------------------------------------------- /.github/workflows/ci-cd.yml: -------------------------------------------------------------------------------- 1 | name: CI/CD 2 | 3 | on: 4 | workflow_dispatch: # Allows you to run this workflow manually from the Actions tab 5 | pull_request: # Runs whenever a pull request is created or updated 6 | push: # Runs whenever a commit is pushed to the repository 7 | branches: [master, hotfix/*] # ...on any of these branches 8 | 9 | concurrency: 10 | group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' 11 | cancel-in-progress: true 12 | 13 | permissions: 14 | contents: write # publish a GitHub release 15 | pages: write # deploy to GitHub Pages 16 | issues: write # comment on released issues 17 | pull-requests: write # comment on released pull requests 18 | 19 | jobs: 20 | ci-cd: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 24 | - uses: wagoid/commitlint-github-action@5ce82f5d814d4010519d15f0552aec4f17a1e1fe # v5 25 | if: github.event_name == 'pull_request' 26 | - uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3 27 | with: 28 | cache: 'npm' 29 | node-version-file: '.nvmrc' 30 | - name: Info 31 | run: | 32 | cat < message.startsWith('chore(release):')] 4 | }; 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "scratch-render-fonts", 3 | "version": "1.0.198", 4 | "description": "", 5 | "main": "./src/index.js", 6 | "author": "Massachusetts Institute of Technology", 7 | "homepage": "https://github.com/scratchfoundation/scratch-render-fonts#readme", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://git@github.com/scratchfoundation/scratch-render-fonts.git" 11 | }, 12 | "scripts": { 13 | "build": "npm run clean && webpack --progress --bail", 14 | "clean": "rimraf ./dist", 15 | "lint": "eslint .", 16 | "test": "npm run build", 17 | "watch": "webpack --progress --watch" 18 | }, 19 | "config": { 20 | "commitizen": { 21 | "path": "cz-conventional-changelog" 22 | } 23 | }, 24 | "dependencies": { 25 | "base64-loader": "^1.0.0" 26 | }, 27 | "devDependencies": { 28 | "@commitlint/cli": "17.8.1", 29 | "@commitlint/config-conventional": "17.8.1", 30 | "babel-core": "6.26.3", 31 | "babel-eslint": "10.1.0", 32 | "babel-loader": "7.1.5", 33 | "babel-preset-env": "1.7.0", 34 | "eslint": "8.57.1", 35 | "eslint-config-scratch": "9.0.9", 36 | "husky": "8.0.3", 37 | "json": "10.0.0", 38 | "lodash.defaultsdeep": "4.6.1", 39 | "rimraf": "2.7.1", 40 | "scratch-semantic-release-config": "3.0.0", 41 | "semantic-release": "19.0.5", 42 | "webpack": "4.47.0", 43 | "webpack-cli": "3.3.12" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /release.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: 'scratch-semantic-release-config', 3 | branches: [ 4 | { 5 | name: 'master' 6 | // default channel 7 | }, 8 | { 9 | name: 'hotfix/*', 10 | channel: 'hotfix' 11 | } 12 | ] 13 | }; 14 | -------------------------------------------------------------------------------- /renovate.json5: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | 4 | "extends": [ 5 | "github>scratchfoundation/scratch-renovate-config:js-lib-bundled" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /src/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['scratch', 'scratch/es6'], 4 | env: { 5 | browser: true 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /src/Grand9K-Pixel.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/Grand9K-Pixel.ttf -------------------------------------------------------------------------------- /src/Griffy-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/Griffy-Regular.ttf -------------------------------------------------------------------------------- /src/Knewave.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/Knewave.ttf -------------------------------------------------------------------------------- /src/NotoSans-Medium.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/NotoSans-Medium.ttf -------------------------------------------------------------------------------- /src/Scratch.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/Scratch.ttf -------------------------------------------------------------------------------- /src/SourceSerifPro-Regular.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/SourceSerifPro-Regular.otf -------------------------------------------------------------------------------- /src/handlee-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scratchfoundation/scratch-render-fonts/1e94b5696d8c9c6375a4299136362f1edb7ccb75/src/handlee-regular.ttf -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // Synchronously load TTF fonts. 2 | // First, have Webpack load their data as Base 64 strings. 3 | let FONTS; 4 | 5 | const getFonts = function () { 6 | if (FONTS) return FONTS; 7 | /* eslint-disable global-require */ 8 | FONTS = { 9 | 'Sans Serif': require('base64-loader!./NotoSans-Medium.ttf'), 10 | 'Serif': require('base64-loader!./SourceSerifPro-Regular.otf'), 11 | 'Handwriting': require('base64-loader!./handlee-regular.ttf'), 12 | 'Marker': require('base64-loader!./Knewave.ttf'), 13 | 'Curly': require('base64-loader!./Griffy-Regular.ttf'), 14 | 'Pixel': require('base64-loader!./Grand9K-Pixel.ttf'), 15 | 'Scratch': require('base64-loader!./Scratch.ttf') 16 | }; 17 | /* eslint-enable global-require */ 18 | 19 | // For each Base 64 string, 20 | // 1. Replace each with a usable @font-face tag that points to a Data URI. 21 | // 2. Inject the font into a style on `document.body`, so measurements 22 | // can be accurately taken in SvgRenderer._transformMeasurements. 23 | for (const fontName in FONTS) { 24 | const fontData = FONTS[fontName]; 25 | FONTS[fontName] = '@font-face {' + 26 | `font-family: "${fontName}";src: url("data:application/x-font-ttf;charset=utf-8;base64,${fontData}");}`; 27 | } 28 | 29 | if (!document.getElementById('scratch-font-styles')) { 30 | const documentStyleTag = document.createElement('style'); 31 | documentStyleTag.id = 'scratch-font-styles'; 32 | for (const fontName in FONTS) { 33 | documentStyleTag.textContent += FONTS[fontName]; 34 | } 35 | document.body.insertBefore(documentStyleTag, document.body.firstChild); 36 | } 37 | 38 | return FONTS; 39 | }; 40 | 41 | module.exports = getFonts; 42 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const defaultsDeep = require('lodash.defaultsdeep'); 2 | const path = require('path'); 3 | 4 | const base = { 5 | mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', 6 | devtool: 'cheap-module-source-map', 7 | entry: { 8 | 'scratch-render-fonts': './src/index.js' 9 | }, 10 | module: { 11 | rules: [{ 12 | include: path.resolve('src'), 13 | test: /\.js$/, 14 | loader: 'babel-loader', 15 | options: { 16 | presets: [['env', {targets: {}}]] 17 | } 18 | }] 19 | } 20 | }; 21 | 22 | module.exports = [ 23 | defaultsDeep({}, base, { 24 | output: { 25 | library: 'ScratchRenderFonts', 26 | libraryTarget: 'umd', 27 | path: path.resolve('dist', 'web'), 28 | filename: '[name].js' 29 | }, 30 | module: { 31 | rules: [{ 32 | options: { 33 | presets: [['env', {targets: {browsers: ['last 3 versions', 'Safari >= 8', 'iOS >= 8']}}]] 34 | } 35 | }] 36 | }, 37 | optimization: { 38 | minimize: process.env.NODE_ENV === 'production' 39 | } 40 | }), 41 | // For testing only: many features will fail outside a browser 42 | defaultsDeep({}, base, { 43 | output: { 44 | library: 'ScratchRenderFonts', 45 | libraryTarget: 'commonjs2', 46 | path: path.resolve('dist', 'node'), 47 | filename: '[name].js' 48 | }, 49 | module: { 50 | rules: [{ 51 | options: { 52 | presets: [['env', {targets: {node: true, uglify: true}}]] 53 | } 54 | }] 55 | }, 56 | performance: { 57 | hints: false 58 | }, 59 | optimization: { 60 | minimize: false 61 | } 62 | }) 63 | ]; 64 | --------------------------------------------------------------------------------