Font object
Font()
#
#
Syntax#
Return valueFont object
#
DescriptionInitialize a Font object.
The following shortcut, $Font(filelines)
, is equivalent to new Font().load_filelines(filelines)
, use it for convenience.
$Font()
shortcut#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
filelines | Required | AsyncIterableIterator<string> | N/A | Asynchronous iterable iterator representing each line in string text from the font file. Go to the page of my Fetch Line JavaScript packages, choose readlineiter for node file system or fetchline for browsers. See the above example |
#
Return valueFont object
#
DescriptionInitialize a Font object and load the BDF font file (file line async iterator).
$Font(filelines)
is a shortcut for new Font().load_filelines(filelines)
, so you don't need to write new
and .load_filelines
.headers
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
Typeobject (string as keys, number or string as values)
#
DescriptionThis attribute of a Font object represents the header information which is typically all the information before the STARTPROPERTIES
line in the font file.
Font header's names (keys / property names), value types, and their descriptions in the BDF spec:
'bdfversion'
: (number) seeSTARTFONT
'fontname'
: (string) seeFONT
'pointsize'
: (number) seeSIZE
'xres'
: (number) seeSIZE
'yres'
: (number) seeSIZE
'fbbx'
: (number) seeFONTBOUNDINGBOX
'fbby'
: (number) seeFONTBOUNDINGBOX
'fbbxoff'
: (number) seeFONTBOUNDINGBOX
'fbbyoff'
: (number) seeFONTBOUNDINGBOX
'swx0'
: (number) see Metrics keywords, see alsoSWIDTH
at glyph-level'swy0'
: (number) see Metrics keywords, see alsoSWIDTH
at glyph-level'dwx0'
: (number) see Metrics keywords, see alsoDWIDTH
at glyph-level'dwy0'
: (number) see Metrics keywords, see alsoDWIDTH
at glyph-level'swx1'
: (number) see Metrics keywords, see alsoSWIDTH1
at glyph-level'swy1'
: (number) see Metrics keywords, see alsoSWIDTH1
at glyph-level'dwx1'
: (number) see Metrics keywords, see alsoDWIDTH1
at glyph-level'dwy1'
: (number) see Metrics keywords, see alsoDWIDTH1
at glyph-level'vvectorx'
: (number) see Metrics keywords, see alsoVVECTOR
at glyph-level'vvectory'
: (number) see Metrics keywords, see alsoVVECTOR
at glyph-level'metricsset'
: (number) seeMETRICSSET
'contentversion'
: (number) seeCONTENTVERSION
'comment'
: (array of strings) seeCOMMENT
note
Headers are to be distinguished from properties (props) which are inside the block between STARTPROPERTIES
and ENDPROPERTIES
lines in the font file.
Header names are defined in the spec, property names are not.
.props
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
Typeobject (string as keys, number or string as values)
#
DescriptionThis attribute of a Font object represents the property infomation which is inside the block between STARTPROPERTIES
and ENDPROPERTIES
lines.
note
Properties (props) are to be distinguished from headers which are typically all the information before the STARTPROPERTIES
line in the font file.
.glyphs
#
#
Syntax#
Typeobject (number as keys, array as values)
#
DescriptionThis attribute of a Font object represents the raw glyph data in the font.
note
As raw data with values without their keys, you usually should not use it directly. Use other API methods instead, such as .glyph() and .iterglyphs().
.load_filelines()
#
#
Syntax#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
filelines | Required | AsyncIterableIterator<string> | N/A | Asynchronous iterable iterator representing each line in string text from the font file. Go to the page of my Fetch Line JavaScript packages, choose readlineiter for node file system or fetchline for browsers. See the above example |
#
Return valuePromise that will be resolved with the Font object itself with font loaded
#
DescriptionLoad the BDF font file (file line async iterator).
$Font(filelines)
shortcut is equivalent to new Font().load_filelines(filelines)
.
.length
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersNo parameters
#
Return value(number) Actual glyph count in the font
#
DescriptionThe getter method always returns how many glyphs actually exist in the font. This could be different with the glyph count number next to the 'CHARS' keyword, if so, there would be a warning when the font is loaded.
.iterglyphs()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
order | Optional | number | 1 | -1 : reverse order in the BDF font file0 : order in the BDF font file1 : ascending codepoint order2 : descending codepoint order |
r | Optional | See below | None | Codepoint range, see below |
The codepoint range parameter r
accepts:
None
(default): all the glyphs in the font- number. Examples:
128
(Basic Latin / ASCII)0x100
(Basic Latin and Latin-1 Supplement / cp1250 / cp1251 / cp1252)
- tuple? of two numbers. Examples:
[0, 127]
(same as128
)[0, 0xff]
(same as0x100
)[48, 57]
(all numbers 0-9)[65, 90]
(all uppercase basic latin letters A-Z)[97, 122]
(all lowercase basic latin letters a-z)[1328, 0x1032F]
- array of tuples? of two numbers. Example:
[[65, 90], [97, 122]]
(all basic latin letters A-Za-z)[[0x2E80, 0x9FFF], [0xA960, 0xA97F], [0xAC00, 0xD7FF], [0xF900, 0xFAFF], [0xFE30, 0xFE4F], [0xFF00, 0xFFEF], [0x20000, 0x3134F]]
(this is roughly all CJK characters in the Unicode)
See also Wikipedia article "Unicode block"
#
Return value(iterator of Glyph objects/None
) An iterator of glyphs as Glyph objects. Missing glyphs are replaced by None
#
DescriptionThis method returns an iterator of all the glyphs (as Glyph objects) in the font (default) or in the specified codepoint range in the font, sorted by the specified order (or by the ascending codepoint order by default).
.itercps()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersSee .iterglyphs()
's "Parameters" section
#
Return value(iterator of numbers) An iterator of the codepoints of glyphs
#
DescriptionThis method is almost identical to .iterglyphs()
, except it returns an iterator of glyph codepoints instead of an iterator of Glyph objects.
.glyph()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
character | Required | string | N/A | Character (a one-character string) of the glyph you need |
#
Return value- Glyph object
None
if the glyph does not exist in the font
#
DescriptionGet a glyph (as Glyph object) by its character.
.glyphbycp()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
codepoint | Required | number | N/A | Codepoint of the glyph you need |
#
Return value- Glyph object
None
if the glyph does not exist in the font
#
DescriptionGet a glyph (as Glyph object) by its codepoint.
.lacksglyphs()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
str | Required | string | N/A | string (one or more characters) |
#
Return value- (array of strings) array of missing glyph(s)' characters
None
if all the glyphs in your string exist in the font
#
DescriptionCheck if there is any missing glyph and gets these glyphs' character.
.draw()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
str | Required | string | N/A | The words, the setences, the paragraphs you want to draw |
options | Optional | number | 512 | Maximum pixels per line (row) |
(in options ) linelimit | Optional | number | 512 | Maximum pixels per line (row) |
(in options ) mode | Optional | number | 1 | 0 : uses ffb, glyphs will be fixed-width (monospace) and -height1 : uses dwidth horizontally, dwidth1 vertically |
(in options ) direction | Optional | string | 'lrtb' | Writing direction. See below |
(in options ) usecurrentglyphspacing | Optional | boolean | false | Useful when direction: 'rl' . For example, font.draw('açš„', {direction: 'rl'}) will misplace the wider "çš„" on the narrower "U" because normally we use the previous glyph's spacing (dwidth). You can use font.draw('açš„', {direction: 'rl', usecurrentglyphspacing: true}) to use the current one and solve the problem |
(in options ) missing | Optional | object or Glyph object | An empty glyph with glyphname 'empty' , codepoint 8203 , hexdata [] and metrics all 0 | The missing glyphs will be replaced by this one. It can be a object of glyph meta information, or a Glyph object |
direction
:
'lrtb'
or'lr'
: left to right, lines from top to bottom (most common direction)'lrbt'
: left to right, lines from bottom to top'rltb'
or'rl'
: right to left, lines from top to bottom (Arabic, Hebrew, Persian, Urdu)'rlbt'
: right to left, lines from bottom to top'tbrl'
or'tb'
: top to bottom, lines from right to left (Chinese traditionally)'tblr'
: top to bottom, lines from left to right'btrl'
or'bt'
: bottom to top, lines from right to left'btlr'
: bottom to top, lines from left to right
#
Return valueBitmap object
#
DescriptionDraw (render) the glyphs of the specified words / setences / paragraphs (as a string), to a Bitmap object.
note
See the missing
parameter if you need to handle potentially missing glyphs.
.drawcps()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
#
ParametersName | R/O | Type | Default Value | Description |
---|---|---|---|---|
cps | Required | iterable of numbers | N/A | iterable (array, generator or any other iterable) of codepoints |
For the rest of the parameters (linelimit
, mode
, direction
and usecurrentglyphspacing
in options
), see .draw()
's "Parameters" section
#
Return valueBitmap object
#
DescriptionDraw the glyphs of the specified codepoints, to a Bitmap object.
This method is almost identical to .draw()
, except in the first parameter, it uses iterable of codepoints instead of string
.drawall()
#
#
Syntax#
Examples- JavaScript (CJS)
- TypeScript (strict)
note
You can copy and paste <BDF size={1} func={(font) => font.drawall()}/>
and <BDF size={1} func={(font) => font.drawall({linelimit: 700})}/>
into the Live Demo & Code Editor to see the result for unifont-reduced.bdf.
#
ParametersFor parameters order
and r
in options
, see .iterglyphs()
's "Parameters" section
For the rest of the parameters (linelimit
, mode
, direction
and usecurrentglyphspacing
in options
), see .draw()
's "Parameters" section
Those parameters have the same names, meanings, optional-ness (they are all optional), types and default values as the ones in .iterglyphs()
and .draw()
, except one thing: mode
is 0
by default (mode: 0
: the glyphs will have the same width and height)
#
Return valueBitmap object
#
DescriptionDraw all the glyphs in the font (default) or in the specified codepoint range in the font, sorted by the specified order (or by the ascending codepoint order by default), to a Bitmap object.
This method is like a combined version of .iterglyphs()
/.itercps()
and .draw()
/.drawcps()
.
note
Read the section for Bitmap's .tobytes() to know how to output your bitmap to BMP / PNG / JPEG files.