npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openvg_ttf

v0.1.0

Published

Pure JS module to parse trutype for OpenVG rendering

Downloads

13

Readme

openvg_ttf

Pure JS module to parse truetype for Open VG rendering. Some parts of parsing code are borrowed from Sean Barrett under the license of MIT.
You need napi_openvg, napi_sdl2 to use this.

Supported OS

Currently tested only in macOS but will support others soon.

Installation

npm install napi_sdl2
npm install napi_openvg
npm install openvg_ttf

Usage

Load ttf font file.

let font = new Font();
font.loadFile('/Library/Fonts/AppleGothic.ttf');

Make text string to Open VG path.

function renderToPath(text, font, size) {
  let textPath = VG.vgCreatePath(VG.VG_PATH_FORMAT_STANDARD, VG.VG_PATH_DATATYPE_F, 1.0, 0.0, 0, 0, VG.VG_PATH_CAPABILITY_ALL);

  let dpi = 220.0;
  let ppem = size * dpi / 72.0 ;
  //ppem = size;
  let scale = ppem / font.unitsPerEm;
  let adv = 0;

  VG.vgSeti(VG.VG_MATRIX_MODE, VG.VG_MATRIX_PATH_USER_TO_SURFACE);
  VG.vgLoadIdentity();
  for (let i = 0; i < text.length; i++) {
    let ch = text.charCodeAt(i);
    let glyph = font.glyphIndex(ch);
    if (glyph < 0 || glyph === undefined) {
      continue; 
    }
    let glyphs = font.glyphs(ch, glyph);

    VG.vgTranslate(adv, (font.ascender * scale));
    VG.vgScale(scale, -scale);
    
    VG.vgTransformPath(textPath, glyphs);
   
    VG.vgSeti(VG.VG_MATRIX_MODE, VG.VG_MATRIX_PATH_USER_TO_SURFACE);
    VG.vgLoadIdentity();

    let cur = (font.glyphAdvances(glyph) * scale) | 0;
    adv += cur;  
  }
  return textPath;
}

Acutal draw string function.

function draw_string(fillColor, strokeColor, text, font, size, x, y) {
    let textPath = renderToPath(text, font, size);

    let fillPaint;
    if(fillColor != null) {
      fillPaint = VG.vgCreatePaint();
      VG.vgSetParameteri(fillPaint, VG.VG_PAINT_TYPE, VG.VG_PAINT_TYPE_COLOR);
      VG.vgSetParameterfv(fillPaint, VG.VG_PAINT_COLOR, 4, fillColor);
      VG.vgSetPaint(fillPaint, VG.VG_FILL_PATH);      
    }
    
    let strokePaint = VG.vgCreatePaint();
    VG.vgSetParameteri(strokePaint, VG.VG_PAINT_TYPE, VG.VG_PAINT_TYPE_COLOR);
    VG.vgSetParameterfv(strokePaint, VG.VG_PAINT_COLOR, 4, strokeColor);
    VG.vgSetPaint(strokePaint, VG.VG_STROKE_PATH);

    VG.vgSeti(VG.VG_MATRIX_MODE, VG.VG_MATRIX_PATH_USER_TO_SURFACE);
    VG.vgLoadIdentity();

    let currentMatrix = VG.vgGetMatrix();
    VG.vgLoadMatrix(currentMatrix);
    let mat = [
        1.0,   0.0, 0.0,
        0.0,  1.0, 0.0,
        x,     y, 1.0
        ];
    
    VG.vgMultMatrix(mat);
    VG.vgSeti(VG.VG_RENDERING_QUALITY, VG.VG_RENDERING_QUALITY_BETTER);
    VG.vgSetf(VG.VG_STROKE_LINE_WIDTH, 0.6);

    let flag = VG.VG_STROKE_PATH;
    if(fillColor != null) {
      flag |= VG.VG_FILL_PATH;
    }

    VG.vgDrawPath(textPath, flag);

    if(fillColor != null) {
      VG.vgDestroyPaint(fillPaint);
    }
    VG.vgDestroyPaint(strokePaint);

    VG.vgLoadMatrix(currentMatrix);
    VG.vgDestroyPath(textPath);
}