getRichTextInlineStyles method
Future<List<InlineSpan>>
getRichTextInlineStyles(
- String line, [
- TextStyle? style,
- bool returnContentIfNeedIt = false,
- bool addFontSize = true,
])
override
Implementation
@override
Future<List<pw.InlineSpan>> getRichTextInlineStyles(String line,
[pw.TextStyle? style, bool returnContentIfNeedIt = false, bool addFontSize = true]) async {
final List<pw.InlineSpan> spans = <pw.InlineSpan>[];
final Iterable<RegExpMatch> matches = Constant.RICH_TEXT_INLINE_STYLES_PATTERN.allMatches(line);
int i = 0;
int currentIndex = 0;
while (i < matches.length) {
final RegExpMatch match = matches.elementAt(i);
final String plainText = line.substring(currentIndex, match.start);
if (plainText.isNotEmpty) {
if (Constant.INLINE_MATCHER.hasMatch(plainText)) {
spans.add(pw.TextSpan(
children: await applyInlineStyles(plainText, style, addFontSize),
style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
} else {
spans.add(pw.TextSpan(text: plainText.decodeSymbols, style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
}
}
final PdfColor? textColor = pdfColorString(match.group(4) ?? match.group(5));
final PdfColor? backgroundTextColor = pdfColorString(match.group(11) ?? match.group(12));
final double? spacing = double.tryParse(match.group(21) ?? '');
final String? fontFamily = match.group(23);
final double? fontSizeMatch = double.tryParse(match.group(26) ?? '');
final double? fontSize = !addFontSize ? null : fontSizeMatch;
final String content = match.group(28) ?? '';
final double? lineSpacing = spacing?.resolveLineHeight();
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>[];
// Give just the necessary fallbacks for the founded fontFamily
final pw.TextStyle decided_style = style?.copyWith(
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,
color: textColor,
background: pw.BoxDecoration(color: backgroundTextColor),
decorationColor: textColor ?? backgroundTextColor,
) ??
defaultTextStyle.copyWith(
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,
color: textColor,
background: pw.BoxDecoration(color: backgroundTextColor),
decorationColor: textColor ?? backgroundTextColor,
);
spans.add(pw.TextSpan(children: await applyInlineStyles(content, decided_style, addFontSize), style: decided_style));
currentIndex = match.end;
i++;
}
final String remainingText = line.substring(currentIndex);
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.decodeSymbols, style: style ?? defaultTextStyle)); // Apply currentinheritedStyle
}
}
if (returnContentIfNeedIt && spans.isEmpty) {
return <pw.TextSpan>[pw.TextSpan(text: line, style: style)];
}
return spans;
}