Glyph object

Glyph() and $Glyph() shortcut

Syntax

new Glyph(meta_obj, font)
// is equivalent to
$Glyph(meta_obj, font)

Parameters

NameR/OTypeDefault ValueDescription
meta_objRequiredobject (string as keys, number or string as values)N/Aobject of the meta information
fontRequiredFont objectN/AThe font the glyph belongs to

Return value

Glyph object

Description

Initialize a Glyph object. Load a object of meta information and the font the glyph belongs.

The shortcut $Glyph(meta_obj, font) is equivalent to new Glyph(meta_obj, font), use it for convenience.

:::note Usually you get the glyph object from font.glyph(character) and do not need to use Glyph(). :::

.meta

Syntax

.meta

Examples

const font = await $Font(getline('test/fonts/unifont-13.0.04.bdf'))
const glyph = font.glyph('a')
// `glyph` here is the Glyph object of "a" in Unifont v13.0.04



console.log(glyph.meta)
// { glyphname: 'U+0061', codepoint: 97, bbw: 8, bbh: 16, bbxoff: 0, bbyoff: -2, swx0: 500, swy0: 0, dwx0: 8, dwy0: 0, swx1: null, swy1: null, dwx1: null, dwy1: null, vvectorx: null, vvectory: null, hexdata: [ '00', '00', '00', '00', '00', '00', '3C', '42', '02', '3E', '42', '42', '46', '3A', '00', '00'] }
console.log(glyph.meta.bbw)
// 8
const font = await $Font(getline('test/fonts/unifont-13.0.04.bdf'))
const glyph = font.glyph('a')
// `glyph` here is the Glyph object of "a" in Unifont v13.0.04
if (!glyph) {
  throw new Error('Can\'t find the glyph')
}
console.log(glyph.meta)
// { glyphname: 'U+0061', codepoint: 97, bbw: 8, bbh: 16, bbxoff: 0, bbyoff: -2, swx0: 500, swy0: 0, dwx0: 8, dwy0: 0, swx1: null, swy1: null, dwx1: null, dwy1: null, vvectorx: null, vvectory: null, hexdata: [ '00', '00', '00', '00', '00', '00', '3C', '42', '02', '3E', '42', '42', '46', '3A', '00', '00'] }
console.log(glyph.meta.bbw)
// 8

Type

object (string as keys, number or string or array as values)

Description

The glyph’s meta data.

Glyph meta information’s names (keys), value types, and their descriptions in the BDF spec:

  • 'glyphname': (string) see STARTCHAR
  • 'codepoint': (number) see ENCODING
  • 'bbw': (number) see BBX
  • 'bbh': (number) see BBX
  • 'bbxoff': (number) see BBX
  • 'bbyoff': (number) see BBX
  • 'swx0': (number) see SWIDTH
  • 'swy0': (number) see SWIDTH
  • 'dwx0': (number) see DWIDTH
  • 'dwy0': (number) see DWIDTH
  • 'swx1': (number) see SWIDTH1
  • 'swy1': (number) see SWIDTH1
  • 'dwx1': (number) see DWIDTH1
  • 'dwy1': (number) see DWIDTH1
  • 'vvectorx': (number) see VVECTOR
  • 'vvectory': (number) see VVECTOR
  • 'hexdata': (array of strings) the glyph’s shape data in the form of array of hexadecimal-encoded string

:::note Same character or glyph’s 'glyphname' (STARTCHAR) information may vary in different fonts, therefore it is unreliable to use it. Use the API method .chr() instead.

Consider using .draw().todata(4) instead of .meta['hexdata']. :::

.font

Syntax

.font

Examples




console.log(glyph.font.headers.fontname)
// the name of the font the glyph belongs to,
// e.g. '-gnu-Unifont-Medium-R-Normal-Sans-16-160-75-75-c-80-iso10646-1'
if (!glyph.font || !glyph.font.headers) {
  throw new Error('Unable to find glyph or font')
}
console.log(glyph.font.headers.fontname)
// the name of the font the glyph belongs to,
// e.g. '-gnu-Unifont-Medium-R-Normal-Sans-16-160-75-75-c-80-iso10646-1'

Type

Font object

Description

It’s a reference to the glyph’s font object.

.cp()

Syntax

.cp()

Examples

console.log(glyph.cp())
// 97
console.log(glyph.cp())
// 97

Parameters

No parameters

Return value

(number) codepoint of the glyph

Description

Get the codepoint of the glyph.

.chr()

Syntax

.chr()

Examples

console.log(glyph.chr())
// 'a'
console.log(glyph.chr())
// 'a'

Parameters

No parameters

Return value

(string) character (one character string) of the glyph

Description

Get the character of the glyph.

.draw()

Syntax

.draw(mode, bb)

Examples

