From c4e1b4253d4731c91a6112f353c37a6a056b9f59 Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 29 Jun 2024 12:39:23 +0200 Subject: layout: clamp image size to available space reduces images spilling out of their containers --- src/layout/engine.nim | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/layout/engine.nim') 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 -- cgit 1.4.1-2-gfad0 n value='author'>author
path: root/util.c
blob: d8e661256ee38f37a6a6adc41462d95395263a89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95