Skip to content

Commit

Permalink
Merge pull request #1 from UXPin/master-synced
Browse files Browse the repository at this point in the history
Sync master with original forked branch
  • Loading branch information
robhil authored Nov 16, 2023
2 parents eb8b5d3 + c619f7a commit e74c188
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 237 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
build/
node_modules/
node_modules/
.vscode/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules/
appveyor.yml
travis-linux.sh
travis-osx.sh
.vscode/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# setup build matrix
env:
- NODE_VERSION=6
- NODE_VERSION=8
- NODE_VERSION=10
- NODE_VERSION=12
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/devongovett/font-manager.svg)](https://travis-ci.org/devongovett/font-manager)
# fontmanager-redux

![Travis (.org)](https://img.shields.io/travis/eugeny/fontmanager-redux?label=Mac%2FLINUX&style=for-the-badge) ![AppVeyor](https://img.shields.io/appveyor/ci/eugeny/fontmanager-redux?label=windows&style=for-the-badge)

# font-manager

A C++ module for Node.js providing access to the system font catalog.

Expand All @@ -18,23 +19,23 @@ A C++ module for Node.js providing access to the system font catalog.

## Installation

Installation of the `font-manager` module is via npm:
Installation of the `fontmanager-redux` module is via npm:

npm install font-manager
npm install fontmanager-redux

On Linux, you also may need to install the `libfontconfig-dev` package, for example:

sudo apt-get install libfontconfig-dev

## API

You load the `font-manager` module using `require` as with all Node modules:
You load the `fontmanager-redux` module using `require` as with all Node modules:

```javascript
var fontManager = require('font-manager');
import * as fontManager from 'fontmanager-redux';
```

All of the methods exported by `font-manager` have both synchronous and asynchronous versions available.
All of the methods exported by `fontmanager-redux` have both synchronous and asynchronous versions available.
You should generally prefer the asynchronous version as it will allow your program to continue doing other
processing while a request for fonts is processing in the background, which may be expensive depending on
the platform APIs that are available.
Expand All @@ -53,7 +54,7 @@ Returns an array of all [font descriptors](#font-descriptor) available on the sy
fontManager.getAvailableFonts(function(fonts) { ... });

// synchronous API
var fonts = fontManager.getAvailableFontsSync();
const fonts = fontManager.getAvailableFontsSync();

// output
[ { path: '/Library/Fonts/Arial.ttf',
Expand All @@ -69,16 +70,16 @@ var fonts = fontManager.getAvailableFontsSync();

### findFonts(fontDescriptor)

Returns an array of [font descriptors](#font-descriptor) matching a query
[font descriptor](#font-descriptor).
Returns an array of [font descriptors](#font-descriptor) matching a query
[font descriptor](#font-descriptor).
The returned array may be empty if no fonts match the font descriptor.

```javascript
// asynchronous API
fontManager.findFonts({ family: 'Arial' }, function(fonts) { ... });

// synchronous API
var fonts = fontManager.findFontsSync({ family: 'Arial' });
const fonts = fontManager.findFontsSync({ family: 'Arial' });

// output
[ { path: '/Library/Fonts/Arial.ttf',
Expand All @@ -103,7 +104,7 @@ var fonts = fontManager.findFontsSync({ family: 'Arial' });

Returns a single [font descriptors](#font-descriptor) matching a query
[font descriptors](#font-descriptor) as well as possible. This method
always returns a result (never `null`), so sometimes the output will not
always returns a result (never `null`), so sometimes the output will not
exactly match the input font descriptor if not all input parameters
could be met.

Expand All @@ -112,7 +113,7 @@ could be met.
fontManager.findFont({ family: 'Arial', weight: 700 }, function(font) { ... });

// synchronous API
var font = fontManager.findFontSync({ family: 'Arial', weight: 700 });
const font = fontManager.findFontSync({ family: 'Arial', weight: 700 });

// output
{ path: '/Library/Fonts/Arial Bold.ttf',
Expand All @@ -139,7 +140,7 @@ in `text`, it is not replaced and the font descriptor for the original font is r
fontManager.substituteFont('TimesNewRomanPSMT', '汉字', function(font) { ... });

// synchronous API
var font = fontManager.substituteFontSync('TimesNewRomanPSMT', '汉字');
const font = fontManager.substituteFontSync('TimesNewRomanPSMT', '汉字');

// output
{ path: '/Library/Fonts/Songti.ttc',
Expand All @@ -156,7 +157,7 @@ var font = fontManager.substituteFontSync('TimesNewRomanPSMT', '汉字');

Font descriptors are normal JavaScript objects that describe characteristics of
a font. They are passed to the `findFonts` and `findFont` methods and returned by
all of the methods. Any combination of the fields documented below can be used to
all of the methods. Any combination of the fields documented below can be used to
find fonts, but all methods return full font descriptors.

Name | Type | Description
Expand Down
7 changes: 1 addition & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
os: Windows Server 2012
environment:
VisualStudioVersion: 11.0
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"
- nodejs_version: "12"

install:
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
Expand All @@ -15,6 +13,3 @@ test_script:
- npm --version
- ps: "npm test # PowerShell" # Pass comment to PS for easier debugging
- cmd: npm test

build: off
version: "{build}"
16 changes: 15 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@
"target_name": "fontmanager",
"sources": [ "src/FontManager.cc" ],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
"<!@(node -p \"require('node-addon-api').include\")"
],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
"conditions": [
['OS=="mac"', {
"sources": ["src/FontManagerMac.mm"],
"link_settings": {
"libraries": ["CoreText.framework", "Foundation.framework"]
},
'cflags+': ['-fvisibility=hidden'],
'xcode_settings': {
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
}
}],
['OS=="win"', {
Expand Down
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module 'font-manager' {
declare module 'fontmanager-redux' {
export interface FontDescriptor {
readonly path: string;
readonly style: string;
Expand All @@ -23,7 +23,7 @@ declare module 'font-manager' {

/**
* Fetches fonts in the system
*
*
* @example
* getAvailableFontsSync();
* @returns All fonts descriptors available
Expand All @@ -32,7 +32,7 @@ declare module 'font-manager' {

/**
* Returns trough a callback all fonts descriptors available on the system
*
*
* @param callback Contains the font data
* @example
* getAvailableFonts((fonts) => { ... });
Expand All @@ -52,7 +52,7 @@ declare module 'font-manager' {

/**
* Queries all the fonts in the system matching the given parameters
*
*
* @param fontDescriptor Query parameters
* @param callback Contains the font data
* @example
Expand All @@ -65,7 +65,7 @@ declare module 'font-manager' {
* Find only one font matching the given query. This function always returns
* a result (never null), so sometimes the output will not exactly match the
* input font descriptor if not all input parameters could be met
*
*
* @param fontDescriptor Query parameters
* @example
* findFontSync({ family: 'Arial', weight: 700 });
Expand Down Expand Up @@ -95,7 +95,7 @@ declare module 'font-manager' {
* characteristics (bold, italic, etc) are used to find a suitable
* replacement. If the font already contains the characters in text, it is
* not replaced and the font descriptor for the original font is returned
*
*
* @param postscriptName Name of the font to be replaced
* @param text Characters for matching
* @returns Only one font description matching the function description
Expand Down
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "fontmanager-redux",
"version": "0.3.2",
"version": "1.0.0",
"description": "Provides access to the system font catalog",
"main": "build/Release/fontmanager",
"types": "index.d.ts",
"dependencies": {},
"dependencies": {
"node-addon-api": "^3.0.2"
},
"devDependencies": {
"mocha": "*",
"nan": "^2.13.2"
"mocha": "*"
},
"directories": {
"test": "test"
Expand Down
61 changes: 24 additions & 37 deletions src/FontDescriptor.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#ifndef FONT_DESCRIPTOR_H
#define FONT_DESCRIPTOR_H
#include <node.h>
#include <v8.h>
#include <nan.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace v8;
#include <napi.h>

enum FontWeight {
FontWeightUndefined = 0,
Expand Down Expand Up @@ -46,13 +42,13 @@ struct FontDescriptor {
bool italic;
bool monospace;

FontDescriptor(Local<Object> obj) {
FontDescriptor(Napi::Object obj) {
path = NULL;
postscriptName = getString(obj, "postscriptName");
family = getString(obj, "family");
style = getString(obj, "style");
weight = (FontWeight) getNumber(obj, "weight");
width = (FontWidth) getNumber(obj, "width");
postscriptName = getString(obj.Get("postscriptName"));
family = getString(obj.Get("family"));
style = getString(obj.Get("style"));
weight = static_cast<FontWeight>(getNumber(obj.Get("weight")));
width = static_cast<FontWidth>(getNumber(obj.Get("width")));
}

FontDescriptor() {
Expand Down Expand Up @@ -105,26 +101,25 @@ struct FontDescriptor {
style = NULL;
}

Local<Object> toJSObject() {
Nan::EscapableHandleScope scope;
Local<Object> res = Nan::New<Object>();
Napi::Object toJSObject(Napi::Env env) {
Napi::Object res = Napi::Object::New(env);
if (path) {
Nan::Set(res, Nan::New<String>("path").ToLocalChecked(), Nan::New<String>(path).ToLocalChecked());
res.Set("path", Napi::String::New(env, path));
}
if (postscriptName) {
Nan::Set(res, Nan::New<String>("postscriptName").ToLocalChecked(), Nan::New<String>(postscriptName).ToLocalChecked());
res.Set("postscriptName", Napi::String::New(env, postscriptName));
}
if (family) {
Nan::Set(res, Nan::New<String>("family").ToLocalChecked(), Nan::New<String>(family).ToLocalChecked());
res.Set("family", Napi::String::New(env, family));
}
if (style) {
Nan::Set(res, Nan::New<String>("style").ToLocalChecked(), Nan::New<String>(style).ToLocalChecked());
res.Set("style", Napi::String::New(env, style));
}
Nan::Set(res, Nan::New<String>("weight").ToLocalChecked(), Nan::New<Number>(weight));
Nan::Set(res, Nan::New<String>("width").ToLocalChecked(), Nan::New<Number>(width));
Nan::Set(res, Nan::New<String>("italic").ToLocalChecked(), Nan::New<v8::Boolean>(italic));
Nan::Set(res, Nan::New<String>("monospace").ToLocalChecked(), Nan::New<v8::Boolean>(monospace));
return scope.Escape(res);
res.Set("weight", Napi::Number::New(env, weight));
res.Set("width", Napi::Number::New(env, width));
res.Set("italic", Napi::Boolean::New(env, italic));
res.Set("monospace", Napi::Boolean::New(env, monospace));
return res;
}

private:
Expand All @@ -137,25 +132,17 @@ struct FontDescriptor {
return str;
}

char *getString(Local<Object> obj, const char *name) {
Nan::HandleScope scope;
MaybeLocal<Value> value = Nan::Get(obj, Nan::New<String>(name).ToLocalChecked());

if (!value.IsEmpty() && value.ToLocalChecked()->IsString()) {
return copyString(*Nan::Utf8String(value.ToLocalChecked()));
char *getString(Napi::Value value) {
if (!value.IsEmpty() && value.IsString()) {
return copyString(value.ToString().Utf8Value().c_str());
}

return NULL;
}

int getNumber(Local<Object> obj, const char *name) {
Nan::HandleScope scope;
MaybeLocal<Value> value = Nan::Get(obj, Nan::New<String>(name).ToLocalChecked());

if (!value.IsEmpty() && value.ToLocalChecked()->IsNumber()) {
return value.ToLocalChecked()->Int32Value(Nan::GetCurrentContext()).FromJust();
int getNumber(Napi::Value value) {
if (!value.IsEmpty() && value.IsNumber()) {
return value.ToNumber().Int32Value();
}

return 0;
}
};
Expand Down
Loading

0 comments on commit e74c188

Please sign in to comment.