Finalize the position of all borders. Args: strokes (Shapes): A group of explicit border strokes. fills (Shapes): A group of explicit cell shadings. .. note:: A border is finalized in priority below: * Follow expl
(self, strokes:Shapes, fills:Shapes)
| 241 | '''Collection of ``Border`` instances.''' |
| 242 | |
| 243 | def finalize(self, strokes:Shapes, fills:Shapes): |
| 244 | '''Finalize the position of all borders. |
| 245 | |
| 246 | Args: |
| 247 | strokes (Shapes): A group of explicit border strokes. |
| 248 | fills (Shapes): A group of explicit cell shadings. |
| 249 | |
| 250 | .. note:: |
| 251 | A border is finalized in priority below: |
| 252 | |
| 253 | * Follow explicit stroke/border. |
| 254 | * Follow explicit fill/shading. |
| 255 | * Align h-borders or v-borders as more as possible to simplify the table structure. |
| 256 | ''' |
| 257 | # add dummy borders to be finalized by explicit strokes/fillings |
| 258 | self._add_full_dummy_borders() |
| 259 | |
| 260 | # finalize borders by explicit strokes in first priority |
| 261 | self._finalize_by_strokes(strokes) |
| 262 | |
| 263 | # finalize borders by explicit fillings in second priority |
| 264 | tmp_strokes = [] |
| 265 | for fill in fills: |
| 266 | # ignore determined filling or filling in white bg-color |
| 267 | if fill.is_determined or fill.color == rgb_value((1,1,1)): continue |
| 268 | |
| 269 | x0, y0, x1, y1 = fill.bbox |
| 270 | tmp_strokes.extend([ |
| 271 | Stroke().update_bbox((x0, y0, x1, y0)), # top |
| 272 | Stroke().update_bbox((x0, y1, x1, y1)), # bottom |
| 273 | Stroke().update_bbox((x0, y0, x0, y1)), # left |
| 274 | Stroke().update_bbox((x1, y0, x1, y1)) # right |
| 275 | ]) |
| 276 | self._finalize_by_strokes(tmp_strokes) |
| 277 | |
| 278 | # finalize borders by their layout finally (use an average position): |
| 279 | # - un-finalized, and |
| 280 | # - not reference-only borders |
| 281 | borders = list(filter(lambda border: not (border.finalized or border.is_reference), self._instances)) |
| 282 | |
| 283 | # h-borders |
| 284 | # NOTE: exclude the top and bottom boundary borders, since they'll be adjusted by |
| 285 | # principle that minimizing the table region. |
| 286 | h_borders = list(filter( |
| 287 | lambda border: border.is_horizontal and |
| 288 | not (border.is_top or border.is_bottom), borders)) |
| 289 | self._finalize_by_layout(h_borders) |
| 290 | |
| 291 | # v-borders |
| 292 | v_borders = list(filter(lambda border: border.is_vertical, borders)) |
| 293 | self._finalize_by_layout(v_borders) |
| 294 | |
| 295 | |
| 296 | def _finalize_by_strokes(self, strokes:list): |
no test coverage detected