~ubuntu-branches/ubuntu/karmic/batik/karmic

« back to all changes in this revision

Viewing changes to sources/org/apache/batik/bridge/CursorManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev, Onkar Shinde, Matvey Kozhev
  • Date: 2008-07-19 01:03:05 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080719010305-0b24skqy185kdsb9
Tags: 1.7.dfsg-0ubuntu1
[ Onkar Shinde ]
* New upstream version (LP: #147818)
* debian/control
  - Add Sun JDK 1.5 as build dependency. Fixes FTBFS on buildd. (LP: #150484)
    Also add Sun JRE as runtime dependencies.
  - Add ant-optional as build dependency.
  - Add libxml-commons-external-java and libxmlgraphics-commons-java as
    build and runtime dependencies.
  - Add 'Homepage' field and correct the url.
  - Change standards version to 3.8.0.
  - Modify Maintainer value to match the DebianMaintainerField
    specification.
* debian/rules
  - Change JAVA_HOME_DIRS for Sun JDK.
  - Add jar file names to DEB_JARS to match new build requirements.
  - Extract version from changelog.
  - Delete bundled jar files in clean target.
  - Don't use hardcoded version in install target.
  - Add get-orig-source target.
* debian/README.Debian-source
  - Change the fo tag name to the one used for this version.
* debian/watch
  - Change expression to match src distribution.
* debian/patches/
  - 01_build_xml.patch, 02_fix_jar_target.patch - Refresh for current source.
  - 03_fix_lib_dirs.patch - Fix the library and classpath references needed
    for pdf transcoder build.
  - 04_fix_transcoder_pkg.patch - Fix transcoder-pkg target to not copy
    files from other jar files.

[ Matvey Kozhev ]
* debian/changelog:
  - Added ".dfsg" to version.
* debian/control, debian/rules:
  - Build with openjdk-6-jdk, depend on openjdk-6-jre.
  - Added java-6-sun as an alternate build JAVA_HOME directory.
  - Fixed get-orig-source to not include debian/ and delete the source dir,
    and made it delete jars from lib/, as done in the current batik package.
* debian/wrappers.sh:
  - Changed java-7-icedtea reference to java-6-openjdk, as the former has been
    removed back in Hardy.
  - Added newline.
* debian/libbatik-java.install:
  - Added newline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 
3
 
   Copyright 2002-2004  The Apache Software Foundation 
4
 
 
5
 
   Licensed under the Apache License, Version 2.0 (the "License");
6
 
   you may not use this file except in compliance with the License.
7
 
   You may obtain a copy of the License at
 
3
   Licensed to the Apache Software Foundation (ASF) under one or more
 
4
   contributor license agreements.  See the NOTICE file distributed with
 
5
   this work for additional information regarding copyright ownership.
 
6
   The ASF licenses this file to You under the Apache License, Version 2.0
 
7
   (the "License"); you may not use this file except in compliance with
 
8
   the License.  You may obtain a copy of the License at
8
9
 
9
10
       http://www.apache.org/licenses/LICENSE-2.0
10
11
 
36
37
 
37
38
import org.apache.batik.css.engine.SVGCSSEngine;
38
39
import org.apache.batik.css.engine.value.Value;
39
 
import org.apache.batik.dom.svg.XMLBaseSupport;
 
40
import org.apache.batik.dom.AbstractNode;
40
41
import org.apache.batik.dom.util.XLinkSupport;
41
42
import org.apache.batik.ext.awt.image.PadMode;
42
43
import org.apache.batik.ext.awt.image.renderable.AffineRable8Bit;
43
44
import org.apache.batik.ext.awt.image.renderable.Filter;
44
45
import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
 
46
import org.apache.batik.ext.awt.image.spi.BrokenLinkProvider;
45
47
import org.apache.batik.ext.awt.image.spi.ImageTagRegistry;
46
48
import org.apache.batik.gvt.GraphicsNode;
47
49
import org.apache.batik.util.ParsedURL;
 
50
import org.apache.batik.util.Platform;
48
51
import org.apache.batik.util.SVGConstants;
49
52
import org.apache.batik.util.SoftReferenceCache;
50
53
import org.w3c.dom.Element;
56
59
 
57
60
 
58
61
/**
59
 
 * The CursorManager class is a helper class which preloads the cursors 
 
62
 * The CursorManager class is a helper class which preloads the cursors
60
63
 * corresponding to the SVG built in cursors.
61
64
 *
62
65
 * @author <a href="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
63
 
 * @version $Id: CursorManager.java,v 1.17 2005/03/27 08:58:30 cam Exp $
 
66
 * @version $Id: CursorManager.java 594367 2007-11-13 00:40:53Z cam $
64
67
 */
65
68
public class CursorManager implements SVGConstants, ErrorConstants {
66
69
    /**
71
74
    /**
72
75
     * Default cursor when value is not found
73
76
     */
74
 
    public static final Cursor DEFAULT_CURSOR 
 
77
    public static final Cursor DEFAULT_CURSOR
75
78
        = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
76
79
 
77
80
    /**
96
99
     * Static initialization of the cursorMap
97
100
     */
98
101
    static {
 
102
        Toolkit toolkit = Toolkit.getDefaultToolkit();
99
103
        cursorMap = new Hashtable();
100
104
        cursorMap.put(SVG_CROSSHAIR_VALUE,
101
105
                      Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
103
107
                      Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
104
108
        cursorMap.put(SVG_POINTER_VALUE,
105
109
                      Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
106
 
        cursorMap.put(SVG_MOVE_VALUE,
107
 
                      Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
108
110
        cursorMap.put(SVG_E_RESIZE_VALUE,
109
111
                      Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
110
112
        cursorMap.put(SVG_NE_RESIZE_VALUE,
125
127
                      Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
126
128
        cursorMap.put(SVG_WAIT_VALUE,
127
129
                      Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
128
 
        cursorMap.put(SVG_HELP_VALUE, 
129
 
                      Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
130
 
        
 
130
        Cursor moveCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
 
131
        if (Platform.isOSX) {
 
132
            try {
 
133
                Image img = toolkit.createImage
 
134
                    (CursorManager.class.getResource("resources/move.gif"));
 
135
                moveCursor = toolkit.createCustomCursor
 
136
                    (img, new Point(11, 11), "move");
 
137
            } catch (Exception ex) {
 
138
            }
 
139
        }
 
140
        cursorMap.put(SVG_MOVE_VALUE, moveCursor);
 
141
        Cursor helpCursor;
 
142
        try {
 
143
            Image img = toolkit.createImage
 
144
                (CursorManager.class.getResource("resources/help.gif"));
 
145
            helpCursor = toolkit.createCustomCursor
 
146
                (img, new Point(1, 3), "help");
 
147
        } catch (Exception ex) {
 
148
            helpCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
 
149
        }
 
150
        cursorMap.put(SVG_HELP_VALUE, helpCursor);
131
151
    }
132
152
 
133
153
    /**
158
178
        return (Cursor)cursorMap.get(cursorName);
159
179
    }
160
180
 
161
 
 
162
 
 
163
181
    /**
164
182
     * Returns the Cursor corresponding to the input element's cursor property
165
183
     *
175
193
            if (cursorValue.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE
176
194
                &&
177
195
                cursorValue.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
178
 
                // Single Value : should be one of the predefined cursors or 
 
196
                // Single Value : should be one of the predefined cursors or
179
197
                // 'inherit'
180
198
                cursorStr = cursorValue.getStringValue();
181
199
                return convertBuiltInCursor(e, cursorStr);
182
 
            } else if (cursorValue.getCssValueType() == 
 
200
            } else if (cursorValue.getCssValueType() ==
183
201
                       CSSValue.CSS_VALUE_LIST) {
184
202
                int nValues = cursorValue.getLength();
185
203
                if (nValues == 1) {
186
204
                    cursorValue = cursorValue.item(0);
187
 
                    if (cursorValue.getPrimitiveType() == 
 
205
                    if (cursorValue.getPrimitiveType() ==
188
206
                        CSSPrimitiveValue.CSS_IDENT) {
189
207
                        cursorStr = cursorValue.getStringValue();
190
208
                        return convertBuiltInCursor(e, cursorStr);
197
215
                    return convertSVGCursor(e, cursorValue);
198
216
                }
199
217
            }
200
 
        } 
201
 
        
 
218
        }
 
219
 
202
220
        return convertBuiltInCursor(e, cursorStr);
203
221
    }
204
 
    
 
222
 
205
223
    public Cursor convertBuiltInCursor(Element e, String cursorStr) {
206
224
        Cursor cursor = null;
207
225
 
208
226
        // The CSS engine guarantees an non null, non empty string
209
227
        // as the computed value for cursor. Therefore, the following
210
228
        // test is safe.
211
 
        if (cursorStr.charAt(0) == 'a') { 
 
229
        if (cursorStr.charAt(0) == 'a') {
212
230
            //
213
231
            // Handle 'auto' value.
214
232
            //
248
266
                           SVGConstants.SVG_TREF_TAG.equals(tag) ) {
249
267
                    cursor = CursorManager.TEXT_CURSOR;
250
268
                } else if (SVGConstants.SVG_IMAGE_TAG.equals(tag)) {
251
 
                    // Do not change the cursor 
 
269
                    // Do not change the cursor
252
270
                    return null;
253
271
                } else {
254
272
                    cursor = CursorManager.DEFAULT_CURSOR;
260
278
            // Specific, logical cursor
261
279
            cursor = CursorManager.getPredefinedCursor(cursorStr);
262
280
        }
263
 
        
 
281
 
264
282
        return cursor;
265
283
    }
266
284
 
267
 
       
 
285
 
268
286
    /**
269
 
     * Returns a cursor for the given value list. Note that the 
 
287
     * Returns a cursor for the given value list. Note that the
270
288
     * code assumes that the input value has at least two entries.
271
289
     * So the caller should check that before calling the method.
272
290
     * For example, CSSUtilities.convertCursor performs that check.
278
296
            Value cursorValue = l.item(i);
279
297
            if (cursorValue.getPrimitiveType() == CSSPrimitiveValue.CSS_URI) {
280
298
                String uri = cursorValue.getStringValue();
281
 
                
 
299
 
282
300
                // If the uri does not resolve to a cursor element,
283
301
                // then, this is not a type of cursor uri we can handle:
284
302
                // go to the next or default to logical cursor
292
310
                        throw be;
293
311
                    }
294
312
                }
295
 
                
 
313
 
296
314
                if (cursorElement != null) {
297
315
                    // We go an element, check it is of type cursor
298
316
                    String cursorNS = cursorElement.getNamespaceURI();
300
318
                        SVGConstants.SVG_CURSOR_TAG.equals
301
319
                        (cursorElement.getLocalName())) {
302
320
                        Cursor c = convertSVGCursorElement(cursorElement);
303
 
                        if (c != null) { 
 
321
                        if (c != null) {
304
322
                            return c;
305
323
                        }
306
324
                    }
307
325
                }
308
326
            }
309
327
        }
310
 
        
 
328
 
311
329
        // If we got to that point, it means that no cursorElement
312
330
        // produced a valid cursor, i.e., either a format we support
313
331
        // or a valid referenced image (no broken image).
317
335
        if (cursorValue.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
318
336
            cursorStr = cursorValue.getStringValue();
319
337
        }
320
 
          
 
338
 
321
339
        return convertBuiltInCursor(e, cursorStr);
322
340
    }
323
341
 
329
347
        // Try to handle its image.
330
348
        String uriStr = XLinkSupport.getXLinkHref(cursorElement);
331
349
        if (uriStr.length() == 0) {
332
 
            throw new BridgeException(cursorElement, ERR_ATTRIBUTE_MISSING,
 
350
            throw new BridgeException(ctx, cursorElement, ERR_ATTRIBUTE_MISSING,
333
351
                                      new Object[] {"xlink:href"});
334
352
        }
335
353
 
336
 
        String baseURI = XMLBaseSupport.getCascadedXMLBase(cursorElement);
 
354
        String baseURI = AbstractNode.getBaseURI(cursorElement);
337
355
        ParsedURL purl;
338
356
        if (baseURI == null) {
339
357
            purl = new ParsedURL(uriStr);
344
362
        //
345
363
        // Convert the cursor's hot spot
346
364
        //
347
 
        UnitProcessor.Context uctx 
 
365
        UnitProcessor.Context uctx
348
366
            = UnitProcessor.createContext(ctx, cursorElement);
349
367
 
350
368
        String s = cursorElement.getAttributeNS(null, SVG_X_ATTRIBUTE);
370
388
 
371
389
        if (cachedCursor != null) {
372
390
            return cachedCursor;
373
 
        } 
374
 
        
 
391
        }
 
392
 
375
393
        //
376
 
        // Load image into Filter f and transform hotSpot to 
 
394
        // Load image into Filter f and transform hotSpot to
377
395
        // cursor space.
378
396
        //
379
397
        Point2D.Float hotSpot = new Point2D.Float(x, y);
380
 
        Filter f = cursorHrefToFilter(cursorElement, 
381
 
                                      purl, 
 
398
        Filter f = cursorHrefToFilter(cursorElement,
 
399
                                      purl,
382
400
                                      hotSpot);
383
401
        if (f == null) {
384
402
            cursorCache.clearCursor(desc);
385
403
            return null;
386
404
        }
387
 
            
388
 
        // The returned Filter is guaranteed to create a 
 
405
 
 
406
        // The returned Filter is guaranteed to create a
389
407
        // default rendering of the desired size
390
408
        Rectangle cursorSize = f.getBounds2D().getBounds();
391
409
        RenderedImage ri = f.createScaledRendering(cursorSize.width,
410
428
        // The cursor image is now into 'img'
411
429
        //
412
430
        Cursor c = Toolkit.getDefaultToolkit()
413
 
            .createCustomCursor(img, 
 
431
            .createCustomCursor(img,
414
432
                                new Point(Math.round(hotSpot.x),
415
433
                                          Math.round(hotSpot.y)),
416
434
                                purl.toString());
417
435
 
418
436
        cursorCache.putCursor(desc, c);
419
 
        return c;        
 
437
        return c;
420
438
    }
421
439
 
422
440
    /**
423
 
     * Converts the input ParsedURL into a Filter and transforms the 
 
441
     * Converts the input ParsedURL into a Filter and transforms the
424
442
     * input hotSpot point (in image space) to cursor space
425
443
     */
426
 
    protected Filter cursorHrefToFilter(Element cursorElement, 
 
444
    protected Filter cursorHrefToFilter(Element cursorElement,
427
445
                                        ParsedURL purl,
428
446
                                        Point2D hotSpot) {
429
 
        
 
447
 
430
448
        AffineRable8Bit f = null;
431
449
        String uriStr = purl.toString();
432
450
        Dimension cursorSize = null;
434
452
        // Try to load as an SVG Document
435
453
        DocumentLoader loader = ctx.getDocumentLoader();
436
454
        SVGDocument svgDoc = (SVGDocument)cursorElement.getOwnerDocument();
437
 
        URIResolver resolver = new URIResolver(svgDoc, loader);
 
455
        URIResolver resolver = ctx.createURIResolver(svgDoc, loader);
438
456
        try {
439
457
            Element rootElement = null;
440
458
            Node n = resolver.getNode(uriStr, cursorElement);
441
459
            if (n.getNodeType() == Node.DOCUMENT_NODE) {
442
460
                SVGDocument doc = (SVGDocument)n;
443
461
                // FIXX: really should be subCtx here.
444
 
                ctx.initializeDocument(doc); 
 
462
                ctx.initializeDocument(doc);
445
463
                rootElement = doc.getRootElement();
446
464
            } else {
447
 
                throw new BridgeException 
448
 
                    (cursorElement, ERR_URI_IMAGE_INVALID,
 
465
                throw new BridgeException
 
466
                    (ctx, cursorElement, ERR_URI_IMAGE_INVALID,
449
467
                     new Object[] {uriStr});
450
468
            }
451
469
            GraphicsNode node = ctx.getGVTBuilder().build(ctx, rootElement);
452
470
 
453
471
            //
454
 
            // The cursorSize define the viewport into which the 
455
 
            // cursor is displayed. That viewport is platform 
 
472
            // The cursorSize define the viewport into which the
 
473
            // cursor is displayed. That viewport is platform
456
474
            // dependant and is not defined by the SVG content.
457
475
            //
458
476
            float width  = DEFAULT_PREFERRED_WIDTH;
459
477
            float height = DEFAULT_PREFERRED_HEIGHT;
460
 
            UnitProcessor.Context uctx 
 
478
            UnitProcessor.Context uctx
461
479
                = UnitProcessor.createContext(ctx, rootElement);
462
480
 
463
481
            String s = rootElement.getAttribute(SVG_WIDTH_ATTRIBUTE);
465
483
                width = UnitProcessor.svgHorizontalLengthToUserSpace
466
484
                (s, SVG_WIDTH_ATTRIBUTE, uctx);
467
485
            }
468
 
            
 
486
 
469
487
            s = rootElement.getAttribute(SVG_HEIGHT_ATTRIBUTE);
470
488
            if (s.length() != 0) {
471
489
                height = UnitProcessor.svgVerticalLengthToUserSpace
472
490
                    (s, SVG_HEIGHT_ATTRIBUTE, uctx);
473
491
            }
474
492
 
475
 
            cursorSize 
 
493
            cursorSize
476
494
                = Toolkit.getDefaultToolkit().getBestCursorSize
477
495
                (Math.round(width), Math.round(height));
478
 
            
 
496
 
479
497
            // Handle the viewBox transform
480
 
            AffineTransform at 
481
 
                = ViewBox.getPreserveAspectRatioTransform(rootElement,
482
 
                                                          cursorSize.width,
483
 
                                                          cursorSize.height);
 
498
            AffineTransform at = ViewBox.getPreserveAspectRatioTransform
 
499
                (rootElement, cursorSize.width, cursorSize.height, ctx);
484
500
            Filter filter = node.getGraphicsNodeRable(true);
485
501
            f = new AffineRable8Bit(filter, at);
486
502
        } catch (BridgeException ex) {
487
503
            throw ex;
488
504
        } catch (SecurityException ex) {
489
 
            throw new BridgeException(cursorElement, ERR_URI_UNSECURE,
 
505
            throw new BridgeException(ctx, cursorElement, ex, ERR_URI_UNSECURE,
490
506
                                      new Object[] {uriStr});
491
507
        } catch (Exception ex) {
492
 
            /* Nothing to do */ 
 
508
            /* Nothing to do */
493
509
        }
494
510
 
495
511
 
503
519
            }
504
520
 
505
521
            // Check if we got a broken image
506
 
            if (filter.getProperty
507
 
                (SVGBrokenLinkProvider.SVG_BROKEN_LINK_DOCUMENT_PROPERTY) != null) {
 
522
            if (BrokenLinkProvider.hasBrokenLinkProperty(filter)) {
508
523
                return null;
509
 
            } 
510
 
            
 
524
            }
 
525
 
511
526
            Rectangle preferredSize = filter.getBounds2D().getBounds();
512
527
            cursorSize = Toolkit.getDefaultToolkit().getBestCursorSize
513
528
                (preferredSize.width, preferredSize.height);
514
 
            
 
529
 
515
530
            if (preferredSize != null && preferredSize.width >0
516
531
                && preferredSize.height > 0 ) {
517
532
                AffineTransform at = new AffineTransform();
518
 
                if (preferredSize.width > cursorSize.width 
 
533
                if (preferredSize.width > cursorSize.width
519
534
                    ||
520
535
                    preferredSize.height > cursorSize.height) {
521
536
                    at = ViewBox.getPreserveAspectRatioTransform
524
539
                         true,
525
540
                         cursorSize.width,
526
541
                         cursorSize.height);
527
 
                } 
 
542
                }
528
543
                f = new AffineRable8Bit(filter, at);
529
544
            } else {
530
545
                // Invalid Size
542
557
        //
543
558
        // In all cases, clip to the cursor boundaries
544
559
        //
545
 
        Rectangle cursorViewport 
 
560
        Rectangle cursorViewport
546
561
            = new Rectangle(0, 0, cursorSize.width, cursorSize.height);
547
562
 
548
 
        PadRable8Bit cursorImage 
 
563
        PadRable8Bit cursorImage
549
564
            = new PadRable8Bit(f, cursorViewport,
550
565
                               PadMode.ZERO_PAD);
551
566
 
552
567
        return cursorImage;
553
 
            
 
568
 
554
569
    }
555
570
 
556
571
 
590
605
            this.y = y;
591
606
 
592
607
            // Desc is used for hascode as well as for toString()
593
 
            this.desc = this.getClass().getName() + 
 
608
            this.desc = this.getClass().getName() +
594
609
                "\n\t:[" + this.purl + "]\n\t:[" + x + "]:[" + y + "]";
595
610
        }
596
611
 
597
612
        public boolean equals(Object obj) {
598
 
            if (obj == null 
 
613
            if (obj == null
599
614
                ||
600
615
                !(obj instanceof CursorDescriptor)) {
601
616
                return false;
602
617
            }
603
618
 
604
619
            CursorDescriptor desc = (CursorDescriptor)obj;
605
 
            boolean isEqual =  
 
620
            boolean isEqual =
606
621
                this.purl.equals(desc.purl)
607
622
                 &&
608
623
                 this.x == desc.x
609
624
                 &&
610
625
                 this.y == desc.y;
611
 
                 
 
626
 
612
627
            return isEqual;
613
628
        }
614
629
 
622
637
    }
623
638
 
624
639
    /**
625
 
     * Simple extension of the SoftReferenceCache that 
 
640
     * Simple extension of the SoftReferenceCache that
626
641
     * offers typed interface (Kind of needed as SoftReferenceCache
627
642
     * mostly has protected methods).
628
643
     */
634
649
            return (Cursor)requestImpl(desc);
635
650
        }
636
651
 
637
 
        public void putCursor(CursorDescriptor desc, 
 
652
        public void putCursor(CursorDescriptor desc,
638
653
                              Cursor cursor) {
639
654
            putImpl(desc, cursor);
640
655
        }
643
658
            clearImpl(desc);
644
659
        }
645
660
    }
646
 
 
647
 
 
648
 
 
649
661
}