In these examples, we use the “quoteright” (') glyph in the BDF spec’s example figure 3.

const font = await $Font(getline('test/fonts/spec_example_fixed.bdf'))
const quoteright = font.glyph("'")



quoteright.draw()
const font = await $Font(getline('test/fonts/spec_example_fixed.bdf'))
const quoteright = font.glyph("'")
if (!quoteright) {
  throw new Error('Can\'t find quoteright glyph')
}
quoteright.draw()
Click to see the output of quoteright.draw().toString() (with default mode 0)
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........

quoteright.draw(1)
Click to see the output of quoteright.draw(1).toString()
.###
.###
.###
.##.
###.
##..

quoteright.draw(2)
Click to see the output of quoteright.draw(2).toString()
.###....
.###....
.###....
.##.....
###.....
##......

quoteright.draw(-1, [6, 17, 1, 1])
Click to see the output of quoteright.draw(-1, [6, 17, 1, 1]).toString()
..###.
..###.
..###.
..##..
.###..
.##...
......
......
......
......
......
......
......
......
......
......
......

Parameters

NameR/OTypeDefault ValueDescription
modeOptionalnumber0See below
bbOptionaltupleTuple in TypeScript = Fixed-length Array in JavaScript of four numbersempty/undefinedSee below

mode can be:

  • 0 (default): area represented by the bitmap hex data, positioned and resized (cropped) (fbbx × fbby) according to FONTBOUNDINGBOX (the font’s global bounding box)
  • 1: area represented by the bitmap hex data, resized (cropped) according to BBX (bbw × bbh), which is the individual glyph bounding box, without unnecessary blank margin (but still possible to have blank margin sometimes)
  • 2: area represented by the bitmap hex data, original, without removing the right-padded '0's
  • -1: user specified area.
    • bb parameter is useless when mode -1 is not chosen. But if mode -1 is chosen, you MUST specify bb parameter, which is a tupleTuple in TypeScript = Fixed-length Array in JavaScript [bbx, bby, bbxoff, bbyoff] representing your customized font bounding box. Similar to FONTBOUNDINGBOX, bbx and bby represent the size, bbxoff and bbyoff represent the relative position (displacement) of the starting (bottom-left) point from the origin

Return value

Bitmap object

Description

Draw the glyph to a Bitmap object.

.origin()

Syntax

.origin(options)
// options = {mode, fromorigin, xoff, yoff}

Examples

console.log(quoteright.origin()) // [2, 6]
console.log(quoteright.origin({ mode: 1 })) // [-2, -12]
console.log(quoteright.origin({ mode: 2 })) // [-2, -12]
console.log(quoteright.origin({ mode: -1, xoff: 1, yoff: 1 })) // [-1, -1]
console.log(quoteright.origin({ fromorigin: true })) // [-2, -6]
console.log(quoteright.origin({ mode: 1, fromorigin: true })) // [2, 12]
console.log(quoteright.origin()) // [2, 6]
console.log(quoteright.origin({ mode: 1 })) // [-2, -12]
console.log(quoteright.origin({ mode: 2 })) // [-2, -12]
console.log(quoteright.origin({ mode: -1, xoff: 1, yoff: 1 })) // [-1, -1]
console.log(quoteright.origin({ fromorigin: true })) // [-2, -6]
console.log(quoteright.origin({ mode: 1, fromorigin: true })) // [2, 12]

Parameters

NameR/OTypeDefault ValueDescription
(in options) modeOptionalnumber0Same as .draw()’s mode parameter
(in options) fromoriginOptionalbooleanfalsefalse: to the origin
true: from the origin
(in options) xoffOptionalnumbersempty/undefinedSee below
(in options) yoffOptionalnumbersempty/undefinedSee below

Similar to .draw(), xoff and yoff parameters are useless when mode -1 is not chosen, but if mode: -1 in .origin(), you MUST specify xoff and yoff, which are equivalent to bb[2] (“bbxoff”) and bb[3] (“bbyoff”) in the method .draw().

Return value

(tupleTuple in TypeScript = Fixed-length Array in JavaScript of two numbers) The relative position (displacement) represented by [x, y] tupleTuple in TypeScript = Fixed-length Array in JavaScript (where right and top directions are positive)

Description

Get the relative position (displacement) of the origin from the left bottom corner of the bitmap drawn by the method .draw(), or vice versa (i.e. displacement of the left bottom corner of the bitmap from the origin).

.toString()

Syntax

.toString()

Examples

console.log(quoteright.toString())
console.log(quoteright.toString())
Click to see the output of quoteright.toString()
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........

Description

(string) .toString() gets a human-readable (multi-line) string representation of the Glyph object. It’s actually the string converted from .draw() with default parameters.

See also toString() for Bitmap object

.repr()

Syntax

.repr()

Examples

console.log(quoteright.repr())
console.log(quoteright.repr())
Click to see the output of quoteright.repr()
Glyph({
  "glyphname": "quoteright",
  "codepoint": 39,
  "bbw": 4,
  "bbh": 6,
  "bbxoff": 2,
  "bbyoff": 12,
  "swx0": 223,
  "swy0": 0,
  "dwx0": 5,
  "dwy0": 0,
  "swx1": null,
  "swy1": null,
  "dwx1": null,
  "dwy1": null,
  "vvectorx": null,
  "vvectory": null,
  "hexdata": [
    "70",
    "70",
    "70",
    "60",
    "E0",
    "C0"
  ]
}, Font(<Helvetica-BoldOblique>)

Description

(string) It gets a programmer-readable string representation of the Glyph object.