Skip to content

Commit b52b716

Browse files
committed
Fix jupyter screenshot for complex pictures
1 parent 2f80ff9 commit b52b716

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/ui/blackboard/jupyterhublet.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ export class JupyterHublet extends Component {
635635
move: React.createRef()
636636
}
637637
this.jupyteredit = React.createRef()
638+
this.jupytereditembedding = React.createRef()
638639
this.jupyterinfo = React.createRef()
639640
this.kernelStatusRef = React.createRef()
640641
this.moveid = {}
@@ -797,9 +798,21 @@ export class JupyterHublet extends Component {
797798
if (!this.props.screenShotSaver) return
798799
if (!this.props.addPicture) return
799800
try {
801+
// we need to calculate our own dpi, as the dimensions on the pdf
802+
// do not match the dimensions in the pdf
803+
804+
// calculate desired width
805+
const dwidth =
806+
400 /* dpi */ *
807+
210 /* mm */ *
808+
0.0393701 /* inch/ mm */ *
809+
this.props.pos.width
810+
811+
const scaleratio = dwidth / this.appletwidth
812+
const dpi = 96 * scaleratio
800813
const { sha } = await this.props.screenShotSaver(
801814
await this.jupyteredit.current?.screenShot?.({
802-
dpi: 300
815+
dpi
803816
})
804817
)
805818
if (!sha) {
@@ -1284,6 +1297,7 @@ export class JupyterHublet extends Component {
12841297
width: '100%',
12851298
height: '100%'
12861299
}}
1300+
ref={this.jupytereditembedding}
12871301
>
12881302
<JupyterEdit
12891303
editActivated={this.state.jupyteredit}

src/ui/failsboard.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,28 @@ export class FailsBoard extends FailsBasis {
410410
}
411411

412412
async screenShotSaver({ data, type }) {
413-
const picture = new Blob([data], { type })
413+
let picture = new Blob([data], { type })
414+
if (picture.size > 950_000) {
415+
// there is a 1 mbyte limit for message sizes, compress differently if necessary
416+
// first attempt switch to jpeg
417+
const origImage = await createImageBitmap(picture)
418+
const canvas = new OffscreenCanvas(origImage.width, origImage.height)
419+
const ctx = canvas.getContext('2d')
420+
ctx.fillStyle = '#ffffff'
421+
ctx.fillRect(0, 0, canvas.width, canvas.height)
422+
ctx.drawImage(origImage, 0, 0)
423+
const qualities = [0.95, 0.9, 0.8, 0.7, 0.6, 0.5]
424+
for (const quality of qualities) {
425+
picture = await canvas.convertToBlob({
426+
type: 'image/jpeg',
427+
quality
428+
})
429+
if (picture.size < 950_000) break
430+
}
431+
if (picture.size > 950_000) {
432+
throw new Error('Picture size of screenshot exceeds limit')
433+
}
434+
}
414435

415436
if (!this.reduce) {
416437
const pica = Pica({ features: ['js', 'wasm', 'cib'] })

0 commit comments

Comments
 (0)