summary refs log tree commit diff stats
path: root/ranger/ext
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-01-08 17:35:56 +0100
committerhut <hut@lavabit.com>2010-01-08 17:35:56 +0100
commitb4c2c703f9acdf39cbd47407ed51a7e7c807ba7d (patch)
tree9514abeea9f9e8cc2e5e5919e88ad4b96cefa3d1 /ranger/ext
parentbba8d293c32050b79cfc2b0ac2807f324ef44800 (diff)
downloadranger-b4c2c703f9acdf39cbd47407ed51a7e7c807ba7d.tar.gz
added license information
Diffstat (limited to 'ranger/ext')
-rw-r--r--ranger/ext/__init__.py14
-rw-r--r--ranger/ext/accumulator.py14
-rw-r--r--ranger/ext/command_parser.py14
-rw-r--r--ranger/ext/debug.py14
-rw-r--r--ranger/ext/get_all_modules.py14
-rw-r--r--ranger/ext/human_readable.py14
-rw-r--r--ranger/ext/move.py14
-rw-r--r--ranger/ext/openstruct.py14
-rw-r--r--ranger/ext/relpath.py14
-rw-r--r--ranger/ext/waitpid_no_intr.py14
10 files changed, 140 insertions, 0 deletions
diff --git a/ranger/ext/__init__.py b/ranger/ext/__init__.py
index 9cf2ee50..497c5da0 100644
--- a/ranger/ext/__init__.py
+++ b/ranger/ext/__init__.py
@@ -1 +1,15 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 """This package includes extensions with broader usability"""
diff --git a/ranger/ext/accumulator.py b/ranger/ext/accumulator.py
index 5aee54de..95d0f4d0 100644
--- a/ranger/ext/accumulator.py
+++ b/ranger/ext/accumulator.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 class Accumulator(object):
 	def __init__(self):
 		self.pointer = 0
diff --git a/ranger/ext/command_parser.py b/ranger/ext/command_parser.py
index 432f0364..4224d52b 100644
--- a/ranger/ext/command_parser.py
+++ b/ranger/ext/command_parser.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 class LazyParser(object):
 	"""Parse commands and extract information"""
 	def __init__(self, line):
diff --git a/ranger/ext/debug.py b/ranger/ext/debug.py
index 9a4dcbc0..989f3060 100644
--- a/ranger/ext/debug.py
+++ b/ranger/ext/debug.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 LOGFILE = '/tmp/errorlog'
 
 def log(*objects, **keywords):
diff --git a/ranger/ext/get_all_modules.py b/ranger/ext/get_all_modules.py
index 2d98278b..918bce02 100644
--- a/ranger/ext/get_all_modules.py
+++ b/ranger/ext/get_all_modules.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 def get_all_modules(dirname):
 	"""returns a list of strings containing the names of modules in a directory"""
 	import os
diff --git a/ranger/ext/human_readable.py b/ranger/ext/human_readable.py
index 4afa03cb..410138b7 100644
--- a/ranger/ext/human_readable.py
+++ b/ranger/ext/human_readable.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 ONE_KB = 1024
 UNITS = 'BKMGTP'
 MAX_EXPONENT = len(UNITS) - 1
diff --git a/ranger/ext/move.py b/ranger/ext/move.py
index 5909a876..2223f9fa 100644
--- a/ranger/ext/move.py
+++ b/ranger/ext/move.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 def move_between(current, minimum, maximum, relative=0, absolute=None):
 	i = current
 	if isinstance(absolute, int):
diff --git a/ranger/ext/openstruct.py b/ranger/ext/openstruct.py
index 0a899de6..eb8d49a9 100644
--- a/ranger/ext/openstruct.py
+++ b/ranger/ext/openstruct.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 class OpenStruct(object):
 	def __init__(self, __dictionary=None, **__keywords):
 		if __dictionary:
diff --git a/ranger/ext/relpath.py b/ranger/ext/relpath.py
index 2d633e7a..ad2db809 100644
--- a/ranger/ext/relpath.py
+++ b/ranger/ext/relpath.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 import os
 import ranger
 
diff --git a/ranger/ext/waitpid_no_intr.py b/ranger/ext/waitpid_no_intr.py
index c2063313..0bd99b49 100644
--- a/ranger/ext/waitpid_no_intr.py
+++ b/ranger/ext/waitpid_no_intr.py
@@ -1,3 +1,17 @@
+# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 def waitpid_no_intr(pid):
 	"""catch interrupts which occur while using os.waitpid"""
 	import os, errno
vious revision' href='/ahoang/chawan/blame/src/layout/engine.nim?id=876f5e9c64992ffef8630cd2bafa8c5ba9431f01'>^
270628aa ^

fb017f27 ^
ca5aea89 ^




fb017f27 ^


270628aa ^
f4805eb8 ^
ca5aea89 ^
5c19adf2 ^




ca5aea89 ^
5c19adf2 ^





be10c6a2 ^
5c19adf2 ^
be10c6a2 ^
5c19adf2 ^
be10c6a2 ^
5c19adf2 ^
ca5aea89 ^
5c19adf2 ^










5c19adf2 ^

be10c6a2 ^
5c19adf2 ^





a42f0b16 ^
5c19adf2 ^



















a42f0b16 ^
5c19adf2 ^










a42f0b16 ^
5c19adf2 ^



be10c6a2 ^
a42f0b16 ^
fb017f27 ^

be10c6a2 ^
fb017f27 ^


ca5aea89 ^

fb017f27 ^



ca5aea89 ^
5c19adf2 ^
ca5aea89 ^
5c19adf2 ^
a42f0b16 ^
5c19adf2 ^
d3692eb6 ^

5c19adf2 ^
be10c6a2 ^
d3692eb6 ^
ca5aea89 ^

5c19adf2 ^
ca5aea89 ^
5c19adf2 ^
ca5aea89 ^

