Example #1
0
  public void drawRenderedImage(BufferedImage img, AffineTransform at) {

    Log.d(
        "drawRenderedImage",
        "sx: "
            + at.getScaleX()
            + ", sy: "
            + at.getScaleY()
            + ", tx: "
            + at.getTranslateX()
            + ", ty: "
            + at.getTranslateY());

    Matrix m = new Matrix();
    float[] values = new float[9];
    values[Matrix.MSCALE_X] = (float) at.getScaleX();
    values[Matrix.MSCALE_Y] = (float) at.getScaleY();
    values[Matrix.MTRANS_X] = (float) at.getTranslateX();
    values[Matrix.MTRANS_Y] = (float) at.getTranslateY();
    values[Matrix.MSKEW_X] = (float) at.getShearX();
    values[Matrix.MSKEW_Y] = (float) at.getShearY();
    values[Matrix.MPERSP_2] = 1;
    m.setValues(values);

    canvas.drawBitmap(img.bm, m, paint);
  }
 private boolean i_validate(EAttribute a) {
   switch (a) {
     case EFAMILY:
       if (family == null || family.length() == 0) family = DEFAULT.family;
       return true;
     case EWEIGHT:
       return weight > 0 && weight < 10;
     case EWIDTH:
       return width >= .5f && width < 10;
     case EPOSTURE:
       return posture >= -1 && posture <= 1;
     case ESIZE:
       return size >= 0;
     case ETRANSFORM:
       if (transform != null && transform.isIdentity()) transform = DEFAULT.transform;
       return true;
     case ESUPERSCRIPT:
       return superscript >= -7 && superscript <= 7;
     case EFONT:
       return true;
     case ECHAR_REPLACEMENT:
       return true;
     case EFOREGROUND:
       return true;
     case EBACKGROUND:
       return true;
     case EUNDERLINE:
       return underline >= -1 && underline < 6;
     case ESTRIKETHROUGH:
       return true;
     case ERUN_DIRECTION:
       return runDirection >= -2 && runDirection <= 1;
     case EBIDI_EMBEDDING:
       return bidiEmbedding >= -61 && bidiEmbedding < 62;
     case EJUSTIFICATION:
       justification = max(0, min(justification, 1));
       return true;
     case EINPUT_METHOD_HIGHLIGHT:
       return true;
     case EINPUT_METHOD_UNDERLINE:
       return imUnderline >= -1 && imUnderline < 6;
     case ESWAP_COLORS:
       return true;
     case ENUMERIC_SHAPING:
       return true;
     case EKERNING:
       return kerning >= 0 && kerning <= 1;
     case ELIGATURES:
       return ligatures >= 0 && ligatures <= 1;
     case ETRACKING:
       return tracking >= -1 && tracking <= 10;
     default:
       throw new InternalError("unknown attribute: " + a);
   }
 }
  public void updateDerivedTransforms() {
    // this also updates the mask for the baseline transform
    if (transform == null) {
      baselineTransform = null;
      charTransform = null;
    } else {
      charTransform = new AffineTransform(transform);
      baselineTransform = extractXRotation(charTransform, true);

      if (charTransform.isIdentity()) {
        charTransform = null;
      }

      if (baselineTransform.isIdentity()) {
        baselineTransform = null;
      }
    }

    if (baselineTransform == null) {
      nondefault &= ~EBASELINE_TRANSFORM.mask;
    } else {
      nondefault |= EBASELINE_TRANSFORM.mask;
    }
  }
