use glam::Vec2;
use mln_math::{deg2radf, fmaxf, fminf};
use mln_style::layers::symbol_layer_properties::SymbolLayoutProperties;
use mln_style_types::image::{ImagePosition, ImagePositions, ImageStretches, RectU16};
use mln_style_types::{SymbolPlacementType, TextRotationAlignmentType};
use mln_text::PositionedIcon;
use mln_text::glyph::{Shaping, WritingModeType};
use mln_util::constants::ONE_EM;
use crate::layout::symbol_instance::SymbolContent;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct SymbolQuad {
pub(crate) tl: Vec2,
pub(crate) tr: Vec2,
pub(crate) bl: Vec2,
pub(crate) br: Vec2,
pub(crate) tex: RectU16,
pub(crate) pixel_offset_tl: Vec2,
pub(crate) pixel_offset_br: Vec2,
pub(crate) glyph_offset: Vec2,
pub(crate) writing_mode: WritingModeType,
pub(crate) is_sdf: bool,
pub(crate) section_index: usize,
pub(crate) min_font_scale: Vec2,
}
impl SymbolQuad {
#[expect(clippy::too_many_arguments, reason = "each argument is a field of the quad")]
#[must_use]
pub(crate) fn new(
tl: Vec2,
tr: Vec2,
bl: Vec2,
br: Vec2,
tex: RectU16,
writing_mode: WritingModeType,
glyph_offset: Vec2,
is_sdf: bool,
pixel_offset_tl: Vec2,
pixel_offset_br: Vec2,
min_font_scale: Vec2,
section_index: usize,
) -> Self {
SymbolQuad {
tl,
tr,
bl,
br,
tex,
pixel_offset_tl,
pixel_offset_br,
glyph_offset,
writing_mode,
is_sdf,
section_index,
min_font_scale,
}
}
}
pub(crate) type SymbolQuads = Vec<SymbolQuad>;
const BORDER: u16 = ImagePosition::PADDING;
fn matrix_multiply(m: &[f32; 4], p: Vec2) -> Vec2 {
Vec2::new(m[0] * p.x + m[1] * p.y, m[2] * p.x + m[3] * p.y)
}
#[must_use]
pub(crate) fn compute_stretch_sum(stretches: &ImageStretches) -> f32 {
let mut sum: f32 = 0.0;
for stretch in stretches {
sum += stretch.1 - stretch.0;
}
sum
}
#[must_use]
pub(crate) fn sum_within_range(stretches: &ImageStretches, min: f32, max: f32) -> f32 {
let mut sum: f32 = 0.0;
for stretch in stretches {
sum += fmaxf(min, fminf(max, stretch.1)) - fmaxf(min, fminf(max, stretch.0));
}
sum
}
#[inline]
#[must_use]
pub(crate) fn get_em_offset(stretch_offset: f32, stretch_size: f32, icon_size: f32, icon_offset: f32) -> f32 {
icon_offset + icon_size * stretch_offset / stretch_size
}
#[inline]
#[must_use]
pub(crate) fn get_px_offset(fixed_offset: f32, fixed_size: f32, stretch_offset: f32, stretch_size: f32) -> f32 {
fixed_offset - fixed_size * stretch_offset / stretch_size
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Cut {
pub(crate) fixed: f32,
pub(crate) stretch: f32,
}
pub(crate) type Cuts = Vec<Cut>;
#[must_use]
pub(crate) fn stretch_zones_to_cuts(stretch_zones: &ImageStretches, fixed_size: f32, stretch_size: f32) -> Cuts {
let mut cuts: Cuts = vec![Cut { fixed: -i32::from(BORDER) as f32, stretch: 0.0 }];
let mut last_stretch = 0.0;
for zone in stretch_zones {
let c1 = zone.0;
let c2 = zone.1;
cuts.push(Cut { fixed: c1 - last_stretch, stretch: last_stretch });
cuts.push(Cut { fixed: c1 - last_stretch, stretch: last_stretch + (c2 - c1) });
last_stretch += c2 - c1;
}
cuts.push(Cut { fixed: fixed_size + f32::from(BORDER), stretch: stretch_size });
cuts
}
#[must_use]
pub(crate) fn get_icon_quads(
shaped_icon: &PositionedIcon,
icon_rotate: f32,
icon_type: SymbolContent,
has_icon_text_fit: bool,
) -> SymbolQuads {
let mut quads = SymbolQuads::new();
let image: &ImagePosition = shaped_icon.image();
let pixel_ratio = image.pixel_ratio;
let image_width = (i32::from(image.padded_rect.w) - 2 * i32::from(BORDER)) as u16;
let image_height = (i32::from(image.padded_rect.h) - 2 * i32::from(BORDER)) as u16;
let stretch_x_full: ImageStretches = vec![(0.0, f32::from(image_width))];
let stretch_y_full: ImageStretches = vec![(0.0, f32::from(image_height))];
let stretch_x: &ImageStretches = if image.stretch_x.is_empty() { &stretch_x_full } else { &image.stretch_x };
let stretch_y: &ImageStretches = if image.stretch_y.is_empty() { &stretch_y_full } else { &image.stretch_y };
let stretch_width = compute_stretch_sum(stretch_x);
let stretch_height = compute_stretch_sum(stretch_y);
let fixed_width = f32::from(image_width) - stretch_width;
let fixed_height = f32::from(image_height) - stretch_height;
let mut stretch_offset_x: f32 = 0.0;
let mut stretch_content_width = stretch_width;
let mut stretch_offset_y: f32 = 0.0;
let mut stretch_content_height = stretch_height;
let mut fixed_offset_x: f32 = 0.0;
let mut fixed_content_width = fixed_width;
let mut fixed_offset_y: f32 = 0.0;
let mut fixed_content_height = fixed_height;
let mut icon = shaped_icon.clone();
if has_icon_text_fit && let Some(content) = &image.content {
let content_width = content.right - content.left;
let content_height = content.bottom - content.top;
if image.text_fit_width.is_some() || image.text_fit_height.is_some() {
icon = icon.apply_text_fit();
}
stretch_offset_x = sum_within_range(stretch_x, 0.0, content.left);
stretch_offset_y = sum_within_range(stretch_y, 0.0, content.top);
stretch_content_width = sum_within_range(stretch_x, content.left, content.right);
stretch_content_height = sum_within_range(stretch_y, content.top, content.bottom);
fixed_offset_x = content.left - stretch_offset_x;
fixed_offset_y = content.top - stretch_offset_y;
fixed_content_width = content_width - stretch_content_width;
fixed_content_height = content_height - stretch_content_height;
}
let mut matrix: Option<[f32; 4]> = None;
if icon_rotate != 0.0 {
let angle = deg2radf(icon_rotate);
let angle_sin = angle.sin();
let angle_cos = angle.cos();
matrix = Some([angle_cos, -angle_sin, angle_sin, angle_cos]);
}
let icon_left = icon.left();
let icon_top = icon.top();
let icon_width = icon.right() - icon_left;
let icon_height = icon.bottom() - icon_top;
let mut make_box = |left: Cut, top: Cut, right: Cut, bottom: Cut| {
let left_em = get_em_offset(left.stretch - stretch_offset_x, stretch_content_width, icon_width, icon_left);
let left_px = get_px_offset(left.fixed - fixed_offset_x, fixed_content_width, left.stretch, stretch_width);
let top_em = get_em_offset(top.stretch - stretch_offset_y, stretch_content_height, icon_height, icon_top);
let top_px = get_px_offset(top.fixed - fixed_offset_y, fixed_content_height, top.stretch, stretch_height);
let right_em = get_em_offset(right.stretch - stretch_offset_x, stretch_content_width, icon_width, icon_left);
let right_px = get_px_offset(right.fixed - fixed_offset_x, fixed_content_width, right.stretch, stretch_width);
let bottom_em = get_em_offset(bottom.stretch - stretch_offset_y, stretch_content_height, icon_height, icon_top);
let bottom_px =
get_px_offset(bottom.fixed - fixed_offset_y, fixed_content_height, bottom.stretch, stretch_height);
let mut tl = Vec2::new(left_em, top_em);
let mut tr = Vec2::new(right_em, top_em);
let mut br = Vec2::new(right_em, bottom_em);
let mut bl = Vec2::new(left_em, bottom_em);
let pixel_offset_tl = Vec2::new(left_px / pixel_ratio, top_px / pixel_ratio);
let pixel_offset_br = Vec2::new(right_px / pixel_ratio, bottom_px / pixel_ratio);
if let Some(matrix) = &matrix {
tl = matrix_multiply(matrix, tl);
tr = matrix_multiply(matrix, tr);
bl = matrix_multiply(matrix, bl);
br = matrix_multiply(matrix, br);
}
let x1 = left.stretch + left.fixed;
let x2 = right.stretch + right.fixed;
let y1 = top.stretch + top.fixed;
let y2 = bottom.stretch + bottom.fixed;
let sub_rect = RectU16 {
x: ((i32::from(image.padded_rect.x) + i32::from(BORDER)) as f32 + x1) as u16,
y: ((i32::from(image.padded_rect.y) + i32::from(BORDER)) as f32 + y1) as u16,
w: (x2 - x1) as u16,
h: (y2 - y1) as u16,
};
let min_font_scale_x = fixed_content_width / pixel_ratio / icon_width;
let min_font_scale_y = fixed_content_height / pixel_ratio / icon_height;
quads.push(SymbolQuad::new(
tl,
tr,
bl,
br,
sub_rect,
WritingModeType::None,
Vec2::ZERO,
icon_type == SymbolContent::IconSDF,
pixel_offset_tl,
pixel_offset_br,
Vec2::new(min_font_scale_x, min_font_scale_y),
0,
));
};
if !has_icon_text_fit || (image.stretch_x.is_empty() && image.stretch_y.is_empty()) {
make_box(
Cut { fixed: 0.0, stretch: -1.0 },
Cut { fixed: 0.0, stretch: -1.0 },
Cut { fixed: 0.0, stretch: (i32::from(image_width) + 1) as f32 },
Cut { fixed: 0.0, stretch: (i32::from(image_height) + 1) as f32 },
);
} else {
let x_cuts = stretch_zones_to_cuts(stretch_x, fixed_width, stretch_width);
let y_cuts = stretch_zones_to_cuts(stretch_y, fixed_height, stretch_height);
for xi in 0..x_cuts.len() - 1 {
let x1 = x_cuts[xi];
let x2 = x_cuts[xi + 1];
for yi in 0..y_cuts.len() - 1 {
let y1 = y_cuts[yi];
let y2 = y_cuts[yi + 1];
make_box(x1, y1, x2, y2);
}
}
}
quads
}
#[must_use]
pub(crate) fn get_glyph_quads(
shaped_text: &Shaping,
text_offset: [f32; 2],
layout: &SymbolLayoutProperties::Values,
placement: SymbolPlacementType,
icons: &ImagePositions,
allow_vertical_placement: bool,
) -> SymbolQuads {
let text_rotate = deg2radf(layout.text_rotate);
let along_line = layout.text_rotation_alignment != TextRotationAlignmentType::Viewport
&& placement != SymbolPlacementType::Point;
let mut quads = SymbolQuads::new();
for line in &shaped_text.positioned_lines {
for positioned_glyph in &line.positioned_glyphs {
if !(positioned_glyph.rect.w != 0 && positioned_glyph.rect.h != 0) {
continue;
}
let glyph_padding: f32 = 1.0;
let mut rect_buffer: f32 = 3.0 + glyph_padding;
let mut pixel_ratio: f32 = 1.0;
let mut line_offset: f32 = 0.0;
let rotate_vertical_glyph = (along_line || allow_vertical_placement) && positioned_glyph.vertical;
let half_advance = positioned_glyph.metrics.advance as f32 * positioned_glyph.scale / 2.0;
let rect = &positioned_glyph.rect;
let mut is_sdf = true;
if allow_vertical_placement && shaped_text.verticalizable {
let scaled_glyph_offset = (positioned_glyph.scale - 1.0) * ONE_EM;
let image_offset = (ONE_EM - positioned_glyph.metrics.width as f32 * positioned_glyph.scale) / 2.0;
line_offset = line.line_offset / 2.0
- (if positioned_glyph.image_id.is_some() { -image_offset } else { scaled_glyph_offset });
}
if let Some(image_id) = &positioned_glyph.image_id {
let Some(image) = icons.get(image_id) else {
continue;
};
pixel_ratio = image.pixel_ratio;
rect_buffer = f32::from(ImagePosition::PADDING) / pixel_ratio;
is_sdf = image.sdf;
}
let glyph_offset =
if along_line { Vec2::new(positioned_glyph.x + half_advance, positioned_glyph.y) } else { Vec2::ZERO };
let mut built_in_offset = if along_line {
Vec2::ZERO
} else {
Vec2::new(
positioned_glyph.x + half_advance + text_offset[0],
positioned_glyph.y + text_offset[1] - line_offset,
)
};
let mut verticalized_label_offset = Vec2::ZERO;
if rotate_vertical_glyph {
verticalized_label_offset = built_in_offset;
built_in_offset = Vec2::ZERO;
}
let texture_scale: f32 = if positioned_glyph.metrics.is_double_resolution { 2.0 } else { 1.0 };
let x1 = (positioned_glyph.metrics.left - rect_buffer) * positioned_glyph.scale - half_advance
+ built_in_offset.x;
let y1 = (-positioned_glyph.metrics.top - rect_buffer) * positioned_glyph.scale + built_in_offset.y;
let x2 = x1 + f32::from(rect.w) / texture_scale * positioned_glyph.scale / pixel_ratio;
let y2 = y1 + f32::from(rect.h) / texture_scale * positioned_glyph.scale / pixel_ratio;
let mut tl = Vec2::new(x1, y1);
let mut tr = Vec2::new(x2, y1);
let mut bl = Vec2::new(x1, y2);
let mut br = Vec2::new(x2, y2);
if rotate_vertical_glyph {
let center = Vec2::new(-half_advance, half_advance - Shaping::Y_OFFSET as f32);
let vertical_rotation = -std::f32::consts::PI / 2.0;
let x_half_widht_offsetcorrection = ONE_EM / 2.0 - half_advance;
let y_image_offset_correction =
if positioned_glyph.image_id.is_some() { x_half_widht_offsetcorrection } else { 0.0 };
let x_offset_correction = Vec2::new(
5.0 - Shaping::Y_OFFSET as f32 - x_half_widht_offsetcorrection,
-y_image_offset_correction,
);
let transform = |p: Vec2| {
Vec2::from_angle(vertical_rotation).rotate(p - center)
+ center
+ x_offset_correction
+ verticalized_label_offset
};
tl = transform(tl);
tr = transform(tr);
bl = transform(bl);
br = transform(br);
}
if text_rotate != 0.0 {
let angle_sin = text_rotate.sin();
let angle_cos = text_rotate.cos();
let matrix: [f32; 4] = [angle_cos, -angle_sin, angle_sin, angle_cos];
tl = matrix_multiply(&matrix, tl);
tr = matrix_multiply(&matrix, tr);
bl = matrix_multiply(&matrix, bl);
br = matrix_multiply(&matrix, br);
}
let pixel_offset_tl = Vec2::ZERO;
let pixel_offset_br = Vec2::ZERO;
let min_font_scale = Vec2::ZERO;
quads.push(SymbolQuad::new(
tl,
tr,
bl,
br,
*rect,
shaped_text.writing_mode,
glyph_offset,
is_sdf,
pixel_offset_tl,
pixel_offset_br,
min_font_scale,
positioned_glyph.section_index,
));
}
}
quads
}
#[cfg(test)]
mod tests {
use mln_style_types::image::Impl as ImageImpl;
use mln_style_types::{IconTextFitType, SymbolAnchorType};
use mln_text::glyph::{GlyphID, GlyphMetrics, PositionedGlyph, PositionedLine};
use mln_tile_id::geometry_tile_data::GeometryCoordinates;
use mln_util::image::PremultipliedImage;
use mln_util::size::Size;
use super::*;
use crate::geometry::anchor::Anchor;
fn assert_float_eq(actual: f32, expected: f32) {
fn biased(f: f32) -> u32 {
let bits = f.to_bits();
if bits & 0x8000_0000 != 0 { !bits + 1 } else { bits | 0x8000_0000 }
}
let (a, b) = (biased(actual), biased(expected));
let distance = a.abs_diff(b);
assert!(distance <= 4, "expected {expected}, got {actual}");
}
fn test_image_impl() -> ImageImpl {
ImageImpl::new(
"test".to_owned(),
PremultipliedImage::new(Size { width: 1, height: 1 }),
1.0,
false,
Vec::new(),
Vec::new(),
None,
None,
None,
)
.expect("valid image")
}
fn rect(x: u16, y: u16, w: u16, h: u16) -> RectU16 {
RectU16 { x, y, w, h }
}
fn positioned_glyph(code: u32, rect: RectU16, metrics: GlyphMetrics) -> PositionedGlyph {
PositionedGlyph::new(GlyphID::new(code), 0.0, 0.0, false, 0, 1.0, rect, metrics, None, 0)
}
#[test]
fn get_glyph_quads_double_resolution_glyph_preserves_logical_quad_size() {
let metrics1x =
GlyphMetrics { width: 24, height: 24, left: 0.0, top: -8.0, advance: 24, is_double_resolution: false };
let mut metrics2x = metrics1x;
metrics2x.left += 0.5;
metrics2x.top -= 0.5;
metrics2x.is_double_resolution = true;
let mut shaped_text1x = Shaping::default();
shaped_text1x.positioned_lines.push(PositionedLine::default());
shaped_text1x.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
'A' as u32,
rect(0, 0, 32, 32),
metrics1x,
));
let mut shaped_text2x = Shaping::default();
shaped_text2x.positioned_lines.push(PositionedLine::default());
shaped_text2x.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
'A' as u32,
rect(0, 0, 62, 62),
metrics2x,
));
let layout = SymbolLayoutProperties::Values::default();
let image_map = ImagePositions::default();
let quads1x =
get_glyph_quads(&shaped_text1x, [0.0, 0.0], &layout, SymbolPlacementType::Point, &image_map, false);
let quads2x =
get_glyph_quads(&shaped_text2x, [0.0, 0.0], &layout, SymbolPlacementType::Point, &image_map, false);
assert_eq!(quads1x.len(), 1);
assert_eq!(quads2x.len(), 1);
assert_float_eq(quads1x[0].tl.x + 0.5, quads2x[0].tl.x);
assert_float_eq(quads1x[0].tl.y + 0.5, quads2x[0].tl.y);
assert_float_eq(quads1x[0].br.x - 0.5, quads2x[0].br.x);
assert_float_eq(quads1x[0].br.y - 0.5, quads2x[0].br.y);
assert_float_eq(quads1x[0].br.x - quads1x[0].tl.x, 32.0);
assert_float_eq(quads2x[0].br.x - quads2x[0].tl.x, 31.0);
assert_float_eq(quads1x[0].br.y - quads1x[0].tl.y, 32.0);
assert_float_eq(quads2x[0].br.y - quads2x[0].tl.y, 31.0);
}
#[test]
fn get_icon_quads_normal() {
let _layout = SymbolLayoutProperties::Values::default();
let _anchor = Anchor::new(2.0, 3.0, 0.0, Some(0));
let image = ImagePosition::new(rect(0, 0, 15, 11), &test_image_impl());
let shaped_icon = PositionedIcon::shape_icon(&image, &[-6.5, -4.5], SymbolAnchorType::Center);
let _line = GeometryCoordinates::new();
let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);
assert_eq!(quads.len(), 1);
let quad = &quads[0];
assert_float_eq(quad.tl.x, -14.0);
assert_float_eq(quad.tl.y, -10.0);
assert_float_eq(quad.tr.x, 1.0);
assert_float_eq(quad.tr.y, -10.0);
assert_float_eq(quad.bl.x, -14.0);
assert_float_eq(quad.bl.y, 1.0);
assert_float_eq(quad.br.x, 1.0);
assert_float_eq(quad.br.y, 1.0);
}
fn assert_quad(quad: &SymbolQuad, left: f32, top: f32, right: f32, bottom: f32) {
assert_float_eq(quad.tl.x, left);
assert_float_eq(quad.tl.y, top);
assert_float_eq(quad.tr.x, right);
assert_float_eq(quad.tr.y, top);
assert_float_eq(quad.bl.x, left);
assert_float_eq(quad.bl.y, bottom);
assert_float_eq(quad.br.x, right);
assert_float_eq(quad.br.y, bottom);
}
#[test]
fn get_icon_quads_style() {
let _anchor = Anchor::new(0.0, 0.0, 0.0, Some(0));
let image = ImagePosition::new(rect(0, 0, 20, 20), &test_image_impl());
let _line = GeometryCoordinates::new();
let mut shaped_text = Shaping { top: -10.0, bottom: 30.0, left: -60.0, right: 20.0, ..Default::default() };
shaped_text.positioned_lines.push(PositionedLine::default());
shaped_text.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
32,
RectU16::default(),
GlyphMetrics::default(),
));
let fit = |text_fit: IconTextFitType, padding: [f32; 4], font_scale: f32| {
let mut shaped_icon = PositionedIcon::shape_icon(&image, &[-9.5, -9.5], SymbolAnchorType::Center);
shaped_icon.fit_icon_to_text(&shaped_text, text_fit, &padding, &[0.0, 0.0], font_scale);
let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);
assert_eq!(quads.len(), 1);
quads[0].clone()
};
{
let shaped_icon = PositionedIcon::shape_icon(&image, &[-9.5, -9.5], SymbolAnchorType::Center);
assert_float_eq(-18.5, shaped_icon.top());
assert_float_eq(-0.5, shaped_icon.right());
assert_float_eq(-0.5, shaped_icon.bottom());
assert_float_eq(-18.5, shaped_icon.left());
let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);
assert_eq!(quads.len(), 1);
assert_quad(&quads[0], -19.5, -19.5, 0.5, 0.5);
}
assert_quad(&fit(IconTextFitType::Width, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0), -64.444_44, 0.0, 24.444_443, 20.0);
assert_quad(
&fit(IconTextFitType::Width, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
-32.222_22,
-5.0,
12.222_221,
15.0,
);
assert_quad(
&fit(IconTextFitType::Width, [5.0, 10.0, 5.0, 10.0], 12.0 / 24.0),
-43.333_332,
-5.0,
23.333_332,
15.0,
);
assert_quad(
&fit(IconTextFitType::Height, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0),
-30.0,
-12.222_221,
-10.0,
32.222_22,
);
assert_quad(
&fit(IconTextFitType::Height, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
-20.0,
-6.111_110_7,
0.0,
16.111_11,
);
assert_quad(
&fit(IconTextFitType::Height, [5.0, 10.0, 5.0, 20.0], 12.0 / 24.0),
-20.0,
-11.666_666,
0.0,
21.666_666,
);
assert_quad(
&fit(IconTextFitType::Both, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0),
-64.444_44,
-12.222_221,
24.444_443,
32.222_22,
);
assert_quad(
&fit(IconTextFitType::Both, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
-32.222_22,
-6.111_110_7,
12.222_221,
16.111_11,
);
assert_quad(
&fit(IconTextFitType::Both, [5.0, 10.0, 5.0, 10.0], 12.0 / 24.0),
-43.333_332,
-11.666_666,
23.333_332,
21.666_666,
);
assert_quad(
&fit(IconTextFitType::Both, [0.0, 5.0, 10.0, 15.0], 12.0 / 24.0),
-48.333_332,
-6.666_666,
18.333_332,
26.666_666,
);
}
}