fb017f27 ^
d3692eb6 ^
ca5aea89 ^
7a2cda0e ^
ca5aea89 ^
7a2cda0e ^
ca5aea89 ^
d3692eb6 ^
ef37b111 ^
ca5aea89 ^
a42f0b16 ^
ef37b111 ^
fb017f27 ^
d3692eb6 ^
be10c6a2 ^
ca5aea89 ^
5148f88d ^

fb017f27 ^



5148f88d ^
bcaaf4e6 ^
270628aa ^
b4798a6b ^
b4798a6b ^
d97776fb ^
ca5aea89 ^
b4798a6b ^
f4805eb8 ^
d3692eb6 ^
ca5aea89 ^


f4805eb8 ^

ca5aea89 ^
b4798a6b ^
ca5aea89 ^

d3692eb6 ^
d97776fb ^
d3692eb6 ^
ca5aea89 ^
fb017f27 ^
d3692eb6 ^
d97776fb ^
ca5aea89 ^
fb017f27 ^
d3692eb6 ^
f4805eb8 ^
d3692eb6 ^

ca5aea89 ^
b4798a6b ^
fb017f27 ^
d3692eb6 ^

ca5aea89 ^
d3692eb6 ^
fb017f27 ^
f4805eb8 ^
ca5aea89 ^
f4805eb8 ^
ca5aea89 ^
270628aa ^

06696ec0 ^
ca5aea89 ^
270628aa ^
e6f7cc72 ^
b4798a6b ^

d3692eb6 ^
e6f7cc72 ^
7a2cda0e ^
d3692eb6 ^
d3d9139d ^
e6f7cc72 ^
d3692eb6 ^
d3d9139d ^
d3692eb6 ^

d97776fb ^
dd58218f ^
b4798a6b ^
bc1aceb8 ^
be10c6a2 ^


b9c13c00 ^

be10c6a2 ^








270628aa ^
be10c6a2 ^
e1194507 ^
be10c6a2 ^
5148f88d ^
b9c13c00 ^

be10c6a2 ^

876f5e9c ^
be10c6a2 ^



876f5e9c ^
be10c6a2 ^


876f5e9c ^
be10c6a2 ^

5148f88d ^
be10c6a2 ^
c63d3f65 ^
d97776fb ^

be10c6a2 ^
d97776fb ^
be10c6a2 ^


ca5aea89 ^
be10c6a2 ^





b9c13c00 ^

be10c6a2 ^

c63d3f65 ^
7addf30a ^

ca5aea89 ^
7addf30a ^

402c01d8 ^
5148f88d ^
7addf30a ^
bcaaf4e6 ^
402c01d8 ^
5148f88d ^
ca5aea89 ^
b4798a6b ^
7addf30a ^


ca5aea89 ^
7addf30a ^
be10c6a2 ^
7addf30a ^
ca5aea89 ^
bcaaf4e6 ^
be10c6a2 ^
8efeab3d ^
be10c6a2 ^
8efeab3d ^
be10c6a2 ^
b4798a6b ^
d3692eb6 ^
ca5aea89 ^
be10c6a2 ^
ca5aea89 ^
d3692eb6 ^
b4798a6b ^

b4798a6b ^
7a2cda0e ^
5c19adf2 ^
b4798a6b ^
b9c13c00 ^
876f5e9c ^
b6b4e896 ^
876f5e9c ^

b9c13c00 ^
876f5e9c ^




ca5aea89 ^
fb017f27 ^


876f5e9c ^
b9c13c00 ^
7addf30a ^
be10c6a2 ^
87a47d56 ^
71b19f37 ^





b9c13c00 ^








402c01d8 ^
7addf30a ^
be10c6a2 ^
8544e90b ^
2f0ea08d ^
be10c6a2 ^
b9c13c00 ^
be10c6a2 ^


8544e90b ^
ca5aea89 ^

be10c6a2 ^




87a47d56 ^
be10c6a2 ^


87a47d56 ^
be10c6a2 ^




87a47d56 ^
be10c6a2 ^


87a47d56 ^
402c01d8 ^
5148f88d ^
b4798a6b ^
ca5aea89 ^
876f5e9c ^
ca5aea89 ^
876f5e9c ^


ca5aea89 ^
876f5e9c ^
a42f0b16 ^
876f5e9c ^
b4798a6b ^

ca5aea89 ^
b4798a6b ^

ca5aea89 ^
04e33d80 ^
402c01d8 ^
d3692eb6 ^
402c01d8 ^
04e33d80 ^
be10c6a2 ^

ca5aea89 ^
b9c13c00 ^
d3692eb6 ^
ca5aea89 ^
bcaaf4e6 ^
ca5aea89 ^
876f5e9c ^


ca5aea89 ^
876f5e9c ^

7addf30a ^
b4798a6b ^
a42f0b16 ^

ca5aea89 ^
a42f0b16 ^
402c01d8 ^
a42f0b16 ^
402c01d8 ^
a42f0b16 ^
402c01d8 ^
be10c6a2 ^
ca5aea89 ^
be10c6a2 ^
a42f0b16 ^
ca5aea89 ^

a42f0b16 ^
25368137 ^

b9c13c00 ^
7addf30a ^
b9c13c00 ^
7addf30a ^
b9c13c00 ^
7addf30a ^
b9c13c00 ^

























































b4798a6b ^
402c01d8 ^
b4798a6b ^
b9c13c00 ^
ca5aea89 ^
b9c13c00 ^




b4798a6b ^
402c01d8 ^
b9c13c00 ^
402c01d8 ^
be10c6a2 ^
bcaaf4e6 ^
b9c13c00 ^
bcaaf4e6 ^
b9c13c00 ^
402c01d8 ^
b9c13c00 ^

be10c6a2 ^
402c01d8 ^
b9c13c00 ^
402c01d8 ^
402c01d8 ^
402c01d8 ^


ca5aea89 ^
7addf30a ^




ca5aea89 ^

f2a49241 ^
ca5aea89 ^

f2a49241 ^
ca5aea89 ^
be10c6a2 ^
ca5aea89 ^

