Class: AastraXmlApi::PhoneGDImage

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

Instance Method Summary collapse

Constructor Details

#initializePhoneGDImage

Creates a new GD image of size 144x40 (maximum allowed on phone). Also creates black and white colors to be used and sets default font path based on whether this is running under rails or a specific operating system.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 101

def initialize
  # create image
  @img = GD::Image.new(144, 40)

  # define black and white
  @white = @img.colorAllocate(255, 255, 255)
  @black = @img.colorAllocate(0, 0, 0)

  # black and white only so disable anti-aliasing
  @black *= -1
  @white *= -1

  # define a default font path
  if defined?(RAILS_ROOT) then
    @fontpath = "#{RAILS_ROOT}/fonts"
  else
    os = RUBY_PLATFORM.downcase
    @fontpath = "C:\\Windows\\Fonts" if not os.scan(/win/).nil?
    @fontpath = "../fonts" if not os.scan(/linux$/).nil?
    @fontpath = "../fonts" if not os.scan(/darwin\d+\.\d+$/).nil?
  end
  ENV['GDFONTPATH'] = @fontpath
end

Instance Method Details

#drawtext(size, x, y, text, colorIndex) ⇒ Object

Draw text on the image.



138
139
140
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 138

def drawtext(size, x, y, text, colorIndex)
  @img.string(size, x, y, text, getColor(colorIndex))
end

#drawttftext(size, angle, x, y, text, colorIndex, font) ⇒ Object

Draw text on the image using a specific true type font. See GD documentation for parameters.



133
134
135
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 133

def drawttftext(size, angle, x, y, text, colorIndex, font)
  @img.stringTTF(getColor(colorIndex), font, size, angle, x, y, text)
end

#ellipse(cx, cy, width, height, colorIndex, filled = false) ⇒ Object

Draw an ellipse on the image. Ellipse will be unfilled by default.



162
163
164
165
166
167
168
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 162

def ellipse(cx, cy, width, height, colorIndex, filled=false)
  if filled then
    @img.filledEllipse(cx, cy, width, height, 0, 360, getColor(colorIndex))
  else
    @img.ellipse(cx, cy, width, height, 0, 360, getColor(colorIndex))
  end
end

#getColor(index) ⇒ Object

Get the GD color element based on an index.

0

white

1

black



182
183
184
185
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 182

def getColor(index)
  return @white if index == 0
  return @black
end

#getGDImageObject

Get the GD image created.



148
149
150
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 148

def getGDImage
  return @img
end

#line(x1, y1, x2, y2, colorIndex, dashed = false) ⇒ Object

Draw a line on the image. Line will be solid by default.



171
172
173
174
175
176
177
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 171

def line(x1, y1, x2, y2, colorIndex, dashed=false)
  if dashed then
    @img.dashedLine(x1, y2, x2, y2, getColor(colorIndex))
  else
    @img.line(x1, y2, x2, y2, getColor(colorIndex))
  end
end

#rectangle(x1, y1, x2, y2, colorIndex, filled = false) ⇒ Object

Draw a rectangle on the image. Rectangle will be unfilled by default.



153
154
155
156
157
158
159
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 153

def rectangle(x1, y1, x2, y2, colorIndex, filled=false)
  if filled then
    @img.filledRectangle(x1, y1, x2, y2, getColor(colorIndex))
  else
    @img.rectangle(x1, y1, x2, y2, getColor(colorIndex))
  end
end

#setFontPath(fontpath) ⇒ Object

Set font path for GD.



126
127
128
129
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 126

def setFontPath(fontpath)
  @fontpath = fontpath
  ENV['GDFONTPATH'] = @fontpath
end

#setGDImage(image) ⇒ Object

Set the image from an externally created GD image.



143
144
145
# File 'lib/aastra_xml_api/phone_gd_image.rb', line 143

def setGDImage(image)
  @img = image
end