0

I am developing an app to create pdf documents using 'PDF'.
When drawing text on PDF, it draws with font applied.

 final data = await rootBundle.load('assets/fonts/SourceHanSans-Normal.ttf');
 final sansFont = pdfWidget.Font.ttf(data);

 pdfWidget.Widget drawText(
      double startX, double startY, String text, pdfWidget.Font font) {
    return pdfWidget.Positioned(
      left: startX,
      top: startY,
      child: pdfWidget.Text(text, style: pdfWidget.TextStyle(font: font)),
    );
  }

Fonts are applied normally. For example, suppose you want to draw the text g to a PDF. The data I want to know is the picture below.
enter image description here

I looked up the API of PDF and found the following function.

 /// 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);

    for (var glyphIndex = 0; glyphIndex < numGlyphs; glyphIndex++) {
      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);
      if (glyphSizes[glyphIndex] == 0) {
        glyphInfoMap[glyphIndex] = PdfFontMetrics(
          left: 0,
          top: 0,
          right: 0,
          bottom: 0,
          ascent: 0,
          descent: 0,
          advanceWidth: advanceWidth / unitsPerEm,
          leftBearing: leftBearing / unitsPerEm,
        );
        continue;
      }
      final offset = glyphOffsets[glyphIndex];
      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

      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,
      );
    }
  }

I save data in glyphInfoMap Map, but I want to know how to get the metrics data for g when I input the desired text g. thank you

Jungwon
  • 1,038
  • 7
  • 20
  • Possibly related: [Meaning of top, ascent, baseline, descent, bottom, and leading in Android's FontMetrics](https://stackoverflow.com/q/27631736/295004) – Morrison Chang Dec 01 '22 at 08:51

0 Answers0