be10c6a2 ^
402c01d8 ^
b4798a6b ^
352c2819 ^
ca5aea89 ^
b4798a6b ^
ca5aea89 ^
87a47d56 ^
87a47d56 ^
ca5aea89 ^
f2a49241 ^
368794ad ^
7addf30a ^

2496c524 ^
ca5aea89 ^
7addf30a ^
368794ad ^

7addf30a ^

368794ad ^
7addf30a ^
368794ad ^
7addf30a ^
368794ad ^
7addf30a ^
368794ad ^

eeebe788 ^
402c01d8 ^
8cd503c8 ^
ca5aea89 ^

8cd503c8 ^
6ff61c5a ^
8cd503c8 ^
7b06c22d ^
b9c13c00 ^
402c01d8 ^
ca5aea89 ^
402c01d8 ^



7b06c22d ^
402c01d8 ^

ca5aea89 ^
402c01d8 ^

b4798a6b ^
8cd503c8 ^
7addf30a ^
8cd503c8 ^






6ff61c5a ^
8cd503c8 ^
7addf30a ^
8cd503c8 ^

7addf30a ^

8cd503c8 ^


402c01d8 ^
368794ad ^
402c01d8 ^

8cd503c8 ^
7a2cda0e ^
402c01d8 ^
ca5aea89 ^
8cd503c8 ^
402c01d8 ^



8cd503c8 ^
402c01d8 ^
368794ad ^

8cd503c8 ^


368794ad ^
8cd503c8 ^
402c01d8 ^
8cd503c8 ^
7cdb8c1b ^
8cd503c8 ^
402c01d8 ^


8cd503c8 ^
8cd503c8 ^
402c01d8 ^

368794ad ^
402c01d8 ^
8cd503c8 ^


b6b4e896 ^
368794ad ^
8cd503c8 ^

5148f88d ^
8cd503c8 ^
7cdb8c1b ^
8cd503c8 ^
b4798a6b ^
402c01d8 ^
be10c6a2 ^





8cd503c8 ^
7b06c22d ^
b4798a6b ^
402c01d8 ^
8cd503c8 ^

b9c13c00 ^
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
           
              
              
 

                     
                
               
              

                   
 

             
                                        
                                                                                    
                                      
 
                                                          

                                                        
                                                                  

                                                    
                                                          
                                                         
 


                                                               
                         
                     
            
            

                  
                             
 

                                                                  
 


                                                    
                                                     



                                                     

                                                      
                          

                          
                                                                          
                            

                                                            
                      
                                                  
                                               
                                                           

          

                                                                              
                              
                                                                  
 

                                      
                               




                                                     


                            
                   
 
                                                                                                                        




                                                                       
                             





                                                
                        
                       
                                                                                     
                          
                        
                        
                                               










                                                                

                              
                                   





                                          
 



















                                                                       
         










                                                                       
       



                              
                         
 

                                                                                        
                              


                            

                                                                        



                                                                
                                                                                                
                                          
                                  
                           
 
                             

                                
                                                    
                                                                                
 

                                                                              
                       
                                                                          
 

                                                                                                 
                        
                 
                                
                                                          
                                               
                            
                                         
 
                                        
                                               
 
                 
                                                                  
 
                                       
                                                                                                      

                                                               



                                           
                                
 
                                      
                          
                         
                                       
                                                            
                   
 
                                                     


                                                                                 

                                                 
                                                                          
          

                                                     
                          
                                                                                                               
                     
                                                                 
                                  
                         
                                                                                                               
                                                                 
                                  
               
 

                                                         
                                    
                                          
                                                               

                                                              
                                                          
         
                                  
 
                                                                                                            
                        
                           

                           
                   
                                            
                 
 

                                
           
                    
                             
                                     
           
         
                 
                           

                         
                                                          
 
                 
 


                                 

                    








                                                                                                                                 
                 
                            
       
                                                 
 

                                                                   

                                                                    
 



                                                                        
 


                                           
 

                                            
 
                                  
                      

                                                                  
                                                   
                       


                                                                                  
                                                                                  





                                                

                                              

                                            
 

                                                                                                    
                              

                                                                  
                                                                                             
             
                                             
 
                                                                                
                                              
                                                                 
 


                                                                               
                                                                 
 
                                                                                                
                                                          
                                               
 
                                                                                        
             
                                                     
 
                                                                              
             
                            
                                
                                                                  
                                               
 

                                                          
                              
                                 
                             
 
                                          
                                 
                                           

                                 
                                          




                                    
                                 


                                                
 
                                                                         
                                                                              
                                                                         
 





                                                    








                                                                                  
                                                                                                                      
                               
                                              
 
                                    
                               
                                                        


                                                                     
 

                                                                                                                            




                                                                                         
 


                                    
 




                                            
 


                                                          
 
                                                             
                        
                 
                                                     
 
                                                                                 


                                                    
                                                                                   
                      
                                                                                 
 

                                
                                                                     

                            
                                  
                      
                                         
                           
                             
                            

                                                         
                                                              
                                
         
                                                              
 
                                                                                     


                                                                                              
                                                                                   

                                        
                                                                                
                                    

                         
                                    
                        
                                           
                         
                               
                              
                                                
                                                       
                                                            
                              
           

                                                                
 

             
                                                                                 
                                      
                                                                    
                                  
                                                      
       

























































                                                                              
 
                                                                           
                      
                            
                                  




                                                                                 
 
                                                                 
                                                                           
                      
                                      
                      
                                          
       
                                                   
 

                                                                             
                                        
                      
                                          
       
                                              


                  
                                                                      




                                                                                                        

                                                                 
             

                                             
 
                                                                            
             

                                                  
 
                                                    
             
                            
                                                    
 
                                                                
             
                            
                                                
 
                                                                                        

                            
                                             
                                            
 

                                                     

                           
                                                                   
                        
                                                                              
 
                                                                                            
             

                                                         
 
                                                                  
                                                                                                              

                                                                                            
                            
 
                                                                                  
 
                                                         
                        
                                                        



                              
 

                       
                                                                             

                        
 
                                                                                                                           
 






                                                                                                                                                                                
 
                                     
                   

                                                        

                              


                                                                       
                              
                       

                    
                                                             
                          
              
                                                  
                                                            



                            
                                                                                                                             
                                  

                                                         


                                   
                                                                              
                   
                     
                                              
                                   
                               


            
                                                                                    
                                            

                                  
                                                         
  


                                   
                
                                                                              

                                                                          
                       
                                                
                                     
                                 
 
            





                                             
                                            
            
 
 

                                                                                  
                                                   
