ttf_parser.dart 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
/*
 * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'dart:convert';
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:meta/meta.dart';

import 'font_metrics.dart';

enum TtfParserName {
  copyright,
  fontFamily,
  fontSubfamily,
  uniqueID,
  fullName,
  version,
  postScriptName,
  trademark,
  manufacturer,
  designer,
  description,
  manufacturerURL,
  designerURL,
  license,
  licenseURL,
  reserved,
  preferredFamily,
  preferredSubfamily,
  compatibleFullName,
  sampleText,
  postScriptFindFontName,
  wwsFamily,
  wwsSubfamily,
}

@immutable
class TtfGlyphInfo {
  const TtfGlyphInfo(this.index, this.data, this.compounds);

  final int index;
  final Uint8List data;
  final List<int> compounds;

  TtfGlyphInfo copy() {
    return TtfGlyphInfo(
      index,
      Uint8List.fromList(data),
      List<int>.from(compounds),
    );
  }

  @override
  String toString() => 'Glyph $index $compounds';
}

class TtfParser {
  TtfParser(ByteData bytes) : bytes = UnmodifiableByteDataView(bytes) {
    final numTables = bytes.getUint16(4);

    for (var i = 0; i < numTables; i++) {
      final name = utf8.decode(bytes.buffer.asUint8List(i * 16 + 12, 4));
      final offset = bytes.getUint32(i * 16 + 20);
      final size = bytes.getUint32(i * 16 + 24);
      tableOffsets[name] = offset;
      tableSize[name] = size;
    }

    assert(tableOffsets.containsKey(head_table),
        'Unable to find the `head` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(name_table),
        'Unable to find the `name` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(hmtx_table),
        'Unable to find the `hmtx` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(hhea_table),
        'Unable to find the `hhea` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(cmap_table),
        'Unable to find the `cmap` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(maxp_table),
        'Unable to find the `maxp` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(loca_table),
        'Unable to find the `loca` table. This file is not a supported TTF font');
    assert(tableOffsets.containsKey(glyf_table),
        'Unable to find the `glyf` table. This file is not a supported TTF font');

    _parseCMap();
    _parseIndexes();
    _parseGlyphs();
  }

  static const String head_table = 'head';
  static const String name_table = 'name';
  static const String hmtx_table = 'hmtx';
  static const String hhea_table = 'hhea';
  static const String cmap_table = 'cmap';
  static const String maxp_table = 'maxp';
  static const String loca_table = 'loca';
  static const String glyf_table = 'glyf';

  final UnmodifiableByteDataView bytes;
  final Map<String, int> tableOffsets = <String, int>{};
  final Map<String, int> tableSize = <String, int>{};

  final Map<int, int> charToGlyphIndexMap = <int, int>{};
  final List<int> glyphOffsets = <int>[];
  final Map<int, PdfFontMetrics> glyphInfoMap = <int, PdfFontMetrics>{};

  int get unitsPerEm => bytes.getUint16(tableOffsets[head_table]! + 18);

  int get xMin => bytes.getInt16(tableOffsets[head_table]! + 36);

  int get yMin => bytes.getInt16(tableOffsets[head_table]! + 38);

  int get xMax => bytes.getInt16(tableOffsets[head_table]! + 40);

  int get yMax => bytes.getInt16(tableOffsets[head_table]! + 42);

  int get indexToLocFormat => bytes.getInt16(tableOffsets[head_table]! + 50);

  int get ascent => bytes.getInt16(tableOffsets[hhea_table]! + 4);

  int get descent => bytes.getInt16(tableOffsets[hhea_table]! + 6);

  int get numOfLongHorMetrics =>
      bytes.getUint16(tableOffsets[hhea_table]! + 34);

  int get numGlyphs => bytes.getUint16(tableOffsets[maxp_table]! + 4);

  String get fontName =>
      getNameID(TtfParserName.postScriptName) ?? hashCode.toString();

  bool get unicode => bytes.getUint32(0) == 0x10000;

  // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
  String? getNameID(TtfParserName fontNameID) {
    final basePosition = tableOffsets[name_table]!;
    // final format = bytes.getUint16(basePosition);
    final count = bytes.getUint16(basePosition + 2);
    final stringOffset = bytes.getUint16(basePosition + 4);
    var pos = basePosition + 6;
    String? _fontName;

    for (var i = 0; i < count; i++) {
      final platformID = bytes.getUint16(pos);
      final nameID = bytes.getUint16(pos + 6);
      final length = bytes.getUint16(pos + 8);
      final offset = bytes.getUint16(pos + 10);
      pos += 12;

      if (platformID == 1 && nameID == fontNameID.index) {
        try {
          _fontName = utf8.decode(bytes.buffer
              .asUint8List(basePosition + stringOffset + offset, length));
        } catch (a) {
          print('Error: $platformID $nameID $a');
        }
      }

      if (platformID == 3 && nameID == fontNameID.index) {
        try {
          return _decodeUtf16(bytes.buffer
              .asUint8List(basePosition + stringOffset + offset, length));
        } catch (a) {
          print('Error: $platformID $nameID $a');
        }
      }
    }
    return _fontName;
  }

  void _parseCMap() {
    final basePosition = tableOffsets[cmap_table]!;
    final numSubTables = bytes.getUint16(basePosition + 2);
    for (var i = 0; i < numSubTables; i++) {
      final offset = bytes.getUint32(basePosition + i * 8 + 8);
      final format = bytes.getUint16(basePosition + offset);

      switch (format) {
        case 0:
          _parseCMapFormat0(basePosition + offset + 2);
          break;

        case 4:
          _parseCMapFormat4(basePosition + offset + 2);
          break;
        case 6:
          _parseCMapFormat6(basePosition + offset + 2);
          break;

        case 12:
          _parseCMapFormat12(basePosition + offset + 2);
          break;
      }
    }
  }

  void _parseCMapFormat0(int basePosition) {
    assert(bytes.getUint16(basePosition) == 262);
    for (var i = 0; i < 256; i++) {
      final charCode = i;
      final glyphIndex = bytes.getUint8(basePosition + i + 2);
      if (glyphIndex > 0) {
        charToGlyphIndexMap[charCode] = glyphIndex;
      }
    }
  }

  void _parseCMapFormat4(int basePosition) {
    final segCount = bytes.getUint16(basePosition + 4) ~/ 2;
    final endCodes = <int>[];
    for (var i = 0; i < segCount; i++) {
      endCodes.add(bytes.getUint16(basePosition + i * 2 + 12));
    }
    final startCodes = <int>[];
    for (var i = 0; i < segCount; i++) {
      startCodes.add(bytes.getUint16(basePosition + (segCount + i) * 2 + 14));
    }
    final idDeltas = <int>[];
    for (var i = 0; i < segCount; i++) {
      idDeltas.add(bytes.getUint16(basePosition + (segCount * 2 + i) * 2 + 14));
    }
    final idRangeOffsetBasePos = basePosition + segCount * 6 + 14;
    final idRangeOffsets = <int>[];
    for (var i = 0; i < segCount; i++) {
      idRangeOffsets.add(bytes.getUint16(idRangeOffsetBasePos + i * 2));
    }
    for (var s = 0; s < segCount - 1; s++) {
      final startCode = startCodes[s];
      final endCode = endCodes[s];
      final idDelta = idDeltas[s];
      final idRangeOffset = idRangeOffsets[s];
      final idRangeOffsetAddress = idRangeOffsetBasePos + s * 2;
      for (var c = startCode; c <= endCode; c++) {
        int glyphIndex;
        if (idRangeOffset == 0) {
          glyphIndex = (idDelta + c) % 65536;
        } else {
          final glyphIndexAddress =
              idRangeOffset + 2 * (c - startCode) + idRangeOffsetAddress;
          glyphIndex = bytes.getUint16(glyphIndexAddress);
        }
        charToGlyphIndexMap[c] = glyphIndex;
      }
    }
  }

  void _parseCMapFormat6(int basePosition) {
    final firstCode = bytes.getUint16(basePosition + 4);
    final entryCount = bytes.getUint16(basePosition + 6);
    for (var i = 0; i < entryCount; i++) {
      final charCode = firstCode + i;
      final glyphIndex = bytes.getUint16(basePosition + i * 2 + 8);
      if (glyphIndex > 0) {
        charToGlyphIndexMap[charCode] = glyphIndex;
      }
    }
  }

  void _parseCMapFormat12(int basePosition) {
    final numGroups = bytes.getUint32(basePosition + 10);
    assert(bytes.getUint32(basePosition + 2) == 12 * numGroups + 16);

    for (var i = 0; i < numGroups; i++) {
      final startCharCode = bytes.getUint32(basePosition + i * 12 + 14);
      final endCharCode = bytes.getUint32(basePosition + i * 12 + 18);
      final startGlyphID = bytes.getUint32(basePosition + i * 12 + 22);

      for (var j = startCharCode; j <= endCharCode; j++) {
        assert(!charToGlyphIndexMap.containsKey(j) ||
            charToGlyphIndexMap[j] == startGlyphID + j - startCharCode);
        charToGlyphIndexMap[j] = startGlyphID + j - startCharCode;
      }
    }
  }

  void _parseIndexes() {
    final basePosition = tableOffsets[loca_table];
    final numGlyphs = this.numGlyphs;
    if (indexToLocFormat == 0) {
      for (var i = 0; i < numGlyphs; i++) {
        glyphOffsets.add(bytes.getUint16(basePosition! + i * 2) * 2);
      }
    } else {
      for (var i = 0; i < numGlyphs; i++) {
        glyphOffsets.add(bytes.getUint32(basePosition! + i * 4));
      }
    }
  }

  /// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html
  void _parseGlyphs() {
    final baseOffset = tableOffsets[glyf_table]!;
    final hmtxOffset = tableOffsets[hmtx_table]!;
    final unitsPerEm = this.unitsPerEm;
    final numOfLongHorMetrics = this.numOfLongHorMetrics;
    final defaultadvanceWidth =
        bytes.getUint16(hmtxOffset + (numOfLongHorMetrics - 1) * 4);
    var glyphIndex = 0;
    for (final offset in glyphOffsets) {
      final xMin = bytes.getInt16(baseOffset + offset + 2); // 2
      final yMin = bytes.getInt16(baseOffset + offset + 4); // 4
      final xMax = bytes.getInt16(baseOffset + offset + 6); // 6
      final yMax = bytes.getInt16(baseOffset + offset + 8); // 8
      final advanceWidth = glyphIndex < numOfLongHorMetrics
          ? bytes.getUint16(hmtxOffset + glyphIndex * 4)
          : defaultadvanceWidth;
      final leftBearing = glyphIndex < numOfLongHorMetrics
          ? bytes.getInt16(hmtxOffset + glyphIndex * 4 + 2)
          : bytes.getInt16(hmtxOffset +
              numOfLongHorMetrics * 4 +
              (glyphIndex - numOfLongHorMetrics) * 2);
      glyphInfoMap[glyphIndex] = PdfFontMetrics(
        left: xMin.toDouble() / unitsPerEm,
        top: yMin.toDouble() / unitsPerEm,
        right: xMax.toDouble() / unitsPerEm,
        bottom: yMax.toDouble() / unitsPerEm,
        ascent: ascent.toDouble() / unitsPerEm,
        descent: descent.toDouble() / unitsPerEm,
        advanceWidth: advanceWidth.toDouble() / unitsPerEm,
        leftBearing: leftBearing.toDouble() / unitsPerEm,
      );
      glyphIndex++;
    }
  }

  /// http://stevehanov.ca/blog/?id=143
  TtfGlyphInfo readGlyph(int index) {
    assert(index < glyphOffsets.length);

    final start = tableOffsets[glyf_table]! + glyphOffsets[index];

    final numberOfContours = bytes.getInt16(start);
    assert(numberOfContours >= -1);

    if (numberOfContours == -1) {
      return _readCompoundGlyph(index, start, start + 10);
    } else {
      return _readSimpleGlyph(index, start, start + 10, numberOfContours);
    }
  }

  TtfGlyphInfo _readSimpleGlyph(
      int glyph, int start, int offset, int numberOfContours) {
    const X_IS_BYTE = 2;
    const Y_IS_BYTE = 4;
    const REPEAT = 8;
    const X_DELTA = 16;
    const Y_DELTA = 32;

    var numPoints = 1;

    for (var i = 0; i < numberOfContours; i++) {
      numPoints = math.max(numPoints, bytes.getUint16(offset) + 1);
      offset += 2;
    }

    // skip over intructions
    offset += bytes.getUint16(offset) + 2;

    if (numberOfContours == 0) {
      return TtfGlyphInfo(
        glyph,
        Uint8List.view(bytes.buffer, start, offset - start),
        const <int>[],
      );
    }

    final flags = <int>[];

    for (var i = 0; i < numPoints; i++) {
      final flag = bytes.getUint8(offset++);
      flags.add(flag);

      if (flag & REPEAT != 0) {
        var repeatCount = bytes.getUint8(offset++);
        i += repeatCount;
        while (repeatCount-- > 0) {
          flags.add(flag);
        }
      }
    }

    var byteFlag = X_IS_BYTE;
    var deltaFlag = X_DELTA;
    for (var a = 0; a < 2; a++) {
      for (var i = 0; i < numPoints; i++) {
        final flag = flags[i];
        if (flag & byteFlag != 0) {
          offset++;
        } else if (~flag & deltaFlag != 0) {
          offset += 2;
        }
      }
      byteFlag = Y_IS_BYTE;
      deltaFlag = Y_DELTA;
    }

    return TtfGlyphInfo(
      glyph,
      Uint8List.view(bytes.buffer, start, offset - start),
      const <int>[],
    );
  }

  TtfGlyphInfo _readCompoundGlyph(int glyph, int start, int offset) {
    const ARG_1_AND_2_ARE_WORDS = 0x0001;
    const HAS_SCALE = 0x008;
    const MORE_COMPONENTS = 0x0020;
    const HAS_X_Y_SCALE = 0x0040;
    const HAS_TRANFORMATION_MATRIX = 0x0080;
    const WE_HAVE_INSTRUCTIONS = 0x0100;

    final components = <int>[];
    var hasInstructions = false;
    var flags = MORE_COMPONENTS;

    while (flags & MORE_COMPONENTS != 0) {
      flags = bytes.getUint16(offset);
      final glyphIndex = bytes.getUint16(offset + 2);
      offset += (flags & ARG_1_AND_2_ARE_WORDS != 0) ? 8 : 6;
      if (flags & HAS_SCALE != 0) {
        offset += 2;
      } else if (flags & HAS_X_Y_SCALE != 0) {
        offset += 4;
      } else if (flags & HAS_TRANFORMATION_MATRIX != 0) {
        offset += 8;
      }

      components.add(glyphIndex);
      if (flags & WE_HAVE_INSTRUCTIONS != 0) {
        assert(!hasInstructions); // Not already set
        hasInstructions = true;
      }
    }

    if (hasInstructions) {
      offset += bytes.getUint16(offset) + 2;
    }

    return TtfGlyphInfo(
      glyph,
      Uint8List.view(bytes.buffer, start, offset - start),
      components,
    );
  }

  String _decodeUtf16(Uint8List bytes) {
    final charCodes = <int>[];
    for (var i = 0; i < bytes.length; i += 2) {
      charCodes.add((bytes[i] << 8) | bytes[i + 1]);
    }
    return String.fromCharCodes(charCodes);
  }
}