From 6d63af6442601f57b6e0bbc0af2a719335f96027 Mon Sep 17 00:00:00 2001 From: Andrej Sandorf <77627197+as-op@users.noreply.github.com> Date: Wed, 29 Jan 2025 15:52:03 +0100 Subject: [PATCH] even better support for html tables (#18) split styles definition for html table vs markdown table; no forced borders for html tables --- .github/workflows/test.yml | 2 +- demo/demo.md | 10 +- docs/STYLES.md | 1 + lib/md_to_pdf/elements/html.rb | 18 +- lib/md_to_pdf/elements/table.rb | 36 ++-- .../prawn-table/table/cell/cell/subtable.rb | 13 ++ lib/md_to_pdf/style/schema_styles.json | 3 + lib/md_to_pdf/style/styles.rb | 14 ++ .../image/{in_table.md => in_table.html} | 0 ...{large_in_table.md => large_in_table.html} | 0 spec/fixtures/table/html_borders.html | 30 +-- .../table/{html.md => html_checklist.html} | 0 .../{html_figure.md => html_figure.html} | 0 spec/fixtures/table/html_no_borders.html | 8 + .../table/subtable_in_header_row.html | 21 ++ spec/fixtures/table/subtable_in_header_row.md | 21 -- spec/markdown_to_pdf/image_spec.rb | 6 +- spec/markdown_to_pdf/table_spec.rb | 187 ++++++++---------- spec/pdf_helpers.rb | 26 ++- 19 files changed, 213 insertions(+), 183 deletions(-) rename spec/fixtures/image/{in_table.md => in_table.html} (100%) rename spec/fixtures/image/{large_in_table.md => large_in_table.html} (100%) rename spec/fixtures/table/{html.md => html_checklist.html} (100%) rename spec/fixtures/table/{html_figure.md => html_figure.html} (100%) create mode 100644 spec/fixtures/table/html_no_borders.html create mode 100644 spec/fixtures/table/subtable_in_header_row.html delete mode 100644 spec/fixtures/table/subtable_in_header_row.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fd3a3b3..a089b65 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: '3.2' + ruby-version: '3.2.1' bundler-cache: true - name: Run rspec run: bundle exec rspec diff --git a/demo/demo.md b/demo/demo.md index 671dd68..ddf36c4 100644 --- a/demo/demo.md +++ b/demo/demo.md @@ -31,22 +31,24 @@ pdf_fields: # Horizontal Rules -`___` +Code: `___` ___ -`---` +Code: `---` --- -`***` +Code: `***` *** -`_________________` +Code: `_________________` _________________ + + # Emphasis This is **bold** text with `**` diff --git a/docs/STYLES.md b/docs/STYLES.md index a44810a..ded7dd3 100644 --- a/docs/STYLES.md +++ b/docs/STYLES.md @@ -20,6 +20,7 @@ | `blockquote` | **Blockquote**
Styling to denote a paragraph as quote
See [Blockquote](#blockquote) | object | | `link` | **Link**
Styling a clickable link
See [Link](#link) | object | | `table` | **Table**
See [Table](#table) | object | +| `html_table` | **Table**
See [Table](#table) | object | | `headless_table` | **Headless table**
Tables without or empty header rows can be styled differently.
See [Headless table](#headless-table) | object | | `image` | **Image**
Styling of images
See [Image](#image) | object | | `image_classes` | **Image classes**
Styling for images by class names
See [Image classes](#image-classes) | object | diff --git a/lib/md_to_pdf/elements/html.rb b/lib/md_to_pdf/elements/html.rb index 7f1773c..3ec505e 100644 --- a/lib/md_to_pdf/elements/html.rb +++ b/lib/md_to_pdf/elements/html.rb @@ -192,7 +192,7 @@ def collect_html_table_tag_rows(tag, table_font_opts, opts) end def draw_html_table_tag(tag, opts) - current_opts = opts.merge({ is_in_table: true }) + current_opts = opts.merge({ is_in_table: true, is_html_table: true }) table_font_opts = build_table_font_opts(current_opts) rows = collect_html_table_tag_rows(tag, table_font_opts, current_opts) column_count = 0 @@ -329,12 +329,15 @@ def remove_font_style(opts, style) def collect_html_table_tag_cell(tag, opts) cell_data = data_inlinehtml_tag(tag, nil, opts) - if tag.key?('style') - style = tag.get_attribute('style') || '' - cell_styling = parse_css_stylings(style) - cell_data = [{}] if cell_data.empty? - cell_data[0] = cell_data[0].merge(cell_styling) - end + cell_styling = + if tag.key?('style') + style = tag.get_attribute('style') || '' + parse_css_stylings(style) + else + { cell_borders: [] } + end + cell_data = [{}] if cell_data.empty? + cell_data[0] = cell_data[0].merge(cell_styling) cell_data end @@ -360,6 +363,7 @@ def parse_css_stylings(style) end end { + cell_borders: cell_border_color || cell_border_width || cell_border_style ? %i[left right top bottom] : [], cell_background_color: cell_background_color, cell_border_color: cell_border_color, cell_border_width: cell_border_width, diff --git a/lib/md_to_pdf/elements/table.rb b/lib/md_to_pdf/elements/table.rb index fd1ecef..2ba7dd5 100644 --- a/lib/md_to_pdf/elements/table.rb +++ b/lib/md_to_pdf/elements/table.rb @@ -31,6 +31,7 @@ def make_table_cell(cell_data, opts) border_colors: additional_cell_settings[:cell_border_color] || opts.dig(:opts_cell, :border_colors), border_widths: additional_cell_settings[:cell_border_width] || opts.dig(:opts_cell, :border_widths), border_line: additional_cell_settings[:cell_border_style] ? [additional_cell_settings[:cell_border_style]] * 4 : opts.dig(:opts_cell, :border_line), + borders: additional_cell_settings[:cell_borders], inline_format: true }.compact Prawn::Table::Cell::Text.new(@pdf, [0, 0], cell_opts) @@ -40,7 +41,7 @@ def draw_table_data(table, data_rows, column_alignments, opts) return if data_rows.empty? data = build_table_data(data_rows, column_alignments, opts) - pdf_tables = try_build_table(table, data, column_alignments) + pdf_tables = try_build_table(table, data, column_alignments, opts) pdf_tables.each do |pdf_table| optional_break_before_table(table, pdf_table) with_block_margin_all(table[:margins]) do @@ -60,7 +61,11 @@ def build_table_settings(header_row_count, opts) cell_style = @styles.table_cell header_style = @styles.table_header - if header_row_count == 0 + if opts[:is_html_table] + table_style = @styles.html_table + cell_style = @styles.html_table_cell + header_style = @styles.html_table_header + elsif header_row_count == 0 table_style = @styles.headless_table cell_style = @styles.headless_table_cell header_style = @styles.headless_table_header @@ -82,7 +87,7 @@ def build_table_settings(header_row_count, opts) private - def build_split_tables(table, data, column_alignments) + def build_split_tables(table, data, column_alignments, opts) range = 4 header_row_count = table[:header_row_count] pdf_tables = [] @@ -96,16 +101,16 @@ def build_split_tables(table, data, column_alignments) end new_column_alignments = column_alignments.slice(start, range) new_column_alignments.unshift :left - pdf_table = build_pdf_table(table, table[:opts_cell], new_rows, new_column_alignments) + pdf_table = build_pdf_table(table, table[:opts_cell], new_rows, new_column_alignments, opts) pdf_tables.push pdf_table end pdf_tables end - def try_build_table(table, data, column_alignments) - [build_pdf_table(table, table[:opts_cell], data, column_alignments)] + def try_build_table(table, data, column_alignments, opts) + [build_pdf_table(table, table[:opts_cell], data, column_alignments, opts)] rescue Prawn::Errors::CannotFit - build_split_tables(table, data, column_alignments) + build_split_tables(table, data, column_alignments, opts) end def merge_cell_data(cell_data) @@ -125,12 +130,10 @@ def optional_break_before_table(table, pdf_table) end end - def build_pdf_table(table, cell_style, data, column_alignments) + def build_pdf_table(table, cell_style, data, column_alignments, opts) column_count = column_alignments.length column_widths = Array.new(column_count, @pdf.bounds.right / column_count) - # the default border width/color property overrides the already set cell border widths/colors - # so we remove it and set it manually for each cell on creation - default_cell_style = cell_style.except(:border_widths, :border_colors) + default_cell_style = opts[:is_html_table] ? cell_style.except(:borders, :border_widths, :border_colors) : cell_style @pdf.make_table( data, width: @pdf.bounds.right, @@ -172,7 +175,16 @@ def make_subtable(cell_data, opts, alignment, column_count) rows.push([make_subtable_cell(row, opts)]) unless row.empty? return make_table_cell([{ text: '' }], opts) if rows.empty? - @pdf.make_table(rows, column_widths: [@pdf.bounds.width / column_count]) do + additional_cell_settings = cell_data[0] || {} + subtable_cell_style = { + border_colors: additional_cell_settings[:cell_border_color] || opts.dig(:opts_cell, :border_colors), + border_widths: additional_cell_settings[:cell_border_width] || opts.dig(:opts_cell, :border_widths), + border_line: additional_cell_settings[:cell_border_style] ? [additional_cell_settings[:cell_border_style]] * 4 : opts.dig(:opts_cell, :border_line), + borders: additional_cell_settings[:cell_borders] + }.compact + @pdf.make_table(rows, + cell_style: subtable_cell_style, + column_widths: [@pdf.bounds.width / column_count]) do columns(0).align = alignment unless alignment == nil end end diff --git a/lib/md_to_pdf/ext/prawn-table/table/cell/cell/subtable.rb b/lib/md_to_pdf/ext/prawn-table/table/cell/cell/subtable.rb index ea2367d..ef7e384 100644 --- a/lib/md_to_pdf/ext/prawn-table/table/cell/cell/subtable.rb +++ b/lib/md_to_pdf/ext/prawn-table/table/cell/cell/subtable.rb @@ -1,4 +1,17 @@ Prawn::Table::Cell::Subtable.prepend(Module.new do + def initialize(pdf, point, options = {}) + super + + border_cell = @subtable.cells[0, 0] + if border_cell + @borders = border_cell.borders + @border_colors = border_cell.border_colors + @border_lines = border_cell.border_lines + @border_widths = border_cell.border_widths + end + @subtable.cells.borders = [] + end + def width=(new_width) @width = @min_width = @max_width = new_width @height = nil diff --git a/lib/md_to_pdf/style/schema_styles.json b/lib/md_to_pdf/style/schema_styles.json index 25eb411..9ce0e04 100644 --- a/lib/md_to_pdf/style/schema_styles.json +++ b/lib/md_to_pdf/style/schema_styles.json @@ -171,6 +171,9 @@ "table": { "$ref": "#/$defs/table" }, + "html_table": { + "$ref": "#/$defs/table" + }, "headless_table": { "$ref": "#/$defs/headless_table" }, diff --git a/lib/md_to_pdf/style/styles.rb b/lib/md_to_pdf/style/styles.rb index fc6eb3a..adb7c2a 100644 --- a/lib/md_to_pdf/style/styles.rb +++ b/lib/md_to_pdf/style/styles.rb @@ -155,6 +155,20 @@ def headless_table_header headless_table[:header] || {} end + def html_table + return table if @styling[:html_table].nil? + + get_style(:html_table) + end + + def html_table_cell + html_table[:cell] || {} + end + + def html_table_header + html_table[:header] || {} + end + def codeblock get_style(:codeblock) end diff --git a/spec/fixtures/image/in_table.md b/spec/fixtures/image/in_table.html similarity index 100% rename from spec/fixtures/image/in_table.md rename to spec/fixtures/image/in_table.html diff --git a/spec/fixtures/image/large_in_table.md b/spec/fixtures/image/large_in_table.html similarity index 100% rename from spec/fixtures/image/large_in_table.md rename to spec/fixtures/image/large_in_table.html diff --git a/spec/fixtures/table/html_borders.html b/spec/fixtures/table/html_borders.html index 24eabde..04366e8 100644 --- a/spec/fixtures/table/html_borders.html +++ b/spec/fixtures/table/html_borders.html @@ -3,39 +3,29 @@ dotted

column1 + style="border-color:hsl(0, 0%, 0%);border-style:solid;">

column1

column2 + style="border-color:hsl(0, 0%, 0%);border-style:solid;">

column2

column3 + >column3

column4 + style="border-color:hsl(0, 0%, 0%);border-style:solid;">

column4

- - line1 + line1 width: 2px - - default - - default - - blue + default + default + blue line3 - - default + default dashed - - red + red width: 4px diff --git a/spec/fixtures/table/html.md b/spec/fixtures/table/html_checklist.html similarity index 100% rename from spec/fixtures/table/html.md rename to spec/fixtures/table/html_checklist.html diff --git a/spec/fixtures/table/html_figure.md b/spec/fixtures/table/html_figure.html similarity index 100% rename from spec/fixtures/table/html_figure.md rename to spec/fixtures/table/html_figure.html diff --git a/spec/fixtures/table/html_no_borders.html b/spec/fixtures/table/html_no_borders.html new file mode 100644 index 0000000..89fc408 --- /dev/null +++ b/spec/fixtures/table/html_no_borders.html @@ -0,0 +1,8 @@ + + + + + + + +
NoBorders!
diff --git a/spec/fixtures/table/subtable_in_header_row.html b/spec/fixtures/table/subtable_in_header_row.html new file mode 100644 index 0000000..3f790f1 --- /dev/null +++ b/spec/fixtures/table/subtable_in_header_row.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +

Header 1Header 2
Entry 1
Entry 2
diff --git a/spec/fixtures/table/subtable_in_header_row.md b/spec/fixtures/table/subtable_in_header_row.md deleted file mode 100644 index 8208a46..0000000 --- a/spec/fixtures/table/subtable_in_header_row.md +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - -

Header 1Header 2
Entry 1
Entry 2
diff --git a/spec/markdown_to_pdf/image_spec.rb b/spec/markdown_to_pdf/image_spec.rb index bb7a3ee..2c9a31a 100644 --- a/spec/markdown_to_pdf/image_spec.rb +++ b/spec/markdown_to_pdf/image_spec.rb @@ -74,8 +74,8 @@ expect_pdf_images([{ x: 256.0, y: 676.925, width: 100.0, height: 79.075 }]) end - it 'creates image in table' do - generator.parse_file('image/in_table.md', { headless_table: { auto_width: true } }) + it 'creates image in a html table' do + generator.parse_file('image/in_table.html', { html_table: { auto_width: true } }) expect_pdf([ { x: 41.0, y: 739.756, text: "Lorem ipsum dolor sit amet, consetetur" }, { x: 41.0, y: 725.884, text: "sadipscing elitr, sed diam nonumy eirmod tempor" }, @@ -129,7 +129,7 @@ end it 'creates large image in table' do - generator.parse_file('image/large_in_table.md', { headless_table: { auto_width: true } }) + generator.parse_file('image/large_in_table.html', { html_table: { auto_width: true } }) expect_pdf([ { x: 36.0, y: 747.384, text: "With image style.width:" }, { x: 41.0, y: 725.884, text: "Lorem ipsum dolor sit amet, consetetur" }, diff --git a/spec/markdown_to_pdf/table_spec.rb b/spec/markdown_to_pdf/table_spec.rb index c0d39b9..4e1a59e 100644 --- a/spec/markdown_to_pdf/table_spec.rb +++ b/spec/markdown_to_pdf/table_spec.rb @@ -79,7 +79,7 @@ end it 'creates a html table with checkboxes' do - generator.parse_file('table/html.md') + generator.parse_file('table/html_checklist.html') expect_pdf([ { x: 113.14286, y: 744.756, text: "Header 1" }, { x: 190.28571, y: 744.756, text: "Header 2" }, @@ -124,17 +124,18 @@ end it 'creates a html table with header column' do - generator.parse_file('table/html_figure.md') + generator.parse_file('table/html_figure.html', + { table: { header: { styles: ['bold'] } } }) expect_pdf([ { x: 36.0, y: 744.756, text: "Header 1" }, { x: 306.0, y: 744.756, text: "Text 1" }, - { x: 36.0, y: 730.884, text: "Header 2" }, - { x: 306.0, y: 730.884, text: "Text 2" }, - { x: 36.0, y: 717.012, text: "Header 3" }, - { x: 306.0, y: 717.012, text: "Text 3" }]) + { x: 36.0, y: 730.476, text: "Header 2" }, + { x: 306.0, y: 730.476, text: "Text 2" }, + { x: 36.0, y: 716.196, text: "Header 3" }, + { x: 306.0, y: 716.196, text: "Text 3" }]) end - it 'creates a html table with linefeeds inside' do + it 'creates a table with html linefeed inside' do generator.parse_file('table/linefeed_in_cell.md') expect_pdf([ { x: 36.0, y: 744.756, text: "Description Column 1" }, @@ -175,7 +176,7 @@ end it 'creates a html table with subtable in a header row' do - generator.parse_file('table/subtable_in_header_row.md') + generator.parse_file('table/subtable_in_header_row.html') expect_pdf([ { x: 221.0, y: 739.756, text: "Header 1" }, { x: 396.0, y: 744.756, text: "Header 2" }, @@ -233,110 +234,78 @@ { x: 36.0, y: 675.396, text: "right" }]) end + it 'creates a html table without cell borders' do + generator.parse_file('table/html_no_borders.html') + expect_pdf_borders([]) + expect_pdf([ + { x: 36.0, y: 744.756, text: "No" }, + { x: 306.0, y: 744.756, text: "Borders!" }]) + end + it 'creates a html table with cell borders' do generator.parse_file('table/html_borders.html') - expect_pdf_border_rects( + expect_pdf_borders( [ - 7.5, [0.0, 0.0, 0.0], - 7.5, [0.0, 0.0, 0.0], - 7.5, [0.0, 0.0, 0.0], - 7.5, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 1.5, [0.0, 0.0, 0.0], - 1.5, [0.0, 0.0, 0.0], - 1.5, [0.0, 0.0, 0.0], - 1.5, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 2.25, [0.0, 0.34902, 1.0], - 2.25, [0.0, 0.34902, 1.0], - 2.25, [0.0, 0.34902, 1.0], - 2.25, [0.0, 0.34902, 1.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 1.5, [0.90196, 0.6, 0.29804], - 1.5, [0.90196, 0.6, 0.29804], - 1.5, [0.90196, 0.6, 0.29804], - 1.5, [0.90196, 0.6, 0.29804], - 1, [0.0, 0.0, 0.0], - 0.25, [1.0, 0.0, 0.0], - 0.25, [1.0, 0.0, 0.0], - 0.25, [1.0, 0.0, 0.0], - 0.25, [1.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 3.0, [0.0, 0.0, 0.0], - 3.0, [0.0, 0.0, 0.0], - 3.0, [0.0, 0.0, 0.0], - 3.0, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 0.25, [0.0, 0.0, 0.0], - 1, [0.0, 0.0, 0.0] + [[7.5, 15.0], 7.5, 0.0, 36.0, 36.0], + [[], [7.5, 15.0], 7.5, 0.0, 144.0, 144.0], + [[], [7.5, 15.0], 7.5, 0.0, 36.0, 144.0], + [[], [7.5, 15.0], 7.5, 0.0, 36.0, 144.0], + [0.25, 0.0, 144.0, 144.0], + [[], 0.25, 0.0, 252.0, 252.0], + [[], 0.25, 0.0, 144.0, 252.0], + [[], 0.25, 0.0, 144.0, 252.0], + [0.25, 0.0, 252.0, 252.0], + [[], 0.25, 0.0, 360.0, 360.0], + [[], 0.25, 0.0, 252.0, 360.0], + [[], 0.25, 0.0, 252.0, 360.0], + [0.25, 0.0, 360.0, 360.0], + [[], 0.25, 0.0, 468.0, 468.0], + [[], 0.25, 0.0, 360.0, 468.0], + [[], 0.25, 0.0, 360.0, 468.0], + [0.25, 0.0, 468.0, 468.0], + [[], 0.25, 0.0, 576.0, 576.0], + [[], 0.25, 0.0, 468.0, 576.0], + [[], 0.25, 0.0, 468.0, 576.0], + [0.25, 0.0, 36.0, 36.0], + [[], 0.25, 0.0, 144.0, 144.0], + [[], 0.25, 0.0, 36.0, 144.0], + [[], 0.25, 0.0, 36.0, 144.0], + [1.5, 0.0, 144.0, 144.0], + [[], 1.5, 0.0, 252.0, 252.0], + [[], 1.5, 0.0, 144.0, 252.0], + [[], 1.5, 0.0, 144.0, 252.0], + [0.25, 0.0, 252.0, 252.0], + [[], 0.25, 0.0, 360.0, 360.0], + [[], 0.25, 0.0, 252.0, 360.0], + [[], 0.25, 0.0, 252.0, 360.0], + [0.25, 0.0, 360.0, 360.0], + [[], 0.25, 0.0, 468.0, 468.0], + [[], 0.25, 0.0, 360.0, 468.0], + [[], 0.25, 0.0, 360.0, 468.0], + [2.25, 0.0, 468.0, 468.0], + [[], 2.25, 0.0, 576.0, 576.0], + [[], 2.25, 0.0, 468.0, 576.0], + [[], 2.25, 0.0, 468.0, 576.0], + [0.25, 0.0, 36.0, 36.0], + [[], 0.25, 0.0, 144.0, 144.0], + [[], 0.25, 0.0, 36.0, 144.0], + [[], 0.25, 0.0, 36.0, 144.0], + [0.25, 0.0, 144.0, 144.0], + [[], 0.25, 0.0, 252.0, 252.0], + [[], 0.25, 0.0, 144.0, 252.0], + [[], 0.25, 0.0, 144.0, 252.0], + [[6.0, 6.0], 1.5, 0.90196, 252.0, 252.0], + [[], [6.0, 6.0], 1.5, 0.90196, 360.0, 360.0], + [[], [6.0, 6.0], 1.5, 0.90196, 252.0, 360.0], + [[], [6.0, 6.0], 1.5, 0.90196, 252.0, 360.0], + [0.25, 1.0, 360.0, 360.0], + [[], 0.25, 1.0, 468.0, 468.0], + [[], 0.25, 1.0, 360.0, 468.0], + [[], 0.25, 1.0, 360.0, 468.0], + [3.0, 0.0, 468.0, 468.0], + [[], 3.0, 0.0, 576.0, 576.0], + [[], 3.0, 0.0, 468.0, 576.0], + [[], 3.0, 0.0, 468.0, 576.0] ] ) expect_pdf([ diff --git a/spec/pdf_helpers.rb b/spec/pdf_helpers.rb index 5e2d577..d555d07 100644 --- a/spec/pdf_helpers.rb +++ b/spec/pdf_helpers.rb @@ -119,18 +119,32 @@ def rectangles def borders border_calls = [] + stroke = [] calls.each do |call| - if call[0] == :set_line_width - border_calls.push(call[1]) - elsif call[0] == :set_color_for_stroking_and_special - border_calls.push(call.slice(1, 3)) + case call[0] + when :set_line_width, :set_color_for_stroking_and_special, :begin_new_subpath, :append_line, :set_line_dash + stroke.push(call.slice(1)) + when :begin_text_object + stroke = [] + when :stroke_path + border_calls.push(stroke) + stroke = [] end end border_calls end + def out_calls + printed_calls = [] + calls.each do |call| + printed_calls.push(call.inspect) + end + puts printed_calls.join("\n") + end + def out_borders - puts "expect_pdf_border_rects(\n[\n#{borders.map(&:to_json).join(",\n")}\n])".gsub(",\n[", ', [') + puts "expect_pdf_borders(\n[\n#{borders.map(&:to_json).join(",\n")}\n])" + # puts "expect_pdf_border_rects(\n[\n#{borders.map(&:to_json).join(",\n")}\n])".gsub(",\n[", ', [') end def out_rectangles @@ -164,7 +178,7 @@ def expect_pdf_color_rects(cases) end end - def expect_pdf_border_rects(cases) + def expect_pdf_borders(cases) actual = borders expect(actual.length).to eq cases.length cases.each_with_index do |case_entry, index|