aastra_xml_api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +341 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/aastra_xml_api.gemspec +22 -0
  8. data/lib/aastra_xml_api/AastraIPPhone.rb +203 -0
  9. data/lib/aastra_xml_api/AastraIPPhoneConfiguration.rb +55 -0
  10. data/lib/aastra_xml_api/AastraIPPhoneConfigurationEntry.rb +41 -0
  11. data/lib/aastra_xml_api/AastraIPPhoneDirectory.rb +119 -0
  12. data/lib/aastra_xml_api/AastraIPPhoneDirectoryEntry.rb +32 -0
  13. data/lib/aastra_xml_api/AastraIPPhoneExecute.rb +58 -0
  14. data/lib/aastra_xml_api/AastraIPPhoneExecuteEntry.rb +30 -0
  15. data/lib/aastra_xml_api/AastraIPPhoneFormattedTextScreen.rb +117 -0
  16. data/lib/aastra_xml_api/AastraIPPhoneFormattedTextScreenEntry.rb +48 -0
  17. data/lib/aastra_xml_api/AastraIPPhoneGDImage.rb +185 -0
  18. data/lib/aastra_xml_api/AastraIPPhoneIconEntry.rb +27 -0
  19. data/lib/aastra_xml_api/AastraIPPhoneImageMenu.rb +143 -0
  20. data/lib/aastra_xml_api/AastraIPPhoneImageMenuEntry.rb +25 -0
  21. data/lib/aastra_xml_api/AastraIPPhoneImageScreen.rb +139 -0
  22. data/lib/aastra_xml_api/AastraIPPhoneInputScreen.rb +249 -0
  23. data/lib/aastra_xml_api/AastraIPPhoneInputScreenEntry.rb +84 -0
  24. data/lib/aastra_xml_api/AastraIPPhoneSoftkeyEntry.rb +37 -0
  25. data/lib/aastra_xml_api/AastraIPPhoneStatus.rb +66 -0
  26. data/lib/aastra_xml_api/AastraIPPhoneStatusEntry.rb +56 -0
  27. data/lib/aastra_xml_api/AastraIPPhoneTextMenu.rb +145 -0
  28. data/lib/aastra_xml_api/AastraIPPhoneTextMenuEntry.rb +52 -0
  29. data/lib/aastra_xml_api/AastraIPPhoneTextScreen.rb +93 -0
  30. data/lib/aastra_xml_api/arraynatsort.rb +63 -0
  31. data/lib/aastra_xml_api/version.rb +3 -0
  32. data/lib/aastra_xml_api.rb +9 -0
  33. metadata +89 -0
