Skip to content

Commit

Permalink
Allow loading fonts with awc.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ifrost committed Nov 5, 2020
1 parent 210ca01 commit 9f766a9
Show file tree
Hide file tree
Showing 28 changed files with 373 additions and 644 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Other features:

# License: MIT

Copyright (c) 2015-2019 Piotr Jamróz
Copyright (c) 2015-2020 Piotr Jamróz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 2 additions & 0 deletions awc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ require.config({
var Bootstrap = require('bootstrap');
var ClientConfig = require('client/client-config');

ClientConfig.awrequire = require;

Bootstrap.init(ClientConfig);
41 changes: 8 additions & 33 deletions docs/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Available options:
| print_watermark | string | watermark text |
| scenes_numbers | "none"/"left"/"right"/"both" | side of auto-numbering scenes |
| each_scene_on_new_page | true/false | break page after a scene |
| font_family | Courier, CourierPrime, CourierPrimeCyrillic, Kosugi | font |

Provide config as attribute
---------------------------
Expand Down Expand Up @@ -204,43 +205,17 @@ That hierachy will be converted to a flat list of snippets equivalent to:
Custom fonts:
-------------

Custom fonts may be passed via a json file of the correct structure along with a matching value in the font_family property of your config map.
Custom fonts may be passed via a jsonp file with PDF fonts. It can be generated by running:

> node awc.js --source screenplay.fountain --pdf --config config.json --fonts myFonts.json
> node tools/fonts-converter.js -r ./RegularFont.ttf -b ./BoldFont.ttf -i ./ItalicFont.ttf -x ./BoldAndItalicFont.ttf -o my-font.js

**config.json**
Only regular font is required so it's enough to pass:

{
font_family: "MyFont"
}
> node tools/fonts-converter.js -r ./RegularFont.ttf -o my-font.js

**myFonts.json** (multiple font profiles may be specified)

{
"MyFont":
{
"normal":
{
"src": "<base64 encodeded ttf>"
"family": "MyFont"
},
"bold":
{
"src": "<base64 encodeded ttf>"
"family": "MyFont-Bold"
},
"bolditalic":
{
"src": "<base64 encodeded ttf>"
"family": "MyFont-BoldOblique"
},
"italic":
{
"src": "<base64 encodeded ttf>"
"family": "MyFont-Oblique"
}
}
}
Generated file (my-font.js) can be used to generate screenplays by running:

> node awc.js --source screenplay.fountain --pdf --fonts ./my-fonts.js

Note: Monospaced fonts are recommended. It is not guaranteed that all fonts will render equally well due to differences is character height and width, so please test your configuration prior to distribution or printing.

Expand Down
3 changes: 2 additions & 1 deletion js/client/awrequire.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');
var Module = require('module');
var process = require('process');

var _require = require;

Expand All @@ -11,7 +12,7 @@ var config = {};
var resolve_module_name = function(name) {
var root = __dirname,
fragments = name.split('/'),
path_parts = [root, '..'].concat(fragments),
path_parts = name.startsWith('.') ? [process.cwd()].concat(fragments) : [root, '..'].concat(fragments),
module_path = path.join.apply(null, path_parts);
return module_path + '.js';
};
Expand Down
9 changes: 7 additions & 2 deletions js/client/client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ define(function(require) {
* @augments bootstrap/core-config
*/
var ClientConfig = CoreConfig.extend({


/**
* Passed in awc.js
*/
awrequire: null,

init: function(context) {

CoreConfig.init.call(this, context);

context.register('options', Options.create());
context.register('configLoader', ConfigLoader.create());
context.register(ClientController.create());
context.register(ClientController.create(this.awrequire));
}

});
Expand Down
23 changes: 17 additions & 6 deletions js/client/client-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var ClientController = Protoplast.Object.extend({
inject: 'configLoader'
},

awrequire: null,

$create: function(awrequire) {
this.awrequire = awrequire;
},

init: function() {

console.info('Loading script:', this.options.ops.source);
Expand All @@ -37,12 +43,17 @@ var ClientController = Protoplast.Object.extend({
if (this.options.ops.pdf) {
this._validatePdf(function () {
console.log('Generating PDF', this.options.ops.pdf);
this.configLoader.loadFromFile(this.options.ops.fonts, null, function(customFonts) {
this.pdfController.getPdf(function () {
console.log('Done!');
process.exit(0);
}, this.options.ops.pdf, customFonts);
}.bind(this));

var customFonts;
if (this.options.ops.fonts) {
customFonts = this.awrequire(this.options.ops.fonts);
}

this.pdfController.getPdf(function () {
console.log('Done!');
process.exit(0);
}, this.options.ops.pdf, customFonts);

}.bind(this));
}
}.bind(this));
Expand Down
18 changes: 7 additions & 11 deletions js/core/controller/pdf-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ define(function(require) {
fontFixEnabled: false,

getPdf: function(callback, filePath, customFonts) {
var loader;

// LOAD IN CUSTOM FONT PROFILE(S)
if (customFonts) {
for (var fontName in customFonts) {
fonts[fontName] = {};
for (var fontType in customFonts[fontName]) {
fonts[fontName][fontType] = {
family: customFonts[fontName][fontType].family,
src: fontUtils.convertBase64ToBinary(customFonts[fontName][fontType].src)
};
}
}
loader = function(callback) {
callback(customFonts);
};
} else {
loader = fonts[this.settings.font_family];
}

fonts[this.settings.font_family](function(font) {
loader(function(font) {
pdfmaker.get_pdf(
{
callback: callback,
Expand Down
26 changes: 14 additions & 12 deletions js/fonts/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# How to add a new font?

1. Copy font files to afterwriting-labs/js/fonts/source/FolderWithMyFonts
2. Generate JSONP file with fonts using:
cd afterwriting-labs/js/fonts
node fonts-converter.js -r source/FolderWithMyFonts/MyFont-Regular.ttf -b source/FolderWithMyFonts/MyFont-Bold.ttf -i source/FolderWithMyFonts/MyFont-Italic.ttf -x source/FolderWithMyFonts/MyFont-Bold-Italic.ttf -o my-font.js
3. Make font available for loading in: afterwriting-labs/js/utils/fonts.js
4. Make font embedded in node.js: afterwriting-labs/js/utils/fonts-node.js
5. Make new font available in settings. Add font_family entry in: afterwriting-labs/js/plugin/settings/model/settings-config-provider.js

# Sources:

## courier-prime.js

Courier Prime: https://quoteunquoteapps.com/courierprime/downloads/courier-prime.zip

node fonts-converter.js -r source\Courier-Prime\Courier-Prime.ttf -b source\Courier-Prime\Courier-Prime-Bold.ttf -i source\Courier-Prime\Courier-Prime-Italic.ttf -x source\Courier-Prime\Courier-Prime-Bold-Italic.ttf -o courier-prime.js
node ../../fonts-converter.js -r source/Courier-Prime/Courier-Prime.ttf -b source/Courier-Prime/Courier-Prime-Bold.ttf -i source/Courier-Prime/Courier-Prime-Italic.ttf -x source/Courier-Prime/Courier-Prime-Bold-Italic.ttf -o courier-prime.js

## courier-prime-cyrillic
Courier Prime Cyrillic: http://dimkanovikov.pro/courierprime/courierprime.zip

node fonts-converter.js -r source\Courier-Prime-Cyrillic\Courier-Prime.ttf -b source\Courier-Prime-Cyrillic\Courier-Prime-Bold.ttf -i source\Courier-Prime-Cyrillic\Courier-Prime-Italic.ttf -x source\Courier-Prime-Cyrillic\Courier-Prime-Bold-Italic.ttf -o courier-prime-cyrillic.js

# noto-jp.js

Noto Sans CJK JP: https://www.google.com/get/noto/#sans-jpan

node fonts-converter.js -r source\NotoSansCJKjp\NotoSansMonoCJKjp-Regular.otf -b source\NotoSansMonoCJKjp\NotoSansCJKjp-Bold.otf -o noto-jp.js

# noto-sc.js
node ../../fonts-converter.js -r source/Courier-Prime-Cyrillic/Courier-Prime.ttf -b source/Courier-Prime-Cyrillic/Courier-Prime-Bold.ttf -i source/Courier-Prime-Cyrillic/Courier-Prime-Italic.ttf -x source/Courier-Prime-Cyrillic/Courier-Prime-Bold-Italic.ttf -o courier-prime-cyrillic.js

Noto Sans CJK SC: https://www.google.com/get/noto/#sans-hans
# kosugi

node fonts-converter.js -r source\NotoSansCJKsc\NotoSansMonoCJKsc-Regular.otf -b source\NotoSansCJKsc\NotoSansMonoCJKsc-Bold.otf -o noto-sc.js
node ../../tools/fonts-converter.js -r source/Kosugi/Kosugi-Regular.otf -o kosugi.js
27 changes: 0 additions & 27 deletions js/fonts/noto-jp.js

This file was deleted.

27 changes: 0 additions & 27 deletions js/fonts/noto-sc.js

This file was deleted.

Binary file added js/fonts/source/Kosugi/Kosugi-Regular.ttf
Binary file not shown.
Loading

0 comments on commit 9f766a9

Please sign in to comment.