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

add the 'layout/padding' element

parent de6ad605
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ include_directories(${CAIRO_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_IN

# Build: fviz Library
# -----------------------------------------------------------------------------
file(GLOB source_files "core/graphics/*.cc" "core/utils/**.cc" "core/sexpr*.cc" "core/fviz.cc" "core/scale.cc" "core/environment.cc" "elements/text.cc" "core/element_factory.cc" "core/config_helpers.cc" "elements/chart/axis.cc" "core/format.cc" "core/layout.cc" "core/data_model.cc" "elements/chart/layout.cc" "elements/fill.cc" "elements/chart/points.cc")
file(GLOB source_files "core/graphics/*.cc" "core/utils/**.cc" "core/sexpr*.cc" "core/fviz.cc" "core/scale.cc" "core/environment.cc" "elements/text.cc" "core/element_factory.cc" "core/config_helpers.cc" "elements/chart/axis.cc" "core/format.cc" "core/layout.cc" "core/data_model.cc" "elements/chart/layout.cc" "elements/fill.cc" "elements/chart/points.cc" "elements/layout/padding.cc")
list(REMOVE_ITEM source_files "core/cli.cc")
add_library(fviz STATIC ${source_files})
set_target_properties(fviz PROPERTIES PUBLIC_HEADER "source/fviz.h;source/fviz_sdl.h")
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "elements/chart/axis.h"
#include "elements/chart/layout.h"
#include "elements/chart/points.h"
#include "elements/layout/padding.h"

#include <iostream>
#include <string.h>
@@ -50,6 +51,7 @@ fviz_t* fviz_init() {
  element_bind(elems, "chart/axis-left", bind(elements::chart::axis::build, _1, _2, _3));
  element_bind(elems, "chart/layout", bind(elements::chart::layout::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));
  return ctx.release();
}

+93 −0
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::layout::padding {

struct PaddingElement {
  Measure font_size;
  std::array<Measure, 4> padding;
  std::vector<ElementRef> elements;
};

ReturnCode draw(
    std::shared_ptr<PaddingElement> config,
    const LayoutInfo& layout,
    Layer* layer) {
  /* convert units  */
  auto padding = config->padding;
  for (auto& m : padding) {
    convert_unit_typographic(layer->dpi, config->font_size, &m);
  }

  auto body_box = layout_margin_box(
      layout.content_box,
      padding[0],
      padding[1],
      padding[2],
      padding[3]);

  for (const auto& e : config->elements) {
    LayoutInfo layout;
    layout.content_box = body_box;

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

  return OK;
}

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

  auto config_rc = expr_walk_map(expr_next(expr), {
    {
      "padding",
      expr_calln_fn({
        bind(&expr_to_measure, _1, &config->padding[0]),
        bind(&expr_to_measure, _1, &config->padding[1]),
        bind(&expr_to_measure, _1, &config->padding[2]),
        bind(&expr_to_measure, _1, &config->padding[3]),
      })
    },
    {"padding-top", bind(&expr_to_measure, _1, &config->padding[0])},
    {"padding-right", bind(&expr_to_measure, _1, &config->padding[1])},
    {"padding-bottom", bind(&expr_to_measure, _1, &config->padding[2])},
    {"padding-left", bind(&expr_to_measure, _1, &config->padding[3])},
    {"body", bind(&element_build_list, env, _1, &config->elements)},
  });

  if (!config_rc) {
    return config_rc;
  }

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

} // namespace fviz::elements::layout::padding
+25 −0
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::layout::padding {

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

} // namespace fviz::elements::layout::padding