diff --git a/CHANGELOG.md b/CHANGELOG.md index 92288cad..06b1e8d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -129,6 +129,8 @@ The text of a sheet prints too: a text note's RTF string (class 0x0F83 block) is - API: `RvtIO.Pdf.PdfDocument` writes PDF 1.4 without a third-party library: pages of A4 to A0 or any size in millimetres, vector lines, polylines and rectangles with millimetre line widths, and single-line text in the standard Helvetica font (printable ASCII only; an embedded font for Cyrillic comes with the sheet renderer). `PdfPage.DrawLine`, `DrawPolyline`, `DrawRectangle` and `DrawText` work in millimetres from the lower-left corner, y up, the frame a printed sheet uses. This is the output layer of the sheet printing work (#18); no reader data is rendered into a page yet. Measured: a page of the decoded title block linework of one sheet is written as an A2 landscape PDF; the 10 tests parse the document back from its bytes and check the operators, the page boxes and the cross-reference offsets. No existing output changes. - API and CLI: `RvtIO.Sheets.SheetCatalog` lists the sheets of a file and the viewports placed on them, and reports what the release does not decode. A sheet is an element that carries the `Sheet Number` or `Sheet Name` parameter (class 995 of the Revit 2022 schema); the `OST_Sheets` category is not used, because every block whose class word is 0x03E3 takes it and one architectural sample has 14,354 elements of that category against 100 sheets. A `SheetRecord` carries the number, name, issue date, the drawn/designed/checked/approved by-lines, the view scale, the viewports of the sheet and the elements the sheet owns; a `ViewportRecord` carries its sheet and detail number. Ownership comes from the stored `m_ownerDBViewId`, now exposed as `Element.OwnerViewId` (null when no view owns the element). `SheetCatalog.Issues` names the missing pieces instead of hiding them: page size (kept in the title block family), view placement (the viewport block stores only the view title label origin), the displayed view (reached through the viewer element, schema class 621, which is not decoded), 2D content and the crop region. `rvtio sheets [output.json]` writes the same catalog as JSON (exit code 0 written, 1 usage or an unwritable output path, 2 an unreadable file). The catalog exposes no coordinate, size or unit, because no sheet coordinate is decoded yet. Measured on one architectural sample of Revit 2022: 100 sheets, 97 viewports placed on sheets, 97 sheets with owned elements (12 to 143 each); on a second sample 17 sheets and 17 viewports, every sheet without a number. In a Revit 2024 and a Revit 2025 save of the same project the catalog is empty: the reader reads those files by their own class schema, maps neither the sheet class nor any viewport reference, and the tool reports the empty result on standard error; covering those view classes is future work. The full JSON and the compact JSON of the sample are byte-for-byte identical to the build before this entry (the public property is not serialized), and the 16 new library tests (one of them on a private project fixture, skipped without it) and 3 new CLI tests pass. +- Printing: the embedded title font prefers Arial's metric-compatible substitutes (Liberation Sans, Arimo) over DejaVu Sans on Linux, so a title wraps the same on every machine, and `RVTIO_FONTS` names extra folders of font files searched first (`:` on Linux/macOS, `;` on Windows), as in rvt-prt's `RVT_PRT_FONTS`. New tests: `PdfFontsTests`. + - Tests: adds `IndependentInflaterCrossCheckTests` to cross-check every compressed `Formats/*`, `Global/*` and `Partitions/*` stream and gzip member (checked-in fixtures and synthetic multi-page `.rvt` containers in CI, plus public `.rvt` and `.rfa` samples when `RVTIO_TEST_FILES` is set) against `System.IO.Compression.DeflateStream`, asserting equal byte length, SHA-256 digest, CRC-32, gzip `ISIZE` trailer and format-9 segment `unc` and record counts while failing strictly on integrity mismatches independently of dictionary-chained or Deflate64 skip classification and reporting compared and skipped counts with reasons. (Closes #95) - Adds `--ifc-length-unit ` (`IfcExporter.LengthUnit` in the library) to override the length unit family declared in the IFC file (`Millimetre`, `Metre` or `Foot`), scale all coordinates, areas, volumes and base quantities consistently, and name `.SQUARE_METRE.` and `.CUBIC_METRE.` on the SI base units referenced by `Foot` area and volume conversion units across both profiles; omitting the setting keeps the project's stored length unit (or millimetres in the legacy profile) byte-for-byte unchanged, and unsupported unit names exit with code 1. Compact JSON keeps the project's formatted display units and full JSON keeps Revit's internal units. (Refs #51) - Export report and diagnostics: `streams[].unpackedBytes` (`StreamRecord.UnpackedBytes`) records the inflated byte size of every decoded stream alongside `rawBytes`, and `export.geometry` (`exact`, `approximated`, `boxOnly`, `none`) and `export.elements[]` (`GeometryQualityClass`, `ElementGeometryQuality`, `IfcExporter.ExportedElementQualities`) report the geometry quality class of each exported element in IFC, compact JSON and full JSON exports with totals that sum to `export.exported`. (Closes #96) diff --git a/docs/printing.md b/docs/printing.md index eb33d764..7641e5b1 100644 --- a/docs/printing.md +++ b/docs/printing.md @@ -176,8 +176,9 @@ stays empty. | Room legend | below the plan: a colour swatch and the name of every kind of room on it | The page is white with a frame at the margin, and the drawing is centred. The title (the level name and the scale) is -written with an embedded TrueType font found on the machine (Arial, Segoe UI or Tahoma on Windows, DejaVu Sans on Linux, -Arial or Helvetica Neue on macOS); without such a font the page is drawn without a title, because the standard PDF font +written with an embedded TrueType font found on the machine (Arial or its metric-compatible substitutes +Liberation Sans and Arimo on Linux, Arial on macOS, Arial, Segoe UI or Tahoma on Windows; `RVTIO_FONTS` names extra +folders searched first, `:` on Linux/macOS and `;` on Windows, as in rvt-prt's `RVT_PRT_FONTS`); without such a font the page is drawn without a title, because the standard PDF font holds no Cyrillic. Elements are selected by their level (`LEVEL_PARAM`); a wall without a location line or a column without a footprint is left out and counted by neither selection nor drawing. diff --git a/docs/public-api.md b/docs/public-api.md index 06f28005..c744eb6f 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -101,7 +101,7 @@ See [printing a plan](printing.md) for what is drawn, what is not, and the roadm | `PdfPageSize` | Paper sizes A4 to A0 in millimetres, `Landscape` for the turned page. | | `PdfTextAlignment` | `Left`, `Center`, `Right`: where the text anchor sits. | | `PdfTrueTypeFont` | A TrueType font parsed for embedding (`UnitsPerEm`, `GlyphOf`, `AdvanceOf`), drawn as a Type0 font with Identity-H encoding and a ToUnicode map, so text in the scripts it covers (Cyrillic included) is real, selectable PDF text. | -| `PdfFonts` | `TryFind` loads the first TrueType font found on the machine (Arial, Segoe UI, Tahoma, DejaVu Sans, Helvetica Neue). | +| `PdfFonts` | `TryFind` loads the first TrueType font found on the machine (Arial, its metric-compatible substitutes Liberation Sans and Arimo, Segoe UI, Tahoma, DejaVu Sans, Helvetica Neue); `RVTIO_FONTS` names extra folders searched first. | Without `PdfDocument.Font` the writer uses the standard Helvetica font, which holds printable ASCII only, and a text with another character is refused. With it, every text is written with the embedded font. diff --git a/src/RvtIO/Pdf/PdfFonts.cs b/src/RvtIO/Pdf/PdfFonts.cs index c0592f57..8919cd9e 100644 --- a/src/RvtIO/Pdf/PdfFonts.cs +++ b/src/RvtIO/Pdf/PdfFonts.cs @@ -3,10 +3,34 @@ namespace RvtIO.Pdf; /// Where the print pipeline finds a TrueType font to embed. public static class PdfFonts { - /// The font files tried in order: Arial, Segoe UI and Tahoma on Windows, DejaVu Sans on Linux, Arial and - /// Helvetica Neue on macOS. + /// Environment variable with extra folders of font files, searched first. + /// Several folders are separated by : on Linux/macOS and ; on Windows, + /// as in rvt-prt's RVT_PRT_FONTS. + public const string FontsEnvVar = "RVTIO_FONTS"; + + /// File names tried in each extra folder first: Arial and its metric-compatible + /// substitutes (Liberation Sans, Arimo), which wrap and fit text as Arial does. + private static readonly string[] ExtraFileNames = + [ + "arial.ttf", + "Arial.ttf", + "LiberationSans-Regular.ttf", + "Arimo-Regular.ttf", + "DejaVuSans.ttf", + ]; + + /// The font files tried in order: Arial and its metric-compatible substitutes + /// (Liberation Sans, Arimo) on Linux, Arial on macOS and Windows, then Segoe UI and Tahoma + /// on Windows, DejaVu Sans and Helvetica Neue. A substitute keeps the widths of Arial, + /// so a title wraps the same on every machine. private static readonly string[] Candidates = [ + // Debian Bookworm and later ship Liberation Sans in fonts-liberation2. + "/usr/share/fonts/truetype/liberation2/LiberationSans-Regular.ttf", + "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", + "/usr/share/fonts/TTF/LiberationSans-Regular.ttf", + "/usr/share/fonts/truetype/arimo/Arimo-Regular.ttf", + "/usr/share/fonts/TTF/Arimo-Regular.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "/usr/share/fonts/TTF/DejaVuSans.ttf", "/System/Library/Fonts/Supplemental/Arial.ttf", @@ -16,13 +40,17 @@ public static class PdfFonts @"C:\Windows\Fonts\tahoma.ttf", ]; - /// Loads the first font file of that exists and parses. + /// Loads the first font file of that exists and parses, + /// after the folders of . /// The file that was used, or null when none exists. /// The font, or null when no candidate is a TrueType font this reader supports. - public static PdfTrueTypeFont? TryFind(out string? path) + public static PdfTrueTypeFont? TryFind(out string? path) => + TryFind(Environment.GetEnvironmentVariable, out path); + + internal static PdfTrueTypeFont? TryFind(Func getEnv, out string? path) { path = null; - foreach (string candidate in Candidates) + foreach (string candidate in CandidateFiles(getEnv)) { if (!File.Exists(candidate)) continue; try @@ -31,11 +59,40 @@ public static class PdfFonts path = candidate; return font; } - catch (Exception exception) when (exception is NotSupportedException or IOException or UnauthorizedAccessException) + catch (Exception exception) when (exception is NotSupportedException or IOException or UnauthorizedAccessException + // A corrupt candidate must not abort the search: every out-of-range table read already + // reports NotSupportedException, this covers any other malformed-input failure. + or ArgumentException) { - // A .ttc collection or a font without a Unicode cmap: try the next candidate. + // A .ttc collection, a font without a Unicode cmap or a corrupt file: try the next candidate. } } return null; } + + /// Every font file tried in order, without touching the disk: the files of the + /// folders first, then . + internal static IReadOnlyList CandidateFiles(Func getEnv) + { + var files = new List(); + string? extra = null; + try + { + extra = getEnv(FontsEnvVar); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + // No extra folders then; the machine candidates still stand. + } + if (!string.IsNullOrWhiteSpace(extra)) + { + foreach (string folder in extra.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + foreach (string name in ExtraFileNames) + files.Add(Path.Combine(folder, name)); + } + } + files.AddRange(Candidates); + return files; + } } diff --git a/src/RvtIO/Pdf/PdfTrueTypeFont.cs b/src/RvtIO/Pdf/PdfTrueTypeFont.cs index f67a5e3b..18f6cf2a 100644 --- a/src/RvtIO/Pdf/PdfTrueTypeFont.cs +++ b/src/RvtIO/Pdf/PdfTrueTypeFont.cs @@ -23,49 +23,76 @@ public PdfTrueTypeFont(string path) : this(File.ReadAllBytes(path), Path.GetFile /// Parses TrueType font bytes. /// The font file. /// The PostScript base name written into the PDF. - /// The file is not a TrueType font with a Unicode cmap. + /// The file is not a TrueType font with a Unicode cmap, + /// or its tables are truncated or corrupt. public PdfTrueTypeFont(byte[] file, string name) { ArgumentNullException.ThrowIfNull(file); _file = file; Name = string.IsNullOrWhiteSpace(name) ? "RvtIOFont" : name; - if (file.Length < 12 || BinaryPrimitives.ReadUInt32BigEndian(file) != 0x00010000) - throw new NotSupportedException("Only a TrueType font (0x00010000) is supported."); - - var tables = new Dictionary(StringComparer.Ordinal); - int count = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(4)); - for (int i = 0; i < count; i++) + try { - int at = 12 + i * 16; - string tag = Encoding.ASCII.GetString(file, at, 4); - tables[tag] = ((int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 8)), - (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 12))); + if (file.Length < 12 || BinaryPrimitives.ReadUInt32BigEndian(file) != 0x00010000) + throw new NotSupportedException("Only a TrueType font (0x00010000) is supported."); + + var tables = new Dictionary(StringComparer.Ordinal); + int count = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(4)); + for (int i = 0; i < count; i++) + { + int at = 12 + i * 16; + Need(file, at, 16, "table directory"); + string tag = Encoding.ASCII.GetString(file, at, 4); + int offset = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 8)); + int length = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 12)); + Need(file, offset, length, tag); + tables[tag] = (offset, length); + } + if (!tables.TryGetValue("head", out var head) || !tables.TryGetValue("hhea", out var hhea) + || !tables.TryGetValue("maxp", out var maxp) || !tables.TryGetValue("hmtx", out var hmtx) + || !tables.TryGetValue("cmap", out var cmap)) + throw new NotSupportedException("The font lacks head, hhea, maxp, hmtx or cmap."); + Need(file, head.Offset, 44, "head"); + Need(file, hhea.Offset, 36, "hhea"); + Need(file, maxp.Offset, 6, "maxp"); + Need(file, cmap.Offset, 4, "cmap"); + + _unitsPerEm = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(head.Offset + 18)); + if (_unitsPerEm == 0) throw new NotSupportedException("The font declares no units per em."); + XMin = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 36)); + YMin = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 38)); + XMax = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 40)); + YMax = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 42)); + + Ascent = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(hhea.Offset + 4)); + Descent = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(hhea.Offset + 6)); + int metrics = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(hhea.Offset + 34)); + int glyphs = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(maxp.Offset + 4)); + if (metrics < 1) throw new NotSupportedException("The font has no horizontal metrics."); + Need(file, hmtx.Offset, (long)(metrics - 1) * 4 + 2, "hmtx"); + _advances = new int[Math.Max(glyphs, 1)]; + for (int gid = 0; gid < glyphs; gid++) + { + int at = hmtx.Offset + Math.Min(gid, metrics - 1) * 4; + _advances[gid] = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(at)); + } + + ReadCmap(file, cmap); + if (_glyphByCode.Count == 0) throw new NotSupportedException("The font has no Unicode cmap for the BMP."); } - if (!tables.TryGetValue("head", out var head) || !tables.TryGetValue("hhea", out var hhea) - || !tables.TryGetValue("maxp", out var maxp) || !tables.TryGetValue("hmtx", out var hmtx) - || !tables.TryGetValue("cmap", out var cmap)) - throw new NotSupportedException("The font lacks head, hhea, maxp, hmtx or cmap."); - - _unitsPerEm = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(head.Offset + 18)); - if (_unitsPerEm == 0) throw new NotSupportedException("The font declares no units per em."); - XMin = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 36)); - YMin = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 38)); - XMax = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 40)); - YMax = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(head.Offset + 42)); - - Ascent = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(hhea.Offset + 4)); - Descent = BinaryPrimitives.ReadInt16BigEndian(file.AsSpan(hhea.Offset + 6)); - int metrics = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(hhea.Offset + 34)); - int glyphs = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(maxp.Offset + 4)); - _advances = new int[Math.Max(glyphs, 1)]; - for (int gid = 0; gid < glyphs; gid++) + catch (ArgumentOutOfRangeException exception) { - int at = hmtx.Offset + Math.Min(gid, metrics - 1) * 4; - _advances[gid] = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(at)); + // Every out-of-range table read is a truncated or corrupt font, reported as + // unsupported so a caller trying candidates falls through to the next one. + throw new NotSupportedException("Truncated or corrupt TrueType font.", exception); } + } - ReadCmap(file, cmap); - if (_glyphByCode.Count == 0) throw new NotSupportedException("The font has no Unicode cmap for the BMP."); + /// Requires [offset, offset + length) to lie inside the file. + /// The font is truncated: the table does not fit. + private static void Need(byte[] file, long offset, long length, string what) + { + if (offset < 0 || length < 0 || offset + length > file.Length) + throw new NotSupportedException($"Truncated TrueType font: {what} runs past the end of the file."); } /// The PostScript base name of the font. @@ -99,9 +126,11 @@ private void ReadCmap(byte[] file, (int Offset, int Length) cmap) for (int i = 0; i < count; i++) { int at = cmap.Offset + 4 + i * 8; + Need(file, at, 8, "cmap encoding record"); int platform = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(at)); int encoding = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(at + 2)); int offset = cmap.Offset + (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 4)); + Need(file, offset, 2, "cmap subtable"); int format = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(offset)); int score = (platform, encoding, format) switch { @@ -121,7 +150,9 @@ private void ReadCmap(byte[] file, (int Offset, int Length) cmap) private void ReadFormat4(byte[] file, int at) { + Need(file, at, 8, "cmap format 4 header"); int segments = BinaryPrimitives.ReadUInt16BigEndian(file.AsSpan(at + 6)) / 2; + Need(file, at, 16L + segments * 8L, "cmap format 4"); int endCodes = at + 14; int startCodes = endCodes + segments * 2 + 2; int deltas = startCodes + segments * 2; @@ -154,10 +185,13 @@ private void ReadFormat4(byte[] file, int at) private void ReadFormat12(byte[] file, int at) { - int groups = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 12)); - for (int g = 0; g < groups; g++) + Need(file, at, 16, "cmap format 12 header"); + long groups = BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(at + 12)); + if (groups < 0 || 16 + groups * 12 > file.Length - (long)at) + throw new NotSupportedException("Truncated TrueType font: cmap format 12 lists more groups than fit."); + for (long g = 0; g < groups; g++) { - int entry = at + 16 + g * 12; + int entry = at + 16 + (int)(g * 12); int start = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(entry)); int end = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(entry + 4)); int glyph = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(entry + 8)); diff --git a/tests/RvtIO.Tests/PdfFontsTests.cs b/tests/RvtIO.Tests/PdfFontsTests.cs new file mode 100644 index 00000000..546d1732 --- /dev/null +++ b/tests/RvtIO.Tests/PdfFontsTests.cs @@ -0,0 +1,78 @@ +using RvtIO.Pdf; +using Xunit; + +namespace RvtIO.Tests; + +/// The font search of the print pipeline: metric-compatible substitutes and extra folders. +public class PdfFontsTests +{ + [Fact] + public void CandidateFiles_ListsSubstitutesBeforeDejaVu() + { + var files = PdfFonts.CandidateFiles(_ => null).ToList(); + + int liberation = files.FindIndex(f => f.Contains("LiberationSans-Regular", StringComparison.Ordinal)); + int arimo = files.FindIndex(f => f.Contains("Arimo-Regular", StringComparison.Ordinal)); + int dejavu = files.FindIndex(f => f.Contains("DejaVuSans", StringComparison.Ordinal)); + Assert.True(liberation >= 0 && arimo >= 0 && dejavu >= 0); + Assert.True(liberation < dejavu && arimo < dejavu); + // Debian Bookworm's fonts-liberation2 path, ahead of the older liberation path. + Assert.Contains("/usr/share/fonts/truetype/liberation2/LiberationSans-Regular.ttf", files); + } + + [Fact] + public void CandidateFiles_ExtraFoldersComeFirst() + { + string extra = string.Join(Path.PathSeparator, ["/fonts/a", "/fonts/b"]); + var files = PdfFonts.CandidateFiles(name => name == PdfFonts.FontsEnvVar ? extra : null).ToList(); + + Assert.StartsWith("/fonts/a", files[0]); + Assert.StartsWith("/fonts/b", files[5]); + Assert.True(files.Count > 10); + int systemFirst = files.FindIndex(f => !f.StartsWith("/fonts/", StringComparison.Ordinal)); + Assert.Equal(10, systemFirst); + } + + [Fact] + public void CandidateFiles_EmptyEnvGivesMachineCandidatesOnly() + { + var files = PdfFonts.CandidateFiles(_ => " "); + + Assert.DoesNotContain(files, f => f.StartsWith("/fonts/", StringComparison.Ordinal)); + Assert.Contains(files, f => f.Contains("DejaVuSans", StringComparison.Ordinal)); + } + + [Fact] + public void TruncatedFont_ReportsUnsupportedInsteadOfOverrunningTables() + { + // The review's repro: a 16-byte file with a valid magic and one declared + // but truncated table directory entry. + byte[] truncated = new byte[16]; + truncated[1] = 1; + truncated[5] = 1; + + var error = Assert.Throws(() => new PdfTrueTypeFont(truncated, "bad")); + Assert.Contains("runcated", error.Message, StringComparison.OrdinalIgnoreCase); + } + + [Fact] + public void TryFind_InvalidPreferredFontFallsThroughWithoutThrowing() + { + string folder = Path.Combine(Path.GetTempPath(), "rvtio-bad-font-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(folder); + try + { + byte[] truncated = new byte[16]; + truncated[1] = 1; + truncated[5] = 1; + File.WriteAllBytes(Path.Combine(folder, "arial.ttf"), truncated); + + // Must not throw: a corrupt preferred font is skipped like an unsupported one. + PdfFonts.TryFind(_ => folder, out string? _); + } + finally + { + Directory.Delete(folder, true); + } + } +}