コード例 #1
0
ファイル: PageArea.java プロジェクト: GiorgioHuang/birt
  protected BackgroundImageInfo createBackgroundImage(String url) {
    ResourceLocatorWrapper rl = null;
    ExecutionContext exeContext =
        ((ReportContent) content.getReportContent()).getExecutionContext();
    if (exeContext != null) {
      rl = exeContext.getResourceLocator();
    }
    IStyle cs = pageContent.getComputedStyle();
    BackgroundImageInfo backgroundImage =
        new BackgroundImageInfo(
            url, cs.getProperty(IStyle.STYLE_BACKGROUND_REPEAT), 0, 0, 0, 0, rl);
    Image img = backgroundImage.getImageInstance();

    IStyle style = pageContent.getStyle();
    String widthStr = style.getBackgroundWidth();
    String heightStr = style.getBackgroundHeight();

    if (img != null) {
      int resolutionX = img.getDpiX();
      int resolutionY = img.getDpiY();
      if (0 == resolutionX || 0 == resolutionY) {
        resolutionX = 96;
        resolutionY = 96;
      }
      float imageWidth = img.getPlainWidth() / resolutionX * 72 * PDFConstants.LAYOUT_TO_PDF_RATIO;
      float imageHeight =
          img.getPlainHeight() / resolutionY * 72 * PDFConstants.LAYOUT_TO_PDF_RATIO;
      int actualWidth = (int) imageWidth;
      int actualHeight = (int) imageHeight;

      if (widthStr != null && widthStr.length() > 0
          || heightStr != null && heightStr.length() > 0) {
        if ("contain".equals(widthStr) || "contain".equals(heightStr)) {
          float rh = imageHeight / height;
          float rw = imageWidth / width;
          if (rh > rw) {
            actualHeight = height;
            actualWidth = (int) (imageWidth * height / imageHeight);
          } else {
            actualWidth = width;
            actualHeight = (int) (imageHeight * width / imageWidth);
          }

        } else if ("cover".equals(widthStr) || "cover".equals(heightStr)) {
          float rh = imageHeight / height;
          float rw = imageWidth / width;
          if (rh > rw) {
            actualWidth = width;
            actualHeight = (int) (imageHeight * width / imageWidth);
          } else {
            actualHeight = height;
            actualWidth = (int) (imageWidth * height / imageHeight);
          }
        } else {
          DimensionType widthDim = DimensionType.parserUnit(widthStr);
          DimensionType heightDim = DimensionType.parserUnit(heightStr);
          if (widthDim != null) {
            actualWidth = PropertyUtil.getDimensionValue(content, widthDim);
            if (heightDim == null) {
              actualHeight = (int) (imageHeight * actualWidth / imageWidth);
            } else {
              actualHeight = PropertyUtil.getDimensionValue(content, heightDim);
            }
          } else if (heightDim != null) {
            actualHeight = PropertyUtil.getDimensionValue(content, heightDim);
            if (widthDim == null) {
              actualWidth = (int) (imageWidth * actualHeight / imageHeight);
            } else {
              actualWidth = PropertyUtil.getDimensionValue(content, widthDim);
            }
          } else {
            actualHeight = (int) imageHeight;
            actualWidth = (int) imageWidth;
          }
        }
      }

      backgroundImage.setXOffset(
          getDimensionValue(
              cs.getProperty(IStyle.STYLE_BACKGROUND_POSITION_X), width - actualWidth));
      backgroundImage.setYOffset(
          getDimensionValue(
              cs.getProperty(IStyle.STYLE_BACKGROUND_POSITION_Y), height - actualHeight));
      backgroundImage.setHeight(actualHeight);
      backgroundImage.setWidth(actualWidth);
      return backgroundImage;
    }
    return null;
  }
コード例 #2
0
 private void scaleToOutputResolution(Image image) {
   float factor = _sharedContext.getDotsPerPixel();
   image.scaleAbsolute(image.getPlainWidth() * factor, image.getPlainHeight() * factor);
 }