Debian Codegraph @

texmf-dist/doc/latex/figput/javascript/layout.js

raw
1 "use strict";
2 /*
3 Code for page layout. The idea is a bit like Java Swing (or a zillion other
4 similar setups), but stripped down to only what I need. In particular, the
5 Panels are assumed to be stacked vertically.
6
7 Everything is accessed through FullPanel, which consists of a list of
8 PagePanel objects. These are stacked vertically, and each one contains at
9 least one PDFPanel, and may contain FigurePanels. In the lingo of something
10 like Java, it would be better to think of PagePanel as a Container or a Box
11 since it's used for layout and doesn't directly do anything other than pass
12 things to its sub-parts.
13
14 */
15 // This class is the only thing that should be touched by code outside
16 // this file. FullPanel.init() is called to set up page layout. Everything
17 // is static since there is only one window/canvas.
18 class FullPanel {
19 static init(specList) {
20 // Add all the pages to this document. specList is PDFDocument.pageSpecs.
21 // Each element of specList corresponds to page.
22 // Call this once, when the program starts, to set up the page layout.
23 let cumV = 0;
24 for (let i = 0; i < specList.length; i++) {
25 let pp = new PagePanel(i, specList[i], cumV);
26 this.thePages[i] = pp;
27 cumV += pp.h;
28 }
29 }
30 static async renderAll(height) {
31 // Render every PagePanel in FullPanel.thePages.
32 // Internal to this function, the height should be in "page pixels,"
33 // meaning the number of pixels tall the destination canvas is at the
34 // current zoom ratio of the offsreen pages.
35 height = height / PDFDocument.getZoom();
36 // Because of the use of promises by pdf.js, rendering is broken into
37 // two steps: pre-render and the actual rendering. Pre-rendering
38 // generates a bunch of promises (the pages rendered offscreen) and
39 // rendering can't be done until those promises resolve.
40 let ps = [];
41 for (let i = 0; i < this.thePages.length; i++) {
42 let p = this.thePages[i].preRender(height);
43 ps.push(p);
44 }
45 // Don't return until this is done!
46 await Promise.all(ps);
47 for (let i = 0; i < this.thePages.length; i++)
48 this.thePages[i].render(height);
49 // Eliminate any excess pages from the buffer of offscreen pages.
50 // Do this after copying out, just in case there *is* some weird
51 // problem with race conditions.
52 PDFDocument.trimBuffer();
53 }
54 static totalHeight() {
55 // Return the total height of all pages. This is used to set up
56 // the scroll bars.
57 let answer = 0;
58 for (let i = 0; i < this.thePages.length; i++)
59 answer += this.thePages[i].h;
60 return answer;
61 }
62 static getFullWidth() {
63 // Return the width of the widest page. Most of the time, the pages of
64 // a document all have the same width, but they might not in some
65 // rare case. This is needed to center things left/right in the window.
66 if (this.totalWidth > 0)
67 return this.totalWidth;
68 // Need to calculate it for the first time.
69 for (let i = 0; i < this.thePages.length; i++) {
70 let pp = this.thePages[i];
71 if (pp.w > this.totalWidth)
72 this.totalWidth = pp.w;
73 }
74 return this.totalWidth;
75 }
76 static mouseDown(x, y) {
77 // (x,y) is in pdf points, relative to the entire document.
78 // So y will be a huge number if it's on the umpteeth page.
79 // To save CPU, I could start off by checking whether x is in
80 // [0,pageWidth] and return if it is not, but the user might want to
81 // place controls outside the page. This is unlikely, but possible.
82 // Whoever had focus loses it. It may be taken up by some other widget
83 // (or the same widget again), but nobody has focus by default.
84 WidgetManager.focusOwner = null;
85 // Figure out which page this is and hand it off.
86 let i = 0;
87 for (; i < this.thePages.length; i++) {
88 if (this.thePages[i].v > y) {
89 // Found the first and only page this could be. It was the
90 // page previous to this one.
91 i -= 1;
92 break;
93 }
94 }
95 // If we got here then it had to be the very last page.
96 if (i === this.thePages.length)
97 i = this.thePages.length - 1;
98 // Safety check:
99 if ((i == this.thePages.length) || (i < 0))
100 return;
101 this.thePages[i].mouseDown(x, y);
102 }
103 static mouseMove(x, y) {
104 // As above. However, these are only of interest if some widget
105 // "owns" the mouse -- something was clicked on so that motion
106 // could mean something.
107 if (WidgetManager.mouseOwner === null)
108 return;
109 // Although we know exactly which widget will ultimately get this
110 // event, it's easier to let this pass through the layout hierarchy,
111 // just as for mouseDown(), so that the coordinates are properly adjusted.
112 // BUG: Not DRY.
113 let i = 0;
114 for (; i < this.thePages.length; i++) {
115 if (this.thePages[i].v > y) {
116 i -= 1;
117 break;
118 }
119 }
120 if (i === this.thePages.length)
121 i = this.thePages.length - 1;
122 if ((i == this.thePages.length) || (i < 0))
123 return;
124 this.thePages[i].mouseMove(x, y);
125 }
126 static mouseUp(x, y) {
127 // The big issue here is that the mouse was released so that ownership
128 // is once again up for grabs. In addition, certain widgets will want
129 // to know where the mouse was released. Buttons are a good example.
130 // You could have mouse-down on the button, then the user moves the
131 // mouse out of the button and releases it; the button should only be
132 // "clicked" if the mouse was released over the button.
133 //
134 // An annoying thing here is that the *only* widget that could care about
135 // this (at least, as designed) is the one that owns the mouse-down.
136 // As above, we want the event to pass through the layout hierarchy
137 // so that (x,y) is adjusted for the right frame, but the ultimate
138 // *consumer* of the event may not even be on the page where the
139 // mouse-up occured.
140 // BUG: Not DRY.
141 if (WidgetManager.mouseOwner === null)
142 return;
143 let i = 0;
144 for (; i < this.thePages.length; i++) {
145 if (this.thePages[i].v > y) {
146 i -= 1;
147 break;
148 }
149 }
150 if (i === this.thePages.length)
151 i = this.thePages.length - 1;
152 if ((i == this.thePages.length) || (i < 0))
153 return;
154 this.thePages[i].mouseUp(x, y);
155 // Whatever happened above, the mouse is now up for grabs.
156 WidgetManager.mouseOwner = null;
157 }
158 }
159 // The full document consists of a list of PagePanel objects.
160 // thePages[i] is the i-th page, counting from zero.
161 FullPanel.thePages = [];
162 // The width of the widest page of the entire document. Don't access
163 // this directly; use getFullWidth().
164 FullPanel.totalWidth = -1;
165 // A PagePanel is the top-level thing, just under the canvas. Each
166 // PagePanel makes up a single page of the printed document. There's
167 // a list of them in FullPanel. It includes references to the PDFPanel
168 // and FigurePanel objects that it contains.
169 class PagePanel {
170 constructor(pageNum, pageSpec, v) {
171 // Every panel has a vertical position within the entire document and height,
172 // in pdf pts. The vertical postion, v, is the top of the page, so the page
173 // extends from v to v+h. The caller must ensure that the Panels stack up
174 // correctly since there is no real page layout.
175 //
176 // In some ways the height, h, is redundant since it could be worked
177 // out from the heights of the individual sub-parts. In fact (see below)
178 // it *is* worked out by the constuctor, but it's easier to do it once
179 // and be done with it. This is the height of the page, as rendered,
180 // taking into account any mismatch due to extra "padding" in the figures
181 // (if there is any, and there often is not).
182 this.v = 0;
183 this.w = 0;
184 this.h = 0;
185 // page number, counting from zero.
186 this.pageNum = 0;
187 // The SubPanels that make up this PagePanel.
188 this.parts = [];
189 // This implicitly applies to the global (because everything is static)
190 // PDFDocument. pageNum is which page this is, counting from zero.
191 // pageSpec has the info about how the page breaks into pieces and the
192 // relevant figures. v is where this page lies in the entire vertical
193 // list of pages.
194 this.w = pageSpec.pageWidth;
195 this.v = v;
196 this.pageNum = pageNum;
197 // Create the PDFPanels and FigurePanels in this PagePanel.
198 let s = pageSpec;
199 if (s.insertPoint.length == 0) {
200 // There are no figures on this page.
201 let p = new PDFPanel(pageNum, this.v, 0, 0, this.v, this.w, s.pageHeight);
202 this.h = s.pageHeight;
203 this.parts = [p];
204 }
205 else {
206 // There are figures.
207 let srcV = 0;
208 let destV = 0;
209 let totalV = v;
210 for (let j = 0; j < s.insertPoint.length; j++) {
211 // Bit of pdf above figure.
212 let p = new PDFPanel(pageNum, this.v, srcV, destV, totalV, this.w, s.insertPoint[j] - srcV);
213 destV += s.insertPoint[j] - srcV;
214 totalV += s.insertPoint[j] - srcV;
215 let f = new FigurePanel(this.v, destV, totalV, this.w, s.deleteHeight[j] + s.aboveHeight[j] + s.belowHeight[j], s.aboveHeight[j], s.belowHeight[j], s.leftMargin, s.textWidth, s.drawFcn[j]);
216 srcV = s.insertPoint[j] + s.deleteHeight[j];
217 destV += s.deleteHeight[j] + s.aboveHeight[j] + s.belowHeight[j];
218 totalV += s.deleteHeight[j] + s.aboveHeight[j] + s.belowHeight[j];
219 this.parts.push(p);
220 this.parts.push(f);
221 }
222 // And the bit of pdf below the last figure on the page.
223 let p = new PDFPanel(pageNum, this.v, srcV, destV, totalV, this.w, s.pageHeight - srcV);
224 this.parts.push(p);
225 this.h = destV + s.pageHeight - srcV;
226 }
227 }
228 async preRender(height) {
229 // This renders the underlying page of pdf. It returns a promise
230 // so that the caller can wait for the promise to resolve before
231 // attempting to copy from the rendered page.
232 //
233 // The vpos is where the top of the ctx should be relative to the entire
234 // document, in pdf pts, and height is how much is visible, in rendered
235 // page pixels.
236 // The first question is whether any of this page is visible.
237 let vpos = window.scrollY;
238 if (this.v + this.h < vpos)
239 // Entire page is above the visible area.
240 return;
241 if (this.v > vpos + height)
242 // Entire page is below the visible area.
243 return;
244 // Got here, so some portion of the page is visible.
245 await PDFDocument.render(this.pageNum);
246 }
247 render(height) {
248 // Render every SubPanel of the current PagePanel.
249 // BUG: Before returning, turn off any of these animations that are
250 // definitely not visible.
251 let vpos = window.scrollY;
252 if (this.v + this.h < vpos)
253 return;
254 if (this.v > vpos + height)
255 return;
256 // Got here, so some portion of the page is visible. From here on,
257 // render everything, whether it's actually visible or not.
258 let ctx = ctxTopLevelAdjust();
259 // Call the parts of the page. These could be PDFPanel or FigurePanel
260 // objects.
261 for (let i = 0; i < this.parts.length; i++)
262 this.parts[i].render();
263 // BUG: I don't like this use of zoom here. Maybe no choice?
264 let z = PDFDocument.getZoom();
265 // Put a rectangle aroud the entire page. I'm not 100% convinced that
266 // I like this.
267 ctx.strokeStyle = "black";
268 ctx.strokeRect(0, 0, this.w * z, this.h * z);
269 }
270 mouseDown(x, y) {
271 // Mouse clicked on this page.
272 // y is given relative to the entire document; make it page-relative.
273 y -= this.v;
274 // Only clicks on a figure could be of interest.
275 for (let i = 0; i < this.parts.length; i++) {
276 // Either a PDFPanel or a FigurePanel.
277 let p = this.parts[i];
278 if (p instanceof PDFPanel)
279 continue;
280 // p must be a FigurePanel.
281 if ((p.destV <= y) && (y <= p.destV + p.h))
282 return p.mouseDown(x, y);
283 }
284 }
285 mouseMove(x, y) {
286 // As above. Note that this event could go to the "wrong" figure,
287 // but that's OK. Also, if the mouse is over a PDFPanel, and not
288 // a figure, then the event dies here, which is also OK.
289 y -= this.v;
290 for (let i = 0; i < this.parts.length; i++) {
291 let p = this.parts[i];
292 if (p instanceof PDFPanel)
293 continue;
294 if ((p.destV <= y) && (y <= p.destV + p.h))
295 p.mouseMove(x, y);
296 }
297 }
298 mouseUp(x, y) {
299 // As above, but the event can't be allowed to die. The owning widget
300 // must hear about the mouse up. At the same time, we can't just
301 // inform the widget directly of the mouse up since we also need to
302 // pass the correct coordinates.
303 y -= this.v;
304 for (let i = 0; i < this.parts.length; i++) {
305 let p = this.parts[i];
306 // This is different than above since *somebody* must take the event,
307 // and the relevant widget must hear about it. If the event is over
308 // a PDFPanel, then tell the widget using crazy coordinates. It
309 // doesn't matter exactly where the mouse was released; it only
310 // matters that it wasn't released anywhere near the widget.
311 if ((p.destV <= y) && (y <= p.destV + p.h)) {
312 if (p instanceof PDFPanel)
313 WidgetManager.mouseOwner.mouseUp(10000000000000, 10000000000000);
314 else
315 // Over a figure.
316 p.mouseUp(x, y);
317 }
318 }
319 }
320 }
321 // A PagePanel consists of one or more SubPanels.
322 class SubPanel {
323 constructor(v, totalV, w, h) {
324 // The vertical position and height within a page, with zero being at the
325 // top, and measured in pdf points. This height, h, is the total height.
326 // There's no ambiguity to this height for PDFPanel subclasses, but for
327 // FigurePanel subclasses, it is the sum of the latex height
328 // (PageData.deleteHeight), plus any additional padding as given in
329 // PageData.aboveHeight/belowHeight.
330 this.destV = 0;
331 this.h = 0;
332 this.w = 0;
333 // The position of this panel within the entire document.
334 // The only reason for this is the possible use of HTML DOM elements
335 // as widgets within a figure. I would prefer not to use those at all,
336 // but sometimes it's easier. See the NumberInputWidget for one example.
337 this.totalV = 0;
338 this.destV = v;
339 this.totalV = totalV;
340 this.w = w;
341 this.h = h;
342 }
343 // Will be filled in by sub-class.
344 render() {
345 console.log("Error: called SubPanel.render()!");
346 }
347 mouseDown(x, y) {
348 console.log("Error: called SubPanel.mouseDown()!");
349 }
350 mouseMove(x, y) {
351 console.log("Error: called SubPanel.mouseMove()!");
352 }
353 mouseUp(x, y) {
354 console.log("Error: called SubPanel.mouseUp()!");
355 }
356 }
357 // Used for portions of a page consisting of rendered pdf.
358 class PDFPanel extends SubPanel {
359 constructor(pageNum, offsetV, srcV, destV, totalV, w, h) {
360 // The pageNum is given relative to the global PDFDocument. The srcV
361 // and destV are locations relative to the page (in pdf points, with the
362 // top of the page at v=0) and h is the height of this piece, which
363 // is the same for src and dest.
364 super(destV, totalV, w, h);
365 // The page numbers start at zero and positions are given in pdf points.
366 this.pageNum = 0;
367 this.srcV = 0;
368 // BUG: I think I can fold this in elsewhere. It's the same as PagePanel.v.
369 this.offsetV = 0;
370 this.pageNum = pageNum;
371 this.srcV = srcV;
372 this.offsetV = offsetV;
373 }
374 render() {
375 // Render a portion of the current page.
376 let theCanvas = PDFDocument.getCanvas(this.pageNum);
377 if (theCanvas === null) {
378 // I'm sure how this happens, but it does occasionally.
379 // It doesn't cause any noticable problems. It seems to happen
380 // if you move the scroll thumb too fast.
381 return;
382 }
383 // BUG: I don't like this use of zoom here. Maybe no choice?
384 let z = PDFDocument.getZoom();
385 let ctx = ctxTopLevelAdjust();
386 // Adjust for scroll bar.
387 ctx.translate(0, (this.offsetV - window.scrollY) * z);
388 // Arguments here:
389 // the source image (or canvas),
390 // the source (x,y),
391 // the source (width,height),
392 // the destination (x,y),
393 // the destination (width,height),
394 // It's confusing because optional things typically come after
395 // required things, but not here somehow.
396 ctx.drawImage(theCanvas, 0, this.srcV * z, theCanvas.width, this.h * z, 0, this.destV * z, theCanvas.width, this.h * z);
397 }
398 }
399 class FigurePanel extends SubPanel {
400 constructor(pageV, destV, totalV, w, h, upperPadding, lowerPadding, margin, textWidth, drawFcn) {
401 // As for PDFPanel, plus the margin is the amount by which to shift the
402 // drawing to the right so that the origin is in line with the text.The
403 // drawFcn is the function provided through Latex.
404 // This is just the name of the function, as a string.
405 super(destV, totalV, w, h);
406 // Page's v position. This is the position of the page on which
407 // this figure appears relative to the entire document.
408 this.pageV = 0;
409 // This margin is the location of the left edge of the text, as
410 // reported by latex.
411 this.margin = 0;
412 // Also from Latex (via figures.aux). This I have more confidence in.
413 this.textWidth = 0;
414 // The height in SubPanel.h is the total height of the figure, including
415 // any padding above or below. The origin used for the figure occurs at
416 // a y-value that is PageData.belowHeight *above* the total height.
417 // lowerPadding is equal to PageData.belowHeight for this figure.
418 this.lowerPadding = 0;
419 this.upperPadding = 0;
420 this.pageV = pageV;
421 this.upperPadding = upperPadding;
422 this.lowerPadding = lowerPadding;
423 this.margin = margin;
424 this.textWidth = textWidth;
425 this.drawFcn = drawFcn;
426 }
427 render() {
428 // Save this for widgets and animations to use.
429 let ctx = ctxTopLevelAdjust();
430 let z = PDFDocument.getZoom();
431 ctx.translate(0, (this.pageV - window.scrollY) * z);
432 // Erase the full width of the page. this.w is the correct width,
433 // but the origin will be shifted for drawing. So, erase, then
434 // return the origin to where it was and start over.
435 // Shift to where the figure appears and erase.
436 ctx.translate(0, this.destV * z);
437 ctx.scale(z, z);
438 // The small adjustment here is to prevent erasing the rectangle
439 // that encloses the entire page.
440 ctx.clearRect(1, 0, this.w - 2, this.h);
441 // Return to the orginal t-matrix, then shift down and right before
442 // drawing the figure (and widgets).
443 // What we want is for the origin to be at the lower-right and
444 // right-handed, adjusted upwards by this.lowerPadding too.
445 ctx = ctxTopLevelAdjust();
446 ctx.translate(0, (this.pageV - window.scrollY) * z);
447 ctx.translate(this.margin * z, (this.destV + this.h - this.lowerPadding) * z);
448 ctx.scale(1, -1);
449 ctx.scale(z, z);
450 // Tack this FigurePanel onto the underlying figure-drawing code.
451 // The first time the figure is rendered, this is set, and it's re-set
452 // to the same value with every subsequent call. That seems like
453 // pointless extra work, but it gets the job done.
454 this.drawFcn.figurePanelClass = this;
455 this.drawFcn(ctx);
456 }
457 mouseDown(x, y) {
458 // (x,y) is in pdf points, relative to the top-left of the page.
459 // Adjust to be relative to the figure, but still LH, relative
460 // to the top of the figure.
461 y -= this.destV;
462 x -= this.margin;
463 // y is now given relative to the top edge of the figure, getting
464 // larger as you go *down* the page.
465 // Convert y to be RH relative to the correct origin, taking
466 // any padding into account. This is confusing. At this stage,
467 // y is the distance below the figure's top edge. Call that y0.
468 // We want the distance above the lower padding (if any); call
469 // that y1. The distance above the lower *edge* of the figure
470 // is this.h - y0, and from this we subtract the padding.
471 y = (this.h - y) - this.lowerPadding;
472 // Pass to the relevant widget.
473 WidgetManager.mouseDown(this.drawFcn, x, y);
474 }
475 mouseMove(x, y) {
476 // As above.
477 y -= this.destV;
478 x -= this.margin;
479 y = (this.h - this.lowerPadding) - y;
480 WidgetManager.mouseMove(this.drawFcn, x, y);
481 }
482 mouseUp(x, y) {
483 // As above.
484 y -= this.destV;
485 x -= this.margin;
486 y = (this.h - this.lowerPadding) - y;
487 WidgetManager.mouseUp(this.drawFcn, x, y);
488 }
489 }