From c7d42d1623c5baa8e45b9145474230cae59a261d Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 27 Aug 2021 22:31:21 +0200 Subject: [PATCH 01/26] New CSS properties (for position absolute): bottom and right --- lib/prawn_html/attributes.rb | 4 ++- lib/prawn_html/document_renderer.rb | 20 ++++++++++---- lib/prawn_html/pdf_wrapper.rb | 26 ++++++++++++++++++ spec/units/prawn_html/pdf_wrapper_spec.rb | 32 +++++++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/lib/prawn_html/attributes.rb b/lib/prawn_html/attributes.rb index 3ebf9b6..14d5fae 100644 --- a/lib/prawn_html/attributes.rb +++ b/lib/prawn_html/attributes.rb @@ -7,7 +7,7 @@ class Attributes < OpenStruct attr_reader :styles STYLES_APPLY = { - block: %i[align leading left margin_left padding_left position top], + block: %i[align bottom leading left margin_left padding_left position right top], tag_close: %i[margin_bottom padding_bottom break_after], tag_open: %i[margin_top padding_top break_before], text_node: %i[background callback character_spacing color font link list_style_type size styles white_space] @@ -37,11 +37,13 @@ class Attributes < OpenStruct 'margin-bottom' => { key: :margin_bottom, set: :convert_size }, 'padding-bottom' => { key: :padding_bottom, set: :convert_size }, # block styles + 'bottom' => { key: :bottom, set: :convert_size }, 'left' => { key: :left, set: :convert_size }, 'line-height' => { key: :leading, set: :convert_size }, 'margin-left' => { key: :margin_left, set: :convert_size }, 'padding-left' => { key: :padding_left, set: :convert_size }, 'position' => { key: :position, set: :convert_symbol }, + 'right' => { key: :right, set: :convert_size }, 'text-align' => { key: :align, set: :convert_symbol }, 'top' => { key: :top, set: :convert_size } }.freeze diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 7e88e84..92e1a54 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -113,7 +113,7 @@ def output_content(buffer, block_styles) left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f options = block_styles.slice(:align, :leading, :mode, :padding_left) options[:indent_paragraphs] = left_indent if left_indent > 0 - pdf.puts(buffer, options, bounding_box: bounds(block_styles)) + pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles)) end def apply_callbacks(buffer) @@ -123,12 +123,22 @@ def apply_callbacks(buffer) end end - def bounds(block_styles) + def bounds(buffer, options, block_styles) return unless block_styles[:position] == :absolute - y = pdf.bounds.height - (block_styles[:top] || 0) - w = pdf.bounds.width - (block_styles[:left] || 0) - [[block_styles[:left] || 0, y], { width: w }] + x = if block_styles.include?(:right) + x1 = pdf.calc_buffer_width(buffer) + block_styles[:right] + x1 < pdf.bounds.width ? (pdf.bounds.width - x1) : 0 + else + block_styles[:left] || 0 + end + y = if block_styles.include?(:bottom) + pdf.calc_buffer_height(buffer, options) + block_styles[:bottom] + else + pdf.bounds.height - (block_styles[:top] || 0) + end + + [[x, y], { width: pdf.bounds.width - x }] end end end diff --git a/lib/prawn_html/pdf_wrapper.rb b/lib/prawn_html/pdf_wrapper.rb index 81aa31a..d0ea6ed 100644 --- a/lib/prawn_html/pdf_wrapper.rb +++ b/lib/prawn_html/pdf_wrapper.rb @@ -24,6 +24,32 @@ def advance_cursor(move_down) pdf.move_down(move_down) end + # Calculate the height of a buffer of items + # + # @param buffer [Array] Buffer of items + # @param options [Hash] Output options + # + # @return [Float] calculated height + def calc_buffer_height(buffer, options) + pdf.height_of_formatted(buffer, options) + end + + # Calculate the width of a buffer of items + # + # @param buffer [Array] Buffer of items + # + # @return [Float] calculated width + def calc_buffer_width(buffer) + width = 0 + buffer.each do |item| + font_family = item[:font] || pdf.font.name + pdf.font(font_family, size: item[:size] || pdf.font_size) do + width += pdf.width_of(item[:text], inline_format: true) + end + end + width + end + # Draw a rectangle # # @param x [Float] left position of the rectangle diff --git a/spec/units/prawn_html/pdf_wrapper_spec.rb b/spec/units/prawn_html/pdf_wrapper_spec.rb index 1c23872..9a93568 100644 --- a/spec/units/prawn_html/pdf_wrapper_spec.rb +++ b/spec/units/prawn_html/pdf_wrapper_spec.rb @@ -46,6 +46,38 @@ end end + describe '#calc_buffer_height' do + subject(:calc_buffer_height) { pdf_wrapper.calc_buffer_height(buffer, options) } + + let(:buffer) { [{ text: 'some content' }] } + let(:options) { {} } + + before do + allow(pdf).to receive(:height_of_formatted) + end + + it 'calls the PDF height_of_formatted method' do + calc_buffer_height + expect(pdf).to have_received(:height_of_formatted).with(buffer, options) + end + end + + describe '#calc_buffer_width' do + subject(:calc_buffer_width) { pdf_wrapper.calc_buffer_width(buffer) } + + let(:buffer) { [{ text: 'some content', font: 'Courier', size: 12 }] } + + before do + allow(pdf).to receive(:font).and_yield + allow(pdf).to receive(:width_of).and_return(0) + end + + it 'calls the PDF width_of method' do + calc_buffer_width + expect(pdf).to have_received(:width_of) # .with(buffer, inline_format: true) + end + end + describe '#draw_rectangle' do subject(:draw_rectangle) { pdf_wrapper.draw_rectangle(x: 50, y: 80, width: 200, height: 150, color: 'ffbb111') } From 7e6fdff2c11114ba8968b97a460148a167e28e40 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sat, 28 Aug 2021 22:43:47 +0200 Subject: [PATCH 02/26] Process tag styles outside the initialize --- lib/prawn_html/document_renderer.rb | 4 +- lib/prawn_html/tag.rb | 22 +++++------ lib/prawn_html/tags/img.rb | 4 +- lib/prawn_html/tags/ol.rb | 2 +- spec/units/blocks_spec.rb | 9 +++-- spec/units/headings_spec.rb | 17 ++++---- spec/units/lists_spec.rb | 11 +++--- spec/units/misc_spec.rb | 39 ++++++++++--------- .../prawn_html/document_renderer_spec.rb | 16 +++++++- spec/units/prawn_html/tag_spec.rb | 4 ++ spec/units/prawn_html/tags/a_spec.rb | 8 ++++ spec/units/prawn_html/tags/b_spec.rb | 4 ++ spec/units/prawn_html/tags/blockquote_spec.rb | 4 ++ spec/units/prawn_html/tags/code_spec.rb | 4 ++ spec/units/prawn_html/tags/del_spec.rb | 4 ++ spec/units/prawn_html/tags/h_spec.rb | 4 ++ spec/units/prawn_html/tags/hr_spec.rb | 8 ++++ spec/units/prawn_html/tags/i_spec.rb | 4 ++ spec/units/prawn_html/tags/mark_spec.rb | 4 ++ spec/units/prawn_html/tags/ol_spec.rb | 4 ++ spec/units/prawn_html/tags/p_spec.rb | 4 ++ spec/units/prawn_html/tags/pre_spec.rb | 5 ++- spec/units/prawn_html/tags/sub_spec.rb | 4 ++ spec/units/prawn_html/tags/sup_spec.rb | 4 ++ spec/units/prawn_html/tags/u_spec.rb | 4 ++ spec/units/prawn_html/tags/ul_spec.rb | 4 ++ spec/units/styles_spec.rb | 31 ++++++++------- 27 files changed, 163 insertions(+), 69 deletions(-) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 92e1a54..c34d96b 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -35,7 +35,9 @@ def on_tag_open(tag_name, attributes:, element_styles: '') tag_class = Tag.class_for(tag_name) return unless tag_class - tag_class.new(tag_name, attributes: attributes, element_styles: element_styles).tap do |element| + tag_class.new(tag_name, attributes: attributes).tap do |element| + options = { width: pdf.bounds.width, height: pdf.bounds.height } + element.process_styles(element_styles: element_styles, options: options) setup_element(element) end end diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index a7ac128..f3006d6 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -15,11 +15,9 @@ class Tag # # @param tag [Symbol] tag name # @param attributes [Hash] hash of element attributes - # @param element_styles [String] document styles tp apply to the element - def initialize(tag, attributes: {}, element_styles: '') + def initialize(tag, attributes: {}) @tag = tag @attrs = Attributes.new(attributes) - process_styles(element_styles, attributes['style']) end # Is a block tag? @@ -38,6 +36,16 @@ def block_styles block_styles end + # Process tag styles + # + # @return element_styles [String] extra styles to apply to the element + # @return options [Hash] options (container width/height/etc.) + def process_styles(element_styles: nil, options: {}) + attrs.merge_text_styles!(tag_styles) if respond_to?(:tag_styles) + attrs.merge_text_styles!(element_styles) if element_styles + attrs.merge_text_styles!(attrs.style) + end + # Styles to apply on tag closing # # @return [Hash] hash of styles to apply @@ -74,13 +82,5 @@ def class_for(tag_name) @tag_classes[tag_name] end end - - private - - def process_styles(element_styles, inline_styles) - attrs.merge_text_styles!(tag_styles) if respond_to?(:tag_styles) - attrs.merge_text_styles!(element_styles) - attrs.merge_text_styles!(inline_styles) - end end end diff --git a/lib/prawn_html/tags/img.rb b/lib/prawn_html/tags/img.rb index cf48840..a6e272d 100644 --- a/lib/prawn_html/tags/img.rb +++ b/lib/prawn_html/tags/img.rb @@ -12,13 +12,13 @@ def block? def custom_render(pdf, context) parsed_styles = Attributes.parse_styles(attrs.style) block_styles = context.block_styles - evaluated_styles = evaluate_styles(pdf, block_styles.merge(parsed_styles)) + evaluated_styles = adjust_styles(pdf, block_styles.merge(parsed_styles)) pdf.image(@attrs.src, evaluated_styles) end private - def evaluate_styles(pdf, img_styles) + def adjust_styles(pdf, img_styles) {}.tap do |result| result[:width] = Utils.convert_size(img_styles['width'], pdf.bounds.width) if img_styles.include?('width') result[:height] = Utils.convert_size(img_styles['height'], pdf.bounds.height) if img_styles.include?('height') diff --git a/lib/prawn_html/tags/ol.rb b/lib/prawn_html/tags/ol.rb index dccfd9c..699fc6c 100644 --- a/lib/prawn_html/tags/ol.rb +++ b/lib/prawn_html/tags/ol.rb @@ -9,7 +9,7 @@ class Ol < Tag attr_accessor :counter - def initialize(tag, attributes: {}, element_styles: '') + def initialize(tag, attributes: {}) super @counter = 0 end diff --git a/spec/units/blocks_spec.rb b/spec/units/blocks_spec.rb index 61da72a..f1b7099 100644 --- a/spec/units/blocks_spec.rb +++ b/spec/units/blocks_spec.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true RSpec.describe 'Blocks' do - let(:pdf_doc) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf_doc) + allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) end @@ -13,7 +14,7 @@ let(:html) { '
Some sample content...
' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some sample content..." }], {}, { bounding_box: nil } ) end @@ -23,7 +24,7 @@ let(:html) { '

Some sample content...

' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some sample content..." }], {}, { bounding_box: nil } ) end diff --git a/spec/units/headings_spec.rb b/spec/units/headings_spec.rb index 66ca034..748f6aa 100644 --- a/spec/units/headings_spec.rb +++ b/spec/units/headings_spec.rb @@ -3,10 +3,11 @@ RSpec.describe 'Headings' do let(:expected_buffer) { [{ size: size, styles: [:bold], text: 'Some sample content...' }] } let(:expected_options) { {} } - let(:pdf_doc) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf_doc) + allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) end @@ -16,7 +17,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h1].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -25,7 +26,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h2].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -34,7 +35,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h3].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -43,7 +44,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h4].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -52,7 +53,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h5].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -61,7 +62,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h6].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end end diff --git a/spec/units/lists_spec.rb b/spec/units/lists_spec.rb index 0af8424..b3e0401 100644 --- a/spec/units/lists_spec.rb +++ b/spec/units/lists_spec.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true RSpec.describe 'Lists' do - let(:pdf_doc) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf_doc) + allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) end @@ -25,13 +26,13 @@ end it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: size, text: "• First item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: size, text: "• Second item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: size, text: "• Third item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } ) end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index d582731..17d3a29 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -1,13 +1,14 @@ # frozen_string_literal: true RSpec.describe 'Misc' do - let(:pdf_doc) do + let(:pdf) do methods = { advance_cursor: true, puts: true, horizontal_rule: true } instance_double(PrawnHtml::PdfWrapper, methods) end before do - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf_doc) + allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) end @@ -16,10 +17,10 @@ let(:html) { 'Some content
More content' } it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content" }], {}, bounding_box: nil ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "More content" }], {}, bounding_box: nil ) end @@ -31,7 +32,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -39,13 +40,13 @@ let(:html) { 'First line
Second line
Third line' } it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "First line" }], {}, bounding_box: nil ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Second line" }], {}, bounding_box: nil ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Third line" }], {}, bounding_box: nil ) end @@ -57,7 +58,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -65,7 +66,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], {}, bounding_box: nil @@ -79,7 +80,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -89,7 +90,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -97,7 +98,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], {}, bounding_box: nil ) end @@ -107,7 +108,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], {}, bounding_box: nil @@ -119,7 +120,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], {}, bounding_box: nil @@ -132,7 +133,7 @@ let(:size) { PrawnHtml::Context::DEF_FONT_SIZE * 0.85 } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with([{ size: size, text: 'Some content...' }], {}, bounding_box: nil) + expect(pdf).to have_received(:puts).with([{ size: size, text: 'Some content...' }], {}, bounding_box: nil) end end @@ -140,7 +141,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: 'Some content...' }], {}, bounding_box: nil ) end @@ -152,7 +153,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -160,7 +161,7 @@ let(:html) { 'Some content...' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], {}, bounding_box: nil ) end diff --git a/spec/units/prawn_html/document_renderer_spec.rb b/spec/units/prawn_html/document_renderer_spec.rb index 55cf9bb..4d7e178 100644 --- a/spec/units/prawn_html/document_renderer_spec.rb +++ b/spec/units/prawn_html/document_renderer_spec.rb @@ -105,9 +105,21 @@ end end - context 'with an element with position absolute' do + context 'with position absolute, top and left properties' do before do - document_renderer.on_tag_open(:div, attributes: { 'style' => 'position: absolute; left: 50px; top: 10px' }) + document_renderer.on_tag_open(:div, attributes: { 'style' => 'position: absolute; top: 10px; left: 50px' }) + document_renderer.on_text_node('Some content') + end + + it "renders the current buffer's content in a bounded box" do + render + expect(pdf).to have_received(:puts) + end + end + + context 'with position absolute, bottom and right properties' do + before do + document_renderer.on_tag_open(:div, attributes: { 'style' => 'position: absolute; bottom: 10px; right: 80px' }) document_renderer.on_text_node('Some content') end diff --git a/spec/units/prawn_html/tag_spec.rb b/spec/units/prawn_html/tag_spec.rb index 3811664..7c30828 100644 --- a/spec/units/prawn_html/tag_spec.rb +++ b/spec/units/prawn_html/tag_spec.rb @@ -44,6 +44,10 @@ describe '#styles' do subject(:styles) { tag.styles } + before do + tag.process_styles + end + it { is_expected.to eq(color: '0088ff') } end diff --git a/spec/units/prawn_html/tags/a_spec.rb b/spec/units/prawn_html/tags/a_spec.rb index 90141bf..ef55245 100644 --- a/spec/units/prawn_html/tags/a_spec.rb +++ b/spec/units/prawn_html/tags/a_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + a.process_styles + end + it "styles doesn't include the link property" do expect(a.styles).to eq(color: 'ffbb11') end @@ -16,6 +20,10 @@ described_class.new(:a, attributes: { 'href' => 'https://www.google.it', 'style' => 'color: #fb1' }) end + before do + a.process_styles + end + it 'includes the link property in the styles' do expect(a.styles).to match(color: 'ffbb11', link: 'https://www.google.it') end diff --git a/spec/units/prawn_html/tags/b_spec.rb b/spec/units/prawn_html/tags/b_spec.rb index aaf60f9..f441f91 100644 --- a/spec/units/prawn_html/tags/b_spec.rb +++ b/spec/units/prawn_html/tags/b_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + b.process_styles + end + it 'returns the expected styles for b tag' do expect(b.styles).to match(color: 'ffbb11', styles: [:bold]) end diff --git a/spec/units/prawn_html/tags/blockquote_spec.rb b/spec/units/prawn_html/tags/blockquote_spec.rb index d2f2262..bc5641c 100644 --- a/spec/units/prawn_html/tags/blockquote_spec.rb +++ b/spec/units/prawn_html/tags/blockquote_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + blockquote.process_styles + end + it 'returns the expected styles for blockquote tag' do expected_styles = { color: 'ffbb11', diff --git a/spec/units/prawn_html/tags/code_spec.rb b/spec/units/prawn_html/tags/code_spec.rb index d29f278..79a7a37 100644 --- a/spec/units/prawn_html/tags/code_spec.rb +++ b/spec/units/prawn_html/tags/code_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + code.process_styles + end + it 'returns the expected styles for code tag' do expect(code.styles).to match(color: 'ffbb11', font: 'Courier') end diff --git a/spec/units/prawn_html/tags/del_spec.rb b/spec/units/prawn_html/tags/del_spec.rb index ec9cfbe..0d5cd93 100644 --- a/spec/units/prawn_html/tags/del_spec.rb +++ b/spec/units/prawn_html/tags/del_spec.rb @@ -8,6 +8,10 @@ describe '#styles' do subject(:styles) { del.styles } + before do + del.process_styles + end + it 'merges the callback property into styles' do expect(styles).to match(color: 'ffbb11', callback: 'StrikeThrough') end diff --git a/spec/units/prawn_html/tags/h_spec.rb b/spec/units/prawn_html/tags/h_spec.rb index 298830b..124c2cc 100644 --- a/spec/units/prawn_html/tags/h_spec.rb +++ b/spec/units/prawn_html/tags/h_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + h1.process_styles + end + it 'returns the expected styles for h1 tag' do expected_styles = { color: 'ffbb11', diff --git a/spec/units/prawn_html/tags/hr_spec.rb b/spec/units/prawn_html/tags/hr_spec.rb index 710d31a..6d2712b 100644 --- a/spec/units/prawn_html/tags/hr_spec.rb +++ b/spec/units/prawn_html/tags/hr_spec.rb @@ -8,6 +8,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + hr.process_styles + end + it 'returns the expected styles for hr tag' do expected_styles = { color: 'ffbb11', @@ -27,6 +31,10 @@ describe '#custom_render' do subject(:custom_render) { hr.custom_render(pdf, context) } + before do + hr.process_styles + end + let(:context) { nil } it 'calls horizontal_rule on the pdf wrapper' do diff --git a/spec/units/prawn_html/tags/i_spec.rb b/spec/units/prawn_html/tags/i_spec.rb index 2226964..f9a594d 100644 --- a/spec/units/prawn_html/tags/i_spec.rb +++ b/spec/units/prawn_html/tags/i_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + i.process_styles + end + it 'returns the expected styles for i tag' do expect(i.styles).to match(color: 'ffbb11', styles: [:italic]) end diff --git a/spec/units/prawn_html/tags/mark_spec.rb b/spec/units/prawn_html/tags/mark_spec.rb index ddb9ad0..0957719 100644 --- a/spec/units/prawn_html/tags/mark_spec.rb +++ b/spec/units/prawn_html/tags/mark_spec.rb @@ -8,6 +8,10 @@ describe '#styles' do subject(:styles) { mark.styles } + before do + mark.process_styles + end + it 'merges the callback property into styles' do expect(styles).to match(color: 'ffbb11', callback: 'Highlight') end diff --git a/spec/units/prawn_html/tags/ol_spec.rb b/spec/units/prawn_html/tags/ol_spec.rb index 4decd39..3b96e42 100644 --- a/spec/units/prawn_html/tags/ol_spec.rb +++ b/spec/units/prawn_html/tags/ol_spec.rb @@ -10,6 +10,10 @@ end context 'without attributes' do + before do + ol.process_styles + end + it 'returns the expected styles for ol tag' do expected_styles = { color: 'ffbb11', diff --git a/spec/units/prawn_html/tags/p_spec.rb b/spec/units/prawn_html/tags/p_spec.rb index c236eed..ea5b84d 100644 --- a/spec/units/prawn_html/tags/p_spec.rb +++ b/spec/units/prawn_html/tags/p_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + p.process_styles + end + it 'returns the expected styles for p tag' do expected_styles = { color: 'ffbb11', diff --git a/spec/units/prawn_html/tags/pre_spec.rb b/spec/units/prawn_html/tags/pre_spec.rb index 9ec6f21..bfd8629 100644 --- a/spec/units/prawn_html/tags/pre_spec.rb +++ b/spec/units/prawn_html/tags/pre_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + pre.process_styles + end + it 'returns the expected styles for pre tag' do expected_styles = { color: 'ffbb11', @@ -14,7 +18,6 @@ font: 'Courier', white_space: :pre } - expect(pre.styles).to match(expected_styles) end end diff --git a/spec/units/prawn_html/tags/sub_spec.rb b/spec/units/prawn_html/tags/sub_spec.rb index 54124e9..9f8e293 100644 --- a/spec/units/prawn_html/tags/sub_spec.rb +++ b/spec/units/prawn_html/tags/sub_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + sub.process_styles + end + it 'returns the expected styles for sub tag' do expect(sub.styles).to match(color: 'ffbb11', styles: [:subscript]) end diff --git a/spec/units/prawn_html/tags/sup_spec.rb b/spec/units/prawn_html/tags/sup_spec.rb index 0c4bda7..d470169 100644 --- a/spec/units/prawn_html/tags/sup_spec.rb +++ b/spec/units/prawn_html/tags/sup_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + sup.process_styles + end + it 'returns the expected styles for sup tag' do expect(sup.styles).to match(color: 'ffbb11', styles: [:superscript]) end diff --git a/spec/units/prawn_html/tags/u_spec.rb b/spec/units/prawn_html/tags/u_spec.rb index eed436b..0e138f2 100644 --- a/spec/units/prawn_html/tags/u_spec.rb +++ b/spec/units/prawn_html/tags/u_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + u.process_styles + end + it 'returns the expected styles for u tag' do expect(u.styles).to match(color: 'ffbb11', styles: [:underline]) end diff --git a/spec/units/prawn_html/tags/ul_spec.rb b/spec/units/prawn_html/tags/ul_spec.rb index 2db6e83..53c2730 100644 --- a/spec/units/prawn_html/tags/ul_spec.rb +++ b/spec/units/prawn_html/tags/ul_spec.rb @@ -6,6 +6,10 @@ it { expect(described_class).to be < PrawnHtml::Tag } context 'without attributes' do + before do + ul.process_styles + end + it 'returns the expected styles for ul tag' do expected_styles = { color: 'ffbb11', diff --git a/spec/units/styles_spec.rb b/spec/units/styles_spec.rb index ed49c29..cf3e8f6 100644 --- a/spec/units/styles_spec.rb +++ b/spec/units/styles_spec.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true RSpec.describe 'Styles' do - let(:pdf_doc) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf_doc) + allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) end @@ -16,7 +17,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -27,7 +28,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -39,7 +40,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -50,7 +51,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -61,7 +62,7 @@ let(:expected_options) { {} } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -70,13 +71,13 @@ let(:size) { TestUtils.default_font_size } it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ character_spacing: 1.5, size: size, text: 'aaa' }], {}, bounding_box: nil ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: size, text: ' bbb ' }], {}, bounding_box: nil ) - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ character_spacing: 2.0, size: size, text: 'ccc' }], {}, bounding_box: nil ) end @@ -86,7 +87,7 @@ let(:html) { '
Some content...
' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], { leading: 12 * PrawnHtml::PX }, bounding_box: nil @@ -101,7 +102,7 @@ let(:expected_options) { { indent_paragraphs: (40 * PrawnHtml::PX).round(4) } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -109,7 +110,7 @@ let(:html) { '
Some content...
' } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with( + expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], {}, bounding_box: nil @@ -125,7 +126,7 @@ let(:expected_options) { { align: :left } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end @@ -136,7 +137,7 @@ let(:expected_options) { { align: :center } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf_doc).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) end end end From e327e6b1ef4bd506e0ffd38838aaf98c5cbc7d7e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 29 Aug 2021 22:16:01 +0200 Subject: [PATCH 03/26] Percent values for position attributes (top, left, bottom, right) --- .rubocop.yml | 4 ++++ lib/prawn_html/attributes.rb | 24 +++++++++++++----------- lib/prawn_html/document_renderer.rb | 6 +++--- lib/prawn_html/tag.rb | 19 ++++++++++++------- lib/prawn_html/tags/img.rb | 5 +++-- lib/prawn_html/tags/ol.rb | 2 +- lib/prawn_html/utils.rb | 18 +++++++++--------- spec/units/prawn_html/attributes_spec.rb | 21 +++++++++++++++++---- spec/units/prawn_html/utils_spec.rb | 2 +- 9 files changed, 63 insertions(+), 38 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index de85f98..70d847b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -15,6 +15,10 @@ AllCops: - vendor/**/* NewCops: enable +Lint/UnusedMethodArgument: + Exclude: + - lib/prawn_html/utils.rb + Naming/FileName: Exclude: - lib/prawn-html.rb diff --git a/lib/prawn_html/attributes.rb b/lib/prawn_html/attributes.rb index 14d5fae..4baedc2 100644 --- a/lib/prawn_html/attributes.rb +++ b/lib/prawn_html/attributes.rb @@ -37,15 +37,15 @@ class Attributes < OpenStruct 'margin-bottom' => { key: :margin_bottom, set: :convert_size }, 'padding-bottom' => { key: :padding_bottom, set: :convert_size }, # block styles - 'bottom' => { key: :bottom, set: :convert_size }, - 'left' => { key: :left, set: :convert_size }, + 'bottom' => { key: :bottom, set: :convert_size, options: :height }, + 'left' => { key: :left, set: :convert_size, options: :width }, 'line-height' => { key: :leading, set: :convert_size }, 'margin-left' => { key: :margin_left, set: :convert_size }, 'padding-left' => { key: :padding_left, set: :convert_size }, 'position' => { key: :position, set: :convert_symbol }, - 'right' => { key: :right, set: :convert_size }, + 'right' => { key: :right, set: :convert_size, options: :width }, 'text-align' => { key: :align, set: :convert_symbol }, - 'top' => { key: :top, set: :convert_size } + 'top' => { key: :top, set: :convert_size, options: :height } }.freeze STYLES_MERGE = %i[margin_left padding_left].freeze @@ -69,9 +69,10 @@ def data # Merge text styles # # @param text_styles [String] styles to parse and process - def merge_text_styles!(text_styles) + # @param options [Hash] options (container width/height/etc.) + def merge_text_styles!(text_styles, options: {}) hash_styles = Attributes.parse_styles(text_styles) - process_styles(hash_styles) unless hash_styles.empty? + process_styles(hash_styles, options: options) unless hash_styles.empty? end class << self @@ -102,19 +103,20 @@ def parse_styles(styles) private - def apply_rule!(result, rule, value) + def apply_rule!(merged_styles:, rule:, value:, options:) return unless rule if rule[:set] == :append_styles - (result[rule[:key]] ||= []) << Utils.normalize_style(value) + (merged_styles[rule[:key]] ||= []) << Utils.normalize_style(value) else - result[rule[:key]] = Utils.send(rule[:set], value) + opts = rule[:options] ? options[rule[:options]] : nil + merged_styles[rule[:key]] = Utils.send(rule[:set], value, options: opts) end end - def process_styles(hash_styles) + def process_styles(hash_styles, options:) hash_styles.each do |key, value| - apply_rule!(@styles, STYLES_LIST[key], value) + apply_rule!(merged_styles: @styles, rule: STYLES_LIST[key], value: value, options: options) end @styles end diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index c34d96b..c4481d1 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -35,9 +35,9 @@ def on_tag_open(tag_name, attributes:, element_styles: '') tag_class = Tag.class_for(tag_name) return unless tag_class - tag_class.new(tag_name, attributes: attributes).tap do |element| - options = { width: pdf.bounds.width, height: pdf.bounds.height } - element.process_styles(element_styles: element_styles, options: options) + options = { width: pdf.bounds.width, height: pdf.bounds.height } + tag_class.new(tag_name, attributes: attributes, options: options).tap do |element| + element.process_styles(element_styles: element_styles) setup_element(element) end end diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index f3006d6..c3ef522 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -15,8 +15,10 @@ class Tag # # @param tag [Symbol] tag name # @param attributes [Hash] hash of element attributes - def initialize(tag, attributes: {}) + # @param options [Hash] options (container width/height/etc.) + def initialize(tag, attributes: {}, options: {}) @tag = tag + @options = options @attrs = Attributes.new(attributes) end @@ -38,12 +40,11 @@ def block_styles # Process tag styles # - # @return element_styles [String] extra styles to apply to the element - # @return options [Hash] options (container width/height/etc.) - def process_styles(element_styles: nil, options: {}) - attrs.merge_text_styles!(tag_styles) if respond_to?(:tag_styles) - attrs.merge_text_styles!(element_styles) if element_styles - attrs.merge_text_styles!(attrs.style) + # @param element_styles [String] extra styles to apply to the element + def process_styles(element_styles: nil) + attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles) + attrs.merge_text_styles!(element_styles, options: options) if element_styles + attrs.merge_text_styles!(attrs.style, options: options) end # Styles to apply on tag closing @@ -82,5 +83,9 @@ def class_for(tag_name) @tag_classes[tag_name] end end + + private + + attr_reader :options end end diff --git a/lib/prawn_html/tags/img.rb b/lib/prawn_html/tags/img.rb index a6e272d..85f0fcc 100644 --- a/lib/prawn_html/tags/img.rb +++ b/lib/prawn_html/tags/img.rb @@ -20,8 +20,9 @@ def custom_render(pdf, context) def adjust_styles(pdf, img_styles) {}.tap do |result| - result[:width] = Utils.convert_size(img_styles['width'], pdf.bounds.width) if img_styles.include?('width') - result[:height] = Utils.convert_size(img_styles['height'], pdf.bounds.height) if img_styles.include?('height') + w, h = img_styles['width'], img_styles['height'] + result[:width] = Utils.convert_size(w, options: pdf.bounds.width) if w + result[:height] = Utils.convert_size(h, options: pdf.bounds.height) if h result[:position] = img_styles[:align] if %i[left center right].include?(img_styles[:align]) end end diff --git a/lib/prawn_html/tags/ol.rb b/lib/prawn_html/tags/ol.rb index 699fc6c..2aadedb 100644 --- a/lib/prawn_html/tags/ol.rb +++ b/lib/prawn_html/tags/ol.rb @@ -9,7 +9,7 @@ class Ol < Tag attr_accessor :counter - def initialize(tag, attributes: {}) + def initialize(tag, attributes: {}, options: {}) super @counter = 0 end diff --git a/lib/prawn_html/utils.rb b/lib/prawn_html/utils.rb index 20eb2c3..602fa35 100644 --- a/lib/prawn_html/utils.rb +++ b/lib/prawn_html/utils.rb @@ -21,7 +21,7 @@ module Utils # @param value [String] HTML string color # # @return [String] adjusted string color or nil if value is invalid - def convert_color(value) + def convert_color(value, options: nil) val = value.to_s.strip.downcase return Regexp.last_match[1] if val.match /\A#([a-f0-9]{6})\Z/ # rubocop:disable Performance/RedundantMatch @@ -42,7 +42,7 @@ def convert_color(value) # @param value [String] string decimal # # @return [Float] converted and rounded float number - def convert_float(value) + def convert_float(value, options: nil) val = value&.gsub(/[^0-9.]/, '') || '' val.to_f.round(4) end @@ -50,14 +50,14 @@ def convert_float(value) # Converts a size string # # @param value [String] size string - # @param container_size [Numeric] container size + # @param options [Numeric] container size # # @return [Float] converted and rounded size - def convert_size(value, container_size = nil) + def convert_size(value, options: nil) val = value&.gsub(/[^0-9.]/, '') || '' val = - if container_size && value.include?('%') - val.to_f * container_size * 0.01 + if options && value&.include?('%') + val.to_f * options * 0.01 else val.to_f * PrawnHtml::PX end @@ -69,7 +69,7 @@ def convert_size(value, container_size = nil) # @param value [String] string # # @return [Symbol] symbol - def convert_symbol(value) + def convert_symbol(value, options: nil) value.to_sym if value && !value.match?(/\A\s*\Z/) end @@ -78,7 +78,7 @@ def convert_symbol(value) # @param value # # @return value - def copy_value(value) + def copy_value(value, options: nil) value end @@ -97,7 +97,7 @@ def normalize_style(value) # @param value [String] string # # @return [String] string without quotes at the beginning/ending - def unquote(value) + def unquote(value, options: nil) (value&.strip || +'').tap do |val| val.gsub!(/\A['"]|["']\Z/, '') end diff --git a/spec/units/prawn_html/attributes_spec.rb b/spec/units/prawn_html/attributes_spec.rb index 7ff41c2..cc8857b 100644 --- a/spec/units/prawn_html/attributes_spec.rb +++ b/spec/units/prawn_html/attributes_spec.rb @@ -24,7 +24,9 @@ end describe '#merge_text_styles!' do - subject(:merge_text_styles!) { attributes.merge_text_styles!(text_styles) } + subject(:merge_text_styles!) { attributes.merge_text_styles!(text_styles, options: options) } + + let(:options) { {} } before do allow(PrawnHtml::Utils).to receive(:send).and_call_original @@ -51,9 +53,20 @@ it 'receives the expected convert messages', :aggregate_failures do merge_text_styles! - expect(PrawnHtml::Utils).to have_received(:send).with(:unquote, "'Times-Roman'") - expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '16px') - expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '22px') + expect(PrawnHtml::Utils).to have_received(:send).with(:unquote, "'Times-Roman'", options: nil) + expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '16px', options: nil) + expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '22px', options: nil) + end + end + + context 'with some options' do + let(:options) { { width: 540, height: 720 } } + let(:text_styles) { 'top: 50%' } + + it 'receives the expected convert messages', :aggregate_failures do + merge_text_styles! + + expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '50%', options: 720) end end end diff --git a/spec/units/prawn_html/utils_spec.rb b/spec/units/prawn_html/utils_spec.rb index ba50b6c..49f07e5 100644 --- a/spec/units/prawn_html/utils_spec.rb +++ b/spec/units/prawn_html/utils_spec.rb @@ -91,7 +91,7 @@ end context 'with a percentage value and a container size (ex. "50%" and 100.242424)' do - subject(:convert_size) { described_class.convert_size(value, 100.242424) } + subject(:convert_size) { described_class.convert_size(value, options: 100.242424) } let(:value) { '50%' } From 1f6516ca80d874ce7b4311e63d9c53efe2b091e0 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Wed, 1 Sep 2021 08:52:48 +0200 Subject: [PATCH 04/26] Wrap PDF bounds in page_width and page_height --- lib/prawn_html/document_renderer.rb | 8 ++++---- lib/prawn_html/pdf_wrapper.rb | 16 +++++++++++++++- lib/prawn_html/tags/img.rb | 4 ++-- spec/units/blocks_spec.rb | 2 +- spec/units/headings_spec.rb | 2 +- spec/units/lists_spec.rb | 2 +- spec/units/misc_spec.rb | 2 +- spec/units/prawn_html/pdf_wrapper_spec.rb | 2 +- spec/units/styles_spec.rb | 2 +- 9 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index c4481d1..56d955d 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -35,7 +35,7 @@ def on_tag_open(tag_name, attributes:, element_styles: '') tag_class = Tag.class_for(tag_name) return unless tag_class - options = { width: pdf.bounds.width, height: pdf.bounds.height } + options = { width: pdf.page_width, height: pdf.page_height } tag_class.new(tag_name, attributes: attributes, options: options).tap do |element| element.process_styles(element_styles: element_styles) setup_element(element) @@ -130,17 +130,17 @@ def bounds(buffer, options, block_styles) x = if block_styles.include?(:right) x1 = pdf.calc_buffer_width(buffer) + block_styles[:right] - x1 < pdf.bounds.width ? (pdf.bounds.width - x1) : 0 + x1 < pdf.page_width ? (pdf.page_width - x1) : 0 else block_styles[:left] || 0 end y = if block_styles.include?(:bottom) pdf.calc_buffer_height(buffer, options) + block_styles[:bottom] else - pdf.bounds.height - (block_styles[:top] || 0) + pdf.page_height - (block_styles[:top] || 0) end - [[x, y], { width: pdf.bounds.width - x }] + [[x, y], { width: pdf.page_width - x }] end end end diff --git a/lib/prawn_html/pdf_wrapper.rb b/lib/prawn_html/pdf_wrapper.rb index d0ea6ed..f5682a5 100644 --- a/lib/prawn_html/pdf_wrapper.rb +++ b/lib/prawn_html/pdf_wrapper.rb @@ -6,7 +6,7 @@ module PrawnHtml class PdfWrapper extend Forwardable - def_delegators :@pdf, :bounds, :start_new_page + def_delegators :@pdf, :start_new_page # Wrapper for Prawn PDF Document # @@ -50,6 +50,20 @@ def calc_buffer_width(buffer) width end + # Height of the page + # + # @return [Float] height + def page_height + pdf.bounds.height + end + + # Width of the page + # + # @return [Float] width + def page_width + pdf.bounds.width + end + # Draw a rectangle # # @param x [Float] left position of the rectangle diff --git a/lib/prawn_html/tags/img.rb b/lib/prawn_html/tags/img.rb index 85f0fcc..cf56c84 100644 --- a/lib/prawn_html/tags/img.rb +++ b/lib/prawn_html/tags/img.rb @@ -21,8 +21,8 @@ def custom_render(pdf, context) def adjust_styles(pdf, img_styles) {}.tap do |result| w, h = img_styles['width'], img_styles['height'] - result[:width] = Utils.convert_size(w, options: pdf.bounds.width) if w - result[:height] = Utils.convert_size(h, options: pdf.bounds.height) if h + result[:width] = Utils.convert_size(w, options: pdf.page_width) if w + result[:height] = Utils.convert_size(h, options: pdf.page_height) if h result[:position] = img_styles[:align] if %i[left center right].include?(img_styles[:align]) end end diff --git a/spec/units/blocks_spec.rb b/spec/units/blocks_spec.rb index f1b7099..fa311a2 100644 --- a/spec/units/blocks_spec.rb +++ b/spec/units/blocks_spec.rb @@ -4,7 +4,7 @@ let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) diff --git a/spec/units/headings_spec.rb b/spec/units/headings_spec.rb index 748f6aa..a045951 100644 --- a/spec/units/headings_spec.rb +++ b/spec/units/headings_spec.rb @@ -6,7 +6,7 @@ let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) diff --git a/spec/units/lists_spec.rb b/spec/units/lists_spec.rb index b3e0401..05210b5 100644 --- a/spec/units/lists_spec.rb +++ b/spec/units/lists_spec.rb @@ -4,7 +4,7 @@ let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index 17d3a29..2234c4b 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -7,7 +7,7 @@ end before do - allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) diff --git a/spec/units/prawn_html/pdf_wrapper_spec.rb b/spec/units/prawn_html/pdf_wrapper_spec.rb index 9a93568..f7d6cee 100644 --- a/spec/units/prawn_html/pdf_wrapper_spec.rb +++ b/spec/units/prawn_html/pdf_wrapper_spec.rb @@ -6,7 +6,7 @@ let(:pdf) { instance_double(Prawn::Document) } describe 'delegated methods' do - %i[bounds start_new_page].each do |method_name| + %i[start_new_page].each do |method_name| context "with #{method_name} method" do before do allow(pdf).to receive(method_name) diff --git a/spec/units/styles_spec.rb b/spec/units/styles_spec.rb index cf3e8f6..b0cce77 100644 --- a/spec/units/styles_spec.rb +++ b/spec/units/styles_spec.rb @@ -4,7 +4,7 @@ let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do - allow(pdf).to receive(:bounds).and_return(OpenStruct.new(width: 0, height: 0)) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) PrawnHtml.append_html(pdf_document, html) From c07d9473c47614de3ca188fa8e4bb6755a064885 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Thu, 2 Sep 2021 08:26:41 +0200 Subject: [PATCH 05/26] Move last_margin in DocumentRenderer --- lib/prawn_html/context.rb | 3 +-- lib/prawn_html/document_renderer.rb | 11 ++++++----- spec/units/prawn_html/context_spec.rb | 4 ---- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 93fb984..d26ba1f 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -4,12 +4,11 @@ module PrawnHtml class Context < Array DEF_FONT_SIZE = 10.3 - attr_accessor :last_margin, :last_text_node + attr_accessor :last_text_node # Init the Context def initialize(*_args) super - @last_margin = 0 @last_text_node = false end diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 56d955d..48a791e 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -11,6 +11,7 @@ class DocumentRenderer def initialize(pdf) @buffer = [] @context = Context.new + @last_margin = 0 @pdf = pdf end @@ -61,14 +62,14 @@ def render output_content(buffer.dup, context.block_styles) buffer.clear - context.last_margin = 0 + @last_margin = 0 end alias_method :flush, :render private - attr_reader :buffer, :context, :pdf + attr_reader :buffer, :context, :last_margin, :pdf def setup_element(element) add_space_if_needed unless render_if_needed(element) @@ -91,14 +92,14 @@ def render_if_needed(element) def apply_tag_close_styles(element) tag_styles = element.tag_close_styles - context.last_margin = tag_styles[:margin_bottom].to_f - pdf.advance_cursor(context.last_margin + tag_styles[:padding_bottom].to_f) + @last_margin = tag_styles[:margin_bottom].to_f + pdf.advance_cursor(last_margin + tag_styles[:padding_bottom].to_f) pdf.start_new_page if tag_styles[:break_after] end def apply_tag_open_styles(element) tag_styles = element.tag_open_styles - move_down = (tag_styles[:margin_top].to_f - context.last_margin) + tag_styles[:padding_top].to_f + move_down = (tag_styles[:margin_top].to_f - last_margin) + tag_styles[:padding_top].to_f pdf.advance_cursor(move_down) if move_down > 0 pdf.start_new_page if tag_styles[:break_before] end diff --git a/spec/units/prawn_html/context_spec.rb b/spec/units/prawn_html/context_spec.rb index 8172831..913ad8c 100644 --- a/spec/units/prawn_html/context_spec.rb +++ b/spec/units/prawn_html/context_spec.rb @@ -6,10 +6,6 @@ it { expect(described_class).to be < Array } describe '#initialize' do - it 'last_margin is set to 0' do - expect(context.last_margin).to be_zero - end - it 'last_text_node is set to false' do expect(context.last_text_node).to be_falsey end From 985ab99d5ae17010a65ae9e9ca67336d7b1e8d2d Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Thu, 2 Sep 2021 08:30:45 +0200 Subject: [PATCH 06/26] Wrap Context pop in remove_last --- lib/prawn_html/context.rb | 6 ++++++ lib/prawn_html/document_renderer.rb | 3 +-- spec/units/prawn_html/context_spec.rb | 16 ++++++++++++++-- spec/units/prawn_html/document_renderer_spec.rb | 4 +++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index d26ba1f..6b8ce67 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -55,6 +55,12 @@ def text_node_styles end end + # Remove the last element from the context + def remove_last + @last_text_node = false + pop + end + private def base_styles diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 48a791e..48e3461 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -21,8 +21,7 @@ def initialize(pdf) def on_tag_close(element) render_if_needed(element) apply_tag_close_styles(element) - context.last_text_node = false - context.pop + context.remove_last end # On tag open callback diff --git a/spec/units/prawn_html/context_spec.rb b/spec/units/prawn_html/context_spec.rb index 913ad8c..3a5131d 100644 --- a/spec/units/prawn_html/context_spec.rb +++ b/spec/units/prawn_html/context_spec.rb @@ -92,8 +92,20 @@ def before_content end end - describe '#text_node_styles' do - subject(:text_node_styles) { context.text_node_styles } + describe '#remove_last' do + subject(:remove_last) { context.remove_last } + + before do + context.add(instance_double(PrawnHtml::Tag, :parent= => true)) + end + + it 'removes the last element from the context' do + expect { remove_last }.to change(context, :size).from(1).to(0) + end + end + + describe '#merged_styles' do + subject(:merged_styles) { context.merged_styles } context 'with no elements' do it { is_expected.to eq(size: PrawnHtml::Context::DEF_FONT_SIZE) } diff --git a/spec/units/prawn_html/document_renderer_spec.rb b/spec/units/prawn_html/document_renderer_spec.rb index 4d7e178..ec1c783 100644 --- a/spec/units/prawn_html/document_renderer_spec.rb +++ b/spec/units/prawn_html/document_renderer_spec.rb @@ -17,12 +17,14 @@ let(:element) { PrawnHtml::Tags::Div.new(:div) } before do + allow(context).to receive(:remove_last) allow(element).to receive(:tag_close_styles).and_call_original end - it 'handles tag closing' do + it 'handles tag closing', :aggregate_failures do on_tag_close expect(element).to have_received(:tag_close_styles) + expect(context).to have_received(:remove_last) end end From 9efc9547090beafc45776ecf4d6a1769c0f25fab Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Thu, 2 Sep 2021 08:32:12 +0200 Subject: [PATCH 07/26] Rename text_node_styles in merged_styles and memoize the styles --- lib/prawn_html/context.rb | 14 +++++++++----- lib/prawn_html/document_renderer.rb | 10 +++++----- spec/units/prawn_html/context_spec.rb | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 6b8ce67..57feb01 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -10,6 +10,7 @@ class Context < Array def initialize(*_args) super @last_text_node = false + @merged_styles = nil end # Add an element to the context @@ -24,6 +25,7 @@ def add(element) element.parent = last push(element) element.on_context_add(self) if element.respond_to?(:on_context_add) + @merged_styles = nil self end @@ -48,15 +50,17 @@ def block_styles # Merge the context styles for text nodes # # @return [Hash] the hash of merged styles - def text_node_styles - each_with_object(base_styles) do |element, res| - evaluate_element_styles(element, res) - element.update_styles(res) if element.respond_to?(:update_styles) - end + def merged_styles + @merged_styles ||= + each_with_object(base_styles) do |element, res| + evaluate_element_styles(element, res) + element.update_styles(res) if element.respond_to?(:update_styles) + end end # Remove the last element from the context def remove_last + @merged_styles = nil @last_text_node = false pop end diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 48e3461..4b62181 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -37,8 +37,7 @@ def on_tag_open(tag_name, attributes:, element_styles: '') options = { width: pdf.page_width, height: pdf.page_height } tag_class.new(tag_name, attributes: attributes, options: options).tap do |element| - element.process_styles(element_styles: element_styles) - setup_element(element) + setup_element(element, element_styles: element_styles) end end @@ -50,7 +49,7 @@ def on_tag_open(tag_name, attributes:, element_styles: '') def on_text_node(content) return if content.match?(/\A\s*\Z/) - buffer << context.text_node_styles.merge(text: prepare_text(content)) + buffer << context.merged_styles.merge(text: prepare_text(content)) context.last_text_node = true nil end @@ -70,10 +69,11 @@ def render attr_reader :buffer, :context, :last_margin, :pdf - def setup_element(element) + def setup_element(element, element_styles:) add_space_if_needed unless render_if_needed(element) - apply_tag_open_styles(element) context.add(element) + element.process_styles(element_styles: element_styles) + apply_tag_open_styles(element) element.custom_render(pdf, context) if element.respond_to?(:custom_render) end diff --git a/spec/units/prawn_html/context_spec.rb b/spec/units/prawn_html/context_spec.rb index 3a5131d..77a67ba 100644 --- a/spec/units/prawn_html/context_spec.rb +++ b/spec/units/prawn_html/context_spec.rb @@ -120,7 +120,7 @@ def before_content end it 'merges the styles of the elements' do - expect(text_node_styles).to match(color: 'abc', size: 12.34) + expect(merged_styles).to match(color: 'abc', size: 12.34) end end @@ -141,7 +141,7 @@ def update_styles(res) end it 'sends the context styles to the update_styles method', :aggregate_failures do - expect(text_node_styles).to match(color: 'fb1', size: 12.34, some_style: :some_value) + expect(merged_styles).to match(color: 'fb1', size: 12.34, some_style: :some_value) expect(tag2).to have_received(:update_styles) end end From 6146b5fefbe8f097a52bdde54b359e7a1d600ee5 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 09:55:40 +0200 Subject: [PATCH 08/26] Bump to version 0.4.2 --- lib/prawn_html/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prawn_html/version.rb b/lib/prawn_html/version.rb index f7c961b..0fbb52b 100644 --- a/lib/prawn_html/version.rb +++ b/lib/prawn_html/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module PrawnHtml # :nodoc: - VERSION = '0.4.0' + VERSION = '0.4.2' end From 0c3d8c8482d5d2662c4177f9837f690b37919aa3 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Thu, 2 Sep 2021 22:45:41 +0200 Subject: [PATCH 09/26] Improve callbacks handling --- lib/prawn_html/attributes.rb | 6 +++--- lib/prawn_html/callbacks/highlight.rb | 4 ++-- lib/prawn_html/document_renderer.rb | 5 +++-- lib/prawn_html/tags/del.rb | 2 +- lib/prawn_html/tags/mark.rb | 2 +- lib/prawn_html/utils.rb | 22 ++++++++++++++++++++-- spec/units/prawn_html/tags/del_spec.rb | 2 +- spec/units/prawn_html/tags/mark_spec.rb | 2 +- spec/units/prawn_html/utils_spec.rb | 22 ++++++++++++++++++++++ 9 files changed, 54 insertions(+), 13 deletions(-) diff --git a/lib/prawn_html/attributes.rb b/lib/prawn_html/attributes.rb index 4baedc2..f3d5328 100644 --- a/lib/prawn_html/attributes.rb +++ b/lib/prawn_html/attributes.rb @@ -10,13 +10,13 @@ class Attributes < OpenStruct block: %i[align bottom leading left margin_left padding_left position right top], tag_close: %i[margin_bottom padding_bottom break_after], tag_open: %i[margin_top padding_top break_before], - text_node: %i[background callback character_spacing color font link list_style_type size styles white_space] + text_node: %i[callback character_spacing color font link list_style_type size styles white_space] }.freeze STYLES_LIST = { # text node styles - 'background' => { key: :background, set: :convert_color }, - 'callback' => { key: :callback, set: :copy_value }, + 'background' => { key: :callback, set: :callback_background }, + 'callback-strike-through' => { key: :callback, set: :callback_strike_through }, 'color' => { key: :color, set: :convert_color }, 'font-family' => { key: :font, set: :unquote }, 'font-size' => { key: :size, set: :convert_size }, diff --git a/lib/prawn_html/callbacks/highlight.rb b/lib/prawn_html/callbacks/highlight.rb index a4dce8f..5ae05d4 100644 --- a/lib/prawn_html/callbacks/highlight.rb +++ b/lib/prawn_html/callbacks/highlight.rb @@ -5,9 +5,9 @@ module Callbacks class Highlight DEF_HIGHLIGHT = 'ffff00' - def initialize(pdf, item) + def initialize(pdf, color = nil) @pdf = pdf - @color = item.delete(:background) || DEF_HIGHLIGHT + @color = color || DEF_HIGHLIGHT end def render_behind(fragment) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 4b62181..e3fed22 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -120,8 +120,9 @@ def output_content(buffer, block_styles) def apply_callbacks(buffer) buffer.select { |item| item[:callback] }.each do |item| - callback = Tag::CALLBACKS[item[:callback]] - item[:callback] = callback.new(pdf, item) + callback, arg = item[:callback] + callback_class = Tag::CALLBACKS[callback] + item[:callback] = callback_class.new(pdf, arg) end end diff --git a/lib/prawn_html/tags/del.rb b/lib/prawn_html/tags/del.rb index c10adac..23d599a 100644 --- a/lib/prawn_html/tags/del.rb +++ b/lib/prawn_html/tags/del.rb @@ -6,7 +6,7 @@ class Del < Tag ELEMENTS = [:del, :s].freeze def tag_styles - 'callback: StrikeThrough' + 'callback-strike-through: 1' end end end diff --git a/lib/prawn_html/tags/mark.rb b/lib/prawn_html/tags/mark.rb index 659dc99..c80e884 100644 --- a/lib/prawn_html/tags/mark.rb +++ b/lib/prawn_html/tags/mark.rb @@ -6,7 +6,7 @@ class Mark < Tag ELEMENTS = [:mark].freeze def tag_styles - 'callback: Highlight' + 'background: #ff0' end end end diff --git a/lib/prawn_html/utils.rb b/lib/prawn_html/utils.rb index 602fa35..023969f 100644 --- a/lib/prawn_html/utils.rb +++ b/lib/prawn_html/utils.rb @@ -10,6 +10,24 @@ module Utils 'underline' => :underline }.freeze + # Setup a background callback + # + # @param value [String] HTML string color + # + # @return [Array] callback name and argument value + def callback_background(value, options: nil) + ['Highlight', convert_color(value, options: options)] + end + + # Setup a strike through callback + # + # @param value [String] unused + # + # @return [Array] callback name and argument value + def callback_strike_through(value, options: nil) + ['StrikeThrough', nil] + end + # Converts a color string # # Supported formats: @@ -103,7 +121,7 @@ def unquote(value, options: nil) end end - module_function :convert_color, :convert_float, :convert_size, :convert_symbol, :copy_value, :normalize_style, - :unquote + module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size, + :convert_symbol, :copy_value, :normalize_style, :unquote end end diff --git a/spec/units/prawn_html/tags/del_spec.rb b/spec/units/prawn_html/tags/del_spec.rb index 0d5cd93..134d2e7 100644 --- a/spec/units/prawn_html/tags/del_spec.rb +++ b/spec/units/prawn_html/tags/del_spec.rb @@ -13,7 +13,7 @@ end it 'merges the callback property into styles' do - expect(styles).to match(color: 'ffbb11', callback: 'StrikeThrough') + expect(styles).to match(color: 'ffbb11', callback: ['StrikeThrough', nil]) end end end diff --git a/spec/units/prawn_html/tags/mark_spec.rb b/spec/units/prawn_html/tags/mark_spec.rb index 0957719..6eb16cf 100644 --- a/spec/units/prawn_html/tags/mark_spec.rb +++ b/spec/units/prawn_html/tags/mark_spec.rb @@ -13,7 +13,7 @@ end it 'merges the callback property into styles' do - expect(styles).to match(color: 'ffbb11', callback: 'Highlight') + expect(styles).to match(color: 'ffbb11', callback: ['Highlight', 'ffff00']) end end end diff --git a/spec/units/prawn_html/utils_spec.rb b/spec/units/prawn_html/utils_spec.rb index 49f07e5..a386726 100644 --- a/spec/units/prawn_html/utils_spec.rb +++ b/spec/units/prawn_html/utils_spec.rb @@ -1,6 +1,28 @@ # frozen_string_literal: true RSpec.describe PrawnHtml::Utils do + describe '.callback_background' do + subject(:callback_background) { described_class.callback_background(value) } + + context 'with a nil value' do + let(:value) { nil } + + it { is_expected.to eq ['Highlight', nil] } + end + + context 'with a color value' do + let(:value) { 'red' } + + it { is_expected.to eq ['Highlight', 'ff0000'] } + end + end + + describe '.callback_strike_through' do + subject(:callback_strike_through) { described_class.callback_strike_through(nil) } + + it { is_expected.to eq ['StrikeThrough', nil] } + end + describe '.convert_color' do subject(:convert_color) { described_class.convert_color(value) } From f918951e1bed03085cd6cfcbf241b81c73050be4 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Thu, 2 Sep 2021 22:59:56 +0200 Subject: [PATCH 10/26] Rename Highlight callback to Background --- lib/prawn_html/callbacks/{highlight.rb => background.rb} | 2 +- lib/prawn_html/tag.rb | 2 +- lib/prawn_html/utils.rb | 2 +- spec/units/prawn_html/tags/mark_spec.rb | 2 +- spec/units/prawn_html/utils_spec.rb | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) rename lib/prawn_html/callbacks/{highlight.rb => background.rb} (95%) diff --git a/lib/prawn_html/callbacks/highlight.rb b/lib/prawn_html/callbacks/background.rb similarity index 95% rename from lib/prawn_html/callbacks/highlight.rb rename to lib/prawn_html/callbacks/background.rb index 5ae05d4..4038b59 100644 --- a/lib/prawn_html/callbacks/highlight.rb +++ b/lib/prawn_html/callbacks/background.rb @@ -2,7 +2,7 @@ module PrawnHtml module Callbacks - class Highlight + class Background DEF_HIGHLIGHT = 'ffff00' def initialize(pdf, color = nil) diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index c3ef522..d098700 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -3,7 +3,7 @@ module PrawnHtml class Tag CALLBACKS = { - 'Highlight' => Callbacks::Highlight, + 'Background' => Callbacks::Background, 'StrikeThrough' => Callbacks::StrikeThrough }.freeze TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze diff --git a/lib/prawn_html/utils.rb b/lib/prawn_html/utils.rb index 023969f..d4c49f5 100644 --- a/lib/prawn_html/utils.rb +++ b/lib/prawn_html/utils.rb @@ -16,7 +16,7 @@ module Utils # # @return [Array] callback name and argument value def callback_background(value, options: nil) - ['Highlight', convert_color(value, options: options)] + ['Background', convert_color(value, options: options)] end # Setup a strike through callback diff --git a/spec/units/prawn_html/tags/mark_spec.rb b/spec/units/prawn_html/tags/mark_spec.rb index 6eb16cf..078ab06 100644 --- a/spec/units/prawn_html/tags/mark_spec.rb +++ b/spec/units/prawn_html/tags/mark_spec.rb @@ -13,7 +13,7 @@ end it 'merges the callback property into styles' do - expect(styles).to match(color: 'ffbb11', callback: ['Highlight', 'ffff00']) + expect(styles).to match(color: 'ffbb11', callback: ['Background', 'ffff00']) end end end diff --git a/spec/units/prawn_html/utils_spec.rb b/spec/units/prawn_html/utils_spec.rb index a386726..c56f12a 100644 --- a/spec/units/prawn_html/utils_spec.rb +++ b/spec/units/prawn_html/utils_spec.rb @@ -7,13 +7,13 @@ context 'with a nil value' do let(:value) { nil } - it { is_expected.to eq ['Highlight', nil] } + it { is_expected.to eq ['Background', nil] } end context 'with a color value' do let(:value) { 'red' } - it { is_expected.to eq ['Highlight', 'ff0000'] } + it { is_expected.to eq ['Background', 'ff0000'] } end end From f7242db70764c98304769a563cabb720c9c6166e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 09:48:20 +0200 Subject: [PATCH 11/26] Handle Del tag style using `text-decoration: line-through` --- lib/prawn_html/attributes.rb | 28 +++++++++++++++++++--------- lib/prawn_html/tags/del.rb | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/prawn_html/attributes.rb b/lib/prawn_html/attributes.rb index f3d5328..fc4a349 100644 --- a/lib/prawn_html/attributes.rb +++ b/lib/prawn_html/attributes.rb @@ -16,7 +16,6 @@ class Attributes < OpenStruct STYLES_LIST = { # text node styles 'background' => { key: :callback, set: :callback_background }, - 'callback-strike-through' => { key: :callback, set: :callback_strike_through }, 'color' => { key: :color, set: :convert_color }, 'font-family' => { key: :font, set: :unquote }, 'font-size' => { key: :size, set: :convert_size }, @@ -25,7 +24,7 @@ class Attributes < OpenStruct 'href' => { key: :link, set: :copy_value }, 'letter-spacing' => { key: :character_spacing, set: :convert_float }, 'list-style-type' => { key: :list_style_type, set: :unquote }, - 'text-decoration' => { key: :styles, set: :append_styles }, + 'text-decoration' => { key: :styles, set: :append_text_decoration }, 'vertical-align' => { key: :styles, set: :append_styles }, 'white-space' => { key: :white_space, set: :convert_symbol }, # tag opening styles @@ -103,6 +102,24 @@ def parse_styles(styles) private + def process_styles(hash_styles, options:) + hash_styles.each do |key, value| + rule = evaluate_rule(key, value) + apply_rule!(merged_styles: @styles, rule: rule, value: value, options: options) + end + @styles + end + + def evaluate_rule(rule_key, attr_value) + rule = STYLES_LIST[rule_key] + if rule && rule[:set] == :append_text_decoration + return { key: :callback, set: :callback_strike_through } if attr_value == 'line-through' + + return { key: :styles, set: :append_styles } + end + rule + end + def apply_rule!(merged_styles:, rule:, value:, options:) return unless rule @@ -113,12 +130,5 @@ def apply_rule!(merged_styles:, rule:, value:, options:) merged_styles[rule[:key]] = Utils.send(rule[:set], value, options: opts) end end - - def process_styles(hash_styles, options:) - hash_styles.each do |key, value| - apply_rule!(merged_styles: @styles, rule: STYLES_LIST[key], value: value, options: options) - end - @styles - end end end diff --git a/lib/prawn_html/tags/del.rb b/lib/prawn_html/tags/del.rb index 23d599a..33aacca 100644 --- a/lib/prawn_html/tags/del.rb +++ b/lib/prawn_html/tags/del.rb @@ -6,7 +6,7 @@ class Del < Tag ELEMENTS = [:del, :s].freeze def tag_styles - 'callback-strike-through: 1' + 'text-decoration: line-through' end end end From 91118729e5018694f4b444384a4ea3125eaec1b8 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 12:27:08 +0200 Subject: [PATCH 12/26] Store the previous tag in the context --- lib/prawn_html/context.rb | 3 +++ spec/units/prawn_html/context_spec.rb | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 57feb01..880ebda 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -4,6 +4,7 @@ module PrawnHtml class Context < Array DEF_FONT_SIZE = 10.3 + attr_reader :previous_tag attr_accessor :last_text_node # Init the Context @@ -11,6 +12,7 @@ def initialize(*_args) super @last_text_node = false @merged_styles = nil + @previous_tag = nil end # Add an element to the context @@ -62,6 +64,7 @@ def merged_styles def remove_last @merged_styles = nil @last_text_node = false + @previous_tag = last.tag pop end diff --git a/spec/units/prawn_html/context_spec.rb b/spec/units/prawn_html/context_spec.rb index 77a67ba..bad891c 100644 --- a/spec/units/prawn_html/context_spec.rb +++ b/spec/units/prawn_html/context_spec.rb @@ -96,11 +96,15 @@ def before_content subject(:remove_last) { context.remove_last } before do - context.add(instance_double(PrawnHtml::Tag, :parent= => true)) + context.add(instance_double(PrawnHtml::Tag, :parent= => true, tag: :some_tag)) end it 'removes the last element from the context' do - expect { remove_last }.to change(context, :size).from(1).to(0) + expect { remove_last }.to( + change(context, :size).from(1).to(0).and( + change(context, :previous_tag).from(nil).to(:some_tag) + ) + ) end end From de81f02ed6de20f60683e11cc03e9dd1ed0b69ee Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 12:28:07 +0200 Subject: [PATCH 13/26] Fix Br tag spacing --- examples/misc_elements.pdf | Bin 5404 -> 5404 bytes examples/random_content.pdf | Bin 40635 -> 40235 bytes lib/prawn_html/tags/br.rb | 4 ++-- spec/units/prawn_html/tags/br_spec.rb | 15 ++++++++++++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index 2d32f70250b3df828c8ac3d40c1f3ef075687509..a2fa103422b5b1c6e08bedd27df089eaec071824 100644 GIT binary patch delta 267 zcmX|+JxT;Y5QTBJr&<~pOx$=xO*Sa{r>eR-TnzS{{nR2lJOq)7REGS&x(_!YoZki^lp(jNoU(1ZBl~s&BNCS~$%~$W zYD4mr%6j%=L6S<7{nhN4Q8U}MTUmm%FIA9oooP?Nz9dxT8TSDU+DewW4sAi-QY0=WFxkjrFGc|DNanJa^CC`<=w*yF_~jF*JSQPW>Ui zuAc8sBpH`m2TzZrxF&`}Yv@_D3t%$4*ikRCt1%KUYsNML37~e7#>#L5z?LTD?Wc6b4`~U+i<7aN$IURr@R@MoJas zTq+dP+7T`t^dw8rESyhEMXZsqkS~Q*eie!-PKh>t~KS_V!2>C7uow@4oq6wAMVa6R%oLdV&GbO0t znJFy^WkM@_V+l>qF+6(@ErcH7Em&#dj5P{Aug-6`@P0C2oH!>e$Npuh5gZHdpIC1< zC-RR59zVEw3sKAj%&lE(GKJ}%h3{e%Zm1QGbunXH7qiB6F=zC@%+dcaKjbj)WUR6C d6X-Ha|5qF9l^y{r>r;cmS*OwD_+F-1-0j delta 914 zcmZvaKWI}y9LH%&Nc(J)w0X%(+B7|(EjW0&zwhp(WDqS#6s;6R99lKeDm2Ab&`EI; zacTHLK^xI5E?KOli?xb_I6Am0*ulZ2n}Z|&a zXp*gH)xX1C{virdmzZd_Dwo z&a0+6lzi#)UqRQ!io$H#fz8wxUnQYBbZ*3zShz$Av|uqsu#ik6x)0$)x)^D}e$Lb3 zkABYaVb}X^mkZa+5R71EsODZsox;2HvhQ>tKeE|W!OZg({ywu55*iDo6;x9;-uNtDy=6^KqU`Bn>BgOTq-lBA2^+6{DJhyMSlKBK z)4geUhK>fjL3OuFGiq|cIsDz!hOAi=Bq`puIS&akH zoY4-Zi0NROs19a`+s+I*fKf^szI;nz!wxWV)!qy@nza}4W>{+YR!MhB_*0qa6NE|E L8XrG-cFOt>K2zfR diff --git a/lib/prawn_html/tags/br.rb b/lib/prawn_html/tags/br.rb index b3b021a..30dccd1 100644 --- a/lib/prawn_html/tags/br.rb +++ b/lib/prawn_html/tags/br.rb @@ -5,14 +5,14 @@ module Tags class Br < Tag ELEMENTS = [:br].freeze - BR_SPACING = Utils.convert_size('12') + BR_SPACING = Utils.convert_size('17') def block? true end def custom_render(pdf, context) - return if context.last_text_node + return if context.last_text_node || context.previous_tag != :br pdf.advance_cursor(BR_SPACING) end diff --git a/spec/units/prawn_html/tags/br_spec.rb b/spec/units/prawn_html/tags/br_spec.rb index f3cd41d..dda38f3 100644 --- a/spec/units/prawn_html/tags/br_spec.rb +++ b/spec/units/prawn_html/tags/br_spec.rb @@ -14,12 +14,21 @@ describe '#custom_render' do subject(:custom_render) { br.custom_render(pdf, context) } - let(:context) { instance_double(PrawnHtml::Context, last_text_node: false) } + let(:context) { instance_double(PrawnHtml::Context, last_text_node: false, previous_tag: nil) } let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true) } - it 'calls advance_cursor on the pdf wrapper' do + it "doesn't call advance_cursor on the pdf wrapper" do custom_render - expect(pdf).to have_received(:advance_cursor) + expect(pdf).not_to have_received(:advance_cursor) + end + + context 'when the last node in the context is another br' do + let(:context) { instance_double(PrawnHtml::Context, last_text_node: false, previous_tag: :br) } + + it 'calls advance_cursor on the pdf wrapper' do + custom_render + expect(pdf).to have_received(:advance_cursor) + end end context 'when the last node in the context is of type text' do From 2e9d311a031d3a988be6a7740706c4446656db43 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 14:00:00 +0200 Subject: [PATCH 14/26] Update Br specs --- spec/integrations/misc_spec.rb | 10 ---------- spec/integrations/tags/br_spec.rb | 19 +++++++++++++++++++ spec/support/shared_contexts.rb | 15 +++++++++++++++ spec/units/misc_spec.rb | 16 ---------------- spec/units/prawn_html/tags/br_spec.rb | 24 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 spec/integrations/tags/br_spec.rb create mode 100644 spec/support/shared_contexts.rb diff --git a/spec/integrations/misc_spec.rb b/spec/integrations/misc_spec.rb index 5df5ccd..778c5cf 100644 --- a/spec/integrations/misc_spec.rb +++ b/spec/integrations/misc_spec.rb @@ -56,14 +56,4 @@ let(:expected_font_family) { 'Helvetica-Bold' } end end - - context 'with some br elements' do - let(:html) { 'First line
Second line
Third line' } - - it 'renders some breaking line elements' do - text_analysis = PDF::Inspector::Text.analyze(pdf.render) - - expect(text_analysis.strings).to match_array(['First line', 'Second line', 'Third line']) - end - end end diff --git a/spec/integrations/tags/br_spec.rb b/spec/integrations/tags/br_spec.rb new file mode 100644 index 0000000..af67f57 --- /dev/null +++ b/spec/integrations/tags/br_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +RSpec.describe 'Misc' do + let(:pdf) { Prawn::Document.new(page_size: 'A4', page_layout: :portrait) } + let(:text_analysis) { PDF::Inspector::Text.analyze(pdf.render) } + + before do + PrawnHtml.append_html(pdf, html) + end + + context 'with some br elements' do + let(:html) { 'First line
Second line
Third line

Last line' } + + it 'renders some breaking line elements' do + expected_strings = ['First line', 'Second line', 'Third line', 'Last line'] + expect(text_analysis.strings).to match_array(expected_strings) + end + end +end diff --git a/spec/support/shared_contexts.rb b/spec/support/shared_contexts.rb new file mode 100644 index 0000000..82d4ad6 --- /dev/null +++ b/spec/support/shared_contexts.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.shared_context 'with pdf wrapper' do + let(:pdf) do + methods = { advance_cursor: true, puts: true, horizontal_rule: true } + instance_double(PrawnHtml::PdfWrapper, methods) + end + + def append_html_to_pdf(html) + allow(pdf).to receive_messages(page_width: 540, page_height: 720) + allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) + pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) + PrawnHtml.append_html(pdf_document, html) + end +end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index 2234c4b..bdc88aa 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -36,22 +36,6 @@ end end - context 'with br elements' do - let(:html) { 'First line
Second line
Third line' } - - it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "First line" }], {}, bounding_box: nil - ) - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Second line" }], {}, bounding_box: nil - ) - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Third line" }], {}, bounding_box: nil - ) - end - end - context 'with some content in an element b' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } diff --git a/spec/units/prawn_html/tags/br_spec.rb b/spec/units/prawn_html/tags/br_spec.rb index dda38f3..36d454e 100644 --- a/spec/units/prawn_html/tags/br_spec.rb +++ b/spec/units/prawn_html/tags/br_spec.rb @@ -40,4 +40,28 @@ end end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'First line
Second line
Third line

Last line' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf', :aggregate_failures do + expected_text = { text: "First line", size: TestUtils.default_font_size } + expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + + expected_text = { text: "Second line", size: TestUtils.default_font_size } + expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + + expected_text = { text: "Third line", size: TestUtils.default_font_size } + expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + + expect(pdf).to have_received(:advance_cursor).with(described_class::BR_SPACING).ordered + + expected_text = { text: "Last line", size: TestUtils.default_font_size } + expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + end + end end From c5966eaf06ea6c3157b5b2bfa1de6d214c1b8688 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 14:45:21 +0200 Subject: [PATCH 15/26] Update default font size --- examples/headings.pdf | Bin 3277 -> 3275 bytes examples/misc_elements.pdf | Bin 5404 -> 5429 bytes examples/random_content.pdf | Bin 40235 -> 40565 bytes examples/styles.pdf | Bin 3253 -> 3257 bytes lib/prawn_html/context.rb | 2 +- spec/integrations/lists_spec.rb | 6 +++--- spec/units/prawn_html/tags/small_spec.rb | 2 +- 7 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/headings.pdf b/examples/headings.pdf index 80cd66590918eae87949e99f886c51f2846d4c9f..cd67a5c228f4767382acd26aecb5c2d7b9f303f4 100644 GIT binary patch delta 360 zcmZY3u}T9$5C&i_CgyTz=P9w;CWXy7v$H!p3o#;SS{rLC+gMs#+K9r6rT7ZL7qC;1 zV6T`rh|l0ftk$W9pO2Y;)_?2YE{ZVy{w!(>9MFn6IjeTZ7SKcxN%PjtY5-Q|v|bFi zJVziO*kwPI_>P&0uq^B#-^Rel9~!pQPWp3 z++-C%3Y58d>$+qih}wKT=#Dl7lu0vn*Zo*tbLf@RMY-2>L&4}X_F68s1tKX0GpW3f qVAa_07d^fAu39_J;Bw5lseTu<>g>NPI!u>qhdqwfU~qIiu6_V(4psR8 delta 368 zcmZXQJx&8b4288zqWuwctfEj&lY+*0Y|pGmib4uVOG8VCsDL9-(;yHdQ6jDoaR9nB zC>H>c8-z2kK~#e)>*>>betsV=hY#y4%)dXg#zJ7WVouNNgBbxDi@rby?l!``+MY%(bb17bF$=rv;&Svwc*Ftw*EdlUe-(leSj% diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index a2fa103422b5b1c6e08bedd27df089eaec071824..e34e67570d43c1f0f6156438752d3873b147f95c 100644 GIT binary patch delta 1573 zcmZ`(&1w`u5GK28LMDG3=Yc{i+Nl%>9Q1w;SS5;s2r{$C7_kXG+b@-Cp43i`KH7~3U zha*d4AON*=;p52K5(*?QT}lRp2I(p7y-4ovXd%-KpY{%ty?a)LuhoTd07e@HmLpeN z03^scuyL%)W5d%*d!Yd0oHi?Pn1C_Df+ZC`SGML15%}}6!_dDKYIs53S{->dtD6lg zyeDTOPpGb5cUF)z8dH)vD4bT?x22E>MQao~TU&29VGw2B|B%SD+M`YG(}MC$+!!yE zODzpdFO%IQ+3F=m36nBep->x$vj47in+!m!e9U>{TDOTHtW!E3!CQ7|DZB(1&)^rk zloX&q#eCOB*LiT78;MyQv4?xK5@HhH*~1NyH!+9#v2K&HL3$Z+I6fSc)rym>uDv;D zd})JI6T9<90JwOA=aM4HlDZW>Omv!7D61L8BXa1SO`WhF!|6ne`D)`!iyociMY|kK zItOKoUQq&^;S@vbRA0VB1- zEM{Q8Tso{xi574@58Cs+{Rj1Ws>AX&d@1o}PuODwDO1wd8GBH8#FD)3Oz$kR_CZU< z7vY2gM0df9uTTTI@NRk~MI%^*NQ}EW*U#zoHUJ+I3xz?Nsi@*=2FYj!m%<)-$rP8_B>p-Z5Eh5j#kkw&h?(7XwDzh(r zl_vckskHPZTlUDrICF)~N-W`c?y(I~+rF(`D2$G2L`Be(2|sRZj}s)F&i3P-_@P;gJXpMVrMligKp*&G@7+Th`_=m!4p0S`v38C?2Z25h zJx;chWOFBR8k|6sfM~E-~nM8B6!MNHE3S!b7vZamKp+j`Y-6!bi2V1w=z=g z8)sNG>x!zquC+7KoQy>4)V2gW+>`3Lnr9}}xpxtyw3L3Vtq9j!G=~hkxYF<1`VHP^ zUNx10_;0X2#I^3tD0Yc*7)gCJ(`oU$G6Kl!Hv)0__wj3{)#6E03>j|PVtuuV1mt}_ zp5g8N`ewF3Mp<7R{MWjiQ6Y0OXb5s;irPv*LpEGF%C_rX^34--5* AbpQYW diff --git a/examples/random_content.pdf b/examples/random_content.pdf index 8d116709fa6b1b9a635e94e2c39131dca54de8e6..3707a60a49679b3d6148a7240630ca1b3d6effe9 100644 GIT binary patch delta 2757 zcmaJ@OK4nG7$y^wCO2&w)66tYlgUY&)KKN*ydN=yq%?uHptTNIs9k82Akrk-mSQPB zHtIr<%2`Nipt@2J+|)%CQ5Rhlj9WJn@UaoZmAdFc@w=JanLB41;V$m|{{Q^v```D| zFAINvRk-ncLFo7X*xcu|cZyUNl8Jdi8S&JZu-S60tf*4X5~~c&si;ilN@4aHMJ4mb z#5$Q7QdvsQO^FMFO`j)~lgV5qNs?og#Cgi)|C|d+>!C9_B~1-WIW8w$;9Y3taAk&G zOrGAdnwK?D&gg{et;0mYEvB@HIm^tiFIq>4u~d2Po0N2>biZ|WKvSrgl$F+=3Q*$P zQ21uBNK!|o!2!!SuH?={F}~S07Cw7uKjQ3gnIy(D_=cCpRHSO1Z3oMqGUYv}dFV46 zXY6L%Xjp0+E<(-VvL++PrVRsOoU8FQ8InbCi>{H1YB(k$6NBxlBqX)cv)Gft_JQz5 z`(Tj^>b28N2M5AN`@vbm1m#v+kz-JU8YpOIsnOQQK9292ELo)1S{Xa|3Pc9c_Qu7; z??I$BmE4G!cz?%a5e1>8wh`$UIwngJ-fGQb7Iz>L@Dvpov*_rYEO|%0S1e|6w)3$W zB-m*#aw$+XC<}lVR1*a+rIae>d$)7EXf)MY_=s;I882x?trarnJCm@Ik|4!x%=ayb z;HBVP#C$g(f|p7%^mk)L_I4efQCy+qt#kEc)}hRot$LE)gBIhFk*i(LmK^#?cpVe| zt&5c`gL&s;!gTl9F?5MHkVxYwdej<%I(FPkLmWhkTv|+)!mUDagv*-l4)!*8yAkd= z3La%o6{6tx{nqy3w?9q-|1d0bAuWN3;AG`{=G{*oOKULJPn0m&7@)?m9fBR6{99#$50tWZIXI<<*B)iWQ+!xN9A zJDln5eSa8}Rxr!!q?`DqcEz{`HG#nzlgtQIHQhAHP5=W!pozR{uw6VWtKFZDfO&YT+hJco3Y*K%J~KQ8TZ7=@1c@Lbv;!>`R@RCvZPlxsc2Yf!Bh z)53EClZ9iF;thkkG4^}Nah3j-Xof9)VL6#N!O8K}GYYHU0GSZa+#dtJVgl?w;w;WT6iR1uaY`pvB|aaFzOf@B3%v`)-K!| zJrYigE-DOfhV~M~aq6&nr9y5LGva(QQMvZgBB@@KUb(oku=@Oq375Bj9sTEOiwZw~ zaPf6mBZv!QWQ8PKFU)xqYJ_=)Ef`}QL5Q*R_a(+8w@sL;>K)Esj9-UlMXSS)>qB6B TfBndAY*l8Gv9Y-)=ac^cXvJh@ delta 2650 zcmZ`*OK4nG7$%d~%uSky$=qo(>D<#iN`3U?ydNcmOicrpV$?1LDbyl% zAy!vOa~2|~?aqZ8B^!~7F5F3~TNhaYf%Z$uiLW}2vbBjR`oLvko!%DT@=6c)578)FhsgC8Y z1(Z`P6&G+{0Ggj`^1gr~ZkpLARKV#pvx#(Gk2c8|OLd?}<9Q!5@LXWX4GltKdqXssvd z)xtBg+F+t>x^zSxN}?edmPja$DHERfYB1z}DHKDfWW^!5nK#@XoC*cRkxNf}H6S5_ zI11tkzYQixGZoSky$2=&ag@Z9GSo9(pic9N@2sO zr}&Q^68p)O@)YCVSH={!npngI)Gnu-AtkU8#vpi-8f`8IEADQth$vitKx}c=ue}B_ zLb0J%Bi@2^tG$941Cn5HWed`cBXps`;TT>y$XW}S21#5<*qDGxY*MDjd-~szQ)R)h z6)OFXc>>iM&I^4{g-l`1xYyLT`Zg+1&6F6#TTn*(R|YI5l&S{B?euR~EH*X`W@>GA z;LXTVPzJ&>XD7|l{XB3xQUoh1d6rXe%(DaY?(hC7_i(T`sx^HB5pJP1eBz^sOVDLg zVm)0)-S*(plQ3rvDvwIc7(C}@-ob2A(0|>ZL+3*Rnc>iln@R4%@OX)VD#k6OvUy;> zAKRd7X%UB@+4lYLM5rL^B7W@6rhkW@k6V+`A}h&VDo*4XgiFI~a~~GpDL_d=Qxr_? z_J#Sj#?ng2549TT7u@IHREZYu*Mgno&b zKV*_!RRo?LuQ-jsl2f!NwbLM;I3#jU?Yo{uAZ@GisZ# zPP@~_o_KMo-XNb=3>*YgFwyK?@YNf!t4xqDu_AP|Tp`KF7P+BL~6o7BiG pKYSf+%(Iaw{4I5<%0+|3xV{U>Yj=V^UTp1c8bnQ^CPrwwl2})Z8t7S?o0*}@D6y>;1vF2;8C#eeper)vaG$)BJp*WjnWd2trtCT~6H`5NOLKG=ZT`t&#%ybMwXW5T&k+B{%%|Vx&&G> diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 880ebda..86d3067 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -2,7 +2,7 @@ module PrawnHtml class Context < Array - DEF_FONT_SIZE = 10.3 + DEF_FONT_SIZE = 9.66 attr_reader :previous_tag attr_accessor :last_text_node diff --git a/spec/integrations/lists_spec.rb b/spec/integrations/lists_spec.rb index 0604f31..7a12435 100644 --- a/spec/integrations/lists_spec.rb +++ b/spec/integrations/lists_spec.rb @@ -49,9 +49,9 @@ y = pdf.y - font.ascender expected_array = [ - [x, y.round(4)], - [x, (y - font.height).round(4)], - [x, (y - font.height * 2).round(4)] + [x, y.round(5)], + [x, (y - font.height).round(5)], + [x, (y - font.height * 2).round(5)] ] expect(text_analysis.positions).to match_array(expected_array) diff --git a/spec/units/prawn_html/tags/small_spec.rb b/spec/units/prawn_html/tags/small_spec.rb index 0588d15..38170c4 100644 --- a/spec/units/prawn_html/tags/small_spec.rb +++ b/spec/units/prawn_html/tags/small_spec.rb @@ -11,7 +11,7 @@ let(:styles) { { some_attr: 'some_value' } } it 'updates the argument styles reducing the default font size' do - expect(update_styles).to eq(some_attr: 'some_value', size: 8.755) + expect(update_styles).to eq(some_attr: 'some_value', size: 8.211) end context 'with a parent font size' do From 69f471efd181d445e57ff6e7a58b1a0f5481bf1c Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 18:56:45 +0200 Subject: [PATCH 16/26] Adjust text leading and pixel conversion unit --- examples/headings.pdf | Bin 3275 -> 3255 bytes examples/misc_elements.pdf | Bin 5429 -> 5306 bytes examples/random_content.pdf | Bin 40565 -> 40129 bytes examples/styles.pdf | Bin 3257 -> 3229 bytes lib/prawn-html.rb | 2 +- lib/prawn_html/context.rb | 2 +- lib/prawn_html/document_renderer.rb | 7 ++++ spec/integrations/lists_spec.rb | 4 +-- spec/support/test_utils.rb | 4 +++ spec/units/blocks_spec.rb | 8 +++-- spec/units/headings_spec.rb | 2 +- spec/units/lists_spec.rb | 14 +++++--- spec/units/misc_spec.rb | 42 +++++++++++++++-------- spec/units/prawn_html/tags/br_spec.rb | 10 +++--- spec/units/prawn_html/tags/small_spec.rb | 2 +- spec/units/prawn_html/utils_spec.rb | 2 +- spec/units/styles_spec.rb | 26 +++++++------- 17 files changed, 81 insertions(+), 44 deletions(-) diff --git a/examples/headings.pdf b/examples/headings.pdf index cd67a5c228f4767382acd26aecb5c2d7b9f303f4..f4e2b5f65ceda67bfaf4105440cbed4de85cb429 100644 GIT binary patch delta 635 zcmZXQKTg9i6vow*iq>KyMykY+r31^$^RxYIuZVVKpa)=JEZl&Bi4CYSFd}Y%#K6YZ z32_3{6HxAe8$#u_nJj<)<@@u|f9pRiT4wsw-*@0WQ!%~}(E?Q8tul86=*TC6gR0ER+yt=Bghb%z-yNK2j$v3M zXm-XFm%`crt>@Xj^cn)uTHd;1ncIl?RoVubwVz~k zMYbvlrRwe=Eyjju|BE!t`_AKmk7p+KTeC!;7(AA5q=tFvJm<^gTE$YU_3ODn8l`}= zG3&@YyOY{`ut+IWF9}k!0k*ioOn1L>qrft`#HQXjBFcgbegm0x7l#@hSfyi~)$psU k*U#QlYukYKMsNym?-KvZatfG7}L zfg^AQxC2>sh8a>tetz-um)GIP@b0T8_us=J0BTTIo}8KSAp@2U!B@i`76k0YrBCrbz#$WfcC!C<`x@Wioxv2i^wJ#0#bs1oZGGptkt0_$^7NzC=CjcvL- zwX>$AsGVUoyMi^ZN3qV)m>&DL#SE%C>X_zG#~XQ&aw9aBtCi0p&+!#!^ZL#?-7+rB!ynX_5{wy0zd!`w9-Q0yMa+_t_*E9&*~c)ldOtp z3SXoQlbKNxZ?r`|uP0MJ#(Qp5(RMo|t5wWSt_SQ>dgp-~t0 z#K)$_L<6*5XJ{N5RtR`oU+tZl6<40>kjUviwWGyY$gsh}bC#q!6Br@q@?@>m* zDY+o&1mhDu$NXlhOYPaR`o0j19Ll2lJJp>rT$SYHr*|UhNlA2|RyYn$tDG_nHy*=> z>CLtTsWrKbrua48TcWG+C`{}?Z;tffhm%Tk+-ay5Jtt+w9yPADh0ux)DQ`8&ixPe| zmf8YFtl61$o1*D(Aml7$x4GRSj`1#XVhf*}>us4loHTH)DP>|!AIRg0nT@f|*(P+R z8G5w_!$*!1TzD{hdMJG);xBp@vE)Z6W#kB^X#{0II0rL;BYyPimod=NCfIVWFrXVL V49=+VWA0`RXasC!<<`9&_76eSRAvAG literal 5429 zcmdT|O>f&q5WVwP?8QKP=yJb53d4Zq#I4&DO=T557&*w)%3*7ZDMcq~|9tzFq$HA} zWII3)6--KA?!1|uncaEAlee!fzu{CSC;#A+aKcD&`+IVFO45rm%&Vd#r^~QhGNu`0 zRxoBbzAe7PS(3gji^XFWj!rr|OTuaqQAG!R_Sc619rJ2_U)%v`eizn6V!|}p3B><5 z<}?EJyd=FS9#<6+N%~{HSd$waWNx=D&Vf|P@J_3t7KGL z%Y75pzEwsUe@28Rr`8;{Z->P_wcZ%{G?s8uh$kZ5q#ZNVd+YQVrj-^O&V=K(IYTYe zQV~qTnagJ)GSeyHOJgtl~$OI*@hn7Z~TfsE9+AOUy z9GA!Fk=527GZqjU{itG{!<&29(C8lBPd}MLwStpUDlweC>5SC>^rS`FE(IW%1$Q z!^wRJi-S*RW&UvR;dxmde0EW+Dj2vXu{KYWbQ;#hW0|4&N6rC0kzI$1+@#38_=CAc-l$Jx>LXX}Vd&hOrsP4luTu&nU+S7+Wx? z`-0UK)Z&~U;b}NEo%ZA0Ksl7Lg+euGm>&3FtWVlXITy?^!4^PY;<{Z3Hb*79#WnY1 zdO&43cjLUl0x4DEA*8Ru$2<$u?6d2E zvbqc(((bn?ih(0CEFOFjj_}T5<#s!|VFR6K&$pEWMhX=H`$7X_Ie3HxZ&?}}qX?&E z$dh1hcX5pF;;`Ct>A_Tw!GV%QVJ!y)q#=vKPz{1ST9CJrgHqW@qL;Bkk5i$oN%p`c;1`Tb28z!d6*rXsEK{n5wzN^ z*rJphHO~ENo{WAoy0N}DJwQy1Ha&(NAi8p@7SzU(z6LPjAVd-U;oW(TZEotpAxQ3>s=c`o#%6NmhM^~lE8lcHSUT~y6= z8TjFOO%9s~oM%;DfQ4L>^w;TC5^v_{cW0l`t*t36~*0spjq+obz~bm^7kfR z{SkR;23p!J37fLpIoB1A9dLdL-L2MWc!vkpOWc^-OnHiG;#F6fhw^#+>2n!S^9ff8 z-qy7rVzic;EXj5Z{ZjpmthQr#S+zf=K`G;>GcRtP!5H@z^tL}w$bCE!-w6!o>5(zR z&>IZ!aO@7pl#(Fs{dvm4+TobDo%L4bJimuKW4Cdg{~e;Q(rHoPZDP!=B)wWK3sS#H VY)I$4tZJ{qQl6Zgy#Dzz`4@e`>!JVv diff --git a/examples/random_content.pdf b/examples/random_content.pdf index 3707a60a49679b3d6148a7240630ca1b3d6effe9..c19685fe3fe30e14c89e0c5984d8c2852775d5f9 100644 GIT binary patch delta 3016 zcmZ`*TWcLv6sAdZX#`fi8-S=AG`qupZX~&nJ zblmu*%UCYnzdtr)h_#w$i)WMhn@8dY5*@SZ6b1HdrWP{pzMuLy8hDrEK|f z_&Vah-}KfbH#{?uQszpXEiNagAMyiTJN<`)>9(lFLk6MFhrB@t^VMH}4uUk}!@S<=D43AA^e(7L6%N(z+#R zcSUH_uO&mdczW4+vYU?CpcIr)fzidv)W6d`F=&YaaH^Vn-4h2aVNCE+Vp>R|%*sh3 zl#ZXYZ;Kvd$gHeD5E;k6)K(fstU-oy@~@!`4^kR<6)diKyhk^4WsZ!4k^>l>r2(PEAfziK?S826 zWNCpRF5<$EB*XsOtplUEfuRT#hwn(H^H)otj!7gbx8z7N*HTJ=&tWmj%ByW9V4xLt zxvabkWu6i%C7FtAYV^)JLzti?Y)Mu~+E2>CCD9RK;-cQ{eW78A5&(5M}sO?*!N&ciBjl~Fg5zi zos$!sVs9C?sMg}*HvZ_kfg+Bhl$C^QPD~!P5VfDbuBb`(=eB)K^cCyA1_^=Pgdk^& zliT+-0#B*971y@EKFTo)QKL)yUm6vGz#?TpJoC5vucVS=UR8C$LWc)dQ-j|W{fyiT zdb>C9UK*wu*f`R!`9I?C_s{!Z2m8{%Fcw=|XE)NgnF0^Y0&8{p!vi4>7fOJ4HQ-TW z1R#dnq8XTdy0CZM1p7-}h>N|YlvpIr!q(?PY~HgW z;<~1;3_rZ{R5?EflY{XQr`Gznos)Y@CY%T=FxizvaA2b4R|wnrOPZQC_^&2TwDWUR zEAf@V!pW)R4nwHp;CB1Ao_GB)!js{nW_!QMtT*Fqcxnev{pX1cuoqYQ!-m09* z`&S$?wkBHRJ$y&1z}F1d%*qJI_*A-RVTjAJrIlp<1j~-Sl03b>01OiWQ#01u zxz;z>o$e{pwR?>iBo2td;;4Y>7N1~iaJl_IGvoe^nbju##L8u$N++&A`Woi{I;cW1(E-kkfLbH8)$`Mdv3 z-^06ow;l{wuH?p_lVwGyRhFl#7nAwZg3y#xBK_13J!b`B+8Uc0(*KLWV|BuSoFO{^nKY_I44>uaoRK@`z6@LW$A$2Z)EXEK+*l04P2*?HE>Gk>Ljt|p13 zQt_B?h#s8xpZ6cA86q8XT@VDa1^KoA9zw#YA6EA`-^(7+qYa38Y> zYIvh|RD8Pgp!Wllvyxa`B~tAuyuwEp>cK#5hDzsBZ5+VV6dwzg7R%ZZs&PrT_QyNx z$0(RWYm=zfZAl)s7EvuEQShjpOgYho!5Kd?Se}N9spf(h;b`Q`!l0et#3`$a_b&`q z{bz&K6Pz%k4DTR7b5VhTvW947T;$-xK|RfhN|nKr8Y3bvH-?)2>d;sQ(FnQIc|qhp zYz%4er>Rnk2&Q?&e`BcO`*)2`wIC8v5Xuxi35exgnQqdc7rN5qX z25qO1tJ{F~jqv3T3?5B5uvt&ImV|NYpqLBuSaclqenZZmn1>>F&PSpe|Mr; z5=2>sj#xPO2M358Wh^R{Bc&$#E9G(DT`R31&mjl*f~D_D%?d*4P<7csm~9Lu*oWok z4q?iK8?Lt|W~;t9M@m~=Xy4qwQ~vF-0+uV$rq``t(53uGm77P38rBkyPNUOq4Ga|j z#Qxhw1)*WcSnw+^;|Hag4e`Ei%^N~Gt5QIiJh6SvbQUBuf5>4<}fj}pi z5u*bG6h68M))F{^jiDt(%|1<{i#kv}?^&EV?}sa0`~=oPA($20@k^@p`J7)T7zvM& z40>=L9WAqv2?M=24xOl;{JyWA4g4OD+)~^SMI8bW#SccXa^;VrB`R!9Up72E>e1MU zw3vF>LLwXOTxdTmp3><{es1dJS!OW^EiEn_`CYzNd;1i|grgLbdb6Qoscwrl+j3|_ zmSd_g3|4#v3tI`L{m-=}5w=}LvEQw&C-Y}ndg`_0$*lrJJH)sa?E=hv)_0z%lYwSO zcS4GBgC$Q%7yLxdi?9q}{#t*oG41akxV9g<=$SmVj95fMNaT{F`p((LyVq|3n!+1O z(^u1^^+Byetwl0NvE0Nm2JW#e?5nX%Fw-TI8LrWJG1kRypi#_U`($w#yAjpN!GlZ7 Hr<4ByeVxnB diff --git a/examples/styles.pdf b/examples/styles.pdf index 4075c36230bd889ebe9cab42163099aa05c97730..c707de03072c80afe58d84be18ae6a2636c8c261 100644 GIT binary patch delta 415 zcmdlfIahMRYi0vWlZo$Q1kCl!EKL+bQn>WpjP(o@3@67jRtQ)an;SvIjDTX6dXt|s zwh5S8nwlXL0tM@tssv08VJb}^D!G}P1dL6LEKs!1XKoiZw$w8>w}hGiGS)1DY`w1Y^#M$jP#5R zUbaPtU>-)!~Kg9+PW0(uK_o^i0joP+jtn z&2zIO=UXN_GeZRf5Kzcd-~uxYjLa-B#elxYP-kIoF*%3FiQCB1+?Y#M)z#mP3jpVz BU)}%! delta 449 zcmZXPAx{HA5QWoTFS#73gOcnNl*w> z|4S8?gx|njxdyIXwR!vYz3;sYrh~_?AR~JEKHp$)Vy!v7h<8UhpviLZXPrrcLQKCh zRk8y6&(2*!O2~iiTAp-o5|%<5w{6_r;5JbvaqW=j;J$AWD9feEspAVE5522R3tD8_ zBrR~>dJm}r8CF+4W7g0vu*&c?C9sC2`psG_6_(+FA4iv4oC}CkTpBftj(r*IY}{a$I3Cn3g;Q`O+Fq4L@BM;+t>kUGk6FaQo>PPMG1n%ZUyuY>HHWg| TQ)GX#zQ2|uX7O-%SdQW!?@47* diff --git a/lib/prawn-html.rb b/lib/prawn-html.rb index 42433fb..978b060 100644 --- a/lib/prawn-html.rb +++ b/lib/prawn-html.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module PrawnHtml - PX = 0.66 # conversion constant for pixel sixes + PX = 0.6 # conversion constant for pixel sixes COLORS = { 'aliceblue' => 'f0f8ff', diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 86d3067..8b95cc0 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -2,7 +2,7 @@ module PrawnHtml class Context < Array - DEF_FONT_SIZE = 9.66 + DEF_FONT_SIZE = 16 * PX attr_reader :previous_tag attr_accessor :last_text_node diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index e3fed22..ef51fda 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -114,6 +114,7 @@ def output_content(buffer, block_styles) apply_callbacks(buffer) left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f options = block_styles.slice(:align, :leading, :mode, :padding_left) + options[:leading] = adjust_leading(buffer, options[:leading]) options[:indent_paragraphs] = left_indent if left_indent > 0 pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles)) end @@ -126,6 +127,12 @@ def apply_callbacks(buffer) end end + def adjust_leading(buffer, leading) + return leading if leading + + (buffer.map { |item| item[:size] || Context::DEF_FONT_SIZE }.max * 0.055).round(4) + end + def bounds(buffer, options, block_styles) return unless block_styles[:position] == :absolute diff --git a/spec/integrations/lists_spec.rb b/spec/integrations/lists_spec.rb index 7a12435..874a4b8 100644 --- a/spec/integrations/lists_spec.rb +++ b/spec/integrations/lists_spec.rb @@ -50,8 +50,8 @@ expected_array = [ [x, y.round(5)], - [x, (y - font.height).round(5)], - [x, (y - font.height * 2).round(5)] + [x, (y - font.height - TestUtils.adjust_leading).round(5)], + [x, (y - font.height * 2 - TestUtils.adjust_leading * 2).round(5)] ] expect(text_analysis.positions).to match_array(expected_array) diff --git a/spec/support/test_utils.rb b/spec/support/test_utils.rb index 2223fa6..6709f15 100644 --- a/spec/support/test_utils.rb +++ b/spec/support/test_utils.rb @@ -3,6 +3,10 @@ module TestUtils extend self + def adjust_leading(size = PrawnHtml::Context::DEF_FONT_SIZE) + (size * 0.055).round(4) + end + def default_font prawn_document.font('Helvetica', size: default_font_size) end diff --git a/spec/units/blocks_spec.rb b/spec/units/blocks_spec.rb index fa311a2..fdb81c9 100644 --- a/spec/units/blocks_spec.rb +++ b/spec/units/blocks_spec.rb @@ -15,7 +15,9 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some sample content..." }], {}, { bounding_box: nil } + [{ size: TestUtils.default_font_size, text: "Some sample content..." }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil } ) end end @@ -25,7 +27,9 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some sample content..." }], {}, { bounding_box: nil } + [{ size: TestUtils.default_font_size, text: "Some sample content..." }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil } ) end end diff --git a/spec/units/headings_spec.rb b/spec/units/headings_spec.rb index a045951..61229a3 100644 --- a/spec/units/headings_spec.rb +++ b/spec/units/headings_spec.rb @@ -2,7 +2,7 @@ RSpec.describe 'Headings' do let(:expected_buffer) { [{ size: size, styles: [:bold], text: 'Some sample content...' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading(size) } } let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } before do diff --git a/spec/units/lists_spec.rb b/spec/units/lists_spec.rb index 05210b5..feea233 100644 --- a/spec/units/lists_spec.rb +++ b/spec/units/lists_spec.rb @@ -25,15 +25,21 @@ PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_LEFT.to_s) end - it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do + it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do # rubocop:disable RSpec/ExampleLength expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• First item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } + [{ size: size, text: "• First item" }], + { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, + { bounding_box: nil } ) expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• Second item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } + [{ size: size, text: "• Second item" }], + { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, + { bounding_box: nil } ) expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• Third item" }], { indent_paragraphs: margin_left }, { bounding_box: nil } + [{ size: size, text: "• Third item" }], + { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, + { bounding_box: nil } ) end end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index bdc88aa..5f20a5e 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -18,10 +18,14 @@ it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some content" }], {}, bounding_box: nil + [{ size: TestUtils.default_font_size, text: "Some content" }], + { leading: TestUtils.adjust_leading }, + bounding_box: nil ) expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "More content" }], {}, bounding_box: nil + [{ size: TestUtils.default_font_size, text: "More content" }], + { leading: TestUtils.adjust_leading }, + bounding_box: nil ) end end @@ -29,7 +33,7 @@ context 'with an a element' do let(:html) { 'A link' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: 'A link', link: 'https://www.google.it' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -39,7 +43,7 @@ context 'with some content in an element b' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -52,7 +56,7 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - {}, + { leading: TestUtils.adjust_leading }, bounding_box: nil ) end @@ -61,7 +65,7 @@ context 'with some content in an element em' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -71,7 +75,7 @@ context 'with some content in an element i' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -83,7 +87,9 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], {}, bounding_box: nil + [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], + { leading: TestUtils.adjust_leading }, + bounding_box: nil ) end end @@ -94,7 +100,7 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - {}, + { leading: TestUtils.adjust_leading }, bounding_box: nil ) end @@ -106,7 +112,7 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - {}, + { leading: TestUtils.adjust_leading }, bounding_box: nil ) end @@ -117,7 +123,11 @@ let(:size) { PrawnHtml::Context::DEF_FONT_SIZE * 0.85 } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with([{ size: size, text: 'Some content...' }], {}, bounding_box: nil) + expect(pdf).to have_received(:puts).with( + [{ size: size, text: 'Some content...' }], + { leading: TestUtils.adjust_leading(size) }, + bounding_box: nil + ) end end @@ -126,7 +136,9 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: 'Some content...' }], {}, bounding_box: nil + [{ size: TestUtils.default_font_size, text: 'Some content...' }], + { leading: TestUtils.adjust_leading }, + bounding_box: nil ) end end @@ -134,7 +146,7 @@ context 'with some content in an element strong' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -146,7 +158,9 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], {}, bounding_box: nil + [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], + { leading: TestUtils.adjust_leading }, + bounding_box: nil ) end end diff --git a/spec/units/prawn_html/tags/br_spec.rb b/spec/units/prawn_html/tags/br_spec.rb index 36d454e..b08b6b2 100644 --- a/spec/units/prawn_html/tags/br_spec.rb +++ b/spec/units/prawn_html/tags/br_spec.rb @@ -49,19 +49,21 @@ before { append_html_to_pdf(html) } it 'sends the expected buffer elements to the pdf', :aggregate_failures do + expected_options = { leading: TestUtils.adjust_leading } + expected_text = { text: "First line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered expected_text = { text: "Second line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered expected_text = { text: "Third line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered expect(pdf).to have_received(:advance_cursor).with(described_class::BR_SPACING).ordered expected_text = { text: "Last line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], {}, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered end end end diff --git a/spec/units/prawn_html/tags/small_spec.rb b/spec/units/prawn_html/tags/small_spec.rb index 38170c4..b4f742e 100644 --- a/spec/units/prawn_html/tags/small_spec.rb +++ b/spec/units/prawn_html/tags/small_spec.rb @@ -11,7 +11,7 @@ let(:styles) { { some_attr: 'some_value' } } it 'updates the argument styles reducing the default font size' do - expect(update_styles).to eq(some_attr: 'some_value', size: 8.211) + expect(update_styles).to eq(some_attr: 'some_value', size: 8.16) end context 'with a parent font size' do diff --git a/spec/units/prawn_html/utils_spec.rb b/spec/units/prawn_html/utils_spec.rb index c56f12a..ad057b8 100644 --- a/spec/units/prawn_html/utils_spec.rb +++ b/spec/units/prawn_html/utils_spec.rb @@ -109,7 +109,7 @@ context 'with a pixel value (ex. "10.125")' do let(:value) { '10.125' } - it { is_expected.to eq 6.6825 } + it { is_expected.to eq 6.075 } end context 'with a percentage value and a container size (ex. "50%" and 100.242424)' do diff --git a/spec/units/styles_spec.rb b/spec/units/styles_spec.rb index b0cce77..0731268 100644 --- a/spec/units/styles_spec.rb +++ b/spec/units/styles_spec.rb @@ -14,7 +14,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ color: 'ffbb11', size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -25,7 +25,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ font: 'Courier', size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -37,7 +37,7 @@ let(:size) { PrawnHtml::Utils.convert_size('20px') } let(:expected_buffer) { [{ size: size, text: "Some content..." }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading(20 * PrawnHtml::PX) } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -48,7 +48,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: "Some content..." }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -59,7 +59,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: "Some content..." }] } - let(:expected_options) { {} } + let(:expected_options) { { leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -72,13 +72,13 @@ it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do expect(pdf).to have_received(:puts).with( - [{ character_spacing: 1.5, size: size, text: 'aaa' }], {}, bounding_box: nil + [{ character_spacing: 1.5, size: size, text: 'aaa' }], { leading: TestUtils.adjust_leading }, bounding_box: nil ) expect(pdf).to have_received(:puts).with( - [{ size: size, text: ' bbb ' }], {}, bounding_box: nil + [{ size: size, text: ' bbb ' }], { leading: TestUtils.adjust_leading }, bounding_box: nil ) expect(pdf).to have_received(:puts).with( - [{ character_spacing: 2.0, size: size, text: 'ccc' }], {}, bounding_box: nil + [{ character_spacing: 2.0, size: size, text: 'ccc' }], { leading: TestUtils.adjust_leading }, bounding_box: nil ) end end @@ -89,7 +89,7 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], - { leading: 12 * PrawnHtml::PX }, + { leading: (12 * PrawnHtml::PX).round(5) }, bounding_box: nil ) end @@ -99,7 +99,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { { indent_paragraphs: (40 * PrawnHtml::PX).round(4) } } + let(:expected_options) { { indent_paragraphs: (40 * PrawnHtml::PX).round(4), leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -112,7 +112,7 @@ it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], - {}, + { leading: TestUtils.adjust_leading }, bounding_box: nil ) end @@ -123,7 +123,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { { align: :left } } + let(:expected_options) { { align: :left, leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) @@ -134,7 +134,7 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { { align: :center } } + let(:expected_options) { { align: :center, leading: TestUtils.adjust_leading } } it 'sends the expected buffer elements to Prawn pdf' do expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) From 760a62f071c4f3432b0dc2491049711c37ad765b Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 3 Sep 2021 23:11:31 +0200 Subject: [PATCH 17/26] Update tag A default styles --- examples/misc_elements.pdf | Bin 5306 -> 5432 bytes examples/random_content.pdf | Bin 40129 -> 40190 bytes lib/prawn_html/tags/a.rb | 8 +++++++- spec/units/misc_spec.rb | 10 ---------- spec/units/prawn_html/tags/a_spec.rb | 20 ++++++++++++++++++-- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index 6d9ddd7c2b298f183d8381bbd6a3027c27ee04e6..78ca2f2072a9cdae67775a2761d2b110b2da74bc 100644 GIT binary patch delta 211 zcmdm`xkGD178kR*iRtECu5cz(13d!;2(~l^0)^t_JT85g)UwRv)F5{!1?ONcbQQtQ zev=RL__!MyTk4q`T9_!98(ZjE7+5Ieav540>RE!AFrgeSWc>yTK)p!j8Gvlt%*SiY su5DsstY82F3V8}#V1|K-g@HMSn5DVJw~3{R1(&L-tG^o;0M(rorT_o{ diff --git a/examples/random_content.pdf b/examples/random_content.pdf index c19685fe3fe30e14c89e0c5984d8c2852775d5f9..845997335bf07b5b443f5a563f1c17b8d68c2afb 100644 GIT binary patch delta 352 zcmX@Olj+}1rU`GDEKD}On=3<7-AM!#4JtB(bZWRK-8%q dTWo1$h+!be)su_nxN=z-8*-_ty863u0RZ{*Ob`G7 delta 224 zcmeyjlj-12rU`GDjEpzFnG3DMj?mE+EPkjc9O6ukgc%!j<7*Ty@92Hf&mC9 z)Ttm_Y-wnUVW6e4q3PuHb6mO1 N%?-FzRbBnvxB!AuGur?F diff --git a/lib/prawn_html/tags/a.rb b/lib/prawn_html/tags/a.rb index db15276..ae70fd8 100644 --- a/lib/prawn_html/tags/a.rb +++ b/lib/prawn_html/tags/a.rb @@ -6,7 +6,13 @@ class A < Tag ELEMENTS = [:a].freeze def tag_styles - "href: #{attrs.href}" if attrs.href + return unless attrs.href + + <<~STYLES + color: #00E; + href: #{attrs.href}; + text-decoration: underline; + STYLES end end end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index 5f20a5e..7664fd8 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -30,16 +30,6 @@ end end - context 'with an a element' do - let(:html) { 'A link' } - let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: 'A link', link: 'https://www.google.it' }] } - let(:expected_options) { { leading: TestUtils.adjust_leading } } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) - end - end - context 'with some content in an element b' do let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } diff --git a/spec/units/prawn_html/tags/a_spec.rb b/spec/units/prawn_html/tags/a_spec.rb index ef55245..c13fcd2 100644 --- a/spec/units/prawn_html/tags/a_spec.rb +++ b/spec/units/prawn_html/tags/a_spec.rb @@ -17,7 +17,7 @@ context 'with an href attribute' do subject(:a) do - described_class.new(:a, attributes: { 'href' => 'https://www.google.it', 'style' => 'color: #fb1' }) + described_class.new(:a, attributes: { 'href' => 'https://www.google.it', 'style' => 'font-weight: bold' }) end before do @@ -25,7 +25,23 @@ end it 'includes the link property in the styles' do - expect(a.styles).to match(color: 'ffbb11', link: 'https://www.google.it') + expect(a.styles).to match(color: '0000ee', link: 'https://www.google.it', styles: [:underline, :bold]) + end + end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'A link' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf', :aggregate_failures do + expected_buffer = [{ size: TestUtils.default_font_size, text: 'A link', link: 'https://www.google.it', color: '0000ee', styles: [:underline] }] + expected_options = { leading: TestUtils.adjust_leading } + expected_extra = { bounding_box: nil, left_indent: 0 } + + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end end From ed1bd001caf2b1d77f5a27cffffffb88d103b080 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sat, 4 Sep 2021 08:20:16 +0200 Subject: [PATCH 18/26] Fix margin left property --- .rubocop.yml | 2 +- lib/prawn_html/document_renderer.rb | 3 +- lib/prawn_html/pdf_wrapper.rb | 13 +++++++-- spec/units/blocks_spec.rb | 4 +-- spec/units/headings_spec.rb | 16 ++++++----- spec/units/lists_spec.rb | 14 ++++----- spec/units/misc_spec.rb | 33 ++++++++++++--------- spec/units/prawn_html/tags/br_spec.rb | 9 +++--- spec/units/styles_spec.rb | 41 ++++++++++++++++----------- 9 files changed, 80 insertions(+), 55 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 70d847b..2edf5b3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -29,7 +29,7 @@ Naming/MethodParameterName: RSpec/ExampleLength: # default: 5 - Max: 10 + Max: 15 RSpec/MultipleMemoizedHelpers: # default: 5 diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index ef51fda..d452115 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -115,8 +115,7 @@ def output_content(buffer, block_styles) left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f options = block_styles.slice(:align, :leading, :mode, :padding_left) options[:leading] = adjust_leading(buffer, options[:leading]) - options[:indent_paragraphs] = left_indent if left_indent > 0 - pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles)) + pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles), left_indent: left_indent) end def apply_callbacks(buffer) diff --git a/lib/prawn_html/pdf_wrapper.rb b/lib/prawn_html/pdf_wrapper.rb index f5682a5..7a816ab 100644 --- a/lib/prawn_html/pdf_wrapper.rb +++ b/lib/prawn_html/pdf_wrapper.rb @@ -106,12 +106,12 @@ def image(src, options = {}) # @param buffer [Array] array of text items # @param options [Hash] hash of options # @param bounding_box [Array] bounding box arguments, if bounded - def puts(buffer, options, bounding_box: nil) - return pdf.formatted_text(buffer, options) unless bounding_box + def puts(buffer, options, bounding_box: nil, left_indent: 0) + return output_buffer(buffer, options, left_indent: left_indent) unless bounding_box current_y = pdf.cursor pdf.bounding_box(*bounding_box) do - pdf.formatted_text(buffer, options) + output_buffer(buffer, options, left_indent: left_indent) end pdf.move_cursor_to(current_y) end @@ -130,5 +130,12 @@ def underline(x1:, x2:, y:) private attr_reader :pdf + + def output_buffer(buffer, options, left_indent:) + formatted_text = proc { pdf.formatted_text(buffer, options) } + return formatted_text.call if left_indent == 0 + + pdf.indent(left_indent, 0, &formatted_text) + end end end diff --git a/spec/units/blocks_spec.rb b/spec/units/blocks_spec.rb index fdb81c9..5517094 100644 --- a/spec/units/blocks_spec.rb +++ b/spec/units/blocks_spec.rb @@ -17,7 +17,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some sample content..." }], { leading: TestUtils.adjust_leading }, - { bounding_box: nil } + { bounding_box: nil, left_indent: 0 } ) end end @@ -29,7 +29,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some sample content..." }], { leading: TestUtils.adjust_leading }, - { bounding_box: nil } + { bounding_box: nil, left_indent: 0 } ) end end diff --git a/spec/units/headings_spec.rb b/spec/units/headings_spec.rb index 61229a3..0de3c33 100644 --- a/spec/units/headings_spec.rb +++ b/spec/units/headings_spec.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true RSpec.describe 'Headings' do + let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:expected_buffer) { [{ size: size, styles: [:bold], text: 'Some sample content...' }] } let(:expected_options) { { leading: TestUtils.adjust_leading(size) } } - let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } before do allow(pdf).to receive_messages(page_width: 540, page_height: 720) @@ -17,7 +19,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h1].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -26,7 +28,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h2].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -35,7 +37,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h3].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -44,7 +46,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h4].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -53,7 +55,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h5].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -62,7 +64,7 @@ let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h6].to_s) } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end end diff --git a/spec/units/lists_spec.rb b/spec/units/lists_spec.rb index feea233..71e01ce 100644 --- a/spec/units/lists_spec.rb +++ b/spec/units/lists_spec.rb @@ -25,21 +25,21 @@ PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_LEFT.to_s) end - it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do # rubocop:disable RSpec/ExampleLength + it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do expect(pdf).to have_received(:puts).with( [{ size: size, text: "• First item" }], - { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, - { bounding_box: nil } + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } ) expect(pdf).to have_received(:puts).with( [{ size: size, text: "• Second item" }], - { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, - { bounding_box: nil } + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } ) expect(pdf).to have_received(:puts).with( [{ size: size, text: "• Third item" }], - { indent_paragraphs: margin_left, leading: TestUtils.adjust_leading }, - { bounding_box: nil } + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } ) end end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb index 7664fd8..998b41c 100644 --- a/spec/units/misc_spec.rb +++ b/spec/units/misc_spec.rb @@ -20,23 +20,25 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content" }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "More content" }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end context 'with some content in an element b' do let(:html) { 'Some content...' } + let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -47,28 +49,32 @@ expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end context 'with some content in an element em' do let(:html) { 'Some content...' } + let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end context 'with some content in an element i' do let(:html) { 'Some content...' } + let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -79,7 +85,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -91,7 +97,7 @@ expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -103,7 +109,7 @@ expect(pdf).to have_received(:puts).with( [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -116,7 +122,7 @@ expect(pdf).to have_received(:puts).with( [{ size: size, text: 'Some content...' }], { leading: TestUtils.adjust_leading(size) }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -128,7 +134,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: 'Some content...' }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -137,9 +143,10 @@ let(:html) { 'Some content...' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -150,7 +157,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end diff --git a/spec/units/prawn_html/tags/br_spec.rb b/spec/units/prawn_html/tags/br_spec.rb index b08b6b2..0ab7fcd 100644 --- a/spec/units/prawn_html/tags/br_spec.rb +++ b/spec/units/prawn_html/tags/br_spec.rb @@ -50,20 +50,21 @@ it 'sends the expected buffer elements to the pdf', :aggregate_failures do expected_options = { leading: TestUtils.adjust_leading } + expected_extra = { bounding_box: nil, left_indent: 0 } expected_text = { text: "First line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, expected_extra).ordered expected_text = { text: "Second line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, expected_extra).ordered expected_text = { text: "Third line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, expected_extra).ordered expect(pdf).to have_received(:advance_cursor).with(described_class::BR_SPACING).ordered expected_text = { text: "Last line", size: TestUtils.default_font_size } - expect(pdf).to have_received(:puts).with([expected_text], expected_options, bounding_box: nil).ordered + expect(pdf).to have_received(:puts).with([expected_text], expected_options, expected_extra).ordered end end end diff --git a/spec/units/styles_spec.rb b/spec/units/styles_spec.rb index 0731268..a31004c 100644 --- a/spec/units/styles_spec.rb +++ b/spec/units/styles_spec.rb @@ -15,9 +15,10 @@ let(:expected_buffer) { [{ color: 'ffbb11', size: TestUtils.default_font_size, text: "Some content..." }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -26,9 +27,10 @@ let(:expected_buffer) { [{ font: 'Courier', size: TestUtils.default_font_size, text: "Some content..." }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -38,9 +40,10 @@ let(:expected_buffer) { [{ size: size, text: "Some content..." }] } let(:expected_options) { { leading: TestUtils.adjust_leading(20 * PrawnHtml::PX) } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -49,9 +52,10 @@ let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: "Some content..." }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -60,9 +64,10 @@ let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: "Some content..." }] } let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -70,15 +75,16 @@ let(:html) { '
aaa
bbb
ccc
' } let(:size) { TestUtils.default_font_size } + let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } + it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do expect(pdf).to have_received(:puts).with( - [{ character_spacing: 1.5, size: size, text: 'aaa' }], { leading: TestUtils.adjust_leading }, bounding_box: nil + [{ character_spacing: 1.5, size: size, text: 'aaa' }], expected_options, expected_extra ) + expect(pdf).to have_received(:puts).with([{ size: size, text: ' bbb ' }], expected_options, expected_extra) expect(pdf).to have_received(:puts).with( - [{ size: size, text: ' bbb ' }], { leading: TestUtils.adjust_leading }, bounding_box: nil - ) - expect(pdf).to have_received(:puts).with( - [{ character_spacing: 2.0, size: size, text: 'ccc' }], { leading: TestUtils.adjust_leading }, bounding_box: nil + [{ character_spacing: 2.0, size: size, text: 'ccc' }], expected_options, expected_extra ) end end @@ -90,7 +96,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], { leading: (12 * PrawnHtml::PX).round(5) }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -99,10 +105,11 @@ let(:html) { '
Some content...
' } let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } - let(:expected_options) { { indent_paragraphs: (40 * PrawnHtml::PX).round(4), leading: TestUtils.adjust_leading } } + let(:expected_options) { { leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 40 * PrawnHtml::PX } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -113,7 +120,7 @@ expect(pdf).to have_received(:puts).with( [{ size: TestUtils.default_font_size, text: "Some content..." }], { leading: TestUtils.adjust_leading }, - bounding_box: nil + { bounding_box: nil, left_indent: 0 } ) end end @@ -124,9 +131,10 @@ let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } let(:expected_options) { { align: :left, leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end @@ -135,9 +143,10 @@ let(:expected_buffer) { [{ size: TestUtils.default_font_size, text: "Some content..." }] } let(:expected_options) { { align: :center, leading: TestUtils.adjust_leading } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, bounding_box: nil) + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) end end end From b7e43cacbb42cd306d4857db1b65e1056f6a1786 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sat, 4 Sep 2021 08:21:58 +0200 Subject: [PATCH 19/26] Update Blockquote tag --- examples/misc_elements.pdf | Bin 5432 -> 5432 bytes lib/prawn_html/tags/blockquote.rb | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index 78ca2f2072a9cdae67775a2761d2b110b2da74bc..d3a9b9a1306167a17be4816295d3d19bfbf6fea2 100644 GIT binary patch delta 209 zcmdm?wL@!zHVdzrfu4banT3U(iJ`gClC0KA>((bJNKa*$gLtU=e3Cu>@)}0h^-0YQk;|G}Y1w#+RMk%_`1j zWUgmqYB0HgS!A*TtL)^Ptm5p3W_kt&mJrK9V(M(-Oa>;C9oT#sjVJf9`LG!QU0{MR VMu1&{6==JO5!4j<%?a!`cmc#OFi8La delta 209 zcmdm?wL@!zHVdz*p`L+)nWdqgiJ`gC Date: Sat, 4 Sep 2021 09:38:47 +0200 Subject: [PATCH 20/26] Update body tag --- lib/prawn_html/tags/body.rb | 4 ++++ spec/units/prawn_html/tags/body_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/prawn_html/tags/body.rb b/lib/prawn_html/tags/body.rb index bca2914..581f0e5 100644 --- a/lib/prawn_html/tags/body.rb +++ b/lib/prawn_html/tags/body.rb @@ -4,6 +4,10 @@ module PrawnHtml module Tags class Body < Tag ELEMENTS = [:body].freeze + + def block? + true + end end end end diff --git a/spec/units/prawn_html/tags/body_spec.rb b/spec/units/prawn_html/tags/body_spec.rb index b5b1a4e..f7aabe1 100644 --- a/spec/units/prawn_html/tags/body_spec.rb +++ b/spec/units/prawn_html/tags/body_spec.rb @@ -1,5 +1,13 @@ # frozen_string_literal: true RSpec.describe PrawnHtml::Tags::Body do + subject(:body) { described_class.new(:body) } + it { expect(described_class).to be < PrawnHtml::Tag } + + describe '#block?' do + subject(:block?) { body.block? } + + it { is_expected.to be_truthy } + end end From e68b1f9c0d2e3f3202b9734ac3b772a2d58d9a48 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sat, 4 Sep 2021 10:54:50 +0200 Subject: [PATCH 21/26] Update H tags --- examples/headings.pdf | Bin 3255 -> 3259 bytes examples/misc_elements.pdf | Bin 5432 -> 5433 bytes examples/random_content.pdf | Bin 40190 -> 40190 bytes lib/prawn_html/tags/h.rb | 30 ++++++------ spec/units/headings_spec.rb | 70 --------------------------- spec/units/prawn_html/tags/h_spec.rb | 70 +++++++++++++++++++++++++-- 6 files changed, 82 insertions(+), 88 deletions(-) delete mode 100644 spec/units/headings_spec.rb diff --git a/examples/headings.pdf b/examples/headings.pdf index f4e2b5f65ceda67bfaf4105440cbed4de85cb429..5f117643ceeca23c43a73c5e882ef9251b14baba 100644 GIT binary patch delta 536 zcmZWlJ5B>J6jTz(ZY(OqDk6Ne1t}=<_W3>g*%loFS_+W302SgRZUF^fcTjNvkPrn( zR2+aKa0A2{a3H`+xUy!(GLD%U30g+9O(?Id2frSBYZSa3g07G%BJ;=pr@WL3YY^f_IfCy2|LcsWVkT1lu zW~Mc;$HeZbRN9BDTJEqsLdcB`k+IJx2@ZP zfGkQ~_T8;&dl1V$iRR+V*im7@qII=8DcP_nWJVYg6A!cbct3tw1aKTY{VQH&0>sWz zN_d@z^aw3e#B3>d2&g$RcdeOtn!Oedh{)G|8*KtPQ5wXcPtw`=lE3w|0SqAx^Scm_ lpogc2KN#TCvG*&|1E6pAAwO9qz6I!tO;YsM+?M1OdS5a-pr7JUvW+*NN|2*^g3 zewFh;c`ZVtanZC9UY9Of5tNDDzjTo5f+~h^;lA1p56dBzgi1!xMSb(Ud0hf2tz87X zt~&g|G%xZAi=FMbr zMKPWUV1P4$jveFPIot}Cs&F@0sKWQb*(%%(W?Mp7rlc^L)kPcL565w9=+wFc=d$fY zXkcDwNqNHx>`rnuQHJ(eaLfu~u$^e_nRI<5uSu3S59$<+z z*xhsjKP8uA%S@?Y8!wEUqt!-9z(*rIv49j8B_N(^tO=CJf@I*f19 ziO4Z!0bitD{FP47Q-mTzjyE85qeDK*Rbl)*A-sp8t$_lGv)`N{5P yNfB@r$^9UL6*5jA<$}yeH&0SnyN&Wx^YwoZB delta 1040 zcmZuvO-oc!7-r_)x#rRNFee>^@T$nvq{s8|o-aZ%A!8O1s4HUb-kE+e9PC6le}%a)R48V% z`j=88LD`uTm^>cF#aIA;;&a`B7s_m2e4adCn&X*!oRPyFW_1gz2^>`|4^CoooX|| z7asfwJ;&AMJUy>M9)`-zS~CG^H9GUKwh@O<3mqqCwNA0ol`O?q+dCUH)f^ey@CWeXV^eg`1#|5DgGbDli6&1ydBr=YpIF8BxLNuXT!JCz5T}~+Hu-WLq z-O9VU^+NSb3IDM=Dp}Rq0j?;CV;-&hj4MoJOcdwP8~!uC|LEzmV^(0JHpvrxQZua- z&sZz?tQNwbHQwb&)FxIcAso~46o8{n+@77t*@V`~YK2{onUG)MlETqTOcB4zZCryPlwV5=Fz#rhJwKG$~y;R-fe8SwpF z-@_)+Br9xVYW1FLJFVeGCkr}N?>h;>zE~m`>#Aa4z0=3j82+etJsgkZ*;_`9gKO9# zRU#>q3@A7%h7FJgMMRk`s$GT`k~H_N#)_vYQOstG;3>ZjZNKZ{RFI8r@gx2r&k!o4 z4G7@2-+~YQWe+PP!X~rO@|wYs7&+^cM75?!!66-jq!J6m-dqR|HqYP#6NVW&@Nx4H z4iy!bR{Xhn78{fpeEpFYpE^R=ZXKOk0c_OzZI0vM3>78f;(iE&d7LT{R9VGw%}Gop z2INpdXyzzx2CzFn>EXkSD9K6a6oEVQ$F0gmrj5zIx29GPuJ3!zj#MZO!-XqTM*w#g z&KHgo?W-ln-|b5!>&gS4maHci&zG#97FUN>8z$2@iaRm5q$0=QgV8}a(xINDMC(ja zmpX$U%9N!WM@b@L9SM;l7=8fmcA9p;b{RUsZZNjehkq6358bbeX?up>>wED0@|G}! z3z3q^zd4o^&{TA&LR<@jpE-UKxM5q(9;}WZe#qEQjp zgc&7R-d|`phBvN_SDZd>kmajps=&2NxP)IPmwR}Ov6LN7ssL&KMPqJkufF}xzcJK? Apa1{> delta 1677 zcmZ8hO=w(I6lQ+jOXf{x@-y#ECe6H=mrhiWaC86Py+W$dpri{$S}7Jcq9OrHn}sOE zjjORxA@M*JDg_r(U5Gzjlx(`tg$q~N73rdtu3QL$xDi^<`^y_$Jh>D^McTb8KU7tMGdtjv9TtKe7d*xlq1qrvfxY9M@w{ixPTZ)HW2ZJMI1Xy2 zIBw4Q@LTPvQ;cYudR592jy`#gBt9u-R(xlkdokq@8o`)ib~4s*-|ax?4B&Eo#nI@h zMDEvD9YuuB5)GY=x+IK$(g@Q351io|>LCo~lQV0b0Yx$?k(LajVmu|XsF>p}V+vH* zjLEvxLG5D;m4*-24d64kzh($yf={@nIr1Oh%1R;>tat(Z(@i(#)Z!WI!G3-DIsq>B|~lBVQ)jTSs= z3>++zH7WV6MjN)g(8VgLID2#3Yr)tX)D@8ozMl=?wl`dh2@`apERy9&5lLDXNd*yc zN;PPn!>knwbr0qLxcMY1sVzm)0FIjHQAJI$Q`ihYzVP9LnT-i+r`ChtXPOg8yQP>S zmd6@3;hK{e)e&(H6`_gl?7h~a+aXGQqkQO$AOZAv=0lUjx z_&NC7O$;Rbsrz+-FTv%-8wIX=*Gv4*-mf@0LZY}wONZq#fG<2OmmBl#SD<@)lw^j~R5_d$`~g#VyQmD2+rplJ*}BpMG2uMzv+p8yCFw zZ5C}^^jSome sample content...' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h1].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element h2' do - let(:html) { '

