Class: ODDB::Text::Picture

Inherits:
String
  • Object
show all
Defined in:
lib/oddb/text/picture.rb

Constant Summary collapse

TWIP =
20
PCNT =
100
BDNS =

base density

180
DMAX =

maximum density

360

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#is_binary_data?

Constructor Details

#initializePicture

Returns a new instance of Picture.



16
17
18
# File 'lib/oddb/text/picture.rb', line 16

def initialize
  super('')
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def height
  @height
end

#height_goalObject

Returns the value of attribute height_goal.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def height_goal
  @height_goal
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def width
  @width
end

#width_goalObject

Returns the value of attribute width_goal.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def width_goal
  @width_goal
end

#xscaleObject

Returns the value of attribute xscale.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def xscale
  @xscale
end

#yscaleObject

Returns the value of attribute yscale.



11
12
13
# File 'lib/oddb/text/picture.rb', line 11

def yscale
  @yscale
end

Instance Method Details

#_xscaleObject



73
74
75
76
77
78
79
80
81
# File 'lib/oddb/text/picture.rb', line 73

def _xscale
  if @xscale
    @xscale
  elsif @width && @width_goal
    PCNT * @width_goal / @width 
  else
    PCNT
  end
end

#_yscaleObject



82
83
84
85
86
87
88
89
90
# File 'lib/oddb/text/picture.rb', line 82

def _yscale
  if @yscale
    @yscale
  elsif @height && @height_goal
    PCNT * @height_goal / @height 
  else
    PCNT
  end
end

#blobObject



19
20
21
# File 'lib/oddb/text/picture.rb', line 19

def blob
  [self].pack('H*')
end

#digestObject



22
23
24
# File 'lib/oddb/text/picture.rb', line 22

def digest
  @digest ||= Digest::MD5.hexdigest(self)
end

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/oddb/text/picture.rb', line 25

def empty?
  super #|| !image
rescue StandardError => err
  ODDB.logger.error("Text::Picture") { 
    sprintf "%s: %s", err.class, err.message
  }
  true
end

#filenameObject



53
54
55
# File 'lib/oddb/text/picture.rb', line 53

def filename
  @filename ||= "%s.png" % digest
end

#finalize!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/oddb/text/picture.rb', line 33

def finalize!
  wmf = File.join ODDB.config.var, path("%s.wmf" % digest)
  FileUtils.mkdir_p File.dirname(wmf)
  File.open(wmf, 'w') { |fh| fh.puts blob }

  twidth = @width ? @width * _xscale / PCNT : @width_goal
  theight = @height ? @height * _yscale / PCNT : @height_goal

  geom = sprintf("%ix%i!", twidth / TWIP, theight / TWIP)
  path = File.join ODDB.config.var, path("%s.png" % digest)
  
  # let imagemagick take care of file-conversion externally
  out = %x{convert -resize #{geom} #{wmf} #{path}}
  if $? != 0
    raise out
  end

  png = File.read path
  replace png.unpack('H*').first
end

#formatsObject



56
57
58
# File 'lib/oddb/text/picture.rb', line 56

def formats
  []
end

#imageObject



59
60
61
# File 'lib/oddb/text/picture.rb', line 59

def image
  Magick::Image.from_blob(blob).first
end

#path(fn = filename) ⇒ Object



62
63
64
# File 'lib/oddb/text/picture.rb', line 62

def path(fn = filename)
  File.join('/images', fn[0,2], fn)
end

#set_format(*ignore) ⇒ Object



65
66
# File 'lib/oddb/text/picture.rb', line 65

def set_format(*ignore)
end

#to_pngObject



67
68
69
# File 'lib/oddb/text/picture.rb', line 67

def to_png
  blob
end

#to_sObject



70
71
72
# File 'lib/oddb/text/picture.rb', line 70

def to_s
  image.inspect
end