Merge pull request #13985 from RickiNano/feat/copyright-title-13821

File Info: a Copyrighted document shows © before its name in the tab and window title (#13821)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-14 01:39:31 +01:00
commit faa588f7d9
4 files changed
+210 -7

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ pub(crate) use batch_run::{batch_action, batch_inputs, batch_keeps_step, batch_s
pub use export::{LayerExportReport, SkippedExportLayer, bake_cube, export_layers_report_with, export_layers_with};
use export::{color_lookup_tables, layers_to_files, push_unique};
use info::file_info;
pub use info::{CAMERA_FIELDS, camera_data, read_file_info, write_file_info};
pub use info::{CAMERA_FIELDS, camera_data, copyright_marked, read_file_info, write_file_info};
#[cfg(test)]
use info::{exposure_text, find_element};
pub(crate) use open::{BatchOpen, batch_open_notice, batch_open_params, remember_batch_open};
+65 -4
View File
@@ -99,6 +99,66 @@ fn find_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> {
Some((i, v1 + 1, &xmp[v0..v1]))
}
/// The packet's `xmpRights:Marked` value, as an element or an attribute.
fn rights_marked(xmp: &str) -> Option<&str> {
find_element(xmp, "xmpRights:Marked").map(|(_, _, v)| v.trim()).or_else(|| marked_attr(xmp).map(|(_, _, v)| v))
}
/// Attribute form `xmpRights:Marked="…"` with either quote, any XML whitespace before the name
/// and around the `=`, as XMP writers lay attributes out on their own lines. Only names inside a
/// tag count, not text in another attribute's value, element text, a comment or CDATA. Returns
/// (start, end, value) with the whitespace before the name in the span, as [`find_attr`] does, so
/// a rewrite removes every form it reads.
fn marked_attr(xmp: &str) -> Option<(usize, usize, &str)> {
// The tab title asks every frame, and packets can be megabytes: walk only when the name is there.
if !xmp.contains("xmpRights:Marked") {
return None;
}
let b = xmp.as_bytes();
let (mut i, mut in_tag, mut quote) = (0, false, None);
while let Some(&c) = b.get(i) {
match (in_tag, quote) {
(_, Some(q)) => quote = (c != q).then_some(q),
(false, None) if c == b'<' => {
let rest = &xmp[i..];
let skip = [("<!--", "-->"), ("<![CDATA[", "]]>")].into_iter().find(|(open, _)| rest.starts_with(open));
match skip {
Some((_, close)) => i += rest.find(close).map_or(rest.len(), |e| e + close.len()) - 1,
None => in_tag = true,
}
}
(false, None) => {}
(true, None) => match c {
b'>' => in_tag = false,
b'"' | b'\'' => quote = Some(c),
_ if c.is_ascii_whitespace() => {
if let Some((end, v)) = marked_value(xmp, i + 1) {
return Some((i, end, v));
}
}
_ => {}
},
}
i += 1;
}
None
}
/// `xmpRights:Marked = "value"` starting at byte `at`: (end after the closing quote, value).
fn marked_value(xmp: &str, at: usize) -> Option<(usize, &str)> {
let rest = xmp.get(at..)?.strip_prefix("xmpRights:Marked")?.trim_start().strip_prefix('=')?.trim_start();
let quote = rest.chars().next().filter(|&c| c == '"' || c == '\'')?;
let v0 = xmp.len() - rest.len() + 1;
let v1 = xmp[v0..].find(quote)? + v0;
Some((v1 + 1, &xmp[v0..v1]))
}
/// Whether File Info's Copyright Status reads Copyrighted (`xmpRights:Marked` is `True`): the
/// document's tab and window title then start with "© " (#13821).
pub fn copyright_marked(xmp: Option<&str>) -> bool {
xmp.and_then(rights_marked) == Some("True")
}
/// File Info fields read from an XMP packet.
pub fn read_file_info(xmp: Option<&str>) -> Value {
let mut m = serde_json::Map::new();
@@ -117,11 +177,9 @@ pub fn read_file_info(xmp: Option<&str>) -> Value {
};
m.insert(key.into(), v);
}
let marked =
find_element(x, "xmpRights:Marked").map(|(_, _, v)| v.trim().to_string()).or_else(|| find_attr(x, "xmpRights:Marked").map(|(_, _, v)| v.to_string()));
m.insert(
"copyrightStatus".into(),
json!(match marked.as_deref() {
json!(match rights_marked(x) {
Some("True") => "copyrighted",
Some("False") => "publicDomain",
_ => "unknown",
@@ -271,7 +329,10 @@ pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String {
while let Some((a, b, _)) = find_element(&x, prop) {
x.replace_range(a..b, "");
}
while let Some((a, b, _)) = find_attr(&x, prop) {
// Marked is removed in every attribute form it is read in, and only from tags (#13821).
loop {
let attr = if prop == "xmpRights:Marked" { marked_attr(&x) } else { find_attr(&x, prop) };
let Some((a, b, _)) = attr else { break };
x.replace_range(a..b, "");
}
}
+13 -2
View File
@@ -564,7 +564,14 @@ pub(crate) fn tab_title_at(app: &PhotocraftApp, i: usize, zoom: f32) -> String {
let layer = active_layer.map(|l| format!("{}, ", l.name)).unwrap_or_default();
// A save in progress: "name @ 100% (Layer 1, RGB/16) - Saving 45%", as Photoshop does (#3389).
let saving = crate::jobs_ui::save_progress(app, st.doc.id).map(|s| format!(" - {s}")).unwrap_or_default();
format!("{} @ {}% ({layer}{model}/{}){}{saving}", st.doc.name, fmt_zoom(zoom), st.doc.depth.bits(), if st.is_dirty() { "*" } else { "" })
format!("{} @ {}% ({layer}{model}/{}){}{saving}", titled_name(st), fmt_zoom(zoom), st.doc.depth.bits(), if st.is_dirty() { "*" } else { "" })
}
/// The document's name as its tab and window title show it: "© name" when File Info marks it
/// Copyrighted, as Photoshop does (#13821).
fn titled_name(st: &photocraft_engine::DocState) -> String {
let copyrighted = photocraft_engine::file_cmds::copyright_marked(st.doc.metadata.xmp.as_deref());
format!("{}{}", if copyrighted { "\u{a9} " } else { "" }, st.doc.name)
}
/// The Studio strip's tab name: "name *" when unsaved, "name * - Saving 45%" while it saves (#3389).
@@ -812,7 +819,7 @@ pub fn extra_windows(app: &mut PhotocraftApp, ctx: &egui::Context) {
let wins = app.ui.windows.clone();
for w in wins.into_iter().filter(|w| w.open) {
let Some(st) = app.session.documents().get(w.document) else { continue };
let title = format!("{} — window {}", st.doc.name, w.id);
let title = format!("{} — window {}", titled_name(st), w.id);
let vid = egui::ViewportId::from_hash_of(("docwin", w.id));
let mut builder = crate::os_window::builder().with_title(title).with_inner_size([800.0, 600.0]);
if let Some([x, y]) = w.position {
@@ -909,3 +916,7 @@ pub(super) fn move_target_after_filter(visible: &[usize], from: usize, to: usize
None => document_count.saturating_sub(1),
}
}
#[cfg(test)]
#[path = "../file_info_copyright_title_tests.rs"]
mod file_info_copyright_title_tests;
@@ -0,0 +1,131 @@
//! #13821: a document File Info marks Copyrighted shows "© " before its name in the document tab
//! and the floating document window's title; Public Domain and Unknown show no prefix.
use serde_json::json;
use crate::PhotocraftApp;
use super::{pro_tab_title, titled_name};
fn app() -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 40, "height": 30, "name": "oracle"})).unwrap();
app
}
fn title(app: &PhotocraftApp) -> String {
pro_tab_title(app, 0)
}
fn status(app: &mut PhotocraftApp, status: &str) {
app.run("file.fileInfo", json!({"copyrightStatus": status})).unwrap();
}
#[test]
fn copyrighted_puts_the_copyright_sign_before_the_name() {
let mut app = app();
assert_eq!(title(&app), "oracle @ 100% (RGB/8)");
status(&mut app, "copyrighted");
assert_eq!(title(&app), "\u{a9} oracle @ 100% (RGB/8)*");
assert_eq!(titled_name(&app.session.documents()[0]), "\u{a9} oracle", "the floating window's title has it too");
}
#[test]
fn public_domain_and_unknown_show_no_prefix() {
let mut app = app();
status(&mut app, "copyrighted");
status(&mut app, "publicDomain");
assert_eq!(title(&app), "oracle @ 100% (RGB/8)*");
status(&mut app, "copyrighted");
status(&mut app, "unknown");
assert_eq!(title(&app), "oracle @ 100% (RGB/8)*");
assert_eq!(titled_name(&app.session.documents()[0]), "oracle");
}
#[test]
fn undo_and_redo_take_the_prefix_with_them() {
let mut app = app();
status(&mut app, "copyrighted");
assert!(app.session.undo());
assert!(title(&app).starts_with("oracle @ "), "{}", title(&app));
assert!(app.session.redo());
assert!(title(&app).starts_with("\u{a9} oracle @ "), "{}", title(&app));
}
#[test]
fn a_file_opened_with_the_mark_already_set_shows_the_prefix() {
let packets = [
// Element form, as Photoshop writes it.
(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/"><xmpRights:Marked> True </xmpRights:Marked></rdf:Description></rdf:RDF></x:xmpmeta>"#,
true,
),
// Attribute form.
(r#"<rdf:Description xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/" xmpRights:Marked="True"/>"#, true),
(r#"<rdf:Description xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/" xmpRights:Marked="False"/>"#, false),
(r#"<rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/"/>"#, false),
// Single quotes, and the name on its own line or after a tab, with space around the '='.
("<rdf:Description xmlns:xmpRights='http://ns.adobe.com/xap/1.0/rights/' xmpRights:Marked='True'/>", true),
("<rdf:Description\n xmpRights:Marked=\"True\"/>", true),
("<rdf:Description\txmpRights:Marked = 'True'/>", true),
("<rdf:Description\n xmpRights:Marked='False'/>", false),
// Not an attribute: a longer name, or the name with no '='.
("<rdf:Description xmpRights:MarkedX=\"True\"/>", false),
("<rdf:Description xmpRights:Marked/>", false),
];
for (xmp, marked) in packets {
let mut app = app();
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(xmp.into());
let want = if marked { "\u{a9} oracle @ 100% (RGB/8)" } else { "oracle @ 100% (RGB/8)" };
assert_eq!(title(&app), want, "{xmp}");
}
}
#[test]
fn changing_the_status_of_an_opened_document_clears_every_attribute_form() {
for attr in ["\n xmpRights:Marked='True'", "\txmpRights:Marked = \"True\"", " xmpRights:Marked=\"True\""] {
let xmp = format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/"{attr}/></rdf:RDF></x:xmpmeta>"#
);
for steps in [&["unknown"][..], &["publicDomain", "unknown"]] {
let mut app = app();
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(xmp.clone().into());
assert!(title(&app).starts_with("\u{a9} "), "{attr:?}");
for &s in steps {
status(&mut app, s);
}
assert!(title(&app).starts_with("oracle @ "), "{attr:?} then {steps:?}: {}", title(&app));
let packet = app.session.documents()[0].doc.metadata.xmp.as_deref().unwrap_or_default().to_string();
assert!(!packet.contains("xmpRights:Marked"), "{attr:?} then {steps:?} leaves {packet}");
assert!(packet.contains("rdf:about=\"\""), "the rest of the description stays: {packet}");
}
}
}
#[test]
fn the_name_inside_other_text_is_neither_read_nor_removed() {
const RDF: &str = r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">"#;
const END: &str = "</rdf:RDF></x:xmpmeta>";
let headline = r#"photoshop:Headline="Example xmpRights:Marked='True' text""#;
let quoted = [
format!("<rdf:Description rdf:about=\"\" {headline}/>"),
"<rdf:Description rdf:about=\"\"><photoshop:Credit>a xmpRights:Marked=\"True\" b</photoshop:Credit></rdf:Description>".to_string(),
"<rdf:Description rdf:about=\"\"><!-- <x xmpRights:Marked='True'/> --></rdf:Description>".to_string(),
"<rdf:Description rdf:about=\"\"><![CDATA[<x xmpRights:Marked='True'/>]]></rdf:Description>".to_string(),
];
for desc in quoted {
let mut app = app();
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(format!("{RDF}{desc}{END}").into());
assert!(title(&app).starts_with("oracle @ "), "text is not the mark: {desc}");
}
// Beside a real attribute: the status change removes the attribute and keeps the headline.
let mut app = app();
let xmp = format!("{RDF}<rdf:Description rdf:about=\"\" {headline}\n xmpRights:Marked='True'/>{END}");
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(xmp.into());
assert!(title(&app).starts_with("\u{a9} "));
status(&mut app, "unknown");
assert!(title(&app).starts_with("oracle @ "), "{}", title(&app));
let packet = app.session.documents()[0].doc.metadata.xmp.as_deref().unwrap_or_default().to_string();
assert!(packet.contains(headline), "the headline is unchanged: {packet}");
assert_eq!(packet.matches("xmpRights:Marked").count(), 1, "only the headline's text is left: {packet}");
}