Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / legend / styling / TextPath.java @ 29973

History | View | Annotate | Download (16.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: TextPath.java 25636 2008-12-01 08:42:11Z vcaballero $
45
 * $Log$
46
 * Revision 1.2  2007-03-09 11:20:57  jaume
47
 * Advanced symbology (start committing)
48
 *
49
 * Revision 1.1.2.3  2007/02/21 07:34:09  jaume
50
 * labeling starts working
51
 *
52
 * Revision 1.1.2.2  2007/02/09 07:47:05  jaume
53
 * Isymbol moved
54
 *
55
 * Revision 1.1.2.1  2007/02/06 17:01:04  jaume
56
 * first version (only lines)
57
 *
58
 *
59
 */
60
package org.gvsig.fmap.mapcontext.rendering.legend.styling;
61

    
62
import java.awt.Font;
63
import java.awt.Graphics2D;
64
import java.awt.font.FontRenderContext;
65
import java.awt.font.GlyphVector;
66
import java.awt.geom.Point2D;
67

    
68
import org.apache.batik.ext.awt.geom.PathLength;
69
import org.gvsig.fmap.geom.Geometry;
70
import org.gvsig.fmap.geom.GeometryLocator;
71
import org.gvsig.fmap.geom.GeometryManager;
72
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
73
import org.gvsig.fmap.geom.exception.CreateGeometryException;
74
import org.gvsig.fmap.geom.primitive.GeneralPathX;
75
import org.gvsig.fmap.geom.util.UtilFunctions;
76
import org.gvsig.fmap.mapcontext.Messages;
77
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
78
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
80

    
81
import com.vividsolutions.jts.algorithm.Angle;
82
/**
83
 * <p>Class that represents baseline of a string and allows the baseline to
84
 * be composed as contiguous segments with distinct slope each.<br></p>
85
 *
86
 * <p>Once a TextPath is created for a string it is possible to know where
87
 * the character at a determined position in the string is placed and
88
 * rotated.<br></p>
89
 *
90
 * @author jaume dominguez faus - jaume.dominguez@iver.es
91
 *
92
 */
93
public class TextPath {
94
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
95
        private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
96

    
97
        public static final int NO_POS = Integer.MIN_VALUE;
98
        /**
99
         * Don't set a concrete word spacing. The word is separated using the normal
100
         * width of the separator glyph.
101
         */
102
        public static final int DEFAULT_WORD_SPACING = Integer.MIN_VALUE;
103

    
104
        private char[] text;
105
        /**
106
         * An array which contains the calculated positions for the glyphs
107
         * Each row represents a glyph, and it contains the X coord, the Y coord, and the rotation angle
108
         */
109
        private double[][] posList;
110
        private int alignment;
111
        private float characterSpacing;
112
        private boolean kerning;
113
        private float wordSpacing;
114
        private float margin;
115
        private boolean rightToLeft;
116
        private int numGlyphs = 0;
117
        private float characterWidth;
118
        private char[] wordSeparators = {' '}; // in the future, separators might be provided as parameter
119

    
120
        /**
121
         * <p>Creates a new instance of TextPath with the current graphics
122
         * context.<br></p>
123
         *
124
         * <p>Given a <b>Graphics2D</b>, TextPath can know which Font and FontRenderContext
125
         * is in use. So, it can calculate the position and rotation of each
126
         * character in <b>char[] text</b> based in the path defined by the
127
         * <b>FShape path</b> argument.</p>
128
         * @param g, Graphics2D
129
         * @param path, FShape
130
         * @param text, char[]
131
         */
132
        public TextPath(Graphics2D g, Geometry path, char[] text, Font font,
133
                        float characterSpacing, float characterWidth, boolean kerning,
134
                        float leading, int alignment, float wordSpacing, float margin,
135
                        boolean rightToLeft) {
136
                this.text = text;
137
                if (alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_LEFT ||
138
                                alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_RIGHT
139
                                ||
140
                                alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_CENTERED ||
141
                                alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_JUSTIFY) {
142
                        this.alignment = alignment;
143
                } else {
144
                        throw new IllegalArgumentException(
145
                                        Messages.getString("invalid_value_for") + ": " +
146
                                        Messages.getString("alignment")+" ( "+alignment+")");
147
                }
148
                this.characterWidth = characterWidth;
149
                this.characterSpacing = characterSpacing;
150
                this.kerning = kerning;
151
                this.wordSpacing = wordSpacing;
152
                this.margin = margin;
153
                this.rightToLeft = rightToLeft;
154

    
155
                FontRenderContext frc = g.getFontRenderContext();
156
                /* java 6 code
157
                 * TODO keep this!!
158
                if (kerning) {
159
                        HashMap<TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
160
                        attrs.put(TextAttribute.KERNING , TextAttribute.KERNING_ON);
161
                }
162
                 */
163
                GlyphVector gv = font.createGlyphVector(frc, text);
164

    
165
                PathLength pl;
166
                try {
167
                        pl = new PathLength(softenShape(path, gv));
168
                        if (alignment==ITextSymbol.SYMBOL_STYLE_ALIGNMENT_RIGHT) {
169
                                posList = computeAtRight(gv, pl, text);
170
                        }
171
                        else if (alignment==ITextSymbol.SYMBOL_STYLE_ALIGNMENT_CENTERED) {
172
                                computeAtMiddle(frc, text, font, pl);
173
                        }
174
                        else {
175
                                posList = computeAtLeft(gv, pl, text);
176
                        }
177
                } catch (CreateGeometryException e) {
178
                        logger.error("Error creating a curve", e);
179
                }                
180
        }
181

    
182
        protected Geometry softenShape(Geometry shape, GlyphVector gv) throws CreateGeometryException {
183

    
184
                float interval = (float) gv.getVisualBounds().getWidth()/(gv.getNumGlyphs()*3);
185

    
186
                PathLength pl = new PathLength(shape);
187

    
188
                GeneralPathX correctedPath = new GeneralPathX();
189
                int controlPoints = 16;
190
                double[][] points = new double[controlPoints][2];
191
                double prevX, prevY;
192
                double xsum=0, ysum=0;
193
                int nextPos = 0;
194
                boolean bufferComplete = false;
195
                boolean movedTo = false;
196
                for (float curPos = 0; curPos<pl.lengthOfPath(); curPos = curPos+interval) {
197
                        prevX = points[nextPos][0];
198
                        prevY = points[nextPos][1];
199
                        Point2D point =pl.pointAtLength(curPos);
200
                        if (!movedTo) {
201
                                correctedPath.moveTo(point.getX(), point.getY());
202
                                movedTo = true;
203
                        }
204

    
205
                        points[nextPos][0] = point.getX();
206
                        points[nextPos][1] = point.getY();
207

    
208
                        if (!bufferComplete) {
209
                                xsum += points[nextPos][0];
210
                                ysum += points[nextPos][1];
211
                                nextPos++;
212
                                if (nextPos==controlPoints) {
213
                                        nextPos = 0;
214
                                        bufferComplete = true;
215

    
216

    
217
                                        /**
218
                                         * calculate the beginning of the line
219
                                         */
220
                                        // this will be the first interpolated point
221
                                        double auxX2 = xsum/controlPoints;
222
                                        double auxY2 = ysum/controlPoints;
223

    
224
                                        for (int i=1; i<controlPoints/2-1; i++) {
225
                                                // calculate the points from the origin of the geometry to the first interpolated point
226
                                                double auxX = (points[0][0]+points[i][0]+auxX2)/3;
227
                                                double auxY = (points[0][1]+points[i][1]+auxY2)/3;
228
                                                correctedPath.lineTo(auxX, auxY);
229
                                        }
230
                                        correctedPath.lineTo(auxX2, auxY2);
231
                                }
232
                        }
233
                        else {
234

    
235
                                xsum = xsum - prevX + points[nextPos][0];
236
                                ysum = ysum - prevY + points[nextPos][1];
237
                                if (!movedTo) {
238
                                        correctedPath.moveTo(xsum/controlPoints, ysum/controlPoints);
239
                                        movedTo = true;
240
                                }
241
                                else {
242
                                        correctedPath.lineTo(xsum/controlPoints, ysum/controlPoints);
243
                                }
244

    
245
                                nextPos = (nextPos+1)%controlPoints;
246
                        }
247
                }
248
                Point2D endPoint = pl.pointAtLength(pl.lengthOfPath());
249
                // last point in the geom
250
                double endPointX = endPoint.getX();
251
                double endPointY = endPoint.getY();
252

    
253
                if (bufferComplete) {
254
                        /**
255
                         * calculate the points from the last interpolated point to the end of the geometry
256
                         */
257

    
258
                        // last interpolated point
259
                        double auxX2 = xsum/controlPoints;
260
                        double auxY2 = ysum/controlPoints;
261
                        nextPos = (nextPos+(controlPoints/2))%controlPoints;
262
                        for (int i=0; i<controlPoints/2-1; i++) {
263
                                // calculate the points from the last interpolated point to the end of the geometry
264
                                double auxX = (auxX2+points[nextPos][0]+endPointX)/3;
265
                                double auxY = (auxY2+points[nextPos][1]+endPointY)/3;
266
                                correctedPath.lineTo(auxX, auxY);
267
                                nextPos = (nextPos+1)%controlPoints;
268
                        }
269
                }
270
                correctedPath.lineTo(endPointX, endPointY);
271

    
272
                return geomManager.createCurve(new GeneralPathX(correctedPath.getPathIterator(null)), SUBTYPES.GEOM2D);
273
        }
274

    
275
        /**
276
         * Initializes the position vector.
277
         * @param g
278
         * @param path
279
         */
280
        private double[][] computeAtRight(GlyphVector gv, PathLength pl, char[] text) {
281
                numGlyphs = gv.getNumGlyphs();
282
                double[][] pos = new double[numGlyphs][3];
283
                float[] charAnchors = new float[numGlyphs];
284

    
285
                /**
286
                 * Compute glyph positions using linear distances
287
                 */
288
                float lengthOfPath = pl.lengthOfPath();
289
                // char distance from the right side
290
                float charDistance = lengthOfPath-margin;
291
                int glyphsConsumed = numGlyphs-1;
292
                float previousAngle = 0.0f;
293
                float angle = 0.0f;
294
                boolean correction = true;
295
                float charWidth = characterWidth;
296
                for (int i = numGlyphs-1; i>=0; i--) {
297
                        if (correction && charDistance>=0) {
298
                                previousAngle = angle;
299
                                angle = pl.angleAtLength(charDistance);
300
                                if (i<numGlyphs-1) {
301
                                        // correct distance according to angle between current and previous glyph
302
                                        int turn = Angle.getTurn(previousAngle, angle);
303
                                        if (turn==1) {  // if turn is positive => increase distance
304
                                                float auxDistance = charDistance - (float)(charWidth*2.5f*Angle.diff(previousAngle, angle)/Math.PI);
305
                                                float auxAngle = pl.angleAtLength(auxDistance);
306
                                                if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn
307
                                                        charDistance = auxDistance;
308
                                                        angle = auxAngle;
309
                                                }
310
                                        }
311
                                        else if (turn==-1) { // if turn is negative => decrease distance
312
                                                float auxDistance = charDistance + (float)(charWidth*0.9f*Angle.diff(previousAngle, angle)/Math.PI);
313
                                                float auxAngle = pl.angleAtLength(auxDistance);
314
                                                if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn
315
                                                        charDistance = auxDistance;
316
                                                        angle = auxAngle;
317
                                                }
318
                                        }
319
                                }
320
                        }
321

    
322
                        if (wordSpacing!=DEFAULT_WORD_SPACING
323
                                        && isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) {
324
                                charWidth = wordSpacing;
325
                        }
326
                        else {
327
                                charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
328

    
329
                        }
330
                        charDistance -= charWidth;
331
                        charAnchors[glyphsConsumed] = charDistance;
332
                        charDistance -= characterSpacing;
333
                        glyphsConsumed--;
334
                }
335

    
336
                /**
337
                 * Calculate 2D positions for the glyphs from the calculated linear distances
338
                 */
339
                for (int i = numGlyphs-1; i>=0; i--) {
340
                        float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i];
341
                        Point2D p = pl.pointAtLength( anchor );
342
                        if (p == null) {
343
                                if (i<numGlyphs-1) { // place in a straight line the glyphs that don't fit in the shape
344
                                        pos[i][0] = pos[i+1][0] + (charAnchors[i]-charAnchors[i+1])*Math.cos(pos[i+1][2]);
345
                                        pos[i][1] = pos[i+1][1] + (charAnchors[i]-charAnchors[i+1])*Math.sin(pos[i+1][2]);
346
                                        pos[i][2] = pos[i+1][2];
347
                                } else {
348
                                        pos[i][0] = NO_POS;
349
                                        pos[i][1] = NO_POS;
350
                                }
351
                                continue;
352
                        }
353
                        pos[i][0] = p.getX();
354
                        pos[i][1] = p.getY();
355
                        pos[i][2] = pl.angleAtLength( anchor );
356
                }
