bigbluebutton-Github/bigbluebutton-html5/app/client/whiteboard_models/whiteboard_text.coffee

207 lines
7.3 KiB
CoffeeScript
Raw Normal View History

# A text in the whiteboard
class @WhiteboardTextModel extends WhiteboardToolModel
constructor: (@paper) ->
super @paper
# the defintion of this shape, kept so we can redraw the shape whenever needed
# format: x, y, width, height, colour, fontSize, calcFontSize, text
@definition = [0, 0, 0, 0, "#000", 0, 0, ""]
# Make a text on the whiteboard
2014-08-20 00:54:01 +08:00
make: (startingData) ->
2014-08-22 23:26:37 +08:00
#console.log "making text:" + JSON.stringify startingData
2014-08-20 00:54:01 +08:00
x = startingData.x
y = startingData.y
width = startingData.textBoxWidth
height = startingData.textBoxHeight
2015-01-06 04:54:03 +08:00
colour = formatColor(startingData.fontColor)
2014-08-20 00:54:01 +08:00
fontSize = startingData.fontSize
calcFontSize = startingData.calcedFontSize
text = startingData.text
@definition =
shape: "text"
data: [x, y, width, height, colour, fontSize, calcFontSize, text]
2014-08-20 00:54:01 +08:00
#calcFontSize = (calcFontSize/100 * @gh)
x = (x * @gw) + @xOffset
y = (y * @gh) + @yOffset + calcFontSize
width = width/100 * @gw
2014-08-20 00:54:01 +08:00
@obj = @paper.text(x/100, y/100, "")
@obj.attr
2015-01-06 04:54:03 +08:00
"fill": colour
"font-family": "Arial" # TODO: make dynamic
"font-size": calcFontSize
@obj.node.style["text-anchor"] = "start" # force left align
@obj.node.style["textAnchor"] = "start" # for firefox, 'cause they like to be different
@obj
2014-08-20 03:53:27 +08:00
# Update text shape drawn
# @param {object} the object containing the shape info
2014-08-20 00:54:01 +08:00
update: (startingData) ->
2014-08-22 23:26:37 +08:00
#console.log "updating text" + JSON.stringify startingData
2014-08-20 00:54:01 +08:00
x = startingData.x
y = startingData.y
2014-08-20 03:36:45 +08:00
maxWidth = startingData.textBoxWidth
2014-08-20 00:54:01 +08:00
height = startingData.textBoxHeight
2015-01-06 04:54:03 +08:00
colour = formatColor(startingData.fontColor)
2014-08-20 00:54:01 +08:00
fontSize = startingData.fontSize
calcFontSize = startingData.calcedFontSize
2014-08-20 03:36:45 +08:00
myText = startingData.text
2014-08-20 00:54:01 +08:00
if @obj?
2014-08-20 03:36:45 +08:00
@definition.data = [x, y, maxWidth, height, colour, fontSize, calcFontSize, myText]
calcFontSize = (calcFontSize/100 * @gh)
x = (x * @gw)/100 + @xOffset
2014-08-20 03:36:45 +08:00
maxWidth = maxWidth/100 * @gw
@obj.attr
2015-01-06 04:54:03 +08:00
"fill": colour
"font-family": "Arial" # TODO: make dynamic
"font-size": calcFontSize
cell = @obj.node
while cell? and cell.hasChildNodes()
cell.removeChild(cell.firstChild)
2014-08-22 23:26:37 +08:00
# used code from textFlow lib http://www.carto.net/papers/svg/textFlow/
# but had to merge it here because "cell" was bigger than what the stack could take
#extract and add line breaks for start
dashArray = new Array()
dashFound = true
indexPos = 0
cumulY = 0
2014-08-22 23:26:37 +08:00
svgNS = "http://www.w3.org/2000/svg"
while dashFound is true
result = myText.indexOf("-", indexPos)
if result is -1
#could not find a dash
dashFound = false
else
dashArray.push result
indexPos = result + 1
#split the text at all spaces and dashes
words = myText.split(/[\s-]/)
line = ""
dy = 0
curNumChars = 0
computedTextLength = 0
myTextNode = undefined
tspanEl = undefined
i = 0
2015-01-30 01:52:04 +08:00
#checking if any of the words exceed the width of a textBox
words = checkWidth(words, maxWidth, x, dy, cell)
while i < words.length
word = words[i]
curNumChars += word.length + 1
if computedTextLength > maxWidth or i is 0
if computedTextLength > maxWidth
tempText = tspanEl.firstChild.nodeValue
tempText = tempText.slice(0, (tempText.length - words[i - 1].length - 2)) #the -2 is because we also strip off white space
tspanEl.firstChild.nodeValue = tempText
2015-01-24 01:41:28 +08:00
#setting up coordinates for the first line of text
if i is 0
dy = calcFontSize
cumulY += dy
#alternatively one could use textLength and lengthAdjust, however, currently this is not too well supported in SVG UA's
tspanEl = document.createElementNS(svgNS, "tspan")
tspanEl.setAttributeNS null, "x", x
tspanEl.setAttributeNS null, "dy", dy
myTextNode = document.createTextNode(line)
tspanEl.appendChild myTextNode
2014-08-20 03:36:45 +08:00
cell.appendChild tspanEl
if checkDashPosition(dashArray, curNumChars - 1)
line = word + "-"
else
line = word + " "
line = words[i - 1] + " " + line unless i is 0
2014-08-20 03:36:45 +08:00
dy = calcFontSize
cumulY += dy
else
if checkDashPosition(dashArray, curNumChars - 1)
line += word + "-"
else
line += word + " "
tspanEl.firstChild.nodeValue = line
2015-01-30 01:52:04 +08:00
computedTextLength = tspanEl.getComputedTextLength()+10
if i is words.length - 1
if computedTextLength > maxWidth
tempText = tspanEl.firstChild.nodeValue
tspanEl.firstChild.nodeValue = tempText.slice(0, (tempText.length - words[i].length - 1))
tspanEl = document.createElementNS(svgNS, "tspan")
tspanEl.setAttributeNS null, "x", x
tspanEl.setAttributeNS null, "dy", dy
myTextNode = document.createTextNode(words[i])
tspanEl.appendChild myTextNode
2014-08-20 03:36:45 +08:00
cell.appendChild tspanEl
i++
cumulY
#this function checks if there should be a dash at the given position, instead of a blank
checkDashPosition = (dashArray, pos) ->
result = false
i = 0
while i < dashArray.length
result = true if dashArray[i] is pos
i++
result
2015-01-30 01:52:04 +08:00
#this function checks the width of the word and adds a " " if the width of the word exceeds the width of the textbox
#in order for the word to be split and shown properly
checkWidth = (words, maxWidth, x, dy, cell) ->
count = 0
temp = words
temp3 = []
str = ""
svgNSi = "http://www.w3.org/2000/svg"
tempSpanEl = document.createElementNS(svgNSi, "tspan")
tempSpanEl.setAttributeNS null, "x", x
tempSpanEl.setAttributeNS null, "dy", dy
tempTextNode = document.createTextNode(str)
tempSpanEl.appendChild tempTextNode
2014-08-20 00:54:01 +08:00
2015-01-30 01:52:04 +08:00
num = 0
while num < temp.length
#creating a textNode and adding it to the cell to check the width
tempSpanEl.firstChild.nodeValue = temp[num]
cell.appendChild tempSpanEl
#if width is bigger than maxWidth + whitespace between textBox borders and a word
if tempSpanEl.getComputedTextLength()+10 > maxWidth
tempWord = temp[num]
cell.removeChild(cell.firstChild)
#initializing temp variables
count = 1
start = 0
partWord = "" + tempWord[0]
tempArray = []
#check the width by increasing the word character by character
while count < tempWord.length
partWord += tempWord[count]
tempSpanEl.firstChild.nodeValue = partWord
cell.appendChild tempSpanEl
if tempSpanEl.getComputedTextLength()+10 > maxWidth
temp3.push partWord.substring(0, partWord.length-1)
partWord = ""
partWord += tempWord[count]
if count is tempWord.length-1
temp3.push partWord
while cell? and cell.hasChildNodes()
cell.removeChild(cell.firstChild)
count++
else
temp3.push temp[num]
while cell? and cell.hasChildNodes()
cell.removeChild(cell.firstChild)
num++
2015-01-30 01:52:04 +08:00
while cell? and cell.hasChildNodes()
cell.removeChild(cell.firstChild)
temp3