Commit 3c76bfaa authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved, baseline-independent text alignment implementation

parent 65879199
Loading
Loading
Loading
Loading
+1 −17
Original line number Diff line number Diff line
@@ -48,20 +48,6 @@ Status svg_text_span(
    SVGDataRef svg) {
  const auto& style = op.style;

  std::string anchor;
  switch (style.halign) {
    case TextHAlign::CENTER:
      anchor = "middle";
      break;
    case TextHAlign::LEFT:
      anchor = "start";
      break;
    case TextHAlign::RIGHT:
      anchor = "end";
      break;
  }

  std::string baseline = "middle";

  svg->buffer
    << "  "
@@ -70,9 +56,7 @@ Status svg_text_span(
    << "y='" << op.y << "' "
    << "fill='" << style.colour.to_hex_str() << "' "
    << "font-size='" << to_px(measures, style.font_size).value << "' "
    << "font-family='" << "\"Helvetica Neue\", Helvetica, Arial, sans-serif" << "' "
    << "text-anchor='" << anchor << "' "
    << "dominant-baseline='" << baseline << "' "
    << "font-family='" << "\"Roboto Medium\", sans-serif" << "' "
    << ">"
    << op.text // FIXME escape
    << "</text>";
+3 −5
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ Status Rasterizer::strokePath(const layer_ops::BrushStrokeOp& op) {
}

Status Rasterizer::drawText(const layer_ops::TextSpanOp& op) {
  std::vector<GlyphPlacement> glyphs;
  std::vector<text::GlyphPlacement> glyphs;
  auto rc = text::layoutText(
      op.text,
      op.x,
@@ -152,10 +152,8 @@ Status Rasterizer::drawText(const layer_ops::TextSpanOp& op) {
      op.style.font_size,
      measures.dpi,
      op.style.direction,
      op.style.halign,
      op.style.valign,
      text_shaper.get(),
      [&glyphs] (const GlyphPlacement& g) { glyphs.emplace_back(g); });
      [&glyphs] (const text::GlyphPlacement& g) { glyphs.emplace_back(g); });

  if (rc != OK) {
    return rc;
@@ -168,7 +166,7 @@ Status Rasterizer::drawText(const layer_ops::TextSpanOp& op) {
}

Status Rasterizer::drawTextGlyphs(
    const GlyphPlacement* glyphs,
    const text::GlyphPlacement* glyphs,
    size_t glyph_count,
    const TextStyle& style) {
  if (!ft_ready) {
+2 −2
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@
#include "brush.h"
#include "layout.h"
#include "layer.h"
#include "text_shaper.h"
#include "text_layout.h"

namespace plotfx {
class Image;
@@ -68,7 +68,7 @@ public:

  Status drawText(const layer_ops::TextSpanOp& op);
  Status drawTextGlyphs(
      const GlyphPlacement* glyphs,
      const text::GlyphPlacement* glyphs,
      size_t glyph_count,
      const TextStyle& style);

+21 −6
Original line number Diff line number Diff line
@@ -36,25 +36,40 @@ namespace plotfx {

TextStyle::TextStyle() :
    direction(TextDirection::LTR),
    halign(TextHAlign::LEFT),
    valign(TextVAlign::BASELINE),
    font_size(from_pt(11)) {}

Status drawText(
Status drawTextLabel(
    const std::string& text,
    double x,
    double y,
    HAlign align_x,
    VAlign align_y,
    const TextStyle& style,
    Layer* layer) {
  Rectangle bbox;
  auto rc = text::text_measure_span(
      text,
      style.font,
      style.font_size,
      layer->measures.dpi,
      layer->text_shaper.get(),
      &bbox);

  double ox, oy;
  layout_align(bbox, x, y, align_x, align_y, &ox, &oy);

  if (rc != OK) {
    return rc;
  }

  layer_ops::TextSpanOp op;
  op.text = text;
  op.x = x;
  op.y = y;
  op.x = ox;
  op.y = oy;
  op.style = style;

  return layer->apply(op);
}


} // namespace plotfx
+4 −32
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@
#include "path.h"
#include "colour.h"
#include "measure.h"
#include "layout.h"

namespace plotfx {
class Layer;
@@ -40,14 +41,6 @@ enum class TextDirection {
  LTR, RTL
};

enum class TextHAlign {
  LEFT, CENTER, RIGHT
};

enum class TextVAlign {
  BASELINE, MIDDLE, TOP, BOTTOM
};

struct FontInfo {
  std::string font_file;
};
@@ -55,40 +48,19 @@ struct FontInfo {
struct TextStyle {
  TextStyle();
  TextDirection direction;
  TextHAlign halign;
  TextVAlign valign;
  FontInfo font;
  Measure font_size;
  Colour colour;
};

struct GlyphInfo {
  uint32_t codepoint;
  double advance_y;
  double advance_x;
  double metrics_ascender;
  double metrics_descender;
};

struct GlyphPlacement {
  uint32_t codepoint;
  double x;
  double y;
};

Status drawText(
Status drawTextLabel(
    const std::string& text,
    double x,
    double y,
    HAlign align_x,
    VAlign align_y,
    const TextStyle& text_style,
    Layer* layer);

Status drawTextGlyphs(
    const GlyphPlacement* glyphs,
    size_t glyph_count,
    const TextStyle& style,
    Layer* layer);


} // namespace plotfx
Loading