ODF_TEST/include/app.hpp

127 lines
3.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef ODF_APP_HPP
#define ODF_APP_HPP
#include <string>
#include <vector>
#include <memory>
#include <ostream>
namespace odf {
// ====================================================================
// 前置声明
// ====================================================================
class TextRun;
class Paragraph;
class Document;
// ====================================================================
// TextRun — 文本片段
// ====================================================================
/**
* @brief 表示文档中的一个文本片段,可携带格式属性。
*/
class TextRun {
public:
/**
* @brief 构造一个 TextRun 对象。
* @param text 文本内容
* @param bold 是否加粗(默认 false
* @param italic 是否斜体(默认 false
*/
explicit TextRun(std::string text, bool bold = false, bool italic = false);
/// @brief 返回文本内容(只读)。
const std::string& text() const noexcept { return text_; }
/// @brief 是否加粗。
bool is_bold() const noexcept { return bold_; }
/// @brief 是否斜体。
bool is_italic() const noexcept { return italic_; }
/**
* @brief 将本 TextRun 输出到输出流(纯文本格式)。
* @param os 输出流
*/
void write(std::ostream& os) const;
private:
std::string text_;
bool bold_;
bool italic_;
};
// ====================================================================
// Paragraph — 段落
// ====================================================================
/**
* @brief 表示文档中的一个段落,由若干 TextRun 组成。
*/
class Paragraph {
public:
/// @brief 默认构造空段落。
Paragraph() = default;
/**
* @brief 向段落末尾追加一个文本片段。
* @param run 待追加的 TextRun支持移动语义
*/
void add_run(TextRun run);
/// @brief 返回段落中所有文本片段(只读)。
const std::vector<TextRun>& runs() const noexcept { return runs_; }
/**
* @brief 将本段落输出到输出流。
* @param os 输出流
*/
void write(std::ostream& os) const;
private:
std::vector<TextRun> runs_;
};
// ====================================================================
// Document — 文档模型
// ====================================================================
/**
* @brief 文档模型,包含一组段落。
*/
class Document {
public:
/// @brief 默认构造空文档。
Document() = default;
/**
* @brief 向文档末尾追加一个段落。
* @param para 待追加的 Paragraph支持移动语义
*/
void add_paragraph(Paragraph para);
/// @brief 返回文档中的所有段落(只读)。
const std::vector<Paragraph>& paragraphs() const noexcept { return paragraphs_; }
/**
* @brief 将整个文档输出为纯文本到输出流。
* @param os 输出流
*/
void write(std::ostream& os) const;
/**
* @brief 返回文档的纯文本字符串表示。
* @return 纯文本字符串
*/
std::string to_string() const;
private:
std::vector<Paragraph> paragraphs_;
};
} // namespace odf
#endif // ODF_APP_HPP