LargePrintImageProcessor.java :  » XML » alttext » nl » dedicon » converter » dtbook » processors » Java Open Source

Java Open Source » XML » alttext 
alttext » nl » dedicon » converter » dtbook » processors » LargePrintImageProcessor.java
/*
 * Copyright (C) 2010 Dedicon <http://dedicon.nl>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Linking altText statically or dynamically with other modules 
is making a combined work based on altText. Thus, the terms and 
conditions of the GNU General Public License cover
the whole combination.  In addition, as a special exception, 
the copyright holders of altText give you permission to
combine altText program with free software programs or libraries 
that are released under the GNU LGPL or the Common Public License version 1.0. 
You may copy and distribute such a system following the terms 
of the GNU GPL for altText and the licenses of the other code
concerned, provided that you include the source code of that other 
code when and as the GNU GPL requires distribution of source code.

Note that people who make modified versions of altText are not obligated to 
grant this special exception for their modified versions; it is their 
choice whether to do so. The GNU General Public License gives permission 
to release a modified version without this exception; this exception 
also makes it possible to release a modified version which carries
forward this exception.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Author : Javier Asensio Cubero capitan{.}cambio{@}gmail{.}com
 */
package nl.dedicon.converter.dtbook.processors;

import java.util.logging.Logger;

import nl.dedicon.converter.core.Processor;
import nl.dedicon.converter.core.io.Package;
import nl.dedicon.converter.dtbook.io.DaisyXmlToOdtPackage;
import nl.dedicon.converter.odt.utils.OdtNamespaceContext;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// TODO: Auto-generated Javadoc
/**
 * The Class LargePrintImageProcessor process all the img imformation to get a largeprint document
 */
public class LargePrintImageProcessor extends Processor {
  
  /** The Constant IMG_XPATH. */
  public static final String IMG_XPATH="//draw:frame/draw:image";
  
  /** The Constant FRAME_XPATH. */
  public static final String FRAME_XPATH="//draw:frame/draw:text-box/text:p/draw:frame/draw:image";

  /** The logger. */
  private Logger logger = Logger.getLogger("nl.dedicon.converter.dtbook.processors");
  
  
  /** The Constant MAX_WIDTH. */
  private static final double MAX_WIDTH=17.0;
  
  /** The Constant MAX_HEIGHT. */
  private static final double MAX_HEIGHT=25.7;
  
  /** The Constant RESIZE_FACTOR. */
  private static final double RESIZE_FACTOR=1.5;


  /* (non-Javadoc)
   * @see nl.dedicon.converter.core.Processor#doPerform(nl.dedicon.converter.core.io.Package)
   */
  @Override
  protected void doPerform(Package pack) {
    DaisyXmlToOdtPackage dPack = (DaisyXmlToOdtPackage) pack;
    this.logger.info("Performing LargePrintImageProcessor");

    try{
      NodeList imgs = dPack.getOdtPart(OdtElements.ODT_CONTENTS).excuteXpathQuery(IMG_XPATH, new OdtNamespaceContext());
      //first the img without frames
      for(int i = 0; i < imgs.getLength();i++){
        this.resizeNode(imgs.item(i).getParentNode());
      }
      NodeList imgFrames = dPack.getOdtPart(OdtElements.ODT_CONTENTS).excuteXpathQuery(FRAME_XPATH, new OdtNamespaceContext());
      //first the img with frames
      for(int i = 0; i < imgFrames.getLength();i++){
        Node outermostFrame  = imgFrames.item(i).getParentNode().getParentNode().getParentNode().getParentNode();
        Node FrameW = imgFrames.item(i).getParentNode().getAttributes().getNamedItem("svg:width").cloneNode(false);
        //Node FrameH = imgFrames.item(i).getParentNode().getAttributes().getNamedItem("svg:height").cloneNode(false);
        outermostFrame.getAttributes().setNamedItem(FrameW);
        //outermostFrame.getAttributes().setNamedItem(FrameH);
        
      }
      
      
    }catch (Exception e){
      String msg = "Error while resizing imgs for the odt file: " +e.getMessage();
      logger.warning(msg);
      
    }
    
  }

  /**
   * Resize node.
   * 
   * @param item the item
   */
  private void resizeNode(Node item) {
    logger.fine("Proceeding to resize all the imgs");
    Node attrW = item.getAttributes().getNamedItem("svg:width");
    Node attrH = item.getAttributes().getNamedItem("svg:height");
    double origW= Double.parseDouble(attrW.getTextContent().replace("cm", ""));
    double origH= Double.parseDouble(attrH.getTextContent().replace("cm", ""));
    
    double values[] = this.calculateNewDims(origH, origW);
    attrH.setNodeValue(values[0]+"cm");
    attrW.setNodeValue(values[1]+"cm");
    
    
  }
  
  /**
   * Calculate new dims.
   * 
   * @param origH the orig h
   * @param origW the orig w
   * 
   * @return the double[]
   */
  protected double[] calculateNewDims(double origH,double origW){
    
    
    double factor = this.sortFactorOut(origH,origW);
    logger.info("img resize factor set to "+factor);
    logger.info("new dim values (h,e):" +origH+","+origW );
    double resizedH= origH*factor;
    double resizedW= origW*factor;
    logger.info("new dim values (h,e):" +resizedH+","+resizedW );
    
    return new double[]{resizedH,resizedW};  
    
  }

  /**
   * Sorts factor the current resize factor out taking care of not going further than the current 
   * margin of the document.
   * @param origH the orig h
   * @param origW the orig w
   * 
   * @return the double
   */
  private double sortFactorOut(double origH, double origW) {
    
    return Math.min(RESIZE_FACTOR, Math.min(MAX_HEIGHT/origH, MAX_WIDTH/origW));    
  }

  
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.