MCPcopy
hub / github.com/ArtifexSoftware/pdf2docx / _get_border_stroke

Method _get_border_stroke

pdf2docx/table/TableStructure.py:88–127  ·  view source on GitHub ↗

Find strokes representing cell borders. Args: strokes (Shapes): Candidate stroke shapes for cell border. direction (str): Either ``row`` or ``col``.

(self, strokes:Shapes, direction:str='row')

Source from the content-addressed store, hash-verified

86
87
88 def _get_border_stroke(self, strokes:Shapes, direction:str='row'):
89 ''' Find strokes representing cell borders.
90
91 Args:
92 strokes (Shapes): Candidate stroke shapes for cell border.
93 direction (str): Either ``row`` or ``col``.
94 '''
95 if not strokes: return Stroke()
96
97 # depends on border direction
98 idx = 0 if direction=='row' else 1
99
100 # cell range
101 x0, x1 = self.merged_bbox[idx], self.merged_bbox[idx+2]
102
103 # check all candidate strokes
104 L = 0.0
105 border_strokes = []
106 for stroke in strokes:
107 bbox = (stroke.x0, stroke.y0, stroke.x1, stroke.y1)
108 t0, t1 = bbox[idx], bbox[idx+2]
109 if t1 <= x0: continue
110 if t0 >= x1: break
111 # intersection length
112 dl = min(x1, t1) - max(x0, t0)
113 # NOTE to ignore small intersection on end point
114 if dl < constants.MAJOR_DIST: continue
115 L += dl
116 border_strokes.append(stroke)
117
118 # use an empty stroke if nothing found, especially for merged cells.
119 # no worry since the border style will be set correctly by adjacent separate cells.
120 if L/(x1-x0) < constants.FACTOR_MAJOR: return Stroke()
121
122 # the entire border of merged cell must have same property, i.e. width and color
123 # otherwise, set empty stroke here and let adjacent cells set the correct style separately
124 if len(border_strokes)==1: return border_strokes[0]
125
126 properties = set([stroke.color for stroke in border_strokes])
127 return border_strokes[0] if len(properties)==1 else Stroke()
128
129
130class TableStructure:

Callers 1

parse_bordersMethod · 0.95

Calls 2

StrokeClass · 0.90
appendMethod · 0.45

Tested by

no test coverage detected