import math
import options
import unicode

import css/stylednode
import css/values
import html/tags
import html/dom
import io/term
import layout/box
import utils/twtstr

# Build phase

# p is what to use for percentage values
func cells_in(l: CSSLength, state: Viewport, d: int, p: Option[int], o: bool): int =
  return cells(l, d, state.term, p, o)

func cells_w(l: CSSLength, state: Viewport, p: int): int =
  return l.cells_in(state, state.term.ppc, p.some, true)

func cells_h(l: CSSLength, state: Viewport, p: Option[int]): int =
  return l.cells_in(state, state.term.ppl, p, false)

func cells_h(l: CSSLength, state: Viewport, p: int): int =
  return l.cells_in(state, state.term.ppl, p.some, false)

func px(l: CSSLength, state: Viewport, p = 0): int {.inline.} =
  return px(l, state.term, p)

type InlineState = object
  ictx: InlineContext
  skip: bool
  node: Node
  word: InlineWord
  maxwidth: int
  computed: CSSComputedValues

func whitespacepre(computed: CSSComputedValues): bool {.inline.} =
  computed{"white-space"} in {WHITESPACE_PRE, WHITESPACE_PRE_WRAP}

func cellwidth(viewport: Viewport): int {.inline.} =
  viewport.term.ppc

func cellwidth(ictx: InlineContext): int {.inline.} =
  ictx.viewport.cellwidth

func cellheight(viewport: Viewport): int {.inline.} =
  viewport.term.ppl

func cellheight(ictx: InlineContext): int {.inline.} =
  ictx.viewport.cellheight

# Whitespace between words
func computeShift(ictx: InlineContext, computed: CSSComputedValues): int =
  if ictx.whitespacenum > 0:
    if ictx.thisrow.atoms.len > 0 or computed.whitespacepre:
      let spacing = computed{"word-spacing"}
      if spacing.auto:
        return ictx.cellwidth * ictx.whitespacenum
      #return spacing.cells_w(ictx.viewport, 0)
      return spacing.px(ictx.viewport) * ictx.whitespacenum
  return 0

func computeLineHeight(viewport: Viewport, computed: CSSComputedValues): int =
  if computed{"line-height"}.auto:
    return viewport.cellheight
  return computed{"line-height"}.px(viewport, viewport.cellheight)

proc newWord(state: var InlineState) =
  let word = InlineWord()
  let format = ComputedFormat()
  let computed = state.computed
  format.color = computed{"color"}
  format.fontstyle = computed{"font-style"}
  format.fontweight = computed{"font-weight"}
  format.textdecoration = computed{"text-decoration"}
  format.node = state.node
  word.format = format
  state.ictx.format = format
  state.word = word

proc horizontalAlignRow(ictx: InlineContext, row: InlineRow, computed: CSSComputedValues, maxwidth: int, last = false) =
  let maxwidth = if ictx.shrink:
    ictx.maxwidth
  else:
    maxwidth
  # we don't support directions for now so left = start and right = end
  case computed{"text-align"}
  of TEXT_ALIGN_START, TEXT_ALIGN_LEFT:
    discard
  of TEXT_ALIGN_END, TEXT_ALIGN_RIGHT:
    # move everything
    let x = max(maxwidth, row.width) - row.width
    for atom in row.atoms:
      atom.offset.x += x
  of TEXT_ALIGN_CENTER:
    let x = max((max(maxwidth - row.offset.x, row.width)) div 2 - row.width div 2, 0)
    for atom in row.atoms:
      atom.offset.x += x
  of TEXT_ALIGN_JUSTIFY:
    if not computed.whitespacepre and not last:
      var sumwidth = 0
      var spaces = 0
      for atom in row.atoms:
        if atom of InlineSpacing:
          discard
        else:
          inc spaces
          sumwidth += atom.width
      dec spaces
      if spaces > 0:
        let spacingwidth = (ictx.maxwidth - sumwidth) div spaces
        row.width = 0
        for atom in row.atoms:
          atom.offset.x = row.width
          if atom of InlineSpacing:
            let atom = InlineSpacing(atom)
            atom.width = spacingwidth
          row.width += atom.width
  else:
    discard

proc verticalAlignRow(ictx: InlineContext) =
  let row = ictx.thisrow
  var baseline = if row.height < row.lineheight:
    let lines = row.lineheight div ictx.cellheight
    int(ceil(lines / 2)) * ictx.cellheight
  else:
    0

  # line-height is the minimum line height
  row.height = max(row.height, row.lineheight)

  for atom in row.atoms:
    case atom.vertalign.keyword
    of VERTICAL_ALIGN_BASELINE:
      let len = atom.vertalign.length.px(ictx.viewport, row.lineheight)
      baseline = max(baseline, atom.height + len)
    of VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_BOTTOM:
      row.height = max(atom.height, row.height)
    of VERTICAL_ALIGN_MIDDLE:
      baseline = max(baseline, atom.height div 2)
    else:
      baseline = max(baseline, atom.height)
  row.height = max(baseline, row.height)

  for atom in row.atoms:
    let diff = case atom.vertalign.keyword
    of VERTICAL_ALIGN_BASELINE:
      let len = atom.vertalign.length.px(ictx.viewport, row.lineheight)
      baseline - atom.height - len
    of VERTICAL_ALIGN_MIDDLE:
      baseline - atom.height div 2
    of VERTICAL_ALIGN_TOP:
      0
    of VERTICAL_ALIGN_BOTTOM:
      row.height - atom.height
    else:
      baseline - atom.height
    atom.offset.y += diff

