Class: TJStamp

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: '王小明', size: 256, corner_size: 10, stroke_width: 20, font: File.join(__dir__, 'simsun.ttf'), color: 'red', background_color: 'none') ⇒ TJStamp



6
7
8
9
10
11
12
13
14
# File 'lib/tjstamp.rb', line 6

def initialize name: '王小明', size: 256, corner_size: 10, stroke_width: 20, font: File.join(__dir__, 'simsun.ttf'), color: 'red', background_color: 'none'
  self.name = name
  self.size = size
  self.corner_size = corner_size
  self.stroke_width = stroke_width
  self.font = font
  self.color = color
  self.background_color = background_color
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



4
5
6
# File 'lib/tjstamp.rb', line 4

def background_color
  @background_color
end

#colorObject

Returns the value of attribute color.



4
5
6
# File 'lib/tjstamp.rb', line 4

def color
  @color
end

#corner_sizeObject

Returns the value of attribute corner_size.



4
5
6
# File 'lib/tjstamp.rb', line 4

def corner_size
  @corner_size
end

#fontObject

Returns the value of attribute font.



4
5
6
# File 'lib/tjstamp.rb', line 4

def font
  @font
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/tjstamp.rb', line 4

def name
  @name
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/tjstamp.rb', line 4

def size
  @size
end

#stroke_widthObject

Returns the value of attribute stroke_width.



4
5
6
# File 'lib/tjstamp.rb', line 4

def stroke_width
  @stroke_width
end

Instance Method Details

#stampObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tjstamp.rb', line 21

def stamp
  raise 'Length of name must be in (1..4)' unless (1..4) === name.length
  canvas = Magick::Image.new(size, size){ self.background_color = 'none' }

  # square
  square = Magick::Draw.new
  square.stroke(color)
  square.fill(background_color)
  square.stroke_width(stroke_width)
  corner_width = corner_height = corner_size
  upper_x, left_y = stroke_width / 2, stroke_width / 2
  lower_x, right_y = size - stroke_width / 2, size - stroke_width / 2
  square.roundrectangle(upper_x, left_y, lower_x, right_y, corner_width, corner_height)
  # square.polyline(180,70, 173,78, 190,78, 191,62)
  
  square.draw(canvas)

  # text
  text = Magick::Draw.new
  text.font = font
  text.gravity = Magick::CenterGravity
  text.pointsize = size
  chars = name.chars
  
  case name.length
  when 1
    put_char char: chars[0], gravity: Magick::CenterGravity, draw: text, img: canvas
  when 2
    put_char char: chars[0], gravity: Magick::EastGravity, draw: text, img: canvas
    put_char char: chars[1], gravity: Magick::WestGravity, draw: text, img: canvas
  when 3
    put_char char: chars[0], gravity: Magick::EastGravity, draw: text, img: canvas
    put_char char: chars[1], gravity: Magick::NorthWestGravity, draw: text, img: canvas
    put_char char: chars[2], gravity: Magick::SouthWestGravity, draw: text, img: canvas
  when 4
    put_char char: chars[0], gravity: Magick::NorthEastGravity, draw: text, img: canvas
    put_char char: chars[1], gravity: Magick::SouthEastGravity, draw: text, img: canvas
    put_char char: chars[2], gravity: Magick::NorthWestGravity, draw: text, img: canvas
    put_char char: chars[3], gravity: Magick::SouthWestGravity, draw: text, img: canvas
  end
  
  canvas
end

#update(**params) ⇒ Object



16
17
18
19
# File 'lib/tjstamp.rb', line 16

def update **params
  params.each{ |key, value| send("#{key}=", value)}
  self
end