001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.geom.Rectangle2D;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.batik.ext.awt.image.CompositeRule;
027: import org.apache.batik.ext.awt.image.PadMode;
028: import org.apache.batik.ext.awt.image.renderable.CompositeRable8Bit;
029: import org.apache.batik.ext.awt.image.renderable.Filter;
030: import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
031: import org.apache.batik.gvt.GraphicsNode;
032: import org.w3c.dom.Element;
033:
034: /**
035: * Bridge class for the <feBlend> element.
036: *
037: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
038: * @version $Id: SVGFeBlendElementBridge.java 475477 2006-11-15 22:44:28Z cam $
039: */
040: public class SVGFeBlendElementBridge extends
041: AbstractSVGFilterPrimitiveElementBridge {
042:
043: /**
044: * Constructs a new bridge for the <feBlend> element.
045: */
046: public SVGFeBlendElementBridge() {
047: }
048:
049: /**
050: * Returns 'feBlend'.
051: */
052: public String getLocalName() {
053: return SVG_FE_BLEND_TAG;
054: }
055:
056: /**
057: * Creates a <tt>Filter</tt> primitive according to the specified
058: * parameters.
059: *
060: * @param ctx the bridge context to use
061: * @param filterElement the element that defines a filter
062: * @param filteredElement the element that references the filter
063: * @param filteredNode the graphics node to filter
064: *
065: * @param inputFilter the <tt>Filter</tt> that represents the current
066: * filter input if the filter chain.
067: * @param filterRegion the filter area defined for the filter chain
068: * the new node will be part of.
069: * @param filterMap a map where the mediator can map a name to the
070: * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
071: * can then access a filter node from the filterMap if they
072: * know its name.
073: */
074: public Filter createFilter(BridgeContext ctx,
075: Element filterElement, Element filteredElement,
076: GraphicsNode filteredNode, Filter inputFilter,
077: Rectangle2D filterRegion, Map filterMap) {
078:
079: // 'mode' attribute - default is 'normal'
080: CompositeRule rule = convertMode(filterElement, ctx);
081:
082: // 'in' attribute
083: Filter in = getIn(filterElement, filteredElement, filteredNode,
084: inputFilter, filterMap, ctx);
085: if (in == null) {
086: return null; // disable the filter
087: }
088:
089: // 'in2' attribute - required
090: Filter in2 = getIn2(filterElement, filteredElement,
091: filteredNode, inputFilter, filterMap, ctx);
092: if (in2 == null) {
093: return null; // disable the filter
094: }
095:
096: Rectangle2D defaultRegion;
097: defaultRegion = (Rectangle2D) in.getBounds2D().clone();
098: defaultRegion.add(in2.getBounds2D());
099:
100: // get filter primitive chain region
101: Rectangle2D primitiveRegion = SVGUtilities
102: .convertFilterPrimitiveRegion(filterElement,
103: filteredElement, filteredNode, defaultRegion,
104: filterRegion, ctx);
105:
106: // build the blend filter
107: List srcs = new ArrayList(2);
108: srcs.add(in2);
109: srcs.add(in);
110:
111: Filter filter = new CompositeRable8Bit(srcs, rule, true);
112: // handle the 'color-interpolation-filters' property
113: handleColorInterpolationFilters(filter, filterElement);
114:
115: filter = new PadRable8Bit(filter, primitiveRegion,
116: PadMode.ZERO_PAD);
117:
118: // update the filter Map
119: updateFilterMap(filterElement, filter, filterMap);
120:
121: return filter;
122: }
123:
124: /**
125: * Converts the 'mode' of the specified feBlend filter primitive.
126: *
127: * @param filterElement the filter feBlend element
128: */
129: protected static CompositeRule convertMode(Element filterElement,
130: BridgeContext ctx) {
131: String rule = filterElement.getAttributeNS(null,
132: SVG_MODE_ATTRIBUTE);
133: if (rule.length() == 0) {
134: return CompositeRule.OVER;
135: }
136: if (SVG_NORMAL_VALUE.equals(rule)) {
137: return CompositeRule.OVER;
138: }
139: if (SVG_MULTIPLY_VALUE.equals(rule)) {
140: return CompositeRule.MULTIPLY;
141: }
142: if (SVG_SCREEN_VALUE.equals(rule)) {
143: return CompositeRule.SCREEN;
144: }
145: if (SVG_DARKEN_VALUE.equals(rule)) {
146: return CompositeRule.DARKEN;
147: }
148: if (SVG_LIGHTEN_VALUE.equals(rule)) {
149: return CompositeRule.LIGHTEN;
150: }
151: throw new BridgeException(ctx, filterElement,
152: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
153: SVG_MODE_ATTRIBUTE, rule });
154: }
155: }
|