proc addSpacing(row: InlineRow, width, height: int, format: ComputedFormat) {.inline.} =
  let spacing = InlineSpacing(width: width, height: height, format: format)
  spacing.offset.x = row.width
  row.width += spacing.width
  row.atoms.add(spacing)

proc flushWhitespace(ictx: InlineContext, computed: CSSComputedValues) =
  let shift = ictx.computeShift(computed)
  ictx.whitespacenum = 0
  if shift > 0:
    ictx.thisrow.addSpacing(shift, ictx.cellheight, ictx.format)

proc finishRow(ictx: InlineContext, computed: CSSComputedValues, maxwidth: int, force = false) =
  if ictx.thisrow.atoms.len != 0 or force:
    ictx.flushWhitespace(computed)
    ictx.verticalAlignRow()

    let oldrow = ictx.thisrow
    ictx.rows.add(oldrow)
    ictx.height += oldrow.height
    ictx.maxwidth = max(ictx.maxwidth, oldrow.width)
    ictx.thisrow = InlineRow(offset: Offset(y: oldrow.offset.y + oldrow.height))

proc finish(ictx: InlineContext, computed: CSSComputedValues, maxwidth: int) =
  ictx.finishRow(computed, maxwidth)
  for row in ictx.rows:
    ictx.horizontalAlignRow(row, computed, maxwidth, row == ictx.rows[^1])

proc addAtom(ictx: InlineContext, atom: InlineAtom, maxwidth: int, computed: CSSComputedValues) =
  var shift = ictx.computeShift(computed)
  ictx.whitespacenum = 0
  # Line wrapping
  if not computed.whitespacepre:
    if ictx.thisrow.width + atom.width + shift > maxwidth:
      ictx.finishRow(computed, maxwidth, false)
      # Recompute on newline
      shift = ictx.computeShift(computed)

  if atom.width > 0 and atom.height > 0:
    atom.vertalign = computed{"vertical-align"}

    if shift > 0:
      ictx.thisrow.addSpacing(shift, ictx.cellheight, ictx.format)

    atom.offset.x += ictx.thisrow.width
    ictx.thisrow.lineheight = max(ictx.thisrow.lineheight, computeLineHeight(ictx.viewport, computed))
    ictx.thisrow.width += atom.width
    ictx.thisrow.height = max(ictx.thisrow.height, atom.height)
    if atom of InlineWord:
      ictx.format = InlineWord(atom).format
    else:
      ictx.format = nil
    ictx.thisrow.atoms.add(atom)

proc addWord(state: var InlineState) =
  if state.word.str != "":
    var word = state.word
    word.height = state.ictx.cellheight
    state.ictx.addAtom(word, state.maxwidth, state.computed)
    state.newWord()

# Start a new line, even if the previous one is empty
proc flushLine(ictx: InlineContext, computed: CSSComputedValues, maxwidth: int) =
  ictx.thisrow.lineheight = computeLineHeight(ictx.viewport, computed)
  ictx.finishRow(computed, maxwidth, true)

proc checkWrap(state: var InlineState, r: Rune) =
  if state.computed{"white-space"} in {WHITESPACE_NOWRAP, WHITESPACE_PRE}:
    return
  let shift = state.ictx.computeShift(state.computed)
  case state.computed{"word-break"}
  of WORD_BREAK_BREAK_ALL:
    if state.ictx.thisrow.width + state.word.width + shift + r.width() * state.ictx.cellwidth > state.maxwidth:
      state.addWord()
      state.ictx.finishRow(state.computed, state.maxwidth, false)
      state.ictx.whitespacenum = 0
  of WORD_BREAK_KEEP_ALL:
    if state.ictx.thisrow.width + state.word.width + shift + r.width() * state.ictx.cellwidth > state.maxwidth:
      state.ictx.finishRow(state.computed, state.maxwidth, false)
      state.ictx.whitespacenum = 0
  else: discard

proc processWhitespace(state: var InlineState, c: char) =
  state.addWord()
  case state.computed{"white-space"}
  of WHITESPACE_NORMAL, WHITESPACE_NOWRAP:
    state.ictx.whitespacenum = max(state.ictx.whitespacenum, 1)
  of WHITESPACE_PRE_LINE, WHITESPACE_PRE, WHITESPACE_PRE_WRAP:
    if c == '\n':
      state.ictx.flushLine(state.computed, state.maxwidth)
    else:
      inc state.ictx.whitespacenum

proc renderText*(ictx: InlineContext, str: string, maxwidth: int, computed: CSSComputedValues, node: Node) =
  var state: InlineState
  state.computed = computed
  state.ictx = ictx
  state.maxwidth = maxwidth
  state.node = node
  state.ictx.flushWhitespace(state.computed)
  state.newWord()

  #if str.strip().len > 0:
    #eprint "start", str.strip()
  var i = 0
  while i < str.len:
    if str[i].isWhitespace():
      state.processWhitespace(str[i])
      inc i
    else:
      var r: Rune
      fastRuneAt(str, i, r)
      state.checkWrap(r)
      state.word.str &= r
      state.word.width += r.width() * state.ictx.cellwidth

  state.addWord()

type PreferredDimensions = object
  compwidth: int
  compheight: Option[int]
  margin_top: int
  margin_bottom: int
  margin_left: int
  margin_right: int
  padding_left: int
  padding_right: int
  padding_top: int
  padding_bottom: int

