Class: RubyPager::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pager/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ex_file_name, ex_data) ⇒ Page

Returns a new instance of Page.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_pager/page.rb', line 6

def initialize(ex_file_name,ex_data)
  @logger = Utils::ApplicationLogger.instance
  @logger.info("Loading data from XML #{ex_file_name}")
  @file_name=ex_file_name
  @data=ex_data
  @text_regions=Hash.new
  =.new(@data["PcGts"]["Metadata"])
  load_xml_schema_data
  load_xml_image_info
  load_text_regions
  @reading_order=Reading_Order.new(@data["PcGts"]["Page"]["ReadingOrder"])
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def file_name
  @file_name
end

#image_dataObject (readonly)

Returns the value of attribute image_data.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def image_data
  @image_data
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def 
  
end

#xmlnsObject (readonly)

Returns the value of attribute xmlns.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def xmlns
  @xmlns
end

#xmlns_xsiObject (readonly)

Returns the value of attribute xmlns_xsi.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def xmlns_xsi
  @xmlns_xsi
end

#xsi_schemaLocationObject (readonly)

Returns the value of attribute xsi_schemaLocation.



5
6
7
# File 'lib/ruby_pager/page.rb', line 5

def xsi_schemaLocation
  @xsi_schemaLocation
end

Class Method Details

.blank(ex_image_name) ⇒ Object



37
38
39
40
# File 'lib/ruby_pager/page.rb', line 37

def self.blank(ex_image_name)
  data=self.blank_data
  return Page.new(ex_image_name,data)
end

.blank_dataObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_pager/page.rb', line 92

def self.blank_data
  logger = Utils::ApplicationLogger.instance
  logger.info("Creating blank XML data")
  res=Hash.new
  res["PcGts"]=Hash.new
  res["PcGts"]["Metadata"]=.blank_data
  res["PcGts"]["@xmlns:xsi"]="http://www.w3.org/2001/XMLSchema-instance"
  res["PcGts"]["@xmlns"]="http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"
  res["PcGts"]["@xsi:schemaLocation"]="http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15 http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15/pagecontent.xsd"
  res["PcGts"]["@pcGtsId"]=""
  res["PcGts"]["Page"]=Hash.new
  res["PcGts"]["Page"]["TextRegion"]=Array.new
  res["PcGts"]["Page"]["ReadingOrder"]=Hash.new
  res["PcGts"]["Page"]["@imageFilename"]="blank.jpg"
  res["PcGts"]["Page"]["@imageWidth"]="0"
  res["PcGts"]["Page"]["@imageHeight"]="0"
  return res
end

.create_from_image(ex_image_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_pager/page.rb', line 26

def self.create_from_image(ex_image_name)
  logger = Utils::ApplicationLogger.instance
  logger.info("Generating XML for image #{ex_image_name}")
  image=Utils::Image.new(ex_image_name)
  data=self.blank_data
  data["PcGts"]["Page"]["@imageFilename"]=ex_image_name
  data["PcGts"]["Page"]["@imageWidth"]=image.rows.to_s
  data["PcGts"]["Page"]["@imageHeight"]=image.columns.to_s
  return Page.new(ex_image_name,data)
end

.load_from_xml(ex_file_name) ⇒ Object



19
20
21
22
23
24
# File 'lib/ruby_pager/page.rb', line 19

def self.load_from_xml(ex_file_name)
  logger = Utils::ApplicationLogger.instance
  logger.info("Loading XML #{ex_file_name}")
  data=XML.load(ex_file_name)
  return Page.new(ex_file_name,data)
end

Instance Method Details

#[](ex_key) ⇒ Object

Raises:

  • (RangeError)


58
59
60
61
# File 'lib/ruby_pager/page.rb', line 58

def [](ex_key)
  raise(RangeError, "Index #{ex_key} is out of range") unless @text_regions.has_key? ex_key
  return @text_regions[ex_key]
end

#create_full_page_region(region_id) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/ruby_pager/page.rb', line 42

def create_full_page_region(region_id)
  @logger.info("Creating full page region #{region_id}")
  data=Text_Region.blank_data
  raise(ArgumentError, "Region id #{region_id} is already in use") if @text_regions.has_key? region_id
  data["Coords"]["@points"]="0,0 0,#{@image_data.width} #{@image_data.height},#{@image_data.width} #{@image_data.height},0"
  data["@id"]=region_id
  push(Text_Region.new(0,data))
end

#delete(ex_region_id) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/ruby_pager/page.rb', line 71

def delete(ex_region_id)
  if has_region? ex_region_id
    @logger.info("Deleting text region #{ex_region_id}")
    @text_regions.delete(ex_region_id)
    review_regions_index()
  else
    raise(ArgumentError, "Region id #{ex_region_id} does not exist so it can not be deleted")
  end
end

#each_regionObject



67
68
69
# File 'lib/ruby_pager/page.rb', line 67

def each_region
  @text_regions.values.each {|text_region| yield text_region}
end

#has_region?(ex_region_id) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ruby_pager/page.rb', line 63

def has_region?(ex_region_id)
  return @text_regions.has_key? ex_region_id
end

#push(ex_text_region) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# File 'lib/ruby_pager/page.rb', line 81

def push(ex_text_region)
  raise(ArgumentError, "Got passed a non text region object") if ex_text_region.class != RubyPager::Text_Region
  raise(ArgumentError, "Region id #{ex_text_region.id} is already in use") if @text_regions.has_key? ex_text_region.id
  ex_text_region.index=@text_regions.size
  @text_regions[ex_text_region.id]=ex_text_region
end

#save(ex_save_name = @file_name) ⇒ Object



51
52
53
54
55
# File 'lib/ruby_pager/page.rb', line 51

def save(ex_save_name=@file_name)
  @logger.info("Saving page object #{@file_name} to #{ex_save_name}")
  consolidate_data
  XML.save(ex_save_name, @data)
end

#sizeObject



88
89
90
# File 'lib/ruby_pager/page.rb', line 88

def size
  return @text_regions.size
end