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

improved text layout api (naming)

parent 8eef3e77
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ struct BrushFillOp {

struct TextSpanOp {
  std::string text;
  std::vector<text::GlyphSpan> spans;
  std::vector<text::GlyphPlacementGroup> glyphs;
  Point origin;
  double rotate;
  Point rotate_pivot;
+2 −2
Original line number Diff line number Diff line
@@ -180,8 +180,8 @@ Status svg_text_span_embed(
    SVGDataRef svg) {
  const auto& style = op.style;

  for (const auto& s : op.spans) {
    for (const auto& g : s.glyphs) {
  for (const auto& gg : op.glyphs) {
    for (const auto& g : gg.glyphs) {
      Path gp;

      auto rc = font_get_glyph_path(
+4 −4
Original line number Diff line number Diff line
@@ -118,8 +118,8 @@ Status Rasterizer::strokePath(const layer_ops::BrushStrokeOp& op) {
}

Status Rasterizer::drawText(const layer_ops::TextSpanOp& op) {
  for (const auto& span : op.spans) {
    auto ft_font = static_cast<FT_Face>(font_get_freetype(span.font));
  for (const auto& gg : op.glyphs) {
    auto ft_font = static_cast<FT_Face>(font_get_freetype(gg.font));

    auto font_size_ft = op.style.font_size * (72.0 / dpi) * 64;
    if (FT_Set_Char_Size(ft_font, 0, font_size_ft, dpi, dpi)) {
@@ -138,10 +138,10 @@ Status Rasterizer::drawText(const layer_ops::TextSpanOp& op) {
    cairo_set_font_face(cr_ctx, cairo_face);
    cairo_set_font_size(cr_ctx, op.style.font_size);

    auto glyph_count = span.glyphs.size();
    auto glyph_count = gg.glyphs.size();
    auto cairo_glyphs = cairo_glyph_allocate(glyph_count);
    for (int i = 0; i < glyph_count; ++i) {
      const auto& g = span.glyphs[i];
      const auto& g = gg.glyphs[i];
      //FT_Load_Glyph(ft_font, g.codepoint, FT_LOAD_DEFAULT);

      cairo_glyphs[i].index = g.codepoint;
+3 −3
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ Status drawTextLabel(
  }

  Rectangle bbox;
  std::vector<text::GlyphSpan> glyphs;
  auto rc = text::text_layout_hline(
  std::vector<text::GlyphPlacementGroup> glyphs;
  auto rc = text::text_layout_line(
      text_begin,
      text_end,
      style.direction,
@@ -66,10 +66,10 @@ Status drawTextLabel(

  layer_ops::TextSpanOp op;
  op.text = text;
  op.glyphs = std::move(glyphs);
  op.rotate = rotate;
  op.rotate_pivot = position;
  op.style = style;
  op.spans = std::move(glyphs);
  op.origin = offset;

  return layer->apply(op);
+55 −37
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ namespace fviz {
namespace text {

/**
 * A line of text that has been prepared for final layout.
 * A line of text that has been prepared for final layout (placement).
 *
 * The `text_runs` member contains the line's text runs as a UTF-8 strings.
 * Note that the runs are given in logical order and the characters within
@@ -51,7 +51,7 @@ struct TextLine {
  std::vector<size_t> visual_order;
};

Status text_layout_hrun(
Status text_place_hrun(
    const std::string& text_logical,
    const TextDirection text_direction,
    const std::string& text_language,
@@ -94,12 +94,12 @@ Status text_layout_hrun(
  return OK;
}

Status text_layout_hline(
Status text_place_hline(
    const TextLine& text_line,
    const FontInfo& font_info,
    double font_size,
    double dpi,
    std::vector<GlyphSpan>* glyph_spans,
    std::vector<GlyphPlacementGroup>* glyphs,
    Rectangle* bbox) {
  double line_top = 0.0;
  double line_bottom = 0.0;
@@ -112,7 +112,7 @@ Status text_layout_hline(

    double span_length = 0.0;
    std::vector<GlyphPlacement> span_glyphs;
    auto rc = text_layout_hrun(
    auto rc = text_place_hrun(
        text_line.runs[i],
        text_direction_run,
        text_line.span_map[i]->language,
@@ -140,13 +140,13 @@ Status text_layout_hline(
          break;
      }

      GlyphSpan gs;
      gs.font = gi.font;
      gs.glyphs.emplace_back(gi);

      // TODO merge glyph spans with the same font
      if (glyph_spans) {
        glyph_spans->emplace_back(gs);
      GlyphPlacementGroup gg;
      gg.font = gi.font;
      gg.glyphs.emplace_back(gi);

      if (glyphs) {
        glyphs->emplace_back(gg);
      }
    }

@@ -172,28 +172,6 @@ Status text_layout_hline(
  return OK;
}



Status text_measure_span(
    const std::string& text,
    const FontInfo& font_info,
    double font_size,
    double dpi,
    Rectangle* bbox) {
  TextSpan span;
  span.text = text;

  return text_layout_hline(
      &span,
      &span + 1,
      TextDirection::LTR,
      font_info,
      font_size,
      dpi,
      nullptr,
      bbox);
}

/**
 * Determine the visual order of text runs in a line according to the unicode
 * bidi algorithm
@@ -259,14 +237,14 @@ std::vector<size_t> text_reorder_line(const TextLine& line) {
  return visual_order;
}

Status text_layout_hline(
Status text_layout_line(
    const TextSpan* text_begin,
    const TextSpan* text_end,
    const TextDirection text_direction_base,
    const FontInfo& font_info,
    double font_size,
    double dpi,
    std::vector<GlyphSpan>* glyph_spans,
    std::vector<GlyphPlacementGroup>* glyphs,
    Rectangle* bbox) {
  TextLine text_line;

@@ -281,15 +259,55 @@ Status text_layout_hline(
  text_line.base_direction = text_direction_base;
  text_line.visual_order = text_reorder_line(text_line);

  return text_layout_hline(
  return text_place_hline(
      text_line,
      font_info,
      font_size,
      dpi,
      glyph_spans,
      glyphs,
      bbox);
}

Status text_layout_line(
    const std::string& text,
    const TextDirection text_direction_base,
    const FontInfo& font_info,
    double font_size,
    double dpi,
    std::vector<GlyphPlacementGroup>* glyphs,
    Rectangle* bbox) {
  TextSpan span;
  span.text = text;

  return text_layout_line(
      &span,
      &span + 1,
      text_direction_base,
      font_info,
      font_size,
      dpi,
      glyphs,
      bbox);
}

Status text_measure_line(
    const std::string& text,
    TextDirection text_direction_base,
    const FontInfo& font,
    double font_size,
    double dpi,
    Rectangle* bbox) {
  return text_layout_line(
      text,
      text_direction_base,
      font,
      font_size,
      dpi,
      nullptr,
      bbox);
}


} // namespace text
} // namespace fviz
Loading