Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import { toBytes, decodeUtf8 } from './tagged-block-common.js';
// The `.rm` formats before version 6 (tablet software 1.x and 2.x): a fixed// header, then plain little-endian records with no tags and no ids.//// header 43 bytes, "reMarkable .lines file, version=N" padded with spaces// int32 number of layers// layer int32 number of strokes// stroke int32 pen, int32 colour, int32 (unused), float32 size,// float32 starting length (version 5 only), int32 number of points// point float32 x, y, speed, direction, width, pressure//// Coordinates are screen pixels, the origin at the top left of the 1404 by// 1872 page. Direction is in radians (0 to 2π), pressure runs from 0 to 1,// width is in pixels and speed in pixels per sample, all a quarter (or a// 255th) of what version 6 stores as integers. The size is 1.875, 2 or 2.125// for the thin, medium and thick setting of a pen.//// Version 5 holds one more field per stroke than version 3. It is zero on// almost every stroke and a float on a few textured ones, which is what the// starting length of a version 6 line looks like, so it is read as that.
export const HEADER_LENGTH = 43;export const LEGACY_VERSIONS = Object.freeze([3, 5]);
const HEADER_RX = /^reMarkable \.lines file, version=(\d+) *$/;
// The format version in a page's header, or null when the bytes are not a// `.rm` file at all.export function rmVersion (data) { const bytes = toBytes(data); if (bytes.length < HEADER_LENGTH) return null; const match = HEADER_RX.exec(decodeUtf8(bytes.subarray(0, HEADER_LENGTH))); return match ? Number(match[1]) : null;}
// The layers of a version 3 or 5 page with the values as the file has them:// { version, layers: [{ strokes: [{ pen, color, size, startingLength,// points: [{ x, y, speed, direction, width, pressure }] }] }] }.export function readLegacyLines (data) { const bytes = toBytes(data); const version = rmVersion(bytes); if (!LEGACY_VERSIONS.includes(version)) { throw new Error(version == null ? 'Not a .rm file' : `Not a version 3 or 5 .rm file: version ${version}`); } const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); let pos = HEADER_LENGTH; const need = (n, what) => { if (pos + n > bytes.length) throw new Error(`Truncated .rm file: ${what} at byte ${pos} runs past the end (${bytes.length})`); }; const int = (what) => { need(4, what); const v = view.getInt32(pos, true); pos += 4; return v; }; const float = (what) => { need(4, what); const v = view.getFloat32(pos, true); pos += 4; return v; }; const count = (what) => { const n = int(what); if (n < 0) throw new Error(`Corrupt .rm file: ${n} for the ${what} at byte ${pos - 4}`); return n; };
const layers = []; const layerCount = count('number of layers'); for (let l = 0; l < layerCount; l++) { const strokes = []; const strokeCount = count('number of strokes'); for (let s = 0; s < strokeCount; s++) { const pen = int('pen'); const color = int('colour'); int('unused field'); const size = float('size'); const startingLength = version >= 5 ? float('starting length') : 0; const pointCount = count('number of points'); need(pointCount * 24, `${pointCount} points`); const points = []; for (let p = 0; p < pointCount; p++) { points.push({ x: view.getFloat32(pos, true), y: view.getFloat32(pos + 4, true), speed: view.getFloat32(pos + 8, true), direction: view.getFloat32(pos + 12, true), width: view.getFloat32(pos + 16, true), pressure: view.getFloat32(pos + 20, true), }); pos += 24; } strokes.push({ pen, color, size, startingLength, points }); } layers.push({ strokes }); } return { version, layers };}
// Where a version 3 or 5 page put a PDF page under its ink: scaled to fit// the screen whole and set against its top left corner (software 3 fits it// to the screen's width and centres it instead, which is how version 6 ink// is stored). Given the size of the PDF page's CropBox in points, returns// the scale in screen pixels per point and the page's left and top in the// coordinates of the scene tree, where x is counted from the middle.export function legacyPagePlacement (width, height) { return { scale: Math.min(1404 / width, 1872 / height), left: -702, top: 0 };}