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

implement svg text rotation

parent 17dce301
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ struct BrushFillOp {
struct TextSpanOp {
  std::string text;
  Point position;
  double rotate;
  Point rotate_pivot;
  TextStyle style;
};

+11 −0
Original line number Diff line number Diff line
@@ -78,6 +78,16 @@ Status svg_text_span(
    SVGDataRef svg) {
  const auto& style = op.style;

  std::string transform;
  if (op.rotate) {
    transform = svg_attr(
        "transform",
        fmt::format("rotate({} {} {})",
        op.rotate,
        op.rotate_pivot.x,
        op.rotate_pivot.y));
  }

  svg->buffer
    << "  "
    << "<text"
@@ -86,6 +96,7 @@ Status svg_text_span(
    << svg_attr("fill", style.color.to_hex_str())
    << svg_attr("font-size", style.font_size)
    << svg_attr("font-family", style.font.font_family_css)
    << transform
    << ">"
    << svg_body(op.text)
    << "</text>"
+13 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ Status drawTextLabel(
    const Point& position,
    HAlign align_x,
    VAlign align_y,
    double rotate,
    const TextStyle& style,
    Layer* layer) {
  Rectangle bbox;
@@ -50,10 +51,22 @@ Status drawTextLabel(
  layer_ops::TextSpanOp op;
  op.text = text;
  op.position = offset;
  op.rotate = rotate;
  op.rotate_pivot = position;
  op.style = style;

  return layer->apply(op);
}

Status drawTextLabel(
    const std::string& text,
    const Point& position,
    HAlign align_x,
    VAlign align_y,
    const TextStyle& style,
    Layer* layer) {
  return drawTextLabel(text, position, align_x, align_y, 0, style, layer);
}

} // namespace fviz
+9 −0
Original line number Diff line number Diff line
@@ -38,6 +38,15 @@ struct TextStyle {
  Color color;
};

Status drawTextLabel(
    const std::string& text,
    const Point& position,
    HAlign align_x,
    VAlign align_y,
    double rotate,
    const TextStyle& text_style,
    Layer* layer);

Status drawTextLabel(
    const std::string& text,
    const Point& position,
+76 −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 "graphics/brush.h"
#include "graphics/color.h"
#include "graphics/layer.h"
#include "graphics/layer_svg.h"
#include "graphics/text.h"
#include "environment.h"
#include "utils/fileutil.h"
#include "unittest.h"

using namespace fviz;

void draw_test(
    const Environment& env,
    Layer* l,
    uint32_t x,
    uint32_t y,
    double a,
    HAlign ax,
    VAlign ay) {
  TextStyle ts;
  ts.font = env.font;
  ts.font_size = from_unit(12);

  StrokeStyle ss;
  ss.line_width = from_unit(1);

  strokeLine(l, Point(x - 10, y), Point(x + 10, y), ss);
  strokeLine(l, Point(x, y - 10), Point(x, y + 10), ss);
  drawTextLabel("Ijsselmeerdijk", Point(x, y), ax, ay, a, ts, l);
}

int main(int argc, char** argv) {
  Environment env;
  EXPECT_OK(environment_setup_defaults(&env));

  LayerRef layer;
  auto rc = layer_bind_svg(
      800,
      500,
      96,
      from_unit(12),
      Color::fromRGB(1.0, 1.0, 1.0),
      [&] (auto svg) {
        FileUtil::write(std::string(argv[0]) + ".svg", Buffer(svg.data(), svg.size()));
        return OK;
      },
      &layer);

  draw_test(env, layer.get(), 100,  100, 45, HAlign::LEFT, VAlign::TOP);
  draw_test(env, layer.get(), 400,  100, 45, HAlign::CENTER, VAlign::TOP);
  draw_test(env, layer.get(), 700,  100, 45, HAlign::RIGHT, VAlign::TOP);

  draw_test(env, layer.get(), 100,  200, 45, HAlign::LEFT, VAlign::CENTER);
  draw_test(env, layer.get(), 400,  200, 45, HAlign::CENTER, VAlign::CENTER);
  draw_test(env, layer.get(), 700,  200, 45, HAlign::RIGHT, VAlign::CENTER);

  draw_test(env, layer.get(), 100,  300, 45, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(env, layer.get(), 400,  300, 45, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(env, layer.get(), 700,  300, 45, HAlign::RIGHT, VAlign::BOTTOM);

  layer_submit(layer.get());
}