Skip to content

Commit 951f12b

Browse files
committed
HACK track the graphics state for each character
how does it vary over the page? Are there some characters that we should ignore?
1 parent e027096 commit 951f12b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/pdf/reader/page_text_receiver.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class PageTextReceiver
2626
def_delegators :@state, :set_line_cap_style, :set_line_dash, :set_line_join_style
2727
def_delegators :@state, :set_line_width, :set_miter_limit
2828

29+
# Graphics State Operators (colour)
30+
def_delegators :@state, :set_cmyk_color_for_stroking, :set_cmyk_color_for_nonstroking
31+
def_delegators :@state, :set_gray_color_for_stroking, :set_gray_color_for_nonstroking
32+
def_delegators :@state, :set_rgb_color_for_stroking, :set_rgb_color_for_nonstroking
33+
def_delegators :@state, :set_stroke_color_space, :set_nonstroke_color_space
34+
2935
# Matrix Operators
3036
def_delegators :@state, :concatenate_matrix
3137

@@ -140,7 +146,7 @@ def internal_show_text(string)
140146
th = 1
141147
scaled_glyph_width = glyph_width * @state.font_size * th
142148
unless utf8_chars == SPACE
143-
@characters << TextRun.new(newx, newy, scaled_glyph_width, @state.font_size, utf8_chars)
149+
@characters << TextRun.new(newx, newy, scaled_glyph_width, @state.font_size, utf8_chars, @state.clone_state)
144150
end
145151
@state.process_glyph_displacement(glyph_width, 0, utf8_chars == SPACE)
146152
end

lib/pdf/reader/text_run.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ class PDF::Reader
77
class TextRun
88
include Comparable
99

10-
attr_reader :origin, :width, :font_size, :text
10+
attr_reader :origin, :width, :font_size, :text, :state
1111

1212
alias :to_s :text
1313

14-
def initialize(x, y, width, font_size, text)
14+
def initialize(x, y, width, font_size, text, state)
1515
@origin = PDF::Reader::Point.new(x, y)
1616
@width = width
1717
@font_size = font_size
1818
@text = text
19+
@state = state
1920
end
2021

2122
# Allows collections of TextRun objects to be sorted. They will be sorted
@@ -62,14 +63,14 @@ def +(other)
6263
raise ArgumentError, "#{other} cannot be merged with this run" unless mergable?(other)
6364

6465
if (other.x - endx) <( font_size * 0.2)
65-
TextRun.new(x, y, other.endx - x, font_size, text + other.text)
66+
TextRun.new(x, y, other.endx - x, font_size, text + other.text, {})
6667
else
67-
TextRun.new(x, y, other.endx - x, font_size, "#{text} #{other.text}")
68+
TextRun.new(x, y, other.endx - x, font_size, "#{text} #{other.text}", {})
6869
end
6970
end
7071

7172
def inspect
72-
"#{text} w:#{width} f:#{font_size} @#{x},#{y}"
73+
"#{text} w:#{width} f:#{font_size} @#{x},#{y} #{@state.inspect}"
7374
end
7475

7576
def intersect?(other_run)

0 commit comments

Comments
 (0)