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

update the 'layout/box' element, delete redundant elements

parent 16e6ac2e
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
#include "graphics/layer_pixmap.h"
#include "utils/fileutil.h"
#include "core/environment.h"
#include "elements/fill.h"
#include "elements/text.h"
#include "elements/chart/axis.h"
#include "elements/chart/layout.h"
@@ -28,7 +27,7 @@
#include "elements/chart/labels.h"
#include "elements/chart/lines.h"
#include "elements/chart/points.h"
#include "elements/layout/padding.h"
#include "elements/layout/box.h"

#include <iostream>
#include <string.h>
@@ -46,7 +45,6 @@ struct fviz_s {
fviz_t* fviz_init() {
  auto ctx = std::make_unique<fviz_t>();
  auto elems = &ctx->env.element_map;
  element_bind(elems, "fill", bind(elements::fill::build, _1, _2, _3));
  element_bind(elems, "text", bind(elements::text::build, _1, _2, _3));
  element_bind(elems, "chart/axis-top", bind(elements::chart::axis::build, _1, _2, _3));
  element_bind(elems, "chart/axis-right", bind(elements::chart::axis::build, _1, _2, _3));
@@ -57,7 +55,7 @@ fviz_t* fviz_init() {
  element_bind(elems, "chart/labels", bind(elements::chart::labels::build, _1, _2, _3));
  element_bind(elems, "chart/lines", bind(elements::chart::lines::build, _1, _2, _3));
  element_bind(elems, "chart/points", bind(elements::chart::points::build, _1, _2, _3));
  element_bind(elems, "layout/padding", bind(elements::layout::padding::build, _1, _2, _3));
  element_bind(elems, "layout/box", bind(elements::layout::box::build, _1, _2, _3));
  return ctx.release();
}

elements/chart/box.h

deleted100644 → 0
+0 −61
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include "sexpr.h"
#include "core/environment.h"
#include "element.h"
#include "core/layout.h"

namespace fviz {
namespace box {

struct BoxBorderConfig {
  Color color;
  Measure width;
};

struct BoxConfig {
  std::vector<ElementRef> children;
  FontInfo font;
  Measure font_size;
  ColorScheme color_scheme;
  std::optional<Color> background;
  Color text_color;
  Color border_color;
  std::array<Measure, 4> margins;
  std::array<BoxBorderConfig, 4> borders;
  LayoutSettings layout;
};

ReturnCode draw(
    const BoxConfig& config,
    const LayoutInfo& layout,
    Layer* layer);

ReturnCode reflow(
    const BoxConfig& config,
    const Layer& layer,
    const std::optional<double> max_width,
    const std::optional<double> max_height,
    double* min_width,
    double* min_height);

ReturnCode configure(
    const plist::PropertyList& plist,
    const Environment& env,
    BoxConfig* config);

} // namespace box
} // namespace fviz

elements/fill.cc

deleted100644 → 0
+0 −65
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "config_helpers.h"
#include "environment.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "graphics/color.h"

using namespace std::placeholders;

namespace fviz::elements::fill {

struct FillElement {
  Color color;
};

ReturnCode draw(
    std::shared_ptr<FillElement> config,
    const LayoutInfo& layout,
    Layer* layer) {
  FillStyle style;
  style.color = config->color;

  fillRectangle(
      layer,
      Point(layout.content_box.x, layout.content_box.y),
      layout.content_box.w,
      layout.content_box.h,
      style);

  return OK;
}

ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem) {
  auto config = std::make_shared<FillElement>();

  auto config_rc = expr_walk_map(expr_next(expr), {
    {"color", bind(&expr_to_color, _1, &config->color)},
  });

  if (!config_rc) {
    return config_rc;
  }

  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, config, _1, _2);
  return OK;
}

} // namespace fviz::elements::fill

elements/fill.h

deleted100644 → 0
+0 −25
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include "element.h"

namespace fviz::elements::fill {

ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem);

} // namespace fviz::elements::fill
+191 −0
Original line number Diff line number Diff line
@@ -11,56 +11,58 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "elements/box.h"
#include "element_factory.h"
#include "config_helpers.h"
#include "box.h"

#include "environment.h"
#include "layout.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "graphics/color.h"
#include "graphics/layer.h"

#include <functional>

using namespace std::placeholders;

