Skip to content

Commit 302da84

Browse files
committed
Merge pull request tangrams#367 from tangrams/word-wrapping
Word wrapping
2 parents 8483bbb + a2a7381 commit 302da84

15 files changed

+365
-60
lines changed

Diff for: core/include/fontstash-es

Diff for: core/resources/scene.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ layers:
338338
draw:
339339
text:
340340
interactive: true
341-
offset: [0px, 13px]
341+
offset: [0, 12px]
342+
align: right
343+
text_wrap: 20
342344
font:
343345
family: sans-serif
344346
weight: 400
@@ -447,7 +449,7 @@ layers:
447449
family: sans-serif
448450
weight: 400
449451
style: normal
450-
size: 18px
452+
size: 20px
451453
fill: darkgreen
452454

453455
pois:

Diff for: core/src/labels/textLabel.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Tangram {
44

5-
TextLabel::TextLabel(Label::Transform _transform, Type _type, glm::vec2 _dim,
6-
TextBuffer& _mesh, Range _vertexRange, Label::Options _options)
7-
: Label(_transform, _dim, _type, static_cast<LabelMesh&>(_mesh), _vertexRange, _options)
5+
TextLabel::TextLabel(Label::Transform _transform, Type _type, glm::vec2 _dim, TextBuffer& _mesh,
6+
Range _vertexRange, Label::Options _options, FontContext::FontMetrics _metrics, int _nLines)
7+
: Label(_transform, _dim, _type, static_cast<LabelMesh&>(_mesh), _vertexRange, _options),
8+
m_metrics(_metrics), m_nLines(_nLines)
89
{}
910

1011
void TextLabel::updateBBoxes(float _zoomFract) {
@@ -20,9 +21,10 @@ void TextLabel::updateBBoxes(float _zoomFract) {
2021
obbCenter = m_transform.state.screenPos;
2122
// move forward on line by half the text length
2223
obbCenter += t * m_dim.x * 0.5f;
23-
// TODO: use real font metrics
2424
// move down on the perpendicular to estimated font baseline
25-
obbCenter -= tperp * (m_dim.y / 8);
25+
obbCenter -= tperp * m_dim.y * 0.5f;
26+
// ajdust with baseline
27+
obbCenter += tperp * m_metrics.lineHeight * (float) m_nLines * 0.5f;
2628

2729
m_obb = OBB(obbCenter.x, obbCenter.y, m_transform.state.rotation, m_dim.x, m_dim.y);
2830
m_aabb = m_obb.getExtent();

Diff for: core/src/labels/textLabel.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
#include "labels/label.h"
44
#include "text/textBuffer.h"
5+
#include "text/fontContext.h"
56

67
namespace Tangram {
78

89
class TextLabel : public Label {
910

1011
public:
11-
TextLabel(Label::Transform _transform, Type _type,
12-
glm::vec2 _dim, TextBuffer& _mesh, Range _vertexRange, Label::Options _options);
12+
TextLabel(Label::Transform _transform, Type _type, glm::vec2 _dim, TextBuffer& _mesh, Range _vertexRange,
13+
Label::Options _options, FontContext::FontMetrics _metrics, int _nLines);
1314

1415
void updateBBoxes(float _zoomFract) override;
1516

1617
protected:
1718

1819
void align(glm::vec2& _screenPosition, const glm::vec2& _ap1, const glm::vec2& _ap2) override;
20+
FontContext::FontMetrics m_metrics;
21+
int m_nLines;
22+
1923
};
2024

2125
}

Diff for: core/src/scene/styleParam.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Tangram {
1313
using Color = CSSColorParser::Color;
1414

1515
const std::map<std::string, StyleParamKey> s_StyleParamMap = {
16+
{"align", StyleParamKey::align},
1617
{"cap", StyleParamKey::cap},
1718
{"centroid", StyleParamKey::centroid},
1819
{"collide", StyleParamKey::collide},
@@ -42,6 +43,7 @@ const std::map<std::string, StyleParamKey> s_StyleParamMap = {
4243
{"sprite_default", StyleParamKey::sprite_default},
4344
{"style", StyleParamKey::style},
4445
{"text_source", StyleParamKey::text_source},
46+
{"text_wrap", StyleParamKey::text_wrap},
4547
{"transition:hide:time", StyleParamKey::transition_hide_time},
4648
{"transition:selected:time", StyleParamKey::transition_selected_time},
4749
{"transition:show:time", StyleParamKey::transition_show_time},
@@ -114,6 +116,14 @@ StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string&
114116
}
115117
return vec2;
116118
}
119+
case StyleParamKey::text_wrap: {
120+
int textWrap;
121+
if (_value == "true") return textWrap;
122+
if (_value == "false") return std::numeric_limits<uint32_t>::max();
123+
if (parseInt(_value, textWrap) > 0) {
124+
return static_cast<uint32_t>(textWrap);
125+
}
126+
}
117127
case StyleParamKey::offset: {
118128
auto vec2 = glm::vec2(0.f, 0.f);
119129
if (!parseVec2(_value, { Unit::pixel }, vec2) || isnan(vec2.y)) {
@@ -136,6 +146,7 @@ StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string&
136146
LOGW("Invalid time param '%s'", _value.c_str());
137147
}
138148
return time;
149+
case StyleParamKey::align:
139150
case StyleParamKey::font_family:
140151
case StyleParamKey::font_weight:
141152
case StyleParamKey::font_style:
@@ -146,7 +157,7 @@ StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string&
146157
case StyleParamKey::style:
147158
return _value;
148159
case StyleParamKey::font_size: {
149-
float fontSize = 16;
160+
float fontSize;
150161
if (!parseFontSize(_value, fontSize)) {
151162
LOGW("Invalid font-size '%s'.", _value.c_str());
152163
}
@@ -242,9 +253,11 @@ std::string StyleParam::toString() const {
242253
case StyleParamKey::font_style:
243254
case StyleParamKey::text_source:
244255
case StyleParamKey::transform:
256+
case StyleParamKey::text_wrap:
245257
case StyleParamKey::sprite:
246258
case StyleParamKey::sprite_default:
247259
case StyleParamKey::style:
260+
case StyleParamKey::align:
248261
if (!value.is<std::string>()) break;
249262
return k + value.get<std::string>();
250263
case StyleParamKey::interactive:

Diff for: core/src/scene/styleParam.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Tangram {
1010
struct Stops;
1111

1212
enum class StyleParamKey : uint8_t {
13+
align,
1314
cap,
1415
color,
1516
extrude,
@@ -36,6 +37,7 @@ enum class StyleParamKey : uint8_t {
3637
sprite_default,
3738
style,
3839
text_source,
40+
text_wrap,
3941
transform,
4042
visible,
4143
width,

Diff for: core/src/style/textStyle.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ auto TextStyle::applyRule(const DrawRule& _rule, const Properties& _props) const
7171

7272
Parameters p;
7373

74-
std::string fontFamily, fontWeight, fontStyle, transform;
74+
std::string fontFamily, fontWeight, fontStyle, transform, align;
7575
glm::vec2 offset;
7676

7777
_rule.get(StyleParamKey::font_family, fontFamily);
@@ -93,12 +93,14 @@ auto TextStyle::applyRule(const DrawRule& _rule, const Properties& _props) const
9393
_rule.get(StyleParamKey::font_stroke_color, p.strokeColor);
9494
_rule.get(StyleParamKey::font_stroke_width, p.strokeWidth);
9595
_rule.get(StyleParamKey::transform, transform);
96+
_rule.get(StyleParamKey::align, align);
9697
_rule.get(StyleParamKey::visible, p.visible);
9798
_rule.get(StyleParamKey::priority, p.labelOptions.priority);
9899
_rule.get(StyleParamKey::collide, p.labelOptions.collide);
99100
_rule.get(StyleParamKey::transition_hide_time, p.labelOptions.hideTransition.time);
100101
_rule.get(StyleParamKey::transition_selected_time, p.labelOptions.selectTransition.time);
101102
_rule.get(StyleParamKey::transition_show_time, p.labelOptions.showTransition.time);
103+
_rule.get(StyleParamKey::text_wrap, p.maxLineWidth);
102104

103105
_rule.get(StyleParamKey::text_source, p.text);
104106
if (!_rule.isJSFunction(StyleParamKey::text_source)) {
@@ -121,6 +123,14 @@ auto TextStyle::applyRule(const DrawRule& _rule, const Properties& _props) const
121123
p.transform = TextTransform::uppercase;
122124
}
123125

126+
if (align == "left") {
127+
p.align = TextAlign::left;
128+
} else if (align == "right") {
129+
p.align = TextAlign::right;
130+
} else if (align == "center") {
131+
p.align = TextAlign::center;
132+
}
133+
124134
/* Global operations done for fontsize and sdfblur */
125135
float emSize = p.fontSize / 16.f;
126136
p.fontSize *= m_pixelScale;

Diff for: core/src/style/textStyle.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ enum class TextTransform {
1919
lowercase,
2020
};
2121

22+
enum class TextAlign : char {
23+
right,
24+
left,
25+
center
26+
};
27+
2228
class TextStyle : public Style {
2329

2430
public:
@@ -31,11 +37,14 @@ class TextStyle : public Style {
3137
uint32_t fill = 0xff000000;
3238
uint32_t strokeColor = 0xffffffff;
3339
float strokeWidth = 0.0f;
34-
float fontSize = 12.0f;
40+
float fontSize = 16.0f;
3541
float blurSpread = 0.0f;
3642
TextTransform transform = TextTransform::none;
43+
TextAlign align = TextAlign::center;
3744
bool visible = true;
3845
Label::Options labelOptions;
46+
bool wordWrap = true;
47+
unsigned int maxLineWidth = 15;
3948

4049
bool isValid() {
4150
return fontSize > 0.f && !text.empty();

Diff for: core/src/text/fontContext.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ FontID FontContext::addFont(const std::string& _family, const std::string& _weig
8282
}
8383
}
8484
}
85+
8586
font = fonsAddFont(m_fsContext, fontKey.c_str(), data, dataSize);
8687

8788
if (font == FONS_INVALID) {
@@ -101,6 +102,12 @@ FontID FontContext::addFont(const std::string& _family, const std::string& _weig
101102
return INVALID_FONT;
102103
}
103104

105+
FontContext::FontMetrics FontContext::getMetrics() {
106+
FontMetrics metrics;
107+
fonsVertMetrics(m_fsContext, &metrics.ascender, &metrics.descender, &metrics.lineHeight);
108+
return metrics;
109+
}
110+
104111
void FontContext::setFont(const std::string& _key, int size) {
105112
FontID id = getFontID(_key);
106113

Diff for: core/src/text/fontContext.h

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class FontContext {
1919

2020
public:
2121

22+
struct FontMetrics {
23+
float ascender;
24+
float descender;
25+
float lineHeight;
26+
};
2227

2328
~FontContext();
2429
FontContext();
@@ -53,7 +58,11 @@ class FontContext {
5358
return m_boundAtlasSize;
5459
}
5560

61+
/* Returns the metrics of the currently used font */
62+
FontMetrics getMetrics();
63+
5664
private:
65+
5766
static void renderUpdate(void* _userPtr, int* _rect, const unsigned char* _data);
5867
static int renderCreate(void* _userPtr, int _width, int _height);
5968
static void pushQuad(void* _userPtr, const FONSquad* _quad);
@@ -68,6 +77,7 @@ class FontContext {
6877
std::mutex m_contextMutex;
6978
std::mutex m_atlasMutex;
7079
bool m_handleAtlasFull = false;
80+
7181
FONScontext* m_fsContext;
7282
std::vector<FONSquad> m_quadBuffer;
7383
glm::vec2 m_boundAtlasSize;

0 commit comments

Comments
 (0)