openscad-text 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f135ca27a17a3fe7cb2cfdf46c1523b03f309ef0
4
- data.tar.gz: 858a02acf97aeed8d7e7cf1fac8f3975e96dbe72
3
+ metadata.gz: f66bfde16ac04d7f2f1e1e9ccecd8333ef7568e7
4
+ data.tar.gz: e4fab6444746173adcfdf10693a04d454a397f9d
5
5
  SHA512:
6
- metadata.gz: 180046888ad588d800db4f4ca2b3b3a0c54f254d674e21cb62fcd128d525790bbd26333183f1ac4d4d3a5cac4deefa50c121cd05868dce109673cb38b1390f5e
7
- data.tar.gz: a8dd610d9c7d493fd95f8872fc9a23ed1dd5e764848b76b95f77efdb4edf424126d66b19375c197d6d36ca9fb48b64d4cc6be9c6d547e502c9ebcca15924c40b
6
+ metadata.gz: 958160f74e8b355520204ad21aaa3d099ff84b28ed9fe14d0719ff1c346719dad0f15c2127917dcbd9d5372dc2def6721162397718a245b06ff930b0e50578d3
7
+ data.tar.gz: 4b6480ad3172e6cd4698b30adc2ff5219863c76f8713aee3122dc9f21ffde1b3e8081947f014ee0f2a05dbb77eb476370be7ff6651aff978771ce1a16d62c3b3
@@ -3,3 +3,7 @@ require 'RMagick'
3
3
 
4
4
  require_relative 'openscad-text/text'
5
5
  require_relative 'openscad-text/image'
6
+
7
+ font = Text.available_fonts.find { |f| f =~ /arial/ }
8
+ text = Text.new("Arial!", font)
9
+ puts text.to_openscad
@@ -34,37 +34,35 @@ class Text
34
34
  # checks if a point is already in the points ary
35
35
  # aka if it has already been used and also if a point is surrounded
36
36
  # by too many other black points
37
- def point_invalid?(x,y)
37
+ def point_invalid?(point)
38
38
  # white points are always invalid
39
- return true if @matrix[x,y] == :white
39
+ return true if @matrix[*point] == :white
40
40
 
41
41
  # point already taken
42
- return true if @points.any? { |point| point == [x,y] }
42
+ return true if @points.any? { |p| p == point }
43
43
 
44
44
  # if not already taken border points are always valid
45
- return false if x == 0 or y == 0 or x == @x or y == @y
45
+ return false if point[0] == 0 or point[1] == 0 or point[0] == @x or point[1] == @y
46
46
 
47
47
  # if all non-diagonal neighbours are black, the point must be invalid
48
- neighbours = find_direct_neighbours(x,y)
48
+ neighbours = find_direct_neighbours(point)
49
49
  return true if neighbours.count == 4 and neighbours.all? { |neighbour| @matrix[*neighbour] == :black }
50
50
 
51
51
  # point is valid
52
52
  false
53
53
  end
54
54
 
55
- def find_direct_neighbours(x,y)
56
- x_min = [x-1, 0].max
57
- x_max = [x+1, @x].min
58
-
59
- y_min = [y-1, 0].max
60
- y_max = [y+1, @y].min
55
+ def find_direct_neighbours(point)
56
+ # vecs to the 4 non-daigonal points
57
+ vecs = [
58
+ Vector[-1, 0],
59
+ Vector[ 1, 0],
60
+ Vector[ 0, 1],
61
+ Vector[ 0,-1]
62
+ ]
61
63
 
62
- [
63
- [x_min, y ],
64
- [x_max, y ],
65
- [x , y_max],
66
- [x , y_min]
67
- ].uniq - [[x,y]]
64
+ neighbours = vecs.map { |vec| point + vec }
65
+ neighbours.delete_if { |point| point.any? { |i| i < 0 } }
68
66
  end
69
67
 
70
68
  # finds next point in chain from current point
@@ -82,11 +80,11 @@ class Text
82
80
  ]
83
81
 
84
82
  # color of the last pixel
85
- last_color = @matrix[*(Vector[*current]+vecs.last).to_a]
83
+ last_color = @matrix[*current+vecs.last]
86
84
 
87
85
  # turn the vector and find each which touches a white pixel
88
86
  touchy_vecs = vecs.map.with_index do |vec,i|
89
- current_color = @matrix[*(Vector[*current]+vec).to_a]
87
+ current_color = @matrix[*current+vec]
90
88
  color_changed = current_color != last_color
91
89
  last_color = current_color
92
90
 
@@ -107,10 +105,10 @@ class Text
107
105
  touchy_vecs.uniq!
108
106
 
109
107
  # possible next points
110
- touchy_points = touchy_vecs.map { |vec| (Vector[*current] + vec).to_a }
108
+ touchy_points = touchy_vecs.map { |vec| current + vec }
111
109
 
112
110
  # remove the invalid ones
113
- touchy_points.delete_if { |point| point_invalid? *point }
111
+ touchy_points.delete_if { |point| point_invalid? point }
114
112
 
115
113
  # return the next point or nil
116
114
  touchy_points[0]
@@ -118,16 +116,13 @@ class Text
118
116
 
119
117
  # starting with point(x,y), try to create a path (or chain)
120
118
  # until the starting point is reached again
121
- def create_pixel_chain(x,y)
119
+ def create_pixel_chain(current_point)
122
120
  # can't create a chain if the point is invalid
123
- return if point_invalid?(x,y)
121
+ return if point_invalid?(current_point)
124
122
 
125
123
  # create a new ary in the paths ary
126
124
  @paths << []
127
125
 
128
- # setup state
129
- current_point = [x,y]
130
-
131
126
  while current_point
132
127
  # add the point to the points array
133
128
  @points << current_point
@@ -142,10 +137,12 @@ class Text
142
137
 
143
138
  # aligns the text to the bottom-left corner of the first quadrant
144
139
  def align_points
145
- x_min = @points.map { |x,_| x }.min
146
- y_min = @points.map { |_,y| y }.min
140
+ x_min = @points.map { |p| p[0] }.min
141
+ y_min = @points.map { |p| p[1] }.min
142
+
143
+ vec = Vector[x_min,y_min]
147
144
 
148
- @points.map! { |x,y| [x-x_min, y-y_min] }
145
+ @points.map! { |p| p - vec }
149
146
  end
150
147
 
151
148
  public
@@ -159,14 +156,14 @@ class Text
159
156
  # go through each point aka pixel to make sure it gets used once
160
157
  # and try to retrace the letters
161
158
  @matrix.each_with_index do |_,x,y|
162
- create_pixel_chain(x,y)
159
+ create_pixel_chain(Vector[x,y])
163
160
  end
164
161
 
165
162
  # align them!
166
163
  align_points
167
164
 
168
165
  # finished woop woop
169
- "polygon(points=#{@points.to_s}, paths=#{@paths.to_s});"
166
+ "polygon(points=#{@points.map(&:to_a).to_s}, paths=#{@paths.to_s});"
170
167
  end
171
168
 
172
169
  =begin !!just for debugging!!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openscad-text
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Lackner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick