# XprObjBarcode.java

    /*
    ** Module   : XprObjBarcode.java
    ** Abstract : Class to represent barcode object feature for XPR content elements.
    **
    ** Copyright (c) 2018, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------Description----------------------------------
    ** 001 EVL 20180515 Created initial version.
    ** 002 EVL 20180522 Adding more barcode types support.
    ** 003 EVL 20180603 Adding more barcode options to adjust for particular type.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.reporting;

    import java.util.*;

    /**
     * The barcode object inside XPR file.
     */
    public class XprObjBarcode
    extends XprObjGraphics
    {
       /** The type depending barcode object options in a form key, value map. */
       private Map<String, String> settings = new HashMap<String, String>();

       /** Barcode type names mapping XPR -&gt; Jasperreports */
       private static Map<String, String> typeMap = new HashMap<String, String>();

       /** Y scaling factor for barcode. */
       private int yScale = 1;

       /** X space distance to be blanked. */
       private int xBlank = 0;

       /** One time type mapping init. */
       static
       {  // XPR name, Jasperreport name
          typeMap.put("128A", "Code128A" );
          typeMap.put("128B", "Code128B" );
          typeMap.put("128C", "Code128C" );
          typeMap.put("2_5_interleaved", "Int2of5" );
          typeMap.put("2_5_industrial", "Std2of5" );
          typeMap.put("39", "Code39" );
          typeMap.put("39Extended", "Code39 (Extended)" );
          typeMap.put("Codabar", "Codabar" );
          typeMap.put("PostNet", "PostNet" );
          typeMap.put("EAN13", "EAN13" );
          typeMap.put("EAN128A", "EAN128" );
          typeMap.put("EAN128B", "EAN128" );
          typeMap.put("EAN128C", "EAN128" );
          typeMap.put("UCCEAN128", "UCC128" );
          typeMap.put("UPC_A", "UPCA" );
          typeMap.put("PDF417", "PDF417" );
       }

       /**
        * Create a new barcode XPR object.
        *
        * @param    rowStart
        *           The starting row value for barcode.
        * @param    colStart
        *           The starting column value for barcode.
        * @param    rowEnd
        *           The ending row value for barcode.
        * @param    colEnd
        *           The ending column value for barcode.
        * @param    tagValue
        *           The cumulative string representing the barcode features value.
        */
       public XprObjBarcode(double rowStart, double colStart, double rowEnd, double colEnd,
                            String tagValue)
       {
          super(XprObjBase.TYPE_BARCODE, rowStart, colStart, rowEnd, colEnd);
          initialize(tagValue);
       }

       /**
        * Returns the font name for text object.
        *
        * @param    paramName
        *           The name of the parameter to get the current value.
        *
        * @return   The parameter value for given parameter name or empty string if the parameter
        *           does not exist.
        */
       public String getParameterValue(String paramName)
       {
          String retVal = settings.get(paramName);

          return retVal != null ? retVal : "";
       }

       /**
        * Returns the Jasper report compatible type name.
        *
        * @return   The current barcode type or null if not supported.
        */
       public String getBarcodeType()
       {
          String val = getParameterValue("TYPE");

          return typeMap.get(val);
       }

       /**
        * Returns if the barcode needs check sum.
        *
        * @return   <code>TRUE</code> if checksum is required.
        */
       public boolean isChecksum()
       {
          String val = getParameterValue("CHECKSUM");

          return val.equals("TRUE");
       }

       /**
        * Returns if the barcode needs to show the text.
        *
        * @return   <code>TRUE</code> if SHOW attribute means to draw the text.
        */
       public boolean isShow()
       {
          String val = getParameterValue("SHOW");

          return val.isEmpty() ? false : !val.equals("NONE");
       }

       /**
        * Returns Y scaling factor to get required bar height.
        *
        * @return   Integer multiplier to adjust the visible bar height.
        */
       public int getYScale()
       {
          return yScale;
       }

       /**
        * Returns reserved blank space on both horizonatal sides in pixels.
        *
        * @return   Blank space in pixels.
        */
       public int getXBlank()
       {
          return xBlank;
       }

       /**
        * Returns the bar minimal width depending on type and requested dimensions.
        *
        * @param    dx
        *           The width of the barcode area.
        * @param    dy
        *           The height of the barcode area.
        *
        * @return   The barcode width, 1 or 2 depending on size proportions and barcode type.
        */
       public int getBarWidth(float dx, float dy)
       {
          int bw = 0;

          // return value depends on barcode type
          String type = getBarcodeType();
          if (type != null && !type.isEmpty())
          {
             // every type has its own specific
             if (type.equals("Code128B"))
             {
                bw = dx/dy < 5.0f ? 1 : 2;
             }
             else if (type.equals("Code39"))
             {
                bw = dx/dy < 10.0f ? 1 : 2;
             }
          }

          return bw;
       }

       /**
        * Returns the barcode height depending on type and requested dimensions.
        *
        * @param    dx
        *           The width of the barcode area.
        * @param    dy
        *           The height of the barcode area.
        *
        * @return   The barcode height depending on size proportions and barcode type.
        */
       public int getBarHeight(float dx, float dy)
       {
          int bh = (int)dy;

          // return value depends on barcode type
          String type = getBarcodeType();
          if (type != null && !type.isEmpty())
          {
             // every type has its own specific
             if (type.equals("Code128B"))
             {
                bh = dx/dy < 5.0f ? (int)dy : (int)(dx/8.0f);
             }
             else if (type.equals("Code39"))
             {
                bh = dx/dy < 10.0f ? (int)dy : (int)(dx/8.0f);
             }
          }

          return bh;
       }

       /**
        * Initialize internal barcode features depending on barcode type.
        *
        * @param    tagValue
        *           The cumulative string representing the barcode features option delimited by ','.
        */
       private void initialize(String tagValue)
       {
          if (tagValue != null && !tagValue.isEmpty())
          {
             String[] tagOptions = tagValue.split(",");
             for (int i = 0; i < tagOptions.length; i++)
             {
                if (tagOptions[i] != null && !tagOptions[i].isEmpty())
                {
                   String[] option = tagOptions[i].split("=");
                   if (option[0] != null && !option[0].isEmpty() && option[1] != null)
                   {
                      // make the option name be case insensitive
                      settings.put(option[0].toUpperCase(), option[1]);
                   }
                }
             }
          }
          // set up some config depending on barcode type
          String type = getBarcodeType();
          if (type != null && !type.isEmpty())
          {
             // every type has its own specific
             if (type.equals("Code128B"))
             {
                yScale = 2;
                xBlank = 10;
             }
             else if (type.equals("Code39"))
             {
                yScale = 2;
             }
          }
       }
    }