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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
#include "stdafx.h"
#include "Text.h"
#include "RenderMgr.h"
#include "TextMgr.h"
#include <limits>
namespace gui {
/**
* Text line.
*/
TextLine::TextLine(Float baseline, Str *text) :
baseline(baseline), text(text) {}
void TextLine::toS(StrBuf *to) const {
*to << S("{baseline=") << baseline << S(" text=")
<< text << S("}");
}
/**
* Text effect.
*/
TextEffect::TextEffect() : type(tNone), from(0), to(0), d0(0), d1(0), d2(0), d3(0), ptr(null) {}
TextEffect::TextEffect(Type type, Str::Iter begin, Str::Iter end, Float d0, Float d1, Float d2, Float d3, void *ptr)
: type(type), from(begin.offset()), to(end.offset()),
d0(d0), d1(d1), d2(d2), d3(d3), ptr(ptr) {}
TextEffect TextEffect::color(Str::Iter begin, Str::Iter end, Color color) {
return TextEffect(tColor, begin, end, color.r, color.g, color.b, color.a, null);
}
TextEffect TextEffect::underline(Str::Iter begin, Str::Iter end, Bool enable) {
return TextEffect(tUnderline, begin, end, enable ? 1.0f : 0.0f, 0, 0, 0, null);
}
TextEffect TextEffect::strikeOut(Str::Iter begin, Str::Iter end, Bool enable) {
return TextEffect(tStrikeOut, begin, end, enable ? 1.0f : 0.0f, 0, 0, 0, null);
}
TextEffect TextEffect::italic(Str::Iter begin, Str::Iter end, Bool enable) {
return TextEffect(tItalic, begin, end, enable ? 1.0f : 0.0f, 0, 0, 0, null);
}
TextEffect TextEffect::weight(Str::Iter begin, Str::Iter end, Int weight) {
return TextEffect(tWeight, begin, end, Float(weight), 0, 0, 0, null);
}
TextEffect TextEffect::family(Str::Iter begin, Str::Iter end, Str *family) {
return TextEffect(tFamily, begin, end, 0, 0, 0, 0, family);
}
TextEffect TextEffect::scaleSize(Str::Iter begin, Str::Iter end, Float size) {
return TextEffect(tScaleSize, begin, end, size, 0, 0, 0, null);
}
void TextEffect::toS(StrBuf *to) const {
*to << from << S("-") << to << S(" ");
switch (type) {
case TextEffect::tNone:
*to << S("<no effect>");
break;
case TextEffect::tColor:
*to << S("color: ") << color();
break;
case TextEffect::tUnderline:
*to << S("underline: ") << boolean();
break;
case TextEffect::tStrikeOut:
*to << S("strike out: ") << boolean();
break;
case TextEffect::tItalic:
*to << S("italic: ") << boolean();
break;
case TextEffect::tWeight:
*to << S("weight: ") << integer();
break;
case TextEffect::tFamily:
*to << S("family: ") << family();
break;
case TextEffect::tScaleSize:
*to << S("scale size: ") << d0;
break;
default:
*to << S("<unknown effect>");
break;
}
}
/**
* The text object itself.
*/
// Small extra space added around the border to prevent unneeded wrapping.
static const float borderExtra = 0.001f;
Text::Text(Str *text, Font *font)
: myText(text), myFont(font), myEffects(null), appliedEffects(0) {
float maxFloat = std::numeric_limits<float>::max();
myBorder = Size(maxFloat, maxFloat);
init();
}
Text::Text(Str *text, Font *font, Size size)
: myText(text), myFont(font), myBorder(size), myEffects(null), appliedEffects(0) {
init();
}
void Text::init() {
mgr = renderMgr(engine())->text();
TextMgr::Resource r = mgr->createLayout(this);
layout = r.data;
cleanup = r.cleanup;
}
void Text::recreate() {
// Apply any outstanding effects now.
if (myEffects && appliedEffects < myEffects->count()) {
while (appliedEffects < myEffects->count()) {
TextEffect effect = myEffects->at(appliedEffects);
myEffects->at(appliedEffects) = TextEffect();
appliedEffects++;
insertEffect(effect);
}
cleanupEffects();
}
if (cleanup)
(*cleanup)(layout);
TextMgr::Resource r = mgr->createLayout(this);
layout = r.data;
cleanup = r.cleanup;
}
Text::~Text() {
if (layout && cleanup) {
(*cleanup)(layout);
}
}
Size Text::size() {
return mgr->size(layout) + Size(borderExtra, borderExtra);
}
void Text::layoutBorder(Size border) {
if (!mgr->updateBorder(layout, border)) {
recreate();
}
}
void Text::effect(TextEffect effect) {
if (!myEffects)
myEffects = new (this) Array<TextEffect>();
// Try to apply it now.
TextMgr::EffectResult r = mgr->addEffect(layout, effect, myFont, myText, null);
if (r == TextMgr::eApplied) {
// Applied now, update!
insertEffect(effect);
} else {
// Apply it later.
*myEffects << effect;
if (r == TextMgr::eReCreate) {
// We could be a bit more eager in this case. That would be beneficial if (for some
// reason) the effect had an impact on size or layout computations.
}
}
}
void Text::insertEffect(TextEffect effect) {
if (effect.empty())
return;
for (Nat i = 0; i < appliedEffects; i++) {
TextEffect &e = myEffects->at(i);
// Only examine effects of the same type.
if (e.type != effect.type) {
continue;
}
// Exactly the same range?
if (e.from == effect.from && e.to == effect.to) {
e = effect;
// Done!
return;
}
// Is the existing one smaller than this one?
if (e.from >= effect.from && e.to <= effect.to) {
removeEffectI(i); i--;
continue;
}
// The other one before this one?
if (e.to >= effect.from && e.to < effect.from) {
// Merge them?
if (e.sameData(effect)) {
effect.from = e.from;
removeEffectI(i); i--;
} else {
// Shorten the other one.
e.to = effect.from;
}
continue;
}
// The other one after this one?
if (e.from > effect.from && e.from <= effect.to) {
// Merge them?
if (e.sameData(effect)) {
effect.to = e.to;
removeEffectI(i); i--;
} else {
// Shorten the other one.
e.from = effect.to;
}
continue;
}
// The existing one is larger than this one.
if (e.from < effect.from && e.to > effect.to) {
if (e.sameData(effect)) {
// Nothing to do, it is the same data.
return;
} else {
// Split the other one.
TextEffect last = e;
e.to = effect.from;
last.from = effect.to;
insertEffectI(last);
}
}
}
insertEffectI(effect);
}
void Text::removeEffectI(Nat id) {
// Move effects "forward" until we have a "hole" right before "appliedEffects".
for (Nat i = id; i + 1 < appliedEffects; i++) {
myEffects->at(i) = myEffects->at(i + 1);
}
// Create a proper "hole".
myEffects->at(appliedEffects - 1) = TextEffect();
}
void Text::insertEffectI(TextEffect effect) {
// Go from the end and backwards.
Nat empty = appliedEffects;
for (Nat i = appliedEffects; i > 0; i--) {
if (myEffects->at(i - 1).any())
break;
empty = i - 1;
}
// Full?
if (empty >= appliedEffects) {
// No room. Insert it, moving the others one step ahead.
myEffects->insert(appliedEffects, effect);
appliedEffects++;
} else {
// Replace 'empty'?
myEffects->at(empty) = effect;
}
}
void Text::cleanupEffects() {
while (appliedEffects < myEffects->count()) {
if (myEffects->last().type == TextEffect::tNone)
myEffects->pop();
else
break;
}
}
Array<TextLine *> *Text::lineInfo() {
return mgr->lineInfo(layout, this);
}
Array<Rect> *Text::boundsOf(Str::Iter begin, Str::Iter end) {
return mgr->boundsOf(layout, this, begin, end);
}
Array<TextEffect> *Text::effects() const {
if (!myEffects) {
return new (this) Array<TextEffect>();
} else {
return new (this) Array<TextEffect>(*myEffects);
}
}
void *Text::backendLayout(Graphics *g) {
if (myEffects && appliedEffects < myEffects->count()) {
while (appliedEffects < myEffects->count()) {
TextMgr::EffectResult r = mgr->addEffect(layout, myEffects->at(appliedEffects), myFont, myText, g);
if (r == TextMgr::eApplied) {
// Clear it out and merge it back in.
TextEffect effect = myEffects->at(appliedEffects);
myEffects->at(appliedEffects) = TextEffect();
appliedEffects++;
insertEffect(effect);
} else if (r == TextMgr::eReCreate) {
// We need to recreate the entire thing.
recreate();
break;
} else if (r == TextMgr::eWait) {
// Wrong context apparently. Wait for other draws.
break;
}
}
cleanupEffects();
}
// Now, we are ready!
return layout;
}
}
|