| 1 |
<?xml version="1.0"?> |
| 2 |
|
| 3 |
<!-- |
| 4 |
|
| 5 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 6 |
contributor license agreements. See the NOTICE file distributed with |
| 7 |
this work for additional information regarding copyright ownership. |
| 8 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 9 |
(the "License"); you may not use this file except in compliance with |
| 10 |
the License. You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, software |
| 15 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
See the License for the specific language governing permissions and |
| 18 |
limitations under the License. |
| 19 |
|
| 20 |
--> |
| 21 |
|
| 22 |
<!-- ====================================================================== --> |
| 23 |
<!-- author [email protected] --> |
| 24 |
<!-- version $Id$ --> |
| 25 |
<!-- ====================================================================== --> |
| 26 |
|
| 27 |
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd"> |
| 28 |
<document> |
| 29 |
<header> |
| 30 |
<title>Extending Batik</title> |
| 31 |
</header> |
| 32 |
|
| 33 |
<body> |
| 34 |
<p> |
| 35 |
This page provides an overview of the built in extension mechanisms of |
| 36 |
Batik. As an open source project, people can of course make any |
| 37 |
extension they feel is interesting, however Batik has been |
| 38 |
designed with several forms of extension in mind. |
| 39 |
</p> |
| 40 |
|
| 41 |
<p> |
| 42 |
In general, extensions are added through the Service |
| 43 |
Provider Interface mechanism as described in the |
| 44 |
<a href="http://java.sun.com/j2se/1.4/docs/guide/jar/jar.html#Service Provider">jar |
| 45 |
file documentation</a>. This allows for the extension of Batik |
| 46 |
simply by adding a new jar file(s) to the class path, and thus no |
| 47 |
modification of the Batik source is required! |
| 48 |
</p> |
| 49 |
<note> |
| 50 |
If you feel that the Batik team has overlooked an |
| 51 |
important area for extension please let your feelings be |
| 52 |
known on the mailing lists. |
| 53 |
</note> |
| 54 |
|
| 55 |
<section id="customXMLTags"> |
| 56 |
<title>Custom XML elements</title> |
| 57 |
<p> |
| 58 |
First one must ask what it means to support custom |
| 59 |
XML elements? There are three basic options Batik considers: |
| 60 |
</p> |
| 61 |
<dl> |
| 62 |
<dt>Having your elements appear in the DOM tree</dt> |
| 63 |
<dd> |
| 64 |
<p> |
| 65 |
As long as your custom elements are well formed |
| 66 |
XML they will appear in the SVG DOM tree. When rendering Batik |
| 67 |
will skip branches of the tree that use elements it doesn’t know |
| 68 |
about (so even if standard SVG elements are child nodes they will |
| 69 |
not be displayed). Note that you must make use of XML namespaces |
| 70 |
for your personal elements even if you are not planning on |
| 71 |
validating the XML. |
| 72 |
</p> |
| 73 |
<p> |
| 74 |
This can be useful if you want to add extra pieces of |
| 75 |
data into the standard SVG drawing. These might be |
| 76 |
annotations, or other application specific data. In |
| 77 |
general this wouldn’t be particularly useful with |
| 78 |
squiggle (the SVG browser) or the rasterizer, but |
| 79 |
might be very useful if you were writing a custom |
| 80 |
browser, rasterizer, or pre/post processing tools. |
| 81 |
</p> |
| 82 |
</dd> |
| 83 |
|
| 84 |
<dt>Adding functionality to your custom element DOM objects</dt> |
| 85 |
<dd> |
| 86 |
<p> |
| 87 |
If you need your elements to use a custom element subclass |
| 88 |
in the DOM tree (for behavioral or performance reasons) then you |
| 89 |
need to provide an <a href="#domExtension">extension to the Batik |
| 90 |
DOM</a>. |
| 91 |
</p> |
| 92 |
<p> |
| 93 |
Doing this gives you the opportunity to override the standard |
| 94 |
methods on DOM elements, or to provide additional methods to |
| 95 |
your DOM elements. For example, you may wish to add specialized |
| 96 |
get and set methods for attributes on your custom elements, so |
| 97 |
that they can be manipulated more easily than just using the |
| 98 |
string-based <code>getAttribute</code> and |
| 99 |
<code>setAttribute</code> methods provided by DOM Core. |
| 100 |
</p> |
| 101 |
</dd> |
| 102 |
|
| 103 |
<dt>Having your custom elements be rendered</dt> |
| 104 |
<dd> |
| 105 |
<p> |
| 106 |
Probably the most common reason to develop custom elements is to |
| 107 |
add new rendering primitives to the SVG language. In this case |
| 108 |
you must provide an <a href="#bridgeExtension">extension to the |
| 109 |
Batik bridge</a>. The bridge extension is resposible |
| 110 |
for constructing the class(es) that will handle the rendering of |
| 111 |
the new primitive in Batik. |
| 112 |
</p> |
| 113 |
<p> |
| 114 |
In most cases it will also be necessary to write a DOM |
| 115 |
extension to make the element behave like other SVG elements |
| 116 |
(most notably for support of styling). |
| 117 |
</p> |
| 118 |
</dd> |
| 119 |
</dl> |
| 120 |
|
| 121 |
<section id="domExtension"> |
| 122 |
<title>Writing a Batik DOM extension</title> |
| 123 |
<p> |
| 124 |
The ability to extend the elements used in the SVG DOM tree allows |
| 125 |
users to provide implementations for nodes that can be used in |
| 126 |
place of Batik’s default implementation of a node. This may be |
| 127 |
done for a variety of reasons, but is most commonly done to |
| 128 |
extend the behavior of standard node calls (such as |
| 129 |
to include styling in attribute lookup), or to |
| 130 |
implement the DOM interface for an element. |
| 131 |
</p> |
| 132 |
<p> |
| 133 |
The key class for building the DOM tree is |
| 134 |
<a class="class" href="../javadoc/org/apache/batik/dom/ExtensibleSVGDOMImplementation.html">org.apache.batik.dom.ExtensibleSVGDOMImplementation</a>. |
| 135 |
When an instance of this class is constructed it searches for |
| 136 |
instances of the |
| 137 |
<a class="class" href="../javadoc/org/apache/batik/dom/svg/DomExtension.html">org.apache.batik.dom.svg.DomExtension</a> |
| 138 |
Service Provider Interface. It then calls the |
| 139 |
<code>registerTags</code> method, passing itself as the only |
| 140 |
parameter. This method typically would typically call |
| 141 |
<code>registerCustomElementFactory</code> for each element that |
| 142 |
it wishes to handle. |
| 143 |
</p> |
| 144 |
<p> |
| 145 |
With Batik the most likely reason to extend a node is to provide |
| 146 |
proper CSS styling of the node attributes. To this end Batik |
| 147 |
provides a class you can extend: |
| 148 |
<a class="class" href="../javadoc/org/apache/batik/extension/PrefixableStylableExtensionElement.html">org.apache.batik.extension.PrefixableStylableExtensionElement</a>. |
| 149 |
If you derive a new DOM class from this class you are only |
| 150 |
required to implement three methods: <code>getLocalName</code>, |
| 151 |
<code>getNamespaceURI</code>, and <code>newNode</code> |
| 152 |
(plus constructors). If all you want is proper style support |
| 153 |
(commonly the case) then you are done implementing your element |
| 154 |
at this point. |
| 155 |
</p> |
| 156 |
|
| 157 |
<p> |
| 158 |
The distribution comes with a number of examples: |
| 159 |
</p> |
| 160 |
<ul> |
| 161 |
<li><code>org.apache.batik.extension.svg.BatikStarElement</code></li> |
| 162 |
<li><code>org.apache.batik.extension.svg.BatikRegularPolygonElement</code></li> |
| 163 |
<li><code>org.apache.batik.extension.svg.BatikHistogramNormalizationElement</code></li> |
| 164 |
<li><code>org.apache.batik.extension.svg.SolidColorElement</code></li> |
| 165 |
<li><code>org.apache.batik.extension.svg.ColorSwitchElement</code></li> |
| 166 |
</ul> |
| 167 |
<p> |
| 168 |
Included with these examples is |
| 169 |
<code>org.apache.batik.extension.svg.BatikDomExtension</code>, |
| 170 |
which is the required instance of <code>DomExtension</code> |
| 171 |
used to register the elements with the |
| 172 |
<code>ExtensibleSVGDOMImplementation</code>. |
| 173 |
</p> |
| 174 |
<p> |
| 175 |
If your new element requires new “presentation attributes” |
| 176 |
(XML attributes that can be modified through CSS, or, depending |
| 177 |
on your viewpoint, the other way around—CSS properties that |
| 178 |
can be specified using XML attributes), you will also need to |
| 179 |
extend the CSS engine. This can be done by registering a |
| 180 |
custom CSS value factory. Both of the color examples do |
| 181 |
this (see <code>BatikDomExtension</code>). |
| 182 |
</p> |
| 183 |
</section> |
| 184 |
|
| 185 |
<section id="bridgeExtension"> |
| 186 |
<title>Writing a Batik bridge extension</title> |
| 187 |
<p> |
| 188 |
Before you write a bridge extension it may be useful to |
| 189 |
understand what role the bridge package plays in |
| 190 |
Batik. The bridge package is responsible for creating and |
| 191 |
maintaining elements in the Graphics Vector Toolkit (GVT) |
| 192 |
tree based on the corresponding element in the SVG DOM. This |
| 193 |
is done because, for a variety of reasons, the SVG DOM is not |
| 194 |
well suited for rendering, thus the GVT tree is used for all |
| 195 |
rendering and transcoding operations. |
| 196 |
</p> |
| 197 |
<p> |
| 198 |
The key class for managing this link is the |
| 199 |
<a class="class" href="../javadoc/org/apache/batik/bridge/BridgeContext.html">BridgeContext</a>. |
| 200 |
This class maintains an association between a element name |
| 201 |
with namespace and a particular bridge instance that will |
| 202 |
handle it. The work of constructing the proper entity or |
| 203 |
entities in the GVT tree is then deferred to the |
| 204 |
<a class="class" href="../javadoc/org/apache/batik/bridge/Bridge.html">Bridge</a> |
| 205 |
registered for a particular element. If no bridge is |
| 206 |
regiestered nothing is done. |
| 207 |
</p> |
| 208 |
<p> |
| 209 |
New associations can be added by implementors of the |
| 210 |
<a class="class" href="../javadoc/org/apache/batik/bridge/BridgeExtension.html">BridgeExtension</a> |
| 211 |
Service Provider Interface. This interface has a number |
| 212 |
of methods that provide information about the particular |
| 213 |
extension being registered (including contact information, |
| 214 |
and the list of implemented extensions). It also has a |
| 215 |
<code>registerTags</code> method which is responsible for |
| 216 |
registering the bridge instances with a |
| 217 |
<code>BridgeContext</code>. All the built-in |
| 218 |
bridges are bundled together with a |
| 219 |
<code>BridgeExtension</code> (the |
| 220 |
<a class="class" href="../javadoc/org/apache/batik/bridge/SVGBridgeExtension.html">org.apache.batik.bridge.SVGBridgeExtension</a> |
| 221 |
class), as are the example extensions |
| 222 |
(<a class="class" href="../javadoc/org/apache/batik/extension/svg/BatikBridgeExtension.html">org.apache.batik.extension.svg</a>), |
| 223 |
so these are both good places to start. |
| 224 |
</p> |
| 225 |
<p> |
| 226 |
The <code>Bridge</code> interface itself is very simple. |
| 227 |
It only includes methods to get the namespace and local |
| 228 |
name of the element the bridge is responsible for. This |
| 229 |
interface is then extended for each of the major |
| 230 |
concepts present in SVG: |
| 231 |
</p> |
| 232 |
<dl> |
| 233 |
<dt><a href="#graphicsNodeBridge">GraphicsNodeBridge</a></dt> |
| 234 |
<dd> |
| 235 |
<p> |
| 236 |
These are probably the most common SVG elements, as |
| 237 |
they represent graphic elements in the “visible” SVG tree. |
| 238 |
These are the elements most other bridges modify in some way |
| 239 |
(by clipping, masking, filtering, etc). |
| 240 |
</p> |
| 241 |
<p> |
| 242 |
<em>Example SVG elements:</em> |
| 243 |
<code>svg</code>, <code>g</code>, <code>path</code>, |
| 244 |
<code>rect</code>. |
| 245 |
</p> |
| 246 |
<p> |
| 247 |
<em>Example extension bridges:</em> |
| 248 |
<code>BatikRegularPolygonElementBridge</code>, |
| 249 |
<code>BatikStarElementBridge</code>. |
| 250 |
</p> |
| 251 |
</dd> |
| 252 |
<dt>FilterBridge</dt> |
| 253 |
<dd> |
| 254 |
<p> |
| 255 |
This handles the SVG <code>filter</code> element. If you |
| 256 |
wanted to implement a new element that could be |
| 257 |
referenced from the <code>filter</code> attribute on an SVG |
| 258 |
graphics node then you would need to subclass this |
| 259 |
bridge. However, adding new types of filters to the |
| 260 |
existing SVG <code>filter</code> element is accomplished via the |
| 261 |
FilterPrimitiveBridge. |
| 262 |
</p> |
| 263 |
<p> |
| 264 |
<em>Example SVG element:</em> <code>filter</code> |
| 265 |
</p> |
| 266 |
</dd> |
| 267 |
<dt><a href="#filterPrimitiveBridge">FilterPrimitiveBridge</a></dt> |
| 268 |
<dd> |
| 269 |
<p> |
| 270 |
This constructs an element in the filter chain |
| 271 |
applied to an SVG graphics node. |
| 272 |
</p> |
| 273 |
<p> |
| 274 |
<em>Example SVG elements:</em> |
| 275 |
<code>feBlend</code>, <code>feCompose</code>, |
| 276 |
<code>feGaussianBlur</code>. |
| 277 |
</p> |
| 278 |
<p> |
| 279 |
<em>Example extension bridge:</em> |
| 280 |
<code>BatikHistogramNormalizationElementBridge</code> |
| 281 |
</p> |
| 282 |
</dd> |
| 283 |
<dt><a href="#paintBridge">PaintBridge</a></dt> |
| 284 |
<dd> |
| 285 |
<p> |
| 286 |
This constructs a Java |
| 287 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Paint.html">Paint</a> |
| 288 |
object to be used in filling or stroking graphic elements. |
| 289 |
</p> |
| 290 |
<p> |
| 291 |
<em>Example SVG elements:</em> |
| 292 |
<code>gradient</code>, <code>pattern</code>. |
| 293 |
</p> |
| 294 |
<p> |
| 295 |
<em>Example extension bridge:</em> |
| 296 |
<code>ColorSwitchBridge</code>. |
| 297 |
</p> |
| 298 |
</dd> |
| 299 |
<dt>ClipBridge</dt> |
| 300 |
<dd> |
| 301 |
<p> |
| 302 |
This constructs a |
| 303 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/ClipRable.html">ClipRable</a> |
| 304 |
to apply to a graphics node. This provides a path that data |
| 305 |
is clipped to. |
| 306 |
</p> |
| 307 |
<p> |
| 308 |
<em>Example SVG element:</em> <code>clipPath</code>. |
| 309 |
</p> |
| 310 |
</dd> |
| 311 |
<dt>MarkerBridge</dt> |
| 312 |
<dd> |
| 313 |
<p> |
| 314 |
This constructs a |
| 315 |
<a class="class" href="../javadoc/org/apache/batik/gvt/Marker.html">Marker</a> |
| 316 |
for annotating the path of a graphics node. |
| 317 |
</p> |
| 318 |
<p> |
| 319 |
<em>Example SVG element:</em> <code>marker</code>. |
| 320 |
</p> |
| 321 |
</dd> |
| 322 |
<dt>MaskBridge</dt> |
| 323 |
<dd> |
| 324 |
<p> |
| 325 |
This constructs a mask filter to apply to a graphics |
| 326 |
node. Mask filters typically modify the alpha channel of the |
| 327 |
graphics node output to make portions fully or partially |
| 328 |
transparent that wouldn’t be otherwise. |
| 329 |
</p> |
| 330 |
<p> |
| 331 |
<em>Example SVG element:</em> <code>mask</code>. |
| 332 |
</p> |
| 333 |
</dd> |
| 334 |
</dl> |
| 335 |
|
| 336 |
<p> |
| 337 |
Extension writers are free to work with any of the |
| 338 |
above bridges, however the three most common are |
| 339 |
likely to be the <code>GraphicsNodeBridge</code>, the |
| 340 |
<code>FilterPrimitiveBridge</code>, and the |
| 341 |
<code>PaintBridge</code> (each of which has example |
| 342 |
extensions available for inspection). Each of these interfaces |
| 343 |
has several very useful subclasses that handle much of the |
| 344 |
common behavior among elements. |
| 345 |
</p> |
| 346 |
|
| 347 |
<p> |
| 348 |
In some simple cases it is possible to provide only an |
| 349 |
extension to the bridge and achieve your desired effect, |
| 350 |
however in most cases you will find that for your |
| 351 |
element to behave like a normal SVG element (for |
| 352 |
example, to support styling) you will need to provide |
| 353 |
a DOM extension as well. |
| 354 |
</p> |
| 355 |
|
| 356 |
<section id="graphicsNodeBridge"> |
| 357 |
<title>GraphicsNodeBridge</title> |
| 358 |
<p> |
| 359 |
The graphics node bridge is oriented around |
| 360 |
constructing a new |
| 361 |
<a class="class" href="../javadoc/org/apache/batik/gvt/GraphicsNode.html">GraphicsNode</a> |
| 362 |
in the GVT tree. The <code>GraphicsNode</code> is the basic |
| 363 |
object that makes up the GVT tree. Each |
| 364 |
<code>GraphicsNode</code> has a <code>paint</code> method that |
| 365 |
is responsible for painting the object (including considering |
| 366 |
clipping, masking, filtering, and opacity for the |
| 367 |
node). |
| 368 |
</p> |
| 369 |
<p> |
| 370 |
If you want to you can implement the |
| 371 |
<a class="class" href="../javadoc/org/apache/batik/bridge/GraphicsNodeBridge.html">GraphicsNodeBridge</a> |
| 372 |
interface directly, or you can subclass the |
| 373 |
<a class="class" href="../javadoc/org/apache/batik/bridge/AbstractGraphicsNodeBridge.html">AbstractGraphicsNodeBridge</a> |
| 374 |
class. This gives you the most flexibility since you can |
| 375 |
construct your new subclass of <code>GraphicsNode</code>, where |
| 376 |
you can implement the paint method to do essentially anything |
| 377 |
you want. This is quite involved, however, and the steps |
| 378 |
necessary to create a full <code>GraphicsNodeBridge</code> |
| 379 |
are not detailed here. |
| 380 |
</p> |
| 381 |
<p> |
| 382 |
However, if you just want to generate a custom |
| 383 |
filled or stroked shape the easiest way is to |
| 384 |
subclass one of the following two classes. In this |
| 385 |
case you are essentially only responsible for |
| 386 |
constructing a standard Java |
| 387 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Shape.html">Shape</a> |
| 388 |
object to describe the desired area to operate on: |
| 389 |
</p> |
| 390 |
<dl> |
| 391 |
<dt>SVGShapeElementBridge</dt> |
| 392 |
<dd> |
| 393 |
<p> |
| 394 |
Subclasses of this class only need to implement |
| 395 |
<code>buildShape</code>, <code>getNamespaceURI</code>, |
| 396 |
and <code>getLocalName</code>. <code>buildShape</code> |
| 397 |
generally constructs a <code>Shape</code> object and sets it |
| 398 |
on the provided <code>shapeNode</code> object, however it |
| 399 |
may adjust other features of the given shape node. |
| 400 |
</p> |
| 401 |
</dd> |
| 402 |
<dt>SVGDecoratedShapeElementBridge</dt> |
| 403 |
<dd> |
| 404 |
<p> |
| 405 |
This is very similar to <code>SVGShapeElementBridge</code>, |
| 406 |
except that it also handles the standard marker properties. |
| 407 |
Markers will be placed at the end of each segment of |
| 408 |
the path that describes the shape. |
| 409 |
</p> |
| 410 |
</dd> |
| 411 |
</dl> |
| 412 |
<p> |
| 413 |
If you decide that you need to implement a new subclass |
| 414 |
of <code>GraphicsNode</code> it is strongly suggested that |
| 415 |
you extend |
| 416 |
<a class="class" href="../javadoc/org/apache/batik/gvt/AbstractGraphicsNode.html">AbstractGraphicsNode</a>, |
| 417 |
as this class does much of the work to behave like other |
| 418 |
rendered elements in SVG (like clipping, filtering and |
| 419 |
masking). In this case you implement the |
| 420 |
<code>primitivePaint</code> method instead of the |
| 421 |
<code>paint</code> method. |
| 422 |
</p> |
| 423 |
</section> |
| 424 |
|
| 425 |
<section id="filterPrimitiveBridge"> |
| 426 |
<title>FilterPrimitiveBridge</title> |
| 427 |
<p> |
| 428 |
The |
| 429 |
<a class="class" href="../javadoc/org/apache/batik/bridge/FilterPrimitiveBridge.html">FilterPrimitiveBridge</a> |
| 430 |
is concerned with the construction of individual elements of the |
| 431 |
filter chain. Unlike graphics nodes, which generally just |
| 432 |
draw new objects on top of the destination, filters take |
| 433 |
existing image data and modify it to apply effects. |
| 434 |
</p> |
| 435 |
<p> |
| 436 |
This part of GVT rendering is based on the Java2D |
| 437 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/renderable/RenderableImage.html">java.awt.image.renderable.RenderableImage</a> |
| 438 |
and <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/renderable/RenderedImage.html">java.awt.image.RenderedImage</a> |
| 439 |
interfaces. These provide a convenient framework to handle |
| 440 |
image processing (an inherently resolution dependent |
| 441 |
operation) in the resolution independent system defined by |
| 442 |
SVG. |
| 443 |
</p> |
| 444 |
<p> |
| 445 |
The <code>org.apache.batik.ext.awt.image</code> package |
| 446 |
hierarchy contains a large set of generally useful extensions |
| 447 |
to the core JDK classes and methods, that help to implement |
| 448 |
SVG-related graphics operations. |
| 449 |
</p> |
| 450 |
<p> |
| 451 |
Note that the <code>FilterPrimitiveBridge</code> is invoked |
| 452 |
once for each reference to the <code>filter</code> element that |
| 453 |
the filter primitive is part of. So if a filter effect is used |
| 454 |
a half dozen times the <code>createFilter</code> method will be |
| 455 |
called a half dozen times, even though the element may only |
| 456 |
appear once in the file. This means that it is safe for the |
| 457 |
filters returned to be “fixed” for a particular |
| 458 |
<code>GraphicsNode</code> being filtered. |
| 459 |
</p> |
| 460 |
<p> |
| 461 |
You will notice that Batik uses extended versions of |
| 462 |
the standard <code>RenderableImage</code> and <code>RenderedImage</code> |
| 463 |
interfaces to provide additional information about |
| 464 |
surrounding requirements for operations as well as a few |
| 465 |
convenience methods. These interfaces are called: |
| 466 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/Filter.html">org.apache.batik.ext.awt.image.renderable.Filter</a> |
| 467 |
and |
| 468 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/CacheableRed.html">org.apache.batik.ext.awt.image.rendered.CacheableRed</a>. |
| 469 |
Batik contains simple wrapper classes that can take the default |
| 470 |
JDK <code>RenderableImage</code> and <code>RenderedImage</code> |
| 471 |
interfaces. Within the codebase the naming convention “Red” for |
| 472 |
classes implementing <code>RenderedImage</code> and “Rable” for classes |
| 473 |
implementing <code>RenderableImage</code> is commonly |
| 474 |
used (“Red” is to be pronounced like the color, and |
| 475 |
“Rable” is to be pronounced like “horrible” with a |
| 476 |
silent “h”). |
| 477 |
</p> |
| 478 |
<p> |
| 479 |
The <code>FilterPrimitiveBridge</code> has only |
| 480 |
one method, <code>createFilter</code>, that must |
| 481 |
construct an instance of <code>Filter</code> to perform |
| 482 |
the required operation. This is still a fairly complex task |
| 483 |
given the general need to support accessing the various |
| 484 |
standard sources of image data. To this end there is |
| 485 |
a provided subclass, |
| 486 |
<a class="class" href="../javadoc/org/apache/batik/bridge/AbstractSVGFilterPrimitiveElementBridge.html">AbstractSVGFilterPrimitiveElementBridge</a>, |
| 487 |
that provides convenience methods to handle many common tasks. |
| 488 |
</p> |
| 489 |
<p> |
| 490 |
Generally the bulk of the work in writing a filter |
| 491 |
extension is the writing of the concrete <code>Filter</code> |
| 492 |
class, not tying it into the GVT tree. Batik does contain |
| 493 |
several base classes that make this processes a bit |
| 494 |
easier: |
| 495 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/AbstractRable.html">org.apache.batik.ext.awt.image.renderable.AbstractRable</a>, |
| 496 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/AbstractRed.html">org.apache.batik.ext.awt.image.rendered.AbstractRed</a>, |
| 497 |
and |
| 498 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/AbstractTiledRed.html">org.apache.batik.ext.awt.image.rendered.AbstractTiledRed</a>, |
| 499 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/TiledRed.html">TiledRed</a> |
| 500 |
ties into the Batik tile cache (use this with caution as it is a |
| 501 |
complex area of the Batik code). |
| 502 |
</p> |
| 503 |
<p> |
| 504 |
The <code>org.apache.batik.ext.awt.image.rendered</code> |
| 505 |
and <code>org.apache.batik.ext.awt.image.renderable</code> |
| 506 |
packages contain quite a number of fairly general examples |
| 507 |
covering most common cases, please refer to them for more detail. |
| 508 |
</p> |
| 509 |
</section> |
| 510 |
|
| 511 |
<section id="paintBridge"> |
| 512 |
<title>PaintBridge</title> |
| 513 |
<p> |
| 514 |
The |
| 515 |
<a class="class" href="../javadoc/org/apache/batik/bridge/PaintBridge.html">PaintBridge</a> |
| 516 |
constructs an instance of <code>java.awt.Paint</code> to be used |
| 517 |
to fill or stroke shapes/text (part of the paint server |
| 518 |
architecture of SVG). |
| 519 |
</p> |
| 520 |
<p> |
| 521 |
Like the filter primitive bridge, the <code>PaintBridge</code> |
| 522 |
is invoked for each reference to the paint. This makes |
| 523 |
it possible to customize the <code>Paint</code> returned for the |
| 524 |
particular element to be painted. |
| 525 |
</p> |
| 526 |
<p> |
| 527 |
This is how gradients and patterns are implemented |
| 528 |
in Batik, so it is possible to construct rather |
| 529 |
complex paint effects through this mechanism. |
| 530 |
</p> |
| 531 |
<p> |
| 532 |
For paints you are mostly on your own, because unlike |
| 533 |
the other cases there aren’t any really generally useful |
| 534 |
base classes to derive off, the closest is the |
| 535 |
<a class="class" href="../javadoc/org/apache/batik/bridge/AbstractSVGGradientElementBridge.html">AbstractSVGGradientElementBridge</a>, |
| 536 |
which is used to handle most of the radial and linear gradient |
| 537 |
attributes. |
| 538 |
</p> |
| 539 |
<p> |
| 540 |
The existing gradient paint implementations are in the |
| 541 |
<code>org.apache.batik.ext.awt</code>, and the pattern |
| 542 |
implementation is in <code>org.apache.batik.gvt</code> since it |
| 543 |
requires access to GVT internals. |
| 544 |
</p> |
| 545 |
</section> |
| 546 |
</section> |
| 547 |
</section> |
| 548 |
|
| 549 |
<section id="imageTagFormats"> |
| 550 |
<title>New image file formats</title> |
| 551 |
<p> |
| 552 |
When Batik encounters an <code>image</code> element and it |
| 553 |
determines the element does not reference an SVG file, it defers |
| 554 |
the loading of the referenced image to |
| 555 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/spi/ImageTagRegistry.html">org.apache.batik.ext.awt.image.spi.ImageTagRegistry</a>. |
| 556 |
This class maintains a list of |
| 557 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/spi/RegistryEntry.html">RegistryEntry</a>s, |
| 558 |
generally one for each format. |
| 559 |
</p> |
| 560 |
<p> |
| 561 |
Since the formats supported natively by Batik are also |
| 562 |
implemented through this mechanism. The |
| 563 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/codec/jpeg/JPEGRegistryEntry.html">JPEGRegistryEntry</a> |
| 564 |
and |
| 565 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/codec/png/PNGRegistryEntry.html">PNGRegistryEntry</a> |
| 566 |
classes should be used as good references for extensions. |
| 567 |
</p> |
| 568 |
<section> |
| 569 |
<title>RegistryEntry</title> |
| 570 |
<p> |
| 571 |
There are currently two flavors of <code>RegistryEntry</code>: |
| 572 |
</p> |
| 573 |
<dl> |
| 574 |
<dt>URLRegistryEntry</dt> |
| 575 |
<dd> |
| 576 |
<p> |
| 577 |
A |
| 578 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/spi/URLRegistryEntry.html">URLRegistryEntry</a> |
| 579 |
takes a |
| 580 |
<a class="class" href="../javadoc/org/apache/batik/util/ParsedURL.html">ParsedURL</a> |
| 581 |
and tries to decide if the URL is intended for it. This group |
| 582 |
of entries is mostly intended to handle alternate network |
| 583 |
protocols. It can also be useful for interfacing with |
| 584 |
libraries that want a URL instead of a stream. |
| 585 |
</p> |
| 586 |
</dd> |
| 587 |
<dt>StreamRegistryEntry</dt> |
| 588 |
<dd> |
| 589 |
<p> |
| 590 |
A |
| 591 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/spi/StreamRegistryEntry.html">StreamRegistryEntry</a> |
| 592 |
works with a markable |
| 593 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html">InputStream</a>. |
| 594 |
This is the preferred form of registry entry as it generally |
| 595 |
avoids opening a potentially expensive connection multiple |
| 596 |
times, instead it opens the stream once and relies on mark and |
| 597 |
reset to allow entries to check the stream. |
| 598 |
</p> |
| 599 |
</dd> |
| 600 |
</dl> |
| 601 |
</section> |
| 602 |
|
| 603 |
<section> |
| 604 |
<title>Helper classes</title> |
| 605 |
<p> |
| 606 |
There exists quite a number of classes to assist in |
| 607 |
implementing a <code>RegistryEntry</code>. It is strongly |
| 608 |
recommended that you review these classes and make use |
| 609 |
of them where appropriate. They will likely save you |
| 610 |
time and improve the integration with Batik. |
| 611 |
</p> |
| 612 |
<dl> |
| 613 |
<dt>MagicNumberRegistryEntry</dt> |
| 614 |
<dd> |
| 615 |
<p> |
| 616 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/spi/MagicNumberRegistryEntry.html">MagicNumberRegistryEntry</a> |
| 617 |
is an abstract class that can handle the |
| 618 |
<code>isCompatibleStream</code> method for formats that |
| 619 |
make use of “magic numbers.” Magic numbers are a well |
| 620 |
known sequence of bytes at a well known offset in the |
| 621 |
file, that are commonly used to identify image file formats. |
| 622 |
</p> |
| 623 |
</dd> |
| 624 |
<dt>RedRable</dt> |
| 625 |
<dd> |
| 626 |
<p> |
| 627 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/RedRable.html">RedRable</a> |
| 628 |
takes any <code>java.awt.image.RenderedImage</code> and wraps it |
| 629 |
into a <code>Filter</code> (Batik’s subinterface of |
| 630 |
<code>RenderableImage</code>). This is very useful for |
| 631 |
<em>single resolution</em> file formats. |
| 632 |
</p> |
| 633 |
</dd> |
| 634 |
<dt>DeferRable</dt> |
| 635 |
<dd> |
| 636 |
<p> |
| 637 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/DeferRable.html">DeferRable</a> |
| 638 |
allows one to load the image in a background |
| 639 |
thread, rather than hold up the construction of the |
| 640 |
GVT tree while reading the image (useful since |
| 641 |
reading the image is generally I/O bound, so it |
| 642 |
makes a good background task). This is used by most of |
| 643 |
the current image readers. |
| 644 |
</p> |
| 645 |
</dd> |
| 646 |
<dt>AbstractRable</dt> |
| 647 |
<dd> |
| 648 |
<p> |
| 649 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/renderable/AbstractRable.html">AbstractRable</a> |
| 650 |
is an abstract base class that makes it relatively |
| 651 |
easy to implement the Filter interface. |
| 652 |
</p> |
| 653 |
</dd> |
| 654 |
<dt>AbstractRed</dt> |
| 655 |
<dd> |
| 656 |
<p> |
| 657 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/AbstractRed.html">AbstractRed</a> |
| 658 |
is an abstract base class that makes it relatively |
| 659 |
easy to implement the |
| 660 |
<a class="class" href="../javadoc/org/apache/batik/ext/awt/image/rendered/CacheableRed.html">CacheableRed</a> |
| 661 |
interface (Batik's subclass of <code>RenderedImage</code>). |
| 662 |
</p> |
| 663 |
</dd> |
| 664 |
</dl> |
| 665 |
</section> |
| 666 |
</section> |
| 667 |
|
| 668 |
<section id="urlProtocols"> |
| 669 |
<title>New URL protocols</title> |
| 670 |
<p> |
| 671 |
For a variety of reasons (not the least of which is the |
| 672 |
heavy use of the <code>data:</code> URL protocol in SVG), several |
| 673 |
parts of Batik use a |
| 674 |
<a class="class" href="../javadoc/org/apache/batik/util/ParsedURL.html">ParsedURL</a> |
| 675 |
instead of the JDK’s |
| 676 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html">java.net.URL</a> |
| 677 |
class. |
| 678 |
</p> |
| 679 |
<p> |
| 680 |
<code>ParsedURL</code> offers a few advantages over the JDK’s |
| 681 |
<code>URL</code> class. First, it is designed to make minimal use of |
| 682 |
exceptions, so it is possible to use it to parse a malformed |
| 683 |
URL and get “the good parts”. Second, it is extensible, so |
| 684 |
support for new URL protocols can be added, even those that |
| 685 |
change the normal parsing rules for URLs (such as our friend the |
| 686 |
<code>data:</code> protocol). Third, it can automatically check a |
| 687 |
when a stream can be opened for common compression types and decode |
| 688 |
them for you (this behavior can also be bypassed if needed). |
| 689 |
</p> |
| 690 |
<p> |
| 691 |
The service class is |
| 692 |
<a class="class" href="../javadoc/org/apache/batik/util/ParsedURLProtocolHandler.html">org.apache.batik.util.ParsedURLProtocolHandler</a>. |
| 693 |
This interface consists of three methods: one returns the protocol |
| 694 |
to be handled, one is for parsing an absolute URL string and |
| 695 |
one is for parsing relative URL strings. Both the parsing |
| 696 |
methods return an object of type |
| 697 |
<a class="class" href="../javadoc/org/apache/batik/util/ParsedURLData.html">ParsedURLData</a> |
| 698 |
(the instance may of course be a subclass of |
| 699 |
<code>ParsedURLData</code>). |
| 700 |
</p> |
| 701 |
<p> |
| 702 |
The <code>ParsedURLData</code> class holds all the data and |
| 703 |
implements all the stream handling commands for the |
| 704 |
<code>ParsedURL</code> class. This allows |
| 705 |
<code>ParsedURLProtocolHandler</code>s to return custom subclasses |
| 706 |
for particular protocols. |
| 707 |
</p> |
| 708 |
</section> |
| 709 |
|
| 710 |
<section id="interpreters"> |
| 711 |
<title>Additional script interpreters</title> |
| 712 |
|
| 713 |
<p> |
| 714 |
While conforming SVG browsers need support only ECMAScript as a scripting |
| 715 |
language, Batik can support any scripting language given the right glue |
| 716 |
to connect it to the rest of the system. |
| 717 |
</p> |
| 718 |
<p> |
| 719 |
Script interpreters are also handled via the Service Provider Interface, |
| 720 |
The interface that needs to be implemented to expose a new interpreter |
| 721 |
to Batik is |
| 722 |
<a class="class" href="../javadoc/org/apache/batik/script/InterpreterFactory.html">InterpreterFactory</a>. |
| 723 |
This class has two methods: <code>getMimeType</code>, which returns |
| 724 |
a string that specifies what script type this intepreter handles |
| 725 |
(specifically, what the <code>type</code> attribute of |
| 726 |
<code>script</code> elements must be for them to be handled by this |
| 727 |
intepreter), and <code>createInterpreter</code>, which creates an |
| 728 |
instance of the |
| 729 |
<a class="class" href="../javadoc/org/apache/batik/script/Interpreter.html">Interpreter</a> |
| 730 |
interface. |
| 731 |
</p> |
| 732 |
<p> |
| 733 |
Batik comes with implementations of <code>Interpreter</code> and |
| 734 |
<code>IntepreterFactory</code> to support TCL and Python script in |
| 735 |
SVG documents, if the Jacl and Jython distributions are installed, |
| 736 |
respectively. See the classes in the |
| 737 |
<code>org.apache.batik.script.jacl</code> and |
| 738 |
<code>org.apache.batik.script.jython</code> packages to guidance |
| 739 |
on how to implement the interpreter interfaces, and the |
| 740 |
<a href="site:install">installation notes</a> on what jar files are |
| 741 |
needed for TCL and Python support. |
| 742 |
</p> |
| 743 |
</section> |
| 744 |
</body> |
| 745 |
</document> |