indie-raster 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/indie-raster.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |s|
3
3
 
4
4
  s.name = "indie-raster"
5
- s.version = "0.0.7"
5
+ s.version = "0.0.8"
6
6
 
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["ShopKeep"]
@@ -63,8 +63,45 @@ class IndieRasterSession
63
63
  return glyphWidth * opt[:text].length
64
64
  end
65
65
 
66
+ def heightOfFont(opt)
67
+ return ith_byte 12, @fonts[opt[:font]] #ASSUMPTION: height < 256
68
+ end
69
+
66
70
  def drawText(opt)
67
71
  data = ascii_to_utf32 opt[:text]
72
+
73
+ if opt[:centeredInRect]
74
+ rect = opt[:centeredInRect]
75
+ rx = rect[0]
76
+ ry = rect[1]
77
+ rw = rect[2]
78
+ rh = rect[3]
79
+ if opt[:fonts]
80
+ fonts = opt[:fonts]
81
+ else
82
+ fonts = [opt[:font]]
83
+ end
84
+
85
+ bestFont = nil
86
+ w = 0
87
+ fonts.each do |font|
88
+ _w = widthOfText :text => opt[:text], :font => font
89
+ if (_w < rw) && (_w > w)
90
+ bestFont = font
91
+ w = _w
92
+ end
93
+ end
94
+
95
+ if bestFont
96
+ h = heightOfFont :font => bestFont
97
+ opt[:x] = rx + ((rw - w) / 2).round
98
+ opt[:y] = ry + ((rh - h) / 2).round
99
+ opt[:font] = bestFont
100
+ else
101
+ return
102
+ end
103
+ end
104
+
68
105
  if opt[:toLeftOf]
69
106
  opt[:x] = opt[:toLeftOf] - self.widthOfText(opt)
70
107
  end
@@ -113,5 +150,34 @@ class IndieRasterSession
113
150
  return out
114
151
  end
115
152
 
153
+ #TODO: refactor
154
+ def parse_pnm(data)
155
+ # assumes standard whitespace
156
+
157
+ if (data[i] == "1" or data[i] == 0x31) or (data[i] == "4" or data[i] == 0x34)
158
+ num_breaks_expected = 2
159
+ regex = /P[14]\n([0-9]+) ([0-9]+)\n/
160
+ else
161
+ num_breaks_expected = 3
162
+ regex = /P[2356]\n([0-9]+) ([0-9]+)\n255\n/
163
+ end
164
+
165
+ num_breaks = 0
166
+ for i in 0..(data.length)
167
+ num_breaks += 1 if (data[i] == "\n" or data[i] == 10)
168
+ break if num_breaks >= num_breaks_expected
169
+ end
170
+
171
+ header = data[0..i]
172
+ m = header.match regex
173
+
174
+ return {
175
+ :w => m[1].to_i,
176
+ :h => m[2].to_i,
177
+ :header => header,
178
+ :body => data[(i + 1)...(data.length)]
179
+ }
180
+ end
181
+
116
182
  end
117
183
 
data/test/expected.pbm CHANGED
Binary file