2011年1月31日

xhtmlrenderer死循环问题

今天使用xhtmlrenderer时发现它在css背景图片不存在时存在bug,程序在渲染这种html时会死循环。

调试发现问题出在org.xhtmlrenderer.render.AbstractOutputDevicepaintTiles方法(paintVerticalBand和paintHorizontalBand 也有一样问题):

private void paintTiles(FSImage image, int left, int top, int right, int bottom) {  
     int width = image.getWidth();  
     int height = image.getHeight();  
     for (int x = left; x < right; x+= width) {  
         for (int y = top; y < bottom; y+= height) {  
             drawImage(image, x, y);  
         }  
     }  
 }  

当图片不存在时(比如404),此时会提供一个默认的Image对象org.xhtmlrenderer.swing.AWTFSImage.NULL_FS_IMAGE, 这个对象的width和height都是0.当把它作为参数传给paintTiles时,就死循环了。

找到原因就很好解决了,只要在paint前判断一下,如果是NULL_FS_IMAGE,直接return即可。

core-renderer-repack.jar是我重新打的包。

今天还在The Perils of Image.getScaledInstance()找到一个提高图片压缩质量的方法,用着还不错:

/**  
  * Convenience method that returns a scaled instance of the  
  * provided {@code BufferedImage}.  
  *  
  * @param img the original image to be scaled  
  * @param targetWidth the desired width of the scaled instance,  
  *  in pixels  
  * @param targetHeight the desired height of the scaled instance,  
  *  in pixels  
  * @param hint one of the rendering hints that corresponds to  
  *  {@code RenderingHints.KEY_INTERPOLATION} (e.g.  
  *  {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},  
  *  {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},  
  *  {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})  
  * @param higherQuality if true, this method will use a multi-step  
  *  scaling technique that provides higher quality than the usual  
  *  one-step technique (only useful in downscaling cases, where  
  *  {@code targetWidth} or {@code targetHeight} is  
  *  smaller than the original dimensions, and generally only when  
  *  the {@code BILINEAR} hint is specified)  
  * @return a scaled version of the original {@code BufferedImage}  
  */  
 public BufferedImage getScaledInstance(BufferedImage img,  
                     int targetWidth,  
                     int targetHeight,  
                     Object hint,  
                     boolean higherQuality)  
 {  
   int type = (img.getTransparency() == Transparency.OPAQUE) ?  
     BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;  
   BufferedImage ret = (BufferedImage)img;  
   int w, h;  
   if (higherQuality) {  
     // Use multi-step technique: start with original size, then  
     // scale down in multiple passes with drawImage()  
     // until the target size is reached  
     w = img.getWidth();  
     h = img.getHeight();  
   } else {  
     // Use one-step technique: scale directly from original  
     // size to target size with a single drawImage() call  
     w = targetWidth;  
     h = targetHeight;  
   }  
   do {  
     if (higherQuality && w > targetWidth) {  
       w /= 2;  
       if (w < targetWidth) {  
         w = targetWidth;  
       }  
     }  
     if (higherQuality && h > targetHeight) {  
       h /= 2;  
       if (h < targetHeight) {  
         h = targetHeight;  
       }  
     }  
     BufferedImage tmp = new BufferedImage(w, h, type);  
     Graphics2D g2 = tmp.createGraphics();  
     g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);  
     g2.drawImage(ret, 0, 0, w, h, null);  
     g2.dispose();  
     ret = tmp;  
   } while (w != targetWidth || h != targetHeight);  
   return ret;  
 }