Esempio n. 1
0
 private static Optional<Color> getColor(LayoutBox layoutBox, String name) {
   switch (layoutBox.getType()) {
     case BLOCK_NODE:
     case INLINE_NODE:
       return ((StyledLayoutBox) layoutBox)
           .getStyledNode()
           .value(name)
           .filter(val -> (val instanceof ColorValue))
           .map(val -> ((ColorValue) val).getColor());
     case ANONYMOUS_BLOCK:
     default:
       return Optional.empty();
   }
 }
Esempio n. 2
0
  private static void renderBorders(List<DisplayCommand> displayList, LayoutBox layoutBox) {
    Optional<Color> colorVal = getColor(layoutBox, "border-color");

    if (colorVal.isPresent()) {
      Color color = colorVal.get();
      Dimensions dims = layoutBox.getDimensions();
      Rect borderBox = dims.borderBox();

      // left border
      displayList.add(
          new SolidColor(
              color, new Rect(borderBox.x, borderBox.y, dims.border.left, borderBox.height)));
      // right border
      displayList.add(
          new SolidColor(
              color,
              new Rect(
                  borderBox.x + borderBox.width - dims.border.right,
                  borderBox.y,
                  dims.border.right,
                  borderBox.height)));
      // top border
      displayList.add(
          new SolidColor(
              color, new Rect(borderBox.x, borderBox.y, borderBox.width, dims.border.top)));
      // bottom border
      displayList.add(
          new SolidColor(
              color,
              new Rect(
                  borderBox.x,
                  borderBox.y + borderBox.height - dims.border.bottom,
                  borderBox.width,
                  dims.border.bottom)));
    }
  }
Esempio n. 3
0
  private static void renderLayoutBox(List<DisplayCommand> displayList, LayoutBox layoutBox) {
    System.out.println(layoutBox);
    switch (layoutBox.getType()) {
      case BLOCK_NODE:
        renderBackground(displayList, layoutBox);
        renderBorders(displayList, layoutBox);
        for (LayoutBox child : layoutBox.getChildren()) {
          renderLayoutBox(displayList, child);
        }
        break;
      case INLINE_NODE:
        InlineBox box = (InlineBox) layoutBox;
        Optional<Color> colorVal = getColor(layoutBox, "border-color");
        Dimensions dims = layoutBox.getDimensions();
        Rect borderBox = dims.borderBox();
        Deque<LineBox> lines = box.getLines();

        if (colorVal.isPresent()) {
          Color color = colorVal.get();
          LineBox firstLine = lines.getFirst();
          float boxHeight = firstLine.getBoxHeight();
          float lineHeight = firstLine.getLineHeight();
          float borderHeight = lineHeight + dims.border.top + dims.border.bottom;

          // left border
          displayList.add(
              new SolidColor(
                  color,
                  new Rect(
                      borderBox.x,
                      borderBox.y + boxHeight - lineHeight,
                      dims.border.left,
                      borderHeight)));

          LineBox lastLine = lines.getLast();
          // right border
          displayList.add(
              new SolidColor(
                  color,
                  new Rect(
                      borderBox.x
                          + lastLine.getFilledWidth()
                          + dims.border.left
                          + dims.border.right,
                      borderBox.y + borderHeight * (lines.size() - 1),
                      dims.border.right,
                      borderHeight)));

          float lineX = dims.content.x - dims.border.left;
          float lineY = dims.content.y;

          for (LineBox line : box.getLines()) {
            // TODO proper line-height calculations
            float lineWidth = line.getFilledWidth() + dims.border.left + dims.border.right;

            // top border
            displayList.add(
                new SolidColor(
                    color, new Rect(lineX, lineY - dims.border.top, lineWidth, dims.border.top)));
            // bottom border
            displayList.add(
                new SolidColor(
                    color, new Rect(lineX, lineY + lineHeight, lineWidth, dims.border.bottom)));

            lineY += borderHeight;
          }
        }
        break;
      default:
        break;
    }
  }
Esempio n. 4
0
 private static void renderBackground(List<DisplayCommand> displayList, LayoutBox layoutBox) {
   getColor(layoutBox, "background")
       .map(
           color -> displayList.add(new SolidColor(color, layoutBox.getDimensions().borderBox())));
 }