proc preferredDimensions(computed: CSSComputedValues, viewport: Viewport, width: int, height: Option[int]): PreferredDimensions =
  let pwidth = computed{"width"}
  if pwidth.auto:
    result.compwidth = width
  else:
    result.compwidth = pwidth.px(viewport, width)

  result.margin_top = computed{"margin-top"}.px(viewport, width)
  result.margin_bottom = computed{"margin-top"}.px(viewport, width)
  result.margin_left = computed{"margin-left"}.px(viewport, width)
  result.margin_right = computed{"margin-right"}.px(viewport, width)

  result.padding_top = computed{"padding-top"}.px(viewport, width)
  result.padding_bottom = computed{"padding-bottom"}.px(viewport, width)
  result.padding_left = computed{"padding-left"}.px(viewport, width)
  result.padding_right = computed{"padding-right"}.px(viewport, width)

  if result.compwidth >= width:
    result.compwidth -= result.margin_left
    result.compwidth -= result.margin_right

    result.compwidth -= result.padding_left
    result.compwidth -= result.padding_right

  let pheight = computed{"height"}
  if not pheight.auto:
    #bctx.compheight = pheight.cells_h(bctx.viewport, height).some
    if pheight.unit != UNIT_PERC:
      result.compheight = pheight.px(viewport).some
    elif height.issome:
      result.compheight = pheight.px(viewport, height.get).some

proc setPreferredDimensions(bctx: BlockContext, width: int, height: Option[int]) =
  let preferred = preferredDimensions(bctx.computed, bctx.viewport, width, height)
  bctx.compwidth = preferred.compwidth
  bctx.compheight = preferred.compheight
  bctx.padding_top = preferred.padding_top
  bctx.padding_bottom = preferred.padding_bottom
  bctx.padding_left = preferred.padding_left
  bctx.padding_right = preferred.padding_right
  bctx.margin_top = preferred.margin_top
  bctx.margin_bottom = preferred.margin_bottom
  bctx.margin_left = preferred.margin_left
  bctx.margin_right = preferred.margin_right

proc newBlockContext_common2(bctx: BlockContext, parent: BlockContext, box: BoxBuilder) {.inline.} =
  bctx.viewport = parent.viewport
  bctx.computed = box.computed
  bctx.setPreferredDimensions(parent.compwidth, parent.compheight)

proc newBlockContext_common(parent: BlockContext, box: BoxBuilder): BlockContext {.inline.} =
  new(result)
  result.newBlockContext_common2(parent, box)

proc newBlockContext(parent: BlockContext, box: BlockBoxBuilder): BlockContext =
  result = newBlockContext_common(parent, box)
  result.shrink = result.computed{"width"}.auto and parent.shrink

proc newListItem(parent: BlockContext, builder: ListItemBoxBuilder): ListItem =
  new(result)
  result.newBlockContext_common2(parent, builder.content)
  result.shrink = result.computed{"width"}.auto and parent.shrink

proc newInlineBlockContext(parent: BlockContext, builder: InlineBlockBoxBuilder): BlockContext =
  result = newBlockContext_common(parent, builder.content)
  result.shrink = result.computed{"width"}.auto

proc newInlineBlock(parent: BlockContext, builder: InlineBlockBoxBuilder): InlineBlock =
  new(result)
  result.bctx = parent.newInlineBlockContext(builder)

proc newBlockContext(viewport: Viewport, box: BlockBoxBuilder): BlockContext =
  new(result)
  result.viewport = viewport
  result.computed = box.computed
  result.setPreferredDimensions(viewport.term.width_px, none(int))
  result.shrink = result.computed{"width"}.auto

proc newInlineContext(bctx: BlockContext): InlineContext =
  new(result)
  result.thisrow = InlineRow()
  result.viewport = bctx.viewport
  result.shrink = bctx.shrink

proc positionInlines(bctx: BlockContext) =
  bctx.width += bctx.padding_left
  bctx.inline.offset.x += bctx.padding_left

  bctx.height += bctx.padding_top
  bctx.inline.offset.y += bctx.padding_top

  bctx.height += bctx.padding_bottom

  bctx.width += bctx.padding_right

  if bctx.computed{"width"}.auto:
    bctx.width = min(bctx.width, bctx.compwidth)
  else:
    bctx.width = bctx.compwidth

proc buildBlock(box: BlockBoxBuilder, parent: BlockContext): BlockContext
proc buildInlines(bctx: BlockContext, inlines: seq[BoxBuilder]): InlineContext
proc buildBlocks(bctx: BlockContext, blocks: seq[BoxBuilder], node: Node)

proc applyInlineDimensions(bctx: BlockContext) =
  bctx.height += bctx.inline.height
  if bctx.compheight.issome:
    bctx.height = bctx.compheight.get
  bctx.width = max(bctx.width, bctx.inline.maxwidth)

# Builder only contains inline boxes.
proc buildInlineLayout(bctx: BlockContext, children: seq[BoxBuilder]) =
  bctx.inline = bctx.buildInlines(children)
  bctx.applyInlineDimensions()
  bctx.positionInlines()

proc buildBlockLayout(bctx: BlockContext, children: seq[BoxBuilder], node: Node) =
  bctx.buildBlocks(children, node)

proc buildInlineBlock(builder: InlineBlockBoxBuilder, parent: InlineContext, parentblock: BlockContext): InlineBlock =
  assert builder.content != nil
  result = parentblock.newInlineBlock(builder)

  let blockbuilder = builder.content
  if blockbuilder.inlinelayout:
    result.bctx.buildInlineLayout(blockbuilder.children)
  else:
    # Builder only contains block boxes.
    result.bctx.buildBlocks(blockbuilder.children, blockbuilder.node)

  let preferred = preferredDimensions(builder.computed, parentblock.viewport, parentblock.compwidth, parentblock.compheight)
  let pwidth = builder.computed{"width"}
  if pwidth.auto:
    # Half-baked shrink-to-fit
    result.bctx.width = min(max(result.bctx.width, parent.maxwidth), preferred.compwidth)
  else:
    result.bctx.width = preferred.compwidth

  # Set inline block dimensions,
  result.width = result.bctx.width
  result.height = result.bctx.height

  # Plus margins, for the final result.
  result.width += result.bctx.margin_left
  result.height += result.bctx.margin_top
  result.width += result.bctx.margin_right
  result.height += result.bctx.margin_bottom

  # Set offset here because positionInlines will reset it.
  result.bctx.offset.x = result.bctx.margin_left
  result.bctx.offset.y = result.bctx.margin_top

