about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-06-29 12:39:23 +0200
committerbptato <nincsnevem662@gmail.com>2024-06-29 12:51:08 +0200
commitc4e1b4253d4731c91a6112f353c37a6a056b9f59 (patch)
treef3575bd805d10a671d2f19c9987b6ad59c2633a1 /src
parent2e50aa23237da76802d2a61cb7426bf51c122d14 (diff)
downloadchawan-c4e1b4253d4731c91a6112f353c37a6a056b9f59.tar.gz
layout: clamp image size to available space
reduces images spilling out of their containers
Diffstat (limited to 'src')
-rw-r--r--src/layout/engine.nim19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index f0513644..7fea2289 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -1375,22 +1375,31 @@ proc addInlineImage(ictx: var InlineContext; state: var InlineState;
   let atom = InlineAtom(
     t: iatImage,
     bmp: bmp,
-    size: size(w = int(bmp.width), h = int(bmp.height)), #TODO overflow
+    size: size(w = int(bmp.width), h = int(bmp.height)) #TODO overflow
   )
   let computed = state.fragment.computed
   let lctx = ictx.lctx
-  if computed{"width"}.canpx(ictx.space.w):
+  let hasWidth = computed{"width"}.canpx(ictx.space.w)
+  let hasHeight = computed{"height"}.canpx(ictx.space.h)
+  if hasWidth:
     let w = computed{"width"}.spx(lctx, ictx.space.w, computed, padding)
-    if not computed{"height"}.canpx(ictx.space.h):
+    if not hasHeight:
       # maintain aspect ratio
       atom.size.h = atom.size.h div atom.size.w * w
     atom.size.w = w
-  if computed{"height"}.canpx(ictx.space.h):
+  if hasHeight:
     let h = computed{"height"}.spx(lctx, ictx.space.h, computed, padding)
-    if not computed{"width"}.canpx(ictx.space.w):
+    if not hasWidth:
       # maintain aspect ratio
       atom.size.w = atom.size.w div atom.size.h * h
     atom.size.h = h
+  if not hasWidth and not hasHeight:
+    if ictx.space.w.isDefinite() and atom.size.w > ictx.space.w.u:
+      atom.size.h = atom.size.h div atom.size.w * ictx.space.w.u
+      atom.size.w = ictx.space.w.u
+    if ictx.space.h.isDefinite() and atom.size.h > ictx.space.h.u:
+      atom.size.w = atom.size.w div atom.size.h * ictx.space.h.u
+      atom.size.h = ictx.space.h.u
   let iastate = InlineAtomState(
     vertalign: state.fragment.computed{"vertical-align"},
     baseline: atom.size.h