source: Dev/branches/rest-dojo-ui/client/dojox/drawing/stencil/Text.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 6.0 KB
Line 
1dojo.provide("dojox.drawing.stencil.Text");
2
3dojox.drawing.stencil.Text = dojox.drawing.util.oo.declare(
4        // summary:
5        //              Creates a dojox.gfx Text (SVG or VML) based on data provided.
6        // description:
7        //              There are two text classes. TextBlock extends this one and
8        //              adds editable functionality, discovers text width etc.
9        //              This class displays text only. There is no line wrapping.
10        //              Multiple lines can be acheived by inserting \n linebreaks
11        //              in the text.
12        //
13        dojox.drawing.stencil._Base,
14        function(options){
15                // summary:
16                //              constructor.
17        },
18        {
19                type:"dojox.drawing.stencil.Text",
20                anchorType:"none",
21                baseRender:true,
22               
23                // align: String
24                //      Text horizontal alignment.
25                //              Options: start, middle, end
26                align:"start",
27                //
28                // valign:String
29                //      Text vertical alignment
30                //              Options: top, middle, bottom (FIXME: bottom not supported)
31                valign:"top",
32                //
33                // _lineHeight: [readonly] Number
34                //      The height of each line of text. Based on style information
35                //      and font size.
36                _lineHeight:1,
37               
38/*=====
39StencilData: {
40        // summary:
41        //              The data used to create the dojox.gfx Text
42        //      x: Number
43        //              Left point x
44        //      y: Number
45        //              Top point y
46        //      width: ? Number
47        //              Optional width of Text. Not required but reccommended.
48        //              for auto-sizing, use TextBlock
49        //      height: ? Number
50        //              Optional height of Text. If not provided, _lineHeight is used.
51        //      text: String
52        //              The string content. If not provided, may auto-delete depending on defaults.
53},
54
55StencilPoints: [
56        // summary:
57        //              An Array of dojox.__StencilPoint objects that describe the Stencil
58        //      0: Object
59        //              Top left point
60        //      1: Object
61        //              Top right point
62        //      2: Object
63        //              Bottom right point
64        //      3: Object
65        //              Bottom left point
66],
67=====*/
68
69                typesetter: function(text){
70                        // summary:
71                        //              Register raw text, returning typeset form.
72                        //              Uses function dojox.drawing.stencil.Text.typeset
73                        //              for typesetting, if it exists.
74                        //
75                        if(dojox.drawing.util.typeset){
76                                this._rawText = text;
77                                return dojox.drawing.util.typeset.convertLaTeX(text);
78                        }
79                        return text;
80                },
81
82                setText: function(text){
83                        // summary:
84                        //              Setter for text.
85                        //
86                        // Only apply typesetting to objects that the user can modify.
87                        // Else, it is assumed that typesetting is done elsewhere.
88                        if(this.enabled){
89                                text = this.typesetter(text);
90                        }
91                        // This only has an effect if text is null or this.created is false.
92                        this._text = text;
93
94                        this._textArray = [];
95                        this.created && this.render(text);
96                },
97               
98                getText: function(){
99                        // summary:
100                        //              Getter for text.
101                        //
102                        return this._rawText || this._text;
103                },
104               
105                dataToPoints: function(/*Object*/o){
106                        //summary:
107                        //              Converts data to points.
108                        o = o || this.data;
109                        var w = o.width =="auto" ? 1 : o.width;
110                        var h = o.height || this._lineHeight;
111                        this.points = [
112                                {x:o.x, y:o.y},                                 // TL
113                                {x:o.x + w, y:o.y},                             // TR
114                                {x:o.x + w, y:o.y + h},                 // BR
115                                {x:o.x, y:o.y + h}                              // BL
116                        ];
117                        return this.points;
118                },
119                pointsToData: function(/*Array*/p){
120                        // summary:
121                        //              Converts points to data
122                        p = p || this.points;
123                        var s = p[0];
124                        var e = p[2];
125                        this.data = {
126                                x: s.x,
127                                y: s.y,
128                                width: e.x-s.x,
129                                height: e.y-s.y
130                        };
131                        return this.data;
132                },
133               
134                render: function(/* String*/text){
135                        // summary:
136                        //              Renders the 'hit' object (the shape used for an expanded
137                        //              hit area and for highlighting) and the'shape' (the actual
138                        //              display object). Text is slightly different than other
139                        //              implementations. Instead of calling render twice, it calls
140                        //              _createHilite for the 'hit'
141                        // arguments:
142                        //              text String
143                        //                      Changes text if sent. Be sure to use the setText and
144                        //                      not to call this directly.
145                        //
146                        this.remove(this.shape, this.hit);
147                        //console.log("text render, outline:", !this.annotation, this.renderHit, (!this.annotation && this.renderHit))
148                        !this.annotation && this.renderHit && this._renderOutline();
149                        if(text!=undefined){
150                                this._text = text;
151                                this._textArray = this._text.split("\n");
152                        }
153                       
154                        var d = this.pointsToData();
155                        var h = this._lineHeight;
156                        var x = d.x + this.style.text.pad*2;
157                        var y = d.y + this._lineHeight - (this.textSize*.4);
158                        if(this.valign=="middle"){
159                                y -= h/2;
160                        }
161                        this.shape = this.container.createGroup();
162                       
163                        /*console.log("    render ", this.type, this.id)
164                        console.log("    render Y:", d.y, "textSize:", this.textSize, "LH:", this._lineHeight)
165                        console.log("    render text:", y, " ... ", this._text, "enabled:", this.enabled);
166                        console.log("    render text:", this.style.currentText);
167                        */
168                        dojo.forEach(this._textArray, function(txt, i){
169                                var tb = this.shape.createText({x: x, y: y+(h*i), text: unescape(txt), align: this.align})
170                                        .setFont(this.style.currentText)
171                                        .setFill(this.style.currentText.color);
172                               
173                                this._setNodeAtts(tb);
174                       
175                        }, this);
176                       
177                        this._setNodeAtts(this.shape);
178                       
179                },
180                _renderOutline: function(){
181                        // summary:
182                        //              Create the hit and highlight area
183                        //              for the Text.
184                        //
185                        if(this.annotation){ return; }
186                        var d = this.pointsToData();
187                       
188                        if(this.align=="middle"){
189                                d.x -= d.width/2 - this.style.text.pad * 2;
190                        }else if(this.align=="start"){
191                                d.x += this.style.text.pad;
192                        }else if(this.align=="end"){
193                                d.x -= d.width - this.style.text.pad * 3;
194                        }
195                       
196                        if(this.valign=="middle"){
197                                d.y -= (this._lineHeight )/2 - this.style.text.pad;
198                        }
199                       
200                        this.hit = this.container.createRect(d)
201                                .setStroke(this.style.currentHit)
202                                .setFill(this.style.currentHit.fill);
203                                //.setFill("#ffff00");
204                       
205                        this._setNodeAtts(this.hit);
206                        this.hit.moveToBack();
207                },
208                makeFit: function(text, w){
209                        var span = dojo.create('span', {innerHTML:text, id:"foo"}, document.body);
210                        var sz = 1;
211                        dojo.style(span, "fontSize", sz+"px");
212                        var cnt = 30;
213                        while(dojo.marginBox(span).w<w){
214                                sz++;
215                                dojo.style(span, "fontSize", sz+"px");
216                                if(cnt--<=0) break;
217                        }
218                        sz--;
219                        var box = dojo.marginBox(span);
220                        dojo.destroy(span);
221                       
222                        return {size:sz, box:box};
223                }
224        }
225);
226dojox.drawing.register({
227        name:"dojox.drawing.stencil.Text"
228}, "stencil");
Note: See TracBrowser for help on using the repository browser.