diff --git a/exporter/src/app/wasm/text.cljs b/exporter/src/app/wasm/text.cljs index 33dd4ad470..71d2d16372 100644 --- a/exporter/src/app/wasm/text.cljs +++ b/exporter/src/app/wasm/text.cljs @@ -12,7 +12,8 @@ [app.common.render-wasm.helpers :as h] [app.common.render-wasm.serializers :as sr] [app.common.render-wasm.text-content :as tc] - [app.common.render-wasm.wasm :as wasm])) + [app.common.render-wasm.wasm :as wasm] + [app.common.types.text.cjk-layout :as cjk])) (defn set-shape-text! "Serializes a text shape's content into the current WASM shape. Mirrors the @@ -20,9 +21,11 @@ paragraph -> layout. Byte writing and font resolution are the shared `text-content/write-shape-text!` defaults; the exporter has no fonts DB, so - it injects no variant normalization." + it injects no variant normalization. Like the workspace, it lays out the + active content (`cjk/active-content`): only the CJK attrs of the text + language." [content] - (when content + (when-let [content (some-> content cjk/active-content)] (h/call wasm/internal-module "_clear_shape_text") (h/call wasm/internal-module "_set_shape_vertical_align" (sr/translate-vertical-align (get content :vertical-align))) diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 275a489bbd..0884e96952 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -344,7 +344,7 @@ (assoc :name name)) (cond-> (some? new-size) (gsh/transform-shape - (ctm/change-size shape (:width new-size) (:height new-size)))))) + (dwwt/text-resize-modifiers shape content new-size))))) {:undo-group (when new-shape? id)}))))) (let [content (editor-shape-content (ted/export-content content) shape) diff --git a/frontend/src/app/main/data/workspace/wasm_text.cljs b/frontend/src/app/main/data/workspace/wasm_text.cljs index e2f52946ba..032b7d6c7c 100644 --- a/frontend/src/app/main/data/workspace/wasm_text.cljs +++ b/frontend/src/app/main/data/workspace/wasm_text.cljs @@ -61,23 +61,27 @@ (wasm.api/get-text-dimensions))] (resolve-text-size selrect grow-type content dimension))))) +(defn text-resize-modifiers + "Modifiers that resize `shape` with `content` to `new-size`, keeping the + point `text.geom/resize-origin` sets." + [{:keys [selrect] :as shape} content new-size] + (let [width-scale (/ (:width new-size) (:width selrect)) + height-scale (/ (:height new-size) (:height selrect)) + resize-v (gpt/point width-scale height-scale) + origin (text.geom/resize-origin (assoc shape :content content))] + (ctm/resize-modifiers + resize-v + origin + (:transform shape (gmt/matrix)) + (:transform-inverse shape (gmt/matrix))))) + (defn resize-wasm-text-modifiers ([shape] (resize-wasm-text-modifiers shape (:content shape))) - ([{:keys [id selrect] :as shape} content] + ([{:keys [id] :as shape} content] (when-let [new-size (get-wasm-text-new-size shape content)] - (let [width-scale (/ (:width new-size) (:width selrect)) - height-scale (/ (:height new-size) (:height selrect)) - resize-v (gpt/point width-scale height-scale) - origin (text.geom/resize-origin (assoc shape :content content))] - {id - {:modifiers - (ctm/resize-modifiers - resize-v - origin - (:transform shape (gmt/matrix)) - (:transform-inverse shape (gmt/matrix)))}})))) + {id {:modifiers (text-resize-modifiers shape content new-size)}}))) (defn resize-wasm-text "Resize a single text shape (auto-width/auto-height) by id. diff --git a/frontend/src/app/util/text/geometry.cljs b/frontend/src/app/util/text/geometry.cljs index c25829b7c0..98c0390fd8 100644 --- a/frontend/src/app/util/text/geometry.cljs +++ b/frontend/src/app/util/text/geometry.cljs @@ -30,27 +30,25 @@ (live-axis (:height dimension) (:height selrect)))}))) (defn- block-anchor + "Share of the width a vertical text grows by to its left: its right edge + stays for top alignment, its center for center, its left edge for bottom." [content] - (if (wm/vertical-text-content? content) - (case (:vertical-align content) "center" 0.5 "bottom" 0 1) - (case (:vertical-align content) "center" 0.5 "bottom" 1 0))) + (case (:vertical-align content) "center" 0.5 "bottom" 0 1)) (defn resize-origin - "Keep the text's block alignment anchored when its measured size changes." + "Point that stays put when a text's measured size changes. Vertical text + keeps the block-axis edge its alignment sets; horizontal text keeps its + top-left corner, as the WASM renderer does." [{:keys [points content]}] - (gpt/lerp (nth points 0) - (nth points (if (wm/vertical-text-content? content) 1 3)) - (block-anchor content))) + (if (wm/vertical-text-content? content) + (gpt/lerp (nth points 0) (nth points 1) (block-anchor content)) + (nth points 0))) (defn live-rect "Bounds of live editor content, before its geometry is committed." [selrect grow-type content dimension] (if-let [{:keys [width height]} (resolve-size selrect grow-type content dimension)] - (let [vertical? (wm/vertical-text-content? content) - anchor (block-anchor content)] - (assoc selrect - :x (+ (:x selrect) (if vertical? (* anchor (- (:width selrect) width)) 0)) - :y (+ (:y selrect) (if vertical? 0 (* anchor (- (:height selrect) height)))) - :width width - :height height)) + (cond-> (assoc selrect :width width :height height) + (wm/vertical-text-content? content) + (update :x + (* (block-anchor content) (- (:width selrect) width)))) selrect)) diff --git a/frontend/test/frontend_tests/data/workspace_texts_test.cljs b/frontend/test/frontend_tests/data/workspace_texts_test.cljs index 32f683ef7e..8c8955a3f3 100644 --- a/frontend/test/frontend_tests/data/workspace_texts_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_texts_test.cljs @@ -142,18 +142,23 @@ (t/is (= 50 (:y shape))) (t/is (= [4 17] [(:width shape) (:height shape)])))) -(t/deftest horizontal-auto-grow-keeps-the-block-anchor - (doseq [[align expected-y] [["top" 20] ["center" -60] ["bottom" -140]]] - (let [shape (cts/setup-shape - {:type :text :x 10 :y 20 :width 100 :height 200 - :grow-type :auto-height - :content (assoc (writing-mode-content "horizontal-tb") :vertical-align align)}) +(t/deftest horizontal-auto-grow-keeps-the-top-left-corner + (doseq [align ["top" "center" "bottom"]] + (let [content (assoc (writing-mode-content "horizontal-tb") :vertical-align align) + shape (cts/setup-shape + {:type :text :x 10 :y 20 :width 100 :height 200 + :grow-type :auto-height + :content content}) resized (with-redefs [dwwt/get-wasm-text-new-size (fn ([_] {:width 100 :height 360}) ([_ _] {:width 100 :height 360}))] (let [modifiers (dwwt/resize-wasm-text-modifiers shape)] (gsh/transform-shape shape (get-in modifiers [(:id shape) :modifiers]))))] - (t/is (= expected-y (:y resized)) align)))) + (t/is (= [10 20 360] [(:x resized) (:y resized) (:height resized)]) align) + (t/is (= {:x 10 :y 20 :width 100 :height 360} + (text.geom/live-rect {:x 10 :y 20 :width 100 :height 200} :auto-height + content {:width 100 :height 360})) + align)))) (t/deftest vertical-grow-type-resize-axes-are-remapped (t/is (= :auto-height diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 51426f436f..4fd138c74c 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -1775,6 +1775,9 @@ impl RenderState { text_content, fills_surface_id, strokes_surface_id, + innershadows_surface_id, + text_drop_shadows_surface_id, + parent_shadows.as_deref(), skip_effects, )?; } else { diff --git a/render-wasm/src/render/text.rs b/render-wasm/src/render/text.rs index 79100f208e..4ce89374fb 100644 --- a/render-wasm/src/render/text.rs +++ b/render-wasm/src/render/text.rs @@ -24,12 +24,19 @@ use text_vertical::GlyphOutput; /// one layout. Rebinds the content to the selrect: stored text bounds /// describe the measured content and can be taller than a fixed shape, whose /// height is the column-wrap budget. +/// +/// With `parent_shadows` (a container's drop-shadow pass) only the shadows +/// of the text silhouette are painted. +#[allow(clippy::too_many_arguments)] pub fn render_vertical_text( state: &mut RenderState, shape: &Shape, text_content: &TextContent, fills_surface_id: SurfaceId, strokes_surface_id: SurfaceId, + innershadows_surface_id: SurfaceId, + text_drop_shadows_surface_id: SurfaceId, + parent_shadows: Option<&[Paint]>, skip_effects: bool, ) -> Result<()> { let blur_filter = (!skip_effects).then(|| shape.image_filter(1.0)).flatten(); @@ -37,6 +44,27 @@ pub fn render_vertical_text( let vertical_align = shape.vertical_align(); let layout = text_content.vertical_layout(&bounds); + if let Some(parent_shadows) = parent_shadows { + if !state.should_skip_drop_shadows() { + let strokes: Vec<&Stroke> = shape.visible_strokes().collect(); + let canvas = state + .surfaces + .canvas_and_mark_dirty(text_drop_shadows_surface_id); + for shadow in parent_shadows { + text_vertical::paint_shadow_silhouette( + canvas, + &layout, + &bounds, + vertical_align, + shadow, + &strokes, + GlyphOutput::Text, + ); + } + } + return Ok(()); + } + let skip_shadows = skip_effects || state.should_skip_drop_shadows(); if !skip_shadows { let mut drop_shadows = shape.drop_shadow_paints(); @@ -46,7 +74,7 @@ pub fn render_vertical_text( if !drop_shadows.is_empty() { let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id); for shadow in &drop_shadows { - text_vertical::paint_drop_shadow( + text_vertical::paint_shadow( canvas, &layout, &bounds, @@ -71,6 +99,7 @@ pub fn render_vertical_text( &layout, &bounds, blur_filter.as_ref(), + None, GlyphOutput::Text, ) }, @@ -94,6 +123,25 @@ pub fn render_vertical_text( } } + if !skip_effects { + let inner_shadows = shape.inner_shadow_paints(); + if !inner_shadows.is_empty() { + let canvas = state + .surfaces + .canvas_and_mark_dirty(innershadows_surface_id); + for shadow in &inner_shadows { + text_vertical::paint_shadow( + canvas, + &layout, + &bounds, + vertical_align, + shadow, + GlyphOutput::Text, + ); + } + } + } + if state.options.is_text_grid_visible() { let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id); text_vertical::paint_grid(canvas, &layout, &bounds, vertical_align); @@ -418,6 +466,7 @@ pub fn paint_vertical_fill( layout: &text_vertical::VerticalLayout, bounds: &Rect, blur: Option<&ImageFilter>, + single_paint: Option<&Paint>, output: GlyphOutput, ) { if let Some(blur_filter) = blur { @@ -429,7 +478,17 @@ pub fn paint_vertical_fill( .paint(&blur_paint), ); } - text_vertical::paint_layout(canvas, layout, bounds, shape.vertical_align(), output); + match single_paint { + Some(paint) => text_vertical::paint_glyphs( + canvas, + layout, + bounds, + shape.vertical_align(), + paint, + output, + ), + None => text_vertical::paint_layout(canvas, layout, bounds, shape.vertical_align(), output), + } if blur.is_some() { canvas.restore(); } diff --git a/render-wasm/src/render/vector.rs b/render-wasm/src/render/vector.rs index b1d2bf662d..c00930a05c 100644 --- a/render-wasm/src/render/vector.rs +++ b/render-wasm/src/render/vector.rs @@ -84,7 +84,7 @@ impl<'a> VectorRenderer<'a> { let output = crate::render::svg::vertical_glyph_output(); for shadow in &shape.drop_shadow_paints() { - text_vertical::paint_drop_shadow( + text_vertical::paint_shadow( self.canvas, &layout, &bounds, @@ -93,7 +93,22 @@ impl<'a> VectorRenderer<'a> { output, ); } - text::paint_vertical_fill(self.canvas, shape, &layout, &bounds, blur_filter, output); + // Inside a container's drop-shadow pass the glyphs paint opaque. + let silhouette = self.silhouette.map(|_| { + let mut paint = Paint::default(); + paint.set_color(skia::Color::BLACK); + paint.set_anti_alias(true); + paint + }); + text::paint_vertical_fill( + self.canvas, + shape, + &layout, + &bounds, + blur_filter, + silhouette.as_ref(), + output, + ); let selrect = shape.selrect(); for stroke in shape.visible_strokes().rev() { text_vertical::paint_stroke( diff --git a/render-wasm/src/shapes/text_vertical/mod.rs b/render-wasm/src/shapes/text_vertical/mod.rs index e8b5cab249..c4e05eb40d 100644 --- a/render-wasm/src/shapes/text_vertical/mod.rs +++ b/render-wasm/src/shapes/text_vertical/mod.rs @@ -42,7 +42,7 @@ pub(crate) mod test_support; pub(crate) use layout::CellKind; pub use layout::{block_axis_offset, layout_for_rect, VerticalLayout}; pub use paint::{ - paint_drop_shadow, paint_grid, paint_layout, paint_stroke, paint_text_vertical, - paint_text_vertical_with, vertical_text_paths, GlyphOutput, + paint_glyphs, paint_grid, paint_layout, paint_shadow, paint_shadow_silhouette, paint_stroke, + paint_text_vertical, paint_text_vertical_with, vertical_text_paths, GlyphOutput, }; pub use positions::{caret_from_point, caret_rect, intersects, position_data, range_rects}; diff --git a/render-wasm/src/shapes/text_vertical/paint.rs b/render-wasm/src/shapes/text_vertical/paint.rs index 40df7f4c1c..9362977fba 100644 --- a/render-wasm/src/shapes/text_vertical/paint.rs +++ b/render-wasm/src/shapes/text_vertical/paint.rs @@ -413,7 +413,7 @@ pub fn paint_layout( /// Paint every glyph and decoration with one `paint` (stroke / shadow /// silhouette passes). -fn paint_glyphs( +pub fn paint_glyphs( canvas: &Canvas, layout: &VerticalLayout, bounds: &Rect, @@ -577,11 +577,11 @@ pub fn paint_grid(canvas: &Canvas, layout: &VerticalLayout, bounds: &Rect, align } } -/// Paint the vertical glyph shadows. The shadow paint carries the -/// blur/offset image filter, so glyphs draw through it with no offscreen -/// layer (a `save_layer` with the filter can exceed GPU limits on tall -/// columns). -pub fn paint_drop_shadow( +/// Paint a drop or inner shadow of the vertical glyphs. The shadow paint +/// carries the shadow image filter, so glyphs draw through it with no +/// offscreen layer (a `save_layer` with the filter can exceed GPU limits on +/// tall columns). +pub fn paint_shadow( canvas: &Canvas, layout: &VerticalLayout, bounds: &Rect, @@ -595,6 +595,34 @@ pub fn paint_drop_shadow( paint_glyphs(canvas, layout, bounds, vertical_align, &paint, output); } +/// Paint a container's drop shadow over the vertical glyphs: the glyph fill +/// plus the outside reach of every center and outer stroke, through one +/// shadow paint. +pub fn paint_shadow_silhouette( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + shadow_paint: &Paint, + strokes: &[&Stroke], + output: GlyphOutput, +) { + paint_shadow(canvas, layout, bounds, vertical_align, shadow_paint, output); + for stroke in strokes { + let width = match stroke.kind { + StrokeKind::Inner => continue, + StrokeKind::Center => stroke.width, + StrokeKind::Outer => stroke.width * 2.0, + }; + let mut paint = shadow_paint.clone(); + paint.set_color(skia::Color::BLACK); + paint.set_anti_alias(true); + paint.set_style(skia::PaintStyle::Stroke); + paint.set_stroke_width(width); + paint_glyphs(canvas, layout, bounds, vertical_align, &paint, output); + } +} + /// Paint a stroke on the vertical glyphs. Center strokes draw directly; /// inner/outer strokes mask with `SrcIn` / `SrcOut` against the glyph /// silhouette, as the horizontal path does. @@ -993,6 +1021,43 @@ mod tests { assert!(inked_pixels(&mut surface, strip) > 0, "the ruby is painted"); } + #[test] + fn shadow_silhouette_widens_with_center_and_outer_strokes_only() { + let content = make_content_with_spans(&["く"], 400.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let bounds = content.bounds(); + let inked = |strokes: &[&Stroke]| { + let mut surface = skia::surfaces::raster_n32_premul((256, 512)).unwrap(); + paint_shadow_silhouette( + surface.canvas(), + &layout, + &bounds, + VerticalAlign::Top, + &Paint::default(), + strokes, + GlyphOutput::Text, + ); + inked_pixels(&mut surface, skia::Rect::from_wh(256.0, 512.0)) + }; + let inner = Stroke::new_inner_stroke(4.0, StrokeStyle::Solid, None, None, None, None); + let center = Stroke::new_center_stroke(4.0, StrokeStyle::Solid, None, None, None, None); + let outer = Stroke::new_outer_stroke(4.0, StrokeStyle::Solid, None, None, None, None); + + let fill = inked(&[]); + assert!(fill > 0, "the glyph fill is painted"); + assert_eq!( + inked(&[&inner]), + fill, + "an inner stroke stays inside the glyph" + ); + let centered = inked(&[¢er]); + assert!(centered > fill, "a center stroke reaches outside the glyph"); + assert!( + inked(&[&outer]) > centered, + "an outer stroke reaches its full width out" + ); + } + #[test] fn paint_passes_do_not_panic() { let content = decorated_content("あいAB。", TextDecoration::LINE_THROUGH); @@ -1015,7 +1080,7 @@ mod tests { VerticalAlign::Top, GlyphOutput::Text, ); - paint_drop_shadow( + paint_shadow( canvas, &layout, &bounds, @@ -1034,7 +1099,7 @@ mod tests { None, None, )); - paint_drop_shadow( + paint_shadow( canvas, &layout, &bounds,