Skip to content

Commit 9d1b857

Browse files
authored
Merge pull request #27 from blocknotes/fix-some-elements-rendering
Improve elements rendering
2 parents d04ef23 + 4f3b2e9 commit 9d1b857

45 files changed

Lines changed: 629 additions & 424 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Naming/MethodParameterName:
2929

3030
RSpec/ExampleLength:
3131
# default: 5
32-
Max: 10
32+
Max: 15
3333

3434
RSpec/MultipleMemoizedHelpers:
3535
# default: 5

examples/headings.pdf

-18 Bytes
Binary file not shown.

examples/misc_elements.pdf

32 Bytes
Binary file not shown.

examples/random_content.pdf

-445 Bytes
Binary file not shown.

examples/styles.pdf

-24 Bytes
Binary file not shown.

lib/prawn-html.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module PrawnHtml
4-
PX = 0.66 # conversion constant for pixel sixes
4+
PX = 0.6 # conversion constant for pixel sixes
55

66
COLORS = {
77
'aliceblue' => 'f0f8ff',

lib/prawn_html/context.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
module PrawnHtml
44
class Context < Array
5-
DEF_FONT_SIZE = 10.3
5+
DEF_FONT_SIZE = 16 * PX
66

7+
attr_reader :previous_tag
78
attr_accessor :last_text_node
89

910
# Init the Context
1011
def initialize(*_args)
1112
super
1213
@last_text_node = false
1314
@merged_styles = nil
15+
@previous_tag = nil
1416
end
1517

1618
# Add an element to the context
@@ -60,8 +62,10 @@ def merged_styles
6062

6163
# Remove the last element from the context
6264
def remove_last
65+
last.on_context_remove(self) if last.respond_to?(:on_context_remove)
6366
@merged_styles = nil
6467
@last_text_node = false
68+
@previous_tag = last.tag
6569
pop
6670
end
6771

lib/prawn_html/document_renderer.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ def prepare_text(content)
113113
def output_content(buffer, block_styles)
114114
apply_callbacks(buffer)
115115
left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f
116-
options = block_styles.slice(:align, :leading, :mode, :padding_left)
117-
options[:indent_paragraphs] = left_indent if left_indent > 0
118-
pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles))
116+
options = block_styles.slice(:align, :indent_paragraphs, :leading, :mode, :padding_left)
117+
options[:leading] = adjust_leading(buffer, options[:leading])
118+
pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles), left_indent: left_indent)
119119
end
120120

121121
def apply_callbacks(buffer)
@@ -126,6 +126,12 @@ def apply_callbacks(buffer)
126126
end
127127
end
128128

129+
def adjust_leading(buffer, leading)
130+
return leading if leading
131+
132+
(buffer.map { |item| item[:size] || Context::DEF_FONT_SIZE }.max * 0.055).round(4)
133+
end
134+
129135
def bounds(buffer, options, block_styles)
130136
return unless block_styles[:position] == :absolute
131137

lib/prawn_html/pdf_wrapper.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ def image(src, options = {})
106106
# @param buffer [Array] array of text items
107107
# @param options [Hash] hash of options
108108
# @param bounding_box [Array] bounding box arguments, if bounded
109-
def puts(buffer, options, bounding_box: nil)
110-
return pdf.formatted_text(buffer, options) unless bounding_box
109+
def puts(buffer, options, bounding_box: nil, left_indent: 0)
110+
return output_buffer(buffer, options, left_indent: left_indent) unless bounding_box
111111

112112
current_y = pdf.cursor
113113
pdf.bounding_box(*bounding_box) do
114-
pdf.formatted_text(buffer, options)
114+
output_buffer(buffer, options, left_indent: left_indent)
115115
end
116116
pdf.move_cursor_to(current_y)
117117
end
@@ -130,5 +130,12 @@ def underline(x1:, x2:, y:)
130130
private
131131

132132
attr_reader :pdf
133+
134+
def output_buffer(buffer, options, left_indent:)
135+
formatted_text = proc { pdf.formatted_text(buffer, options) }
136+
return formatted_text.call if left_indent == 0
137+
138+
pdf.indent(left_indent, 0, &formatted_text)
139+
end
133140
end
134141
end

lib/prawn_html/tags/a.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ class A < Tag
66
ELEMENTS = [:a].freeze
77

88
def tag_styles
9-
"href: #{attrs.href}" if attrs.href
9+
return unless attrs.href
10+
11+
<<~STYLES
12+
color: #00E;
13+
href: #{attrs.href};
14+
text-decoration: underline;
15+
STYLES
1016
end
1117
end
1218
end

0 commit comments

Comments
 (0)