Coder Social home page Coder Social logo

font's Introduction

@asandmann/font

This wasm wrapping of fontdue provides tools for handling font rendering, layout creation, and text rasterization in bitmap format.

Installation

npm install @asandmann/font

Example

import { Font, Layout } from '@asandmann/font';

// Create a font instance
const fontBuffer = new Uint8Array(); // Load your font buffer
const font = new Font(1.0, fontBuffer);

// Check if the font has a glyph for 'A'
if (font.has('A')) {
    // Get metrics for 'A'
    const metrics = font.metrics('A');

    // Rasterize 'A'
    const rasterizedChar = font.rasterize('A');
}

// Create a layout instance
const layout = new Layout();

// Set layout options
layout.reset({
    max_width: 500,
    wrap_style: 'word',
    vertical_align: 'middle',
    horizontal_align: 'center'
});

// Append text to the layout
layout.append(font, 'Hello, World!', { scale: 1, r: 255, g: 255, b: 255 });

// Render the layout
const rasterizedLayout = layout.rasterize(0, 0, 0);

// Clean up
font.free();
layout.clear();

This example demonstrates how to create a font instance, check for glyph availability, get character metrics, rasterize a character, create a layout, set layout options, append text, and render the layout to a bitmap.

Usage

Font Class

Constructor

const font = new Font(scale: number, buffer: Uint8Array);

Creates a new font instance.

Methods

  • free(): void
    Frees the font instance.

  • has(char: string): boolean
    Checks if the font has a glyph for a specific character.

  • metrics(char: string, scale?: number): metrics
    Gets the metrics of a rendered character.

  • rasterize(char: string, scale?: number): font_rasterized
    Renders a character to a bitmap.

Layout Class

Constructor

const layout = new Layout();

Creates a new layout instance.

Methods

  • clear(): void
    Clears all glyphs from the layout.

  • lines(): number
    Returns the number of lines in the layout.

  • reset(options?: layout_options): void
    Resets the layout's options.

  • append(font: Font, text: string, init: append_options): void
    Appends text to the layout.

  • rasterize(r: number, g: number, b: number): layout_rasterized
    Renders the layout to a bitmap.

font's People

Contributors

acsandmann avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.