← Back to list

AutoCAD SHX Font Online Viewer

How to View SHX Fonts Without Installing AutoCAD? The answer is: you can use @mlightcad/shx-parser (Gitlab or Github).

mlightcad · 2025-05-22 14:40 · 0 claps · 2.6 min read
#autocad #autocad-file #autocad-tutorials #autocad-training
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud 🔓 · Open Source

AutoCAD SHX Font Online Viewer

AutoCAD SHX Font Online Viewer

AutoCAD SHX Font Online Viewer

How to View SHX Fonts Without Installing AutoCAD? The answer is: you can use @mlightcad/shx-parser ([Gitlab](https://gitlab.com/mlightcad/shx-parser) or [Github](https://github.com/mlightcad/shx-parser)).

SHX Parser

@mlightcad/shx-parser is a TypeScript library for parsing AutoCAD SHX font files. It offers the following features.

Features

  • Parse SHX font files and extract font data
  • Support for various SHX font types: shapes, bigfont (including extended big font), and unifont
  • Shape parsing with performance optimization:
  • On-demand parsing
  • Modern TypeScript implementation
  • Object-oriented design
  • Comprehensive test coverage

Installation

npm install @mlightcad/shx-parser

SHX Font Online Viewer

@mlightcad/shx-parserprovides one demo app to view and explore SHX font files with the following features:

  • Dual Loading Modes: upload local SHX files or select from a remote font library
  • View all characters in a responsive grid layout
  • Search characters by code (decimal/hex)
  • Click characters to see them in a larger modal view
  • Toggle between decimal and hexadecimal code display
  • Display font Information: font type, version, and character count
  • Renders characters as SVG graphics
  • Responsive grid layout that works on different screen sizes

Implement One Viewer by Your Own

Loading and Displaying Font Information

import { readFile } from 'fs/promises';
import { ShxFont } from '@mlightcad/shx-parser';

async function loadFont(filePath: string) {
  const buffer = await readFile(filePath);
  const font = new ShxFont(buffer.buffer);

  // Display font information
  const fontData = font.fontData;
  console.log('Font Information:');
  console.log('----------------');
  console.log('Font Type:', fontData.header.fontType);
  console.log('Header:', fontData.header.fileHeader);
  console.log('Version:', fontData.header.fileVersion);
  console.log('Info:', fontData.content.info);
  console.log('Orientation:', fontData.content.orientation);
  console.log('Height:', fontData.content.height);
  console.log('Width:', fontData.content.width);
  console.log('Number of shapes:', Object.keys(fontData.content.data).length);

  return font;
}

Converting Shape to SVG Path

function shapeToSvgPath(shape: ShxShape, x: number = 0, y: number = 0): string {
  if (!shape?.polylines.length) return '';

  return shape.polylines.map(polyline => {
    if (!Array.isArray(polyline) || polyline.length === 0) return '';

    return polyline.map((point, i) => {
      const scaledX = (Number(point.x) || 0) + x;
      const scaledY = -(Number(point.y) || 0) + y; // Flip Y coordinate for SVG
      const command = i === 0 ? 'M' : 'L';
      return `${command} ${scaledX.toFixed(2)} ${scaledY.toFixed(2)}`;
    }).join(' ');
  }).filter(Boolean).join(' ');
}

Rendering Text to SVG

interface SvgOptions {
  width?: number;
  height?: number;
  strokeWidth?: string;
  strokeColor?: string;
  isAutoFit?: boolean;
}

function renderTextToSvg(
  font: ShxFont, 
  text: string, 
  size: number, 
  options: SvgOptions = {}
): SVGElement {
  const {
    width = 1000,
    height = 1000,
    strokeWidth = '0.1%',
    strokeColor = 'black',
    isAutoFit = false
  } = options;

  // Create SVG element
  const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  svg.setAttribute('width', width.toString());
  svg.setAttribute('height', height.toString());
  svg.setAttribute('viewBox', `0 0 ${width} ${height}`);

  const padding = size;
  let currentX = padding;
  let maxHeight = 0;

  // Process each character
  for (const char of text) {
    const charCode = char.charCodeAt(0);
    const shape = font.getCharShape(charCode, size);

    if (shape) {
      const group = document.createElementNS('http://www.w3.org/2000/svg', 'g');

      if (isAutoFit) {
        // Auto-fit positioning
        const bbox = shape.bbox;
        const padding = 0.2; // 20% padding
        const width = bbox.maxX - bbox.minX;
        const height = bbox.maxY - bbox.minY;
        const centerX = (bbox.minX + bbox.maxX) / 2;
        const centerY = (bbox.minY + bbox.maxY) / 2;
        group.setAttribute('transform', `translate(${currentX - centerX}, ${-centerY})`);
      } else {
        // Fixed positioning
        group.setAttribute('transform', `translate(${currentX + width / 2}, ${height / 2})`);
      }

      // Create path for the character
      const pathData = shapeToSvgPath(shape);
      const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
      path.setAttribute('d', pathData);
      path.setAttribute('fill', 'none');
      path.setAttribute('stroke', strokeColor);
      path.setAttribute('stroke-width', strokeWidth);

      group.appendChild(path);
      svg.appendChild(group);

      // Update position for next character
      if (shape.lastPoint) {
        currentX += shape.lastPoint.x + size * 0.5;
      } else {
        currentX += size;
      }

      maxHeight = Math.max(maxHeight, size);
    }
  }

  return svg;
}

// Example usage:
async function main() {
  try {
    const font = await loadFont('path/to/your/font.shx');

    // Example 1: Basic rendering
    const svgElement1 = renderTextToSvg(font, "Hello", 12);
    document.body.appendChild(svgElement1);

    // Example 2: Auto-fit rendering with custom options
    const svgElement2 = renderTextToSvg(font, "Hello", 12, {
      width: 1000,
      height: 1000,
      strokeWidth: '0.1%',
      strokeColor: 'black',
      isAutoFit: true
    });
    document.body.appendChild(svgElement2);

    // Clean up resources when done
    font.release();
  } catch (error) {
    console.error('Error:', error instanceof Error ? error.message : 'An unknown error occurred');
  }
}

메타데이터
post_id
c5d2d165dd85
slug
autocad-shx-font-online-viewer-c5d2d165dd85
url
https://medium.com/@mlightcad/autocad-shx-font-online-viewer-c5d2d165dd85
canonical_url
https://medium.com/@mlightcad/autocad-shx-font-online-viewer-c5d2d165dd85
author_url
https://medium.com/@mlightcad
status
ok
fetched_at
2026-06-26 21:52:29