pdfDecodePageText function
Reverses pdfEncodePageText, or returns null when bytes are absent,
truncated, corrupt, or from an incompatible format - every such case
is a cache miss the caller recomputes from.
Implementation
PdfPageText? pdfDecodePageText(Uint8List bytes) {
try {
final r = _Reader(bytes);
if (r.u32() != _textBlobMagic) return null;
final pageIndex = r.i32();
final text = r.str();
final runCount = r.u32();
final runs = <PdfExtractedRun>[];
for (var i = 0; i < runCount; i++) {
final runText = r.str();
final startIndex = r.i32();
final isRightToLeft = r.u8() != 0;
final transform =
PdfMatrix(r.f64(), r.f64(), r.f64(), r.f64(), r.f64(), r.f64());
final width = r.f64();
final descent = r.f64();
final ascent = r.f64();
final bounds = PdfRect(r.f64(), r.f64(), r.f64(), r.f64());
final offsetCount = r.u32();
final offsets = offsetCount == 0
? null
: [for (var j = 0; j < offsetCount; j++) r.f64()];
runs.add(PdfExtractedRun(
text: runText,
startIndex: startIndex,
transform: transform,
width: width,
descent: descent,
ascent: ascent,
bounds: bounds,
charOffsets: offsets != null && offsets.length == runText.length + 1
? offsets
: null,
isRightToLeft: isRightToLeft,
));
}
return PdfPageText(pageIndex: pageIndex, text: text, runs: runs);
} catch (_) {
return null;
}
}