Class: Magick::Draw

Inherits:
Object
  • Object
show all
Defined in:
lib/gruff/base.rb,
lib/CocoaMagick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDraw

Returns a new instance of Draw.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/CocoaMagick.rb', line 61

def initialize
  @fill = "white" 
  @pointsize = 18
  @stroke_opacity = 1
  @fill_opacity = 1
  @stroke_width = 1
  @stroke_color = OSX::NSColor.colorWithName("grey")
  @fill_color = OSX::NSColor.colorWithName("grey")
  @stack = []
  OSX::NSBezierPath.setDefaultLineJoinStyle(OSX::NSRoundLineJoinStyle)
  OSX::NSBezierPath.setDefaultLineWidth(1)
end

Instance Attribute Details

#dasharrayObject

Returns the value of attribute dasharray.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def dasharray
  @dasharray
end

#fill(color) ⇒ Object

Returns the value of attribute fill.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def fill
  @fill
end

#fill_colorObject

Returns the value of attribute fill_color.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def fill_color
  @fill_color
end

#fontObject

Returns the value of attribute font.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def font
  @font
end

#font_weightObject

Returns the value of attribute font_weight.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def font_weight
  @font_weight
end

#gravityObject

Returns the value of attribute gravity.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def gravity
  @gravity
end

#pointsizeObject

Returns the value of attribute pointsize.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def pointsize
  @pointsize
end

#stroke(color) ⇒ Object

Returns the value of attribute stroke.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def stroke
  @stroke
end

#stroke_color(color) ⇒ Object

Returns the value of attribute stroke_color.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def stroke_color
  @stroke_color
end

#stroke_width(a) ⇒ Object

Returns the value of attribute stroke_width.



53
54
55
# File 'lib/CocoaMagick.rb', line 53

def stroke_width
  @stroke_width
end

Instance Method Details

#annotate(image, width, height, x, y, text) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/CocoaMagick.rb', line 117

def annotate(image,width,height,x,y,text)
  log "annotate #{image} #{width} #{height} #{x} #{y} #{text}" 
  attributes = string_attributes()      
  string = OSX::NSString.stringWithString(text)
  size = string.sizeWithAttributes(attributes) 
  if @gravity == EastGravity or @gravity == SouthEastGravity or @gravity == NorthEastGravity
    x = x + width - size.width/2
  elsif @gravity == CenterGravity or @gravity == NorthGravity or @gravity == SouthGravity
    x = x + width/2 - size.width/2 
  end
  if @gravity == NorthGravity or @gravity == NorthEastGravity or @gravity == NorthWestGravity
    y = y + height - size.height
  elsif @gravity == CenterGravity or @gravity == EastGravity or @gravity == WestGravity
    y = y + height/2 - size.height/2
  else
    y = y - size.height/2
  end
  string.drawAtPoint_withAttributes([x,image.height-y-size.height], attributes)
  self
end

#annotate_scaled(img, width, height, x, y, text, scale) ⇒ Object

Additional method since Draw.scale doesn’t affect annotations.



901
902
903
904
905
906
907
908
909
# File 'lib/gruff/base.rb', line 901

def annotate_scaled(img, width, height, x, y, text, scale)
  scaled_width = (width * scale) >= 1 ? (width * scale) : 1
  scaled_height = (height * scale) >= 1 ? (height * scale) : 1
  
  self.annotate( img, 
                  scaled_width, scaled_height,
                  x * scale, y * scale,
                  text)
end

#circle(cx, cy, px, py) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/CocoaMagick.rb', line 188

def circle(cx,cy,px,py)
  log "circle #{cx} #{cy} #{px} #{py}" 
  r = Math::sqrt((cx-px)*(cx-px) + (cy-py)*(cy-py))
  path = OSX::NSBezierPath.bezierPathWithOvalInRect(
    [(cx-r)*@scalex,$height-(cy+r)*@scaley,2*r*@scalex,2*r*@scaley])
  stroke_and_fill(path)
  self
end

#draw(a) ⇒ Object



234
235
236
237
# File 'lib/CocoaMagick.rb', line 234

def draw(a)
  log "draw #{a}" 
  self
end

#ellipse(x, y, w, h, as, ae) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/CocoaMagick.rb', line 196

def ellipse(x, y, w, h, as, ae)
  log "ellipse #{x} #{y} #{w} #{h} #{as} #{ae}" 
  push
  stroke_width(1)
  @fill_color = @stroke_color   
  path = OSX::NSBezierPath.bezierPath
  center = [x*@scalex,$height-y*@scaley]
  path.moveToPoint(center)
  path.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle(
    center,2*w*@scalex,-ae,-as)
  path.closePath
  stroke_and_fill(path)
  pop
  self
end

#fill_opacity(opacity) ⇒ Object



