🐛 Fix image loading callback

This commit is contained in:
Elena Torro 2026-04-21 08:57:24 +02:00 committed by Belén Albeza
parent f00ea8789f
commit f673b32567
2 changed files with 37 additions and 10 deletions

View File

@ -627,6 +627,7 @@
(if (empty? fills) (if (empty? fills)
(h/call wasm/internal-module "_clear_shape_fills") (h/call wasm/internal-module "_clear_shape_fills")
(let [fills (types.fills/coerce fills) (let [fills (types.fills/coerce fills)
image-ids (types.fills/get-image-ids fills)
offset (mem/alloc->offset-32 (types.fills/get-byte-size fills)) offset (mem/alloc->offset-32 (types.fills/get-byte-size fills))
heap (mem/get-heap-u32)] heap (mem/get-heap-u32)]
@ -648,7 +649,7 @@
(when (zero? cached-image?) (when (zero? cached-image?)
(fetch-image shape-id id thumbnail?)))) (fetch-image shape-id id thumbnail?))))
(types.fills/get-image-ids fills))))) image-ids))))
(defn set-shape-strokes (defn set-shape-strokes
[shape-id strokes thumbnail?] [shape-id strokes thumbnail?]
@ -675,7 +676,8 @@
(some? gradient) (some? gradient)
(do (do
(types.fills.impl/write-gradient-fill offset dview opacity gradient) (types.fills.impl/write-gradient-fill offset dview opacity gradient)
(h/call wasm/internal-module "_add_shape_stroke_fill")) (h/call wasm/internal-module "_add_shape_stroke_fill")
nil)
(some? image) (some? image)
(let [image-id (get image :id) (let [image-id (get image :id)
@ -692,7 +694,8 @@
(some? color) (some? color)
(do (do
(types.fills.impl/write-solid-fill offset dview opacity color) (types.fills.impl/write-solid-fill offset dview opacity color)
(h/call wasm/internal-module "_add_shape_stroke_fill"))))) (h/call wasm/internal-module "_add_shape_stroke_fill")
nil))))
strokes)) strokes))
(defn set-shape-svg-attrs (defn set-shape-svg-attrs
@ -1302,16 +1305,17 @@
(when (or (seq pending-thumbnails) (seq pending-full)) (when (or (seq pending-thumbnails) (seq pending-full))
(->> (rx/concat (->> (rx/concat
(->> (rx/from (vals pending-thumbnails)) (->> (rx/from (vals pending-thumbnails))
(rx/merge-map (fn [callback] (callback))) (rx/merge-map
(fn [callback]
(if (fn? callback) (callback) (rx/empty))))
(rx/reduce conj [])) (rx/reduce conj []))
(->> (rx/from (vals pending-full)) (->> (rx/from (vals pending-full))
(rx/mapcat (fn [callback] (callback))) (rx/mapcat
(fn [callback]
(if (fn? callback) (callback) (rx/empty))))
(rx/reduce conj []))) (rx/reduce conj [])))
(rx/subs! (rx/subs!
(fn [_] (fn [_]
;; Fonts are now loaded — recompute text
;; layouts so Skia uses the real metrics
;; instead of fallback-font estimates.
(let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes)] (let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes)]
(when (seq text-ids) (when (seq text-ids)
(update-text-layouts text-ids))) (update-text-layouts text-ids)))

View File

@ -1,11 +1,34 @@
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::mem; use crate::mem;
use crate::shapes::Fill;
use crate::state::State;
use crate::uuid::Uuid; use crate::uuid::Uuid;
use crate::with_state_mut; use crate::with_state_mut;
use crate::STATE; use crate::STATE;
use crate::{shapes::ImageFill, utils::uuid_from_u32_quartet}; use crate::{shapes::ImageFill, utils::uuid_from_u32_quartet};
use macros::wasm_error; use macros::wasm_error;
fn touch_shapes_with_image(state: &mut State, image_id: Uuid) {
let ids: Vec<Uuid> = state
.shapes
.iter()
.filter(|shape| {
shape
.fills()
.any(|f| matches!(f, Fill::Image(i) if i.id() == image_id))
|| shape
.strokes
.iter()
.any(|s| matches!(&s.fill, Fill::Image(i) if i.id() == image_id))
})
.map(|shape| shape.id)
.collect();
for id in ids {
state.touch_shape(id);
}
}
const FLAG_KEEP_ASPECT_RATIO: u8 = 1 << 0; const FLAG_KEEP_ASPECT_RATIO: u8 = 1 << 0;
const IMAGE_IDS_SIZE: usize = 32; const IMAGE_IDS_SIZE: usize = 32;
const IMAGE_HEADER_SIZE: usize = 36; // 32 bytes for IDs + 4 bytes for is_thumbnail flag const IMAGE_HEADER_SIZE: usize = 36; // 32 bytes for IDs + 4 bytes for is_thumbnail flag
@ -90,7 +113,7 @@ pub extern "C" fn store_image() -> Result<()> {
{ {
eprintln!("{}", msg); eprintln!("{}", msg);
} }
state.touch_shape(ids.shape_id); touch_shapes_with_image(state, ids.image_id);
}); });
mem::free_bytes()?; mem::free_bytes()?;
@ -167,7 +190,7 @@ pub extern "C" fn store_image_from_texture() -> Result<()> {
// FIXME: Review if we should return a RecoverableError // FIXME: Review if we should return a RecoverableError
eprintln!("store_image_from_texture error: {}", msg); eprintln!("store_image_from_texture error: {}", msg);
} }
state.touch_shape(ids.shape_id); touch_shapes_with_image(state, ids.image_id);
}); });
mem::free_bytes()?; mem::free_bytes()?;