mod clipping;
mod device_context;
mod node;
mod saved_device_context;
mod source_contract;
mod source_rop_pair;
mod ternary_raster_operator;
mod text_advances;
mod util;
use crate::wmf::{
converter::{
svg::{
device_context::DeviceContext,
node::{share_repeated_bitmaps, Data, Node},
ternary_raster_operator::{BrushOnlyRopSequence, TernaryRasterOperator},
util::{as_point_string, url_string, Fill, Stroke},
},
GraphicsObject, PlayError, SelectedGraphicsObject,
},
imports::*,
parser::*,
};
#[derive(Default)]
pub struct SVGPlayer {
context_stack: Vec<saved_device_context::SavedDeviceContext>,
context_current: DeviceContext,
definitions: Vec<Node>,
brush_only_rop_sequence: BrushOnlyRopSequence,
source_rop_pair: Option<source_rop_pair::PendingSourcePair>,
elements: Vec<Node>,
object_selected: SelectedGraphicsObject,
current_clip_id: Option<String>,
clip_bounds: BTreeMap<String, clipping::DeviceClipBounds>,
source_contract: Option<source_contract::SourceContract>,
}
impl SVGPlayer {
pub fn new() -> Self {
Self::default()
}
pub fn with_source_contract(mut self) -> Self {
self.source_contract = Some(Default::default());
self
}
#[inline]
fn issue_definition_id(&self) -> String {
format!("defs{}", self.definitions.len())
}
#[inline]
fn push_element(&mut self, record_number: usize, mut element: Node) {
if let Some(contract) = &mut self.source_contract {
element = contract.element(element, &self.context_current, &self.object_selected.font);
}
if record_number > 0 {
element = element.set("id", format!("elem{record_number}"));
}
if let Some(ref clip_id) = self.current_clip_id {
element = element.set("clip-path", format!("url(#{clip_id})"));
}
self.elements.push(element);
}
fn selected_brush(&self) -> &Brush {
&self.object_selected.brush
}
fn selected_pen(&self) -> &Pen {
&self.object_selected.pen
}
}
impl crate::wmf::converter::Player for SVGPlayer {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn generate(self) -> Result<Vec<u8>, PlayError> {
let Self {
context_current,
definitions,
brush_only_rop_sequence,
elements,
clip_bounds,
source_contract,
..
} = self;
let (definitions, elements) = brush_only_rop_sequence.finish(definitions, elements);
let (raw_vb_x, raw_vb_y, mut vb_w, mut vb_h) = context_current.window.as_view_box();
let vb_x = i32::from(raw_vb_x);
let vb_y = i32::from(raw_vb_y);
let mut vb_w_i32 = i32::from(vb_w);
let mut vb_h_i32 = i32::from(vb_h);
let mut vb_right = vb_x.saturating_add(vb_w_i32);
let mut vb_bottom = vb_y.saturating_add(vb_h_i32);
for elem in elements.iter() {
let (max_x, max_y) = clipping::visible_element_maxima(elem, &clip_bounds);
if let Some(mx) = max_x {
if mx > vb_right {
vb_right = mx;
}
}
if let Some(my) = max_y {
if my > vb_bottom {
vb_bottom = my;
}
}
}
vb_w_i32 = vb_right.saturating_sub(vb_x);
vb_h_i32 = vb_bottom.saturating_sub(vb_y);
let _ = (&mut vb_w, &mut vb_h);
let mut document = Node::new("svg")
.set("xmlns", "http://www.w3.org/2000/svg")
.set("width", vb_w_i32)
.set("height", vb_h_i32)
.set("viewBox", format!("{vb_x} {vb_y} {vb_w_i32} {vb_h_i32}"));
if let Some(contract) = source_contract {
document = contract.document(document, &context_current);
}
let (definitions, elements) = share_repeated_bitmaps(definitions, elements);
if !definitions.is_empty() {
let mut defs = Node::new("defs");
for v in definitions {
defs = defs.add(v);
}
document = document.add(defs);
}
for v in elements {
document = document.add(v);
}
Ok(document.to_string().into_bytes())
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn bit_blt(mut self, record_number: usize, record: META_BITBLT) -> Result<Self, PlayError> {
let operator = match record {
META_BITBLT::WithBitmap {
raster_operation,
height,
width,
y_dest,
x_dest,
target,
..
} => {
let rect = self
.context_current
.blit_dest_rect(x_dest, y_dest, width, height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
if raster_operation.use_source() {
operator = operator.source_bitmap16(target);
}
operator
}
META_BITBLT::WithoutBitmap {
raster_operation,
height,
width,
y_dest,
x_dest,
..
} => {
let rect = self
.context_current
.blit_dest_rect(x_dest, y_dest, width, height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
operator
}
};
let Some(elem) = operator
.clip(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
)
.run(
&mut self.definitions,
&mut self.brush_only_rop_sequence,
self.elements.len(),
)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?
else {
return Ok(self);
};
self.push_element(record_number, elem);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn device_independent_bitmap_bit_blt(
mut self,
record_number: usize,
record: META_DIBBITBLT,
) -> Result<Self, PlayError> {
let operator = match record {
META_DIBBITBLT::WithBitmap {
raster_operation,
height,
width,
y_dest,
x_dest,
target,
..
} => {
let rect = self
.context_current
.blit_dest_rect(x_dest, y_dest, width, height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
if raster_operation.use_source() {
operator = operator.source_bitmap(*target);
}
operator
}
META_DIBBITBLT::WithoutBitmap {
raster_operation,
height,
width,
y_dest,
x_dest,
..
} => {
let rect = self
.context_current
.blit_dest_rect(x_dest, y_dest, width, height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
operator
}
};
let Some(elem) = operator
.clip(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
)
.run(
&mut self.definitions,
&mut self.brush_only_rop_sequence,
self.elements.len(),
)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?
else {
return Ok(self);
};
self.push_element(record_number, elem);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn device_independent_bitmap_stretch_blt(
mut self,
record_number: usize,
record: META_DIBSTRETCHBLT,
) -> Result<Self, PlayError> {
let operator = match record {
META_DIBSTRETCHBLT::WithBitmap {
raster_operation,
src_height,
src_width,
y_src,
x_src,
dest_height,
dest_width,
y_dest,
x_dest,
target,
..
} => {
let rect =
self.context_current
.blit_dest_rect(x_dest, y_dest, dest_width, dest_height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if matches!(
raster_operation,
TernaryRasterOperation::SRCPAINT | TernaryRasterOperation::SRCAND
) {
let bitmap = crate::wmf::converter::Bitmap::from((*target).clone());
let composed = source_rop_pair::PendingSourcePair::observe(
&mut self.source_rop_pair,
source_rop_pair::SourceBlit {
record: record_number,
element: self.elements.len(),
operation: raster_operation,
destination: rect,
source: [x_src, y_src, src_width, src_height],
bitmap: &bitmap,
},
);
if let Some(composed) = composed {
if let Some(previous) = self.elements.get_mut(composed.previous_element) {
use base64::{engine::general_purpose::STANDARD, Engine};
*previous = Node::new("g");
let uri =
format!("data:image/png;base64,{}", STANDARD.encode(composed.png));
self.push_element(
record_number,
TernaryRasterOperator::image_node(rect, uri),
);
return Ok(self);
}
}
} else {
self.source_rop_pair = None;
}
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
if raster_operation.use_source() {
operator = operator
.source_bitmap(*target)
.source_rectangle(x_src, y_src, src_width, src_height);
}
operator
}
META_DIBSTRETCHBLT::WithoutBitmap {
raster_operation,
dest_height,
dest_width,
y_dest,
x_dest,
..
} => {
let rect =
self.context_current
.blit_dest_rect(x_dest, y_dest, dest_width, dest_height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
operator
}
};
let Some(elem) = operator
.clip(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
)
.run(
&mut self.definitions,
&mut self.brush_only_rop_sequence,
self.elements.len(),
)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?
else {
return Ok(self);
};
self.push_element(record_number, elem);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_device_independent_bitmap_to_dev(
self,
record_number: usize,
record: META_SETDIBTODEV,
) -> Result<Self, PlayError> {
info!("META_SETDIBTODEV: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn stretch_blt(
mut self,
record_number: usize,
record: META_STRETCHBLT,
) -> Result<Self, PlayError> {
let operator = match record {
META_STRETCHBLT::WithBitmap {
raster_operation,
dest_height,
dest_width,
y_dest,
x_dest,
target,
..
} => {
let rect =
self.context_current
.blit_dest_rect(x_dest, y_dest, dest_width, dest_height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
if raster_operation.use_source() {
operator = operator.source_bitmap16(target);
}
operator
}
META_STRETCHBLT::WithoutBitmap {
raster_operation,
dest_height,
dest_width,
y_dest,
x_dest,
..
} => {
let rect =
self.context_current
.blit_dest_rect(x_dest, y_dest, dest_width, dest_height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
operator
}
};
let Some(elem) = operator
.clip(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
)
.run(
&mut self.definitions,
&mut self.brush_only_rop_sequence,
self.elements.len(),
)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?
else {
return Ok(self);
};
self.push_element(record_number, elem);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn stretch_device_independent_bitmap(
mut self,
record_number: usize,
record: META_STRETCHDIB,
) -> Result<Self, PlayError> {
let META_STRETCHDIB {
raster_operation,
src_height,
src_width,
y_src,
x_src,
dest_height,
dest_width,
y_dst,
x_dst,
dib,
..
} = record;
let rect = self
.context_current
.blit_dest_rect(x_dst, y_dst, dest_width, dest_height);
let mut operator = TernaryRasterOperator::new(raster_operation, rect);
if raster_operation.use_selected_brush() {
operator = operator.brush(self.selected_brush().clone());
}
if raster_operation.use_source() {
let bottom_up = match &dib.dib_header_info {
crate::wmf::parser::BitmapInfoHeader::Core(_) => true,
crate::wmf::parser::BitmapInfoHeader::Info(header) => header.height > 0,
crate::wmf::parser::BitmapInfoHeader::V4(header) => header.height > 0,
crate::wmf::parser::BitmapInfoHeader::V5(header) => header.height > 0,
};
operator = operator
.source_bitmap(dib)
.source_rectangle(x_src, y_src, src_width, src_height);
if bottom_up {
operator = operator.bottom_up_source();
}
}
let Some(elem) = operator
.clip(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
)
.run(
&mut self.definitions,
&mut self.brush_only_rop_sequence,
self.elements.len(),
)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?
else {
return Ok(self);
};
self.push_element(record_number, elem);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn eof(self, record_number: usize, _: META_EOF) -> Result<Self, PlayError> {
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn header(mut self, record_number: usize, header: MetafileHeader) -> Result<Self, PlayError> {
let (placeable, header) = match header {
MetafileHeader::StartsWithHeader(header) => (None, header),
MetafileHeader::StartsWithPlaceable(placeable, header) => (Some(placeable), header),
};
if let Some(placeable) = placeable {
let Rect {
left,
top,
right,
bottom,
} = placeable.bounding_box;
self.context_current = self
.context_current
.window_origin(left, top)
.window_ext(right.saturating_sub(left), bottom.saturating_sub(top));
}
self.context_current = self
.context_current
.create_object_table(header.number_of_objects);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn arc(mut self, record_number: usize, record: META_ARC) -> Result<Self, PlayError> {
let stroke = Stroke::from(self.selected_pen().clone());
let start = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_start_arc,
y: record.y_start_arc,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let end = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_end_arc,
y: record.y_end_arc,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let (rx, ry) = (
rect_half_extent(record.left_rect, record.right_rect),
rect_half_extent(record.top_rect, record.bottom_rect),
);
let center = self.context_current.point_s_to_absolute_point(&PointS {
x: record.left_rect.saturating_add(rx),
y: record.top_rect.saturating_add(ry),
});
let start_dx = f32::from(start.x) - f32::from(center.x);
let start_dy = f32::from(start.y) - f32::from(center.y);
let end_dx = f32::from(end.x) - f32::from(center.x);
let end_dy = f32::from(end.y) - f32::from(center.y);
let cross = -(start_dx * end_dy - start_dy * end_dx);
let large_arc = i16::from(cross < 0.0);
let data = Data::new()
.move_to(format!("{} {}", start.x, start.y))
.elliptical_arc_to(format!(
"{} {} {} {} {} {} {}",
rx, ry, 0, large_arc, 0, end.x, end.y
));
let path = Node::new("path").set("fill", "none").set("d", data);
let path = stroke.set_props(path);
self.context_current = self.context_current.drawing_position(end);
self.push_element(record_number, path);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn chord(mut self, record_number: usize, record: META_CHORD) -> Result<Self, PlayError> {
let rx = rect_half_extent(record.left_rect, record.right_rect);
let ry = rect_half_extent(record.top_rect, record.bottom_rect);
if rx == 0 || ry == 0 {
info!("META_CHORD is skipped because rx or ry is zero.");
return Ok(self);
}
let center = self.context_current.point_s_to_absolute_point(&PointS {
x: record.left_rect.saturating_add(rx),
y: record.top_rect.saturating_add(ry),
});
let p1 = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_radial1,
y: record.y_radial1,
});
let p2 = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_radial2,
y: record.y_radial2,
});
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let stroke = Stroke::from(self.selected_pen().clone());
let large_arc = 0;
let sweep = 1;
let data = Data::new()
.move_to(format!("{} {}", p1.x, p1.y))
.elliptical_arc_to(format!(
"{} {} 0 {} {} {} {}",
rx, ry, large_arc, sweep, p2.x, p2.y
))
.line_to(format!("{} {}", center.x, center.y))
.close();
let path = Node::new("path")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("d", data);
let path = stroke.set_props(path);
self.push_element(record_number, path);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn ellipse(mut self, record_number: usize, record: META_ELLIPSE) -> Result<Self, PlayError> {
let (rx, ry) = (
rect_half_extent(record.left_rect, record.right_rect),
rect_half_extent(record.top_rect, record.bottom_rect),
);
if rx == 0 || ry == 0 {
info!(
%rx, %ry,
"META_ELLIPSE is skipped because rx or ry is zero.",
);
return Ok(self);
}
let stroke = Stroke::from(self.selected_pen().clone());
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let point = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.left_rect.saturating_add(rx),
y: record.top_rect.saturating_add(ry),
});
self.context_current = self.context_current.extend_window(&point);
point
};
let ellipse = Node::new("ellipse")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("cx", point.x)
.set("cy", point.y)
.set("rx", rx)
.set("ry", ry);
let ellipse = stroke.set_props(ellipse);
self.push_element(record_number, ellipse);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn ext_flood_fill(
self,
record_number: usize,
record: META_EXTFLOODFILL,
) -> Result<Self, PlayError> {
info!("META_EXTFLOODFILL: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn ext_text_out(
mut self,
record_number: usize,
record: META_EXTTEXTOUT,
) -> Result<Self, PlayError> {
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
let font = &self.object_selected.font;
let text_content =
record
.into_utf8(font.charset)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?;
let explicit = text_advances::explicit_advances(&record, font.charset, &text_content);
let point = {
let point = PointS {
x: if self.context_current.text_align_update_cp {
self.context_current.drawing_position.x
} else {
record.x
},
y: if self.context_current.text_align_update_cp {
self.context_current.drawing_position.y
} else {
record.y
}
.saturating_add(match self.context_current.text_align_vertical {
VerticalTextAlignmentMode::VTA_TOP => {
let em = font.height.saturating_abs();
(em as f64 * 0.8) as i16
}
VerticalTextAlignmentMode::VTA_BOTTOM => {
let em = font.height.saturating_abs();
-((em as f64 * 0.2) as i16)
}
VerticalTextAlignmentMode::VTA_BASELINE => 0,
_ => 0,
}),
};
let point = if self.context_current.text_align_update_cp {
point
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
let text_align = self.context_current.as_css_text_align();
let shape_inside = if let (true, Some(rect)) = (
record.fw_opts.contains(&ExtTextOutOptions::ETO_CLIPPED),
record.rectangle,
) {
let tl = {
let point = PointS {
x: rect.left,
y: rect.top,
};
let point = if self.context_current.text_align_update_cp {
self.context_current.point_s_to_relative_point(&point)
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
let tr = {
let point = PointS {
x: rect.right,
y: rect.top,
};
let point = if self.context_current.text_align_update_cp {
self.context_current.point_s_to_relative_point(&point)
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
let bl = {
let point = PointS {
x: rect.left,
y: rect.bottom,
};
let point = if self.context_current.text_align_update_cp {
self.context_current.point_s_to_relative_point(&point)
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
let br = {
let point = PointS {
x: rect.right,
y: rect.bottom,
};
let point = if self.context_current.text_align_update_cp {
self.context_current.point_s_to_relative_point(&point)
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
Some(format!(
"shape-inside: polygon({} {} {} {});",
as_point_string(&tl),
as_point_string(&bl),
as_point_string(&br),
as_point_string(&tr),
))
} else {
None
};
let mut text = Node::new("text")
.set("x", point.x)
.set("y", point.y)
.set("text-anchor", text_align)
.set(
"dominant-baseline",
self.context_current.as_css_text_align_vertical(),
)
.set("fill", self.context_current.text_color_as_css_color());
if let Some(advances) = &explicit {
let alignment = match self.context_current.text_align_horizontal {
TextAlignmentMode::TA_CENTER => f64::from(advances.total) / 2.0,
TextAlignmentMode::TA_RIGHT => f64::from(advances.total),
_ => 0.0,
};
let window = &self.context_current.window;
let reference = if self.context_current.text_align_update_cp {
f64::from(point.x)
} else {
f64::from(window.to_device(f32::from(record.x), 0.0).0)
};
let zero = window.to_device(0.0, 0.0).0;
text = text.set("text-anchor", "start");
for (content, advance) in &advances.cells {
let logical = (f64::from(*advance) - alignment) as f32;
let x = reference + f64::from(window.to_device(logical, 0.0).0 - zero);
text = text.add(Node::new("tspan").set("x", x).add(Node::new_text(content)));
}
} else if record.dx.len() <= 1 {
text = text.add(Node::new_text(&text_content));
} else {
for (i, s) in text_content.graphemes(true).enumerate() {
let dx = if i == 0 {
0
} else {
*record.dx.get(i - 1).unwrap_or(&0)
};
let mut tspan = Node::new("tspan").add(Node::new_text(s));
if dx != 0 {
let excess_dx = (font.height.saturating_abs() / 2)
.saturating_mul(i16::try_from(s.width()).unwrap_or(0));
let dx = core::cmp::max(dx.saturating_sub(excess_dx), 0);
tspan = tspan.set("dx", dx);
}
text = text.add(tspan);
}
}
let (mut text, mut styles) = self.object_selected.font.set_props(text, &point);
if let Some(shape_inside) = shape_inside {
styles.push(shape_inside);
}
if !styles.is_empty() {
text = text.set("style", styles.join(""));
}
if self.context_current.text_align_update_cp {
let dx = explicit.as_ref().map_or_else(
|| {
f64::from(
(font.height.saturating_abs() / 2)
.saturating_mul(i16::try_from(text_content.width()).unwrap_or(0)),
)
},
|advances| {
let window = &self.context_current.window;
f64::from(
window.to_device(advances.total as f32, 0.0).0
- window.to_device(0.0, 0.0).0,
)
},
);
let point = PointS {
x: (f64::from(point.x) + dx) as i16,
y: point.y,
};
self.context_current = self.context_current.drawing_position(point);
}
if self.context_current.bk_mode == MixMode::OPAQUE {
let id = self.issue_definition_id();
let brush = if matches!(self.selected_brush(), Brush::Null) {
Brush::Solid {
color_ref: self.context_current.text_bk_color.clone(),
}
} else {
self.selected_brush().clone()
};
self.definitions
.push(brush.as_filter().set("id", id.as_str()));
text = text.set("filter", url_string(format!("#{id}").as_str()));
}
self.push_element(record_number, text);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn fill_region(self, record_number: usize, record: META_FILLREGION) -> Result<Self, PlayError> {
info!("META_FILLREGION: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn flood_fill(self, record_number: usize, record: META_FLOODFILL) -> Result<Self, PlayError> {
info!("META_FLOODFILL: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn frame_region(
self,
record_number: usize,
record: META_FRAMEREGION,
) -> Result<Self, PlayError> {
info!("META_FRAMEREGION: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn invert_region(
self,
record_number: usize,
record: META_INVERTREGION,
) -> Result<Self, PlayError> {
info!("META_INVERTREGION: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn line_to(mut self, record_number: usize, record: META_LINETO) -> Result<Self, PlayError> {
let stroke = Stroke::from(self.selected_pen().clone());
let point = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x,
y: record.y,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let data = Data::new()
.move_to(format!(
"{} {}",
self.context_current.drawing_position.x, self.context_current.drawing_position.y
))
.line_to(format!("{} {}", point.x, point.y));
let path = Node::new("path").set("fill", "none").set("d", data);
let path = stroke.set_props(path);
self.context_current = self.context_current.drawing_position(point);
self.push_element(record_number, path);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn paint_region(
self,
record_number: usize,
record: META_PAINTREGION,
) -> Result<Self, PlayError> {
info!("META_PAINTREGION: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn pat_blt(mut self, record_number: usize, record: META_PATBLT) -> Result<Self, PlayError> {
if record.width == 0 || record.height == 0 {
info!(
%record.width,
%record.height,
"META_PATBLT is skipped because width or height is zero.",
);
return Ok(self);
}
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let rect = Node::new("rect")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("stroke", "none")
.set("x", record.x_left)
.set("y", record.y_left)
.set("height", record.height)
.set("width", record.width);
self.push_element(record_number, rect);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn pie(mut self, record_number: usize, record: META_PIE) -> Result<Self, PlayError> {
let brush = self.selected_brush();
let stroke = Stroke::from(brush.clone());
let fill = match Fill::from(brush.clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let (rx, ry) = (
rect_half_extent(record.left_rect, record.right_rect),
rect_half_extent(record.top_rect, record.bottom_rect),
);
let (center_x, center_y) = (
record.left_rect.saturating_add(rx),
record.top_rect.saturating_add(ry),
);
let ellipse = Node::new("ellipse")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("cx", center_x)
.set("cy", center_y)
.set("rx", rx)
.set("ry", ry);
let ellipse = stroke.set_props(ellipse);
let stroke = Stroke::from(self.selected_pen().clone());
let p1 = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_radial1,
y: record.y_radial1,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let center = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: center_x,
y: center_y,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let p2 = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x_radial2,
y: record.y_radial2,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let data = Data::new()
.move_to(format!("{} {}", p1.x, p1.y))
.line_to(format!("{} {}", center.x, center.y))
.line_to(format!("{} {}", p2.x, p2.y));
let path = Node::new("path").set("fill", "none").set("d", data);
let path = stroke.set_props(path);
self.context_current = self.context_current.drawing_position(p2);
self.push_element(record_number, ellipse);
self.push_element(record_number, path);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn polyline(mut self, record_number: usize, record: META_POLYLINE) -> Result<Self, PlayError> {
let stroke = Stroke::from(self.selected_pen().clone());
let Some(point) = record.a_points.first() else {
return Err(PlayError::InvalidRecord {
cause: "aPoints[0] is not defined".to_owned(),
});
};
let mut coordinate = {
let point = self.context_current.point_s_to_absolute_point(point);
self.context_current = self.context_current.extend_window(&point);
point
};
let mut data = Data::new().move_to(format!("{} {}", coordinate.x, coordinate.y));
for i in 1..record.number_of_points {
let Some(point) = record.a_points.get(i as usize) else {
return Err(PlayError::InvalidRecord {
cause: format!("aPoints[{i}] is not defined"),
});
};
coordinate = {
let point = self.context_current.point_s_to_absolute_point(point);
self.context_current = self.context_current.extend_window(&point);
point
};
data = data.line_to(format!("{} {}", coordinate.x, coordinate.y));
}
let path = Node::new("path").set("fill", "none").set("d", data);
let path = stroke.set_props(path);
self.context_current = self.context_current.drawing_position(coordinate);
self.push_element(record_number, path);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn polygon(mut self, record_number: usize, record: META_POLYGON) -> Result<Self, PlayError> {
if record.number_of_points == 0 {
info!(%record.number_of_points, "polygon has no points");
return Ok(self);
}
let stroke = Stroke::from(self.selected_pen().clone());
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let mut points = Vec::with_capacity(record.number_of_points as usize);
for i in 0..record.number_of_points {
let Some(point) = record.a_points.get(i as usize) else {
return Err(PlayError::InvalidRecord {
cause: format!("aPoints[{i}] is not defined"),
});
};
let point = {
let point = self.context_current.point_s_to_absolute_point(point);
self.context_current = self.context_current.extend_window(&point);
point
};
points.push(as_point_string(&point));
}
let polygon = Node::new("polygon")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("points", points.join(" "));
let polygon = stroke.set_props(polygon);
self.push_element(record_number, polygon);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn poly_polygon(
mut self,
record_number: usize,
record: META_POLYPOLYGON,
) -> Result<Self, PlayError> {
let stroke = Stroke::from(self.selected_pen().clone());
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let mut a_point: VecDeque<_> = record.poly_polygon.a_points.into();
let mut current_point_index = 0;
let mut subpaths: Vec<String> = Vec::new();
let mut bounds = [i32::MAX, i32::MAX, i32::MIN, i32::MIN];
for i in 0..record.poly_polygon.number_of_polygons {
let Some(points_of_polygon) = record.poly_polygon.a_points_per_polygon.get(i as usize)
else {
return Err(PlayError::InvalidRecord {
cause: format!("aPointsPerPolygon[{i}] is not defined"),
});
};
let mut points = Vec::with_capacity(*points_of_polygon as usize);
for _ in 0..*points_of_polygon {
let Some(point) = a_point.pop_front() else {
return Err(PlayError::InvalidRecord {
cause: format!("aPoints[{current_point_index}] is not defined"),
});
};
let point = {
let point = self.context_current.point_s_to_absolute_point(&point);
self.context_current = self.context_current.extend_window(&point);
point
};
points.push(as_point_string(&point));
bounds[0] = bounds[0].min(i32::from(point.x));
bounds[1] = bounds[1].min(i32::from(point.y));
bounds[2] = bounds[2].max(i32::from(point.x));
bounds[3] = bounds[3].max(i32::from(point.y));
current_point_index += 1;
}
if let Some((first, rest)) = points.split_first() {
let mut d = format!("M {}", first);
for p in rest {
d.push_str(&format!(" L {}", p));
}
d.push_str(" Z");
subpaths.push(d);
}
}
if !subpaths.is_empty() {
let path = Node::new("path")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("d", subpaths.join(" "));
let path = stroke.set_props(path);
self.brush_only_rop_sequence.observe_vector_mask(
&path,
&self.object_selected.brush,
self.context_current.draw_mode,
bounds,
(
self.current_clip_id.as_deref(),
self.context_current.clipping_region.as_ref(),
),
self.elements.len(),
);
self.push_element(record_number, path);
}
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn rectangle(
mut self,
record_number: usize,
record: META_RECTANGLE,
) -> Result<Self, PlayError> {
let stroke = Stroke::from(self.selected_pen().clone());
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let tl = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.left_rect,
y: record.top_rect,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let br = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.right_rect,
y: record.bottom_rect,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let rect = Node::new("rect")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("x", tl.x)
.set("y", tl.y)
.set("height", br.y.saturating_sub(tl.y))
.set("width", br.x.saturating_sub(tl.x));
let rect = stroke.set_props(rect);
self.push_element(record_number, rect);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn round_rect(
mut self,
record_number: usize,
record: META_ROUNDRECT,
) -> Result<Self, PlayError> {
let (width, height) = (
record.right_rect.saturating_sub(record.left_rect),
record.bottom_rect.saturating_sub(record.top_rect),
);
if width == 0 || height == 0 {
info!(
%width, %height,
"META_ROUNDRECT is skipped because width or height is zero.",
);
return Ok(self);
}
let stroke = Stroke::from(self.selected_pen().clone());
let fill = match Fill::from(self.selected_brush().clone()) {
Fill::Pattern { pattern } => {
let id = self.issue_definition_id();
self.definitions.push(pattern.set("id", id.as_str()));
url_string(format!("#{id}").as_str())
}
Fill::Value { value } => value,
};
let fill_rule = self.context_current.poly_fill_rule();
let point = {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.left_rect,
y: record.top_rect,
});
self.context_current = self.context_current.extend_window(&point);
point
};
let rect = Node::new("rect")
.set("fill", fill.as_str())
.set("fill-rule", fill_rule.as_str())
.set("x", point.x)
.set("y", point.y)
.set("height", height)
.set("width", width)
.set("rx", record.width)
.set("ry", record.height);
let rect = stroke.set_props(rect);
self.push_element(record_number, rect);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_pixel(self, record_number: usize, record: META_SETPIXEL) -> Result<Self, PlayError> {
info!("META_SETPIXEL: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn text_out(mut self, record_number: usize, record: META_TEXTOUT) -> Result<Self, PlayError> {
let font = &self.object_selected.font;
let text_content =
record
.into_utf8(font.charset)
.map_err(|err| PlayError::InvalidRecord {
cause: err.to_string(),
})?;
let point = {
let point = PointS {
x: record.x_start,
y: record
.y_start
.saturating_add(match self.context_current.text_align_vertical {
VerticalTextAlignmentMode::VTA_TOP => {
let em = font.height.saturating_abs();
(em as f64 * 0.8) as i16
}
VerticalTextAlignmentMode::VTA_BOTTOM => {
let em = font.height.saturating_abs();
-((em as f64 * 0.2) as i16)
}
VerticalTextAlignmentMode::VTA_BASELINE => 0,
_ => 0,
}),
};
let point = if self.context_current.text_align_update_cp {
self.context_current.point_s_to_relative_point(&point)
} else {
self.context_current.point_s_to_absolute_point(&point)
};
self.context_current = self.context_current.extend_window(&point);
point
};
let text = Node::new("text")
.set("x", point.x)
.set("y", point.y)
.set("fill", self.context_current.text_color_as_css_color())
.add(Node::new_text(text_content));
let (text, styles) = self.object_selected.font.set_props(text, &point);
let text = text.set("style", styles.join(""));
self.push_element(record_number, text);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_brush_indirect(
mut self,
record_number: usize,
record: META_CREATEBRUSHINDIRECT,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Brush(record.create_brush()));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_font_indirect(
mut self,
record_number: usize,
record: META_CREATEFONTINDIRECT,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Font(record.font));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_palette(
mut self,
record_number: usize,
record: META_CREATEPALETTE,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Palette(record.palette));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_pattern_brush(
mut self,
record_number: usize,
record: META_CREATEPATTERNBRUSH,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Brush(record.create_brush()));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_pen_indirect(
mut self,
record_number: usize,
record: META_CREATEPENINDIRECT,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Pen(record.pen));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_region(
mut self,
record_number: usize,
record: META_CREATEREGION,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Region(record.region));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn delete_object(
mut self,
record_number: usize,
record: META_DELETEOBJECT,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.delete(record.object_index as usize);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn create_device_independent_bitmap_pattern_brush(
mut self,
record_number: usize,
record: META_DIBCREATEPATTERNBRUSH,
) -> Result<Self, PlayError> {
self.context_current
.object_table
.push(GraphicsObject::Brush(record.create_brush()));
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn select_clip_region(
mut self,
record_number: usize,
record: META_SELECTCLIPREGION,
) -> Result<Self, PlayError> {
let object = self
.context_current
.object_table
.get(record.region as usize);
let rectangle = match object {
GraphicsObject::Region(region) => Some(region.bounding_rectangle.clone()),
_ => None,
};
if let Some(rectangle) = rectangle {
self.context_current.clipping_region = Some(rectangle.clone());
self.install_device_clip(record_number, &rectangle, false);
} else {
self.current_clip_id = None;
self.context_current.clipping_region = None;
}
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn select_object(
mut self,
record_number: usize,
record: META_SELECTOBJECT,
) -> Result<Self, PlayError> {
let object = self
.context_current
.object_table
.get(record.object_index as usize)
.clone();
let selected = self.object_selected.clone();
self.object_selected = match object {
GraphicsObject::Brush(v) => selected.brush(v),
GraphicsObject::Font(v) => selected.font(v),
GraphicsObject::Palette(v) => selected.palette(v),
GraphicsObject::Pen(v) => selected.pen(v),
GraphicsObject::Region(v) => selected.region(v),
GraphicsObject::Null => {
return Err(PlayError::UnexpectedGraphicsObject {
cause: "Graphics Object is null".to_owned(),
})
}
};
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn select_palette(
mut self,
record_number: usize,
record: META_SELECTPALETTE,
) -> Result<Self, PlayError> {
let object = self
.context_current
.object_table
.get(record.palette as usize);
let GraphicsObject::Palette(palette) = object else {
return Err(PlayError::UnexpectedGraphicsObject {
cause: "Graphics Object is not palette object".to_owned(),
});
};
self.object_selected = self.object_selected.palette(palette.clone());
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn animate_palette(
self,
record_number: usize,
record: META_ANIMATEPALETTE,
) -> Result<Self, PlayError> {
info!("META_ANIMATEPALETTE: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn exclude_clip_rect(
self,
record_number: usize,
record: META_EXCLUDECLIPRECT,
) -> Result<Self, PlayError> {
info!("META_EXCLUDECLIPRECT: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn intersect_clip_rect(
mut self,
record_number: usize,
record: META_INTERSECTCLIPRECT,
) -> Result<Self, PlayError> {
let META_INTERSECTCLIPRECT {
bottom,
right,
top,
left,
..
} = record;
let rectangle = Rect {
left,
top,
right,
bottom,
};
self.context_current = self.context_current.clipping_region(rectangle.clone());
self.install_device_clip(record_number, &rectangle, true);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn move_to(mut self, record_number: usize, record: META_MOVETO) -> Result<Self, PlayError> {
let point = self.context_current.point_s_to_absolute_point(&PointS {
x: record.x,
y: record.y,
});
self.context_current = self
.context_current
.extend_window(&point)
.drawing_position(point);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn offset_clip_region(
self,
record_number: usize,
record: META_OFFSETCLIPRGN,
) -> Result<Self, PlayError> {
info!("META_OFFSETCLIPRGN: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn offset_viewport_origin(
self,
record_number: usize,
record: META_OFFSETVIEWPORTORG,
) -> Result<Self, PlayError> {
info!("META_OFFSETVIEWPORTORG: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn offset_window_origin(
self,
record_number: usize,
record: META_OFFSETWINDOWORG,
) -> Result<Self, PlayError> {
info!("META_OFFSETWINDOWORG: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn realize_palette(
self,
record_number: usize,
record: META_REALIZEPALETTE,
) -> Result<Self, PlayError> {
info!("META_REALIZEPALETTE: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn resize_palette(
self,
record_number: usize,
record: META_RESIZEPALETTE,
) -> Result<Self, PlayError> {
info!("META_RESIZEPALETTE: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn restore_device_context(
mut self,
record_number: usize,
record: META_RESTOREDC,
) -> Result<Self, PlayError> {
self.restore_playback_context(record.n_saved_dc);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn save_device_context(
mut self,
record_number: usize,
record: META_SAVEDC,
) -> Result<Self, PlayError> {
self.save_playback_context();
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn scale_viewport_ext(
self,
record_number: usize,
record: META_SCALEVIEWPORTEXT,
) -> Result<Self, PlayError> {
info!("META_SCALEVIEWPORTEXT: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn scale_window_ext(
mut self,
record_number: usize,
record: META_SCALEWINDOWEXT,
) -> Result<Self, PlayError> {
let scale_x = (self.context_current.window.scale_x * f32::from(record.x_num))
/ f32::from(record.x_denom);
let scale_y = (self.context_current.window.scale_y * f32::from(record.y_num))
/ f32::from(record.y_denom);
self.context_current = self.context_current.window_scale(scale_x, scale_y);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_bk_color(
mut self,
record_number: usize,
record: META_SETBKCOLOR,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.text_bk_color(record.color_ref);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_bk_mode(
mut self,
record_number: usize,
record: META_SETBKMODE,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.bk_mode(record.bk_mode);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_layout(self, record_number: usize, record: META_SETLAYOUT) -> Result<Self, PlayError> {
info!("META_SETLAYOUT: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_map_mode(
mut self,
record_number: usize,
record: META_SETMAPMODE,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.map_mode(record.map_mode);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_mapper_flags(
self,
record_number: usize,
record: META_SETMAPPERFLAGS,
) -> Result<Self, PlayError> {
info!("META_SETMAPPERFLAGS: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_pal_entries(
self,
record_number: usize,
record: META_SETPALENTRIES,
) -> Result<Self, PlayError> {
info!("META_SETPALENTRIES: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_polyfill_mode(
mut self,
record_number: usize,
record: META_SETPOLYFILLMODE,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.poly_fill_mode(record.poly_fill_mode);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_relabs(self, record_number: usize, record: META_SETRELABS) -> Result<Self, PlayError> {
info!("META_SETRELABS: reserved record and not supported");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_raster_operation(
mut self,
record_number: usize,
record: META_SETROP2,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.draw_mode(record.draw_mode);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_stretch_blt_mode(
self,
record_number: usize,
record: META_SETSTRETCHBLTMODE,
) -> Result<Self, PlayError> {
info!("META_SETSTRETCHBLTMODE: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_text_align(
mut self,
record_number: usize,
record: META_SETTEXTALIGN,
) -> Result<Self, PlayError> {
let update_cp = record.text_alignment_mode & (TextAlignmentMode::TA_UPDATECP as u16)
== TextAlignmentMode::TA_UPDATECP as u16;
let align_horizontal = [TextAlignmentMode::TA_CENTER, TextAlignmentMode::TA_RIGHT]
.into_iter()
.find(|a| record.text_alignment_mode & (*a as u16) == *a as u16)
.unwrap_or(TextAlignmentMode::TA_LEFT);
let v_bits = record.text_alignment_mode & 0x0018;
let align_vertical = if v_bits == 0x0018 {
VerticalTextAlignmentMode::VTA_BASELINE
} else if v_bits == 0x0008 {
VerticalTextAlignmentMode::VTA_BOTTOM
} else {
VerticalTextAlignmentMode::VTA_TOP
};
self.context_current = self
.context_current
.text_align_update_cp(update_cp)
.text_align_horizontal(align_horizontal)
.text_align_vertical(align_vertical);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_text_char_extra(
self,
record_number: usize,
record: META_SETTEXTCHAREXTRA,
) -> Result<Self, PlayError> {
info!("META_SETTEXTCHAREXTRA: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_text_color(
mut self,
record_number: usize,
record: META_SETTEXTCOLOR,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.text_color(record.color_ref);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_text_justification(
self,
record_number: usize,
record: META_SETTEXTJUSTIFICATION,
) -> Result<Self, PlayError> {
info!("META_SETTEXTJUSTIFICATION: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_viewport_ext(
self,
record_number: usize,
record: META_SETVIEWPORTEXT,
) -> Result<Self, PlayError> {
info!("META_SETVIEWPORTEXT: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_viewport_origin(
self,
record_number: usize,
record: META_SETVIEWPORTORG,
) -> Result<Self, PlayError> {
info!("META_SETVIEWPORTORG: not implemented");
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_window_ext(
mut self,
record_number: usize,
record: META_SETWINDOWEXT,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.window_ext(record.x, record.y);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn set_window_origin(
mut self,
record_number: usize,
record: META_SETWINDOWORG,
) -> Result<Self, PlayError> {
self.context_current = self.context_current.window_origin(record.x, record.y);
Ok(self)
}
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip(self),
err(level = tracing::Level::ERROR, Display),
))]
fn escape(self, record_number: usize, record: META_ESCAPE) -> Result<Self, PlayError> {
Ok(self)
}
}
fn element_max_x(elem: &Node) -> Option<i32> {
let s = elem.to_string();
let x = parse_attr_i32(&s, "x").unwrap_or(0);
let w = parse_attr_i32(&s, "width").unwrap_or(0);
if w > 0 {
Some(x.saturating_add(w))
} else {
None
}
}
fn element_max_y(elem: &Node) -> Option<i32> {
let s = elem.to_string();
let y = parse_attr_i32(&s, "y").unwrap_or(0);
let h = parse_attr_i32(&s, "height").unwrap_or(0);
if h > 0 {
return Some(y.saturating_add(h));
}
if s.starts_with("<text ") {
if let Some(fs) = parse_attr_i32(&s, "font-size") {
return Some(y.saturating_add(fs));
}
}
None
}
fn parse_attr_i32(s: &str, attr: &str) -> Option<i32> {
let needle = format!(" {attr}=\"");
let start = s.find(&needle)?;
let val_start = start + needle.len();
let val_end = s[val_start..].find('"')?;
s[val_start..val_start + val_end].parse().ok()
}
fn rect_half_extent(from: i16, to: i16) -> i16 {
let half = (i32::from(to) - i32::from(from)) / 2;
i16::try_from(half).unwrap_or(if half < 0 { i16::MIN } else { i16::MAX })
}