Exemplo 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();
   }
 }
Exemplo n.º 2
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;
    }
  }