357
                return pos;
358
        }
359

    
360
        /**
361
         * Initializes the position vector.
362
         * @param g
363
         * @param path
364
         */
365
        private double[][] computeAtLeft(GlyphVector gv, PathLength pl, char[] text) {
366
                numGlyphs = gv.getNumGlyphs();
367
                double[][] pos = new double[numGlyphs][3];
368
                float[] charAnchors = new float[numGlyphs];
369
                float[] charWidths = new float[numGlyphs];
370

    
371
                /**
372
                 * Compute glyph positions using linear distances
373
                 */
374
                float lengthOfPath = pl.lengthOfPath();
375
                float charDistance = margin;
376
                int glyphsConsumed = 0;
377
                float previousAngle = 0.0f;
378
                float angle = 0.0f;
379
                boolean correction = true;
380
                float charWidth = characterWidth;
381
                for (int i = 0; i < gv.getNumGlyphs(); i++) {
382

    
383
                        if (correction && charDistance<=lengthOfPath) {
384
                                previousAngle = angle;
385
                                angle = pl.angleAtLength(charDistance);
386
                                if (i>0) {
387
                                        // correct distance according to angle between current and previous glyph
388
                                        int turn = Angle.getTurn(previousAngle, angle);
389
                                        if (turn==1) {  // if turn is positive => decrease distance
390
                                                float auxDistance = charDistance - (float)(charWidth*0.9*Angle.diff(previousAngle, angle)/Math.PI);
391
                                                float auxAngle = pl.angleAtLength(auxDistance);
392
                                                if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn
393
                                                        charDistance = auxDistance;
394
                                                        angle = auxAngle;
395
                                                }
396
                                        }
397
                                        else if (turn == -1){ // if turn is negative => increase distance
398

    
399
                                                float auxDistance = charDistance + (float)(charWidth*2.5*Angle.diff(previousAngle, angle)/Math.PI);
400
                                                float auxAngle = pl.angleAtLength(auxDistance);
401
                                                if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn
402
                                                        charDistance = auxDistance;
403
                                                        angle = auxAngle;
404
                                                }
405
                                        }
406
                                }
407
                        }
