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

extend the drawTextLabel api for text spans

parent 310d63c6
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -22,21 +22,24 @@ TextStyle::TextStyle() :
    direction(TextDirection::LTR) {}

Status drawTextLabel(
    const std::string& text,
    const text::TextSpan* text_begin,
    const text::TextSpan* text_end,
    const Point& position,
    HAlign align_x,
    VAlign align_y,
    double rotate,
    const TextStyle& style,
    Layer* layer) {
  text::TextSpan span;
  span.text = text;
  std::string text;
  for (auto text_iter = text_begin; text_iter != text_end; ++text_iter) {
    text += text_iter->text;
  }

  Rectangle bbox;
  std::vector<text::GlyphSpan> glyphs;
  auto rc = text::text_layout_hline(
      &span,
      &span + 1,
      text_begin,
      text_end,
      style.direction,
      style.font,
      style.font_size,
@@ -72,6 +75,19 @@ Status drawTextLabel(
  return layer->apply(op);
}

Status drawTextLabel(
    const std::string& text,
    const Point& position,
    HAlign align_x,
    VAlign align_y,
    double rotate,
    const TextStyle& style,
    Layer* layer) {
  text::TextSpan span;
  span.text = text;
  return drawTextLabel(&span, &span + 1, position, align_x, align_y, 0, style, layer);
}

Status drawTextLabel(
    const std::string& text,
    const Point& position,
+5 −1
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@

namespace fviz {
class Layer;
namespace text {
struct TextSpan;
}

enum class TextDirection {
  LTR, RTL
@@ -35,7 +38,8 @@ struct TextStyle {
};

Status drawTextLabel(
    const std::string& text,
    const text::TextSpan* text_begin,
    const text::TextSpan* text_end,
    const Point& position,
    HAlign align_x,
    VAlign align_y,
+43 −28
Original line number Diff line number Diff line
@@ -32,46 +32,61 @@ void draw_test(
    VAlign ay) {
  TextStyle ts;
  ts.font = font;
  ts.font_size = from_unit(32);
  ts.font_size = from_unit(28);
  ts.color = Color::fromRGB(0,0,0);
  ts.direction = dir;

  std::vector<text::TextSpan> text;
  {
    text::TextSpan ts;
    ts.text = "test";
    text.push_back(ts);
  }

  {
    text::TextSpan ts;
    ts.text += "∞ⁱ Ω תל אביב , 北京市, القاهرة = testend";
    text.push_back(ts);
  }

  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("Hello תל אביב !", Point(x, y), ax, ay, 0, ts, l);
  drawTextLabel(text.data(), text.data() + text.size(), Point(x, y), ax, ay, 0, ts, l);
}

void draw_test(Layer* layer, const FontInfo& font) {
  draw_test(layer, font, 100 * 2,  100 * 2, TextDirection::LTR, HAlign::LEFT, VAlign::TOP);
  draw_test(layer, font, 400 * 2,  100 * 2, TextDirection::LTR, HAlign::CENTER, VAlign::TOP);
  draw_test(layer, font, 700 * 2,  100 * 2, TextDirection::LTR, HAlign::RIGHT, VAlign::TOP);
  draw_test(layer, font, 100 * 4,  100 * 4, TextDirection::LTR, HAlign::LEFT, VAlign::TOP);
  draw_test(layer, font, 400 * 4,  100 * 4, TextDirection::LTR, HAlign::CENTER, VAlign::TOP);
  draw_test(layer, font, 700 * 4,  100 * 4, TextDirection::LTR, HAlign::RIGHT, VAlign::TOP);

  draw_test(layer, font, 100 * 2,  200 * 2, TextDirection::LTR, HAlign::LEFT, VAlign::CENTER);
  draw_test(layer, font, 400 * 2,  200 * 2, TextDirection::LTR, HAlign::CENTER, VAlign::CENTER);
  draw_test(layer, font, 700 * 2,  200 * 2, TextDirection::LTR, HAlign::RIGHT, VAlign::CENTER);
  draw_test(layer, font, 100 * 4,  200 * 4, TextDirection::LTR, HAlign::LEFT, VAlign::CENTER);
  draw_test(layer, font, 400 * 4,  200 * 4, TextDirection::LTR, HAlign::CENTER, VAlign::CENTER);
  draw_test(layer, font, 700 * 4,  200 * 4, TextDirection::LTR, HAlign::RIGHT, VAlign::CENTER);

  draw_test(layer, font, 100 * 2,  300 * 2, TextDirection::LTR, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(layer, font, 400 * 2,  300 * 2, TextDirection::LTR, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(layer, font, 700 * 2,  300 * 2, TextDirection::LTR, HAlign::RIGHT, VAlign::BOTTOM);
  draw_test(layer, font, 100 * 4,  300 * 4, TextDirection::LTR, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(layer, font, 400 * 4,  300 * 4, TextDirection::LTR, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(layer, font, 700 * 4,  300 * 4, TextDirection::LTR, HAlign::RIGHT, VAlign::BOTTOM);

  draw_test(layer, font, 100 * 2,  400 * 2, TextDirection::RTL, HAlign::LEFT, VAlign::TOP);
  draw_test(layer, font, 400 * 2,  400 * 2, TextDirection::RTL, HAlign::CENTER, VAlign::TOP);
  draw_test(layer, font, 700 * 2,  400 * 2, TextDirection::RTL, HAlign::RIGHT, VAlign::TOP);
  draw_test(layer, font, 100 * 4,  400 * 4, TextDirection::RTL, HAlign::LEFT, VAlign::TOP);
  draw_test(layer, font, 400 * 4,  400 * 4, TextDirection::RTL, HAlign::CENTER, VAlign::TOP);
  draw_test(layer, font, 700 * 4,  400 * 4, TextDirection::RTL, HAlign::RIGHT, VAlign::TOP);

  draw_test(layer, font, 100 * 2,  500 * 2, TextDirection::RTL, HAlign::LEFT, VAlign::CENTER);
  draw_test(layer, font, 400 * 2,  500 * 2, TextDirection::RTL, HAlign::CENTER, VAlign::CENTER);
  draw_test(layer, font, 700 * 2,  500 * 2, TextDirection::RTL, HAlign::RIGHT, VAlign::CENTER);

  draw_test(layer, font, 100 * 2,  600 * 2, TextDirection::RTL, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(layer, font, 400 * 2,  600 * 2, TextDirection::RTL, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(layer, font, 700 * 2,  600 * 2, TextDirection::RTL, HAlign::RIGHT, VAlign::BOTTOM);
  draw_test(layer, font, 100 * 4,  500 * 4, TextDirection::RTL, HAlign::LEFT, VAlign::CENTER);
  draw_test(layer, font, 400 * 4,  500 * 4, TextDirection::RTL, HAlign::CENTER, VAlign::CENTER);
  draw_test(layer, font, 700 * 4,  500 * 4, TextDirection::RTL, HAlign::RIGHT, VAlign::CENTER);

  draw_test(layer, font, 100 * 4,  600 * 4, TextDirection::RTL, HAlign::LEFT, VAlign::BOTTOM);
  draw_test(layer, font, 400 * 4,  600 * 4, TextDirection::RTL, HAlign::CENTER, VAlign::BOTTOM);
  draw_test(layer, font, 700 * 4,  600 * 4, TextDirection::RTL, HAlign::RIGHT, VAlign::BOTTOM);
}

int main(int argc, char** argv) {
  FontRef font_ref0;
  EXPECT_OK(font_load("/usr/share/fonts/dejavu/DejaVuSans.ttf", &font_ref0));

  FontRef font_ref1;
  EXPECT_OK(font_load("/usr/share/fonts/msttcore/arial.ttf", &font_ref1));

@@ -79,14 +94,14 @@ int main(int argc, char** argv) {
  EXPECT_OK(font_load("/usr/share/fonts/msttcore/comic.ttf", &font_ref2));

  FontInfo font;
  font.fonts = {font_ref2, font_ref1};
  font.fonts = {font_ref2, font_ref1, font_ref0};

  {
    LayerRef layer;
    auto rc = layer_bind_svg(
        1600,
        1400,
        96,
        3200,
        2800,
        240,
        from_unit(12),
        Color::fromRGB(1.0, 1.0, 1.0),
        [&] (auto svg) {
@@ -102,9 +117,9 @@ int main(int argc, char** argv) {
  {
    LayerRef layer;
    auto rc = layer_bind_png(
        1600,
        1400,
        96,
        3200,
        2800,
        240,
        from_unit(12),
        Color::fromRGB(1.0, 1.0, 1.0),
        [&] (auto svg) {