Revision 11778 branches/v10/extensions/extAnnotations/src/com/iver/cit/gvsig/fmap/operation/strategies/Annotation_Strategy.java

View differences:

Annotation_Strategy.java
46 46
import java.awt.Font;
47 47
import java.awt.FontMetrics;
48 48
import java.awt.Graphics2D;
49
import java.awt.geom.AffineTransform;
49 50
import java.awt.geom.Point2D;
50 51
import java.awt.geom.Rectangle2D;
51 52
import java.awt.image.BufferedImage;
......
66 67
import com.iver.cit.gvsig.fmap.core.FShape;
67 68
import com.iver.cit.gvsig.fmap.core.IGeometry;
68 69
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
70
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
69 71
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
70 72
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
71 73
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
......
96 98
public class Annotation_Strategy extends DefaultStrategy {
97 99
    private FSymbol symbolPoint = new FSymbol(FShape.POINT, Color.black);
98 100
    private Annotation_Layer capa;
99

  
101
    private Geometry geometry;
102
    private AffineTransform ati=new AffineTransform();
100 103
    /**
101 104
     * Crea un nuevo AnotationStrategy.
102 105
     *
......
261 264
    // }
262 265
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
263 266
        Cancellable cancel) {
267
//        geometry=null;
268
        BufferedImage bi=new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);
269
        Graphics2D gBi=(Graphics2D)bi.getGraphics();
264 270
        // Copia draw AttrInTableLabeling
265 271
        Annotation_Legend l = (Annotation_Legend) capa.getLegend();
266 272
        FSymbol sym = (FSymbol) l.getDefaultSymbol();
......
283 289
            int idTextField = mapping.getColumnText();
284 290

  
285 291
            double rotation = 0D;
286
            double size = sym.getFont().getSize();
292
            float size = sym.getFont().getSize();
287 293

  
288 294
            //			sym.setFontSizeInPixels(((Annotation_Layer) capa).isInPixels());
289 295
            String fontName = "Dialog";
290 296
            int fontStyle = sym.getFont().getStyle();
291 297
            int fontColor = sym.getFontColor().getRGB();
292

  
298
            long t1 = System.currentTimeMillis();
293 299
            for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
294 300
                if (cancel.isCanceled()) {
295 301
                    break;
......
300 306
                if (idHeightField != -1) {
301 307
                    // text size is defined in the table
302 308
                    try {
303
                        size = ((NumericValue) vv[idHeightField]).doubleValue() * FConstant.FONT_HEIGHT_SCALE_FACTOR;
309
                        size = (float)(((NumericValue) vv[idHeightField]).doubleValue() * FConstant.FONT_HEIGHT_SCALE_FACTOR);
304 310
                    } catch (ClassCastException ccEx) {
305 311
                        if (!NullValue.class.equals(
306 312
                                    vv[idHeightField].getClass())) {
......
350 356
                    sym = (FSymbol) sym.getSymbolForSelection();
351 357
                }
352 358

  
353
                IGeometry geom = source.getShape(i);
354 359
                sym.setFont(new Font(fontName, fontStyle, (int) size));
355 360

  
356
                // sym.setDescription(vv[idTextField].toString());
357
                // sym.setRotation((int) rotation);
358
                Rectangle2D r=geom.getBounds2D();
359
                FLabel[] aux = new FLabel[]{new FLabel()};//geom.createLabels(0, true);
360
                aux[0].setOrig(new Point2D.Double(r.getX(),r.getY()));
361
                aux[0].setHeight(size);
362
                aux[0].setRotation((int) rotation);
363
                aux[0].setString(vv[idTextField].toString());
361
                if (!sym.isFontSizeInPixels()){
362
                	Rectangle2D r=capa.getTextWrappingGeometry(size,vv[idTextField].toString(),rotation,i).getBounds2D();
363
	                Rectangle2D rPixels=viewPort.fromMapRectangle(r);
364
	                FPoint2D fp=new FPoint2D(r.getX(),r.getY());
365
	                FPoint2D fpPixels=new FPoint2D(rPixels.getX(),rPixels.getY());
366
	                rPixels.setRect(rPixels.getX(),rPixels.getY()-rPixels.getHeight(),rPixels.getWidth(),rPixels.getHeight());
367
	                if (l.isOverWrite() || isTrasparent(bi,rPixels)){
368
	                    FLabel[] aux = new FLabel[]{new FLabel()};//geom.createLabels(0, true);
369
	                    aux[0].setOrig(new Point2D.Double(fp.getX(),fp.getY()));
370
	                    aux[0].setHeight(size);
371
	                    aux[0].setRotation((int) rotation);
372
	                    aux[0].setString(vv[idTextField].toString());
364 373

  
365
                if (sym.isShapeVisible()) {
366
                    symbolPoint.draw((Graphics2D) g,
367
                        viewPort.getAffineTransform(),
368
                        new FPoint2D(viewPort.fromMapPoint(aux[0].getOrig())));
374
	                    if (sym.isShapeVisible()) {
375
	                        symbolPoint.draw(gBi,
376
	                               viewPort.getAffineTransform(),
377
	                               fpPixels);
378
	                    }
379
//	                    g.drawRect((int)rPixels.getX(),(int)rPixels.getY(),(int)rPixels.getWidth(),(int)rPixels.getHeight());
380
	                    FGraphicUtilities.DrawAnnotation(gBi,
381
	                            viewPort.getAffineTransform(), sym, aux[0], metrics, false);
382
	                }
383
                }else{
384
	                Rectangle2D r=capa.getTextWrappingGeometryInPixels(size,vv[idTextField].toString(),rotation,i).getBounds2D();
385
	                Rectangle2D rPixels=r;//capa.getMapContext().getViewPort().fromMapRectangle(r);
386
	                FPoint2D fp=new FPoint2D(rPixels.getX(),rPixels.getY());
387
	                rPixels.setRect(rPixels.getX(),rPixels.getY()-rPixels.getHeight(),rPixels.getWidth(),rPixels.getHeight());
388
	                if (l.isOverWrite() || isTrasparent(bi,rPixels)){
389
	                    FLabel[] aux = new FLabel[]{new FLabel()};//geom.createLabels(0, true);
390
	                    aux[0].setOrig(new Point2D.Double(fp.getX(),fp.getY()));
391
	                    aux[0].setHeight(size);
392
	                    aux[0].setRotation((int) rotation);
393
	                    aux[0].setString(vv[idTextField].toString());
394

  
395
	                    if (sym.isShapeVisible()) {
396
	                        symbolPoint.draw(gBi,
397
	                               ati,
398
	                               fp);
399
	                    }
400
//	                    g.drawRect((int)rPixels.getX(),(int)rPixels.getY(),(int)rPixels.getWidth(),(int)rPixels.getHeight());
401
	                    FGraphicUtilities.DrawAnnotation(gBi,
402
	                            ati, sym, aux[0], metrics, false);
403

  
404
	                }
369 405
                }
406
                //Al dibujar esta imagen sobre el graphics la trasparencia no puede existir.
407
                if (i%1000==0){
408
                	 g.drawImage(bi,0,0,null);
409
                }
410
            }
411
            g.drawImage(bi,0,0,null);
412
            System.err.println(System.currentTimeMillis()-t1+"millis");
370 413

  
371
                FGraphicUtilities.DrawAnnotation(g,
372
                    viewPort.getAffineTransform(), sym, aux[0], metrics, false);
373
            }
374 414
        } catch (Exception e) {
375 415
            // Logger.getAnonymousLogger().log(Level.SEVERE, "Could not get the
376 416
            // layer extent.\n" +
......
486 526
    //
487 527
    // }
488 528

  
529
    private boolean isTrasparent(BufferedImage bi, Rectangle2D rPixels) {
530
        for (int i=(int)rPixels.getX();i<rPixels.getMaxX();i++){
531
            for (int j=(int)rPixels.getY();j<rPixels.getMaxY();j++){
532
                if (i<0 || j<0 || bi.getWidth()<i+1 || bi.getHeight()<j+1)
533
                    continue;
534
//                try{
535
                if (bi.getRGB(i,j)!=0){
536
                    return false;
537
                }
538
//                }catch (Exception e) {
539
//					System.err.println("X= "+i+", Y= "+j);
540
//				}
541
            }
542
        }
543
//        System.err.println("imageX= "+bi.getWidth()+", imageY= "+bi.getHeight());
544
//    	if (bi.getRGB((int)rPixels.getX(),(int)rPixels.getY())!=0){
545
//			return false;
546
//		}
547
//		if (bi.getRGB((int)rPixels.getCenterX(),(int)rPixels.getCenterY())!=0){
548
//			return false;
549
//		}
550
//		if (bi.getRGB((int)rPixels.getMaxX(),(int)rPixels.getMaxY())!=0){
551
//			return false;
552
//		}
553
//		if (bi.getRGB((int)rPixels.getMaxX(),(int)rPixels.getMinY())!=0){
554
//			return false;
555
//		}
556
//		if (bi.getRGB((int)rPixels.getMinX(),(int)rPixels.getMaxY())!=0){
557
//			return false;
558
//		}
559

  
560
        return true;
561
    }
562

  
489 563
    /**
490 564
     * Construcci?n del rect?ngulo
491 565
     *
......
734 808
        Rectangle2D bounds = rect;
735 809
        // if (lyr.getSpatialIndex() == null)
736 810
        if (lyr.getISpatialIndex() == null) {
737
        	return super.queryByRect(rect);
811
            return super.queryByRect(rect);
738 812
        }
739 813
        // Coordinate c1 = new Coordinate(bounds.getMinX(), bounds.getMinY());
740 814
        // Coordinate c2 = new Coordinate(bounds.getMaxX(), bounds.getMaxY());

Also available in: Unified diff