Debian Codegraph @

texmf-dist/doc/latex/figput/javascript/development/layout.ts

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