namespace fviz {
namespace box {
namespace fviz::elements::layout::box {

struct BoxBorderConfig {
  Color color;
  Measure width;
};

struct BoxElement {
  Measure font_size;
  std::optional<Color> background;
  Color border_color;
  std::array<Measure, 4> margins;
  std::array<BoxBorderConfig, 4> borders;
  std::vector<ElementRef> elements;
};

ReturnCode draw(
    const BoxConfig& config,
    std::shared_ptr<BoxElement> config,
    const LayoutInfo& layout,
    Layer* layer) {
  const auto bbox = layout.content_box;

  /* convert units  */
  auto margins = config.margins;
  auto margins = config->margins;
  for (auto& m : margins) {
    convert_unit_typographic(layer->dpi, config.font_size, &m);
    convert_unit_typographic(layer->dpi, config->font_size, &m);
  }

  /* calculate margin box */
  auto margin_box = layout_margin_box(
      bbox,
  /* calculate body box */
  auto bbox = layout_margin_box(
      layout.content_box,
      margins[0],
      margins[1],
      margins[2],
      margins[3]);

  /* layout children */
  std::vector<ElementPlacement> children;
  for (const auto& c : config.children) {
    ElementPlacement e;
    e.element = c;
    children.emplace_back(e);
  }

  Rectangle content_box;
  if (auto rc = layout_elements(*layer, margin_box, &children, &content_box); !rc) {
    return rc;
  }

  /* draw background */
  if (config.background) {
  if (config->background) {
    const auto& bg_box = bbox;
    FillStyle bg_fill;
    bg_fill.color = *config.background;
    bg_fill.color = *config->background;

    fillRectangle(
        layer,
@@ -71,17 +73,20 @@ ReturnCode draw(
  }

  /* draw children */
  for (const auto& c : children) {
    if (auto rc = c.element->draw(c.layout, layer); !rc.isSuccess()) {
  for (const auto& e : config->elements) {
    LayoutInfo layout;
    layout.content_box = bbox;

    if (auto rc = e->draw(layout, layer); !rc.isSuccess()) {
      return rc;
    }
  }

  /* draw top border  */
  if (config.borders[0].width > 0) {
  if (config->borders[0].width > 0) {
    StrokeStyle border_style;
    border_style.line_width = config.borders[0].width;
    border_style.color = config.borders[0].color;
    border_style.line_width = config->borders[0].width;
    border_style.color = config->borders[0].color;

    strokeLine(
        layer,
@@ -91,10 +96,10 @@ ReturnCode draw(
  }

  /* draw right border  */
  if (config.borders[1].width > 0) {
  if (config->borders[1].width > 0) {
    StrokeStyle border_style;
    border_style.line_width = config.borders[1].width;
    border_style.color = config.borders[1].color;
    border_style.line_width = config->borders[1].width;
    border_style.color = config->borders[1].color;

    strokeLine(
        layer,
@@ -104,10 +109,10 @@ ReturnCode draw(
  }

  /* draw top border  */
  if (config.borders[2].width > 0) {
  if (config->borders[2].width > 0) {
    StrokeStyle border_style;
    border_style.line_width = config.borders[2].width;
    border_style.color = config.borders[2].color;
    border_style.line_width = config->borders[2].width;
    border_style.color = config->borders[2].color;

    strokeLine(
        layer,
@@ -117,10 +122,10 @@ ReturnCode draw(
  }

  /* draw left border  */
  if (config.borders[3].width > 0) {
  if (config->borders[3].width > 0) {
    StrokeStyle border_style;
    border_style.line_width = config.borders[3].width;
    border_style.color = config.borders[3].color;
    border_style.line_width = config->borders[3].width;
    border_style.color = config->borders[3].color;

    strokeLine(
        layer,
@@ -132,103 +137,55 @@ ReturnCode draw(
  return OK;
}

ReturnCode reflow(
    const BoxConfig& config,
    const Layer& layer,
    const std::optional<double> max_width,
    const std::optional<double> max_height,
    double* min_width,
    double* min_height) {
  return OK; // TODO
}

ReturnCode configure(
    const plist::PropertyList& plist,
ReturnCode build(
    const Environment& env,
    BoxConfig* config) {
  config->font = env.font;
    const Expr* expr,
    ElementRef* elem) {
  auto config = std::make_shared<BoxElement>();
  config->font_size = env.font_size;
  config->color_scheme = env.color_scheme;
  config->text_color = env.text_color;
  config->border_color = env.border_color;

  ParserDefinitions pdefs = {
    {"position", bind(&configure_position, _1, &config->layout.position)},
    {"width", bind(&configure_measure_opt, _1, &config->layout.width)},
    {"height", bind(&configure_measure_opt, _1, &config->layout.height)},
  auto config_rc = expr_walk_map(expr_next(expr), {
    {
      "margin",
      configure_multiprop({
        bind(&configure_measure, _1, &config->margins[0]),
        bind(&configure_measure, _1, &config->margins[1]),
        bind(&configure_measure, _1, &config->margins[2]),
        bind(&configure_measure, _1, &config->margins[3]),
      expr_calln_fn({
        bind(&expr_to_measure, _1, &config->margins[0]),
        bind(&expr_to_measure, _1, &config->margins[1]),
        bind(&expr_to_measure, _1, &config->margins[2]),
        bind(&expr_to_measure, _1, &config->margins[3]),
      })
    },
    {"margin-top", bind(&configure_measure, _1, &config->margins[0])},
    {"margin-right", bind(&configure_measure, _1, &config->margins[1])},
    {"margin-bottom", bind(&configure_measure, _1, &config->margins[2])},
    {"margin-left", bind(&configure_measure, _1, &config->margins[3])},
    {"border-top-color", bind(&configure_color, _1, &config->borders[0].color)},
    {"border-right-color", bind(&configure_color, _1, &config->borders[1].color)},
    {"border-bottom-color", bind(&configure_color, _1, &config->borders[2].color)},
    {"border-left-color", bind(&configure_color, _1, &config->borders[3].color)},
    {"border-top-width", bind(&configure_measure, _1, &config->borders[0].width)},
    {"border-right-width", bind(&configure_measure, _1, &config->borders[1].width)},
    {"border-bottom-width", bind(&configure_measure, _1, &config->borders[2].width)},
    {"border-left-width", bind(&configure_measure, _1, &config->borders[3].width)},
    {"background-color", configure_color_opt(&config->background)},
    {"margin-top", bind(&expr_to_measure, _1, &config->margins[0])},
    {"margin-right", bind(&expr_to_measure, _1, &config->margins[1])},
    {"margin-bottom", bind(&expr_to_measure, _1, &config->margins[2])},
    {"margin-left", bind(&expr_to_measure, _1, &config->margins[3])},
    {"border-top-color", bind(&expr_to_color, _1, &config->borders[0].color)},
    {"border-right-color", bind(&expr_to_color, _1, &config->borders[1].color)},
    {"border-bottom-color", bind(&expr_to_color, _1, &config->borders[2].color)},
    {"border-left-color", bind(&expr_to_color, _1, &config->borders[3].color)},
    {"border-top-width", bind(&expr_to_measure, _1, &config->borders[0].width)},
    {"border-right-width", bind(&expr_to_measure, _1, &config->borders[1].width)},
    {"border-bottom-width", bind(&expr_to_measure, _1, &config->borders[2].width)},
    {"border-left-width", bind(&expr_to_measure, _1, &config->borders[3].width)},
    {"background-color", bind(&expr_to_color_opt, _1, &config->background)},
    {
      "foreground-color",
      configure_multiprop({
          bind(&configure_color, _1, &config->text_color),
          bind(&configure_color, _1, &config->border_color),
      expr_calln_fn({
        bind(&expr_to_color, _1, &config->border_color),
      })
    },
    {"text-color", bind(&configure_color, _1, &config->text_color)},
    {"border-color", bind(&configure_color, _1, &config->border_color)},
  };

  if (auto rc = parseAll(plist, pdefs); !rc) {
    return rc;
  }

  Environment child_env;
  child_env.screen_width = env.screen_width;
  child_env.screen_height = env.screen_height;
  child_env.dpi = env.dpi;
  child_env.font = config->font;
  child_env.font_size = config->font_size;
  child_env.color_scheme = config->color_scheme;
  child_env.text_color = config->text_color;
  child_env.border_color = config->border_color;
  child_env.background_color = config->background.value_or(env.background_color);

  for (size_t i = 0; i < plist.size(); ++i) {
    if (!plist::is_map(plist[i])) {
      continue;
    }

    const auto& elem_name = plist[i].name;
    const auto& elem_config = plist[i].next.get();

    ElementRef elem;
    auto rc = buildElement(
        elem_name,
        *elem_config,
        child_env,
        &elem);

    if (!rc) {
      return rc;
    }
    {"border-color", bind(&expr_to_color, _1, &config->border_color)},
    {"body", bind(&element_build_list, env, _1, &config->elements)},
  });

    config->children.emplace_back(std::move(elem));
  if (!config_rc) {
    return config_rc;
  }

  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, config, _1, _2);
  return OK;
}

} // namespace box
} // namespace fviz
} // namespace fviz::elements::layout::margins
Loading