tjstamp 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/simsun.ttf +0 -0
  3. data/lib/tjstamp.rb +86 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18c2bcd622e784908a09227ef9958af1e4dbe390
4
+ data.tar.gz: 1bf617ee400e26fcd28a42b41479549c8de19ca9
5
+ SHA512:
6
+ metadata.gz: 28eb2ec3e650dfdbd9b433d4bdda9d3cd9a46f6fa175d2d76f88d8571b7225c9f4ff4befc3a5cbadd75bcbcfa7f87cf197dd4c25f49ec9ef73c22f97252fe538
7
+ data.tar.gz: 7748a7b50c5f58dd4bb2b260a0d84b3cafb8a0330f67400ca5a03a57b5eb8bbe0408507731c9725c238927ed1b77cc83ca668f26090f9dcd000309fcaa2e2468
data/lib/simsun.ttf ADDED
Binary file
data/lib/tjstamp.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'RMagick'
2
+
3
+ class TJStamp
4
+ attr_accessor :name, :size, :corner_size, :stroke_width, :font, :color, :background_color
5
+
6
+ def initialize name: '王小明', size: 256, corner_size: 10, stroke_width: 20, font: File.join(__dir__, 'simsun.ttf'), color: 'red', background_color: 'none'
7
+ self.name = name
8
+ self.size = size
9
+ self.corner_size = corner_size
10
+ self.stroke_width = stroke_width
11
+ self.font = font
12
+ self.color = color
13
+ self.background_color = background_color
14
+ end
15
+
16
+ def update **params
17
+ params.each{ |key, value| send("#{key}=", value)}
18
+ end
19
+
20
+ def stamp
21
+ raise 'Length of name must be in (1..4)' unless (1..4) === name.length
22
+ canvas = Magick::Image.new(size, size){ self.background_color = 'none' }
23
+
24
+ # square
25
+ square = Magick::Draw.new
26
+ square.stroke(color)
27
+ square.fill(background_color)
28
+ square.stroke_width(stroke_width)
29
+ corner_width = corner_height = corner_size
30
+ upper_x, left_y = stroke_width / 2, stroke_width / 2
31
+ lower_x, right_y = size - stroke_width / 2, size - stroke_width / 2
32
+ square.roundrectangle(upper_x, left_y, lower_x, right_y, corner_width, corner_height)
33
+ # square.polyline(180,70, 173,78, 190,78, 191,62)
34
+
35
+ square.draw(canvas)
36
+
37
+ # text
38
+ text = Magick::Draw.new
39
+ text.font = font
40
+ text.gravity = Magick::CenterGravity
41
+ text.pointsize = size
42
+ chars = name.chars
43
+
44
+ case name.length
45
+ when 1
46
+ put_char char: chars[0], gravity: Magick::CenterGravity, draw: text, img: canvas
47
+ when 2
48
+ put_char char: chars[0], gravity: Magick::EastGravity, draw: text, img: canvas
49
+ put_char char: chars[1], gravity: Magick::WestGravity, draw: text, img: canvas
50
+ when 3
51
+ put_char char: chars[0], gravity: Magick::EastGravity, draw: text, img: canvas
52
+ put_char char: chars[1], gravity: Magick::NorthWestGravity, draw: text, img: canvas
53
+ put_char char: chars[2], gravity: Magick::SouthWestGravity, draw: text, img: canvas
54
+ when 4
55
+ put_char char: chars[0], gravity: Magick::NorthEastGravity, draw: text, img: canvas
56
+ put_char char: chars[1], gravity: Magick::SouthEastGravity, draw: text, img: canvas
57
+ put_char char: chars[2], gravity: Magick::NorthWestGravity, draw: text, img: canvas
58
+ put_char char: chars[3], gravity: Magick::SouthWestGravity, draw: text, img: canvas
59
+ end
60
+
61
+ canvas
62
+ end
63
+
64
+ private
65
+
66
+ def put_char char: nil, img: nil, draw: nil, gravity: nil, color: color
67
+ char_image = Magick::Image.new(size, size){ self.background_color = 'none' }
68
+ char_unit_size = size/2 - stroke_width
69
+ draw.annotate(char_image, 0, 0, 0, 0, char) { self.fill = color }
70
+
71
+ w, h, x, y = case gravity
72
+ when Magick::NorthWestGravity, Magick::NorthEastGravity, Magick::SouthWestGravity, Magick::SouthEastGravity
73
+ [char_unit_size, char_unit_size, stroke_width, stroke_width]
74
+ when Magick::WestGravity, Magick::EastGravity
75
+ [char_unit_size, size - stroke_width, stroke_width, 0]
76
+ when Magick::SouthGravity, Magick::NorthGravity
77
+ [size - stroke_width, char_unit_size, 0, stroke_width]
78
+ when Magick::CenterGravity
79
+ [size - stroke_width, size - stroke_width, 0, 0]
80
+ else
81
+ raise 'Missing gravity'
82
+ end
83
+
84
+ img.composite!(char_image.resize(w, h), gravity, x, y, Magick::OverCompositeOp)
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tjstamp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Jian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A tool to generate Chinese name stamp image.
14
+ email: tonytonyjan@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/tjstamp.rb
20
+ - lib/simsun.ttf
21
+ homepage: https://github.com/tonytonyjan/tjstamp
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.1.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A tool to generate Chinese name stamp image.
45
+ test_files: []