Module: Rpdfium::Table::Debugger
- Defined in:
- lib/rpdfium/table/debugger.rb
Overview
Genera una visualizzazione di debug: la pagina renderizzata in PNG con sovrapposti gli edges rilevati e i bbox delle celle. Equivalente di pdfplumber.Page.to_image().debug_tablefinder().
Implementato puro Ruby: rasterizza la pagina via render(), poi disegna sopra il bitmap manipolando i bytes RGBA, infine salva in PNG.
Constant Summary collapse
- RED =
[255, 0, 0, 200].freeze
- GREEN =
[0, 200, 0, 200].freeze
- BLUE =
[80, 80, 255, 120].freeze
Class Method Summary collapse
Class Method Details
.visualize(page, output_path, scale: 2.0, **table_opts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rpdfium/table/debugger.rb', line 18 def visualize(page, output_path, scale: 2.0, **table_opts) extractor = Extractor.new(page, **table_opts) edges = extractor.edges intersections = extractor.intersections tables = extractor.tables w, h, bytes, _stride = page.render(scale: scale, output: :rgba) canvas = Canvas.new(w, h, bytes) # Disegna edges. Nuovo formato: ogni edge ha orientation + x0/x1/top/bottom. # Un edge orizzontale ha top == bottom; un verticale ha x0 == x1. edges.each do |e| canvas.line((e[:x0] * scale).to_i, (e[:top] * scale).to_i, (e[:x1] * scale).to_i, (e[:bottom] * scale).to_i, RED) end # Disegna intersezioni (cerchi 4px). Sono Hash con chiave [x, y]. intersections.each_key do |(x, y)| canvas.dot((x * scale).to_i, (y * scale).to_i, GREEN, 4) end # Riempie tabelle con blu trasparente. Table#bbox รจ tuple [x0, top, x1, bottom]. tables.each do |t| x0, top, x1, bottom = t.bbox canvas.rect_fill((x0 * scale).to_i, (top * scale).to_i, (x1 * scale).to_i, (bottom * scale).to_i, BLUE) end Rpdfium::IO::PNG.write(output_path, w, h, canvas.bytes, stride: w * 4) output_path end |