1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
/* WrappedText.h
Copyright (c) 2014-2020 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "Alignment.h"
#include "../Point.h"
#include "Truncate.h"
#include <string>
#include <vector>
class Color;
class Font;
// Class for calculating word positions in wrapped text. You can specify various
// parameters of the formatting, including text alignment.
class WrappedText {
public:
WrappedText() = default;
explicit WrappedText(const Font &font);
// Set the alignment mode.
void SetAlignment(Alignment align);
// Set the truncate mode.
// Apply the truncation to a word only if a line has a single word.
void SetTruncate(Truncate trunc);
// Set the wrap width. This does not include any margins.
int WrapWidth() const;
void SetWrapWidth(int width);
// Set the font to use. This will also set sensible defaults for the tab
// width, line height, and paragraph break. You must specify the wrap width
// and the alignment separately.
void SetFont(const Font &font);
// Set the width in pixels of a single '\t' character.
int TabWidth() const;
void SetTabWidth(int width);
// Set the height in pixels of one line of text within a paragraph.
int LineHeight() const;
void SetLineHeight(int height);
// Set the extra spacing in pixels to be added in between paragraphs.
int ParagraphBreak() const;
void SetParagraphBreak(int height);
// Wrap the given text. Use Draw() to draw it.
void Wrap(const std::string &str);
void Wrap(const char *str);
/// Get the height of the wrapped text.
/// With trailingBreak, include a paragraph break after the text.
int Height(bool trailingBreak = true) const;
// Return the width of the longest line of the wrapped text.
int LongestLineWidth() const;
// Draw the text.
void Draw(const Point &topLeft, const Color &color) const;
private:
void SetText(const char *it, size_t length);
void Wrap();
void AdjustLine(size_t &lineBegin, int &lineWidth, bool isEnd);
int Space(char c) const;
private:
// The returned text is a series of words and (x, y) positions:
class Word {
public:
Word() = default;
size_t Index() const;
Point Pos() const;
private:
size_t index = 0;
int x = 0;
int y = 0;
friend class WrappedText;
};
private:
const Font *font = nullptr;
int space = 0;
int wrapWidth = 1000;
int tabWidth = 0;
int lineHeight = 0;
int paragraphBreak = 0;
Alignment alignment = Alignment::JUSTIFIED;
Truncate truncate = Truncate::NONE;
std::string text;
std::vector<Word> words;
int height = 0;
int longestLineWidth = 0;
};
|