Skip to content

Commit

Permalink
feat: add dts, support esm and ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Aug 11, 2023
1 parent a52bb41 commit 3324321
Show file tree
Hide file tree
Showing 15 changed files with 479 additions and 42 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.DS_Store
npm-debug.log
/node_modules
node_modules
fonts/dest*
/coverage
.nyc_output
package-lock.json
test/ts/example.js
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ $ npm install --save fontmin
## Usage

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.src('fonts/*.ttf')
.dest('build/fonts');

Expand All @@ -50,10 +50,10 @@ fontmin.run(function (err, files) {
You can use [gulp-rename](https://github.com/hparra/gulp-rename) to rename your files:

```js
var Fontmin = require('fontmin');
var rename = require('gulp-rename');
import Fontmin from 'fontmin';
const rename = require('gulp-rename');

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.src('fonts/big.ttf')
.use(rename('small.ttf'));
```
Expand Down Expand Up @@ -114,10 +114,10 @@ The following plugins are bundled with fontmin:
Compress ttf by glyph.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
.use(Fontmin.glyph({
const fontmin = new Fontmin()
.use(Fontmin.glyph({
text: '天地玄黄 宇宙洪荒',
hinting: false // keep ttf hint info (fpgm, prep, cvt). default = true
}));
Expand All @@ -128,9 +128,9 @@ var fontmin = new Fontmin()
Convert ttf to eot.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.ttf2eot());
```

Expand All @@ -139,9 +139,9 @@ var fontmin = new Fontmin()
Convert ttf to woff.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.ttf2woff({
deflate: true // deflate woff. default = false
}));
Expand All @@ -152,9 +152,9 @@ var fontmin = new Fontmin()
Convert ttf to woff2.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.ttf2woff2());
```

Expand All @@ -165,10 +165,10 @@ Convert ttf to svg.
you can use [imagemin-svgo](https://github.com/imagemin/imagemin-svgo) to compress svg:

```js
var Fontmin = require('fontmin');
var svgo = require('imagemin-svgo');
import Fontmin from 'fontmin';
const svgo = require('imagemin-svgo');

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.ttf2svg())
.use(svgo());

Expand All @@ -179,12 +179,12 @@ var fontmin = new Fontmin()
Generate css from ttf, often used to make iconfont.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.css({
fontPath: './', // location of font file
base64: true, // inject base64 data:application/x-font-ttf; (gzip font with css).
fontPath: './', // location of font file
base64: true, // inject base64 data:application/x-font-ttf; (gzip font with css).
// default = false
glyph: true, // generate class for each glyph. default = false
iconPrefix: 'my-icon', // class prefix, only work when glyph is `true`. default to "icon"
Expand All @@ -196,9 +196,9 @@ var fontmin = new Fontmin()

Alternatively, a transform function can be passed as `fontFamily` option.
```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.use(Fontmin.css({
// ...
fontFamily: function(fontInfo, ttf) {
Expand All @@ -213,9 +213,9 @@ var fontmin = new Fontmin()
Convert font format svg to ttf.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.src('font.svg')
.use(Fontmin.svg2ttf());
```
Expand All @@ -227,9 +227,9 @@ Concat svg files to a ttf, just like css sprite.
awesome work with [css](#css) plugin:

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.src('svgs/*.svg')
.use(Fontmin.svgs2ttf('font.ttf', {fontName: 'iconfont'}))
.use(Fontmin.css({
Expand All @@ -242,9 +242,9 @@ var fontmin = new Fontmin()
Convert otf to ttf.

```js
var Fontmin = require('fontmin');
import Fontmin from 'fontmin';

var fontmin = new Fontmin()
const fontmin = new Fontmin()
.src('fonts/*.otf')
.use(Fontmin.otf2ttf());
```
Expand Down
178 changes: 178 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* @file index.d.ts
* @author kekee000(kekee000@gmail.com)
*/
import through from 'through2';
import {EventEmitter} from 'events';
import {Transform} from 'stream';
import {TTF} from 'fonteditor-core'

type PluginDesc = (...args: any[]) => Transform;
type InternalPlugin<T = any> = (opts?: T) => PluginDesc;

interface GlyphPluginOptions {
/**
* use this text to generate compressed font
*/
text: string;
/**
* add basic chars to glyph
* @example "!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}"
*/
basicText?: boolean;
/**
* keep gylph hinting, defaul true
*/
hinting?: boolean;
/**
* use other plugin
*/
use?: PluginDesc;
}

interface FontInfo {
fontFile: string;
fontPath: string;
base64: string;
glyph: boolean;
iconPrefix: string;
local: boolean;
}

interface CssPluginOptions {
/**
*generate class for each glyph. default = false
*/
glyph?: boolean;
/**
* inject base64 data:application/x-font-ttf; (gzip font with css). default = false
*/
base64?: boolean;
/**
* class prefix, only work when glyph is `true`. default to "icon"
*/
iconPrefix?: string;
/**
* rewrite fontFamily as filename force. default = false
*/
asFileName?: boolean;
/**
* location of font file
*/
fontPath?: string;
/**
* custom fontFamily, default to filename or get from analysed ttf file
*/
fontFamily?: string | ((fontInfo: FontInfo, ttf: TTF.TTFObject) => string);
/**
* boolean to add local font. default = false
*/
local?: boolean;
}

interface Svgs2ttfPluginOptions {
/**
* set font name
*/
fontName?: string;
}

declare namespace Fontmin {
/*
* get subset font using giving text
*/
const glyph: InternalPlugin<GlyphPluginOptions>;

/*
* convert ttf to eot
*/
const ttf2eot: InternalPlugin;

/*
* convert ttf to woff
*/
const ttf2woff: InternalPlugin<{
/**
* use deflate to transform woff, default false
*/
deflate: boolean;
}>;

/*
* convert ttf to woff2
*/
const ttf2woff2: InternalPlugin;

/*
* convert ttf to svg text
*/
const ttf2svg: InternalPlugin;

/*
* Generate css from ttf, often used to make iconfont.
*/
const css: InternalPlugin<CssPluginOptions>;

/**
* convert font format svg to ttf
*/
const svg2ttf: InternalPlugin<{hinting?: boolean}>;

/**
* concat svg files to a ttf, just like css sprite
*/
const svgs2ttf: (file: string, opts?: Svgs2ttfPluginOptions) => through.Through2Constructor;

/**
* convert otf to ttf
*/
const otf2ttf: InternalPlugin;
}

type PluginNames = keyof typeof Fontmin;

declare class Fontmin extends EventEmitter {
static plugins: PluginNames[];

/**
* Get or set the source files
* @param file files to be optimized
*/
src(src: ArrayLike<number> | Buffer | string): this;

/**
* Get or set the destination folder
* @param dir folder to written
*/
dest(dest: string): this;

/**
* Add a plugin to the middleware stack
* @param plugin plugin function
*/
use(plugin: PluginDesc): this;

/**
* run Optimize files
* @param callback plugin function
*/
run(callback: (err: Error, files: Buffer[]) => void): Transform;

/**
* run Optimize files with return Promise
*/
runAsync(): Promise<Buffer[]>;
}

export default Fontmin;

export const mime: {
'.*': 'application/octet-stream',
'ttf': 'application/font-sfnt',
'otf': 'application/font-sfnt',
'woff': 'application/font-woff',
'woff2': 'application/font-woff2',
'eot': 'application/octet-stream',
'svg': 'image/svg+xml',
'svgz': 'image/svg+xml'
};
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ Fontmin.prototype.run = function (cb) {
return stream;
};

/**
* run Optimize files with return Promise
*
* @return {Array<Buffer>} file result
* @api public
*/
Fontmin.prototype.runAsync = function () {
return new Promise((resolve, reject) => {
var stream = this.createStream();
stream.on('error', reject);

stream.pipe(concat(resolve));
});
};


/**
* Create stream
*
Expand Down Expand Up @@ -165,5 +181,6 @@ Fontmin.plugins.forEach(function (plugin) {
module.exports = Fontmin;

// exports util, mime
module.exports.default = Fontmin;
module.exports.util = exports.util = require('./lib/util');
module.exports.mime = exports.mime = require('./lib/mime-types');
Loading

0 comments on commit 3324321

Please sign in to comment.