getCodeBlock method

  1. @override
Future<List<Widget>> getCodeBlock(
  1. String line, [
  2. TextStyle? style
])
override

Implementation

@override
Future<List<pw.Widget>> getCodeBlock(String line, [pw.TextStyle? style]) async {
  final List<pw.Widget> spans = <pw.Widget>[];
  final pw.TextStyle defaultStyle = pw.TextStyle(
    fontSize: 12,
    font: codeBlockFont ?? pw.Font.courier(),
    fontFallback: <pw.Font>[pw.Font.courierBold(), pw.Font.courierBoldOblique(), pw.Font.courierOblique(), pw.Font.symbol()],
    letterSpacing: 1.5,
    lineSpacing: 1.1,
    wordSpacing: 0.5,
    color: PdfColor.fromHex("#808080"),
  );
  final pw.TextStyle codeBlockStyle = codeBlockTextStyle ?? defaultStyle;
  final Iterable<RegExpMatch> matches = Constant.CODE_PATTERN.allMatches(line);
  for (int i = 0; i < matches.length; i++) {
    final RegExpMatch match = matches.elementAt(i);
    final String codeBlock = match.group(1)!;
    final String fixedBlock = codeBlock
        .replaceAll('&lt;', '<')
        .replaceAll('&gt;', '>'); //we must to replace this, because the issue comes to coverter from delta to html|
    if (fixedBlock.contains('\n')) {
      final List<String> splittedBlock = fixedBlock.split('\n');
      if (splittedBlock.isNotEmpty && splittedBlock[splittedBlock.length - 1] == '') {
        splittedBlock.removeLast();
      }
      for (String newLine in splittedBlock) {
        _numCodeLine += 1;
        spans.add(pw.Container(
          width: pageWidth,
          color: this.codeBlockBackgroundColor ?? PdfColor.fromHex('#fbfbf9'),
          child: pw.RichText(
            softWrap: true,
            overflow: pw.TextOverflow.span,
            text: pw.TextSpan(
              style: codeBlockStyle,
              children: <pw.InlineSpan>[
                pw.TextSpan(text: "$_numCodeLine", style: codeBlockNumLinesTextStyle),
                pw.TextSpan(text: "   $newLine"),
              ],
            ),
          ),
        ));
      }
    } else {
      _numCodeLine += 1;
      spans.add(pw.Container(
        width: pageWidth,
        color: this.codeBlockBackgroundColor ?? PdfColor.fromHex('#fbfbf9'),
        child: pw.RichText(
          softWrap: true,
          overflow: pw.TextOverflow.span,
          text: pw.TextSpan(
            style: codeBlockStyle,
            children: <pw.InlineSpan>[
              pw.TextSpan(text: "$_numCodeLine", style: codeBlockNumLinesTextStyle),
              pw.TextSpan(text: "   $fixedBlock"),
            ],
          ),
        ),
      ));
    }
  }
  _numCodeLine = 0;
  return spans;
}