Repository: GraphiteEditor/Graphite
File: node-graph/libraries/rendering/src/renderer.rs (Rust)

Code before:

	fn new_ids_from_hash(&mut self, reference: Option<NodeId>) {
		for row in self.iter_mut() {
			row.element.vector_new_ids_from_hash(reference.map(|id| id.0).unwrap_or_default());
		}
	}
}

impl Render for Table<Raster<CPU>> {
	fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
		for row in self.iter() {
			let image = row.element;

			let transform = *row.transform;

			if image.data.is_empty() {
				continue;
			}

			if render_params.to_canvas() {
				let mut image_copy = image.clone();
				image_copy.data_mut().map_pixels(|p| p.to_unassociated_alpha());
				let id = match render.image_data.entry(image_copy.into_data()) {
					std::collections::hash_map::Entry::Occupied(occupied_entry) => *occupied_entry.get(),
					std::collections::hash_map::Entry::Vacant(vacant_entry) => *vacant_entry.insert(generate_uuid()),
				};

				render.parent_tag(
					"foreignObject",
					|attributes| {
						let mut transform_values = transform.to_scale_angle_translation();
						let size = DVec2::new(image.width as f64, image.height as f64);
						transform_values.0 /= size;

						let matrix = DAffine2::from_scale_angle_translation(transform_values.0, transform_values.1, transform_values.2);
						let matrix = format_transform_matrix(matrix);
						if !matrix.is_empty() {
							attributes.push("transform", matrix);
						}

						attributes.push("width", size.x.to_string());
						attributes.push("height", size.y.to_string());

						let opacity = row.alpha_blending.opacity(render_params.for_mask);
						if opacity < 1. {
							attributes.push("opacity", opacity.to_string());
						}

						if row.alpha_blending.blend_mode != BlendMode::default() {
							attributes.push("style", row.alpha_blending.blend_mode.render());
						}

Review comment (suggestion):
This is just [`or_insert_with`](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.or_insert_with) I guess.

Code after:

	fn new_ids_from_hash(&mut self, reference: Option<NodeId>) {
		for row in self.iter_mut() {
			row.element.vector_new_ids_from_hash(reference.map(|id| id.0).unwrap_or_default());
		}
	}
}

impl Render for Table<Raster<CPU>> {
	fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
		for row in self.iter() {
			let image = row.element;

			let transform = *row.transform;

			if image.data.is_empty() {
				continue;
			}

			if render_params.to_canvas() {
				let mut image_copy = image.clone();
				image_copy.data_mut().map_pixels(|p| p.to_unassociated_alpha());
				let id = *render.image_data.entry(image_copy.into_data()).or_insert_with(generate_uuid);

				render.parent_tag(
					"foreignObject",
					|attributes| {
						let mut transform_values = transform.to_scale_angle_translation();
						let size = DVec2::new(image.width as f64, image.height as f64);
						transform_values.0 /= size;

						let matrix = DAffine2::from_scale_angle_translation(transform_values.0, transform_values.1, transform_values.2);
						let matrix = format_transform_matrix(matrix);
						if !matrix.is_empty() {
							attributes.push("transform", matrix);
						}

						attributes.push("width", size.x.to_string());
						attributes.push("height", size.y.to_string());

						let opacity = row.alpha_blending.opacity(render_params.for_mask);
						if opacity < 1. {
							attributes.push("opacity", opacity.to_string());
						}

						if row.alpha_blending.blend_mode != BlendMode::default() {
							attributes.push("style", row.alpha_blending.blend_mode.render());
						}
					},
					|render| {
						render.leaf_tag(