| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef _TVG_TEXT_H |
| 24 |
#define _TVG_TEXT_H |
| 25 |
|
| 26 |
#include <cstring> |
| 27 |
#include "tvgMath.h" |
| 28 |
#include "tvgShape.h" |
| 29 |
#include "tvgFill.h" |
| 30 |
#include "tvgLoader.h" |
| 31 |
|
| 32 |
struct Text::Impl |
| 33 |
{ |
| 34 |
FontLoader* loader = nullptr; |
| 35 |
Text* paint; |
| 36 |
Shape* shape; |
| 37 |
FontMetrics metrics; |
| 38 |
char* utf8 = nullptr; |
| 39 |
float fontSize; |
| 40 |
bool italic = false; |
| 41 |
bool changed = false; |
| 42 |
|
| 43 |
Impl(Text* p) : paint(p), shape(Shape::gen().release()) {} |
| 44 |
|
| 45 |
~Impl() |
| 46 |
{ |
| 47 |
free(utf8); |
| 48 |
LoaderMgr::retrieve(loader); |
| 49 |
delete(shape); |
| 50 |
} |
| 51 |
|
| 52 |
Result text(const char* utf8) |
| 53 |
{ |
| 54 |
free(this->utf8); |
| 55 |
if (utf8) this->utf8 = strdup(utf8); |
| 56 |
else this->utf8 = nullptr; |
| 57 |
changed = true; |
| 58 |
|
| 59 |
return Result::Success; |
| 60 |
} |
| 61 |
|
| 62 |
Result font(const char* name, float size, const char* style) |
| 63 |
{ |
| 64 |
auto loader = LoaderMgr::loader(name); |
| 65 |
if (!loader) return Result::InsufficientCondition; |
| 66 |
|
| 67 |
if (style && strstr(style, "italic")) italic = true; |
| 68 |
else italic = false; |
| 69 |
|
| 70 |
fontSize = size; |
| 71 |
|
| 72 |
|
| 73 |
if (this->loader == loader) { |
| 74 |
this->loader->sharing--; |
| 75 |
return Result::Success; |
| 76 |
} else if (this->loader) { |
| 77 |
LoaderMgr::retrieve(this->loader); |
| 78 |
} |
| 79 |
this->loader = static_cast<FontLoader*>(loader); |
| 80 |
|
| 81 |
changed = true; |
| 82 |
return Result::Success; |
| 83 |
} |
| 84 |
|
| 85 |
RenderRegion bounds(RenderMethod* renderer) |
| 86 |
{ |
| 87 |
return P(shape)->bounds(renderer); |
| 88 |
} |
| 89 |
|
| 90 |
bool render(RenderMethod* renderer) |
| 91 |
{ |
| 92 |
if (!loader) return true; |
| 93 |
renderer->blend(PP(paint)->blendMethod); |
| 94 |
return PP(shape)->render(renderer); |
| 95 |
} |
| 96 |
|
| 97 |
float load() |
| 98 |
{ |
| 99 |
if (!loader) return 0.0f; |
| 100 |
|
| 101 |
|
| 102 |
if (changed) { |
| 103 |
loader->read(shape, utf8, metrics); |
| 104 |
changed = false; |
| 105 |
} |
| 106 |
return loader->transform(shape, metrics, fontSize, italic); |
| 107 |
} |
| 108 |
|
| 109 |
RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper) |
| 110 |
{ |
| 111 |
auto scale = 1.0f / load(); |
| 112 |
if (tvg::zero(scale)) return nullptr; |
| 113 |
|
| 114 |
|
| 115 |
auto fill = P(shape)->rs.fill; |
| 116 |
if (fill && P(shape)->rFlag & RenderUpdateFlag::Gradient) { |
| 117 |
if (fill->type() == Type::LinearGradient) { |
| 118 |
P(static_cast<LinearGradient*>(fill))->x1 *= scale; |
| 119 |
P(static_cast<LinearGradient*>(fill))->y1 *= scale; |
| 120 |
P(static_cast<LinearGradient*>(fill))->x2 *= scale; |
| 121 |
P(static_cast<LinearGradient*>(fill))->y2 *= scale; |
| 122 |
} else { |
| 123 |
P(static_cast<RadialGradient*>(fill))->cx *= scale; |
| 124 |
P(static_cast<RadialGradient*>(fill))->cy *= scale; |
| 125 |
P(static_cast<RadialGradient*>(fill))->r *= scale; |
| 126 |
P(static_cast<RadialGradient*>(fill))->fx *= scale; |
| 127 |
P(static_cast<RadialGradient*>(fill))->fy *= scale; |
| 128 |
P(static_cast<RadialGradient*>(fill))->fr *= scale; |
| 129 |
} |
| 130 |
} |
| 131 |
return PP(shape)->update(renderer, transform, clips, opacity, pFlag, false); |
| 132 |
} |
| 133 |
|
| 134 |
bool bounds(float* x, float* y, float* w, float* h, TVG_UNUSED bool stroking) |
| 135 |
{ |
| 136 |
if (load() == 0.0f) return false; |
| 137 |
PP(shape)->bounds(x, y, w, h, true, true, false); |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
Paint* duplicate(Paint* ret) |
| 142 |
{ |
| 143 |
if (ret) TVGERR("RENDERER", "TODO: duplicate()"); |
| 144 |
|
| 145 |
load(); |
| 146 |
|
| 147 |
auto text = Text::gen().release(); |
| 148 |
auto dup = text->pImpl; |
| 149 |
P(shape)->duplicate(dup->shape); |
| 150 |
|
| 151 |
if (loader) { |
| 152 |
dup->loader = loader; |
| 153 |
++dup->loader->sharing; |
| 154 |
} |
| 155 |
|
| 156 |
dup->utf8 = strdup(utf8); |
| 157 |
dup->italic = italic; |
| 158 |
dup->fontSize = fontSize; |
| 159 |
|
| 160 |
return text; |
| 161 |
} |
| 162 |
|
| 163 |
Iterator* iterator() |
| 164 |
{ |
| 165 |
return nullptr; |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#endif |