CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Text/WrapText.h
Views: 1401
#pragma once12#include <string>3#include <string_view>45class WordWrapper {6public:7WordWrapper(std::string_view str, float maxW, int flags)8: str_(str), maxW_(maxW), flags_(flags) {9}10virtual ~WordWrapper() {}1112// TODO: This should return a vector of std::string_view for the lines, instead of building up a new string.13std::string Wrapped();1415protected:16virtual float MeasureWidth(std::string_view str) = 0;1718void Wrap();19bool WrapBeforeWord();20void AppendWord(int endIndex, int lastChar, bool addNewline);21void AddEllipsis();2223static bool IsCJK(uint32_t c);24static bool IsPunctuation(uint32_t c);25static bool IsSpace(uint32_t c);26static bool IsShy(uint32_t c);27static bool IsSpaceOrShy(uint32_t c) {28return IsSpace(c) || IsShy(c);29}3031const std::string_view str_;32const float maxW_;33const int flags_;34std::string out_;3536// Index of last output / start of current word.37int lastIndex_ = 0;38// Ideal place to put an ellipsis if we run out of space.39int lastEllipsisIndex_ = -1;40// Index of last line start.41size_t lastLineStart_ = 0;42// Last character written to out_.43int lastChar_ = 0;44// Position the current word starts at.45float x_ = 0.0f;46// Most recent width of word since last index.47float wordWidth_ = 0.0f;48// Width of "..." when flag is set, zero otherwise.49float ellipsisWidth_ = 0.0f;50// Force the next word to cut partially and wrap.51bool forceEarlyWrap_ = false;52// Skip all characters until the next newline.53bool scanForNewline_ = false;54// Skip the next word, replaced with ellipsis.55bool skipNextWord_ = false;56};575859