use std::collections::BTreeSet;
use std::num::NonZeroU32;
use geometry::Point;
use glam::Vec2;
use mln_style::types::{AlignmentType, IconTextFitType, TextRotationAlignmentType, TextWritingModeType};
use super::context::translation;
use super::{JointOpacityState, Placement, VariableOffset};
use crate::camera::{TileProjector, View};
use crate::layer::render_layer::BucketPlacementData;
use crate::placement::collision_index::OverlapMode;
use crate::symbol::bucket::{Buffer, SymbolBucket, SymbolBucketState};
use crate::symbol::instance::{Justification, SymbolInstance, TextPart};
use crate::symbol::projection::{
LabelPlaneProjector, LineLabelAlignment, SymbolHeights, add_dynamic_attributes, evaluate_size_for_feature,
hide_glyphs, reproject_line_labels, tile_skew_vectors,
};
use crate::text::constants::ONE_EM;
fn extend<T: Copy>(v: &mut Vec<T>, n: usize, value: T) {
v.extend(std::iter::repeat_n(value, n));
}
impl Placement {
fn orientation_of(&self, allow_vertical: bool, id: Option<NonZeroU32>) -> Option<TextWritingModeType> {
if !allow_vertical {
return None;
}
self.placed_orientations.get(&id?).copied()
}
fn shows_run(
&self,
allow_vertical: bool,
text: &TextPart,
id: Option<NonZeroU32>,
run: usize,
orientation: TextWritingModeType,
) -> bool {
let shown = self.orientation_of(allow_vertical, id);
if allow_vertical && shown != Some(orientation) {
return false;
}
let Some(offset) = id.and_then(|id| self.variable_offsets.get(&id)) else {
return false;
};
orientation == TextWritingModeType::Vertical
|| text.runs.get(Justification::of_anchor(offset.anchor)).is_none_or(|justified| justified == run)
}
pub(crate) fn update_bucket_opacities(
&self,
bucket: &mut SymbolBucket,
view: &View,
data: &BucketPlacementData,
seen_cross_tile_ids: &mut BTreeSet<NonZeroU32>,
) -> bool {
let layout = &*bucket.layout;
let text_always = OverlapMode::text(layout) == OverlapMode::Always;
let icon_always = OverlapMode::icon(layout) == OverlapMode::Always;
let has_text = bucket.has_text_data();
let has_icons = bucket.has_icon_data() || bucket.has_sdf_icon_data();
let shows_early = *layout.symbol_screen_space.uniform() || bucket.state.just_reloaded;
let default_opacity = JointOpacityState::new(
shows_early && text_always && (icon_always || !has_icons || *layout.icon_optional.uniform()),
shows_early && icon_always && (text_always || !has_text || *layout.text_optional.uniform()),
true,
);
let duplicate_opacity = JointOpacityState::new(false, false, true);
let state = &mut bucket.state;
for buffer in [&mut state.text, &mut state.icon, &mut state.sdf_icon] {
buffer.opacity_attribute_data.clear();
buffer.elevation_attribute_data.clear();
}
let heights = SymbolHeights::new(self.ground(), data.tile.id, layout);
let symbols = std::mem::take(&mut state.symbol_instances);
for symbol in &symbols {
let id = symbol.cross_tile_id;
let opacity = match id {
Some(id) if seen_cross_tile_ids.insert(id) => {
self.opacities.get(&id).copied().unwrap_or(default_opacity)
}
_ => duplicate_opacity,
};
let hidden = opacity.is_hidden();
if let Some(text) = &symbol.text {
let vertex = SymbolBucket::opacity_attributes(opacity.text.placed, opacity.text.opacity);
for (run, _) in text.runs.all() {
set_run_opacity(&mut state.text, run, hidden, vertex, heights);
}
}
if let Some(icon) = &symbol.icon {
let vertex = SymbolBucket::opacity_attributes(opacity.icon.placed, opacity.icon.opacity);
let buffer = if icon.sdf { &mut state.sdf_icon } else { &mut state.icon };
for (run, _) in icon.runs() {
set_run_opacity(buffer, run, hidden, vertex, heights);
}
}
}
state.symbol_instances = symbols;
bucket.sort_features(view.angle() as f32)
}
pub(crate) fn update_bucket_positions(
&self,
bucket: &mut SymbolBucket,
view: &View,
data: &BucketPlacementData,
layer_id: Option<&str>,
) -> bool {
let layout = &*bucket.layout;
let tile = &data.tile;
let along_line = *layout.symbol_placement.uniform() != mln_style::types::SymbolPlacementType::Point;
let has_text = bucket.has_text_data();
let has_variable_anchors = bucket.has_variable_text_anchors() && has_text;
let screen_space = *layout.symbol_screen_space.uniform();
let text_translation = translation(&tile.id, &data.text_translate, view, screen_space);
let icon_translation = translation(&tile.id, &data.icon_translate, view, screen_space);
let state = &mut bucket.state;
match layer_id {
None => {
state.dynamic_text_translation = text_translation;
state.dynamic_icon_translation = icon_translation;
}
Some(layer_id) => {
let moved = text_translation != state.dynamic_text_translation
|| icon_translation != state.dynamic_icon_translation;
if (!along_line && !has_variable_anchors)
|| (!state.has_layer_dynamic_attribute_data(layer_id) && !moved)
{
return false;
}
}
}
let tile_projector = TileProjector::with_data(view, &tile.id, tile.projection);
let heights = SymbolHeights::new(self.ground(), tile.id, layout);
if along_line {
let mut laid_out = false;
if *layout.icon_rotation_alignment.uniform() == AlignmentType::Map {
let alignment = LineLabelAlignment {
pitch_with_map: *layout.icon_pitch_alignment.uniform() == AlignmentType::Map,
rotate_with_map: true,
keep_upright: *layout.icon_keep_upright.uniform(),
};
for buffer in [&mut bucket.state.sdf_icon, &mut bucket.state.icon] {
if buffer.segments.is_empty() {
continue;
}
let (positions, runs, pools) = buffer.positions_and_symbols(layer_id);
reproject_line_labels(
positions,
runs,
pools,
&tile_projector,
heights,
&bucket.icon_size_binder,
icon_translation,
alignment,
);
laid_out = true;
}
}
let rotation_alignment = *layout.text_rotation_alignment.uniform();
if has_text && rotation_alignment != TextRotationAlignmentType::Viewport {
let (positions, runs, pools) = bucket.state.text.positions_and_symbols(layer_id);
reproject_line_labels(
positions,
runs,
pools,
&tile_projector,
heights,
&bucket.text_size_binder,
text_translation,
LineLabelAlignment {
pitch_with_map: *layout.text_pitch_alignment.uniform() == AlignmentType::Map,
rotate_with_map: rotation_alignment == TextRotationAlignmentType::Map,
keep_upright: *layout.text_keep_upright.uniform(),
},
);
laid_out = true;
}
laid_out
} else if has_variable_anchors {
self.lay_out_variable_anchors(bucket, view, data, layer_id, &tile_projector, text_translation);
true
} else if bucket.allow_vertical_placement && has_text {
self.lay_out_orientations(bucket, layer_id);
true
} else {
false
}
}
fn lay_out_variable_anchors(
&self,
bucket: &mut SymbolBucket,
view: &View,
data: &BucketPlacementData,
layer_id: Option<&str>,
tile_projector: &TileProjector<'_>,
translation: Vec2,
) {
let layout = &*bucket.layout;
let tile = &data.tile;
let zoom = view.zoom() as f32;
let text_size = bucket.text_size_binder.evaluate_for_zoom(zoom);
let tile_scale = 2f64.powf(view.zoom() - f64::from(tile.overscaled_id.overscaled_z)) as f32;
let rotate_with_map = *layout.text_rotation_alignment.uniform() == TextRotationAlignmentType::Map;
let pitch_with_map = *layout.text_pitch_alignment.uniform() == AlignmentType::Map;
let fit_icons = *layout.icon_text_fit.uniform() != IconTextFitType::None
&& (bucket.has_icon_data() || bucket.has_sdf_icon_data());
let heights = SymbolHeights::new(self.ground(), tile.id, layout);
let label_plane = LabelPlaneProjector::new(
tile_projector,
heights,
pitch_with_map,
rotate_with_map,
tile.id.pixels_to_tile_units(1.0, zoom),
translation,
);
let (east, south) = if pitch_with_map && !rotate_with_map {
let (east, south) = tile_skew_vectors(view);
(east.as_vec2(), south.as_vec2())
} else {
(Vec2::X, Vec2::Y)
};
let shifted_anchor = |offset: &VariableOffset, run: &crate::symbol::bucket::PlacedSymbol| {
let label_plane = label_plane.raised(run.height_offset);
let tile_anchor = run.anchor_point;
let projected = if pitch_with_map {
label_plane.to_clip_space_from_tile(tile_anchor)
} else {
label_plane.project(tile_anchor)
};
let projected_anchor = projected.point;
let perspective_ratio = 0.5 + 0.5 * (view.camera_to_center_distance() as f32 / projected.camera_distance);
let mut size = evaluate_size_for_feature(&text_size, run) * perspective_ratio / ONE_EM;
if pitch_with_map {
size *= bucket.tile_pixel_ratio / tile_scale;
}
let shift = offset.render_shift(size);
if pitch_with_map {
let correction = tile_projector.pitched_text_correction(&Point::new(
f64::from(tile_anchor.x + translation.x),
f64::from(tile_anchor.y + translation.y),
)) as f32;
label_plane.project(tile_anchor + (east * shift.x + south * shift.y) * correction).point
} else if rotate_with_map {
let east = label_plane.project_at(tile_anchor + Vec2::X, label_plane.elevation(tile_anchor)).point;
let to_east = east - projected_anchor;
let angle = (to_east.y / to_east.x).atan() + if to_east.x < 0.0 { std::f32::consts::PI } else { 0.0 };
projected_anchor + Vec2::from_angle(angle).rotate(shift)
} else {
projected_anchor + shift
}
};
let allow_vertical = bucket.allow_vertical_placement;
let SymbolBucketState {
symbol_instances: symbols,
text: text_buffer,
icon,
sdf_icon,
has_variable_placement,
..
} = &mut bucket.state;
let mut icon_anchors: Vec<[Option<Vec2>; 2]> = vec![[None; 2]; if fit_icons { symbols.len() } else { 0 }];
let mut placed_any = false;
let (positions, runs, _) = text_buffer.positions_and_symbols(layer_id);
positions.clear();
for (index, symbol) in symbols.iter().enumerate() {
let Some(text) = &symbol.text else {
continue;
};
let id = symbol.cross_tile_id;
let offset = id.and_then(|id| self.variable_offsets.get(&id));
for (run_index, orientation) in text.runs.all() {
let run = &runs[run_index];
let glyphs = run.glyph_offsets.len();
let Some(offset) =
offset.filter(|_| !run.hidden && self.shows_run(allow_vertical, text, id, run_index, orientation))
else {
hide_glyphs(glyphs, positions);
continue;
};
placed_any = true;
let anchor = shifted_anchor(offset, run);
if let Some(icon_anchor) = icon_anchors.get_mut(index) {
icon_anchor[orientation as usize].get_or_insert(anchor);
}
for _ in 0..glyphs {
add_dynamic_attributes(anchor, run.angle, 0.0, positions);
}
}
}
if layer_id.is_none() {
*has_variable_placement = placed_any;
}
if !(fit_icons && placed_any) {
return;
}
for (buffer, sdf) in [(icon, false), (sdf_icon, true)] {
let (positions, runs, _) = buffer.positions_and_symbols(layer_id);
positions.clear();
for (index, symbol) in symbols.iter().enumerate() {
let Some(icon) = symbol.icon.as_ref().filter(|icon| icon.sdf == sdf) else {
continue;
};
for (run_index, orientation) in icon.runs() {
let run = &runs[run_index];
match icon_anchors[index][orientation as usize].filter(|_| !run.hidden) {
Some(anchor) => {
for _ in 0..run.glyph_offsets.len() {
add_dynamic_attributes(anchor, run.angle, 0.0, positions);
}
}
None => hide_glyphs(run.glyph_offsets.len(), positions),
}
}
}
}
}
fn lay_out_orientations(&self, bucket: &mut SymbolBucket, layer_id: Option<&str>) {
let fit_icons = *bucket.layout.icon_text_fit.uniform() != IconTextFitType::None;
let allow_vertical = bucket.allow_vertical_placement;
let SymbolBucketState { symbol_instances: symbols, text, icon, sdf_icon, .. } = &mut bucket.state;
let shown = |symbol: &SymbolInstance| self.orientation_of(allow_vertical, symbol.cross_tile_id);
let write =
|buffer: &mut Buffer,
runs: &mut dyn Iterator<Item = (Option<TextWritingModeType>, usize, TextWritingModeType)>| {
let (positions, placed, _) = buffer.positions_and_symbols(layer_id);
positions.clear();
for (shown, run_index, orientation) in runs {
let run = &placed[run_index];
if run.hidden || shown != Some(orientation) {
hide_glyphs(run.glyph_offsets.len(), positions);
} else {
for _ in 0..run.glyph_offsets.len() {
add_dynamic_attributes(run.anchor_point, run.angle, 0.0, positions);
}
}
}
};
write(
text,
&mut symbols.iter().flat_map(|symbol| {
let shown = shown(symbol);
symbol
.text
.iter()
.flat_map(move |text| text.runs.all().map(move |(run, orientation)| (shown, run, orientation)))
}),
);
if fit_icons {
for (buffer, sdf) in [(icon, false), (sdf_icon, true)] {
write(
buffer,
&mut symbols.iter().flat_map(|symbol| {
let shown = shown(symbol);
symbol
.icon
.iter()
.filter(move |icon| icon.sdf == sdf)
.flat_map(move |icon| icon.runs().map(move |(run, orientation)| (shown, run, orientation)))
}),
);
}
}
}
}
fn set_run_opacity(
buffer: &mut Buffer,
run: usize,
hidden: bool,
vertex: crate::symbol::bucket::SymbolOpacityVertex,
heights: SymbolHeights<'_>,
) {
let run = &mut buffer.placed_symbols[run];
run.hidden = hidden;
let vertices = 4 * run.glyph_offsets.len();
extend(&mut buffer.opacity_attribute_data, vertices, vertex);
if heights.on_ground() {
extend(&mut buffer.elevation_attribute_data, vertices, heights.at(run.anchor_point, run.height_offset));
}
}