MCPcopy Create free account
hub / github.com/PyQt5/PyQt / resize9patch

Function resize9patch

QLabel/Lib/QtNinePatch2.py:34–117  ·  view source on GitHub ↗
(image, dw, dh)

Source from the content-addressed store, hash-verified

32
33
34def resize9patch(image, dw, dh):
35 sw = image.width()
36 sh = image.height()
37 if sw > 2 and sh > 2 and dw > 0 and dh > 0:
38 pixmap = QPixmap(dw, dh)
39 pixmap.fill(Qt.transparent)
40 pr = QPainter(pixmap)
41 pr.setRenderHint(QPainter.Antialiasing)
42 pr.setRenderHint(QPainter.SmoothPixmapTransform)
43
44 horz = []
45 vert = []
46 horz_stretch = 0
47 vert_stretch = 0
48
49 pos = 0
50 last = image.pixel(1, 0)
51 for x in range(1, sw - 1):
52 nextP = image.pixel(x + 1, 0)
53 if isStretchableMarker(last) != isStretchableMarker(nextP) or x == sw - 2:
54 stretchable = isStretchableMarker(last)
55 length = x - pos
56 horz.append(Part(pos, length, stretchable))
57 if stretchable:
58 horz_stretch += length
59 last = nextP
60 pos = x
61 pos = 0
62 last = image.pixel(0, 1)
63 for y in range(1, sh - 1):
64 nextP = image.pixel(0, y + 1)
65 if isStretchableMarker(last) != isStretchableMarker(nextP) or y == sh - 2:
66 stretchable = isStretchableMarker(last)
67 length = y - pos
68 vert.append(Part(pos, length, stretchable))
69 if stretchable:
70 vert_stretch += length
71 last = nextP
72 pos = y
73
74 horz_mul = 0
75 vert_mul = 0
76 if horz_stretch > 0:
77 horz_mul = float((dw - (sw - 2 - horz_stretch)) / horz_stretch)
78 if vert_stretch > 0:
79 vert_mul = float((dh - (sh - 2 - vert_stretch)) / vert_stretch)
80 dy0 = 0
81 dy1 = 0
82 vstretch = 0
83 len_vert = len(vert)
84 len_horz = len(horz)
85 for i in range(len_vert):
86 sy0 = vert[i].pos
87 sy1 = vert[i].pos + vert[i].length
88 if i + 1 == len_vert:
89 dy1 = dh
90 elif vert[i].stretchable:
91 vstretch += float(vert[i].length * vert_mul)

Callers 1

Calls 4

isStretchableMarkerFunction · 0.85
PartClass · 0.85
widthMethod · 0.45
heightMethod · 0.45

Tested by

no test coverage detected