315 lines
12 KiB
C++
315 lines
12 KiB
C++
#include "vde/foundation/io_dwfx.h"
|
||
#include "vde/brep/format_io.h"
|
||
|
||
#include <gtest/gtest.h>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <variant>
|
||
#include <vector>
|
||
|
||
using namespace vde::foundation;
|
||
|
||
namespace {
|
||
|
||
/// 合成 DWFx(ZIP store 方式,无压缩)的最小 ZIP 写出;读取端不校验 CRC。
|
||
struct ZipEntry {
|
||
std::string name;
|
||
std::string data;
|
||
};
|
||
|
||
void put_u16(std::vector<uint8_t>& out, uint16_t value) {
|
||
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
|
||
out.insert(out.end(), p, p + 2);
|
||
}
|
||
void put_u32(std::vector<uint8_t>& out, uint32_t value) {
|
||
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
|
||
out.insert(out.end(), p, p + 4);
|
||
}
|
||
|
||
std::vector<uint8_t> build_zip(const std::vector<ZipEntry>& entries) {
|
||
std::vector<uint8_t> out;
|
||
struct Central {
|
||
std::string name;
|
||
uint32_t offset = 0;
|
||
uint32_t size = 0;
|
||
};
|
||
std::vector<Central> central;
|
||
for (const auto& entry : entries) {
|
||
central.push_back(Central{entry.name, static_cast<uint32_t>(out.size()),
|
||
static_cast<uint32_t>(entry.data.size())});
|
||
put_u32(out, 0x04034B50u); // local file header
|
||
put_u16(out, 20);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0); // stored
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u32(out, 0); // crc(读取端不校验)
|
||
put_u32(out, static_cast<uint32_t>(entry.data.size()));
|
||
put_u32(out, static_cast<uint32_t>(entry.data.size()));
|
||
put_u16(out, static_cast<uint16_t>(entry.name.size()));
|
||
put_u16(out, 0);
|
||
out.insert(out.end(), entry.name.begin(), entry.name.end());
|
||
out.insert(out.end(), entry.data.begin(), entry.data.end());
|
||
}
|
||
const uint32_t cd_offset = static_cast<uint32_t>(out.size());
|
||
for (const auto& entry : central) {
|
||
put_u32(out, 0x02014B50u); // central directory entry
|
||
put_u16(out, 20);
|
||
put_u16(out, 20);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u32(out, 0);
|
||
put_u32(out, entry.size);
|
||
put_u32(out, entry.size);
|
||
put_u16(out, static_cast<uint16_t>(entry.name.size()));
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u32(out, 0);
|
||
put_u32(out, entry.offset);
|
||
out.insert(out.end(), entry.name.begin(), entry.name.end());
|
||
}
|
||
const uint32_t cd_size = static_cast<uint32_t>(out.size()) - cd_offset;
|
||
put_u32(out, 0x06054B50u); // EOCD
|
||
put_u16(out, 0);
|
||
put_u16(out, 0);
|
||
put_u16(out, static_cast<uint16_t>(entries.size()));
|
||
put_u16(out, static_cast<uint16_t>(entries.size()));
|
||
put_u32(out, cd_size);
|
||
put_u32(out, cd_offset);
|
||
put_u16(out, 0);
|
||
return out;
|
||
}
|
||
|
||
std::string write_binary_fixture(const char* name, const std::vector<uint8_t>& bytes) {
|
||
const std::string path = std::string(VDE_TEST_TEMP_DIR) + name;
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
out.write(reinterpret_cast<const char*>(bytes.data()),
|
||
static_cast<std::streamsize>(bytes.size()));
|
||
out.close();
|
||
return path;
|
||
}
|
||
|
||
std::string write_text_fixture(const char* name, const std::string& text) {
|
||
const std::string path = std::string(VDE_TEST_TEMP_DIR) + name;
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
out << text;
|
||
out.close();
|
||
return path;
|
||
}
|
||
|
||
const char* kFixedPage =
|
||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
||
"<FixedPage xmlns=\"http://schemas.microsoft.com/xps/2005/06\" Width=\"800\" "
|
||
"Height=\"600\">\n"
|
||
"<Canvas RenderTransform=\"1,0,0,1,0,0\">\n"
|
||
"<Path Data=\"M 0,0 L 100,0 L 100,100 L 0,100 Z\" Fill=\"#FF0000\" />\n"
|
||
"<Path Data=\"M 10,10 L 50,10 L 50,50\" />\n"
|
||
"<Glyphs UnicodeString=\"DWFx 108\" OriginX=\"20\" OriginY=\"30\" "
|
||
"FontRenderingEmSize=\"12\" />\n"
|
||
"</Canvas>\n"
|
||
"</FixedPage>\n";
|
||
|
||
std::string write_dwfx_fixture(const char* name, const char* fixed_page) {
|
||
std::vector<ZipEntry> entries = {
|
||
{"DWFX/FixedPage.fpage", fixed_page},
|
||
{"DWFX/[Content_Types].xml", "<Types></Types>"},
|
||
};
|
||
return write_binary_fixture(name, build_zip(entries));
|
||
}
|
||
|
||
bool has_hatch(const DxfDocument& doc) {
|
||
for (const auto& e : doc.entities)
|
||
if (std::holds_alternative<DxfHatch>(e)) return true;
|
||
return false;
|
||
}
|
||
bool has_text(const DxfDocument& doc) {
|
||
for (const auto& e : doc.entities)
|
||
if (std::holds_alternative<DxfText>(e)) return true;
|
||
return false;
|
||
}
|
||
bool has_polyline(const DxfDocument& doc) {
|
||
for (const auto& e : doc.entities)
|
||
if (std::holds_alternative<DxfLwPolyline>(e)) return true;
|
||
return false;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(Dwfx, SyntheticFixedPageImportsPathFillAndGlyphs) {
|
||
const std::string path = write_dwfx_fixture("wp108_dwfx_synth.dwfx", kFixedPage);
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
ASSERT_TRUE(r.success) << r.error;
|
||
EXPECT_GT(r.drawing.entities_parsed, 0);
|
||
EXPECT_TRUE(has_hatch(r.drawing));
|
||
EXPECT_TRUE(has_text(r.drawing));
|
||
EXPECT_TRUE(has_polyline(r.drawing));
|
||
bool only2d = false;
|
||
for (const auto& w : r.warnings) only2d = only2d || w.find("仅导入 2D") != std::string::npos;
|
||
EXPECT_TRUE(only2d);
|
||
}
|
||
|
||
TEST(Dwfx, SyntheticGlyphsOnlyFixedPageImportsText) {
|
||
const char* page =
|
||
"<FixedPage><Glyphs UnicodeString=\"Only Text\" OriginX=\"5\" "
|
||
"OriginY=\"7\" FontRenderingEmSize=\"10\" /></FixedPage>";
|
||
const std::string path = write_dwfx_fixture("wp108_dwfx_text.dwfx", page);
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
ASSERT_TRUE(r.success) << r.error;
|
||
EXPECT_TRUE(has_text(r.drawing));
|
||
EXPECT_GE(r.drawing.entities_parsed, 1);
|
||
}
|
||
|
||
TEST(Dwfx, ImportCadDetectsSyntheticDwfx) {
|
||
const std::string path = write_dwfx_fixture("wp108_dwfx_cad.dwfx", kFixedPage);
|
||
auto r = vde::brep::import_cad(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_TRUE(r.ok) << (r.messages.empty() ? "" : r.messages.front());
|
||
EXPECT_TRUE(r.has_drawing);
|
||
EXPECT_EQ(r.detected.format, vde::io::FileFormat::Dwf);
|
||
}
|
||
|
||
TEST(Dwfx, ZipWithoutFixedPageFailsClearly) {
|
||
std::vector<ZipEntry> entries = {{"readme.txt", "no fixed page here"}};
|
||
const std::string path =
|
||
write_binary_fixture("wp108_dwfx_nopage.dwfx", build_zip(entries));
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_FALSE(r.success);
|
||
EXPECT_NE(r.error.find("未找到 FixedPage"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(Dwfx, EmptyFixedPageFailsClearly) {
|
||
const std::string path =
|
||
write_dwfx_fixture("wp108_dwfx_empty.dwfx", "<FixedPage></FixedPage>");
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_FALSE(r.success);
|
||
EXPECT_NE(r.error.find("未找到可解析的 2D"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(Dwfx, NonZipFileFailsClearly) {
|
||
const std::string path = write_text_fixture("wp108_dwfx_bad.dwfx", "not a zip");
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_FALSE(r.success);
|
||
EXPECT_NE(r.error.find("未找到 ZIP 容器"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(Dwfx, MissingFileFailsClearly) {
|
||
DwfxImportResult r =
|
||
import_dwfx(std::string(VDE_TEST_TEMP_DIR) + "no_such_file_108.dwfx");
|
||
EXPECT_FALSE(r.success);
|
||
EXPECT_NE(r.error.find("无法打开"), std::string::npos) << r.error;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// FMT-11 剩余:图片元数据 + DWF6 二进制 W2D 明确诊断
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
std::string write_dwfx_entries_file(const char* name,
|
||
const std::vector<ZipEntry>& entries) {
|
||
return write_binary_fixture(name, build_zip(entries));
|
||
}
|
||
} // namespace
|
||
|
||
TEST(Dwfx, SyntheticImageResourceAndImageTagMetadata) {
|
||
const std::string png =
|
||
std::string("\x89PNG\r\n\x1a\n", 8) + std::string(24, '\x01');
|
||
const char* page =
|
||
"<FixedPage>"
|
||
"<Path Data=\"M 0,0 L 10,0 L 10,10 Z\" Fill=\"#FF0000\" />"
|
||
"<Image Source=\"/Resources/Images/logo.png\" Width=\"10\" Height=\"10\" />"
|
||
"<ImageBrush ImageSource=\"../Resources/Images/logo.png\" "
|
||
"Viewbox=\"0,0,1,1\" />"
|
||
"</FixedPage>";
|
||
const std::string path = write_dwfx_entries_file(
|
||
"wp108_dwfx_image.dwfx",
|
||
{{"DWFX/FixedPage.fpage", page},
|
||
{"DWFX/Resources/Images/logo.png", png},
|
||
{"DWFX/[Content_Types].xml", "<Types></Types>"}});
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
|
||
ASSERT_TRUE(r.success) << r.error;
|
||
ASSERT_EQ(r.images.size(), 1u) << "同一资源的 Image/ImageBrush 应去重";
|
||
EXPECT_NE(r.images[0].name.find("logo.png"), std::string::npos);
|
||
EXPECT_EQ(r.images[0].size_bytes, png.size());
|
||
EXPECT_EQ(r.images[0].content_type, "image/png");
|
||
bool image_warning = false;
|
||
for (const auto& w : r.warnings)
|
||
if (w.find("图像未导入") != std::string::npos) image_warning = true;
|
||
EXPECT_TRUE(image_warning) << "图像必须明确 warning 未导入";
|
||
EXPECT_GT(r.drawing.entities_parsed, 0);
|
||
EXPECT_FALSE(r.has_binary_w2d);
|
||
}
|
||
|
||
TEST(Dwfx, SyntheticMissingImageReferenceIsMetadataOnly) {
|
||
const char* page =
|
||
"<FixedPage>"
|
||
"<Path Data=\"M 0,0 L 5,0 L 5,5 Z\" />"
|
||
"<Image Source=\"/Resources/Images/missing.jpg\" />"
|
||
"</FixedPage>";
|
||
const std::string path = write_dwfx_entries_file(
|
||
"wp108_dwfx_image_missing.dwfx",
|
||
{{"DWFX/FixedPage.fpage", page}});
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
ASSERT_TRUE(r.success) << r.error;
|
||
ASSERT_EQ(r.images.size(), 1u);
|
||
EXPECT_NE(r.images[0].name.find("missing.jpg"), std::string::npos);
|
||
EXPECT_EQ(r.images[0].size_bytes, 0u);
|
||
EXPECT_EQ(r.images[0].content_type, "image/jpeg");
|
||
bool missing_warning = false;
|
||
for (const auto& w : r.warnings) {
|
||
if (w.find("未在 ZIP 中找到") != std::string::npos) missing_warning = true;
|
||
}
|
||
EXPECT_TRUE(missing_warning);
|
||
}
|
||
|
||
TEST(Dwfx, SyntheticBinaryW2DFailsClearlyWithDiagnostics) {
|
||
std::string binary("W2D\x00\x01\x02\x03", 7);
|
||
binary += std::string(32, '\x7f');
|
||
const std::string path = write_dwfx_entries_file(
|
||
"wp108_dwfx_binary_w2d.dwfx", {{"DWFX/drawing.w2d", binary}});
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_FALSE(r.success);
|
||
EXPECT_TRUE(r.has_binary_w2d);
|
||
EXPECT_NE(r.unsupported_detail.find("二进制 W2D"), std::string::npos)
|
||
<< r.unsupported_detail;
|
||
EXPECT_NE(r.error.find("二进制 W2D"), std::string::npos) << r.error;
|
||
bool binary_warning = false;
|
||
for (const auto& w : r.warnings)
|
||
if (w.find("二进制 W2D") != std::string::npos) binary_warning = true;
|
||
EXPECT_TRUE(binary_warning);
|
||
}
|
||
|
||
TEST(Dwfx, SyntheticBinaryW2DWithFixedPageStillImportsVector) {
|
||
std::string binary("W2D\x00\x01\x02", 6);
|
||
binary += std::string(16, '\x7f');
|
||
const char* page =
|
||
"<FixedPage><Path Data=\"M 0,0 L 20,0 L 20,20 Z\" /></FixedPage>";
|
||
const std::string path = write_dwfx_entries_file(
|
||
"wp108_dwfx_binary_with_page.dwfx",
|
||
{{"DWFX/FixedPage.fpage", page}, {"DWFX/drawing.w2d", binary}});
|
||
DwfxImportResult r = import_dwfx(path);
|
||
std::remove(path.c_str());
|
||
ASSERT_TRUE(r.success) << r.error;
|
||
EXPECT_TRUE(r.has_binary_w2d);
|
||
EXPECT_FALSE(r.unsupported_detail.empty());
|
||
EXPECT_GT(r.drawing.entities_parsed, 0);
|
||
bool binary_warning = false;
|
||
for (const auto& w : r.warnings)
|
||
if (w.find("二进制 W2D") != std::string::npos) binary_warning = true;
|
||
EXPECT_TRUE(binary_warning);
|
||
}
|