Skip to content

Commit f572900

Browse files
authored
feat: Add interpolation for text style properties (galileo-map#302)
1 parent 32b10ac commit f572900

4 files changed

Lines changed: 102 additions & 11 deletions

File tree

galileo/examples/vector_tiles_labels.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
use galileo::layer::data_provider::remove_parameters_modifier;
44
use galileo::layer::vector_tile_layer::style::{
5-
StyleRule, VectorTileLabelSymbol, VectorTileStyle, VectorTileSymbol,
5+
StyleRule, VectorTileLabelSymbol, VectorTileStyle, VectorTileSymbol, VtTextStyle,
66
};
77
use galileo::layer::vector_tile_layer::{VectorTileLayer, VectorTileLayerBuilder};
88
use galileo::render::text::text_service::TextService;
9-
use galileo::render::text::{FontWeight, RustybuzzRasterizer, TextStyle};
9+
use galileo::render::text::{FontWeight, RustybuzzRasterizer};
1010
use galileo::tile_schema::{TileIndex, TileSchema, TileSchemaBuilder};
1111
use galileo::{Color, MapBuilder};
1212

@@ -48,7 +48,7 @@ pub(crate) fn run() {
4848
properties: Default::default(),
4949
symbol: VectorTileSymbol::Label(VectorTileLabelSymbol {
5050
pattern: String::from("{name}"),
51-
text_style: TextStyle {
51+
text_style: VtTextStyle {
5252
font_family: vec![
5353
"Noto Sans".to_string(),
5454
"Noto Sans Arabic".to_string(),
@@ -57,14 +57,14 @@ pub(crate) fn run() {
5757
"Noto Sans KR".to_string(),
5858
"Noto Sans JP".to_string(),
5959
],
60-
font_size: 12.0,
61-
font_color: Color::BLACK,
60+
font_size: 12.0.into(),
61+
font_color: Color::BLACK.into(),
6262
horizontal_alignment: Default::default(),
6363
vertical_alignment: Default::default(),
6464
weight: FontWeight::BOLD,
6565
style: Default::default(),
66-
outline_width: 2.0,
67-
outline_color: Color::WHITE,
66+
outline_width: 2.0.into(),
67+
outline_color: Color::WHITE.into(),
6868
},
6969
}),
7070
}],

galileo/src/layer/vector_tile_layer/expressions.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ impl StyleValue<f64> {
191191
}
192192
}
193193

194+
impl StyleValue<f32> {
195+
/// Evaluates value of Number depending upon the type of expression used.
196+
pub fn get_value(&self, current_resolution: f64) -> f32 {
197+
match self {
198+
StyleValue::Simple(t) => *t,
199+
StyleValue::Interpolate(expression) => expression.evaluate(current_resolution),
200+
StyleValue::Steps(expression) => expression.evaluate(current_resolution),
201+
}
202+
}
203+
}
204+
194205
impl<T> InterpolateExpression<T> {
195206
/// Returns a new instance of `InterpolateExpression`
196207
pub fn new(interpolation_args: InterpolationArgs<T>) -> Self {
@@ -234,6 +245,22 @@ trait InterpolatableValue: Copy {
234245
fn set_component(&mut self, component: &Self::COMPONENTS, value: f64);
235246
}
236247

248+
impl InterpolatableValue for f32 {
249+
type COMPONENTS = ();
250+
251+
fn iter_components() -> impl Iterator<Item = Self::COMPONENTS> {
252+
std::iter::once(())
253+
}
254+
255+
fn get_component(&self, _component: &Self::COMPONENTS) -> f64 {
256+
*self as f64
257+
}
258+
259+
fn set_component(&mut self, _component: &Self::COMPONENTS, value: f64) {
260+
*self = value as f32
261+
}
262+
}
263+
237264
impl InterpolatableValue for f64 {
238265
type COMPONENTS = ();
239266

galileo/src/layer/vector_tile_layer/style.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use serde::{Deserialize, Serialize};
55

66
use crate::layer::vector_tile_layer::expressions::StyleValue;
77
use crate::render::point_paint::PointPaint;
8-
use crate::render::text::TextStyle;
8+
use crate::render::text::{
9+
FontStyle, FontWeight, HorizontalAlignment, TextStyle, VerticalAlignment,
10+
};
911
use crate::render::{LineCap, LinePaint, PolygonPaint};
1012
use crate::Color;
1113

@@ -329,7 +331,68 @@ pub struct VectorTileLabelSymbol {
329331
/// Text of the label with substitutes for feature attributes.
330332
pub pattern: String,
331333
/// Style of the text.
332-
pub text_style: TextStyle,
334+
pub text_style: VtTextStyle,
335+
}
336+
337+
/// Raw Style of a text label that generates a `TextStyle`.
338+
/// This allows for interpolation of fields like font_size,font_color,outline_color,outline_width.
339+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
340+
pub struct VtTextStyle {
341+
/// Name of the font to use.
342+
pub font_family: Vec<String>,
343+
/// Size of the font in pixels.
344+
pub font_size: StyleValue<f32>,
345+
/// Color of the font.
346+
#[serde(default = "default_font_color_style")]
347+
pub font_color: StyleValue<Color>,
348+
/// Alignment of label along horizontal axis.
349+
#[serde(default)]
350+
pub horizontal_alignment: HorizontalAlignment,
351+
/// Alignment of label along vertical axis.
352+
#[serde(default)]
353+
pub vertical_alignment: VerticalAlignment,
354+
/// Weight of the font.
355+
#[serde(default)]
356+
pub weight: FontWeight,
357+
/// sTyle of the font.
358+
#[serde(default)]
359+
pub style: FontStyle,
360+
/// Width of the outline around the letters.
361+
#[serde(default = "default_outline_width_style")]
362+
pub outline_width: StyleValue<f32>,
363+
/// Color of the outline around the letters.
364+
#[serde(default = "default_outline_color_style")]
365+
pub outline_color: StyleValue<Color>,
366+
}
367+
368+
impl VtTextStyle {
369+
/// This method returns the value of the struct `TextStyle` on the basis of the
370+
/// current resolution level.
371+
pub fn get_value(self, current_resolution: f64) -> TextStyle {
372+
TextStyle {
373+
font_family: self.font_family,
374+
font_size: self.font_size.get_value(current_resolution),
375+
font_color: self.font_color.get_value(current_resolution),
376+
horizontal_alignment: self.horizontal_alignment,
377+
vertical_alignment: self.vertical_alignment,
378+
weight: self.weight,
379+
style: self.style,
380+
outline_width: self.outline_width.get_value(current_resolution),
381+
outline_color: self.outline_color.get_value(current_resolution),
382+
}
383+
}
384+
}
385+
386+
fn default_font_color_style() -> StyleValue<Color> {
387+
Color::BLACK.into()
388+
}
389+
390+
fn default_outline_color_style() -> StyleValue<Color> {
391+
Color::TRANSPARENT.into()
392+
}
393+
394+
fn default_outline_width_style() -> StyleValue<f32> {
395+
0.0.into()
333396
}
334397

335398
#[cfg(test)]

galileo/src/layer/vector_tile_layer/tile_provider/vt_processor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ impl VtProcessor {
137137
.or_else(|| {
138138
rule.symbol
139139
.label()
140-
.and_then(|symbol| Self::format_label(symbol, feature))
140+
.and_then(|symbol| Self::format_label(symbol, resolution, feature))
141141
})
142142
}
143143

144144
fn format_label<'a>(
145145
label_symbol: &VectorTileLabelSymbol,
146+
resolution: f64,
146147
feature: &MvtFeature,
147148
) -> Option<PointPaint<'a>> {
148149
let re = Regex::new("\\{(?<name>.+)}").ok()?;
@@ -159,7 +160,7 @@ impl VtProcessor {
159160
}
160161
Some(PointPaint::label_owned(
161162
text,
162-
label_symbol.text_style.clone(),
163+
label_symbol.text_style.clone().get_value(resolution),
163164
))
164165
}
165166

0 commit comments

Comments
 (0)