|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777 |
- /*
- * 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.
- */
-
- /* $Id$ */
-
- package org.apache.fop.afp;
-
- import java.awt.Point;
- import java.io.IOException;
- import java.io.ObjectInputStream;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- import org.apache.xmlgraphics.java2d.color.ColorConverter;
- import org.apache.xmlgraphics.java2d.color.DefaultColorConverter;
-
- import org.apache.fop.afp.fonts.AFPPageFonts;
- import org.apache.fop.util.AbstractPaintingState;
-
- /**
- * This keeps information about the current painting state when writing to an
- * AFP datastream.
- */
- public class AFPPaintingState extends org.apache.fop.util.AbstractPaintingState {
-
- private static final long serialVersionUID = 8206711712452344473L;
-
- private static Log log = LogFactory.getLog("org.apache.xmlgraphics.afp");
-
- /** the portrait rotation */
- private int portraitRotation;
-
- /** the landscape rotation */
- private int landscapeRotation = 270;
-
- /** color image support */
- private boolean colorImages;
-
- /** dithering quality setting (0.0f..1.0f) */
- private float ditheringQuality;
-
- /** image encoding quality setting (0.0f..1.0f) */
- private float bitmapEncodingQuality;
-
- /** color image handler */
- private transient ColorConverter colorConverter;
-
- /**
- * true if certain image formats may be embedded unchanged in their native
- * format.
- */
- private boolean nativeImagesSupported;
-
- private boolean canEmbedJpeg;
-
- /**
- * true if CMYK images (requires IOCA FS45 suppport on the target platform)
- * may be generated
- */
- private boolean cmykImagesSupported;
-
- /** default value for image depth */
- private int bitsPerPixel = 8;
-
- /** the output resolution */
- private int resolution = 240; // 240 dpi
-
- /**
- * A configurable value to correct the line width so that the output matches the expected. Different
- * devices may need different values.
- */
- private float lineWidthCorrection = AFPConstants.LINE_WIDTH_CORRECTION;
-
- /** determines whether GOCA is enabled or disabled */
- private boolean gocaEnabled = true;
- /** determines whether to stroke text in GOCA mode or to use text operators where possible */
- private boolean strokeGocaText;
-
-
- /** use page segment with F11 and F45 images*/
- private boolean pSeg;
-
- private boolean gocaPSeg;
-
- /** use FS45 images*/
- private boolean fs45;
-
- /** the current page */
- private transient AFPPagePaintingState pagePaintingState;
-
- // /** reference orientation */
- // private int orientation = 0;
-
- /** a unit converter */
- private final transient AFPUnitConverter unitConv;
-
- public AFPPaintingState() {
- colorConverter = GrayScaleColorConverter.getInstance();
- pagePaintingState = new AFPPagePaintingState();
- unitConv = new AFPUnitConverter(this);
- }
-
- private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
- ois.defaultReadObject();
- }
-
- /**
- * Sets the rotation to be used for portrait pages, valid values are 0
- * (default), 90, 180, 270.
- *
- * @param rotation
- * The rotation in degrees.
- */
- public void setPortraitRotation(int rotation) {
- if (rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270) {
- portraitRotation = rotation;
- } else {
- throw new IllegalArgumentException("The portrait rotation must be one"
- + " of the values 0, 90, 180, 270");
-
- }
- }
-
- /**
- * Returns the rotation to be used for portrait pages
- *
- * @return the rotation to be used for portrait pages
- */
- protected int getPortraitRotation() {
- return this.portraitRotation;
- }
-
- /**
- * Sets the rotation to be used for landscape pages, valid values are 0, 90,
- * 180, 270 (default).
- *
- * @param rotation
- * The rotation in degrees.
- */
- public void setLandscapeRotation(int rotation) {
- if (rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270) {
- landscapeRotation = rotation;
- } else {
- throw new IllegalArgumentException("The landscape rotation must be one"
- + " of the values 0, 90, 180, 270");
- }
- }
-
- /**
- * Returns the landscape rotation
- *
- * @return the landscape rotation
- */
- protected int getLandscapeRotation() {
- return this.landscapeRotation;
- }
-
- /**
- * Sets the number of bits used per pixel
- *
- * @param bitsPerPixel
- * number of bits per pixel
- */
- public void setBitsPerPixel(int bitsPerPixel) {
- switch (bitsPerPixel) {
- case 1:
- case 4:
- case 8:
- this.bitsPerPixel = bitsPerPixel;
- break;
- default:
- log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
- this.bitsPerPixel = 8;
- break;
- }
- }
-
- /**
- * Returns the number of bits per pixel
- *
- * @return the number of bits per pixel
- */
- public int getBitsPerPixel() {
- return this.bitsPerPixel;
- }
-
- /**
- * Sets whether images are color or not and instantiates a ColorHandler
- *
- * @param colorImages
- * color image output
- */
- public void setColorImages(boolean colorImages) {
- this.colorImages = colorImages;
-
- if (colorImages) {
- this.colorConverter = DefaultColorConverter.getInstance();
- }
-
- }
-
- /**
- * Returns true if color images are to be used
- *
- * @return true if color images are to be used
- */
- public boolean isColorImages() {
- return this.colorImages;
- }
-
- /**
- * Used to convert color in respect of the colorImages flag
- *
- * @return the color converter
- */
- public ColorConverter getColorConverter() {
- return this.colorConverter;
- }
-
- /**
- * Sets whether images are natively supported or not in the AFP environment
- *
- * @param nativeImagesSupported
- * true if images are natively supported in this AFP environment
- */
- public void setNativeImagesSupported(boolean nativeImagesSupported) {
- this.nativeImagesSupported = nativeImagesSupported;
- }
-
- /**
- * Returns true if images are supported natively in this AFP environment
- *
- * @return true if images are supported natively in this AFP environment
- */
- public boolean isNativeImagesSupported() {
- return this.nativeImagesSupported;
- }
-
- /**
- * Set whether or not JPEG images can be embedded within an AFP document.
- *
- * @param canEmbed true if the JPEG image can be embedded
- */
- public void setCanEmbedJpeg(boolean canEmbed) {
- canEmbedJpeg = canEmbed;
- }
-
- /**
- * Returns true if JPEGs can be embedded in an AFP document.
- *
- * @return true if JPEG embedding is allowed
- */
- public boolean canEmbedJpeg() {
- return canEmbedJpeg;
- }
-
- /**
- * Controls whether CMYK images (IOCA FS45) are enabled. By default, support
- * is disabled for wider compatibility. When disabled, any CMYK image is
- * converted to the selected color format.
- *
- * @param value
- * true to enabled CMYK images
- */
- public void setCMYKImagesSupported(boolean value) {
- this.cmykImagesSupported = value;
- }
-
- /**
- * Indicates whether CMYK images (IOCA FS45) are enabled.
- *
- * @return true if IOCA FS45 is enabled
- */
- public boolean isCMYKImagesSupported() {
- return this.cmykImagesSupported;
- }
-
- /**
- * Gets the dithering quality setting to use when converting images to monochrome images.
- * @return the dithering quality (a value between 0.0f and 1.0f)
- */
- public float getDitheringQuality() {
- return this.ditheringQuality;
- }
-
- /**
- * Sets the dithering quality setting to use when converting images to monochrome images.
- * @param quality Defines the desired quality level for the conversion.
- * Valid values: a value between 0.0f (fastest) and 1.0f (best)
- */
- public void setDitheringQuality(float quality) {
- quality = Math.max(quality, 0.0f);
- quality = Math.min(quality, 1.0f);
- this.ditheringQuality = quality;
- }
-
- /**
- * Gets the image encoding quality setting to use when encoding bitmap images.
- * @return the encoding quality (a value between 0.0f and 1.0f, 1.0 meaning loss-less)
- */
- public float getBitmapEncodingQuality() {
- return this.bitmapEncodingQuality;
- }
-
- /**
- * Sets the image encoding quality setting to use when encoding bitmap images.
- * @param quality Defines the desired quality level for the conversion.
- * Valid values: a value between 0.0f (lowest) and 1.0f (best, loss-less)
- */
- public void setBitmapEncodingQuality(float quality) {
- quality = Math.max(quality, 0.0f);
- quality = Math.min(quality, 1.0f);
- this.bitmapEncodingQuality = quality;
- }
-
- /**
- * Sets the output/device resolution
- *
- * @param resolution
- * the output resolution (dpi)
- */
- public void setResolution(int resolution) {
- if (log.isDebugEnabled()) {
- log.debug("renderer-resolution set to: " + resolution + "dpi");
- }
- this.resolution = resolution;
- }
-
- /**
- * Sets the line width correction
- *
- * @param correction the line width multiplying factor correction
- */
- public void setLineWidthCorrection(float correction) {
- if (log.isDebugEnabled()) {
- log.debug("line width correction set to: " + correction);
- }
- this.lineWidthCorrection = correction;
- }
-
- /**
- * Returns the output/device resolution.
- *
- * @return the resolution in dpi
- */
- public int getResolution() {
- return this.resolution;
- }
-
- /**
- * Returns the line width correction.
- * @return the correction
- */
- public float getLineWidthCorrection() {
- return this.lineWidthCorrection;
- }
-
- /**
- * Controls whether GOCA is enabled or disabled.
- * @param enabled true if GOCA is enabled, false if it is disabled
- */
- public void setGOCAEnabled(boolean enabled) {
- this.gocaEnabled = enabled;
- }
-
- /**
- * Indicates whether GOCA is enabled or disabled.
- * @return true if GOCA is enabled, false if GOCA is disabled
- */
- public boolean isGOCAEnabled() {
- return this.gocaEnabled;
- }
-
- /**
- * Controls whether to stroke text in GOCA mode or to use text operators where possible.
- * @param stroke true to stroke, false to paint with text operators where possible
- */
- public void setStrokeGOCAText(boolean stroke) {
- this.strokeGocaText = stroke;
- }
-
- /**
- * Indicates whether to stroke text in GOCA mode or to use text operators where possible.
- * @return true to stroke, false to paint with text operators where possible
- */
- public boolean isStrokeGOCAText() {
- return this.strokeGocaText;
- }
-
- /**
- * Whether FS11 and SF45 non-inline images should be wrapped in a page segment
- * @return true iff images should be wrapped
- */
- public boolean getWrapPSeg() {
- return pSeg;
- }
-
- /**
- * Sets whether FS11 and FS45 non-inline images should be wrapped in a page segment
- * @param pSeg true iff images should be wrapped
- */
- public void setWrapPSeg(boolean pSeg) {
- this.pSeg = pSeg;
- }
-
- public boolean getWrapGocaPSeg() {
- return gocaPSeg;
- }
-
- public void setWrapGocaPSeg(boolean pSeg) {
- this.gocaPSeg = pSeg;
- }
-
- /**
- * gets whether images should be FS45
- * @return true iff images should be FS45
- */
- public boolean getFS45() {
- return fs45;
- }
-
- /**
- * sets whether images should be FS45
- * @param fs45 true iff images should be FS45
- */
- public void setFS45(boolean fs45) {
- this.fs45 = fs45;
- }
-
-
-
- /** {@inheritDoc} */
- @Override
- protected AbstractData instantiateData() {
- return new AFPData();
- }
-
- /** {@inheritDoc} */
- @Override
- protected AbstractPaintingState instantiate() {
- return new AFPPaintingState();
- }
-
- /**
- * Returns the painting state of the current page
- *
- * @return the painting state of the current page
- */
- protected AFPPagePaintingState getPagePaintingState() {
- return this.pagePaintingState;
- }
-
- /**
- * Gets the current page fonts
- *
- * @return the current page fonts
- */
- public AFPPageFonts getPageFonts() {
- return pagePaintingState.getFonts();
- }
-
- /**
- * Sets the page width
- *
- * @param pageWidth
- * the page width
- */
- public void setPageWidth(int pageWidth) {
- pagePaintingState.setWidth(pageWidth);
- }
-
- /**
- * Returns the page width
- *
- * @return the page width
- */
- public int getPageWidth() {
- return pagePaintingState.getWidth();
- }
-
- /**
- * Sets the page height
- *
- * @param pageHeight
- * the page height
- */
- public void setPageHeight(int pageHeight) {
- pagePaintingState.setHeight(pageHeight);
- }
-
- /**
- * Returns the page height
- *
- * @return the page height
- */
- public int getPageHeight() {
- return pagePaintingState.getHeight();
- }
-
- /**
- * Returns the page rotation
- *
- * @return the page rotation
- */
- public int getPageRotation() {
- return pagePaintingState.getOrientation();
- }
-
- /**
- * Sets the uri of the current image
- *
- * @param uri
- * the uri of the current image
- */
- public void setImageUri(String uri) {
- ((AFPData) getData()).imageUri = uri;
- }
-
- /**
- * Gets the uri of the current image
- *
- * @return the uri of the current image
- */
- public String getImageUri() {
- return ((AFPData) getData()).imageUri;
- }
-
- /**
- * Returns the currently derived rotation
- *
- * @return the currently derived rotation
- */
- public int getRotation() {
- return getData().getDerivedRotation();
- }
-
- /**
- * Returns the unit converter
- *
- * @return the unit converter
- */
- public AFPUnitConverter getUnitConverter() {
- return this.unitConv;
- }
-
- /**
- * Returns a point on the current page, taking the current painting state
- * into account.
- *
- * @param x
- * the X-coordinate
- * @param y
- * the Y-coordinate
- * @return a point on the current page
- */
- public Point getPoint(int x, int y) {
- Point p = new Point();
- int rotation = getRotation();
- switch (rotation) {
- case 90:
- p.x = y;
- p.y = getPageWidth() - x;
- break;
- case 180:
- p.x = getPageWidth() - x;
- p.y = getPageHeight() - y;
- break;
- case 270:
- p.x = getPageHeight() - y;
- p.y = x;
- break;
- default:
- p.x = x;
- p.y = y;
- break;
- }
- return p;
- }
-
- /** {@inheritDoc} */
- @Override
- public Object clone() {
- AFPPaintingState paintingState = (AFPPaintingState) super.clone();
- paintingState.pagePaintingState = (AFPPagePaintingState) this.pagePaintingState.clone();
- paintingState.portraitRotation = this.portraitRotation;
- paintingState.landscapeRotation = this.landscapeRotation;
- paintingState.bitsPerPixel = this.bitsPerPixel;
- paintingState.colorImages = this.colorImages;
- paintingState.colorConverter = this.colorConverter;
- paintingState.resolution = this.resolution;
- return paintingState;
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return "AFPPaintingState{" + "portraitRotation=" + portraitRotation
- + ", landscapeRotation=" + landscapeRotation + ", colorImages=" + colorImages
- + ", bitsPerPixel=" + bitsPerPixel + ", resolution=" + resolution + ", pageState="
- + pagePaintingState + super.toString() + "}";
- }
-
- /**
- * Page level state data
- */
- private class AFPPagePaintingState implements Cloneable {
- /** page width */
- private int width;
-
- /** page height */
- private int height;
-
- /** page fonts */
- private AFPPageFonts fonts = new AFPPageFonts();
-
- /** page font count */
- private int fontCount;
-
- /** page orientation */
- private int orientation;
-
- /**
- * Returns the page width
- *
- * @return the page width
- */
- protected int getWidth() {
- return width;
- }
-
- /**
- * Sets the page width
- *
- * @param width
- * the page width
- */
- protected void setWidth(int width) {
- this.width = width;
- }
-
- /**
- * Returns the page height
- *
- * @return the page height
- */
- protected int getHeight() {
- return height;
- }
-
- /**
- * Sets the page height
- *
- * @param height
- * the page height
- */
- protected void setHeight(int height) {
- this.height = height;
- }
-
- /**
- * Returns the page fonts
- *
- * @return the page fonts
- */
- protected AFPPageFonts getFonts() {
- return fonts;
- }
-
- /**
- * Sets the current page fonts
- *
- * @param fonts
- * the current page fonts
- */
- protected void setFonts(AFPPageFonts fonts) {
- this.fonts = fonts;
- }
-
- /**
- * Increments and returns the current page font count
- *
- * @return increment and return the current page font count
- */
- protected int incrementFontCount() {
- return ++fontCount;
- }
-
- /**
- * Returns the current page orientation
- *
- * @return the current page orientation
- */
- protected int getOrientation() {
- return orientation;
- }
-
- /**
- * Sets the current page orientation
- *
- * @param orientation
- * the current page orientation
- */
- protected void setOrientation(int orientation) {
- this.orientation = orientation;
- }
-
- /** {@inheritDoc} */
- @Override
- public Object clone() {
- AFPPagePaintingState state = new AFPPagePaintingState();
- state.width = this.width;
- state.height = this.height;
- state.orientation = this.orientation;
- state.fonts = new AFPPageFonts(this.fonts);
- state.fontCount = this.fontCount;
- return state;
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return "AFPPagePaintingState{width=" + width + ", height=" + height + ", orientation="
- + orientation + ", fonts=" + fonts + ", fontCount=" + fontCount + "}";
- }
- }
-
- /**
- * Block level state data
- */
- // @SuppressFBWarnings("SE_INNER_CLASS")
- private class AFPData extends org.apache.fop.util.AbstractPaintingState.AbstractData {
- private static final long serialVersionUID = -1789481244175275686L;
-
- /** The current fill status */
- private boolean filled;
-
- private String imageUri;
-
- /** {@inheritDoc} */
- @Override
- public Object clone() {
- AFPData obj = (AFPData) super.clone();
- obj.filled = this.filled;
- obj.imageUri = this.imageUri;
- return obj;
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return "AFPData{" + super.toString() + ", filled=" + filled + ", imageUri=" + imageUri
- + "}";
- }
-
- /** {@inheritDoc} */
- @Override
- protected AbstractData instantiate() {
- return new AFPData();
- }
- }
-
- }
|