408
                        if (wordSpacing!=DEFAULT_WORD_SPACING
409
                                        && isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) {
410
                                // use defined wordspacing
411
                                charWidth = wordSpacing;
412
                        }
413
                        else {
414
                                charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
415

    
416
                        }
417
                        charWidths[glyphsConsumed] = charWidth;
418
                        charAnchors[glyphsConsumed] = charDistance;
419
                        charDistance += charWidth;
420
                        charDistance += characterSpacing;
421
                        glyphsConsumed++;
422
                }
423

    
424
                /**
425
                 * Calculate 2D positions for the glyphs from the calculated linear distances
426
                 */
427
                for (int i = 0; i < charAnchors.length; i++) {
428
                        float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i];
429
                        Point2D p = pl.pointAtLength( anchor );
430
                        if (p == null) {
431
                                if (i>0) { // place in a straight line the glyphs that don't fit in the shape
432
                                        pos[i][0] = pos[i-1][0] + (charAnchors[i]-charAnchors[i-1])*Math.cos(pos[i-1][2]);
433
                                        pos[i][1] = pos[i-1][1] + (charAnchors[i]-charAnchors[i-1])*Math.sin(pos[i-1][2]);
434
                                        pos[i][2] = pos[i-1][2];
435
                                } else {
436
                                        pos[i][0] = NO_POS;
437
                                        pos[i][1] = NO_POS;
438
                                }
439
                                continue;
440
                        }
