# Font Export Modules

This directory contains exporters for converting FontDefinition objects to standard font file formats.

## Exporters Available

### JavaScript Exporter (`js-exporter.js`)

Uses **opentype.js** library to generate fonts in the browser or Node.js.

**Capabilities:**
- Exports to: TTF, OTF
- Works in browser and Node.js
- No external dependencies beyond opentype.js

**Usage:**

```javascript
const { FontExporterJS } = require('./js-exporter.js');

const exporter = new FontExporterJS();

// Export to ArrayBuffer
const ttfBuffer = exporter.exportToArrayBuffer(fontDefinition, 'ttf');

// Export to data URL (for CSS)
const dataUrl = exporter.exportToDataURL(fontDefinition, 'ttf');

// Export to file (browser)
exporter.exportToFile(fontDefinition, 'myfont.ttf', 'ttf');
```

**Limitations:**
- WOFF2 format requires external conversion
- Lower quality than Python exporter for complex glyphs
- Limited hinting support

---

### Python Exporter (`python-exporter.py`)

Uses **fonttools** and **fontmake** for production-quality font generation.

**Capabilities:**
- Exports to: TTF, WOFF2 (with compression)
- Higher quality output
- Better handling of complex glyphs
- Full font metrics support

**Installation:**

```bash
pip install fonttools fontmake
```

**Usage (Python):**

```python
from python_exporter import FontExporterPython

exporter = FontExporterPython()

# Export from JSON file
exporter.export('font.json', 'output.ttf', format='ttf')
exporter.export('font.json', 'output.woff2', format='woff2')

# Export from dict
font_data = {...}  # FontDefinition as dict
exporter.export(font_data, 'output.ttf', format='ttf')
```

**Usage (CLI):**

```bash
python python-exporter.py font.json output.ttf
python python-exporter.py font.json output.woff2
```

**API Methods:**

- `export(font_json_path_or_data, output_path, format)` - Main export function
- `export_to_ttf(font_data, output_path)` - Export specifically to TTF
- `export_to_woff2(font_data, output_path)` - Export specifically to WOFF2
- `create_ufo_font(font_data, ufo_path)` - Create UFO intermediate format

---

## Choosing an Exporter

### Use JavaScript Exporter (`js-exporter.js`) if:
- You want a browser-based workflow
- You need TTF/OTF format
- You don't want Python dependencies
- You're prototyping/experimenting

### Use Python Exporter (`python-exporter.py`) if:
- You need WOFF2 format for web deployment
- You want production-quality fonts
- You have complex glyphs with curves
- You have server-side build process
- You need full font metrics support

---

## File Format Reference

### Input Format (JSON)

Both exporters accept FontDefinition as JSON:

```json
{
  "name": "MyFont",
  "familyName": "My Font Family",
  "styleName": "Regular",
  "metrics": {
    "unitsPerEm": 1000,
    "ascender": 800,
    "descender": -200,
    "xHeight": 500,
    "capHeight": 700,
    "lineGap": 0
  },
  "metadata": {
    "copyright": "© 2024 Designer",
    "version": "1.0",
    "designer": "Name",
    "description": "Font Description"
  },
  "glyphs": {
    "A": {
      "char": "A",
      "unicode": 65,
      "width": 600,
      "leftSideBearing": 50,
      "rightSideBearing": 50,
      "contours": [
        {
          "points": [
            {"x": 100, "y": 0, "onCurve": true},
            {"x": 500, "y": 0, "onCurve": true},
            {"x": 300, "y": 700, "onCurve": true}
          ]
        }
      ]
    }
  },
  "kerning": {
    "AV": -50,
    "To": -40
  }
}
```

### Point Format

Points in contours use this format:

```json
{
  "x": 100,
  "y": 200,
  "onCurve": true
}
```

- `x`, `y`: Font unit coordinates
- `onCurve`: Boolean
  - `true` = on-curve point (anchor point for curves)
  - `false` = off-curve control point (Bézier handle)

**Curve Types:**
- **Line:** One on-curve point followed by another on-curve point
- **Quadratic Bézier:** Two on-curve points with one off-curve control point between
- **Cubic Bézier:** Two on-curve points with two off-curve control points between

---

## Troubleshooting

### Python Exporter Issues

**Issue: `UFOWriter.getInfo() API incompatible`**

*Solution:* The exporter now uses fallback methods to handle different fonttools versions. Make sure you have a recent version:

```bash
pip install --upgrade fonttools
```

**Issue: `fontmake not found`**

*Solution:* Install fontmake:

```bash
pip install fontmake
```

**Issue: `WOFF2 export fails`**

*Solution:* Ensure you have Brotli support:

```bash
pip install brotli
```

### JavaScript Exporter Issues

**Issue: `opentype.js not found`**

*Solution:* Make sure it's installed:

```bash
npm install opentype.js
```

**Issue: WOFF2 not supported**

*Solution:* Use the Python exporter for WOFF2, or convert TTF to WOFF2 using external tools:

```bash
# Using fonttools command line
python -m fontTools.ttLib.woff2_compress font.ttf
```

---

## Performance Notes

- **Python exporter** is slower but produces better quality
- **JavaScript exporter** is faster but less feature-rich
- For web deployment, use Python → WOFF2 for smallest file size
- For testing/preview, use JavaScript → TTF for speed

---

## Examples

See `../examples/` for complete working examples:

- `simple-pixel-font.js` - Pixel font generation
- `algorithmic-font.js` - Procedurally-generated font
- `living-os-font.js` - Complex organic font

---

## Version Compatibility

### JavaScript Exporter
- opentype.js: 1.3.0+
- Works in browsers and Node.js 12+

### Python Exporter
- fonttools: 4.0+
- fontmake: 2.3+
- Python: 3.6+

Check versions:
```bash
python -m fontTools.feaLib.builder --version
fontmake --version
```