proc buildInline(bctx: BlockContext, box: InlineBoxBuilder) =
  assert box.ictx != nil
  if box.newline:
    box.ictx.flushLine(bctx.computed, bctx.compwidth)

  let margin_left = box.computed{"margin-left"}.px(bctx.viewport, bctx.compwidth)
  box.ictx.thisrow.width += margin_left

  let paddingformat = ComputedFormat(node: box.node)
  let padding_left = box.computed{"padding-left"}.px(bctx.viewport, bctx.compwidth)
  if padding_left > 0:
    box.ictx.thisrow.addSpacing(padding_left, box.ictx.cellheight, paddingformat)

  for text in box.text:
    assert box.children.len == 0
    box.ictx.renderText(text, bctx.compwidth, box.computed, box.node)

  for child in box.children:
    case child.computed{"display"}
    of DISPLAY_INLINE:
      let child = InlineBoxBuilder(child)
      child.ictx = box.ictx
      bctx.buildInline(child)
    of DISPLAY_INLINE_BLOCK:
      let child = InlineBlockBoxBuilder(child)
      let iblock = child.buildInlineBlock(box.ictx, bctx)
      box.ictx.addAtom(iblock, bctx.compwidth, child.computed)
      box.ictx.whitespacenum = 0
    else:
      assert false, "child.t is " & $child.computed{"display"}

  let padding_right = box.computed{"padding-right"}.px(bctx.viewport, bctx.compwidth)
  if padding_right > 0:
    box.ictx.thisrow.addSpacing(padding_right, max(box.ictx.thisrow.height, 1), paddingformat)

  let margin_right = box.computed{"margin-right"}.px(bctx.viewport, bctx.compwidth)
  box.ictx.thisrow.width += margin_right

proc buildInlines(bctx: BlockContext, inlines: seq[BoxBuilder]): InlineContext =
  let ictx = bctx.newInlineContext()
  if inlines.len > 0:
    for child in inlines:
      case child.computed{"display"}
      of DISPLAY_INLINE:
        let child = InlineBoxBuilder(child)
        child.ictx = ictx
        bctx.buildInline(child)
      of DISPLAY_INLINE_BLOCK:
        let child = InlineBlockBoxBuilder(child)
        let iblock = child.buildInlineBlock(ictx, bctx)
        ictx.addAtom(iblock, bctx.compwidth, child.computed)
        ictx.whitespacenum = 0
      else:
        assert false, "child.t is " & $child.computed{"display"}
    ictx.finish(bctx.computed, bctx.compwidth)

  return ictx

proc buildListItem(builder: ListItemBoxBuilder, parent: BlockContext): ListItem =
  result = parent.newListItem(builder)
  result.marker = result.buildInlines(@[BoxBuilder(builder.marker)])
  if builder.content.inlinelayout:
    result.buildInlineLayout(builder.content.children)
  else:
    result.buildBlockLayout(builder.content.children, builder.content.node)

# Blocks' positions do not have to be positioned if buildBlocks is called with
# children, whence the separate procedure.
proc positionBlocks(bctx: BlockContext) =
  var y = 0
  var x = 0
  var margin_todo: Strut

  y += bctx.padding_top
  bctx.height += bctx.padding_top

  x += bctx.padding_left
  if bctx.computed{"text-align"} == TEXT_ALIGN_MOZ_CENTER:
    x += bctx.compwidth div 2

  template apply_child(child: BlockContext) =
    child.offset.y = y
    child.offset.x = x + child.margin_left
    if bctx.computed{"text-align"} == TEXT_ALIGN_MOZ_CENTER:
      child.offset.x -= child.width div 2
    y += child.height
    bctx.height += child.height
    bctx.width = max(bctx.width, child.width)
    margin_todo = Strut()
    margin_todo.append(child.margin_bottom)

  if bctx.nested.len > 0:
    let child = bctx.nested[0]

    margin_todo.append(bctx.margin_top)
    margin_todo.append(child.margin_top)
    bctx.margin_top = margin_todo.sum()

    apply_child(child)

  var i = 1
  while i < bctx.nested.len:
    let child = bctx.nested[i]

    margin_todo.append(child.margin_top)
    y += margin_todo.sum()
    bctx.height += margin_todo.sum()

    apply_child(child)
    inc i

  margin_todo.append(bctx.margin_bottom)
  bctx.margin_bottom = margin_todo.sum()

  bctx.height += bctx.padding_bottom

  if bctx.compheight.issome:
    bctx.height = bctx.compheight.get

  bctx.width += bctx.padding_left
  bctx.width += bctx.padding_right


proc buildBlocks(bctx: BlockContext, blocks: seq[BoxBuilder], node: Node) =
  for child in blocks:
    var cblock: BlockContext
    case child.computed{"display"}
    of DISPLAY_BLOCK: cblock = buildBlock(BlockBoxBuilder(child), bctx)
    of DISPLAY_LIST_ITEM: cblock = buildListItem(ListItemBoxBuilder(child), bctx)
    else: assert false, "child.t is " & $child.computed{"display"}
    bctx.nested.add(cblock)
  bctx.positionBlocks()

# Build a block box inside another block box, based on a builder.
proc buildBlock(box: BlockBoxBuilder, parent: BlockContext): BlockContext =
  assert parent != nil
  result = parent.newBlockContext(box)
  if box.inlinelayout:
    result.buildInlineLayout(box.children)
  else:
    result.buildBlockLayout(box.children, box.node)

# Establish a new flow-root context and build a block box.
proc buildRootBlock(box: BlockBoxBuilder, viewport: Viewport): BlockContext =
  result = viewport.newBlockContext(box)
  if box.inlinelayout:
    result.buildInlineLayout(box.children)
  else:
    result.buildBlocks(box.children, box.node)

# Generation phase

#WARNING yes there is a {}= macro but that modifies the computed value
# reference itself and those are copied across arrays...
#TODO figure out something here
proc setDisplay(computed: var CSSComputedValues, display: CSSDisplay) =
  computed[PROPERTY_DISPLAY] = CSSComputedValue(t: PROPERTY_DISPLAY, v: VALUE_DISPLAY, display: display)

