2021-08-09 05:25:20 +08:00
|
|
|
#include <memory> // for make_shared
|
|
|
|
#include <string> // for string
|
|
|
|
#include <vector> // for vector
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
#include "ftxui/dom/deprecated.hpp" // for text, vtext
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, text, vtext
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
2021-08-09 05:25:20 +08:00
|
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
|
|
|
#include "ftxui/screen/string.hpp" // for string_width, Utf8ToGlyphs, to_string
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
using ftxui::Screen;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
class Text : public Node {
|
|
|
|
public:
|
2021-08-09 05:25:20 +08:00
|
|
|
Text(std::string text) : text_(text) {}
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
2021-08-09 05:25:20 +08:00
|
|
|
requirement_.min_x = string_width(text_);
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_y = 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2019-01-20 05:06:05 +08:00
|
|
|
int x = box_.x_min;
|
|
|
|
int y = box_.y_min;
|
|
|
|
if (y > box_.y_max)
|
2018-09-18 14:48:40 +08:00
|
|
|
return;
|
2021-08-09 05:25:20 +08:00
|
|
|
for (const auto& cell : Utf8ToGlyphs(text_)) {
|
|
|
|
if (x > box_.x_max)
|
|
|
|
return;
|
|
|
|
screen.PixelAt(x, y).character = cell;
|
|
|
|
++x;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string text_;
|
2018-09-18 14:48:40 +08:00
|
|
|
};
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
class VText : public Node {
|
|
|
|
public:
|
2021-08-09 05:25:20 +08:00
|
|
|
VText(std::string text)
|
|
|
|
: text_(text), width_{std::min(string_width(text_), 1)} {}
|
2020-06-02 05:40:32 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = width_;
|
2021-08-09 05:25:20 +08:00
|
|
|
requirement_.min_y = string_width(text_);
|
2020-06-02 05:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
|
|
|
int x = box_.x_min;
|
|
|
|
int y = box_.y_min;
|
|
|
|
if (x + width_ - 1 > box_.x_max)
|
|
|
|
return;
|
2021-08-09 05:25:20 +08:00
|
|
|
for (const auto& it : Utf8ToGlyphs(text_)) {
|
2020-06-02 05:40:32 +08:00
|
|
|
if (y > box_.y_max)
|
|
|
|
return;
|
2021-08-09 05:25:20 +08:00
|
|
|
screen.PixelAt(x, y).character = it;
|
2020-06-02 05:40:32 +08:00
|
|
|
y += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string text_;
|
2020-06-02 05:40:32 +08:00
|
|
|
int width_ = 1;
|
|
|
|
};
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
/// @brief Display a piece of UTF8 encoded unicode text.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = text("Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Hello world!
|
|
|
|
/// ```
|
|
|
|
Element text(std::string text) {
|
|
|
|
return std::make_shared<Text>(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Display a piece of UTF16 encoded unicode text.
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = text(L"Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Hello world!
|
|
|
|
/// ```
|
2020-05-21 02:36:47 +08:00
|
|
|
Element text(std::wstring text) {
|
2021-08-09 05:25:20 +08:00
|
|
|
return std::make_shared<Text>(to_string(text));
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
/// @brief Display a piece of unicode text vertically.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = vtext("Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// H
|
|
|
|
/// e
|
|
|
|
/// l
|
|
|
|
/// l
|
|
|
|
/// o
|
|
|
|
///
|
|
|
|
/// w
|
|
|
|
/// o
|
|
|
|
/// r
|
|
|
|
/// l
|
|
|
|
/// d
|
|
|
|
/// !
|
|
|
|
/// ```
|
|
|
|
Element vtext(std::string text) {
|
|
|
|
return std::make_shared<VText>(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Display a piece of UTF16 encoded unicode text vertically.
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see ftxui::to_wstring
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = vtext(L"Hello world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// H
|
|
|
|
/// e
|
|
|
|
/// l
|
|
|
|
/// l
|
|
|
|
/// o
|
|
|
|
///
|
|
|
|
/// w
|
|
|
|
/// o
|
|
|
|
/// r
|
|
|
|
/// l
|
|
|
|
/// d
|
|
|
|
/// !
|
|
|
|
/// ```
|
2020-06-02 05:40:32 +08:00
|
|
|
Element vtext(std::wstring text) {
|
2021-08-09 05:25:20 +08:00
|
|
|
return std::make_shared<VText>(to_string(text));
|
2020-06-02 05:40:32 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|