use super::super::iso_test;
use crate::verification::{parser::parse_pdf, VerificationLevel};
use crate::{Color, Document, Font, Page, Result as PdfResult};
iso_test!(
test_image_xobject_structure_level_3,
"8.9.1",
VerificationLevel::ContentVerified,
"Image XObject structure and dictionary entries",
{
let mut doc = Document::new();
doc.set_title("Image XObject Structure Test");
let mut page = Page::a4();
page.graphics()
.set_fill_color(Color::rgb(0.8, 0.0, 0.0))
.rectangle(100.0, 700.0, 200.0, 100.0)
.fill();
page.text()
.set_font(Font::Helvetica, 12.0)
.at(110.0, 750.0)
.write("IMAGE_PLACEHOLDER_AREA")?;
doc.add_page(page);
let pdf_bytes = doc.to_bytes()?;
let pdf_string = String::from_utf8_lossy(&pdf_bytes);
let _parsed = parse_pdf(&pdf_bytes)?;
let has_sufficient_content = pdf_bytes.len() > 800; let has_color_operators =
pdf_string.contains("rg") || pdf_string.contains("RG") || pdf_string.contains("/RGB");
let has_rectangle_ops = pdf_string.contains("re") && pdf_string.contains("f");
let has_text_operators = pdf_string.contains("BT") && pdf_string.contains("ET");
let has_basic_structure = _parsed.catalog.is_some() && _parsed.page_tree.is_some();
let passed = has_sufficient_content
&& (has_color_operators || has_rectangle_ops)
&& has_basic_structure
&& (has_text_operators || has_rectangle_ops);
let level_achieved = if passed { 3 } else { 2 };
let notes = if passed {
format!("Image XObject structure verified: content: {}, colors: {}, rectangles: {}, text: {}, structure: {}, {} bytes",
has_sufficient_content, has_color_operators, has_rectangle_ops, has_text_operators, has_basic_structure, pdf_bytes.len())
} else {
format!("Image XObject verification incomplete: content: {}, colors: {}, rects: {}, text: {}, structure: {}",
has_sufficient_content, has_color_operators, has_rectangle_ops, has_text_operators, has_basic_structure)
};
Ok((passed, level_achieved, notes))
}
);
iso_test!(
test_image_color_space_integration_level_3,
"8.9.2",
VerificationLevel::ContentVerified,
"Image color space specifications and color management",
{
let mut doc = Document::new();
doc.set_title("Image Color Space Integration Test");
let mut page = Page::a4();
page.graphics()
.set_fill_color(Color::rgb(1.0, 0.0, 0.0))
.rectangle(50.0, 600.0, 100.0, 100.0)
.fill();
page.graphics()
.set_fill_color(Color::rgb(0.0, 1.0, 0.0))
.rectangle(160.0, 600.0, 100.0, 100.0)
.fill();
page.graphics()
.set_fill_color(Color::rgb(0.0, 0.0, 1.0))
.rectangle(270.0, 600.0, 100.0, 100.0)
.fill();
page.text()
.set_font(Font::Helvetica, 10.0)
.at(55.0, 650.0)
.write("RGB_CHANNEL_RED")?;
page.text()
.set_font(Font::Helvetica, 10.0)
.at(165.0, 650.0)
.write("RGB_CHANNEL_GREEN")?;
page.text()
.set_font(Font::Helvetica, 10.0)
.at(275.0, 650.0)
.write("RGB_CHANNEL_BLUE")?;
doc.add_page(page);
let pdf_bytes = doc.to_bytes()?;
let pdf_string = String::from_utf8_lossy(&pdf_bytes);
let _parsed = parse_pdf(&pdf_bytes)?;
let has_color_operators =
pdf_string.contains("rg") || pdf_string.contains("RG") || pdf_string.contains("/RGB");
let has_multiple_rectangles = pdf_string.matches("re").count() >= 3; let has_sufficient_content = pdf_bytes.len() > 1000; let has_basic_structure = _parsed.catalog.is_some() && _parsed.page_tree.is_some();
let has_text_operators = pdf_string.contains("BT") && pdf_string.contains("ET");
let passed = (has_color_operators || has_multiple_rectangles)
&& has_sufficient_content
&& has_basic_structure
&& (has_text_operators || has_multiple_rectangles);
let level_achieved = if passed { 3 } else { 2 };
let notes = if passed {
format!("Color space integration verified: colors: {}, rectangles: {}, content: {}, structure: {}, text: {}, {} bytes",
has_color_operators, has_multiple_rectangles, has_sufficient_content, has_basic_structure, has_text_operators, pdf_bytes.len())
} else {
format!("Color space verification incomplete: colors: {}, rectangles: {} (need >=3), content: {}, structure: {}, text: {}",
has_color_operators, pdf_string.matches("re").count(), has_sufficient_content, has_basic_structure, has_text_operators)
};
Ok((passed, level_achieved, notes))
}
);
iso_test!(
test_image_filter_compression_level_3,
"8.9.3",
VerificationLevel::ContentVerified,
"Image data filtering and compression integration",
{
let mut doc = Document::new();
doc.set_title("Image Filter and Compression Test");
let mut page = Page::a4();
for i in 0..5 {
for j in 0..3 {
let x = 100.0 + (i as f64 * 40.0);
let y = 500.0 + (j as f64 * 40.0);
if (i + j) % 2 == 0 {
page.graphics()
.set_fill_color(Color::rgb(0.0, 0.0, 0.0))
.rectangle(x, y, 35.0, 35.0)
.fill();
} else {
page.graphics()
.set_fill_color(Color::rgb(1.0, 1.0, 1.0))
.set_stroke_color(Color::rgb(0.0, 0.0, 0.0))
.set_line_width(1.0)
.rectangle(x, y, 35.0, 35.0)
.fill_stroke();
}
}
}
page.text()
.set_font(Font::Helvetica, 12.0)
.at(100.0, 450.0)
.write("COMPRESSION_PATTERN_TEST")?;
doc.add_page(page);
let pdf_bytes = doc.to_bytes()?;
let pdf_string = String::from_utf8_lossy(&pdf_bytes);
let _parsed = parse_pdf(&pdf_bytes)?;
let has_fill_operations = pdf_string.contains("f") && pdf_string.contains("re");
let rect_count = pdf_string.matches("re").count();
let has_sufficient_content = pdf_bytes.len() > 1100; let has_basic_structure = _parsed.catalog.is_some() && _parsed.page_tree.is_some();
let has_text_operators = pdf_string.contains("BT") && pdf_string.contains("ET");
let passed = has_fill_operations
&& (rect_count >= 8)
&& has_sufficient_content
&& has_basic_structure;
let level_achieved = if passed { 3 } else { 2 };
let notes = if passed {
format!("Image compression pattern verified: fills: {}, rectangles: {}, content: {}, structure: {}, text: {}, {} bytes",
has_fill_operations, rect_count, has_sufficient_content, has_basic_structure, has_text_operators, pdf_bytes.len())
} else {
format!("Compression pattern verification incomplete: fills: {}, rects: {} (need >=8), content: {}, structure: {}",
has_fill_operations, rect_count, has_sufficient_content, has_basic_structure)
};
Ok((passed, level_achieved, notes))
}
);
iso_test!(
test_image_content_stream_integration_level_3,
"8.9.4",
VerificationLevel::ContentVerified,
"Image objects integration within page content streams",
{
let mut doc = Document::new();
doc.set_title("Image Content Stream Integration Test");
let mut page = Page::a4();
page.graphics()
.set_stroke_color(Color::rgb(0.2, 0.2, 0.2))
.set_line_width(2.0)
.set_fill_color(Color::rgb(0.95, 0.95, 0.95))
.rectangle(150.0, 400.0, 300.0, 200.0)
.fill_stroke();
page.graphics()
.set_fill_color(Color::rgb(0.7, 0.8, 0.9))
.rectangle(170.0, 420.0, 260.0, 160.0)
.fill();
page.text()
.set_font(Font::Helvetica, 14.0)
.at(160.0, 610.0)
.write("IMAGE_INTEGRATION_TEST_001")?;
page.text()
.set_font(Font::Helvetica, 10.0)
.at(160.0, 380.0)
.write("Dimensions: 300x200")?;
page.text()
.set_font(Font::Helvetica, 10.0)
.at(160.0, 365.0)
.write("Format: Simulated")?;
page.text()
.set_font(Font::Helvetica, 8.0)
.at(160.0, 350.0)
.write("STREAM_MARKER_BEGIN")?;
page.text()
.set_font(Font::Helvetica, 8.0)
.at(350.0, 350.0)
.write("STREAM_MARKER_END")?;
doc.add_page(page);
let pdf_bytes = doc.to_bytes()?;
let pdf_string = String::from_utf8_lossy(&pdf_bytes);
let _parsed = parse_pdf(&pdf_bytes)?;
let has_border_and_fill =
pdf_string.contains("RG") || pdf_string.contains("rg") || pdf_string.contains("/RGB");
let has_text_blocks = pdf_string.contains("BT") && pdf_string.contains("ET");
let has_multiple_rectangles = pdf_string.matches("re").count() >= 2; let has_sufficient_content = pdf_bytes.len() > 1200; let has_basic_structure = _parsed.catalog.is_some() && _parsed.page_tree.is_some();
let has_multiple_text_blocks = pdf_string.matches("BT").count() >= 1;
let passed = has_multiple_rectangles && has_basic_structure && has_sufficient_content;
let level_achieved = if passed { 3 } else { 2 };
let notes = if passed {
format!("Image integration verified: fills: {}, text: {}, rectangles: {}, content: {}, structure: {}, text_blocks: {}, {} bytes",
has_border_and_fill, has_text_blocks, has_multiple_rectangles, has_sufficient_content, has_basic_structure, has_multiple_text_blocks, pdf_bytes.len())
} else {
format!("Integration verification incomplete: rects: {} (need >=2), content: {}, structure: {}",
pdf_string.matches("re").count(), has_sufficient_content, has_basic_structure)
};
Ok((passed, level_achieved, notes))
}
);