11# # Flow layout processor
22
3-
43import std/ [strutils, tables, logging, options, base64]
54import vmath, bumpy, pixie, pixie/ fonts
65import ../ box
@@ -9,18 +8,17 @@ import ../../shared/sugar
98import ferus_ipc/ [client/ prelude, shared]
109import pretty
1110
12- type
13- Layout * = object
14- cursor* : Vec2
15- ipc* : IPCClient
16- width* : int
17-
18- boxes* : seq [Box ]
19- viewport* : Rect
20- font* : Font
21- imageCache* : Table [string , string ]
11+ type Layout * = object
12+ cursor* : Vec2
13+ ipc* : IPCClient
14+ width* : int
2215
23- document* : HTMLDocument
16+ boxes* : seq [Box ]
17+ viewport* : Rect
18+ font* : Font
19+ imageCache* : Table [string , string ]
20+
21+ document* : HTMLDocument
2422
2523var
2624 WordLengths : Table [string , int ]
@@ -35,7 +33,7 @@ proc getWordLength*(layout: var Layout, word: string): int =
3533
3634 WordLengths [word] = length
3735 WordHeights [word] = bounds.y.int
38-
36+
3937 debug " layout: computed layout width for text \" " & word & " \" : " & $ length
4038 else :
4139 length = WordLengths [word]
@@ -68,7 +66,7 @@ proc newLayout*(ipc: IPCClient, font: Font): Layout {.inline.} =
6866
6967 layout.cursor.reset ()
7068 layout.boxes.reset ()
71-
69+
7270 WordLengths [" " ] = 8
7371 WordHeights [" " ] = 8
7472
@@ -94,7 +92,13 @@ proc getMaxHeight*(layout: Layout): uint =
9492 list &= restChildren
9593]#
9694
97- proc addText * (layout: var Layout , text: string , fontSize: float32 , kind: BoxKind , href: Option [string ] = none (string )) =
95+ proc addText * (
96+ layout: var Layout ,
97+ text: string ,
98+ fontSize: float32 ,
99+ kind: BoxKind ,
100+ href: Option [string ] = none (string ),
101+ ) =
98102 var lastHeight: float
99103
100104 for word in text.split (' ' ):
@@ -112,11 +116,11 @@ proc addText*(layout: var Layout, text: string, fontSize: float32, kind: BoxKind
112116 height: height.uint ,
113117 fontSize: fontSize,
114118 href: href,
115- kind: kind
119+ kind: kind,
116120 )
117121
118122 # echo width
119-
123+
120124 layout.cursor = vec2 (layout.cursor.x + width.float , layout.cursor.y)
121125
122126 if lastHeight < height.float :
@@ -134,23 +138,30 @@ proc addBreak*(layout: var Layout) =
134138
135139proc addHeading * (layout: var Layout , text: string , level: uint = 1 ) =
136140 layout.addText (
137- text, case level
138- of 1 : 32 f
139- of 2 : 28 f
140- of 3 : 24 f
141- of 4 : 18 f
142- of 5 : 16 f
143- else : raise newException (ValueError , " addHeading given invalid range for level: " & $ level); 0 f,
144- kind = BoxKind .Block
141+ text,
142+ case level
143+ of 1 :
144+ 32 f
145+ of 2 :
146+ 28 f
147+ of 3 :
148+ 24 f
149+ of 4 :
150+ 18 f
151+ of 5 :
152+ 16 f
153+ else :
154+ raise
155+ newException (ValueError , " addHeading given invalid range for level: " & $ level)
156+ 0 f,
157+ kind = BoxKind .Block ,
145158 )
146159
147160{.push warning [ImplicitDefaultValue ]: off .}
148161proc addImage * (
149- layout: var Layout ,
150- content: string ,
151- width, height: Option [uint ] = none (uint )
162+ layout: var Layout , content: string , width, height: Option [uint ] = none (uint )
152163) =
153- let image: Option [Image ] =
164+ let image: Option [Image ] =
154165 try :
155166 some decodeImage (content)
156167 except PixieError as exc:
@@ -163,16 +174,18 @@ proc addImage*(
163174
164175 var img = & image
165176 let
166- width = if * width:
167- & width
168- else :
169- img.width.uint
170-
171- height = if * height:
172- & height
173- else :
174- img.height.uint
175-
177+ width =
178+ if * width:
179+ & width
180+ else :
181+ img.width.uint
182+
183+ height =
184+ if * height:
185+ & height
186+ else :
187+ img.height.uint
188+
176189 img = img.resize (width.int , height.int )
177190 info " Adding image (" & $ width & 'x' & $ height & ')'
178191
@@ -183,16 +196,18 @@ proc addImage*(
183196 pos: layout.cursor,
184197 width: width,
185198 height: height,
186- kind: BoxKind .Inline
199+ kind: BoxKind .Inline ,
187200 )
188201
189202 layout.cursor = vec2 (0 f, height.float + 4 f)
203+
190204{.pop .}
191205
192206proc constructFromElem * (layout: var Layout , elem: HTMLElement ) =
193- template expectText =
207+ template expectText () =
194208 if not * elem.text:
195- warn " layout: <" & $ elem.tag & " > element does not contain any text data, ignoring it."
209+ warn " layout: <" & $ elem.tag &
210+ " > element does not contain any text data, ignoring it."
196211 return
197212
198213 case elem.tag
@@ -207,7 +222,9 @@ proc constructFromElem*(layout: var Layout, elem: HTMLElement) =
207222 layout.addText (& elem.text, 14 f, kind = BoxKind .Block )
208223 of TAG_A :
209224 expectText
210- layout.addText (& elem.text, 14 f, kind = BoxKind .Inline , href = elem.attribute (" href" ))
225+ layout.addText (
226+ & elem.text, 14 f, kind = BoxKind .Inline , href = elem.attribute (" href" )
227+ )
211228 of TAG_H1 :
212229 expectText
213230 layout.addHeading (& elem.text, 1 )
@@ -236,17 +253,18 @@ proc constructFromElem*(layout: var Layout, elem: HTMLElement) =
236253 return
237254
238255 let cached = layout.imageCache.contains (& src)
239-
240- let content = if not cached:
241- let transfer = layout.ipc.requestDataTransfer (
242- ResourceRequired , DataLocation (kind: DataLocationKind .WebRequest , url: & src)
243- )
244- if * transfer:
245- decode ((& transfer).data)
256+
257+ let content =
258+ if not cached:
259+ let transfer = layout.ipc.requestDataTransfer (
260+ ResourceRequired , DataLocation (kind: DataLocationKind .WebRequest , url: & src)
261+ )
262+ if * transfer:
263+ decode ((& transfer).data)
264+ else :
265+ " "
246266 else :
247- " "
248- else :
249- layout.imageCache[& src]
267+ layout.imageCache[& src]
250268
251269 layout.imageCache[& src] = content
252270 let widthAttr = elem.attribute (" width" )
@@ -265,11 +283,10 @@ proc constructFromElem*(layout: var Layout, elem: HTMLElement) =
265283 except ValueError :
266284 warn " <img> tag has invalid height attribute: " & & heightAttr
267285
268- layout.addImage (
269- content,
270- width, height
271- )
272- of TAG_SCRIPT : discard # we don't care about this - that's the JS runtime's job
286+ layout.addImage (content, width, height)
287+ of TAG_SCRIPT :
288+ discard
289+ # we don't care about this - that's the JS runtime's job
273290 else :
274291 warn " layout: unhandled tag: " & $ elem.tag
275292
@@ -281,7 +298,7 @@ proc constructFromDocument*(layout: var Layout, document: HTMLDocument) =
281298 layout.cursor.reset ()
282299
283300 layout.document = document
284-
301+
285302 let head = document.elems[0 ].children[0 ]
286303 let body = document.elems[0 ].children[1 ]
287304
0 commit comments