@@ -0,0 +1,55 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneConfiguration
3
+ # Copyright Aastra Telecom 8007
4
+ #
5
+ # Ruby adaptation by Carlton O'Riley
6
+ #
7
+ # AastraIPPhoneConfiguration object.
8
+ #
9
+ # Public methods
10
+ #
11
+ # Inherited from AastraIPPhone
12
+ # setBeep to enable a notification beep with the object (optional)
13
+ #
14
+ # Specific to the object
15
+ # addEntry(parameter,value) to add a configuration change.
16
+ # setTriggerDestroyOnExit to set the triggerDestroyOnExit tag to
17
+ # "yes" (optional)
18
+ #
19
+ # Example
20
+ # require 'AastraIPPhoneConfiguration.rb'
21
+ # configuration = AastraIPPhoneConfiguration.new
22
+ # configuration.addEntry('softkey1 label','Test')
23
+ # configuration.addEntry('softkey1 type','xml')
24
+ # configuration.setTriggerDestroyOnExit
25
+ # configuration.setBeep
26
+ # aastra_output configuration
27
+ #
28
+ ################################################################################
29
+
30
+ class AastraIPPhoneConfiguration < AastraIPPhone
31
+
32
+ # Adds a parameter and value entry to the list.
33
+ def addEntry(parameter, value)
34
+ @entries += [AastraIPPhoneConfigurationEntry.new(parameter, value)]
35
+ end
36
+
37
+ # When set, the previous user interface XML object is destroyed
38
+ # if its destroyOnExit tag is also set to yes.
39
+ def setTriggerDestroyOnExit
40
+ @triggerDestroyOnExit = "yes"
41
+ end
42
+
43
+ # Create XML text output.
44
+ def render
45
+ out = "<AastraIPPhoneConfiguration"
46
+ out += " Beep=\"yes\"" if @beep == "yes"
47
+ out += " triggerDestroyOnExit=\"yes\"" if @triggerDestroyOnExit == "yes"
48
+ out += ">\n"
49
+ @entries.each do |entry|
50
+ out += entry.render
51
+ end
52
+ out += "</AastraIPPhoneConfiguration>\n"
53
+ return out
54
+ end
55
+ end
@@ -0,0 +1,41 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneConfigurationEntry
3
+ # Firmware 2.0 or better
4
+ # Copyright Aastra Telecom 2008
5
+ #
6
+ # Ruby adaptation by Carlton O'Riley
7
+ #
8
+ # Internal class for AastraIPPhoneConfiguration object.
9
+ ################################################################################
10
+ class AastraIPPhoneConfigurationEntry < AastraIPPhone
11
+ @parameter
12
+ @value
13
+
14
+ # Create a new parameter/value pair. This overrides the
15
+ # initialize method in AastraIPPhone.
16
+ def initialize(parameter, value)
17
+ @parameter = parameter
18
+ @value = value
19
+ end
20
+
21
+ # Set the parameter for this entry.
22
+ def setParameter(parameter)
23
+ @parameter = parameter
24
+ end
25
+
26
+ # Set the value for this entry.
27
+ def setValue(value)
28
+ @value = value
29
+ end
30
+
31
+ # Create XML text output for this entry.
32
+ def render
33
+ parameter = escape(@parameter)
34
+ value = escape(@value)
35
+ xml = "<ConfigurationItem>\n"
36
+ xml += "<Parameter>#{parameter}</Parameter>\n"
37
+ xml += "<Value>#{value}</Value>\n"
38
+ xml += "</ConfigurationItem>\n"
39
+ return xml
40
+ end
41
+ end
@@ -0,0 +1,119 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneDirectory
3
+ # Copyright Aastra Telecom 2008
4
+ #
5
+ # Ruby adaptation by Carlton O'Riley
6
+ #
7
+ # AastraIPPhoneDirectory object.
8
+ #
9
+ # Public methods
10
+ #
11
+ # Inherited from AastraIPPhone
12
+ # setTitle(Title) to setup the title of an object (optional)
13
+ # setTitleWrap to set the title to be wrapped on 2 lines (optional)
14
+ # setDestroyOnExit to set DestroyonExit parameter to 'yes', 'no' by default (optional)
15
+ # setCancelAction(uri) to set the cancel parameter with the URI to be called on Cancel (optional)
16
+ # setBeep to enable a notification beep with the object (optional)
17
+ # setLockIn to set the Lock-in tag to 'yes' (optional)
18
+ # setTimeout(timeout) to define a specific timeout for the XML object (optional)
19
+ # addSoftkey(index,label,uri,icon_index) to add custom softkeys to the object (optional)
20
+ # addIcon(index,icon) to add custom icons to the object (optional)
21
+ # setRefresh(timeout,URL) to add Refresh parameters to the object (optional)
22
+ #
23
+ # Specific to the object
24
+ # setNext(next) to set URI of the next page (optional)
25
+ # setPrevious(previous) to set URI of the previous page (optional)
26
+ # addEntry(name,phone) to add an element in the list to be displayed, at least one is needed.
27
+ # natsortbyname to order the list
28
+ #
29
+ # Example
30
+ # require 'AastraIPPhoneDirectory.rb'
31
+ # directory = AastraIPPhoneDirectory.new
32
+ # directory.setTitle('Title')
33
+ # directory.setNext('http://myserver.com/script.php?page=2')
34
+ # directory.setPrevious('http://myserver.com/script.php?page=0')
35
+ # directory.setDestroyOnExit
36
+ # directory.addEntry('John Doe', '200')
37
+ # directory.addEntry('Jane Doe', '201')
38
+ # directory.natsortByName
39
+ # directory.addSoftkey('1', 'Label', 'http://myserver.com/script.php?action=1')
40
+ # directory.addSoftkey('6', 'Exit', 'SoftKey:Exit')
41
+ # aastra_output directory
42
+ #
43
+ ################################################################################
44
+
45
+ class AastraIPPhoneDirectory < AastraIPPhone
46
+ @next
47
+ @previous
48
+
49
+ # Set the URI to load the next page of the directory.
50
+ def setNext(nextval)
51
+ @next = nextval
52
+ end
53
+
54
+ # Set the URI to load the previous page of the directory.
55
+ def setPrevious(previous)
56
+ @previous = previous
57
+ end
58
+
59
+ # Add directory entry with a name to be displayed and a telephone
60
+ # number to dial.
61
+ def addEntry(name, telephone)
62
+ @entries += [AastraIPPhoneDirectoryEntry.new(name, telephone)]
63
+ end
64
+
65
+ # Sort array of names using natural sort order. i.e. Bob2 comes
66
+ # before Bob10.
67
+ def natsortByName
68
+ tmparray = []
69
+ linklist = {}
70
+ for i in 0..@entries.size-1
71
+ tmparray += [@entries[i].getName]
72
+ linklist[@entries[i].getName] = i
73
+ end
74
+ tmparray.natsort!
75
+ newentries = []
76
+ tmparray.each do |name|
77
+ newentries += [@entries[linklist[name]]]
78
+ end
79
+ @entries = newentries
80
+ end
81
+
82
+ # Create XML text output.
83
+ def render
84
+ out = "<AastraIPPhoneDirectory"
85
+ if not @previous.nil? then
86
+ previous = escape(@previous)
87
+ out += " previous=\"#{previous}\""
88
+ end
89
+ if not @next.nil? then
90
+ nextval = escape(@next)
91
+ out += " next=\"#{nextval}\""
92
+ end
93
+ out += " destroyOnExit=\"yes\"" if @destroyOnExit == "yes"
94
+ if not @cancelAction.nil? then
95
+ cancelAction = escape(@cancelAction)
96
+ out += " cancelAction=\"#{cancelAction}\""
97
+ end
98
+ out += " Beep=\"yes\"" if @beep == "yes"
99
+ out += " LockIn=\"yes\"" if @lockin == "yes"
100
+ out += " Timeout=\"#{@timeout}\"" if @timeout != 0
101
+ out += ">\n"
102
+ if not @title.nil? then
103
+ title = escape(title)
104
+ out += "<Title"
105
+ out += " wrap=\"yes\"" if @title_wrap == "yes"
106
+ out += ">#{title}</Title>\n"
107
+ end
108
+ index = 0
109
+ @entries.each do |entry|
110
+ out += entry.render if index < 30
111
+ index += 1
112
+ end
113
+ @softkeys.each do |softkey|
114
+ out += softkey.render
115
+ end
116
+ out += "</AastraIPPhoneDirectory>\n"
117
+ return out
118
+ end
119
+ end
@@ -0,0 +1,32 @@
1
+ ########################################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneDirectoryEntry
3
+ # Copyright Aastra Telecom 2008
4
+ #
5
+ # Ruby adaptation by Carlton O'Riley
6
+ #
7
+ # Internal class for AastraIPPhoneDirectory object.
8
+ ########################################################################################################
9
+
10
+ class AastraIPPhoneDirectoryEntry < AastraIPPhone
11
+ @name
12
+ @telephone
13
+
14
+ # Create new name and number entry. Overrides the initialize method
15
+ # of AastraIPPhone.
16
+ def initialize(name, telephone)
17
+ @name = name
18
+ @telephone = telephone
19
+ end
20
+
21
+ # Get the name associated with this entry.
22
+ def getName
23
+ @name
24
+ end
25
+
26
+ # Create XML text output for this entry.
27
+ def render
28
+ name = escape(@name)
29
+ telephone = escape(@telephone)
30
+ return "<MenuItem>\n<Prompt>#{name}</Prompt>\n<URI>#{telephone}</URI>\n</MenuItem>\n"
31
+ end
32
+ end
@@ -0,0 +1,58 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneExecute
3
+ # Copyright Aastra Telecom 2008
4
+ #
5
+ # Ruby adaptation by Carlton O'Riley
6
+ #
7
+ # AastraIPPhoneExecute object.
8
+ #
9
+ # Public methods
10
+ #
11
+ # Inherited from AastraIPPhone
12
+ # setBeep to enable a notification beep with the object (optional)
13
+ #
14
+ # Specific to the object
15
+ # setTriggerDestroyOnExit to set the triggerDestroyOnExit tag to "yes" (optional)
16
+ # addEntry(url,interruptCall) to add an action to be executed.
17
+ #
18
+ # Example
19
+ # require 'AastraIPPhoneExecute.class.php'
20
+ # execute = AastraIPPhoneExecute.new
21
+ # execute.addEntry('http://myserver.com/script.php?choice=2')
22
+ # execute.addEntry('Command: Reset')
23
+ # aastra_output execute
24
+ #
25
+ ################################################################################
26
+
27
+ class AastraIPPhoneExecute < AastraIPPhone
28
+ @defaultIndex
29
+ @triggerDestroyOnExit
30
+
31
+ # Add a url (action to be performed). If interruptCall is not nil, then
32
+ # then if a dial action is given, the current call will be put on hold.
33
+ # Default behavior is to not allow a current active call to be
34
+ # interrupted.
35
+ def addEntry(url, interruptCall=nil)
36
+ @entries += [AastraIPPhoneExecuteEntry.new(url, interruptCall)]
37
+ end
38
+
39
+ # When set, the previous user interface XML object is destroyed
40
+ # if its destroyOnExit tag is also set to yes.
41
+ def setTriggerDestroyOnExit
42
+ @triggerDestroyOnExit = "yes"
43
+ end
44
+
45
+ # Create XML text output.
46
+ def render
47
+ title = escape(@title)
48
+ out = "<AastraIPPhoneExecute"
49
+ out += " Beep=\"yes\"" if @beep == "yes"
50
+ out += " triggerDestroyOnExit=\"yes\"" if @triggerDestroyOnExit == "yes"
51
+ out += ">\n"
52
+ @entries.each do |entry|
53
+ out += entry.render
54
+ end
55
+ out += "</AastraIPPhoneExecute>\n"
56
+ return out
57
+ end
58
+ end
@@ -0,0 +1,30 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneExecuteEntry
3
+ # Firmware 1.4.1 or better
4
+ # Copyright Aastra Telecom 2008
5
+ #
6
+ # Ruby adaptation by Carlton O'Riley
7
+ #
8
+ # Internal class for AastraIPPhoneExecute object.
9
+ ################################################################################
10
+
11
+ class AastraIPPhoneExecuteEntry < AastraIPPhone
12
+ @url
13
+ @interruptCall
14
+
15
+ # Create a new action to be performed. if interruptCall is not nil then
16
+ # a currently active call can be interrupted by this action.
17
+ def initialize(url, interruptCall)
18
+ @url = url
19
+ @interruptCall = interruptCall
20
+ end
21
+
22
+ # Create XML text output for this entry.
23
+ def render
24
+ url = escape(@url)
25
+ xml = "<ExecuteItem URI=\"#{url}\""
26
+ xml += " interruptCall=\"no\"" if @interruptCall == "no"
27
+ xml += "/>\n"
28
+ return xml
29
+ end
30
+ end
@@ -0,0 +1,117 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPFormattedPhoneTextScreen
3
+ # Copyright Aastra Telecom 2008
4
+ #
5
+ # AastraIPPhoneFormattedTextScreen object.
6
+ #
7
+ # Public methods
8
+ #
9
+ # Inherited from AastraIPPhone
10
+ # setDestroyOnExit to set DestroyonExit parameter to 'yes', 'no' by default (optional)
11
+ # setCancelAction(uri) to set the cancel parameter with the URI to be called on Cancel (optional)
12
+ # setBeep to enable a notification beep with the object (optional)
13
+ # setLockIn to set the Lock-in tag to 'yes' (optional)
14
+ # setAllowAnswer to set the allowAnswer tag to 'yes' (optional)
15
+ # setTimeout(timeout) to define a specific timeout for the XML object (optional)
16
+ # addSoftkey(index, label, uri) to add custom softkeys to the object (optional)
17
+ # addIcon(index,icon) to add custom icons to the object (optional)
18
+ # setRefresh(timeout,URL) to add Refresh parameters to the object (optional)
19
+ #
20
+ # Specific to the object
21
+ # addLine(text,size,align) to add a formatted line
22
+ # setScrollStart(height) to define the beginning of the scrolling section and its height
23
+ # setScrollEnd to define the end of the scrolling section
24
+ # setAllowDTMF to allow DTMF passthrough on the object
25
+ # setDoneAction(uri) to set the URI to be called when the user selects the default "Done" key (optional)
26
+ #
27
+ # Example
28
+ # require 'AastraIPPhoneFormattedTextScreen.rb'
29
+ # ftext = AastraIPPhoneFormattedTextScreen.new
30
+ # ftext.setDestroyOnExit
31
+ # ftext.addLine('Formatted Screen', 'double', 'center')
32
+ # ftext.setScrollStart('2')
33
+ # ftext.addLine('Scrolled text1')
34
+ # ftext.addLine('Scrolled text2')
35
+ # ftext.addLine('Scrolled text3')
36
+ # ftext.addLine('Scrolled text4')
37
+ # ftext.addLine('Scrolled text5')
38
+ # ftext.setScrollEnd
39
+ # ftext.addLine('Footer',NULL,'center')
40
+ # ftext.addSoftkey('1', 'Label', 'http://myserver.com/script.php?action=1','1')
41
+ # ftext.addSoftkey('6', 'Exit', 'SoftKey:Exit')
42
+ # ftext.addIcon('1', 'Icon:Envelope')
43
+ # aastra_output ftext
44
+ #
45
+ ################################################################################
46
+
47
+ class AastraIPPhoneFormattedTextScreen < AastraIPPhone
48
+ @doneAction
49
+ @allowDTMF
50
+
51
+ # Add a line of formatted text. size can only be 'normal' (default)
52
+ # or 'double'. align can be one of 'left' (default), 'center',
53
+ # or 'right'.
54
+ def addLine(text, size=nil, align=nil)
55
+ @entries += [AastraIPPhoneFormattedTextScreenEntry.new(text, size, align, 'normal')]
56
+ end
57
+
58
+ # Starts the beginning of a scrolling section on the display. If height
59
+ # is not given, then all available space is used to display the scrolling
60
+ # section. Otherwise, height cannot be bigger than 2.
61
+ def setScrollStart(height=nil)
62
+ @entries += [AastraIPPhoneFormattedTextScreenEntry.new(nil, height, nil, 'scrollstart')]
63
+ end
64
+
65
+ # Sets the end of a scrolling section on the display.
66
+ def setScrollEnd
67
+ @entries += [AastraIPPhoneFormattedTextScreenEntry.new(nil, nil, nil, 'scrollend')]
68
+ end
69
+
70
+ # Defines URI to call when the user selects the 'Done' softkey.
71
+ def setDoneAction(uri)
72
+ @doneAction = uri
73
+ end
74
+
75
+ # Allows keypad strokes to generate DTMF when a call is in progress
76
+ # while this object is displayed.
77
+ def setAllowDTMF
78
+ @allowDTMF = "yes"
79
+ end
80
+
81
+ # Create XML text output.
82
+ def render
83
+ out = "<AastraIPPhoneFormattedTextScreen"
84
+ out += " destroyOnExit=\"yes\"" if @destroyOnExit == "yes"
85
+ if not @cancelAction.nil? then
86
+ cancelAction = escape(@cancelAction)
87
+ out += " cancelAction=\"#{cancelAction}\""
88
+ end
89
+ if not @doneAction.nil? then
90
+ doneAction = escape(@doneAction)
91
+ out += " doneAction=\"#{doneAction}\""
92
+ end
93
+ out += " Beep=\"yes\"" if @beep == "yes"
94
+ out += " LockIn=\"yes\"" if @lockin == "yes"
95
+ out += " allowAnswer=\"yes\"" if @allowAnswer == "yes"
96
+ out += " Timeout=\"#{@timeout}\"" if @timeout != 0
97
+ out += " allowDTMF=\"#{yes}\"" if @allowDTMF == "yes"
98
+ out += ">\n"
99
+ @entries.each do |entry|
100
+ out += entry.render
101
+ end
102
+ @softkeys.each do |softkey|
103
+ out += softkey.render
104
+ end
105
+ iconList = 0
106
+ @icons.each do |icon|
107
+ if iconList == 0 then
108
+ out += "<IconList>\n"
109
+ iconList = 1
110
+ end
111
+ out += icon.render
112
+ end
113
+ out += "</IconList>\n" if iconList != 0
114
+ out += "</AastraIPPhoneFormattedTextScreen>\n"
115
+ return out
116
+ end
117
+ end
@@ -0,0 +1,48 @@
1
+ ################################################################################
2
+ # Aastra XML API Classes - AastraIPPhoneFormattedTextScreenEntry
3
+ # Firmware 2.0 or better
4
+ # Copyright Aastra Telecom 2008
5
+ #
6
+ # Ruby adaptation by Carlton O'Riley
7
+ #
8
+ # Internal class for AastraIPPhoneFormattedTextScreen object.
9
+ ################################################################################
10
+
11
+ class AastraIPPhoneFormattedTextScreenEntry < AastraIPPhone
12
+ @text
13
+ @size
14
+ @align
15
+ @type
16
+
17
+ # Creates new formatted text entry. size is one of 'normal' (default)
18
+ # or 'double'. align is one of 'left' (default), 'center', or 'right'.
19
+ # type must be one of 'normal', 'scrollstart', or 'scrollend'.
20
+ def initialize(text, size, align, type)
21
+ if size == 'double' then
22
+ @text = convert_high_ascii(text)
23
+ else
24
+ @text = text
25
+ end
26
+ @size = size
27
+ @align = align
28
+ @type = type
29
+ end
30
+
31
+ # Create XML text output for this entry.
32
+ def render
33
+ case @type
34
+ when "normal"
35
+ xml = "<Line"
36
+ xml += " Size=\"#{@size}\"" if not @size.nil?
37
+ xml += " Align=\"#{@align}\"" if not @align.nil?
38
+ xml += ">"
39
+ xml += "#{escape(@text)}</Line>\n"
40
+ when "scrollstart"
41
+ xml = "<Scroll"
42
+ xml += " Height=\"#{@size}\"" if not @size.nil?
43
+ xml += ">\n"
44
+ when "scrollend" then xml = "</Scroll>\n"
45
+ end
46
+ return xml
47
+ end
48
+ end
@@ -0,0 +1,185 @@
1
+ require 'GD'
2
+
3
+ ################################################################################
4
+ # Aastra XML API Classes - Aastra XML API Classes - AastraIPPhoneGDImage
5
+ # Copyright Aastra Telecom 2008
6
+ #
7
+ # Ruby adaptation by Carlton O'Riley
8
+ #
9
+ # Firmware 2.2.0 or better
10
+ #
11
+ # AastraIPPhoneGDImage for AastraIPPhoneImageScreen and AastraIPPhoneImageScreen.
12
+ #
13
+ # ruby needs ruby-gd gem installed
14
+ # ------------------------------
15
+ #
16
+ # Public methods
17
+ #
18
+ # drawttftext(fontsize,angle,x,y,text,colorIndex,fontfile)
19
+ # Writes text to the image using TrueType fonts
20
+ # fontsize The font size. Depending on your version of GD, this should be specified as the pixel
21
+ # size (GD1) or point size (GD2)
22
+ # angle The angle in degrees, with 0 degrees being left-to-right reading text. Higher values
23
+ # represent a counter-clockwise rotation. For example, a value of 90 would result in
24
+ # bottom-to-top reading text.
25
+ # x,y The coordinates given by x and y will define the basepoint of the first character
26
+ # (roughly the lower-left corner of the character).
27
+ # colorIndex 0=White 1=Black
28
+ # fontfile Location and name of the ttf file to use
29
+ #
30
+ # drawtext(fontsize,x,y,text,colorIndex)
31
+ # Writes text to the image using built-in font
32
+ # fontsize The font size. From 1 to 5
33
+ # x,y The coordinates given by x and y will define the basepoint of the first character
34
+ # (roughly the lower-left corner of the character).
35
+ # colorIndex 0=White 1=Black
36
+ #
37
+ # rectangle(x1,y1,x2,y2,colorIndex,filled)
38
+ # Creates a rectangle starting at the specified coordinates.
39
+ # x1,y1 Upper left x,y coordinate. 0,0 is the top left corner of the image.
40
+ # x2,y2 Bottom right x,y coordinate
41
+ # colorIndex 0=White 1=Black
42
+ # filled Boolean, optional (default if False)
43
+ #
44
+ # ellipse(cx,cy,width,height,colorIndex,filled)
45
+ # Draws an ellipse centered at the specified coordinates.
46
+ # cx,cy x-coordinate and y-coordinate of the center
47
+ # width the ellipse width
48
+ # height the ellipse height
49
+ # colorIndex 0=White 1=Black
50
+ # filled Boolean, optional (default if False)
51
+ #
52
+ # line(x1,y1,x2,y2,colorIndex)
53
+ # Draws a line
54
+ # x1,y1 x,y coordinates for the first point
55
+ # x2,y2 x,y coordinates for the second point
56
+ # colorIndex 0=White 1=Black
57
+ #
58
+ # setGDImage(image)
59
+ # Imports an externally generated GD image
60
+ # image GD image to import
61
+ #
62
+ # getGDImage()
63
+ # Exports the current GD image
64
+ #
65
+ # setFontPath(fontpath)
66
+ # Set directory path for the fonts to use
67
+ # fontpath Directory for the ttf fonts
68
+ # Default value
69
+ # Windows based platform C:\Windows\Fonts
70
+ # Linux based platform ../fonts
71
+ # Mac OS X based platform ../fonts
72
+ # Rails based platform RAILS_ROOT/fonts
73
+ #
74
+ # Example 1
75
+ # require 'AastraIPPhoneGDImage.rb'
76
+ # phoneImageGD = AastraIPPhoneGDImage.new
77
+ # time = Time.now.strftime("%H:%M")
78
+ # phoneImageGD.drawttftext(30, 0, 10, 39, time, 1,'Ni7seg.ttf')
79
+ #
80
+ # Example 2
81
+ # require 'AastraIPPhoneGDImage.rb'
82
+ # phoneImageGD = AastraIPPhoneGDImage.new
83
+ # utf8text = "&#19996;&#19997;&#19998;&#19999;&#20024;"
84
+ # phoneImageGD.drawttftext(20, 0, 5, 35, utf8text, 1, 'arialuni.ttf')
85
+ #
86
+ ################################################################################
87
+
88
+ class AastraIPPhoneGDImage
89
+ @img
90
+ @white
91
+ @black
92
+ @blackNoAntiAliasing
93
+ @font
94
+ @fontpath
95
+
96
+ # Creates a new GD image of size 144x40 (maximum allowed on phone). Also
97
+ # creates black and white colors to be used and sets default font path
98
+ # based on whether this is running under rails or a specific
99
+ # operating system.
100
+ def initialize
101
+ # create image
102
+ @img = GD::Image.new(144, 40)
103
+
104
+ # define black and white
105
+ @white = @img.colorAllocate(255, 255, 255)
106
+ @black = @img.colorAllocate(0, 0, 0)
107
+
108
+ # black and white only so disable anti-aliasing
109
+ @black *= -1
110
+ @white *= -1
111
+
112
+ # define a default font path
113
+ if defined?(RAILS_ROOT) then
114
+ @fontpath = "#{RAILS_ROOT}/fonts"
115
+ else
116
+ os = RUBY_PLATFORM.downcase
117
+ @fontpath = "C:\\Windows\\Fonts" if not os.scan(/win/).nil?
118
+ @fontpath = "../fonts" if not os.scan(/linux$/).nil?
119
+ @fontpath = "../fonts" if not os.scan(/darwin\d+\.\d+$/).nil?
120
+ end
121
+ ENV['GDFONTPATH'] = @fontpath
122
+ end
123
+
124
+ # Set font path for GD.
125
+ def setFontPath(fontpath)
126
+ @fontpath = fontpath
127
+ ENV['GDFONTPATH'] = @fontpath
128
+ end
129
+
130
+ # Draw text on the image using a specific true type font.
131
+ # See GD documentation for parameters.
132
+ def drawttftext(size, angle, x, y, text, colorIndex, font)
133
+ @img.stringTTF(getColor(colorIndex), font, size, angle, x, y, text)
134
+ end
135
+
136
+ # Draw text on the image.
137
+ def drawtext(size, x, y, text, colorIndex)
138
+ @img.string(size, x, y, text, getColor(colorIndex))
139
+ end
140
+
141
+ # Set the image from an externally created GD image.
142
+ def setGDImage(image)
143
+ @img = image
144
+ end
145
+
146
+ # Get the GD image created.
147
+ def getGDImage
148
+ return @img
149
+ end
150
+
151
+ # Draw a rectangle on the image. Rectangle will be unfilled by default.
152
+ def rectangle(x1, y1, x2, y2, colorIndex, filled=false)
153
+ if filled then
154
+ @img.filledRectangle(x1, y1, x2, y2, getColor(colorIndex))
155
+ else
156
+ @img.rectangle(x1, y1, x2, y2, getColor(colorIndex))
157
+ end
158
+ end
159
+
160
+ # Draw an ellipse on the image. Ellipse will be unfilled by default.
161
+ def ellipse(cx, cy, width, height, colorIndex, filled=false)
162
+ if filled then
163
+ @img.filledEllipse(cx, cy, width, height, 0, 360, getColor(colorIndex))
164
+ else
165
+ @img.ellipse(cx, cy, width, height, 0, 360, getColor(colorIndex))
166
+ end
167
+ end
168
+
169
+ # Draw a line on the image. Line will be solid by default.
170
+ def line(x1, y1, x2, y2, colorIndex, dashed=false)
171
+ if dashed then
172
+ @img.dashedLine(x1, y2, x2, y2, getColor(colorIndex))
173
+ else
174
+ @img.line(x1, y2, x2, y2, getColor(colorIndex))
175
+ end
176
+ end
177
+
178
+ # Get the GD color element based on an index.
179
+ # 0:: white
180
+ # 1:: black
181
+ def getColor(index)
182
+ return @white if index == 0
183
+ return @black
184
+ end
185
+ end