# Returns a block box, disregarding the computed value of display
proc getBlockBox(computed: CSSComputedValues): BlockBoxBuilder =
  new(result)
  result.computed = computed.copyProperties()
  result.computed.setDisplay(DISPLAY_BLOCK)

proc getInlineBlockBox(computed: CSSComputedValues): InlineBlockBoxBuilder =
  new(result)
  result.computed = computed.copyProperties()
  result.computed.setDisplay(DISPLAY_INLINE_BLOCK)

proc getTextBox(box: BoxBuilder): InlineBoxBuilder =
  new(result)
  result.inlinelayout = true
  result.computed = box.computed.inheritProperties()

proc getTextBox(computed: CSSComputedValues): InlineBoxBuilder =
  new(result)
  result.inlinelayout = true
  result.computed = computed.inheritProperties()

proc getMarkerBox(computed: CSSComputedValues, listItemCounter: int): MarkerBoxBuilder =
  new(result)
  result.inlinelayout = true
  result.computed = computed.copyProperties()
  result.computed.setDisplay(DISPLAY_INLINE)

  if result.computed{"display"} == DISPLAY_LIST_ITEM:
    result.ordinalvalue = listItemCounter
  else:
    result.ordinalvalue = 1
  if computed{"list-style-position"} == LIST_STYLE_POSITION_INSIDE:
    result.inside = true
  result.text.add(computed{"list-style-type"}.listMarker(result.ordinalvalue))

proc getListItemBox(computed: CSSComputedValues, listItemCounter: int): ListItemBoxBuilder =
  new(result)
  result.computed = computed.copyProperties()
  result.marker = getMarkerBox(computed, listItemCounter)

# Don't generate empty anonymous inline blocks between block boxes
func canGenerateAnonymousInline(blockgroup: seq[BoxBuilder], computed: CSSComputedValues, str: string): bool =
  return blockgroup.len > 0 and blockgroup[^1].computed{"display"} == DISPLAY_INLINE or
    computed{"white-space"} in {WHITESPACE_PRE_LINE, WHITESPACE_PRE, WHITESPACE_PRE_WRAP} or
    not str.onlyWhitespace()

proc generateBlockBox(styledNode: StyledNode, viewport: Viewport): BlockBoxBuilder

template flush_block_group(computed: CSSComputedValues) =
  if blockgroup.len > 0:
    let bbox = getBlockBox(computed.inheritProperties())
    bbox.inlinelayout = true
    bbox.children = blockgroup
    box.children.add(bbox)
    blockgroup.setLen(0)

template flush_ibox() =
  if ibox != nil:
    assert ibox.computed{"display"} in {DISPLAY_INLINE, DISPLAY_INLINE_BLOCK}
    blockgroup.add(ibox)
    ibox = nil

proc generateInlineBoxes(box: BlockBoxBuilder, styledNode: StyledNode, blockgroup: var seq[BoxBuilder], viewport: Viewport)

proc generateFromElem(box: BlockBoxBuilder, styledNode: StyledNode, blockgroup: var seq[BoxBuilder], viewport: Viewport, ibox: var InlineBoxBuilder, listItemCounter: var int) =
  if styledNode.node != nil:
    let elem = Element(styledNode.node)
    if elem.tagType == TAG_BR:
      ibox = box.getTextBox()
      ibox.newline = true
      flush_ibox

  case styledNode.computed{"display"}
  of DISPLAY_BLOCK:
    flush_block_group(styledNode.computed)
    let childbox = styledNode.generateBlockBox(viewport)
    box.children.add(childbox)
  of DISPLAY_LIST_ITEM:
    flush_block_group(styledNode.computed)
    let childbox = getListItemBox(styledNode.computed, listItemCounter)
    childbox.content = styledNode.generateBlockBox(viewport)
    box.children.add(childbox)
    inc listItemCounter
  of DISPLAY_INLINE:
    flush_ibox
    box.generateInlineBoxes(styledNode, blockgroup, viewport)
  of DISPLAY_INLINE_BLOCK:
    flush_ibox
    let childbox = getInlineBlockBox(box.computed)
    childbox.content = styledNode.generateBlockBox(viewport)
    blockgroup.add(childbox)
  else:
    discard #TODO

proc generateInlineBoxes(box: BlockBoxBuilder, styledNode: StyledNode, blockgroup: var seq[BoxBuilder], viewport: Viewport) =
  var ibox: InlineBoxBuilder = nil
  var listItemCounter = 1 # ordinal value of current list

  for child in styledNode.children:
    case child.t
    of STYLED_ELEMENT:
      box.generateFromElem(child, blockgroup, viewport, ibox, listItemCounter)
    of STYLED_TEXT:
      if ibox == nil:
        ibox = getTextBox(styledNode.computed)
        ibox.node = styledNode.node
      ibox.text.add(child.text)

  flush_ibox

proc generateBlockBox(styledNode: StyledNode, viewport: Viewport): BlockBoxBuilder =
  let box = getBlockBox(styledNode.computed)
  var blockgroup: seq[BoxBuilder]
  var ibox: InlineBoxBuilder = nil
  var listItemCounter = 1 # ordinal value of current list
  
  for child in styledNode.children:
    case child.t
    of STYLED_ELEMENT:
      flush_ibox
      box.generateFromElem(child, blockgroup, viewport, ibox, listItemCounter)
    of STYLED_TEXT:
      if canGenerateAnonymousInline(blockgroup, box.computed, child.text):
        if ibox == nil:
          ibox = getTextBox(styledNode.computed)
          ibox.node = styledNode.node
        ibox.text.add(child.text)

  flush_ibox
  if blockgroup.len > 0:
    # Avoid unnecessary anonymous block boxes
    if box.children.len == 0:
      box.children = blockgroup
      box.inlinelayout = true
    else:
      flush_block_group(styledNode.computed)
  return box


proc renderLayout*(viewport: var Viewport, document: Document, root: StyledNode) =
  let builder = root.generateBlockBox(viewport)
  viewport.root = buildRootBlock(builder, viewport)