use the helper code within whiteboard_text
This commit is contained in:
parent
a27bd5cd91
commit
a976cd7c2e
@ -1,158 +0,0 @@
|
||||
/*
|
||||
Scripts to create flowText (rectangular) in SVG 1.1 UAs
|
||||
Copyright (C) <2007> <Andreas Neumann>
|
||||
Version 1.0, 2007-02-26
|
||||
neumann@karto.baug.ethz.ch
|
||||
http://www.carto.net/
|
||||
http://www.carto.net/neumann/
|
||||
|
||||
----
|
||||
|
||||
Documentation: http://www.carto.net/papers/svg/textFlow/
|
||||
|
||||
----
|
||||
|
||||
current version: 1.0.
|
||||
|
||||
1.0 (2007-02-26)
|
||||
initial release
|
||||
|
||||
-------
|
||||
|
||||
This ECMA script library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library (lesser_gpl.txt); if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
----
|
||||
|
||||
original document site: http://www.carto.net/papers/svg/textFlow/
|
||||
Please contact the author in case you want to use code or ideas commercially.
|
||||
If you use this code, please include this copyright header, the included full
|
||||
LGPL 2.1 text and read the terms provided in the LGPL 2.1 license
|
||||
(http://www.gnu.org/copyleft/lesser.txt)
|
||||
|
||||
-------------------------------
|
||||
|
||||
Please report bugs and send improvements to neumann@karto.baug.ethz.ch
|
||||
If you use this control, please link to the original (http://www.carto.net/papers/svg/textFlow/)
|
||||
somewhere in the source-code-comment or the "about" of your project and give credits, thanks!
|
||||
|
||||
*/
|
||||
|
||||
Meteor.methods({
|
||||
//'textFlow': function(myText, textToAppend, maxWidth, x, ddy, justified) {
|
||||
'textFlow': function(myText, maxWidth, x, ddy, justified) {
|
||||
alert("got here111!");
|
||||
//main function
|
||||
//function textFlowHelper(myText, textToAppend, maxWidth, x, ddy, justified) {
|
||||
function textFlowHelper(myText, maxWidth, x, ddy, justified) {
|
||||
//extract and add line breaks for start
|
||||
var dashArray = new Array();
|
||||
var dashFound = true;
|
||||
var indexPos = 0;
|
||||
var cumulY = 0;
|
||||
alert( getElementById('whiteboard-paper'));
|
||||
while (dashFound == true) {
|
||||
var result = myText.indexOf("-", indexPos);
|
||||
if (result == -1) {
|
||||
//could not find a dash
|
||||
dashFound = false;
|
||||
} else {
|
||||
dashArray.push(result);
|
||||
indexPos = result + 1;
|
||||
}
|
||||
}
|
||||
//split the text at all spaces and dashes
|
||||
var words = myText.split(/[\s-]/);
|
||||
var line = "";
|
||||
var dy = 0;
|
||||
var curNumChars = 0;
|
||||
var computedTextLength = 0;
|
||||
var myTextNode;
|
||||
var tspanEl;
|
||||
var lastLineBreak = 0;
|
||||
|
||||
for (i = 0; i < words.length; i++) {
|
||||
var word = words[i];
|
||||
curNumChars += word.length + 1;
|
||||
if (computedTextLength > maxWidth || i == 0) {
|
||||
if (computedTextLength > maxWidth) {
|
||||
var 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;
|
||||
if (justified) {
|
||||
//determine the number of words in this line
|
||||
var nrWords = tempText.split(/\s/).length;
|
||||
computedTextLength = tspanEl.getComputedTextLength();
|
||||
var additionalWordSpacing = (maxWidth - computedTextLength) / (nrWords - 1);
|
||||
tspanEl.setAttributeNS(null, "word-spacing", additionalWordSpacing);
|
||||
//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);
|
||||
textToAppend.appendChild(tspanEl);
|
||||
|
||||
if (checkDashPosition(dashArray, curNumChars - 1)) {
|
||||
line = word + "-";
|
||||
} else {
|
||||
line = word + " ";
|
||||
}
|
||||
if (i != 0) {
|
||||
line = words[i - 1] + " " + line;
|
||||
}
|
||||
dy = ddy;
|
||||
cumulY += dy;
|
||||
} else {
|
||||
if (checkDashPosition(dashArray, curNumChars - 1)) {
|
||||
line += word + "-";
|
||||
} else {
|
||||
line += word + " ";
|
||||
}
|
||||
}
|
||||
tspanEl.firstChild.nodeValue = line;
|
||||
computedTextLength = tspanEl.getComputedTextLength();
|
||||
if (i == words.length - 1) {
|
||||
if (computedTextLength > maxWidth) {
|
||||
var 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);
|
||||
textToAppend.appendChild(tspanEl);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return cumulY;
|
||||
}
|
||||
|
||||
//this function checks if there should be a dash at the given position, instead of a blank
|
||||
function checkDashPosition(dashArray, pos) {
|
||||
var result = false;
|
||||
for (var i = 0; i < dashArray.length; i++) {
|
||||
if (dashArray[i] == pos) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return textFlowHelper(myText, textToAppend, maxWidth, x, ddy, justified);
|
||||
}
|
||||
});
|
@ -66,22 +66,139 @@ class @WhiteboardTextModel extends WhiteboardToolModel
|
||||
calcFontSize = startingData.calcedFontSize
|
||||
text = startingData.text
|
||||
|
||||
svgNS = "http://www.w3.org/2000/svg"
|
||||
|
||||
if @obj?
|
||||
@definition.data = [x, y, width, height, colour, fontSize, calcFontSize, text]
|
||||
|
||||
calcFontSize = (calcFontSize/100 * @gh)
|
||||
x = (x * @gw) + @xOffset
|
||||
x = ((x * @gw) + @xOffset)/100
|
||||
width = width/100 * @gw
|
||||
|
||||
#colour = Utils.strokeAndThickness(colour)["stroke"]
|
||||
|
||||
|
||||
@obj.attr
|
||||
fill: Meteor.call("strokeAndThickness",colour, false)
|
||||
fill: "#000" #Meteor.call("strokeAndThickness",colour, false)
|
||||
"font-size": calcFontSize
|
||||
cell = @obj.node
|
||||
while cell? and cell.hasChildNodes()
|
||||
cell.removeChild(cell.firstChild)
|
||||
Meteor.call("textFlow", text, cell, width, x, calcFontSize, false)
|
||||
#Meteor.call("textFlow", text, cell, width, x, calcFontSize, false)
|
||||
#Meteor.call("textFlow", text, width, x, calcFontSize, false)
|
||||
|
||||
myText = text
|
||||
textToAppend = cell
|
||||
maxWidth = width
|
||||
ddy = calcFontSize
|
||||
justified = false
|
||||
|
||||
|
||||
#extract and add line breaks for start
|
||||
dashArray = new Array()
|
||||
dashFound = true
|
||||
indexPos = 0
|
||||
cumulY = 0
|
||||
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
|
||||
lastLineBreak = 0
|
||||
i = 0
|
||||
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
|
||||
if justified
|
||||
|
||||
#determine the number of words in this line
|
||||
nrWords = tempText.split(/\s/).length
|
||||
computedTextLength = tspanEl.getComputedTextLength()
|
||||
additionalWordSpacing = (maxWidth - computedTextLength) / (nrWords - 1)
|
||||
tspanEl.setAttributeNS null, "word-spacing", additionalWordSpacing
|
||||
|
||||
#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
|
||||
textToAppend.appendChild tspanEl
|
||||
if checkDashPosition(dashArray, curNumChars - 1)
|
||||
line = word + "-"
|
||||
else
|
||||
line = word + " "
|
||||
line = words[i - 1] + " " + line unless i is 0
|
||||
dy = ddy
|
||||
cumulY += dy
|
||||
else
|
||||
if checkDashPosition(dashArray, curNumChars - 1)
|
||||
line += word + "-"
|
||||
else
|
||||
line += word + " "
|
||||
tspanEl.firstChild.nodeValue = line
|
||||
computedTextLength = tspanEl.getComputedTextLength()
|
||||
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
|
||||
textToAppend.appendChild tspanEl
|
||||
i++
|
||||
cumulY
|
||||
|
||||
|
||||
|
||||
#this function checks if there should be a dash at the given position, instead of a blank
|
||||
checkDashPosition = (dashArray, pos) ->
|
||||
alert "checkDashPosition"
|
||||
result = false
|
||||
i = 0
|
||||
|
||||
while i < dashArray.length
|
||||
result = true if dashArray[i] is pos
|
||||
i++
|
||||
result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Draw a text on the whiteboard
|
||||
|
Loading…
Reference in New Issue
Block a user