/* * [Dim.java] * * Summary: Displays dimension ( width x height) in both metric and Imperial measure. * * Copyright: (c) 2008-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2008-04-24 initial/ version * 1.1 2011-02-10 add optional td parm. */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.UnitsOfMeasure; import static java.lang.System.*; /** * Displays dimension ( width x height) in both metric and Imperial measure. *

* * * @author Roedy Green, Canadian Mind Products * @version 1.1 2011-02-10 add optional td parm. * @since 2008-04-24 */ public final class Dim extends Macro { /** * how to use the macro */ private static final String USAGE = "\nDim macro needs width [x height x depth] unitsOfMeasure [td] e.g. Dim 40 x " + "20 cm [td] [br] separator"; /** * Degrees macro expansion. * * @param unitsOfMeasure units units of measure of the quantity * @param width width in odd units * @param height height in odd units * @param depth depth in odd units * @param dims how many dimensions * @param sep how to separate the metric from imperial ( td br * * @return "25.4 x 25.4 cm (10 x 10 in) both. */ private static String expand( final UnitsOfMeasure unitsOfMeasure, final double width, final double height, final double depth, final int dims, final String sep ) { final FastCat sb = new FastCat( 8 ); /* lead to help sort */ switch ( dims ) { case 1: default: break; case 2: sb.append( "" ); break; case 3: sb.append( "" ); break; } if ( sep.equals( "(" ) ) { switch ( dims ) { case 1: default: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width ) ) ); sb.append( " (" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width ) ) ); sb.append( ")" ); break; case 2: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height ) ) ); sb.append( " (" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height ) ) ); sb.append( ")" ); break; case 3: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height, depth ) ) ); sb.append( " (" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height, depth ) ) ); sb.append( ")" ); break; } } else if ( sep.equals( "td" ) ) { switch ( dims ) { case 1: default: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width ) ) ); sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width ) ) ); sb.append( "" ); break; case 2: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height ) ) ); sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height ) ) ); sb.append( "" ); break; case 3: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height, depth ) ) ); sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height, depth ) ) ); sb.append( "" ); break; } } else if ( sep.equals( "br" ) ) { switch ( dims ) { case 1: default: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width ) ) ); sb.append( "
" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width ) ) ); sb.append( "" ); break; case 2: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height ) ) ); sb.append( "
" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height ) ) ); sb.append( "" ); break; case 3: sb.append( "" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toMetricDisplay( width, height, depth ) ) ); sb.append( "
" ); sb.append( UnitsOfMeasure.fractify( unitsOfMeasure.toImperialDisplay( width, height, depth ) ) ); sb.append( "" ); break; } } else { throw new IllegalArgumentException( "Dim programming bug invalid sep parm " + sep ); } return sb.toString(); } public String expandMacro( final String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "D" ); } try { if ( !( 2 <= parms.length && parms.length <= 7 ) ) { throw new IllegalArgumentException( USAGE ); } final UnitsOfMeasure unitsOfMeasure; int dims; final double width; final double height; final double depth; width = Double.parseDouble( ST.stripNaughtyCharacters( parms[ 0 ], ",_" ) ); if ( !parms[ 1 ].equalsIgnoreCase( "x" ) ) { dims = 1; height = 0; depth = 0; unitsOfMeasure = UnitsOfMeasure.valueOfAlias( parms[ 1 ] ); } else { height = Double.parseDouble( ST.stripNaughtyCharacters( parms[ 2 ], ",_" ) ); if ( !parms[ 3 ].equalsIgnoreCase( "x" ) ) { dims = 2; depth = 0; unitsOfMeasure = UnitsOfMeasure.valueOfAlias( parms[ 3 ] ); } else { depth = Double.parseDouble( ST.stripNaughtyCharacters( parms[ 4 ], ",_" ) ); dims = 3; unitsOfMeasure = UnitsOfMeasure.valueOfAlias( parms[ 5 ] ); } } final String sep; if ( parms[ parms.length - 1 ].equals( "td" ) ) { sep = "td"; } else if ( parms[ parms.length - 1 ].equals( "br" ) ) { sep = "br"; } else { sep = "("; } return expand( unitsOfMeasure, width, height, depth, dims, sep ); } catch ( NumberFormatException e ) { throw new IllegalArgumentException( USAGE ); } } /** * * * @param parms dist/units * @param quiet true if want output suppressed. * @param verbose @return expanded macro text. */ }