441
                        pos[i][2] = pl.angleAtLength( anchor );
442
                        //                        pos[i][0] = p.getX() - charWidths[i]*Math.cos(pos[i][2]);
443
                        //                        pos[i][1] = p.getY() - charWidths[i]*Math.sin(pos[i][2]);
444
                        pos[i][0] = p.getX();
445
                        pos[i][1] = p.getY();
446
                }
447
                return pos;
448
        }
449

    
450

    
451
        /**
452
         * Initializes the position vector.
453
         * @param g
454
         * @param path
455
         */
456
        private void computeAtMiddle(FontRenderContext frc, char[] text, Font font, PathLength pl) {
457
                if (text.length==0) {
458
                        return; // nothing to compute if text length is 0
459
                }
460
                int middleChar = (text.length-1)/2;
461
                char[] text1 = new char[middleChar+1];
462
                char[] text2 = new char[text.length-text1.length];
463
                System.arraycopy(text, 0, text1, 0, text1.length);
464
                System.arraycopy(text, text1.length,  text2, 0, text2.length);
465

    
466
                float halfLength = pl.lengthOfPath()/2.0f;
467
                margin = halfLength;
468
                GlyphVector gv = font.createGlyphVector(frc, text1);
469
                double[][] pos1 = computeAtRight(gv, pl, text1);
470
                int glyphCount = numGlyphs;
471
                gv = font.createGlyphVector(frc, text2);
472
                margin = halfLength + characterSpacing;
473
                double[][] pos2 = computeAtLeft(gv, pl, text2);
474
                numGlyphs += glyphCount;
475
                posList = new double[pos1.length+pos2.length][3];
476
                System.arraycopy(pos1, 0, posList, 0, pos1.length);
477
                System.arraycopy(pos2, 0, posList, pos1.length, pos2.length);
478
        }
479

    
480

    
481
        /**
482
         * <p>Returns the placement of the next character to draw and the corresponding
483
         * rotation in a double array of three elements with this order:</p><br>
484
         *
485
         * <p><b>double[0]</b> Position in X in the screen</p>
486
         * <p><b>double[1]</b> Position in Y in the screen</p>
487
         * <p><b>double[2]</b> Angle of the character.</p>
488
         * @return
489
         */
490
        public double[] nextPosForGlyph(int glyphIndex) {
491
                return posList[glyphIndex];
492
        }
493

    
494
        public int getGlyphCount() {
495
                return numGlyphs;
496
        }
497

    
498
        protected static boolean isWordSeparator(char c, char[] wordSeparators) {
499
                char separator;
500
                for (int i = 0; i < wordSeparators.length; i++) {
501
                        separator = wordSeparators[i];
502
                        if (c==separator) {
503
                                return true;
504
                        }
505
                }
506
                return false;
507
        }
508

    
509
}