getLinkStyle method

  1. @override
Future<List<TextSpan>> getLinkStyle(
  1. String line, [
  2. TextStyle? style,
  3. bool addFontSize = true
])
override

Implementation

@override
Future<List<pw.TextSpan>> getLinkStyle(String line, [pw.TextStyle? style, bool addFontSize = true]) async {
  final List<pw.TextSpan> spans = <pw.TextSpan>[];
  final Iterable<RegExpMatch> matches = Constant.HTML_LINK_TAGS_PATTERN.allMatches(line);
  int currentIndexMatch = 0;
  int i = 0;
  while (i < matches.length) {
    final RegExpMatch match = matches.elementAt(i);
    final String plainText = line.substring(currentIndexMatch, match.start);
    if (plainText.isNotEmpty) {
      if (Constant.INLINE_MATCHER.hasMatch(plainText)) {
        spans.add(pw.TextSpan(
            children: await applyInlineStyles(plainText.convertUTF8QuotesToValidString, style, addFontSize),
            style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
      } else {
        spans.add(pw.TextSpan(
            text: plainText.convertUTF8QuotesToValidString.decodeSymbols,
            style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
      }
    }
    //get content into [title]
    final double? fontSize = double.tryParse(match.group(10) ?? '');
    final double? lineHeight = double.tryParse(match.group(5) ?? '');
    final String? fontFamily = match.group(8);
    final double? lineSpacing = lineHeight?.resolveLineHeight();
    final String href = match.group(11)!;
    final String hrefContent = match.group(13)!;
    final pw.Font font = await onRequestFont.call(fontFamily ?? Constant.DEFAULT_FONT_FAMILY);
    final List<pw.Font> fonts = await onRequestFallbacks?.call(fontFamily ?? Constant.DEFAULT_FONT_FAMILY) ?? <pw.Font>[];
    spans.add(
      pw.TextSpan(
        annotation: pw.AnnotationLink(href),
        text: hrefContent,
        style: (style ?? defaultTextStyle).copyWith(
          color: defaultLinkColor,
          font: font,
          fontBold: await onRequestBoldFont.call(fontFamily ?? Constant.DEFAULT_FONT_FAMILY),
          fontItalic: await onRequestItalicFont.call(fontFamily ?? Constant.DEFAULT_FONT_FAMILY),
          fontBoldItalic: await onRequestBothFont.call(fontFamily ?? Constant.DEFAULT_FONT_FAMILY),
          fontFallback: fonts,
          fontSize: !addFontSize ? null : fontSize ?? defaultFontSize.toDouble(),
          lineSpacing: lineSpacing,
          decoration: pw.TextDecoration.underline,
          decorationStyle: pw.TextDecorationStyle.solid,
          decorationColor: defaultLinkColor,
        ),
      ),
    );
    currentIndexMatch = match.end;
    i++;
  }
  final String remainingText = line.substring(currentIndexMatch);
  if (remainingText.isNotEmpty) {
    if (Constant.INLINE_MATCHER.hasMatch(remainingText)) {
      spans.add(pw.TextSpan(
          children: await applyInlineStyles(remainingText, style, addFontSize),
          style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
    } else {
      spans.add(pw.TextSpan(
          text: remainingText.convertUTF8QuotesToValidString.decodeSymbols, style: style ?? defaultTextStyle)); // Apply current style
    }
  }
  return spans;
}