Skip to content
Repos

DevPlaceApp

C++ 1.5 KB60 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-TEXT-01. Rich text: spans with their own font/color, word wrapping, line limit, tail
// truncation, alignment. All values in points.
#include <string>
#include <vector>

#include "ui/Font.hpp"
#include "ui/Geometry.hpp"

namespace dp::ui {

struct TextStyle {
  FontSpec font = FontSpec::body();
  Color color = Color::rgb(0x000000);
  bool underline = false;
  bool strikethrough = false;
  std::optional<Color> background;  // inline code chip
  std::string link;                 // non-empty: tappable run
  bool operator==(const TextStyle&) const = default;
};

struct TextSpan {
  std::string text;
  TextStyle style;
};

enum class TextAlign { Leading, Center, Trailing };

struct TextRun {
  std::string text;
  TextStyle style;
  float x = 0;
  float width = 0;
};

struct TextLine {
  std::vector<TextRun> runs;
  float y = 0;
  float width = 0;
  float height = 0;
  float baseline = 0;
};

struct TextBlock {
  std::vector<TextLine> lines;
  Size size;
  bool truncated = false;
};

class TextLayout {
 public:
  // maxWidth: kInf for a single unwrapped line. lineLimit 0 = unlimited.
  static TextBlock layout(const std::vector<TextSpan>& spans, float maxWidth, int lineLimit, TextAlign align,
                          float lineSpacing = 0);
  // Widest unbreakable token (the minimum width a wrapping text can take).
  static float minWidth(const std::vector<TextSpan>& spans);
  static std::vector<std::string> codepoints(const std::string& utf8);
};

}  // namespace dp::ui
35.1ms