Some sample content...

' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h2].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element h3' do - let(:html) { '

Some sample content...

' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h3].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element h4' do - let(:html) { '

Some sample content...

' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h4].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element h5' do - let(:html) { '
Some sample content...
' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h5].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element h6' do - let(:html) { '
Some sample content...
' } - let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h6].to_s) } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end -end diff --git a/spec/units/prawn_html/tags/h_spec.rb b/spec/units/prawn_html/tags/h_spec.rb index 124c2cc..0dcfb42 100644 --- a/spec/units/prawn_html/tags/h_spec.rb +++ b/spec/units/prawn_html/tags/h_spec.rb @@ -5,6 +5,12 @@ it { expect(described_class).to be < PrawnHtml::Tag } + describe '#block?' do + subject(:block?) { h1.block? } + + it { is_expected.to be_truthy } + end + context 'without attributes' do before do h1.process_styles @@ -22,9 +28,67 @@ end end - describe '#block?' do - subject(:block?) { h1.block? } + describe 'tag rendering' do + include_context 'with pdf wrapper' - it { is_expected.to be_truthy } + let(:expected_buffer) { [{ size: size, styles: [:bold], text: 'Some sample content' }] } + let(:expected_options) { { leading: TestUtils.adjust_leading(size) } } + let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } + + before { append_html_to_pdf(html) } + + context 'with some content in an element h1' do + let(:html) { '

Some sample content

' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h1].to_s) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end + + context 'with some content in an element h2' do + let(:html) { '

Some sample content

' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h2].to_s) } + + it 'sends the expected buffer elements to Prawn pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end + + context 'with some content in an element h3' do + let(:html) { '

Some sample content

' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h3].to_s) } + + it 'sends the expected buffer elements to Prawn pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end + + context 'with some content in an element h4' do + let(:html) { '

Some sample content

' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h4].to_s) } + + it 'sends the expected buffer elements to Prawn pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end + + context 'with some content in an element h5' do + let(:html) { '
Some sample content
' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h5].to_s) } + + it 'sends the expected buffer elements to Prawn pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end + + context 'with some content in an element h6' do + let(:html) { '
Some sample content
' } + let(:size) { PrawnHtml::Utils.convert_size(PrawnHtml::Tags::H::SIZES[:h6].to_s) } + + it 'sends the expected buffer elements to Prawn pdf' do + expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) + end + end end end From 1e97b8d5c60bb01c7ecf2e668e3e49fcc91b9b0e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 5 Sep 2021 14:25:46 +0200 Subject: [PATCH 22/26] Add on_context_remove callback to Context --- lib/prawn_html/context.rb | 1 + spec/units/prawn_html/context_spec.rb | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index 8b95cc0..0435ade 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -62,6 +62,7 @@ def merged_styles # Remove the last element from the context def remove_last + last.on_context_remove(self) if last.respond_to?(:on_context_remove) @merged_styles = nil @last_text_node = false @previous_tag = last.tag diff --git a/spec/units/prawn_html/context_spec.rb b/spec/units/prawn_html/context_spec.rb index bad891c..7c954e3 100644 --- a/spec/units/prawn_html/context_spec.rb +++ b/spec/units/prawn_html/context_spec.rb @@ -59,10 +59,9 @@ def before_content end end end - let(:tag) { some_tag_class.new(:some_tag) } before do - context << tag + context << some_tag_class.new(:some_tag) end it { is_expected.to eq 'Some before content' } @@ -95,16 +94,26 @@ def before_content describe '#remove_last' do subject(:remove_last) { context.remove_last } + let(:some_tag_class) do + Class.new(PrawnHtml::Tag) do + def on_context_remove(context) + # callback after removal + end + end + end + let(:tag) { instance_double(some_tag_class, on_context_remove: true, :parent= => true, tag: :some_tag) } + before do - context.add(instance_double(PrawnHtml::Tag, :parent= => true, tag: :some_tag)) + context.add(tag) end - it 'removes the last element from the context' do + it 'removes the last element from the context', :aggregate_failures do expect { remove_last }.to( change(context, :size).from(1).to(0).and( change(context, :previous_tag).from(nil).to(:some_tag) ) ) + expect(tag).to have_received(:on_context_remove) end end From 8830c49696fbbef49e52aa147d987ba8fe539517 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 5 Sep 2021 14:26:16 +0200 Subject: [PATCH 23/26] Improve Ul, Ol and Li indentation --- examples/misc_elements.pdf | Bin 5433 -> 5436 bytes examples/random_content.pdf | Bin 40190 -> 40191 bytes lib/prawn_html/document_renderer.rb | 2 +- lib/prawn_html/tags/li.rb | 19 +++++++++-- lib/prawn_html/tags/ol.rb | 21 ++++++++++-- lib/prawn_html/tags/ul.rb | 25 ++++++++++++-- spec/integrations/lists_spec.rb | 6 ++-- spec/units/lists_spec.rb | 46 -------------------------- spec/units/prawn_html/tags/ul_spec.rb | 44 ++++++++++++++++++++++-- 9 files changed, 104 insertions(+), 59 deletions(-) delete mode 100644 spec/units/lists_spec.rb diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index 2ca3a50d72c8c7d3a4552a9ee503287cbcfb4e66..ee3215e673ee64377eb8e4d37b60416c7d10b8ec 100644 GIT binary patch delta 355 zcmZwBF;2r!3jH0BcnSX+6ii!}xhNDiR8T7m7Paah6p@V_h;J z5T)EhR$Uy74NNU0eaosLA$L(~Kw0gVIbOTV;>f7rH-bKxJ8ct5_HYtX+NLI_RIjHqX=aeN=I M&n=%!R-1MH0}e4yOaK4? delta 372 zcmZwBF-`+P3bsOq@(5lG$0>>1EfOYB2>Tu zfQAFG>oh2ftI^Z{_Pi{f7Y{#^Wb1ggsU}Z*GXiW>8B$619>~7;PbxP_KtgIC0qt^H z&s2vb6BO#LwfpJu(bf`zYG&8hUbFY90JoaGe-Y|xtC)eZ_@&!WM@K0N#U8^l`aa15 zoxO#n|3f+kRvfnD=H}qDx}&baVVZRl{ef-EWn_uRIsV6ldDK--yT<*vo&Pz^8S^Mu Rx?*3at3!$N`PKC*{sLr+RJZ^D diff --git a/examples/random_content.pdf b/examples/random_content.pdf index d88b238d51a48dd8fd5aefe043ed74dae455af62..ec57a0dfbc77cd8dbd83e07868c20d06d02dc13c 100644 GIT binary patch delta 1393 zcmZvb%WG6w5Qm9rJKd)rcBfx4c6Y8u1sSZI_d}!+@fksp3Bm}j24)!0Q9*E_D5Cg6 zB~l>xSSY$sTtvyrjfnq&J3$v_;YLv0i^g;B?c3cL@2cvnx^;ea>eP=H%NAcOoBceb zo_|s4oCF#uqd+>2AJSEv9hq3gI){RpX|jEW5VCcKs3=EE;ELi=Q!7JeHkcO#O4(MIA^qse(YbS*CnpC7o8*tX}KqSofgKx@{ z*s0JommqaKluMPd1{c6tcnQ7(CyWk6M3ANGI9n>=`wB@j|6*-?^C}@9?(S(jyTO-DS&?27S6fv0%b5!&er#0(KdyW@;5{1O*}?uAtD*w zHDn>eukFT&w!n=aK=^4k*bP3;sSrrgpPn+flMV+SF(nd_&Iqmhk3qW&Z92 zP1S|phES90Oof%Hnt=qnD2l)wLvrdKp delta 1441 zcmZvcONf(K6vwH?OmdUVBqm=HXOiZ&zKY=Wz8^(Yd`xRgDbisUEd@a+UHCv;xRAaU zg{ojj&O$9h-4*MmgDYK>w!1D0U6ta(YWIpDt_rp1eqX*X!D&|a{?7k9_kYhhH_z{^ z`}fYel}9h>)yJk>6O2>iHPb=DgK`H}W<~?C=R>;m7CE#;2-&|xOsa8;Ny9xnEVog_ z8rH@ytz?|zrx_i>KQpay#Bk=Uvqj`^rB(N7CKZ^GlfacqtC9tzhtkT*Xys~F! zu*?}xPY`1Qlb-4^I8NT1_|jgX1Az{z6fRWf8mU|D1b!RvkZK3+Ru@R>R$I;t{8bH^ zMXfQdvwAFQz#{3^GokW~D-92-34B5}k8?`&6I48@gR~C6k=8EZ85TuPnaoM3vKx>r1O6|3>GmKjg%~2ozs4bVAWC3_x=REf1)B5gGl);a6Kh2vB z&7EV48;K?)_QP>aRgQ8OlR%cE2NxT!Y{JVruk6IV8WZ?>kifmhjv0gfRmP@H>FlXu zANzfC_Ty5FLo#=*2>e{{z&jfc0BsWJHn(Mhea#y$2=>D3t%C)N*{-&}DfU^CGsQl} z?`_<&5NFDp)US0ccqgeEGjzNi_O*Fgb7pNekmuX|9uAupu{5X)%2}#X0;1G~qjOET z-zH@l$IIjRCs0o+Q0pW!*hQ|fB@eOmksauCDvDd?vCaiNhr(c(u_|lq_nkM(PBS5M z$*>epg`LS4B|O8!>0S?hO`c_&U%Iz)`ct+r{Tn%%ho}8NH%JjD;rC5@?b@$4-!4 zhXp-&iX2H_cKT)nUS4>k1YBO`hA$l~E?&JG|zc*h>@WmR3*^4%7$K>rV zw_G^;nV>xW=t)Y_1@}hVHOy+ghB>X)F#kU+X!oKn@Lc!m 1 + + @first_level = true + end + def tag_styles - "margin-left: #{MARGIN_LEFT}px" + if @first_level + <<~STYLES + margin-top: #{MARGIN_TOP}px; + margin-left: #{MARGIN_LEFT}px; + margin-bottom: #{MARGIN_BOTTOM}px; + STYLES + else + "margin-left: #{MARGIN_LEFT}px" + end end end end diff --git a/lib/prawn_html/tags/ul.rb b/lib/prawn_html/tags/ul.rb index c85c53a..c964fd8 100644 --- a/lib/prawn_html/tags/ul.rb +++ b/lib/prawn_html/tags/ul.rb @@ -5,14 +5,35 @@ module Tags class Ul < Tag ELEMENTS = [:ul].freeze - MARGIN_LEFT = 25 + MARGIN_TOP = 15 + MARGIN_LEFT = 40 + MARGIN_BOTTOM = 15 + + def initialize(tag, attributes: {}, options: {}) + super + @first_level = false + end def block? true end + def on_context_add(context) + return if context.map(&:tag).count { |el| el == :ul } > 1 + + @first_level = true + end + def tag_styles - "margin-left: #{MARGIN_LEFT}px" + if @first_level + <<~STYLES + margin-top: #{MARGIN_TOP}px; + margin-left: #{MARGIN_LEFT}px; + margin-bottom: #{MARGIN_BOTTOM}px; + STYLES + else + "margin-left: #{MARGIN_LEFT}px" + end end end end diff --git a/spec/integrations/lists_spec.rb b/spec/integrations/lists_spec.rb index 874a4b8..dfda8a3 100644 --- a/spec/integrations/lists_spec.rb +++ b/spec/integrations/lists_spec.rb @@ -35,7 +35,7 @@ HTML end - it 'renders the list of elements', :aggregate_failures do # rubocop:disable RSpec/ExampleLength + it 'renders the list of elements', :aggregate_failures do text_analysis = PDF::Inspector::Text.analyze(pdf.render) expected_array = [{ name: TestUtils.default_font_family, size: TestUtils.default_font_size }] * 3 @@ -45,8 +45,8 @@ font = TestUtils.default_font margin_left = PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_LEFT.to_s) - x = pdf.page.margins[:left] + margin_left - y = pdf.y - font.ascender + x = pdf.page.margins[:left] + margin_left + PrawnHtml::Tags::Li::INDENT_UL + y = pdf.y - font.ascender - PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_TOP.to_s) expected_array = [ [x, y.round(5)], diff --git a/spec/units/lists_spec.rb b/spec/units/lists_spec.rb deleted file mode 100644 index 71e01ce..0000000 --- a/spec/units/lists_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe 'Lists' do - let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } - - before do - allow(pdf).to receive_messages(page_width: 540, page_height: 720) - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) - pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) - PrawnHtml.append_html(pdf_document, html) - end - - context 'with an ul list' do - let(:html) do - <<~HTML -
    -
  • First item
  • -
  • Second item
  • -
  • Third item
  • -
- HTML - end - let(:size) { TestUtils.default_font_size } - let(:margin_left) do - PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_LEFT.to_s) - end - - it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• First item" }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: margin_left } - ) - expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• Second item" }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: margin_left } - ) - expect(pdf).to have_received(:puts).with( - [{ size: size, text: "• Third item" }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: margin_left } - ) - end - end -end diff --git a/spec/units/prawn_html/tags/ul_spec.rb b/spec/units/prawn_html/tags/ul_spec.rb index 53c2730..61a8387 100644 --- a/spec/units/prawn_html/tags/ul_spec.rb +++ b/spec/units/prawn_html/tags/ul_spec.rb @@ -5,6 +5,12 @@ it { expect(described_class).to be < PrawnHtml::Tag } + describe '#block?' do + subject(:block?) { ul.block? } + + it { is_expected.to be_truthy } + end + context 'without attributes' do before do ul.process_styles @@ -19,9 +25,41 @@ end end - describe '#block?' do - subject(:block?) { ul.block? } + describe 'tag rendering' do + include_context 'with pdf wrapper' - it { is_expected.to be_truthy } + let(:html) do + <<~HTML +
    +
  • First item
  • +
  • Second item
  • +
  • Third item
  • +
+ HTML + end + let(:size) { TestUtils.default_font_size } + let(:margin_left) do + PrawnHtml::Utils.convert_size(PrawnHtml::Tags::Ul::MARGIN_LEFT.to_s) + end + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf', :aggregate_failures do + expect(pdf).to have_received(:puts).with( + [{ size: size, text: "• First item" }], + { indent_paragraphs: PrawnHtml::Tags::Li::INDENT_UL, leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } + ) + expect(pdf).to have_received(:puts).with( + [{ size: size, text: "• Second item" }], + { indent_paragraphs: PrawnHtml::Tags::Li::INDENT_UL, leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } + ) + expect(pdf).to have_received(:puts).with( + [{ size: size, text: "• Third item" }], + { indent_paragraphs: PrawnHtml::Tags::Li::INDENT_UL, leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: margin_left } + ) + end end end From 01c4bda4a5048907af5a3d2c2f1d77ff81deba2f Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 5 Sep 2021 14:48:36 +0200 Subject: [PATCH 24/26] Minor spec improvements --- spec/units/blocks_spec.rb | 36 ----- spec/units/misc_spec.rb | 164 ----------------------- spec/units/prawn_html/tags/b_spec.rb | 30 +++++ spec/units/prawn_html/tags/del_spec.rb | 30 +++++ spec/units/prawn_html/tags/div_spec.rb | 16 +++ spec/units/prawn_html/tags/hr_spec.rb | 51 ++++--- spec/units/prawn_html/tags/i_spec.rb | 30 +++++ spec/units/prawn_html/tags/mark_spec.rb | 16 +++ spec/units/prawn_html/tags/p_spec.rb | 22 ++- spec/units/prawn_html/tags/small_spec.rb | 17 +++ spec/units/prawn_html/tags/span_spec.rb | 16 +++ spec/units/prawn_html/tags/u_spec.rb | 30 +++++ 12 files changed, 240 insertions(+), 218 deletions(-) delete mode 100644 spec/units/blocks_spec.rb delete mode 100644 spec/units/misc_spec.rb diff --git a/spec/units/blocks_spec.rb b/spec/units/blocks_spec.rb deleted file mode 100644 index 5517094..0000000 --- a/spec/units/blocks_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe 'Blocks' do - let(:pdf) { instance_double(PrawnHtml::PdfWrapper, advance_cursor: true, puts: true) } - - before do - allow(pdf).to receive_messages(page_width: 540, page_height: 720) - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) - pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) - PrawnHtml.append_html(pdf_document, html) - end - - context 'with some content in an element div' do - let(:html) { '
Some sample content...
' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some sample content..." }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element p' do - let(:html) { '

Some sample content...

' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some sample content..." }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end -end diff --git a/spec/units/misc_spec.rb b/spec/units/misc_spec.rb deleted file mode 100644 index 998b41c..0000000 --- a/spec/units/misc_spec.rb +++ /dev/null @@ -1,164 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe 'Misc' do - let(:pdf) do - methods = { advance_cursor: true, puts: true, horizontal_rule: true } - instance_double(PrawnHtml::PdfWrapper, methods) - end - - before do - allow(pdf).to receive_messages(page_width: 540, page_height: 720) - allow(PrawnHtml::PdfWrapper).to receive(:new).and_return(pdf) - pdf_document = Prawn::Document.new(page_size: 'A4', page_layout: :portrait) - PrawnHtml.append_html(pdf_document, html) - end - - context 'with an hr element' do - let(:html) { 'Some content
More content' } - - it 'sends the expected buffer elements to Prawn pdf', :aggregate_failures do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "Some content" }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: "More content" }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element b' do - let(:html) { 'Some content...' } - - let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } - let(:expected_options) { { leading: TestUtils.adjust_leading } } - let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element del' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element em' do - let(:html) { 'Some content...' } - - let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } - let(:expected_options) { { leading: TestUtils.adjust_leading } } - let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element i' do - let(:html) { 'Some content...' } - - let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:italic], text: 'Some content...' }] } - let(:expected_options) { { leading: TestUtils.adjust_leading } } - let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element ins' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element mark' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element s' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [hash_including(callback: anything, size: TestUtils.default_font_size, text: 'Some content...')], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element small' do - let(:html) { 'Some content...' } - let(:size) { PrawnHtml::Context::DEF_FONT_SIZE * 0.85 } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: size, text: 'Some content...' }], - { leading: TestUtils.adjust_leading(size) }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element span' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, text: 'Some content...' }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end - - context 'with some content in an element strong' do - let(:html) { 'Some content...' } - let(:expected_buffer) { [{ size: TestUtils.default_font_size, styles: [:bold], text: 'Some content...' }] } - let(:expected_options) { { leading: TestUtils.adjust_leading } } - let(:expected_extra) { { bounding_box: nil, left_indent: 0 } } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with(expected_buffer, expected_options, expected_extra) - end - end - - context 'with some content in an element u' do - let(:html) { 'Some content...' } - - it 'sends the expected buffer elements to Prawn pdf' do - expect(pdf).to have_received(:puts).with( - [{ size: TestUtils.default_font_size, styles: [:underline], text: 'Some content...' }], - { leading: TestUtils.adjust_leading }, - { bounding_box: nil, left_indent: 0 } - ) - end - end -end diff --git a/spec/units/prawn_html/tags/b_spec.rb b/spec/units/prawn_html/tags/b_spec.rb index f441f91..82fdef1 100644 --- a/spec/units/prawn_html/tags/b_spec.rb +++ b/spec/units/prawn_html/tags/b_spec.rb @@ -14,4 +14,34 @@ expect(b.styles).to match(color: 'ffbb11', styles: [:bold]) end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + before { append_html_to_pdf(html) } + + context 'with a B tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:bold], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + + context 'with a Strong tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:bold], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + end end diff --git a/spec/units/prawn_html/tags/del_spec.rb b/spec/units/prawn_html/tags/del_spec.rb index 134d2e7..825870f 100644 --- a/spec/units/prawn_html/tags/del_spec.rb +++ b/spec/units/prawn_html/tags/del_spec.rb @@ -16,4 +16,34 @@ expect(styles).to match(color: 'ffbb11', callback: ['StrikeThrough', nil]) end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + before { append_html_to_pdf(html) } + + context 'with a Del tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ callback: anything, size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + + context 'with a S tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ callback: anything, size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + end end diff --git a/spec/units/prawn_html/tags/div_spec.rb b/spec/units/prawn_html/tags/div_spec.rb index bed068e..71769a5 100644 --- a/spec/units/prawn_html/tags/div_spec.rb +++ b/spec/units/prawn_html/tags/div_spec.rb @@ -10,4 +10,20 @@ it { is_expected.to be_truthy } end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { '
Some sample content
' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end end diff --git a/spec/units/prawn_html/tags/hr_spec.rb b/spec/units/prawn_html/tags/hr_spec.rb index 6d2712b..6d1d360 100644 --- a/spec/units/prawn_html/tags/hr_spec.rb +++ b/spec/units/prawn_html/tags/hr_spec.rb @@ -7,21 +7,6 @@ it { expect(described_class).to be < PrawnHtml::Tag } - context 'without attributes' do - before do - hr.process_styles - end - - it 'returns the expected styles for hr tag' do - expected_styles = { - color: 'ffbb11', - margin_bottom: PrawnHtml::Utils.convert_size(described_class::MARGIN_BOTTOM.to_s), - margin_top: PrawnHtml::Utils.convert_size(described_class::MARGIN_TOP.to_s) - } - expect(hr.styles).to match(expected_styles) - end - end - describe '#block?' do subject(:block?) { hr.block? } @@ -69,4 +54,40 @@ end end end + + context 'without attributes' do + before do + hr.process_styles + end + + it 'returns the expected styles for hr tag' do + expected_styles = { + color: 'ffbb11', + margin_bottom: PrawnHtml::Utils.convert_size(described_class::MARGIN_BOTTOM.to_s), + margin_top: PrawnHtml::Utils.convert_size(described_class::MARGIN_TOP.to_s) + } + expect(hr.styles).to match(expected_styles) + end + end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'Some content
More content' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf', :aggregate_failures do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, text: "Some content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, text: "More content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end end diff --git a/spec/units/prawn_html/tags/i_spec.rb b/spec/units/prawn_html/tags/i_spec.rb index f9a594d..c045809 100644 --- a/spec/units/prawn_html/tags/i_spec.rb +++ b/spec/units/prawn_html/tags/i_spec.rb @@ -14,4 +14,34 @@ expect(i.styles).to match(color: 'ffbb11', styles: [:italic]) end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + before { append_html_to_pdf(html) } + + context 'with an I tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:italic], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + + context 'with an Em tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:italic], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + end end diff --git a/spec/units/prawn_html/tags/mark_spec.rb b/spec/units/prawn_html/tags/mark_spec.rb index 078ab06..e686c01 100644 --- a/spec/units/prawn_html/tags/mark_spec.rb +++ b/spec/units/prawn_html/tags/mark_spec.rb @@ -16,4 +16,20 @@ expect(styles).to match(color: 'ffbb11', callback: ['Background', 'ffff00']) end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'Some sample content' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ callback: anything, size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end end diff --git a/spec/units/prawn_html/tags/p_spec.rb b/spec/units/prawn_html/tags/p_spec.rb index ea5b84d..e5aeb6c 100644 --- a/spec/units/prawn_html/tags/p_spec.rb +++ b/spec/units/prawn_html/tags/p_spec.rb @@ -5,6 +5,12 @@ it { expect(described_class).to be < PrawnHtml::Tag } + describe '#block?' do + subject(:block?) { p.block? } + + it { is_expected.to be_truthy } + end + context 'without attributes' do before do p.process_styles @@ -20,9 +26,19 @@ end end - describe '#block?' do - subject(:block?) { p.block? } + describe 'tag rendering' do + include_context 'with pdf wrapper' - it { is_expected.to be_truthy } + let(:html) { '

Some sample content

' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end end end diff --git a/spec/units/prawn_html/tags/small_spec.rb b/spec/units/prawn_html/tags/small_spec.rb index b4f742e..7e06ba2 100644 --- a/spec/units/prawn_html/tags/small_spec.rb +++ b/spec/units/prawn_html/tags/small_spec.rb @@ -22,4 +22,21 @@ end end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'Some sample content' } + let(:size) { PrawnHtml::Context::DEF_FONT_SIZE * 0.85 } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading(size) }, + { bounding_box: nil, left_indent: 0 } + ) + end + end end diff --git a/spec/units/prawn_html/tags/span_spec.rb b/spec/units/prawn_html/tags/span_spec.rb index f09fd4e..8fbd1e2 100644 --- a/spec/units/prawn_html/tags/span_spec.rb +++ b/spec/units/prawn_html/tags/span_spec.rb @@ -2,4 +2,20 @@ RSpec.describe PrawnHtml::Tags::Span do it { expect(described_class).to be < PrawnHtml::Tag } + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + let(:html) { 'Some sample content' } + + before { append_html_to_pdf(html) } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end end diff --git a/spec/units/prawn_html/tags/u_spec.rb b/spec/units/prawn_html/tags/u_spec.rb index 0e138f2..3d05ea1 100644 --- a/spec/units/prawn_html/tags/u_spec.rb +++ b/spec/units/prawn_html/tags/u_spec.rb @@ -14,4 +14,34 @@ expect(u.styles).to match(color: 'ffbb11', styles: [:underline]) end end + + describe 'tag rendering' do + include_context 'with pdf wrapper' + + before { append_html_to_pdf(html) } + + context 'with a U tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:underline], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + + context 'with a Ins tag' do + let(:html) { 'Some sample content' } + + it 'sends the expected buffer elements to the pdf' do + expect(pdf).to have_received(:puts).with( + [{ size: TestUtils.default_font_size, styles: [:underline], text: "Some sample content" }], + { leading: TestUtils.adjust_leading }, + { bounding_box: nil, left_indent: 0 } + ) + end + end + end end From 4f3b2e99ea82401615a937805f3f6b475aff2038 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Mon, 6 Sep 2021 08:43:27 +0200 Subject: [PATCH 25/26] Update P tag --- examples/misc_elements.pdf | Bin 5436 -> 5436 bytes examples/random_content.pdf | Bin 40191 -> 40190 bytes examples/styles.pdf | Bin 3229 -> 3229 bytes lib/prawn_html/tags/p.rb | 4 ++-- 4 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/misc_elements.pdf b/examples/misc_elements.pdf index ee3215e673ee64377eb8e4d37b60416c7d10b8ec..e93442001d1272a0e7e4be9063947c1d5d07db42 100644 GIT binary patch delta 341 zcmYjNO-jQ+6s9J%l5~YaN)+Si@NA3dVqHTuTaP~ z6x_M?0D>OD6F3o5x_j??AK%Yw@+M#8@umbLP|+;lRK;gO0hZ=*5y}!AWo%3zF$~&8 zXr$NP0doSZHaIaW#?|mhOASJoYVr~Zi6E^XO@~dRyC^!pU%#yM_UL_Bb!Hc10f-t~ z6>sBgVZDmSXv(l7?NGz^eN0WCObrSLQp%FP*e>aW_t9A>=sI}{1V~yWms@{`6G(3v zC!U^o3i^%@Y1OxW&~%efg|?|-du~lZD<%hA0~eyC54J-XrF~r>7`locJqw%`(Z%qGSgzcWo diff --git a/examples/random_content.pdf b/examples/random_content.pdf index ec57a0dfbc77cd8dbd83e07868c20d06d02dc13c..6d2e3444ad341d9f341b31085a942b5f3b0bb064 100644 GIT binary patch delta 1053 zcmZvaJ!lj`6vr{yY>vIVH7|` z^5eeKqkOC>HJSv#THMbQUhJ*R3gavAk^G}mJ z61kq_!TtKc=n!M3Um5nRJnULx9<2^7@AT~7_AXO~XtdN^uqb z_o6ss3xyerONK4Rjbr=WyO#OGhim0Bd~|(5Pns@SX!FbpQ>-- zpaRbZr)!#H&8czO_)Un}AtexPL0GJv>)S#jfig6PUbRJLYa>FL^w0vOB2!n z!b0s*Tv~?ga5G9)h8u1Avtq;fop+c=Fy}n%q_sdaLCNNM9+pIFM5V$Kvh_3LX0}=| zO3}qR=fH7_rskd5a7($u1ZiP5QF028x(lnzbz+r_#b)dR$;5O{u!3*97hP%s&xp6( z^)X+)H~QelZHh>6&{gDw0nt*Z4yJ_YV4mO|O#g@Z!rNZwlelhI`(vNc4#8j-;q~%# NiaKLsM^7Ge{sCj(|C#^* delta 1054 zcmZvbziSjx5XUjuY>v%ca>?D^U6MQRMH7R9&-eSi6~tho(LyBnqtHgM(83tR!b0!| zSa=3vWEy`J>BJu(Ayp7W?d+@sQ%Ld$D1z9kZ+CC^Le8ybJ~Qw8=FQChS?&0{+Ohb3 zn_T{J&+F10YYP2!9~OIdcNuJyfqcCJUwY0sQeh1wE1P)TEyB(0fliwQ@3O%rn$HQM zD5gX*H0v?NuqFcW1YPz{dfuT^t$>Ur$}pcB8I%M|!<9WKWq_GMyh-3e)rYm*o^CF& z;Zj9aqTadG-*5}?qW6*$#=-AVcQQhryQFp*!{l5i0A_fMrPRD0C_ zB=uvFYpEZ`*9Jz1xv=%hs9vRUU^GL#$!x{rFi{{b6Idt);gLq6Qf3@88n(v1EZ|xp z9uc5U3C{QB;akCTDaXVZSa*HM`Wfgc7BiesY=j-ihV6U^CyRb}K(MlXPT391bbz_y zD!T7w@u(|pT`WndIkq|td;L3(y+gs(av47PzhmcP=~Y7C$A+vdC1ePGRlYhwxCNgF z-zG){o)1k|1BoT4cFK0%gkVFJK(q;Ap?bD=6O9GR&=`K*5}B!v3v`;@@g7sA8N!phS+aZ23&DPe&!ow{I{!^X}9pIJj& z^>n?>F;Ec6TvC$Wi4PGEPOoWzmNZfc}w PZf<4*Qn2|k$1Y|72lpO~ delta 116 zcmbO$IahLn3=6ZRp7~^XmI@XlQ$4fEvso6fnj07Zg&bISv6!3dnNI%4x{KY!NYB{7 z&}cFvoABhv98Qz{nRO;Ru(PvS>KT|?nt_EA*jKPxn(A2?PGS?8oWSllIf)~k-ONDG P)ZEMjq+s)7j$O Date: Thu, 9 Sep 2021 08:32:59 +0200 Subject: [PATCH 26/26] Bump to version 0.5.0 --- lib/prawn_html/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prawn_html/version.rb b/lib/prawn_html/version.rb index 0fbb52b..6867152 100644 --- a/lib/prawn_html/version.rb +++ b/lib/prawn_html/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module PrawnHtml # :nodoc: - VERSION = '0.4.2' + VERSION = '0.5.0' end