@@ -88,14 +88,39 @@ function display(d::InlineDisplay, M::MIME, x)
88
88
" data" => d)))
89
89
end
90
90
91
+
92
+ # look for a short sequence in an array
93
+ function get_ind (sub_arr, arr)
94
+ nsub = length (sub_arr)
95
+ search_end = length (arr) - nsub + 1
96
+ return findfirst (i -> all (sub_arr .== arr[i: i+ nsub- 1 ]) , 1 : search_end)
97
+ end
98
+
99
+ # get w, h from PNG base64
100
+ function png_wh (img:: String )
101
+ decoded = base64decode (img[1 : 32 ]) # you need 13 + 8 bytes, this is a bit extra
102
+ ihdr = get_ind (b " IHDR" , decoded) + 4 # find the location of the header
103
+ w, h = ntoh .(reinterpret (Int32, decoded[ihdr: ihdr+ 8 - 1 ])) # get the 8 bytes after
104
+ return w, h
105
+ end
106
+
107
+ const retina = Ref (false ) # flag for setting retina-type images
108
+
91
109
# override display to send IPython a dictionary of all supported
92
110
# output types, so that IPython can choose what to display.
93
111
function display (d:: InlineDisplay , x)
94
112
undisplay (x) # dequeue previous redisplay(x)
113
+
114
+ meta = metadata (x)
115
+ data = display_dict (x)
116
+ if retina[] && " image/png" in keys (data) # if retina, apply metadata to halve sizes
117
+ w, h = png_wh (data[" image/png" ])
118
+ meta[" image/png" ] = Dict (" width" => w/ 2 , " height" => h/ 2 )
119
+ end
95
120
send_ipython (publish[],
96
121
msg_pub (execute_msg, " display_data" ,
97
- Dict (" metadata" => metadata (x) , # optional
98
- " data" => display_dict (x) )))
122
+ Dict (" metadata" => meta , # optional
123
+ " data" => data )))
99
124
end
100
125
101
126
# we overload redisplay(d, x) to add x to a queue of objects to display,
0 commit comments