Perserve unused classes when rendering
Currently, we are trying to move the symbolics generation script for the design team to use librsvg directly so we can catch bugs earlier. The icons are drawn in a set of icons see https://gitlab.gnome.org/GNOME/adwaita-icon-theme/blob/master/src/symbolic/gnome-stencils.svg or https://gitlab.gnome.org/Teams/Design/icon-development-kit/blob/master/src/icons.svg
both sets make uses of classes that get replaced later once the icons are being used as symbolics.
We have something that does this
pub struct Icon {
pub name: String,
pub id: String,
pub keywords: Vec<String>,
pub rect: String,
pub cache_file: PathBuf,
// pub classes: Vec<(String, String)>,
}
impl Icon {
pub fn render(&self, handle: &SvgHandle, into: Option<PathBuf>) -> Result<(), Error> {
let cache = match into {
Some(path) => path,
None => self.get_cache(),
};
let rect = format!("#{}", self.rect);
let group = format!("#{}", self.id);
debug!("Rendering the icon {} with id {} and rect {}", self.name, self.id, self.rect);
let renderer = CairoRenderer::new(handle);
let viewport = {
let doc = renderer.intrinsic_dimensions();
cairo::Rectangle {
x: 0.0,
y: 0.0,
width: doc.width.unwrap().length,
height: doc.height.unwrap().length,
}
};
let (rect, _) = renderer.geometry_for_layer(Some(&rect), &viewport).unwrap();
let mut surface = cairo::SvgSurface::new(rect.width, rect.height, &cache);
surface.set_document_unit(SvgUnit::Px);
let cr = cairo::Context::new(&surface);
cr.translate(-rect.x, -rect.y);
renderer.render_layer(&cr, Some(&group), &viewport)?;
/*
if !self.classes.is_empty() {
let mut src = File::open(&cache)?;
let mut data = String::new();
src.read_to_string(&mut data)?;
for class in self.classes.iter() {
data = data.replace(&class.1, &format!("{}\" class=\"{}", class.1, class.0));
}
src.write(data.as_bytes())?;
}*/
Ok(())
}
}
The issue is that librsvg removes all those classes and re-adding them manually isn't that easy. I looked into it a bit, but if we store the background color and the class name if there are any; we can re-inject the class name by replacing that specific background color. Except, librsvg, does convert any color to RGB percentages, which makes the task not very accurate, and might never work