universal-font-unicode-converter
v1.0.0
Published
Universal, language-independent library for converting legacy font-encoded text into genuine Unicode text using configurable mapping profiles and font analysis.
Maintainers
Readme
universal-font-unicode-converter
A universal, language-independent TypeScript/JavaScript library for converting legacy font-encoded text into genuine Unicode text using configurable mapping profiles and OpenType (TTF/OTF) font analysis.
Features
- Language-Independent: Works with Tamil, Telugu, Hindi, Malayalam, Kannada, Bengali, Gujarati, Punjabi, Marathi, Arabic, and any other Unicode script via JSON mapping profiles.
- Trie-Based Longest-Match-First Engine: Fast, cached, multi-character sequence matching without per-conversion font parsing.
- Unicode Correctness: Validates scalar values, detects lone surrogates and Private Use Area (PUA) misuse, preserves unmapped characters, numbers, punctuation, whitespace, and emojis, and never reorders script characters unless normalization is explicitly requested.
- Font Extraction & Classification: Parses
TTFandOTFfiles viaopentype.jsto inspect metadata,cmap, glyph indexes/names/outlines, Unicode coverage, andGSUB/GPOStables, classifying fonts asUnicode,Legacy encoded,Mixed encoding,Partial Unicode, orUnknown encoding. - Browser & Node.js Compatible: Ships ESM, CommonJS, and TypeScript declarations. Accepts Browser
File/Blob/ArrayBufferand Node.jsBuffer/Uint8Array.
Important: A font's glyph outlines and
cmapdo not reveal intended language semantics. Never claim a legacy glyph has a particular Unicode meaning without a verified mapping.
Installation
npm install universal-font-unicode-converterQuick Start
Node.js / ESM
import {
FontConverter,
loadMappingProfile,
} from 'universal-font-unicode-converter';
const profile = await loadMappingProfile('./profiles/simon-tamil.json');
const converter = new FontConverter(profile);
const result = converter.convert('legacy input');
console.log(result.text);
console.log(result.converted, result.profile, result.errors);CommonJS
const {
FontConverter,
createMappingProfile,
} = require('universal-font-unicode-converter');
const profile = createMappingProfile({
name: 'My Custom Font',
language: 'ta',
mappings: {
f: 'க',
j: 'ி',
fj: 'கி',
},
});
const converter = new FontConverter(profile);
console.log(converter.convertText('fj')); // கிAPI Documentation
FontConverter
new FontConverter(profile, options?)convert(text: string): ConversionResult— Returns{ text, converted, errors, profile, stats }.convertText(text: string): string— Convenience method returning the converted string.convertCharacter(character: string): ConversionResult— Converts a single character or short sequence.convertBatch(textArray: string[]): ConversionResult[]— Converts an array of strings.validate(text: string): UnicodeValidationResult— Validates Unicode scalar values.getMappingInfo(): MappingInfo— Returns profile metadata, entry count, max sequence length, and profile hash.reset(): void— Resets conversion counters and recompiles the active profile.
Mapping Profile API
createMappingProfile(options)— Validates and creates aMappingProfile.MappingBuilder— Fluent builder (addMapping,addMappings,validate,build).loadMappingProfile(source, options?)— Loads from file path (Node.js), URL (fetch), JSON string, or object.importMappingProfile(jsonOrObject, options?)— Parses JSON, detects duplicate keys, and validates Unicode.exportMappingProfile(profile, options?)— Serializes a profile to formatted JSON.validateMappingProfile(profile)— Checks empty mappings, invalid Unicode, PUA/control codepoints, and conflicts.detectMappingConflicts(mappings)— Detects prefix overlaps and duplicate target outputs.ProfileManager— Registers, versions, and queries multiple profiles by name, language, or font family.
Font Extraction API
parseFont(input, options?)— AcceptsArrayBuffer, NodeBuffer,Uint8Array, or BrowserFile/Blob.loadAndParseFont(pathOrUrl, options?)— Loads and parses aTTF/OTFfile from disk or URL.
Returns FontAnalysisResult with metadata, classification, classificationReason, verificationRequired, notice, cmap, glyphs, unicodeCoverage, and layoutTables (GSUB/GPOS).
Mapping JSON Format
{
"name": "Simon Tamil",
"version": "1.0.0",
"language": "ta",
"encoding": "legacy",
"fontFamily": "Simon",
"targetEncoding": "Unicode",
"verified": false,
"mappings": {
"f": "க",
"j": "ி",
"fj": "கி"
}
}Custom Mapping Tutorial
import {
MappingBuilder,
FontConverter,
exportMappingProfile,
} from 'universal-font-unicode-converter';
const profile = new MappingBuilder('My Legacy Font', 'hi')
.setVersion('1.0.0')
.setFontFamily('LegacyHindi')
.setVerified(true)
.addMapping('d', 'क', true)
.addMapping('k', 'ा', true)
.addMapping('dk', 'का', true)
.build();
const converter = new FontConverter(profile);
console.log(converter.convertText('dk')); // का
const json = exportMappingProfile(profile);Font Extraction Tutorial
Browser
import { parseFont } from 'universal-font-unicode-converter';
const input = document.querySelector<HTMLInputElement>('#font-file')!;
input.addEventListener('change', async () => {
const file = input.files?.[0];
if (!file) return;
const analysis = await parseFont(file);
console.log(analysis.metadata.fontFamily, analysis.classification);
console.log(analysis.notice);
});Node.js
import { loadAndParseFont } from 'universal-font-unicode-converter';
const analysis = await loadAndParseFont('./fonts/legacy-font.ttf');
console.log(analysis.classification);
console.log('Cmap entries:', analysis.cmap.length);Interactive Demo Application
The demo/ directory contains a standalone Vite + TypeScript web application where you can:
- Upload a
TTF/OTFfont file. - Inspect extracted glyphs, source character codes, glyph indexes, and
GSUB/GPOSmetadata. - Create or import a mapping profile and toggle Verified vs Unverified status per mapping.
- Enter legacy text, convert to Unicode, compare side-by-side, copy output, and download the JSON profile.
Run the demo:
npm run build
cd demo
npm install
npm run devPublishing to npm
- Verify typechecking, tests, and build:
npm run typecheck npm test npm run build - Inspect the generated package tarball:
npm pack --dry-run - Publish to npm:
npm login npm publish --access public
License
MIT
