use crate::paint::{paint_op_replay_plane, PaintOp, PaintReplayPlane};
use super::caps::BackendFeature;
use super::util::paint_op_kind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PaintOpKindSpec {
pub kind: &'static str,
pub default_plane: PaintReplayPlane,
pub required_feature: Option<BackendFeature>,
pub survives_flatten: bool,
pub appears_in_trace: bool,
pub summary_ko: &'static str,
}
impl PaintOpKindSpec {
pub fn matches_kind(self, kind: &str) -> bool {
self.kind == kind
}
pub fn plane_name(self) -> &'static str {
self.default_plane.as_str()
}
pub fn feature_name(self) -> &'static str {
self.required_feature
.map(BackendFeature::as_str)
.unwrap_or("none")
}
}
pub const PAINT_OP_KIND_SPECS: &[PaintOpKindSpec] = &[
PaintOpKindSpec {
kind: "pageBackground",
default_plane: PaintReplayPlane::Background,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "페이지 배경. 재생은 항상 첫 plane 이다.",
},
PaintOpKindSpec {
kind: "textRun",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "선택·검색 가능한 텍스트 런.",
},
PaintOpKindSpec {
kind: "glyphRun",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "셰이핑된 글리프 런. 폰트 메트릭이 이미 풀린 형태.",
},
PaintOpKindSpec {
kind: "glyphOutline",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "글리프 외곽선. 텍스트 등가군이지 일반 Path 가 아니다.",
},
PaintOpKindSpec {
kind: "charOverlap",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "글자겹침 명시 visual op.",
},
PaintOpKindSpec {
kind: "textControlMark",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "문단 끝·줄바꿈·필드 마커. 화면 전용일 수 있다.",
},
PaintOpKindSpec {
kind: "tabLeader",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "탭 리더 점선·실선 geometry.",
},
PaintOpKindSpec {
kind: "textDecoration",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "밑줄·취소선·강조점.",
},
PaintOpKindSpec {
kind: "footnoteMarker",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::VectorText),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "각주·미주 위첨자 마커.",
},
PaintOpKindSpec {
kind: "line",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "직선. 계측 백엔드도 센다.",
},
PaintOpKindSpec {
kind: "rectangle",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "사각형. 그라디언트가 있으면 Gradients 가 필요하다.",
},
PaintOpKindSpec {
kind: "ellipse",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "타원. 사각형과 같은 스타일 계약을 따른다.",
},
PaintOpKindSpec {
kind: "path",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "임의 패스. 화살표·연결선을 담을 수 있다.",
},
PaintOpKindSpec {
kind: "image",
default_plane: PaintReplayPlane::Flow,
required_feature: Some(BackendFeature::Images),
survives_flatten: true,
appears_in_trace: true,
summary_ko: "래스터 이미지. BehindText/InFrontOfText wrap 이 plane 을 바꾼다.",
},
PaintOpKindSpec {
kind: "equation",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "수식 SVG 조각. 텍스트 추출은 script 필드를 쓴다.",
},
PaintOpKindSpec {
kind: "formObject",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "양식 컨트롤(단추·체크·입력).",
},
PaintOpKindSpec {
kind: "placeholder",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "차트/OLE/그림-없음 자리표시. 인쇄 등가에서는 숨길 수 있다.",
},
PaintOpKindSpec {
kind: "rawSvg",
default_plane: PaintReplayPlane::Flow,
required_feature: None,
survives_flatten: true,
appears_in_trace: true,
summary_ko: "미리 렌더된 SVG 조각(OLE 등).",
},
];
pub const PAINT_OP_KIND_COUNT: usize = 18;
pub fn spec_for_kind(kind: &str) -> Option<&'static PaintOpKindSpec> {
PAINT_OP_KIND_SPECS
.iter()
.find(|spec| spec.matches_kind(kind))
}
pub fn all_kind_names() -> impl Iterator<Item = &'static str> {
PAINT_OP_KIND_SPECS.iter().map(|spec| spec.kind)
}
pub fn classify_op(op: &PaintOp) -> ClassifiedOp {
let kind = paint_op_kind(op);
let spec = spec_for_kind(kind).expect("paint_op_kind 는 카탈로그에 있어야 한다");
ClassifiedOp {
kind,
spec,
plane: paint_op_replay_plane(op),
bounds: OpBounds::from_op(op),
}
}
#[derive(Debug, Clone, Copy)]
pub struct ClassifiedOp {
pub kind: &'static str,
pub spec: &'static PaintOpKindSpec,
pub plane: PaintReplayPlane,
pub bounds: OpBounds,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OpBounds {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
impl OpBounds {
pub fn from_op(op: &PaintOp) -> Self {
let b = op.bounds();
Self {
x: b.x,
y: b.y,
width: b.width,
height: b.height,
}
}
pub fn trace_csv(self) -> String {
format!(
"{:.2},{:.2},{:.2},{:.2}",
self.x, self.y, self.width, self.height
)
}
pub fn is_finite(self) -> bool {
self.x.is_finite()
&& self.y.is_finite()
&& self.width.is_finite()
&& self.height.is_finite()
&& self.width >= 0.0
&& self.height >= 0.0
}
}
pub fn catalog_covers_kind(kind: &str) -> bool {
spec_for_kind(kind).is_some()
}
pub fn catalog_invariants_hold() -> Result<(), String> {
if PAINT_OP_KIND_SPECS.len() != PAINT_OP_KIND_COUNT {
return Err(format!(
"카탈로그 행 수 {} != PAINT_OP_KIND_COUNT {PAINT_OP_KIND_COUNT}",
PAINT_OP_KIND_SPECS.len()
));
}
let mut seen = std::collections::BTreeSet::new();
for spec in PAINT_OP_KIND_SPECS {
if !seen.insert(spec.kind) {
return Err(format!("중복 kind: {}", spec.kind));
}
if spec.kind.is_empty() {
return Err("빈 kind".into());
}
if spec.summary_ko.is_empty() {
return Err(format!("{} 설명이 비었다", spec.kind));
}
if !spec.appears_in_trace {
return Err(format!(
"{} 는 추적 로그에 빠져서는 안 된다 — 계측 계약이 깨진다",
spec.kind
));
}
let plane = spec.plane_name();
if !matches!(
plane,
"background" | "behindText" | "flow" | "inFrontOfText"
) {
return Err(format!("{} plane 이름 불명: {plane}", spec.kind));
}
}
Ok(())
}