/* * [BuildImage.java] * * Summary: Not a macro. Used to generate doneBefore = new HashMap<>( IMAGE_CACHE_CAPACITY ); /** * suffix for a tag */ private static final String TAG_TAIL = configuration.getTagTail(); /** * create a xx * * @param imageFilename name of image file, e.g. dog.gif, or some html to create image in the image * directory tree * @param alt alternate text display, may contain html or entities. * @param align alignment enum e.g. absmiddle, possibly null for none. * @param cssClassForImage cssClass of the image itself * @param width of the image in pixels. * @param height of the image in pixels. * the file currently being processed. * @param fileBeingDistributed where this link will end up * * @return html for one img tag */ private static String buildHTML4ImageTag( final String imageFilename, final String alt, final ImageAlignment align, final String cssClassForImage, final int width, final int height, final File fileBeingDistributed ) { assert fileBeingDistributed != null : "null fileBeingDistributed"; assert imageFilename != null : "null image filename"; // we put field out in canonical order class src alt (title longdesc) width height align border final FastCat sb = new FastCat( 20 ); sb.append( "" ); return sb.toString(); } /** * create a x * * @param imageFilename name of image file, e.g. dog.gif, or some html to create image in the image * directory tree * @param alt alternate text display, may contain html or entities. * @param align alignment enum e.g. absmiddle, possibly null for none. * @param cssClassForImage cssClass of the image itself * @param width width of the image in pixels. * @param height height of the image in pixels. * the file currently being processed. * @param fileBeingDistributed where this link will end up * * @return html for one img tag */ private static String buildXHTMLImageTag( final String imageFilename, final String alt, final ImageAlignment align, final String cssClassForImage, final int width, final int height, final File fileBeingDistributed ) { assert fileBeingDistributed != null : "null fileBeingDistributed"; assert imageFilename != null : "null image filename"; // we put field out in canonical order class src alt (title longdesc) width height align border final FastCat sb = new FastCat( 20 ); sb.append( "" ); return sb.toString(); } /** * create a relative filename given just the unqualified filename in /image for use in a local style reference. * e.g. ../image/icon32/x.png * * @param imageFilename name of image file, e.g. dog.gif, stylesheet/open.png or some html to create * image, in the image directory tree * the file currently being processed. * @param fileBeingDistributed where this link will end up * * @return style="background-image: url(xxx) reference for image, just the xxx. */ public static String buildImageRootForStyleRef( String imageFilename, File fileBeingDistributed ) { assert fileBeingDistributed != null : "null fileBeingDistributed"; assert imageFilename != null && imageFilename.length() > 0 : "null image filename"; if ( imageFilename.endsWith( "jpg" ) | imageFilename.endsWith( "gif" ) | imageFilename.endsWith( "png" ) ) { int[] dimensions = getProjectImageDimensions( imageFilename ); final int width = dimensions[ 0 ]; final int height = dimensions[ 1 ]; if ( width == 0 || height == 0 ) { // oops. problem with image file final File absFile = Tools.toFileFromUPath( "image/" + imageFilename ); if ( absFile.exists() ) { throw new IllegalArgumentException( "image file " + absFile + " is malformed (zero width/height)." ); } else { throw new IllegalArgumentException( "image file " + absFile + " does not exist." ); } } return Tools.relativeURL( "image/" + imageFilename, fileBeingDistributed ); } else { throw new IllegalArgumentException( "image file " + imageFilename + " not png, gif or jpg" ); } } /** * create a x * * @param imageRelativeFilename name of image file, e.g. dog.gif, or some html to create image in the image * directory tree. Image local in image dir. * possibly empty or null * @param alt alternate text display, may contain html or entities. * @param align alignment enum e.g. absmiddle, possibly null for none. * @param cssClassForImage cssClass of the image itself * the file currently being distributed. used to compute relative url of image. * @param fileBeingDistributed where this link will end up * * @return html for one img tag, or "" for null image from */ public static String buildImgTag( final String imageRelativeFilename, final String alt, @Nullable ImageAlignment align, @Nullable final String cssClassForImage, final File fileBeingDistributed ) { assert fileBeingDistributed != null : "null fileBeingDistributed"; if ( ST.isEmpty( imageRelativeFilename ) ) { return ""; } if ( imageRelativeFilename.endsWith( "png" ) | imageRelativeFilename.endsWith( "jpg" ) | imageRelativeFilename.endsWith( "gif" ) | imageRelativeFilename.endsWith( "webp" ) ) { int[] dimensions = getProjectImageDimensions( imageRelativeFilename ); final int width = dimensions[ 0 ]; final int height = dimensions[ 1 ]; if ( width == 0 ) { // oops. problem with image file final File absFile = Tools.toFileFromUPath( "image/" + imageRelativeFilename ); if ( absFile.exists() ) { throw new IllegalArgumentException( "image file " + absFile + " is malformed." ); } else { throw new IllegalArgumentException( "image file " + absFile + " does not exist." ); } } if ( align == null ) { align = ImageAlignment.none; } if ( configuration.isUsingXHTML() ) { return buildXHTMLImageTag( imageRelativeFilename, alt, align, cssClassForImage, width, height, fileBeingDistributed ); } else { return buildHTML4ImageTag( imageRelativeFilename, alt, align, cssClassForImage, width, height, fileBeingDistributed ); } } else { // some html or some custom highWidth || height > highHeight ) { throw new IllegalArgumentException( "image " + absFile + " [" + width + "x" + height + "] is bigger than [" + highWidth + "x" + highHeight + "]" ); } } /** * Find out width and height of an image in the /image/* tree. Do not confuse with the more general * ImageInfo.getImageDimensions. * * @param projectImage name of image file in the image tree, e.g. navigate/home.png * * @return array[2] [0[=width [1]=height 0 0 means invalid. Does not throw exception. */ public static int[] getProjectImageDimensions( String projectImage ) { // see if we have done this one before Integer wxh = doneBefore.get( projectImage ); final int width; final int height; if ( wxh != null ) { // use previous dimensions, saving time to look up image. // packed width in msw and height in lsw width = wxh >>> 16; height = wxh & 0xffff; return new int[] { width, height }; } else { int[] dimensions = ImageInfo.getImageDimensions( Tools.toFileFromUPath( "image/" + projectImage ).getAbsolutePath() ); width = dimensions[ 0 ]; height = dimensions[ 1 ]; if ( width == 0 || height == 0 ) { return new int[] { 0, 0 }; } // new good one // pack width in msw and height in lsw doneBefore.put( projectImage, width << 16 | height ); return dimensions; } } }