Image sizing

People who are familiar with HTML markup often try to use the width attribute in an imagedata tag to scale their graphic. And often it works. But when they try to use other image attributes, they get strange results. Image sizing can be somewhat complex because there are many imagedata attributes that affect it. In addition to sizing the image itself, you can specify a viewport area, which is the space reserved for the image. If the viewport area is larger than the final image size, then there is white space around it. If the viewport area is smaller than the final image size, then the image is supposed to be clipped (but it does not happen in HTML output, may not happen in print output).

These are the attributes available in the imagedata element for sizing:

Imagedata scaling attributes

contentwidth, contentdepth

Specify the horizontal width or vertical height, respectively, of the image itself. You can specify a size using any of these units (with no space between the number and the unit):

px   Pixels (the default unit if none is specified)
pt   Points (1/72 of an inch)
cm   Centimeters
mm   Millimeters
pc   Picas (1/6 of an inch)
in   Inches
em   Ems (type size)
%    Percentage (of intrinsic image size)

If one of these attributes is specified, then the image is scaled proportionally to that size. You can stretch the image by specifying values for both attributes that differ from the original ratio of width to height of the image.

width, depth

Specify the horizontal width and vertical height, respectively, of the viewport area, not the graphic. It uses the same units as above, except percentage is taken to be of the available area (so 50% means half the available width after the current indent is subtracted). If these attributes are not specified, then the viewport is the whole page. These attributes will not affect the size of the image itself, with one exception. For backwards compatibility, if they are used without any other scaling attributes, the stylesheets will scale the image itself to the specified sizes. Think of it as a way of specifying the viewport area with the assumption that it should be filled up.

scale

A percentage scaling factor, but expressed without the percent sign. A value of 75 means to scale the graphic to 75% of its intrinsic size. It scales both width and height proportionally, so the image retains its original aspect ratio.

scalefit

If set to a value of 1, then the processor will scale the graphic proportionally to fit the allowed area. The image is scaled to fit the specified width or depth viewport dimension, whichever fills up first. If neither are specified, then it will scale to the available page width.

The definitive description of how these attributes are supposed to affect the output appears in the reference page for imagedata in DocBook: The Definitive Guide. In practice, some of the attributes are not fully supported by some processors.

Here is some guidance for using the scaling attributes.

Graphic size extension

The DocBook XSL stylesheets come with an XSL extension function written in Java that can be used to extract the intrinsic size information from an image file. Most bitmap formats include header information that identifies the width and height in pixels of the image, and sometimes intended dots per inch (dpi).

When an image is processed for print output, the stylesheet passes the image filename to the FO file. The XSL-FO processor opens the image file, reads the header information, and adjusts the size of the image based on that information as it adds the image to the PDF file. For HTML output, the styleshet passes the image filename to the HTML file, but the image file is not opened until it is read by the web browser, so no image data can be used by the stylesheet in sizing the image.

The stylesheet needs the intrinsic image size when the contentwidth or contentdepth attributes in an imagedata element are expressed as percentages. For those two attributes, the percentage is of the intrinsic image size, not the page size.

The XSLT standard does not provide a function to read image data, but the DocBook XSL stylesheets provide extension functions written in Java to do so. Since they are written in Java, they are only available with the Saxon and Xalan processors. The appropriate extension jar file needs to be configured into your Java CLASSPATH to make them available, as described in the section “Installing Saxon”.

To enable the extension function, set the graphicsize.extension stylesheet parameter to 1, and also set the use.extensions parameter to 1. Then if the stylesheet needs to perform any computation on the contentwidth value, the extension function will open the image file to get intrinsic width information to use in the calculation. Without that information, the stylesheet has to fall back to the fixed value in the nominal.image.width parameter, which has a default value of 540 pixels for HTML output. Since that is not likely the actual image size, the results will not be very satisfactory.

If you set the extensions parameters but the results are not as expected, then check to make sure your CLASSPATH is set correctly. If the stylesheet cannot find the Java graphic size extension function, it does not report an error. Instead, it falls back to a default value. That is the only extension function that does not report an error if it is not available.

One complication arises when you are using the img.src.path parameter to prepend an output path to each fileref value in the IMG element's src value. Normally the extension function just uses the fileref value to locate the image file to open and read. If it needs to include the img.src.path value to locate the file to open, then also set the stylesheet parameter graphicsize.use.img.src.path to 1.

Different sizes for HTML and FO output

You may find that you need to scale an image differently in HTML than in FO output. You can do this by creating nearly duplicate imageobject elements in a mediaobject. You can put different values for the scaling attributes in the imagedata element in each imageobject. Then you let the stylesheet select the appropriate imageobject for a given output. See the section “Selecting file formats” for more information.

If you just want to turn off scaling for HTML output, then that is even easier. If you set the stylesheet parameter ignore.image.scaling to a non-zero value, then all images in HTML output will appear in their intrinsic size. So you use the scaling attributes solely for FO output, and they will be ignored for HTML output.