Commit ad163bee authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved box layout

parent 6c49fc0e
Loading
Loading
Loading
Loading
+70 −1
Original line number Diff line number Diff line
@@ -2,7 +2,71 @@ width: 1920px;
height: 1080px;

background-color: #000;
margin: 4em;
margin: 1em;

box {
  background-color: #f00;
  position: top;
  height: 200;

  margin: 1em;

  box {
    background-color: #000;
    position: left;
    width: 200;
  }

  box {
    background-color: #fff;
    position: left;
    width: 200;
  }

  box {
    background-color: #000;
    position: right;
    width: 200;
  }

  box {
    background-color: #fff;
    position: right;
    width: 200;
  }
}

box {
  background-color: #f00;
  position: bottom;
  height: 200;

  margin: 1em;

  box {
    background-color: #000;
    position: left;
    width: 200;
  }

  box {
    background-color: #fff;
    position: left;
    width: 200;
  }

  box {
    background-color: #000;
    position: right;
    width: 200;
  }

  box {
    background-color: #fff;
    position: right;
    width: 200;
  }
}

box {
  margin: 8em;
@@ -10,5 +74,10 @@ box {

  box {
    background-color: #00f;

    box {
      background-color: #fff;
      width: 500;
    }
  }
}
+22 −0
Original line number Diff line number Diff line
@@ -211,6 +211,28 @@ ReturnCode configure_direction(
  return parseEnum(defs, prop.value, value);
}

ReturnCode configure_position(
    const plist::Property& prop,
    Position* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  static const EnumDefinitions<Position> defs = {
    { "relative", Position::RELATIVE },
    { "absolute", Position::ABSOLUTE },
    { "top", Position::TOP },
    { "right", Position::RIGHT },
    { "bottom", Position::BOTTOM },
    { "left", Position::LEFT },
  };

  return parseEnum(defs, prop.value, value);
}

ReturnCode load_csv(
    const std::string& csv_path,
    bool csv_headers,
+5 −2
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
#include "utils/return_code.h"
#include "source/data_model.h"
#include "source/dimension.h"
#include "core/layout.h"

namespace plotfx {

@@ -82,8 +83,6 @@ ReturnCode configure_measure(

ReturnCode configure_measure_opt(
    const plist::Property& prop,
    double dpi,
    double font_size,
    std::optional<Measure>* value);

ReturnCode configure_color(
@@ -105,6 +104,10 @@ ReturnCode configure_direction(
    const plist::Property& prop,
    Direction* value);

ReturnCode configure_position(
    const plist::Property& prop,
    Position* value);

ReturnCode configure_float_opt(
    const plist::Property& prop,
    std::optional<double>* value);

source/core/layout.cc

0 → 100644
+123 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "layout.h"
#include "graphics/layout.h"

namespace plotfx {

LayoutSettings::LayoutSettings() : position(Position::RELATIVE) {}

ReturnCode layout_compute(
    const LayoutSettings& config,
    LayoutInfo* parent_layout,
    LayoutInfo* layout) {
  layout->content_box = layout->bounding_box;
  auto width = config.width;
  auto width_min = width.value_or(from_unit(0));
  auto width_max = width.value_or(from_unit(parent_layout->content_box.w));

  auto height = config.height;
  auto height_min = height.value_or(from_unit(0));
  auto height_max = height.value_or(from_unit(parent_layout->content_box.h));

  double extent[4] = {0, 0, 0, 0};
  switch (config.position) {

    case Position::RELATIVE:
      layout->bounding_box = Rectangle(
          parent_layout->content_box.x,
          parent_layout->content_box.y,
          width_max,
          height_max);

      layout->content_box = layout->bounding_box;
      break;

    case Position::TOP: {
      layout->bounding_box = Rectangle(
          parent_layout->content_box.x,
          parent_layout->content_box.y,
          width_max,
          height_min);

      layout->content_box = layout->bounding_box;
      extent[0] = height_min;
      break;
    }

    case Position::RIGHT: {
      layout->bounding_box = Rectangle(
          parent_layout->content_box.x + parent_layout->content_box.w - width_min,
          parent_layout->content_box.y,
          width_min,
          height_max);

      layout->content_box = layout->bounding_box;
      extent[1] = width_min;
      break;
    }

    case Position::BOTTOM: {
      layout->bounding_box = Rectangle(
          parent_layout->content_box.x,
          parent_layout->content_box.y + parent_layout->content_box.h - height_min,
          width_max,
          height_min);

      layout->content_box = layout->bounding_box;
      extent[2] = height_min;
      break;
    }

    case Position::LEFT: {
      layout->bounding_box = Rectangle(
          parent_layout->content_box.x,
          parent_layout->content_box.y,
          width_min,
          height_max);

      layout->content_box = layout->bounding_box;
      extent[3] = width_min;
      break;
    }

  }

  parent_layout->content_box = layout_margin_box(
      parent_layout->content_box,
      extent[0],
      extent[1],
      extent[2],
      extent[3]);

  return OK;
}
} // namespace plotfx
+22 −4
Original line number Diff line number Diff line
@@ -29,10 +29,12 @@
 */
#pragma once
#include "graphics/geometry.h"
#include "graphics/measure.h"

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <optional>

namespace plotfx {

@@ -54,12 +56,28 @@ struct LayoutInfo {
   */
  Rectangle content_box;

  /**
   * The elements bounding box
   */
  Rectangle element_box;
};

enum class Position {
  RELATIVE,
  ABSOLUTE,
  TOP,
  RIGHT,
  BOTTOM,
  LEFT
};

struct LayoutSettings {
  LayoutSettings();
  Position position;
  std::optional<Measure> width;
  std::optional<Measure> height;
};

ReturnCode layout_compute(
    const LayoutSettings& config,
    LayoutInfo* parent_layout,
    LayoutInfo* layout);

} // namespace plotfx
Loading