Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 
  */
 package org.apache.batik.svggen;
 
 
Utility class that converts a TexturePaint object into an SVG pattern element

Author(s):
Vincent Hardy
Version:
$Id: SVGTexturePaint.java 476924 2006-11-19 21:13:26Z dvholten $
 
 public class SVGTexturePaint extends AbstractSVGConverter {
    

Parameters:
generatorContext used to build Elements
 
     public SVGTexturePaint(SVGGeneratorContext generatorContext) {
         super(generatorContext);
     }

    
Converts part or all of the input GraphicContext into a set of attribute/value pairs and related definitions

Parameters:
gc GraphicContext to be converted
Returns:
descriptor of the attributes required to represent some or all of the GraphicContext state, along with the related definitions
See also:
SVGDescriptor
 
     public SVGDescriptor toSVG(GraphicContext gc) {
         return toSVG((TexturePaint)gc.getPaint());
     }

    

Parameters:
texture the TexturePaint to be converted
Returns:
a descriptor whose paint value references a pattern. The definition of the pattern in put in the patternDefsMap
 
     public SVGPaintDescriptor toSVG(TexturePaint texture) {
         // Reuse definition if pattern has already been converted
         SVGPaintDescriptor patternDesc = (SVGPaintDescriptor).get(texture);
         Document domFactory = .;
 
         if (patternDesc == null) {
             Rectangle2D anchorRect = texture.getAnchorRect();
             Element patternDef = domFactory.createElementNS(,
                                                             );
             patternDef.setAttributeNS(null,
                                       );
 
             //
             // First, set the pattern anchor
             //
             patternDef.setAttributeNS(null,
                                     doubleString(anchorRect.getX()));
             patternDef.setAttributeNS(null,
                                     doubleString(anchorRect.getY()));
             patternDef.setAttributeNS(null,
                                     doubleString(anchorRect.getWidth()));
             patternDef.setAttributeNS(null,
                                     doubleString(anchorRect.getHeight()));
 
             //
             // Now, add an image element for the image.
             //
             BufferedImage textureImage = texture.getImage();
             //
             // Rescale the image to fit the anchor rectangle
             //
             if (textureImage.getWidth() > 0 &&
                 textureImage.getHeight() > 0){
 
                // Rescale only if necessary
                if(textureImage.getWidth() != anchorRect.getWidth() ||
                   textureImage.getHeight() != anchorRect.getHeight()){
                    // Rescale only if anchor area is not a point or a line
                    if(anchorRect.getWidth() > 0 &&
                       anchorRect.getHeight() > 0){
                        double scaleX =
                            anchorRect.getWidth()/textureImage.getWidth();
                        double scaleY =
                            anchorRect.getHeight()/textureImage.getHeight();
                        BufferedImage newImage
                            = new BufferedImage((int)(scaleX*
                                                      textureImage.getWidth()),
                                                (int)(scaleY*
                                                      textureImage.getHeight()),
                                                .);
                        Graphics2D g = newImage.createGraphics();
                        g.scale(scaleXscaleY);
                        g.drawImage(textureImage, 0, 0, null);
                        g.dispose();
                        textureImage = newImage;
                    }
                }
            }
            // generatorContext.imageHandler.
            // handleImage((RenderedImage)textureImage, imageElement,
            // generatorContext);
            Element patternContent
                = ..createElement
                ();
                ((RenderedImage)textureImage,
                 patternContent,
                 0,
                 0,
                 textureImage.getWidth(),
                 textureImage.getHeight(),
                 );
            patternDef.appendChild(patternContent);
            patternDef.setAttributeNS(null,
                                      ..
                                      generateID());
//            StringBuffer patternAttrBuf = new StringBuffer(URL_PREFIX);
//            patternAttrBuf.append(SIGN_POUND);
//            patternAttrBuf.append(patternDef.getAttributeNS(null, SVG_ID_ATTRIBUTE));
//            patternAttrBuf.append(URL_SUFFIX);
            String patternAttrBuf = 
                    + 
                    + patternDef.getAttributeNS(null)
                    + ;
            patternDesc = new SVGPaintDescriptor(patternAttrBufpatternDef);
            .put(texturepatternDesc);
            .add(patternDef);
        }
        return patternDesc;
    }
New to GrepCode? Check out our FAQ X