Skip to content

Commit 73ff690

Browse files
committed
fix: Apply text opacity to outline in ML
There's no mention of this in Maplibre docs but from my testing it seems that Maplibre applies text opacity to the outline also. This commit applies this logic when converting Maplibre style into Galileo style rules. It also modifies `WithOpacity` expression to blend alpha value insted of replacing it, as it seems to be closer to what Maplibre does.
1 parent 615daf2 commit 73ff690

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

galileo-maplibre/src/layer/vector_tile.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,18 @@ fn get_background(layers: &[&MaplibreStyleLayer]) -> ColorExpr {
9696

9797
get_color_value(
9898
&layer.paint.background_color,
99-
&layer.paint.background_opacity,
99+
Some(&layer.paint.background_opacity),
100100
)
101101
.map(Into::into)
102102
.unwrap_or(DEFAULT_TILE_BACKGROUND)
103103
}
104104

105-
fn get_color_value(color: &MlStyleValue<MlColor>, opacity: &MlStyleValue<f64>) -> Option<Expr> {
105+
fn get_color_value(
106+
color: &MlStyleValue<MlColor>,
107+
opacity: Option<&MlStyleValue<f64>>,
108+
) -> Option<Expr> {
106109
let galileo_color = get_galileo_value(color)?;
107-
let galileo_opacity = get_galileo_value(opacity);
110+
let galileo_opacity = opacity.map(|v| get_galileo_value(v).unwrap_or(1.0.into()));
108111

109112
Some(match galileo_opacity {
110113
Some(v) => Expr::WithOpacity(WithOpacityExpr {
@@ -245,9 +248,13 @@ fn symbol_rule(symbol: &SymbolLayer, tile_schema: &TileSchema) -> Option<StyleRu
245248
.and_then(|lod| tile_schema.lod_resolution(lod.round() as u32));
246249
let filter = symbol.filter.as_ref().and_then(|v| v.to_galileo_expr());
247250

248-
let font_color = get_color_value(&symbol.paint.text_color, &symbol.paint.text_opacity)?;
249-
let outline_color =
250-
get_color_value(&symbol.paint.text_halo_color, &MlStyleValue::Literal(1.0))?;
251+
let font_color = get_color_value(&symbol.paint.text_color, Some(&symbol.paint.text_opacity))?;
252+
// Even though Maplibre docs don't mention this, but it seems that text opacity is also
253+
// applied to the outline color, so we use it here.
254+
let outline_color = get_color_value(
255+
&symbol.paint.text_halo_color,
256+
Some(&symbol.paint.text_opacity),
257+
)?;
251258
let font_size = get_galileo_value(
252259
&symbol
253260
.layout
@@ -264,6 +271,7 @@ fn symbol_rule(symbol: &SymbolLayer, tile_schema: &TileSchema) -> Option<StyleRu
264271
)?;
265272

266273
let (font_family, weight, font_style) = parse_ml_fonts(&symbol.layout.text_font);
274+
dbg!(&font_color);
267275

268276
let style = VtTextStyle {
269277
font_family,
@@ -385,7 +393,7 @@ fn fill_rule(fill: &FillLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
385393

386394
let fill_color = &fill.paint.fill_color;
387395
let fill_opacity = &fill.paint.fill_opacity;
388-
let color = get_color_value(fill_color, fill_opacity)?;
396+
let color = get_color_value(fill_color, Some(fill_opacity))?;
389397

390398
if !fill.paint.fill_antialias {
391399
log::debug!(
@@ -443,7 +451,7 @@ fn line_rule(line: &LineLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
443451

444452
let stroke_color = &line.paint.line_color;
445453
let stroke_opacity = &line.paint.line_opacity;
446-
let color = get_color_value(stroke_color, stroke_opacity)
454+
let color = get_color_value(stroke_color, Some(stroke_opacity))
447455
.unwrap_or(Color::TRANSPARENT.into())
448456
.into();
449457
let stroke_width = &line.paint.line_width;

galileo/src/expr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl WithOpacityExpr {
258258
return ExprValue::Null;
259259
};
260260

261-
ExprValue::Color(color.with_alpha_float(opacity))
261+
ExprValue::Color(color.with_alpha_float(color.a() as f64 / 255.0 * opacity))
262262
}
263263
}
264264

0 commit comments

Comments
 (0)