This is a JavaScript translation/implementaion of the bitmap fonts from SerenityOS.
Demo: https://macdue.github.io/LibFont.js/
// TODO: Make a nicer README.md :^)
Example usage:
const canvas = document.getElementById("some-canvas");
const ctx = canvas.getContext('2d');
// Set the fillStyle to any color you like!
ctx.fillStyle = 'black';
LibFont.BitmapFont.loadFont('./KaticaBold10.font').then(font => {
font.drawTextInto(ctx, 10, 10, "Well, hello friends!");
})LibFont.BitmapFont.loadFont(url) -> Promise<BitmapFont>- Load a
.fontfile from a URL
- Load a
LibFont.BitmapFont.loadFontFromArrayBuffer(buffer) -> BitmapFont- Load a
.fontfile from an array buffer
- Load a
LibFont.BitmapFont.loadFontFromUint8Array(array) -> BitmapFont- Load a
.fontfrom a uint8 array
- Load a
font.drawTextInto(canvasCtx, drawX, drawY, text)- Draw a some text into a canvas at
(drawX, drawY)
- Draw a some text into a canvas at
font.glyph(codepoint) -> Glpyh- Lookup the glyph for
codepoint
- Lookup the glyph for
font.glyphIndex(codepoint) -> integer- Look up the glyph index for
codepoint
- Look up the glyph index for
font.containsGlyph(codepoint) -> bool- Check if a font contains a glyph for
codepoint
- Check if a font contains a glyph for
font.forEachGlyph(text, callback)- Iterate over all glyphs in some text
font.forEachFontGlyph(callback)- Iterate over all the non-empty glyphs in the font
font.textWidth(text) -> integer- Get the width of some text (in pixels)
font.getTextAsDataURL(text, fillStyle='black') -> string- Get some text as a data URL image (that can be used as the
srcof animgtag)
- Get some text as a data URL image (that can be used as the
font.getTextAsHTML(text, fillStyle='black') -> HTMLElement- Get some text as a HTML element, when inserted onto a page this is copyable and supports word wrapping
font.accurateGlyphCount()- Calculate the number of actual glyphs in the font (this is not the same as
font.glyphCount)
- Calculate the number of actual glyphs in the font (this is not the same as
font.namefont.familyfont.glyphWidthfont.glyphHeightfont.minGlyphWidthfont.maxGlyphWidthfont.glyphSpacingfont.baselinefont.meanLinefont.presentationSizefont.weightfont.slopefont.fixedWidthfont.glyphCount
glyph.toDataURL(fillStyle='black') -> string- Converts the glyph into a data URL image (that can be used as the
srcof animgtag)
- Converts the glyph into a data URL image (that can be used as the
glyph.bitmap(is aGlyphBitmap)glyph.leftBearingglyph.advanceglyph.ascent
LibFont.GlyphBitmap.bytesPerRow()
bitmap.paintInto(canvasCtx, x, y)- Paint a glyph bitmap into a canvas at
(x, y)
- Paint a glyph bitmap into a canvas at
bitmap.bitAt(x,y)- Look up the pixel (bit) of a glyph at
(x, y)
- Look up the pixel (bit) of a glyph at
bitmap.row(index)bitmap.rowSlice(y)
bitmap.widthbitmap.height