Example #4
0
  /**
   * Compute width of a single cell
   *
   * @param cell the cell whose width is to be calculated
   * @param defaultCharWidth the width of a single character
   * @param formatter formatter used to prepare the text to be measured
   * @param useMergedCells whether to use merged cells
   * @return the width in pixels
   */
  public static double getCellWidth(
      Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {

    Sheet sheet = cell.getSheet();
    Workbook wb = sheet.getWorkbook();
    Row row = cell.getRow();
    int column = cell.getColumnIndex();

    int colspan = 1;
    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
      CellRangeAddress region = sheet.getMergedRegion(i);
      if (containsCell(region, row.getRowNum(), column)) {
        if (!useMergedCells) {
          // If we're not using merged cells, skip this one and move on to the next.
          return -1;
        }
        cell = row.getCell(region.getFirstColumn());
        colspan = 1 + region.getLastColumn() - region.getFirstColumn();
      }
    }

    CellStyle style = cell.getCellStyle();
    int cellType = cell.getCellType();

    // for formula cells we compute the cell width for the cached formula result
    if (cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();

    Font font = wb.getFontAt(style.getFontIndex());

    AttributedString str;
    TextLayout layout;

    double width = -1;
    if (cellType == Cell.CELL_TYPE_STRING) {
      RichTextString rt = cell.getRichStringCellValue();
      String[] lines = rt.getString().split("\\n");
      for (int i = 0; i < lines.length; i++) {
        String txt = lines[i] + defaultChar;

        str = new AttributedString(txt);
        copyAttributes(font, str, 0, txt.length());

        if (rt.numFormattingRuns() > 0) {
          // TODO: support rich text fragments
        }

        layout = new TextLayout(str.getIterator(), fontRenderContext);
        if (style.getRotation() != 0) {
          /*
           * Transform the text using a scale so that it's height is increased by a multiple of the leading,
           * and then rotate the text before computing the bounds. The scale results in some whitespace around
           * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
           * is added by the standard Excel autosize.
           */
          AffineTransform trans = new AffineTransform();
          trans.concatenate(
              AffineTransform.getRotateInstance(style.getRotation() * 2.0 * Math.PI / 360.0));
          trans.concatenate(AffineTransform.getScaleInstance(1, fontHeightMultiple));
          width =
              Math.max(
                  width,
                  ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth)
                      + cell.getCellStyle().getIndention());
        } else {
          width =
              Math.max(
                  width,
                  ((layout.getBounds().getWidth() / colspan) / defaultCharWidth)
                      + cell.getCellStyle().getIndention());
        }
      }
    } else {
      String sval = null;
      if (cellType == Cell.CELL_TYPE_NUMERIC) {
        // Try to get it formatted to look the same as excel
        try {
          sval = formatter.formatCellValue(cell, dummyEvaluator);
        } catch (Exception e) {
          sval = String.valueOf(cell.getNumericCellValue());
        }
      } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
        sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase();
      }
      if (sval != null) {
        String txt = sval + defaultChar;
        str = new AttributedString(txt);
        copyAttributes(font, str, 0, txt.length());

        layout = new TextLayout(str.getIterator(), fontRenderContext);
        if (style.getRotation() != 0) {
          /*
           * Transform the text using a scale so that it's height is increased by a multiple of the leading,
           * and then rotate the text before computing the bounds. The scale results in some whitespace around
           * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
           * is added by the standard Excel autosize.
           */
          AffineTransform trans = new AffineTransform();
          trans.concatenate(
              AffineTransform.getRotateInstance(style.getRotation() * 2.0 * Math.PI / 360.0));
          trans.concatenate(AffineTransform.getScaleInstance(1, fontHeightMultiple));
          width =
              Math.max(
                  width,
                  ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth)
                      + cell.getCellStyle().getIndention());
        } else {
          width =
              Math.max(
                  width,
                  ((layout.getBounds().getWidth() / colspan) / defaultCharWidth)
                      + cell.getCellStyle().getIndention());
        }
      }
    }
    return width;
  }
 public void setTransform(AffineTransform f) {
   this.transform = (f == null || f.isIdentity()) ? DEFAULT.transform : new AffineTransform(f);
   updateDerivedTransforms();
   update(ETRANSFORM);
 }
  private static AffineTransform extractRotation(
      Point2D.Double pt, AffineTransform tx, boolean andTranslation) {

    tx.deltaTransform(pt, pt);
    AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);

    try {
      AffineTransform rtxi = rtx.createInverse();
      double dx = tx.getTranslateX();
      double dy = tx.getTranslateY();
      tx.preConcatenate(rtxi);
      if (andTranslation) {
        if (dx != 0 || dy != 0) {
          tx.setTransform(tx.getScaleX(), tx.getShearY(), tx.getShearX(), tx.getScaleY(), 0, 0);
          rtx.setTransform(
              rtx.getScaleX(), rtx.getShearY(), rtx.getShearX(), rtx.getScaleY(), dx, dy);
        }
      }
    } catch (NoninvertibleTransformException e) {
      return null;
    }
    return rtx;
  }