Skip to content

Commit

Permalink
Add Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Jan 2, 2017
1 parent 20dd21c commit 1a79cb1
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 106 deletions.
45 changes: 45 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,45 @@
AllCops:
Include:
- '**/Rakefile'
- '*.gemspec'
Exclude:
- 'pkg/**/*'

# These cops need to remain disabled for valid reasons on this code base

# set_ methods are the part of PDF::Reader API
Style/AccessorMethodName:
Enabled: false

# We need to reference non-ascii characters when testing and explaining
# behavior related to win-1252, UTF-8 and UTF-16 encodings for example.
Style/AsciiComments:
Enabled: false

Style/BlockDelimiters:
EnforcedStyle: semantic

Style/Documentation:
Enabled: false

# We don't always prefer modified if statements even if they do fit on
# a line.
Style/IfUnlessModifier:
Enabled: false

Style/MultilineOperationIndentation:
Exclude:
- prawn.gemspec

Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%': '()'
'%i': '[]'
'%I': '[]'
'%q': '()'
'%Q': '()'
'%r': '{}'
'%s': '()'
'%w': '[]'
'%W': '[]'
'%x': '()'
17 changes: 10 additions & 7 deletions Rakefile
@@ -1,24 +1,25 @@
require "bundler"
require 'bundler'
Bundler.setup

require 'rake'
require 'rspec/core/rake_task'
require 'rubygems/package_task'
require 'rubocop/rake_task'

task :default => [:spec]
task default: [:spec, :rubocop]

desc "Run all rspec files"
RSpec::Core::RakeTask.new("spec") do |c|
c.rspec_opts = "-t ~unresolved"
desc 'Run all rspec files'
RSpec::Core::RakeTask.new('spec') do |c|
c.rspec_opts = '-t ~unresolved'
end

spec = Gem::Specification.load "pdf-inspector.gemspec"
spec = Gem::Specification.load 'pdf-inspector.gemspec'
Gem::PackageTask.new(spec) do |pkg|
pkg.need_zip = true
pkg.need_tar = true
end

desc "Run a console with Prawn loaded"
desc 'Run a console with Prawn loaded'
task :console do
require 'irb'
require 'irb/completion'
Expand All @@ -33,3 +34,5 @@ YARD::Rake::YardocTask.new do |t|
t.options = ['--output-dir', 'doc/html']
end
task docs: :yard

RuboCop::RakeTask.new
40 changes: 21 additions & 19 deletions lib/pdf/inspector.rb
@@ -1,36 +1,38 @@
require "rubygems"
require "pdf/reader"
require "pdf/inspector/text"
require "pdf/inspector/xobject"
require "pdf/inspector/extgstate"
require "pdf/inspector/graphics"
require "pdf/inspector/page"
require "stringio"
require 'rubygems'
require 'pdf/reader'
require 'pdf/inspector/text'
require 'pdf/inspector/xobject'
require 'pdf/inspector/extgstate'
require 'pdf/inspector/graphics'
require 'pdf/inspector/page'
require 'stringio'

module PDF
class Inspector
def self.analyze(output,*args,&block)
def self.analyze(output, *args, &block)
if output.is_a?(String)
output = StringIO.new(output)
end
obs = self.new(*args, &block)
obs = new(*args, &block)
PDF::Reader.open(output) do |reader|
reader.pages.each do |page|
page.walk(obs)
end
end
obs
end
def self.analyze_file(filename,*args,&block)
File.open(filename, "rb") do |io|
analyze(io, *args,&block)
obs
end

def self.analyze_file(filename, *args, &block)
File.open(filename, 'rb') do |io|
analyze(io, *args, &block)
end
end
end

def self.parse(obj)
PDF::Reader::Parser.new(
PDF::Reader::Buffer.new(StringIO.new(obj)), nil).parse_token
PDF::Reader::Buffer.new(StringIO.new(obj)),
nil
).parse_token
end
end
end
12 changes: 6 additions & 6 deletions lib/pdf/inspector/extgstate.rb
Expand Up @@ -8,13 +8,13 @@ def initialize
end

