compiler: Do not fail the embed_images pass with URLs

Slintpad uses URLs to images. Do not fail when we "embed"
those so that we find the list of resources on the Documents
later.

This fixes image loading in slintpad again.
This commit is contained in:
Tobias Hunger 2024-10-16 18:08:04 +02:00 committed by Tobias Hunger
parent 30ef606d04
commit 7f8da7bf28
4 changed files with 13 additions and 3 deletions

View file

@ -87,6 +87,8 @@ pub struct BitmapFont {
#[derive(Debug, Clone)]
pub enum EmbeddedResourcesKind {
/// Only List the resource, do not actually embed it
ListOnly,
/// Just put the file content as a resource
RawData,
/// The data has been processed in a texture

View file

@ -852,6 +852,7 @@ fn embed_resource(
declarations: &mut Vec<Declaration>,
) {
match &resource.kind {
crate::embedded_resources::EmbeddedResourcesKind::ListOnly => {}
crate::embedded_resources::EmbeddedResourcesKind::RawData => {
let resource_file = crate::fileaccess::load_file(std::path::Path::new(path)).unwrap(); // embedding pass ensured that the file exists
let data = resource_file.read();

View file

@ -2971,6 +2971,9 @@ fn generate_resources(doc: &Document) -> Vec<TokenStream> {
.map(|(path, er)| {
let symbol = format_ident!("SLINT_EMBEDDED_RESOURCE_{}", er.id);
match &er.kind {
&crate::embedded_resources::EmbeddedResourcesKind::ListOnly => {
quote!()
},
crate::embedded_resources::EmbeddedResourcesKind::RawData => {
let data = embedded_file_tokens(path);
quote!(static #symbol: &'static [u8] = #data;)

View file

@ -127,7 +127,7 @@ fn embed_images_from_expression(
fn embed_image(
global_embedded_resources: &RefCell<HashMap<SmolStr, EmbeddedResources>>,
_embed_files: EmbedResourcesKind,
embed_files: EmbedResourcesKind,
path: &str,
_scale_factor: f64,
diag: &mut BuildDiagnostics,
@ -139,11 +139,15 @@ fn embed_image(
std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
std::collections::hash_map::Entry::Vacant(e) => {
// Check that the file exists, so that later we can unwrap safely in the generators, etc.
if let Some(_file) = crate::fileaccess::load_file(std::path::Path::new(path)) {
if embed_files == EmbedResourcesKind::ListAllResources {
// Really do nothing with the image!
e.insert(EmbeddedResources { id: maybe_id, kind: EmbeddedResourcesKind::ListOnly });
return ImageReference::None;
} else if let Some(_file) = crate::fileaccess::load_file(std::path::Path::new(path)) {
#[allow(unused_mut)]
let mut kind = EmbeddedResourcesKind::RawData;
#[cfg(feature = "software-renderer")]
if _embed_files == EmbedResourcesKind::EmbedTextures {
if embed_files == EmbedResourcesKind::EmbedTextures {
match load_image(_file, _scale_factor) {
Ok((img, source_format, original_size)) => {
kind = EmbeddedResourcesKind::TextureData(generate_texture(