Skip to content

Commit

Permalink
Move code to tessellation_new.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxammann committed Nov 11, 2024
1 parent d6ccbc3 commit bb2e07b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 231 deletions.
1 change: 1 addition & 0 deletions .idea/runConfigurations/Run_demo__debug_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion maplibre/src/legacy/layout/symbol_feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl SymbolGeometryTileFeature {
FeatureType::Point
}
/// maplibre/maplibre-native#4add9ea original name: getValue
pub fn get_value(&self, key: &String) -> Option<&Value> {
pub fn get_value(&self, key: &str) -> Option<&Value> {
todo!()
}
/// maplibre/maplibre-native#4add9ea original name: getProperties
Expand Down
2 changes: 0 additions & 2 deletions maplibre/src/legacy/shaping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,6 @@ fn shape_lines(
rect = glyph_position.rect;
metrics = glyph_position.metrics;
} else {
// TODO why would a glyph position not be available but a glyph? Maybe if a glyph bitmap is empty?
unreachable!();
let glyphs = glyph_map.get(&section.font_stack_hash);
if glyphs.is_none() {
continue;
Expand Down
5 changes: 4 additions & 1 deletion maplibre/src/legacy/style_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ pub mod expression {
// TODO remove
Formatted {
sections: vec![FormattedSection {
text: "AllerAnfangistschwer".to_string(),
text: "Aller Anfang ist schwer".to_string(),
image: None,
font_scale: None,
font_stack: None,
Expand Down Expand Up @@ -1147,6 +1147,9 @@ impl SymbolLayoutProperties_PossiblyEvaluated {
p2: CanonicalTileID,
) -> T::Type {
//todo!()

//p1.get_value(&T::name());

T::default_value()
}

Expand Down
4 changes: 2 additions & 2 deletions maplibre/src/sdf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ mod resource;
mod resource_system;
mod upload_system;

pub mod tessellation;
mod tessellation_new;
pub mod text;
pub mod tessellation;
pub mod tessellation_new;

struct SymbolPipeline(wgpu::RenderPipeline);

Expand Down
172 changes: 115 additions & 57 deletions maplibre/src/sdf/tessellation_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use geozero::{
};
use lyon::{
geom::euclid::{Box2D, Point2D},
tessellation::{geometry_builder::MaxIndex, VertexBuffers},
tessellation::{VertexBuffers},
};

use crate::{
Expand All @@ -32,15 +32,19 @@ use crate::{
render::shaders::ShaderSymbolVertexNew,
sdf::Feature,
};
use crate::legacy::buckets::symbol_bucket::SymbolBucketBuffer;
use crate::legacy::tagged_string::SectionOptions;
use crate::sdf::tessellation::IndexDataType;
use crate::sdf::text::GlyphSet;

type GeoResult<T> = geozero::error::Result<T>;

/// Build tessellations with vectors.
pub struct TextTessellatorNew<I> {
pub struct TextTessellatorNew {
geo_writer: GeoWriter,

// output
pub quad_buffer: VertexBuffers<ShaderSymbolVertexNew, I>,
pub quad_buffer: VertexBuffers<ShaderSymbolVertexNew, IndexDataType>,
pub features: Vec<Feature>,

// iteration variables
Expand All @@ -49,82 +53,119 @@ pub struct TextTessellatorNew<I> {
current_origin: Option<Box2D<f32, TileSpace>>,
}

impl<I> TextTessellatorNew<I> {
pub fn finish() {
impl TextTessellatorNew {
pub fn finish(&mut self) {
let data = include_bytes!("../../../data/0-255.pbf");
let glyphs = GlyphSet::try_from(data.as_slice()).unwrap();

let font_stack = vec![
"Open Sans Regular".to_string(),
"Arial Unicode MS Regular".to_string(),
];

// load glyph/image data

let image_positions = ImagePositions::new();

let glyph_position = GlyphPosition {
rect: Rect::new(Point2D::new(0, 0), Size2D::new(10, 10)),
metrics: GlyphMetrics {
width: 18,
height: 18,
left: 2,
top: -8,
advance: 21,
},
};
let glyph_positions: GlyphPositions = GlyphPositions::from([(
FontStackHasher::new(&font_stack),
GlyphPositionMap::from([('中' as Char16, glyph_position)]),
)]);
let layer_name = "layer".to_string();

let mut glyph = Glyph::default();
glyph.id = '中' as Char16;
glyph.metrics = glyph_position.metrics;

let glyphs: GlyphMap = GlyphMap::from([(
FontStackHasher::new(&font_stack),
Glyphs::from([('中' as Char16, Some(glyph))]),
)]);

let empty_image_map = ImageMap::new();

// layouting
let section_options = SectionOptions::new(1.0, font_stack.clone(), None);

let mut glyph_dependencies = GlyphDependencies::new();

let tile_id = OverscaledTileID {
canonical: CanonicalTileID { x: 0, y: 0, z: 0 },
overscaled_z: 0,
};
let parameters = BucketParameters {
let mut parameters = BucketParameters {
tile_id: tile_id,
mode: MapMode::Continuous,
pixel_ratio: 1.0,
layer_type: LayerTypeInfo,
};
let mut layout = SymbolLayout::new(
&parameters,
&vec![LayerProperties {
id: "layer".to_string(),
layer: SymbolLayer {
layout: SymbolLayoutProperties_Unevaluated,
let layer_data = SymbolGeometryTileLayer {
name: layer_name.clone(),
features: vec![SymbolGeometryTileFeature::new(Box::new(
VectorGeometryTileFeature {
geometry: vec![GeometryCoordinates(vec![Point2D::new(
512, 512,
)])],
},
}],
Box::new(SymbolGeometryTileLayer {
name: "layer".to_string(),
features: vec![SymbolGeometryTileFeature::new(Box::new(
VectorGeometryTileFeature {
geometry: vec![GeometryCoordinates(vec![Point2D::new(1024, 1024)])],
))],
};
let layer_properties = vec![LayerProperties {
id: layer_name.clone(),
layer: SymbolLayer {
layout: SymbolLayoutProperties_Unevaluated,
},
}];

let image_positions = ImagePositions::new();

let glyph_map = GlyphPositionMap::from_iter(glyphs.glyphs.iter().map(
|(unicode_point, glyph)| {
(
*unicode_point as Char16,
GlyphPosition {
rect: Rect::new(
Point2D::new(
glyph.tex_origin_x as u16 + 3,
glyph.tex_origin_y as u16 + 3,
),
Size2D::new(
glyph.buffered_dimensions().0 as u16,
glyph.buffered_dimensions().1 as u16,
),
), // FIXME: verify if this mapping is correct
metrics: GlyphMetrics {
width: glyph.width,
height: glyph.height,
left: glyph.left_bearing,
top: glyph.top_bearing,
advance: glyph.h_advance,
},
},
))],
}),
)
},
));

let glyph_positions: GlyphPositions =
GlyphPositions::from([(FontStackHasher::new(&font_stack), glyph_map)]);

let glyphs: GlyphMap = GlyphMap::from([(
FontStackHasher::new(&font_stack),
Glyphs::from_iter(glyphs.glyphs.iter().map(
|(unicode_point, glyph)| {
(
*unicode_point as Char16,
Some(Glyph {
id: *unicode_point as Char16,
bitmap: Default::default(),
metrics: GlyphMetrics {
width: glyph.width,
height: glyph.height,
left: glyph.left_bearing,
top: glyph.top_bearing,
advance: glyph.h_advance,
},
}),
)
},
)),
)]);

let mut layout = SymbolLayout::new(
&parameters,
&layer_properties,
Box::new(layer_data),
&mut LayoutParameters {
bucket_parameters: &mut parameters.clone(),
glyph_dependencies: &mut glyph_dependencies,
image_dependencies: &mut Default::default(),
available_images: &mut Default::default(),
},
)
.unwrap();
.unwrap();

assert_eq!(glyph_dependencies.len(), 1);

let empty_image_map = ImageMap::new();
layout.prepare_symbols(
&glyphs,
&glyph_positions,
Expand All @@ -141,10 +182,27 @@ impl<I> TextTessellatorNew<I> {
false,
&tile_id.canonical,
);

let new_buffer = output.remove(&layer_name).unwrap();

let mut buffer = VertexBuffers::new();
let text_buffer = new_buffer.bucket.text;
let SymbolBucketBuffer {
shared_vertices,
triangles,
..
} = text_buffer;
buffer.vertices = shared_vertices
.iter()
.map(|v| ShaderSymbolVertexNew::new(v))
.collect();
buffer.indices = triangles.indices.iter().map(|i| *i as u32).collect();

self.quad_buffer = buffer;
}
}

impl<I> Default for TextTessellatorNew<I> {
impl Default for TextTessellatorNew {
fn default() -> Self {
Self {
geo_writer: Default::default(),
Expand All @@ -157,7 +215,7 @@ impl<I> Default for TextTessellatorNew<I> {
}
}

impl<I> GeomProcessor for TextTessellatorNew<I> {
impl GeomProcessor for TextTessellatorNew {
fn xy(&mut self, x: f64, y: f64, idx: usize) -> GeoResult<()> {
self.geo_writer.xy(x, y, idx)
}
Expand Down Expand Up @@ -196,8 +254,8 @@ impl<I> GeomProcessor for TextTessellatorNew<I> {
}
}

impl<I: std::ops::Add + From<lyon::tessellation::VertexId> + MaxIndex> PropertyProcessor
for TextTessellatorNew<I>
impl PropertyProcessor
for TextTessellatorNew
{
fn property(
&mut self,
Expand All @@ -218,8 +276,8 @@ impl<I: std::ops::Add + From<lyon::tessellation::VertexId> + MaxIndex> PropertyP
}
}

impl<I: std::ops::Add + From<lyon::tessellation::VertexId> + MaxIndex> FeatureProcessor
for TextTessellatorNew<I>
impl FeatureProcessor
for TextTessellatorNew
{
fn feature_end(&mut self, _idx: u64) -> geozero::error::Result<()> {
let geometry = self.geo_writer.take_geometry();
Expand Down
Loading

0 comments on commit bb2e07b

Please sign in to comment.