Skip to content

Commit b31b67d

Browse files
committed
Font style inheritence added.
1 parent ff8c48b commit b31b67d

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

src/chart/main/chart.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Chart::Chart() :
3131
void Chart::setBoundRect(const Geom::Rect &rect, Gfx::ICanvas &info)
3232
{
3333
if (actDiagram) {
34+
actDiagram->getStyle().setup();
3435
layout.setBoundary(rect, *actDiagram, info);
3536
}
3637
}

src/chart/main/style.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ using namespace Vizzu::Styles;
1010
#pragma clang diagnostic ignored "-Wc99-designator"
1111
#endif
1212

13+
Font Chart::defaultFont{
14+
.fontFamily = ::Anim::String("Roboto, sans-serif"),
15+
.fontStyle = Gfx::Font::Style::normal,
16+
.fontWeight = Gfx::Font::Weight::Normal(),
17+
.fontSize = Gfx::Length(16)
18+
};
19+
1320
Chart Chart::def()
1421
{
1522
return {
@@ -28,7 +35,7 @@ Chart Chart::def()
2835
.fontFamily = ::Anim::String("Roboto, sans-serif"),
2936
.fontStyle = Gfx::Font::Style::normal,
3037
.fontWeight = Gfx::Font::Weight::Normal(),
31-
.fontSize = 11
38+
.fontSize = Gfx::Length::Relative(1)
3239
},
3340
.plot = {
3441
{
@@ -62,10 +69,10 @@ Chart Chart::def()
6269
.paddingLeft = Gfx::Length::Absolute(5)
6370
},
6471
{
65-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
72+
.fontFamily = ::Anim::String(),
6673
.fontStyle = Gfx::Font::Style::normal,
6774
.fontWeight = Gfx::Font::Weight::Normal(),
68-
.fontSize = 11
75+
.fontSize = Gfx::Length::Relative(0.6875)
6976
},
7077
{
7178
.color = Param<Gfx::Color>(),
@@ -93,10 +100,10 @@ Chart Chart::def()
93100
.paddingLeft = Gfx::Length::Absolute(0)
94101
},
95102
{
96-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
103+
.fontFamily = ::Anim::String(),
97104
.fontStyle = Gfx::Font::Style::normal,
98105
.fontWeight = Gfx::Font::Weight::Normal(),
99-
.fontSize = 14
106+
.fontSize = Gfx::Length::Relative(0.875)
100107
},
101108
{
102109
.color = Gfx::Color::Gray(0.6),
@@ -116,10 +123,10 @@ Chart Chart::def()
116123
.paddingLeft = Gfx::Length::Absolute(25)
117124
},
118125
{
119-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
126+
.fontFamily = ::Anim::String(),
120127
.fontStyle = Gfx::Font::Style::normal,
121128
.fontWeight = Gfx::Font::Weight::Normal(),
122-
.fontSize = 12
129+
.fontSize = Gfx::Length::Relative(0.75)
123130
},
124131
{
125132
.color = Gfx::Color::RGB(0xababab),
@@ -168,10 +175,10 @@ Chart Chart::def()
168175
.paddingLeft = Gfx::Length::Absolute(5)
169176
},
170177
{
171-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
178+
.fontFamily = ::Anim::String(),
172179
.fontStyle = Gfx::Font::Style::normal,
173180
.fontWeight = Gfx::Font::Weight::Normal(),
174-
.fontSize = 14
181+
.fontSize = Gfx::Length::Relative(0.875)
175182
},
176183
{
177184
.color = Gfx::Color::Gray(0.77),
@@ -190,10 +197,10 @@ Chart Chart::def()
190197
.paddingLeft = Gfx::Length::Absolute(10)
191198
},
192199
{
193-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
200+
.fontFamily = ::Anim::String(),
194201
.fontStyle = Gfx::Font::Style::normal,
195202
.fontWeight = Gfx::Font::Weight::Normal(),
196-
.fontSize = 14
203+
.fontSize = Gfx::Length::Relative(0.875)
197204
},
198205
{
199206
.color = Gfx::Color::Gray(0.45),
@@ -218,10 +225,10 @@ Chart Chart::def()
218225
.paddingLeft = Gfx::Length::Absolute(10)
219226
},
220227
{
221-
.fontFamily = ::Anim::String("Roboto, sans-serif"),
228+
.fontFamily = ::Anim::String(),
222229
.fontStyle = Gfx::Font::Style::normal,
223230
.fontWeight = Gfx::Font::Weight::Normal(),
224-
.fontSize = 26
231+
.fontSize = Gfx::Length::Relative(1.625)
225232
},
226233
{
227234
.color = Gfx::Color::RGB(0x494949),

src/chart/main/style.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,38 @@ struct Font {
6363
Param<::Anim::String> fontFamily;
6464
Param<Gfx::Font::Style> fontStyle;
6565
Param<Gfx::Font::Weight> fontWeight;
66-
Param<double> fontSize;
66+
Param<Gfx::Length> fontSize;
67+
const Font *fontParent = nullptr;
68+
69+
double calculatedSize() const
70+
{
71+
if (fontSize.has_value() && fontSize->isAbsolute())
72+
return fontSize->get();
73+
74+
if (fontSize.has_value() && fontParent)
75+
return fontSize->get(fontParent->calculatedSize());
76+
77+
if (fontParent)
78+
return fontParent->calculatedSize();
79+
80+
throw std::logic_error("internal error: no font parent set");
81+
}
82+
83+
std::string calculatedFamily() const
84+
{
85+
if (fontFamily.has_value() && !fontFamily->values[0].value.empty())
86+
return fontFamily->values[0].value;
87+
88+
if (fontParent)
89+
return fontParent->calculatedFamily();
90+
91+
throw std::logic_error("internal error: no font parent set");
92+
}
6793

6894
explicit operator Gfx::Font() const
6995
{
70-
return Gfx::Font(fontFamily->values[0].value,
71-
*fontStyle, *fontWeight, *fontSize);
96+
return Gfx::Font(calculatedFamily(),
97+
*fontStyle, *fontWeight, calculatedSize());
7298
}
7399

74100
void visit(auto &visitor)
@@ -358,7 +384,22 @@ struct Chart : Padding, Box, Font
358384
(data, "data");
359385
}
360386

387+
static Font defaultFont;
361388
static Chart def();
389+
390+
void setup()
391+
{
392+
std::vector<Font*> fonts{
393+
&title,
394+
&plot.axis.title,
395+
&plot.axis.label,
396+
&plot.marker.label,
397+
&legend.title,
398+
&legend.label
399+
};
400+
fontParent = &defaultFont;
401+
for (auto font : fonts) font->fontParent = (Font*)this;
402+
}
362403
};
363404

364405
}

0 commit comments

Comments
 (0)