@Test
 public void verifyFontsizeTranslation() {
   AbstractTagProcessor a = new AbstractTagProcessor() {};
   Tag tag = new Tag("dummy");
   tag.getCSS().put("font-size", "16px");
   a.startElement(null, tag);
   Assert.assertEquals("12.0pt", tag.getCSS().get("font-size"));
 }
 @Test
 public void verifyStart() {
   AbstractTagProcessor a = new AbstractTagProcessor() {};
   Tag tag = new Tag("dummy");
   tag.getCSS().put("page-break-before", "always");
   List<Element> end = a.startElement(null, tag);
   Assert.assertEquals(Chunk.NEXTPAGE, end.get(0));
 }
 @Test
 public void verifyEnd() {
   AbstractTagProcessor a = new AbstractTagProcessor() {};
   Tag tag = new Tag("dummy");
   tag.getCSS().put("page-break-after", "always");
   List<Element> end = a.endElement(null, tag, new ArrayList<Element>());
   Assert.assertEquals(Chunk.NEXTPAGE, end.get(0));
 }
Пример #4
0
  /**
   * Applies css to a HtmlCell
   *
   * @param cell the HtmlCell
   * @param t the tag with the styles
   * @param memory current margin memory
   * @param psc the {@link PageSize} container
   * @return a styled HtmlCell
   */
  public HtmlCell apply(
      final HtmlCell cell, final Tag t, final MarginMemory memory, final PageSizeContainable psc) {
    Tag row = t.getParent();
    while (row != null && !row.getName().equals(HTML.Tag.TR)) {
      row = row.getParent();
    }
    Tag table = t.getParent();
    while (table != null && !table.getName().equals(HTML.Tag.TABLE)) {
      table = table.getParent();
    }

    final TableStyleValues values = Table.setBorderAttributeForCell(table);
    Map<String, String> css = t.getCSS();
    String emptyCells = css.get(CSS.Property.EMPTY_CELLS);
    if (null != emptyCells
        && CSS.Value.HIDE.equalsIgnoreCase(emptyCells)
        && cell.getCompositeElements() == null) {
      cell.setBorder(Rectangle.NO_BORDER);
    } else {
      cell.setVerticalAlignment(
          Element.ALIGN_MIDDLE); // Default css behavior. Implementation of "vertical-align" style
      // further along.
      String vAlign = null;
      if (t.getAttributes().containsKey(HTML.Attribute.VALIGN)) {
        vAlign = t.getAttributes().get(HTML.Attribute.VALIGN);
      } else if (css.containsKey(HTML.Attribute.VALIGN)) {
        vAlign = css.get(HTML.Attribute.VALIGN);
      } else if (row != null) {
        if (row.getAttributes().containsKey(HTML.Attribute.VALIGN)) {
          vAlign = row.getAttributes().get(HTML.Attribute.VALIGN);
        } else if (row.getCSS().containsKey(HTML.Attribute.VALIGN)) {
          vAlign = row.getCSS().get(HTML.Attribute.VALIGN);
        }
      }
      if (vAlign != null) {
        if (vAlign.equalsIgnoreCase(CSS.Value.TOP)) {
          cell.setVerticalAlignment(Element.ALIGN_TOP);
        } else if (vAlign.equalsIgnoreCase(CSS.Value.BOTTOM)) {
          cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        }
      }

      String align = null;
      if (t.getAttributes().containsKey(HTML.Attribute.ALIGN)) {
        align = t.getAttributes().get(HTML.Attribute.ALIGN);
      } else if (css.containsKey(CSS.Property.TEXT_ALIGN)) {
        align = css.get(CSS.Property.TEXT_ALIGN);
      }

      if (align != null) {
        if (align.equalsIgnoreCase(CSS.Value.CENTER)) {
          cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } else if (align.equalsIgnoreCase(CSS.Value.RIGHT)) {
          cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        } else if (align.equalsIgnoreCase(CSS.Value.JUSTIFY)) {
          cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        }
      }

      if (t.getAttributes().get(HTML.Attribute.WIDTH) != null
          || css.get(HTML.Attribute.WIDTH) != null) {
        cell.setFixedWidth(
            new WidthCalculator().getWidth(t, memory.getRootTags(), psc.getPageSize().getWidth()));
      }

      HeightCalculator heightCalc = new HeightCalculator();
      Float height = heightCalc.getHeight(t, psc.getPageSize().getHeight());
      if (height == null && row != null) {
        height = heightCalc.getHeight(row, psc.getPageSize().getHeight());
      }
      if (height != null) {
        cell.setMinimumHeight(height);
      }

      String colspan = t.getAttributes().get(HTML.Attribute.COLSPAN);
      if (null != colspan) {
        cell.setColspan(Integer.parseInt(colspan));
      }
      String rowspan = t.getAttributes().get(HTML.Attribute.ROWSPAN);
      if (null != rowspan) {
        cell.setRowspan(Integer.parseInt(rowspan));
      }
      for (Entry<String, String> entry : css.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        cell.setUseBorderPadding(true);
        if (key.equalsIgnoreCase(CSS.Property.BACKGROUND_COLOR)) {
          values.setBackground(HtmlUtilities.decodeColor(value));
        } else if (key.equalsIgnoreCase(CSS.Property.VERTICAL_ALIGN)) {
          if (value.equalsIgnoreCase(CSS.Value.TOP)) {
            cell.setVerticalAlignment(Element.ALIGN_TOP);
            cell.setPaddingTop(cell.getPaddingTop() + 6);
          } else if (value.equalsIgnoreCase(CSS.Value.BOTTOM)) {
            cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
            cell.setPaddingBottom(cell.getPaddingBottom() + 6);
          }
        } else if (key.contains(CSS.Property.BORDER)) {
          if (key.contains(CSS.Value.TOP)) {
            setTopOfBorder(cell, key, value, values);
          } else if (key.contains(CSS.Value.BOTTOM)) {
            setBottomOfBorder(cell, key, value, values);
          } else if (key.contains(CSS.Value.LEFT)) {
            setLeftOfBorder(cell, key, value, values);
          } else if (key.contains(CSS.Value.RIGHT)) {
            setRightOfBorder(cell, key, value, values);
          }
        } else if (key.contains(CSS.Property.CELLPADDING) || key.contains(CSS.Property.PADDING)) {
          if (key.contains(CSS.Value.TOP)) {
            cell.setPaddingTop(cell.getPaddingTop() + utils.parsePxInCmMmPcToPt(value));
          } else if (key.contains(CSS.Value.BOTTOM)) {
            cell.setPaddingBottom(cell.getPaddingBottom() + utils.parsePxInCmMmPcToPt(value));
          } else if (key.contains(CSS.Value.LEFT)) {
            cell.setPaddingLeft(cell.getPaddingLeft() + utils.parsePxInCmMmPcToPt(value));
          } else if (key.contains(CSS.Value.RIGHT)) {
            cell.setPaddingRight(cell.getPaddingRight() + utils.parsePxInCmMmPcToPt(value));
          }
        } else if (key.contains(CSS.Property.TEXT_ALIGN)) {
          cell.setHorizontalAlignment(CSS.getElementAlignment(value));
        }
      }
      cell.setPaddingLeft(
          cell.getPaddingLeft() + values.getHorBorderSpacing() + values.getBorderWidthLeft());
      cell.setPaddingRight(cell.getPaddingRight() + values.getBorderWidthRight());
      cell.setPaddingTop(
          cell.getPaddingTop() + values.getVerBorderSpacing() + values.getBorderWidthTop());
      cell.setPaddingBottom(cell.getPaddingBottom() + values.getBorderWidthBottom());
    }
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setCellEvent(new CellSpacingEvent(values));
    cell.setCellValues(values);
    return cell;
  }