prawn-styled-text 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +23 -15
- data/examples/test.html +6 -5
- data/lib/prawn-styled-text/callbacks.rb +31 -0
- data/lib/prawn-styled-text/prawn-document.rb +2 -5
- data/lib/prawn-styled-text/prawn-styled-text.rb +19 -2
- data/lib/prawn-styled-text/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c73fb17ed91bd33843991a644f9c635c2d2cfaa5
|
4
|
+
data.tar.gz: 57aad42cddc27f7302df26aba88ae5379a3c1f5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fbcaead4bdf4d53477cd4195b62032492056a29d5a33e78cf13c9c878814b2d798df50e872fdbaa29a768e5cf085bdb7592d75298ac86fd6b30914416312c87
|
7
|
+
data.tar.gz: b90e425105862e661321e3dd54295d73e9cf3ba0e3cab74a41f4a4dbcb31a0c6752537cb7d31a747762b7e1c1ac3cb50a1a205e2f94b9c131fcdd9364a64552e
|
data/README.md
CHANGED
@@ -20,26 +20,33 @@ For more examples see this [folder](https://github.com/blocknotes/prawn-styled-t
|
|
20
20
|
## Supported tags & attributes
|
21
21
|
|
22
22
|
HTML tags:
|
23
|
-
- a
|
24
|
-
- b
|
25
|
-
- br
|
26
|
-
-
|
27
|
-
-
|
28
|
-
-
|
29
|
-
-
|
30
|
-
-
|
31
|
-
-
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
-
|
23
|
+
- a (link)
|
24
|
+
- b (bold)
|
25
|
+
- br (new line)
|
26
|
+
- del (strike-through)
|
27
|
+
- div (block element)
|
28
|
+
- em (italic)
|
29
|
+
- h1 - h6 (headings)
|
30
|
+
- hr (horizontal line)
|
31
|
+
- i (italic)
|
32
|
+
- ins (underline)
|
33
|
+
- img (image)
|
34
|
+
- li (list item)
|
35
|
+
- mark (highlight)
|
36
|
+
- p (block element)
|
37
|
+
- s (strike-through)
|
38
|
+
- small (smaller text)
|
39
|
+
- span (inline element)
|
40
|
+
- strong (bold)
|
41
|
+
- u (underline)
|
42
|
+
- ul (list)
|
37
43
|
|
38
44
|
CSS attributes:
|
45
|
+
- background (for *mark* tag, only 6 hex digits format, # is ignored - ex. `style="background: #FFCC00"`)
|
39
46
|
- color (only 6 hex digits format, # is ignored - ex. `style="color: #FFBB11"`)
|
40
47
|
- font-family (ex. `style="font: Courier"`)
|
41
48
|
- font-size (units are ignored - ex. `style="font-size: 20px"`)
|
42
|
-
- font-style (accepts list of values - ex. `style="font-style: bold, italic"`)
|
49
|
+
- font-style (*:bold*, *:bold_italic*, *:italic*, *:normal*; accepts list of values - ex. `style="font-style: bold, italic"`)
|
43
50
|
- height (for *img* tag, ex. `<img src="test.jpg" style="width: 50%; height: 200"/>`)
|
44
51
|
- href (for *a* tag, ex. `<a href="http://www.google.com/">Google</a>`)
|
45
52
|
- letter-spacing (ex. `style="letter-spacing: 1.5"`)
|
@@ -52,6 +59,7 @@ CSS attributes:
|
|
52
59
|
|
53
60
|
Olther attributes:
|
54
61
|
- dash (for *hr* tag, ex. `<hr style="dash: 4"/>`)
|
62
|
+
- image-at (for *img* tag, origin (0, 0) is left bottom, ex. `<img src="image.jpg" style="image-at: 100, 600" />`)
|
55
63
|
- image-position (for *img* tag, ex. `<img src="image.jpg" style="image-position: center" />`)
|
56
64
|
- image-scale (for *img* tag, ex. `<img src="image.jpg" style="image-scale: 0.3" />`)
|
57
65
|
- list-symbol (for *ul* tag, ex. `<ul style="list-symbol: -">`)
|
data/examples/test.html
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
<h1>A test document</h1>
|
1
|
+
<h1>A test document <small>(with the supported elements)</small></h1>
|
2
2
|
This is <i style="color: #888888">italic</i>, <u>underline</u> and <b>bold, <i>bold italic</i></b>
|
3
3
|
<br/>
|
4
4
|
|
@@ -9,7 +9,7 @@ This is <i style="color: #888888">italic</i>, <u>underline</u> and <b>bold, <i>b
|
|
9
9
|
<div style="text-align: center">text-align: center</div>
|
10
10
|
<div style="text-align: right">text-align: right</div>
|
11
11
|
<div style="text-align: justify">text-align: justify</div>
|
12
|
-
<hr/>
|
12
|
+
<hr style="dash: 5, 5, 1, 5"/>
|
13
13
|
<div style="margin-top: 30px">margin-top</div>
|
14
14
|
<div style="margin-left: 30px">margin-left</div>
|
15
15
|
<br/>
|
@@ -31,16 +31,17 @@ This is <i style="color: #888888">italic</i>, <u>underline</u> and <b>bold, <i>b
|
|
31
31
|
<br/>
|
32
32
|
|
33
33
|
Some HTML entities: € × ÷ ½ « » ©
|
34
|
-
<p>A paragraph</p><p>Another paragraph</p>
|
35
|
-
<hr style="color: #
|
34
|
+
<p>A paragraph</p><p>Another paragraph with <mark style="background: #ffbb11">highlight words</mark> and <del>deleted words</del>.</p>
|
35
|
+
<hr style="color: #ff00ff; dash: 1" />
|
36
36
|
<br/>
|
37
37
|
|
38
38
|
A span with a color: <span style="color: #4488CC">this is <span style="color: #ff0000; font-family: 'Courier'; font-size: 20px">courier red 20</span>, this is <span style="color: #008800; font-style: bold, italic">green bold italic</span>, this is <span style="letter-spacing: 2">character spacing 2</span>, this is <a href="http://www.google.com">a Google link</a></span>
|
39
39
|
<h3 style="mode: stroke">Stroke mode</h3>
|
40
|
-
<div style="font-size: 14px; line-height: 12px; text-align: justify">line-height test... Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod <em>tempor</em> incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure <strong>dolor</strong> in reprehenderit in voluptate velit
|
40
|
+
<div style="font-size: 14px; line-height: 12px; text-align: justify">line-height test... Lorem ipsum dolor sit amet, consectetur <small>adipisicing</small> elit, sed do eiusmod <em>tempor</em> incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure <strong>dolor</strong> in reprehenderit in voluptate velit.</div>
|
41
41
|
<hr/>
|
42
42
|
<br/>
|
43
43
|
|
44
44
|
An image:
|
45
45
|
<img src="image.jpg" style="width: 50%" />
|
46
|
+
<!-- <img src="image.jpg" style="image-at: 100, 600" /> -->
|
46
47
|
<!-- <img src="image.jpg" style="image-position: center; image-scale: 0.3" /> -->
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module PrawnStyledText
|
2
|
+
class HighlightCallback
|
3
|
+
def initialize( pdf )
|
4
|
+
@document = pdf
|
5
|
+
end
|
6
|
+
|
7
|
+
def set_color( color )
|
8
|
+
@color = color ? color : DEF_BG_MARK
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_behind( fragment )
|
12
|
+
original_color = @document.fill_color
|
13
|
+
@document.fill_color = @color
|
14
|
+
@document.fill_rectangle(fragment.top_left, fragment.width, fragment.height)
|
15
|
+
@document.fill_color = original_color
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class StrikeThroughCallback
|
20
|
+
def initialize( pdf )
|
21
|
+
@document = pdf
|
22
|
+
end
|
23
|
+
|
24
|
+
def render_in_front( fragment )
|
25
|
+
y = ( fragment.top_left[1] + fragment.bottom_left[1] ) / 2
|
26
|
+
@document.stroke do
|
27
|
+
@document.line [ fragment.top_left[0], y ], [ fragment.top_right[0], y ]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -21,7 +21,6 @@ Prawn::Document.class_eval do
|
|
21
21
|
parts[-1][:text] = parts[-1][:text].rstrip
|
22
22
|
parts.pop if parts[-1][:text].empty?
|
23
23
|
end
|
24
|
-
# p '###', parts ### DEBUG
|
25
24
|
if parts.any?
|
26
25
|
parts[0][:text] = extra_options[:pre] + parts[0][:text] if extra_options[:pre]
|
27
26
|
self.indent( extra_options[:margin_left] ) do
|
@@ -33,9 +32,7 @@ Prawn::Document.class_eval do
|
|
33
32
|
extra_options = { margin_left: 0 }
|
34
33
|
end
|
35
34
|
options = context[:options]
|
36
|
-
# p '@@@', options ### DEBUG
|
37
35
|
if type == :text_node
|
38
|
-
# prepare options
|
39
36
|
text_options[:align] = options[:'text-align'].to_sym if options[:'text-align']
|
40
37
|
margin_top = options.delete( :'margin-top' ).to_i
|
41
38
|
self.move_down( margin_top ) if margin_top > 0
|
@@ -44,13 +41,13 @@ Prawn::Document.class_eval do
|
|
44
41
|
if !text_options[:leading] && ( leading = options.delete( :'line-height' ).to_i ) > 0
|
45
42
|
text_options[:leading] = leading
|
46
43
|
end
|
47
|
-
text_options[:mode] = options[:
|
44
|
+
text_options[:mode] = options[:mode].to_sym if options[:mode]
|
48
45
|
extra_options[:pre] = context[:pre] if context[:pre]
|
49
46
|
parts << { text: text }.merge( options ) # push the data
|
50
47
|
elsif type == :closing_tag
|
51
48
|
self.formatted_text( context[:text] ) if context[:text]
|
52
49
|
if context[:tag] == :hr
|
53
|
-
self.dash( options[:dash].to_i ) if options[:dash]
|
50
|
+
self.dash( options[:dash].include?( ',' ) ? options[:dash].split( ',' ).map( &:to_i ) : options[:dash].to_i ) if options[:dash]
|
54
51
|
if options[:color]
|
55
52
|
last_stroke_color = self.stroke_color
|
56
53
|
self.stroke_color( options[:color] )
|
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'oga'
|
2
|
+
require_relative 'callbacks'
|
2
3
|
require_relative 'prawn-document'
|
3
4
|
|
4
5
|
module PrawnStyledText
|
5
6
|
BLOCK_TAGS = [ :br, :div, :h1, :h2, :h3, :h4, :h5, :h6, :hr, :li, :p, :ul ]
|
7
|
+
DEF_BG_MARK = 'ffff00'
|
6
8
|
DEF_HEADING_T = 16
|
7
9
|
DEF_HEADING_H = 8
|
8
10
|
DEF_MARGIN_UL = 15
|
@@ -83,6 +85,7 @@ module PrawnStyledText
|
|
83
85
|
def self.text_node( pdf, data )
|
84
86
|
context = { pre: '', options: {} }
|
85
87
|
styles = []
|
88
|
+
font_size = pdf.font_size
|
86
89
|
data.each do |part|
|
87
90
|
# Evalutate tag
|
88
91
|
tag = part[:name]
|
@@ -92,6 +95,9 @@ module PrawnStyledText
|
|
92
95
|
context[:options][:link] = link if link
|
93
96
|
when :b, :strong # bold
|
94
97
|
styles.push :bold
|
98
|
+
when :del, :s
|
99
|
+
@@strike_through ||= StrikeThroughCallback.new( pdf )
|
100
|
+
context[:options][:callback] = @@strike_through
|
95
101
|
when :h1, :h2, :h3, :h4, :h5, :h6
|
96
102
|
context[:options][:size] = HEADINGS[tag]
|
97
103
|
context[:options][:'margin-top'] = DEF_HEADING_T
|
@@ -101,13 +107,24 @@ module PrawnStyledText
|
|
101
107
|
when :li # list item
|
102
108
|
context[:options][:'margin-left'] = @@margin_ul
|
103
109
|
context[:pre] = @@symbol_ul.force_encoding( 'windows-1252' ).encode( 'UTF-8' )
|
104
|
-
when :
|
110
|
+
when :mark
|
111
|
+
@@highlight ||= HighlightCallback.new( pdf )
|
112
|
+
@@highlight.set_color nil
|
113
|
+
context[:options][:callback] = @@highlight
|
114
|
+
when :small
|
115
|
+
context[:options][:size] = font_size * 0.66
|
116
|
+
when :u, :ins # underline
|
105
117
|
styles.push :underline
|
106
118
|
end
|
107
119
|
context[:options][:styles] = styles if styles.any?
|
108
120
|
# Evalutate attributes
|
109
121
|
attributes = part[:node].get 'style'
|
110
|
-
|
122
|
+
if attributes
|
123
|
+
values = adjust_values( pdf, attributes.scan( /\s*([^:]+):\s*([^;]+)[;]*/ ) )
|
124
|
+
@@highlight.set_color( values[:background].delete( '#' ) ) if tag == :mark && values[:background]
|
125
|
+
context[:options].merge! values
|
126
|
+
end
|
127
|
+
font_size = context[:options][:size] if font_size
|
111
128
|
end
|
112
129
|
context
|
113
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-styled-text
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: prawn
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- examples/test.html.erb
|
59
59
|
- examples/test.html.slim
|
60
60
|
- lib/prawn-styled-text.rb
|
61
|
+
- lib/prawn-styled-text/callbacks.rb
|
61
62
|
- lib/prawn-styled-text/prawn-document.rb
|
62
63
|
- lib/prawn-styled-text/prawn-styled-text.rb
|
63
64
|
- lib/prawn-styled-text/version.rb
|