def page=(page)
page.graphic_states.each do |label, stream|
page.graphic_states.each do |_label, stream|
@extgstates << {
:opacity => stream[:ca],
:stroke_opacity => stream[:CA],
:soft_mask => stream[:SMask],
:blend_mode => stream[:BM]
}
opacity: stream[:ca],
stroke_opacity: stream[:CA],
soft_mask: stream[:SMask],
blend_mode: stream[:BM]
}
end
end
end
Expand Down
73 changes: 35 additions & 38 deletions lib/pdf/inspector/graphics.rb
@@ -1,68 +1,67 @@
module PDF
class Inspector
module Graphics
class Inspector
module Graphics
class Line < Inspector
attr_accessor :points, :widths

def initialize
@points = []
@widths = []
end
@widths = []
end

def append_line(*params)
@points << params
end
end

def begin_new_subpath(*params)
@points << params
end
end

def set_line_width(params)
@widths << params
end
end

end

class Rectangle < Inspector
attr_reader :rectangles

def initialize
@rectangles = []
@rectangles = []
end

def append_rectangle(*params)
@rectangles << { :point => params[0..1],
:width => params[2],
:height => params[3] }
def append_rectangle(*params)
@rectangles << {
point: params[0..1],
width: params[2],
height: params[3]
}
end
end

class Curve < Inspector

class Curve < Inspector
attr_reader :coords

def initialize
@coords = []
end
@coords = []
end

def begin_new_subpath(*params)
def begin_new_subpath(*params)
@coords += params
end

def append_curved_segment(*params)
@coords += params
end
end
end

end

class Color < Inspector
attr_reader :stroke_color, :fill_color, :stroke_color_count,
attr_reader :stroke_color, :fill_color, :stroke_color_count,
:fill_color_count, :stroke_color_space_count, :color_space

def initialize
@stroke_color_count = 0
@fill_color_count = 0
@stroke_color_space_count = {:DeviceCMYK => 0, :DeviceRGB => 0}
@fill_color_count = 0
@stroke_color_space_count = { DeviceCMYK: 0, DeviceRGB: 0 }
end

def set_color_for_stroking_and_special(*params)
Expand All @@ -74,14 +73,13 @@ def set_color_for_nonstroking_and_special(*params)
@fill_color_count += 1
@fill_color = params
end

def set_stroke_color_space(*params)
@stroke_color_space_count[params[0]] += 1
@color_space = params[0]
end
end

end

class Dash < Inspector
attr_reader :stroke_dash, :stroke_dash_count

Expand All @@ -94,7 +92,7 @@ def set_line_dash(*params)
@stroke_dash = params
end
end

class CapStyle < Inspector
attr_reader :cap_style, :cap_style_count

Expand All @@ -107,7 +105,7 @@ def set_line_cap_style(*params)
@cap_style = params[0]
end
end

class JoinStyle < Inspector
attr_reader :join_style, :join_style_count

Expand All @@ -120,7 +118,7 @@ def set_line_join_style(*params)
@join_style = params[0]
end
end

class Matrix < Inspector
attr_reader :matrices

Expand All @@ -132,20 +130,20 @@ def concatenate_matrix(*values)
@matrices << values
end
end

class State < Inspector
attr_reader :save_graphics_state_count, :restore_graphics_state_count

def initialize
@save_graphics_state_count = 0
@restore_graphics_state_count = 0
@restore_graphics_state_count = 0
end
def save_graphics_state(*values)

def save_graphics_state(*)
@save_graphics_state_count += 1
end

def restore_graphics_state(*values)
def restore_graphics_state(*)
@restore_graphics_state_count += 1
end
end
Expand All @@ -164,7 +162,6 @@ def page=(page)
end
end
end

end
end
end
13 changes: 6 additions & 7 deletions lib/pdf/inspector/page.rb
Expand Up @@ -8,29 +8,28 @@ class Page < Inspector
attr_reader :pages

def_delegators :@state, :set_text_font_and_size

def initialize
@pages = []
end

def page=(page)
@pages << {:size => page.attributes[:MediaBox][-2..-1], :strings => []}
@pages << { size: page.attributes[:MediaBox][-2..-1], strings: [] }
@state = PDF::Reader::PageState.new(page)
end
end

def show_text(*params)
params.each do |param|
@pages.last[:strings] << @state.current_font.to_utf8(param)
end
end

def show_text_with_positioning(*params)
def show_text_with_positioning(*params)
# ignore kerning information
show_text params[0].reject { |e|
Numeric === e
e.is_a? Numeric
}.join
end

end
end
end
end

0 comments on commit 1a79cb1

Please sign in to comment.