carve-hexapdf 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e542a29c024a850009d9f9fa32d6fe69ba870a97eab23f595522878d7da1c367
4
- data.tar.gz: 05d84eaa30d5ad7c4d6f111dc442f88a113bf8a7aa817849df4ee7b58bb5942f
3
+ metadata.gz: 56039d75c83f496d9038debcc8a7a8ad9a03b331c1f5b6b289381a0d3f24fe4a
4
+ data.tar.gz: 812463d7437234c71d8c76c57a5b37691cefa2f30861ca1bebec5b85805ae176
5
5
  SHA512:
6
- metadata.gz: 1382a76d094f651b2cc1b6cbae0ce65409d27222e02f4355361fce93ef6c9cc873b1ebd58ea01b6f02003ebb112ee8228784bb4dddf0aea60ac7186fc4e48f74
7
- data.tar.gz: 25cd7967c9fe80a6c18df8a2d261c6d27d041a874029c056b52f43f621af5e97f939e3a19f9c141eaa51a87292829445ac58b8074a09a477c8afd61cff31b149
6
+ metadata.gz: 9e4a51e9786486184cd1ee2e1ef33f015a8b35c30643af497a72507012ba4be61819706e93b49df1bc8515f1dadb09ee0f207198ab5d7157e4bf605bdff17825
7
+ data.tar.gz: 2bd43b01f76c7735ddb3e4a3d8b9b10e3997da632c3da4fa7e10e19705c4a5e1ce65d5689926fa758ebdcdc35d99e4002b9307adb6623e953f1fad714fda9c1f
data/CHANGELOG.md CHANGED
@@ -5,7 +5,39 @@ All notable changes to this project are documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [Unreleased]
8
+ ## [0.2.0] - 2026-12-04
9
+
10
+ carve-hexapdf reads the Carve 0.2 tree and needs carve-lang 0.2. Navigation extensions now reach the PDF, and a `{.page-break}` thematic break starts a new page.
11
+
12
+ ### Breaking
13
+
14
+ - Ported to Carve 0.2: the renderer reads the 0.2 tree (AST contract 2.0) and requires carve-lang `>= 0.2.0, < 0.3.0`. Captions are caption nodes on the block they describe (0.2 has no figure node), and divs draw their quoted title (#73).
15
+ - Ruby 3.2 or newer is required, as in carve-lang 0.2 (#87).
16
+ - Include warnings carry the engine's `code` (`include-not-found`, `include-outside-root`, `include-cycle`), and `on_includes:` dependencies carry `path`, `from`, `read` and `file` instead of `resolved` and `denial` (#73).
17
+ - `profile:` no longer changes the parse; a 0.2 profile restricts the HTML render (#73).
18
+
19
+ ### Improvements
20
+
21
+ - `extensions:` selects the engine's standard extensions. The navigation extensions (autolink, heading numbers, permalinks, table of contents) are drawn from the render view (#77, #78).
22
+ - A top-level `{.page-break}` thematic break starts a new page instead of drawing a bar; a break on an empty page adds no blank page (#83).
23
+ - Named snippets are drawn as their value where they are used (#86).
24
+
25
+ ### Release preparation
26
+
27
+ - The 0.2 port moved to main (#74), with engine readiness checks and interim binding pins (#75, #76, #79). The published `carve-lang` 0.2.0 gem replaces those interim pins.
28
+ - Removed the obsolete substitution node from the inline vocabulary (#82) and updated documentation after the branch switch (#84). The release changelog was completed against the merged work (#88).
29
+
30
+ **Full Changelog**: https://github.com/markup-carve/carve-hexapdf/compare/v0.1.7...v0.2.0
31
+
32
+ ## [0.1.7] - 2026-11-12
33
+
34
+ The default fonts no longer crash the render: `x <= 10` and CJK text now produce a PDF.
35
+
36
+ ### Fixes
37
+
38
+ - Text the font cannot draw no longer aborts the render. With the default Type1 fonts, `<=`, `>=` and `!=` print as written instead of `≤ ≥ ≠`, and other characters without a glyph print as `?` with a warning; a TrueType `base_font:` draws them (#70, #71).
39
+
40
+ **Full Changelog**: https://github.com/markup-carve/carve-hexapdf/compare/v0.1.6...v0.1.7
9
41
 
10
42
  ## [0.1.6] - 2026-11-12
11
43
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # carve-hexapdf
2
2
 
3
- Ruby PDF renderer for Carve documents. It supports `carve-lang` 0.1.1 or later and uses the HexaPDF composition engine. See the [Carve documentation](https://markup-carve.github.io/carve/).
3
+ Ruby PDF renderer for Carve 0.2 documents. It reads the 0.2 tree from the published `carve-lang` gem and uses the HexaPDF composition engine. See the [Carve documentation](https://markup-carve.github.io/carve/).
4
4
 
5
5
  ## Install
6
6
 
@@ -61,6 +61,8 @@ module Carve
61
61
  highlight_color: highlight_color,
62
62
  styles: styles))
63
63
  @renderers = renderers || {}
64
+ @drawable = {}
65
+ @warned = {}
64
66
  end
65
67
 
66
68
  def render_document(doc)
@@ -76,6 +78,9 @@ module Carve
76
78
  # ---- block dispatch ------------------------------------------------
77
79
 
78
80
  def block(node, target)
81
+ # 0.2 has no figure node: a caption belongs to the block it describes.
82
+ return captioned(node, target) if node[:caption] && !%w[table figure_group].include?(node[:type])
83
+
79
84
  case node[:type]
80
85
  # A definition is RELOCATED, not rendered in place - it belongs in the
81
86
  # endnote section, which is what the HTML renderer does with it too.
@@ -90,16 +95,17 @@ module Carve
90
95
  when "list" then list(node, target)
91
96
  when "block_quote" then block_quote(node, target)
92
97
  when "table" then table(node, target)
93
- when "thematic_break" then thematic_break(target)
94
- when "div" then container_of(node[:children], target)
98
+ when "thematic_break" then thematic_break(node, target)
99
+ when "div" then div(node, target)
95
100
  when "admonition" then admonition(node, target)
96
101
  when "definition_list" then definition_list(node, target)
97
- when "figure" then figure(node, target)
98
102
  when "figure_group" then figure_group(node, target)
103
+ when "section" then Array(node[:children]).each { |ch| block(ch, target) }
104
+ when "include" then include_node(node, target)
99
105
  when "block_image", "image" then image_block(node, target)
100
- when "block_extension" then container_of(node[:children], target)
101
- when "raw_block", "comment", "abbreviation_def"
102
- # No meaningful PDF form - drop.
106
+ when "block_extension" then container_of(extension_content(node), target)
107
+ when "raw_block", "comment", "abbreviation_def", "snippet_definition"
108
+ # No meaningful PDF form - drop. A snippet definition renders nothing (C2-H-214).
103
109
  else
104
110
  if inline_children?(node[:children])
105
111
  emit_paragraph(node[:children], target)
@@ -160,8 +166,12 @@ module Carve
160
166
  end
161
167
 
162
168
  def paragraph(node, target)
163
- # A paragraph that is only a display-math node becomes a centered image.
164
169
  children = Array(node[:children])
170
+ # An image paragraph (C2-A `role: "image"`) draws its images as blocks.
171
+ if node[:role] == "image" && children.all? { |ch| ch[:type] == "image" }
172
+ return children.each { |image| image_block(image, target) }
173
+ end
174
+ # A paragraph that is only a display-math node becomes a centered image.
165
175
  if children.size == 1 && children.first[:type] == "math" && children.first[:display]
166
176
  return display_math(children.first, target)
167
177
  end
@@ -178,15 +188,15 @@ module Carve
178
188
  end
179
189
 
180
190
  def code_block(node, target)
181
- lang = node[:lang].to_s.downcase
182
- if (key = DIAGRAM_LANGS[lang]) && (bytes = call_renderer(key, node[:content].to_s))
191
+ lang = node[:syntax].to_s.downcase
192
+ if (key = DIAGRAM_LANGS[lang]) && (bytes = call_renderer(key, node[:value].to_s))
183
193
  return image_bytes(bytes, target)
184
194
  end
185
195
 
186
- content = resolve_nbsp(node[:content]).chomp
196
+ content = resolve_nbsp(node[:value]).chomp
187
197
  style = style_for("code.block", inherited_font: true, with_box: true)
188
198
  box = style.delete(:box)
189
- target.text(content, **style, box_style: box)
199
+ target.text(drawable_text(content, style[:font] || code_font), **style, box_style: box)
190
200
  end
191
201
 
192
202
  def list(node, target)
@@ -194,15 +204,16 @@ module Carve
194
204
  # paragraph chain, and Composer#list rejects text keywords (:font,
195
205
  # :font_size, ...) that the chain inherits from base - whitelist.
196
206
  style = style_for("list").slice(:item_spacing, :content_indentation)
197
- items = Array(node[:items])
198
- ordered = node[:ordered]
199
- task = items.any? { |it| !it[:checked].nil? }
200
- start = node[:start] || 1
207
+ items = Array(node[:children])
208
+ marker_info = node[:marker] || {}
209
+ ordered = marker_info[:kind] == "ordered"
210
+ task = items.any? { |it| it[:task] }
211
+ start = marker_info[:start] || 1
201
212
  marker = if task
202
213
  # Standard PDF fonts have no checkbox glyphs; draw an ASCII
203
214
  # checkbox in the marker column so item text (and nested
204
215
  # lists) align like any other list.
205
- checked = items.map { |it| it[:checked] }
216
+ checked = items.map { |it| it[:task] && it[:task][:mark].to_s.strip != "" }
206
217
  font = style_for("paragraph", inherited_font: true)[:font] || "Times"
207
218
  ->(doc, list_box, index) do
208
219
  # A list split across pages continues in a box whose
@@ -230,9 +241,17 @@ module Carve
230
241
  def block_quote(node, target)
231
242
  target.container(style: style_for("quote", with_box: true)[:box]) do |cont|
232
243
  Array(node[:children]).each { |ch| block(ch, cont) }
233
- if node[:attribution]
234
- emit_paragraph(node[:attribution], cont, margin: [2, 0, 0])
235
- end
244
+ end
245
+ end
246
+
247
+ # A div's quoted title (C2-B-120) is drawn before its body.
248
+ def div(node, target)
249
+ title = Array(node[:title])
250
+ return container_of(node[:children], target) if title.empty?
251
+
252
+ target.container(style: { margin: [0, 0, BLOCK_GAP] }) do |cont|
253
+ cont.formatted_text(inline_runs(title, bold: true), margin: [0, 0, 2])
254
+ Array(node[:children]).each { |ch| block(ch, cont) }
236
255
  end
237
256
  end
238
257
 
@@ -257,9 +276,9 @@ module Carve
257
276
  def definition_list(node, target)
258
277
  style = style_for("definition_list", with_box: true)
259
278
  target.container(style: style[:box]) do |cont|
260
- # `items` is a flat run of definition_term / definition_description
279
+ # `children` is a flat run of definition_term / definition_description
261
280
  # nodes, not term/definition pairs.
262
- Array(node[:items]).each do |item|
281
+ Array(node[:children]).each do |item|
263
282
  case item[:type]
264
283
  when "definition_term"
265
284
  cont.formatted_text(inline_runs(item[:children], bold: true), margin: [2, 0, 1])
@@ -272,23 +291,52 @@ module Carve
272
291
  end
273
292
  end
274
293
 
275
- def figure(node, target)
276
- block(node[:target], target) if node[:target]
277
- if node[:caption] && !node[:caption].empty?
278
- style = text_style("figure.caption")
279
- target.formatted_text(inline_runs(node[:caption], italic: true, font_family: style[:font]),
280
- **style)
294
+ # A captioned block: the block as it stands, then its caption.
295
+ def captioned(node, target)
296
+ block(node.reject { |key, _| key == :caption }, target)
297
+ caption = caption_inlines(node[:caption])
298
+ return if caption.empty?
299
+
300
+ style = text_style("figure.caption")
301
+ target.formatted_text(inline_runs(caption, italic: true, font_family: style[:font]), **style)
302
+ end
303
+
304
+ # A caption node holds blocks; a caption line is drawn from their inlines.
305
+ def caption_inlines(caption)
306
+ return [] unless caption.is_a?(Hash)
307
+
308
+ Array(caption[:children]).each_with_index.flat_map do |blk, index|
309
+ lead = index.positive? ? [{ type: "text", value: " " }] : []
310
+ lead + (blk[:type] == "paragraph" ? Array(blk[:children]) : [])
311
+ end
312
+ end
313
+
314
+ # An expanded include stands for its blocks; an unexpanded one is the
315
+ # directive as text, as the HTML writer renders it (C2-N-070).
316
+ def include_node(node, target)
317
+ if node[:children]
318
+ Array(node[:children]).each { |ch| block(ch, target) }
319
+ else
320
+ emit_paragraph([{ type: "text", value: "{{ #{node[:path]} }}" }], target)
281
321
  end
282
322
  end
283
323
 
324
+ # An extension this renderer does not implement prints the fallback it
325
+ # promised (C2-X-023), or its children when it has none.
326
+ def extension_content(node)
327
+ children = Array(node[:children])
328
+ children.empty? ? Array(node[:fallback]) : children
329
+ end
330
+
284
331
  # A composite figure (PART 9 section 4c) is ONE float. Its panels, the
285
332
  # stray content preserved between them and the group caption are laid out
286
333
  # as a single box that a page break may not enter, so the caption cannot
287
334
  # be stranded from the panels it numbers, nor a panel from its own
288
335
  # caption.
289
336
  #
290
- # PANELS ARE THE `figure` AND `table` CHILDREN, in source order; every
291
- # other child is plain group content and is drawn IN PLACE between them.
337
+ # PANELS ARE THE CAPTIONED CHILDREN (an image paragraph, a table), in
338
+ # source order; every other child is plain group content and is drawn IN
339
+ # PLACE between them.
292
340
  # Nothing here re-attaches or drops it.
293
341
  #
294
342
  # THE GROUP CAPTION IS ALREADY NUMBERED when it arrives: the number is a
@@ -299,9 +347,10 @@ module Carve
299
347
  style = style_for("figure.group", with_box: true)
300
348
  panels = ::HexaPDF::Document::Layout::ChildrenCollector.collect(@layout) do |cont|
301
349
  Array(node[:children]).each do |child|
302
- case child[:type]
303
- when "figure", "table" then panel(child, cont)
304
- else block(child, cont)
350
+ if child[:type] == "table" || child[:caption]
351
+ panel(child, cont)
352
+ else
353
+ block(child, cont)
305
354
  end
306
355
  end
307
356
  end
@@ -311,7 +360,8 @@ module Carve
311
360
  else
312
361
  panels
313
362
  end
314
- body += [group_caption_box(node[:caption])] if node[:caption] && !node[:caption].empty?
363
+ caption = caption_inlines(node[:caption])
364
+ body += [group_caption_box(caption)] unless caption.empty?
315
365
  emit_box(keep_together(body, style: style[:box]), target)
316
366
  end
317
367
 
@@ -321,7 +371,7 @@ module Carve
321
371
  # the panel wrapper adds nothing to it.
322
372
  def panel(node, target)
323
373
  children = ::HexaPDF::Document::Layout::ChildrenCollector.collect(@layout) do |cont|
324
- node[:type] == "table" ? table(node, cont) : figure(node, cont)
374
+ node[:type] == "table" ? table(node, cont) : captioned(node, cont)
325
375
  end
326
376
  emit_box(keep_together(children), target)
327
377
  end
@@ -386,28 +436,45 @@ module Carve
386
436
  else
387
437
  alt = resolve_nbsp(node[:alt])
388
438
  alt = "[image: #{node[:src]}]" if alt.empty?
389
- target.formatted_text([{ text: alt, font: [base_font, { variant: :italic }] }],
439
+ font = [base_font, { variant: :italic }]
440
+ target.formatted_text([{ text: drawable_text(alt, font), font: font }],
390
441
  margin: [0, 0, BLOCK_GAP])
391
442
  end
392
443
  end
393
444
 
394
445
  def display_math(node, target)
395
- if (bytes = call_renderer(:math, node[:content].to_s, true))
446
+ if (bytes = call_renderer(:math, node[:value].to_s, true))
396
447
  return image_bytes(bytes, target, align: :center)
397
448
  end
398
449
 
399
450
  style = style_for("math", with_box: true)
400
451
  box = style.delete(:box)
401
452
  style[:font] ||= code_font
402
- target.text(node[:content].to_s, **style, text_align: :center, box_style: box)
453
+ target.text(drawable_text(node[:value].to_s, style[:font]), **style,
454
+ text_align: :center, box_style: box)
403
455
  end
404
456
 
405
- def thematic_break(target)
457
+ # `{.page-break}` on `***` is the page-break convention (carve#6793). Only
458
+ # the composer can start a page; inside a container the break draws
459
+ # nothing, and on a page with nothing on it yet it adds no blank page.
460
+ def thematic_break(node, target)
461
+ attrs = node[:attrs]
462
+ if attrs.is_a?(Hash) && Array(attrs[:classes]).map(&:to_s).include?("page-break")
463
+ target.new_page if target.respond_to?(:new_page) && !page_empty?(target)
464
+ return
465
+ end
466
+
406
467
  style = style_for("thematic_break")
407
468
  height = style.delete(:height)
408
469
  target.box(:base, height: height, style: style)
409
470
  end
410
471
 
472
+ # Nothing drawn yet: the frame cannot tell, since a float or an unmasked
473
+ # box draws without moving +y+ or cutting the frame shape.
474
+ def page_empty?(composer)
475
+ composer.canvas.contents.empty?
476
+ end
477
+
411
478
  # Draw a rendered image ({bytes:, width:, height:} or raw bytes) as a
412
479
  # block image. :width/:height are box constructor arguments in HexaPDF,
413
480
  # not style properties.
@@ -426,13 +493,28 @@ module Carve
426
493
 
427
494
  # ---- tables (with row/col spans) -----------------------------------
428
495
 
496
+ # The rows of a table and the number in its head group (C2-A table).
497
+ def table_rows(node)
498
+ rows = []
499
+ head = 0
500
+ Array(node[:children]).each do |group|
501
+ Array(group[:children]).each do |row|
502
+ next unless row[:type] == "table_row"
503
+
504
+ rows << row
505
+ head += 1 if group[:type] == "table_head"
506
+ end
507
+ end
508
+ [rows, head]
509
+ end
510
+
429
511
  def table(node, target)
430
- resolved = resolve_spans(Array(node[:rows]))
512
+ rows, head_rows = table_rows(node)
513
+ resolved = resolve_spans(rows)
431
514
  return if resolved.empty?
432
515
 
433
- # HexaPDF cannot repeat a header whose cell spans into the body.
434
- header_count = resolved.first.any? { |o| o[:header] } &&
435
- resolved.first.none? { |o| o[:row_span] > 1 } ? 1 : 0
516
+ # HexaPDF repeats one header row, and not one whose cell spans into the body.
517
+ header_count = head_rows.positive? && resolved.first.none? { |o| o[:row_span] > 1 } ? 1 : 0
436
518
  table_style = style_for("table")
437
519
  header_style = style_for("table.header")
438
520
  columns = column_alignments(node)
@@ -440,7 +522,7 @@ module Carve
440
522
  cell_boxes = resolved.map do |row|
441
523
  row.map do |o|
442
524
  cell_style = o[:header] ? table_style.merge(header_style) : table_style
443
- runs = inline_runs(o[:cell][:children], bold: o[:header],
525
+ runs = inline_runs(cell_inlines(o[:cell]), bold: o[:header],
444
526
  font_family: cell_style[:font])
445
527
  runs = [{ text: "" }] if runs.empty?
446
528
  # :margin belongs to the table box, :cell_padding is our pseudo-prop.
@@ -462,10 +544,19 @@ module Carve
462
544
  body = [[{ content: @layout.text_box("") }]] if body.nil? || body.empty?
463
545
 
464
546
  target.table(body, header: header, margin: table_style[:margin])
465
- if node[:caption] && !node[:caption].empty?
466
- style = text_style("table.caption")
467
- target.formatted_text(inline_runs(node[:caption], italic: true, font_family: style[:font]),
468
- **style)
547
+ caption = caption_inlines(node[:caption])
548
+ return if caption.empty?
549
+
550
+ style = text_style("table.caption")
551
+ target.formatted_text(inline_runs(caption, italic: true, font_family: style[:font]), **style)
552
+ end
553
+
554
+ # A cell holds cell paragraphs (or blocks); a table cell draws one run of
555
+ # their inlines.
556
+ def cell_inlines(cell)
557
+ Array(cell[:children]).each_with_index.flat_map do |blk, index|
558
+ lead = index.positive? ? [{ type: "text", value: " " }] : []
559
+ lead + (%w[cell_paragraph paragraph].include?(blk[:type]) ? Array(blk[:children]) : [blk])
469
560
  end
470
561
  end
471
562
 
@@ -473,16 +564,14 @@ module Carve
473
564
 
474
565
  # A cell without its own alignment inherits its column's: the table's
475
566
  # `columns` metadata, overridden by the head rows (the HTML `thead`), a
476
- # later row winning. Any other cell's alignment is local. The head is
477
- # `rowGroups.headRows` when published, else the leading header rows.
567
+ # later row winning. Any other cell's alignment is local. The head is the
568
+ # table's head group.
478
569
  # Vertical alignment is not drawn: `text_valign` overflows a table cell.
479
570
  def column_alignments(node)
480
- columns = Array(node[:columns]).map { |c| (c || {}).slice(:align) }
481
- rows = Array(node[:rows])
482
- declared = node.dig(:rowGroups, :headRows)
483
- head = declared.is_a?(Integer) ? rows.first(declared) : rows.take_while { |row| header_row?(row) }
484
- head.each do |row|
485
- Array(row[:cells]).each_with_index do |cell, col|
571
+ columns = Array(node[:columns]).map { |c| { align: (c || {})[:align] || (c || {})[:resolvedAlign] }.compact }
572
+ rows, head_rows = table_rows(node)
573
+ rows.first(head_rows).each do |row|
574
+ Array(row[:children]).each_with_index do |cell, col|
486
575
  next if cell[:span]
487
576
 
488
577
  (columns[col] ||= {})[:align] = cell[:align] if cell[:align]
@@ -491,12 +580,6 @@ module Carve
491
580
  columns
492
581
  end
493
582
 
494
- # A `^` / `<` marker is not a header cell, but leaves its row in the head.
495
- def header_row?(row)
496
- content = Array(row[:cells]).reject { |cell| cell[:span] }
497
- !content.empty? && content.all? { |cell| cell[:header] }
498
- end
499
-
500
583
  # Resolve Carve's explicit span markers (a `<` cell = merge left, a `^`
501
584
  # cell = merge up) into per-cell col_span / row_span counts, returning
502
585
  # rows of originator hashes {cell:, header:, col:, col_span:, row_span:} with
@@ -509,7 +592,7 @@ module Carve
509
592
  emitted = []
510
593
  last = nil
511
594
  bumped = {} # originators already row-extended in THIS row (by object id)
512
- Array(row[:cells]).each_with_index do |cell, col|
595
+ Array(row[:children]).each_with_index do |cell, col|
513
596
  case cell[:span]
514
597
  when "colspan"
515
598
  owner = last || col_owner[col - 1]
@@ -562,8 +645,6 @@ module Carve
562
645
  case node[:type]
563
646
  when "text" then out << run(node[:value].to_s, ctx)
564
647
  when "soft_break" then out << run(" ", ctx)
565
- # A `+` continuation row join; the spec renders it as one space.
566
- when "cell_wrap" then out << run(" ", ctx)
567
648
  when "hard_break" then out << { text: "\n" }
568
649
  # Each emphasis sort is its OWN node type. They used to be one
569
650
  # `emphasis` node carrying a `kind`, so a profile could not deny bold
@@ -595,53 +676,39 @@ module Carve
595
676
  # dropped. There is no wrapper in a PDF text run, so what is left is the
596
677
  # content, unstyled: carve-js renders `A !`raw span` B` as `A raw span B`
597
678
  # in plain text.
598
- when "literal_inline" then out << run(node[:content].to_s, ctx)
679
+ when "literal_inline" then out << run(node[:value].to_s, ctx)
599
680
  # The number in "Figure 1:". Without this the caption renders without it.
600
681
  when "caption_number" then out << run(node[:n].to_s, ctx)
601
682
  # `:name[content]` - the content is an inline array, and the extension's
602
683
  # own presentation has no PDF form, so emit what it wraps. carve-js
603
684
  # renders `:kbd[Ctrl]` as `Ctrl` in plain text.
604
685
  when "inline_extension" then inline_extension(node, ctx, out)
605
- when "mention" then out << run("@#{node[:user]}", ctx)
686
+ when "mention" then out << run("@#{node[:target]}", ctx)
606
687
  when "tag" then out << run("##{node[:name]}", ctx)
607
688
  # `footnote_ref` is `[^label]` and `inline_footnote` is `^[body]`.
608
689
  # Both used to publish as `footnote`, which is the BLOCK definition's
609
690
  # type - one identifier for three constructs, so nothing could tell them
610
691
  # apart (carve-rb#19). The old name is still accepted: a tree stored by
611
692
  # an older version can still be rendered.
612
- when "footnote_ref", "inline_footnote", "footnote"
693
+ when "footnote_ref", "inline_footnote"
613
694
  number = register_footnote(node)
614
695
  out << run("[#{number}]", ctx.merge(super: true)) if number
615
696
  when "citation_group" then out << run(node[:raw].to_s, ctx)
616
- when "abbreviation" then out << run(node[:abbr].to_s, ctx)
617
- # `heading_ref` is what `</#id>` publishes; `cross_ref` was its name
618
- # before the rename and is still accepted, like the footnote arm above.
619
- # Reading only `cross_ref` dropped every cross-reference from the page
620
- # with no marker left behind: `See </#Plan>.` drew "See .".
621
- #
622
- # A resolved reference carries `href` and a `target` that is the target
623
- # heading's TEXT, so it draws as a link over that text. An unresolved
624
- # one carries no `href` and a `target` that is the selector as authored,
625
- # which the HTML target renders as literal source - so it is drawn back
626
- # in its source spelling rather than as a bare word that reads like
627
- # prose.
628
- when "heading_ref", "cross_ref"
629
- if node[:href].to_s.empty?
630
- out << run("</##{node[:target]}>", ctx)
697
+ when "abbreviation" then out << run(node[:key].to_s, ctx)
698
+ # A cross-reference (`</#id>`). A resolved one carries `href` and draws
699
+ # as a link over its key; an unresolved one has no `href` and is drawn
700
+ # in its source spelling, as the HTML writer renders it.
701
+ when "xref"
702
+ if node[:namespace] == "footnote"
703
+ number = register_footnote(node)
704
+ out << run("[#{number}]", ctx.merge(super: true)) if number
705
+ elsif node[:href].to_s.empty?
706
+ out << run("</##{node[:key]}>", ctx)
631
707
  else
632
- out << run(node[:target].to_s, ctx.merge(link: node[:href].to_s))
708
+ out << run(node[:key].to_s, ctx.merge(link: node[:href].to_s))
633
709
  end
634
- when "caption_number" then (out << run(node[:number].to_s, ctx) if node[:number])
635
710
  when "insert" then emit_children(node, ctx.merge(underline: true), out)
636
711
  when "delete" then emit_children(node, ctx.merge(strike: true), out)
637
- # Both halves are inline arrays, and both reach the page: struck for the
638
- # replaced run, underlined for the replacement, which is what the
639
- # reference ANSI and HTML renderers show.
640
- # carve-lang 0.1.3 and older publish the halves as `oldText`/`newText`
641
- # strings; the gemspec floor still admits those engines.
642
- when "substitution"
643
- emit_half(node[:old], node[:oldText], ctx.merge(strike: true), out)
644
- emit_half(node[:new], node[:newText], ctx.merge(underline: true), out)
645
712
  when "ruby"
646
713
  Array(node[:pairs]).each do |pair|
647
714
  Array(pair[:base]).each { |part| emit_inline(part, ctx, out) }
@@ -649,12 +716,8 @@ module Carve
649
716
  Array(pair[:annotation]).each { |part| emit_inline(part, ctx, out) }
650
717
  out << run(")", ctx)
651
718
  end
652
- when "smart_punctuation" then out << run(smart_punctuation_text(node), ctx)
653
- # A character the author escaped. The backslash is authoring syntax, so
654
- # the page shows the character - but the node has no children, so
655
- # without this arm it falls through to the branch that emits children
656
- # and renders nothing at all (carve#350, carve#355).
657
- when "escaped_text" then out << run(node[:value].to_s, ctx)
719
+ when "smart_punctuation"
720
+ out << run(smart_punctuation_text(node), ctx, fallback: node[:source])
658
721
  when "raw_inline", "critic_comment"
659
722
  # No safe PDF form - drop.
660
723
  else
@@ -698,21 +761,13 @@ module Carve
698
761
  glyph = node[:glyph]
699
762
  return glyph.to_s unless glyph.nil? || glyph.to_s.empty?
700
763
 
701
- SMART_PUNCTUATION_GLYPHS.fetch(node[:kind].to_s) { node[:value].to_s }
764
+ SMART_PUNCTUATION_GLYPHS.fetch(node[:kind].to_s) { node[:source].to_s }
702
765
  end
703
766
 
704
767
  def emit_children(node, ctx, out)
705
768
  Array(node[:children]).each { |c| emit_inline(c, ctx, out) }
706
769
  end
707
770
 
708
- def emit_half(inlines, text, ctx, out)
709
- if inlines.nil? && text
710
- out << run(text.to_s, ctx)
711
- else
712
- emit_children({ children: inlines }, ctx, out)
713
- end
714
- end
715
-
716
771
  # Styling for one emphasis node.
717
772
  #
718
773
  # `/*both*/` is a single `strong` node carrying `boldItalic`, not a
@@ -739,15 +794,9 @@ module Carve
739
794
 
740
795
  # Build one formatted-text run hash for +text+ under styling +ctx+.
741
796
  # `:name[content]` - the extension's own presentation has no PDF form, so
742
- # emit what it wraps. `content` is an inline ARRAY in a parsed tree; a
743
- # hand-built one may hold a bare string, and `render_ast` is public, so
744
- # take both rather than raising on the second.
797
+ # emit what it wraps, or the fallback it promised when it wraps nothing.
745
798
  def inline_extension(node, ctx, out)
746
- content = node[:content]
747
- case content
748
- when Array then emit_children({ children: content }, ctx, out)
749
- when String then out << run(content, ctx)
750
- end
799
+ emit_children({ children: extension_content(node) }, ctx, out)
751
800
  end
752
801
 
753
802
  # U+E000 STANDS FOR a no-break space the parser resolved - from an escaped
@@ -767,7 +816,9 @@ module Carve
767
816
  text.to_s.gsub(RESOLVED_NBSP, NBSP)
768
817
  end
769
818
 
770
- def run(text, ctx)
819
+ # +fallback+ is the author's spelling of the same text, drawn instead when
820
+ # the run font cannot draw +text+ (a smart-typography glyph).
821
+ def run(text, ctx, fallback: nil)
771
822
  item = { text: resolve_nbsp(text) }
772
823
  if ctx[:code]
773
824
  # :box is a block-level pseudo-property (e.g. inherited from a user
@@ -793,9 +844,77 @@ module Carve
793
844
  item[:link] = ctx[:link]
794
845
  item.merge!(run_style("link"))
795
846
  end
847
+ font = item[:font] || ctx[:font_family] || base_font
848
+ item[:text] = drawable_text(item[:text], font, fallback: fallback)
796
849
  item
797
850
  end
798
851
 
852
+ # A character the font cannot encode raises inside HexaPDF and loses the
853
+ # whole document (carve-hexapdf#70). That is either a missing glyph (CJK
854
+ # in Times) or a glyph the font's encoding cannot reach (standard Type1
855
+ # Times has `lessequal`, WinAnsi has no slot for it).
856
+ def drawable_text(text, font, fallback: nil)
857
+ wrapper = font_wrapper(font)
858
+ return text if wrapper.nil? || text.each_char.all? { |ch| drawable?(wrapper, ch) }
859
+
860
+ if fallback && fallback.to_s.each_char.all? { |ch| drawable?(wrapper, ch) }
861
+ return fallback.to_s
862
+ end
863
+
864
+ text.each_char.map do |ch|
865
+ next ch if drawable?(wrapper, ch)
866
+
867
+ warn_missing_glyph(wrapper, ch)
868
+ drawable?(wrapper, REPLACEMENT) ? REPLACEMENT : ""
869
+ end.join
870
+ end
871
+
872
+ REPLACEMENT = "?"
873
+
874
+ def font_wrapper(font)
875
+ return font if font.respond_to?(:decode_codepoint)
876
+
877
+ name, opts = font.is_a?(Array) ? font : [font, {}]
878
+ return name if name.respond_to?(:decode_codepoint)
879
+
880
+ document = @c.document
881
+ name ||= document.config["font.default"]
882
+ document.fonts.add(name, variant: (opts || {})[:variant] || :none)
883
+ rescue StandardError
884
+ nil
885
+ end
886
+
887
+ # Mirrors HexaPDF: decode, let `font.on_invalid_glyph` (and so any
888
+ # configured `font.fallback`) supply a missing glyph, then encode. Tabs,
889
+ # line breaks and the like are layout controls, never drawn.
890
+ def drawable?(wrapper, char)
891
+ key = [wrapper, char]
892
+ return @drawable[key] if @drawable.key?(key)
893
+
894
+ @drawable[key] = encodable?(wrapper, char)
895
+ end
896
+
897
+ def encodable?(wrapper, char)
898
+ glyphs = [wrapper.decode_codepoint(char.ord)]
899
+ if glyphs.first.is_a?(::HexaPDF::Font::InvalidGlyph)
900
+ return true if glyphs.first.control_char?
901
+
902
+ glyphs = @c.document.config["font.on_invalid_glyph"].call(char.ord, glyphs.first)
903
+ end
904
+ glyphs.all? { |g| !g.is_a?(::HexaPDF::Font::InvalidGlyph) && g.font_wrapper.encode(g) }
905
+ rescue ::HexaPDF::Error
906
+ false
907
+ end
908
+
909
+ def warn_missing_glyph(wrapper, char)
910
+ return if @warned[[wrapper, char]]
911
+
912
+ @warned[[wrapper, char]] = true
913
+ name = wrapper.wrapped_font.full_name rescue wrapper.to_s
914
+ warn format("carve-hexapdf: font '%s' has no glyph for U+%04X %p; drawing %p instead",
915
+ name, char.ord, char, REPLACEMENT)
916
+ end
917
+
799
918
  # Link/highlight styles are decoration overlays on a run that may already
800
919
  # carry a variant or code font; the font they inherit from +base+ must not
801
920
  # clobber it (only a font set explicitly on the key itself wins), and a
@@ -829,8 +948,8 @@ module Carve
829
948
  def walk_footnote_defs(node, defs)
830
949
  return unless node.is_a?(Hash)
831
950
 
832
- if node[:type] == "footnote" && node[:label]
833
- defs[node[:label].to_s] ||= Array(node[:children])
951
+ if node[:type] == "footnote" && node[:key]
952
+ defs[node[:key].to_s] ||= Array(node[:children])
834
953
  end
835
954
 
836
955
  node.each_value do |value|
@@ -845,17 +964,12 @@ module Carve
845
964
  # when there is nothing to number (unresolvable reference). Repeated
846
965
  # references to the same definition share one number and one endnote.
847
966
  def register_footnote(node)
848
- if node[:inline]
849
- @footnotes << { number: @footnotes.size + 1, inline: node[:inline] }
967
+ if node[:type] == "inline_footnote"
968
+ @footnotes << { number: @footnotes.size + 1, inline: Array(node[:children]) }
850
969
  return @footnotes.last[:number]
851
970
  end
852
971
 
853
- # `label` since carve-rb 0.1.4; `id` is the pre-rename spelling and is
854
- # still accepted, the same way the node-type arms above accept both
855
- # vocabularies. Reading only `id` silently dropped the body: the marker
856
- # still numbered itself from `node[:number]`, so the page kept `[2]`
857
- # and lost "2. Referenced note body." entirely.
858
- id = node[:label] || node[:id]
972
+ id = node[:key]&.to_s
859
973
  if id && @footnote_defs.key?(id)
860
974
  return @footnote_numbers[id] ||= begin
861
975
  @footnotes << { number: @footnotes.size + 1, blocks: @footnote_defs[id] }
@@ -904,16 +1018,16 @@ module Carve
904
1018
  end
905
1019
 
906
1020
  def inline_math(node, ctx, out)
907
- if (img = call_renderer(:math, node[:content].to_s, !!node[:display]))
1021
+ if (img = call_renderer(:math, node[:value].to_s, !!node[:display]))
908
1022
  spec = { box: [:image, StringIO.new(img[:bytes])],
909
1023
  height: img[:height] || 11, valign: :baseline }
910
1024
  spec[:width] = img[:width] if img[:width]
911
1025
  out << spec
912
1026
  else
913
- out << run(node[:content].to_s, ctx.merge(code: true))
1027
+ out << run(node[:value].to_s, ctx.merge(code: true))
914
1028
  end
915
1029
  rescue StandardError
916
- out << run(node[:content].to_s, ctx.merge(code: true))
1030
+ out << run(node[:value].to_s, ctx.merge(code: true))
917
1031
  end
918
1032
 
919
1033
  def inline_image(node, ctx, out)
@@ -971,7 +1085,7 @@ module Carve
971
1085
  text emphasis code link image span math raw_inline emoji autolink
972
1086
  cross_ref caption_number mention tag citation_group inline_extension
973
1087
  abbreviation footnote soft_break hard_break insert delete
974
- substitution critic_comment
1088
+ critic_comment
975
1089
  ].freeze
976
1090
  end
977
1091
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Carve
4
4
  module Hexapdf
5
- VERSION = "0.1.6"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/lib/carve/hexapdf.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "hexapdf"
4
4
  require "carve"
5
+ require "json"
5
6
 
6
7
  require_relative "hexapdf/version"
7
8
  require_relative "hexapdf/style_map"
@@ -25,6 +26,9 @@ module Carve
25
26
  # (MIT) only bridges to it; your use of HexaPDF is governed by HexaPDF's own
26
27
  # terms.
27
28
  module Hexapdf
29
+ # Raised when the installed engine does not produce the Carve 0.2 tree.
30
+ class EngineError < StandardError; end
31
+
28
32
  class << self
29
33
  # Render Carve +source+ to a PDF and return the document as a binary
30
34
  # String.
@@ -80,7 +84,7 @@ module Carve
80
84
  composer = ::HexaPDF::Composer.new(page_size: page_size, margin: margin)
81
85
  Renderer.new(composer, base_font: base_font, code_font: code_font,
82
86
  link_color: link_color, highlight_color: highlight_color,
83
- styles: styles, renderers: renderers).render_document(ast)
87
+ styles: styles, renderers: renderers).render_document(expand_snippets(ast))
84
88
  composer.write_to_string
85
89
  end
86
90
 
@@ -109,7 +113,7 @@ module Carve
109
113
  "pass include_root: and source_path:, or drop them"
110
114
  end
111
115
 
112
- return ::Carve.parse(source)
116
+ return require_tree_v2(::Carve.parse(source))
113
117
  end
114
118
 
115
119
  result = ::Carve.parse_with_includes(source, root: include_root,
@@ -117,7 +121,50 @@ module Carve
117
121
  extensions: extensions,
118
122
  profile: profile)
119
123
  report_includes(result, on_includes)
120
- result[:value]
124
+ tree = require_tree_v2(result[:value])
125
+ return tree if Array(extensions).empty?
126
+
127
+ render_view(tree, extensions)
128
+ end
129
+
130
+ # A snippet use has no children: its copy exists only in the render
131
+ # view (C2-N-091, C2-A-221), so a tree that holds one is drawn from there.
132
+ def expand_snippets(tree)
133
+ snippet_use?(tree) ? render_view(tree, nil) : tree
134
+ end
135
+
136
+ def snippet_use?(node)
137
+ case node
138
+ when Hash then node[:type] == "snippet_use" || node.each_value.any? { |value| snippet_use?(value) }
139
+ when Array then node.any? { |value| snippet_use?(value) }
140
+ else false
141
+ end
142
+ end
143
+
144
+ # The view the HTML renderer would draw. Document transforms (heading
145
+ # numbers, autolink, the table of contents) edit that view only, never
146
+ # the parse tree (C2-X-053), so a host transform registered after them
147
+ # hands it back. The expanded tree goes in, so the drawn document is the
148
+ # one the include report describes.
149
+ def render_view(tree, extensions)
150
+ view = nil
151
+ capture = ->(document) { view = document }
152
+ ::Carve.render_with_host(tree: tree,
153
+ options: { extensions: Array(extensions).map(&:to_s) },
154
+ transforms: [capture])
155
+ require_tree_v2(JSON.parse(JSON.generate(view), symbolize_names: true))
156
+ end
157
+
158
+ # The renderer reads the 0.2 tree. carve-rb's 0.2 line still carries the gem
159
+ # version of the last 0.1 release, so the gemspec range cannot tell the
160
+ # two engines apart; the tree's own contract version can.
161
+ def require_tree_v2(tree)
162
+ version = tree.is_a?(Hash) ? tree[:astVersion].to_s : ""
163
+ return tree if version.start_with?("2.")
164
+
165
+ raise ::Carve::Hexapdf::EngineError,
166
+ "carve-hexapdf needs the Carve 0.2 engine (AST contract 2.x); the installed " \
167
+ "carve-lang #{::Carve::VERSION} produced #{version.empty? ? 'a 0.1 tree' : "contract #{version}"}"
121
168
  end
122
169
 
123
170
  # Hand the caller everything expansion found, or say what degraded.
@@ -134,9 +181,11 @@ module Carve
134
181
  return
135
182
  end
136
183
 
184
+ # A 0.2 diagnostic names its cause in `code` (C2-N-013) and the
185
+ # included file it points into in `file`, never a host path.
137
186
  Array(result[:warnings]).each do |warning|
138
187
  where = warning[:file] ? "#{warning[:file]}: " : ""
139
- $stderr.puts "carve-hexapdf: #{where}#{warning[:rule]}: #{warning[:message]}"
188
+ $stderr.puts "carve-hexapdf: #{where}#{warning[:code]}: #{warning[:message]}"
140
189
  end
141
190
  suppressed = result[:suppressedWarnings].to_i
142
191
  return unless suppressed.positive?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carve-hexapdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markup-carve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-12 00:00:00.000000000 Z
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carve-lang
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1
19
+ version: 0.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 0.2.0
22
+ version: 0.3.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.1
29
+ version: 0.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.2.0
32
+ version: 0.3.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: hexapdf
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- version: 3.0.0
109
+ version: '3.2'
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
112
  - - ">="