178
179
180
181
# File 'lib/CocoaMagick.rb', line 178

def fill_opacity(opacity)
  @fill_opacity = opacity
  self
end

#get_type_metrics(a, text) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/CocoaMagick.rb', line 109

def get_type_metrics(a,text)
  log "get_type_metrics #{a} #{text.inspect}" 
  attributes = string_attributes()
  metrics = TypeMetric.new
  size = OSX::NSString.stringWithString(text).sizeWithAttributes(attributes)
  metrics.width = size.width
  metrics
end

#line(a, b, c, d) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/CocoaMagick.rb', line 159

def line(a,b,c,d)
  log "line #{a} #{b} #{c} #{d}" 
  @stroke_color.set
  path = OSX::NSBezierPath.bezierPath
  path.setLineDash_count_phase(@dasharray, @dashcount, 0) if @dasharray
  path.moveToPoint([a*@scalex,$height-b*@scaley])
  path.lineToPoint([c*@scalex,$height-d*@scaley])
  path.stroke
  self
end

#polygon(*args) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/CocoaMagick.rb', line 222

def polygon(*args)
  path = OSX::NSBezierPath.bezierPath
  path.moveToPoint([args[0]*@scalex,$height-args[1]*@scaley])
  i = 2
  while(i < args.length) 
    path.lineToPoint([args[i]*@scalex,$height-args[i+1]*@scaley])
    i = i + 2
  end
  path.closePath
  stroke_and_fill(path)
  self
end

#polyline(*args) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/CocoaMagick.rb', line 211

def polyline(*args)
  path = OSX::NSBezierPath.bezierPath
  path.moveToPoint([args[0]*@scalex,$height-args[1]*@scaley])
  i = 2
  while(i < args.length) 
    path.lineToPoint([args[i]*@scalex,$height-args[i+1]*@scaley])
    i = i + 2
  end
  stroke_and_fill(path)
  self
end

#popObject



84
85
86
87
88
89
# File 'lib/CocoaMagick.rb', line 84

def pop
  log "Draw.pop"  
  hash = @stack.pop
  hash.keys.each {|key| self.instance_variable_set("@"+key.to_s, hash[key])}
  self
end

#pushObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/CocoaMagick.rb', line 73

def push
  log "Draw.push" 
  @stack.push({ # add more if needed
    :fill_color => @fill_color, 
    :stroke_color => @stroke_color, 
    :dasharray => @dasharray, 
    :dashcount => @dashcount,
    :stroke_width => @stroke_width
  })
  self
end

#rectangle(a, b, c, d) ⇒ Object



147
148
149
150
151
152
# File 'lib/CocoaMagick.rb', line 147

def rectangle(a,b,c,d)
  log "rectangle #{a} #{b} #{c} #{d}" 
  @fill_color.set
  OSX::NSRectFill([a*@scalex,$height-d*@scalex,(c-a)*@scalex,(d-b)*@scaley])
  self
end

#scale(x, y) ⇒ Object



90
91
92
93
94
95
# File 'lib/CocoaMagick.rb', line 90

def scale(x,y)
  log "scale #{x} #{y}" 
  @scalex = x
  @scaley = y
  self
end

#string_attributesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/CocoaMagick.rb', line 96

def string_attributes
  attributes = OSX::NSMutableDictionary.alloc.initWithCapacity_(10)
  attributes.setObject_forKey(OSX::NSColor.colorWithName(@fill), 
    OSX.NSForegroundColorAttributeName)
  if @font_weight == BoldWeight
    attributes.setObject_forKey(OSX::NSFont.boldSystemFontOfSize(@pointsize), 
      OSX.NSFontAttributeName) 
  else
    attributes.setObject_forKey(OSX::NSFont.systemFontOfSize(@pointsize), 
      OSX.NSFontAttributeName) 
  end     
  attributes
end

#stroke_and_fill(path) ⇒ Object



55
56
57
58
59
60
# File 'lib/CocoaMagick.rb', line 55

def stroke_and_fill(path)
  @stroke_color.colorWithAlphaComponent(@stroke_opacity).set
  path.stroke
  @fill_color.colorWithAlphaComponent(@fill_opacity).set
  path.fill
end

#stroke_dasharray(a, b) ⇒ Object



182
183
184
185
186
187
# File 'lib/CocoaMagick.rb', line 182

def stroke_dasharray(a, b)
  log "stroke_dasharray #{a} #{b}" 
  @dashcount = 2
  @dasharray = [a,b].pack('f2')
  self
end

#stroke_opacity(opacity) ⇒ Object



169
170
171
172
173
# File 'lib/CocoaMagick.rb', line 169

def stroke_opacity(opacity)
  log "stroke_opacity #{opacity}" 
  @stroke_opacity = opacity
  self
end