| 1 | package export |
| 2 | |
| 3 | import "github.com/carlosframework/docs/internal/pdf" |
| 4 | |
| 5 | // PageOne is the geometry of the first page of a document: where every |
| 6 | // line of text actually landed, in points, measured from the top-left |
| 7 | // of the page. |
| 8 | // |
| 9 | // It exists for the document list's thumbnails (internal/docs's |
| 10 | // preview.go); spec §11.4 is where the argument for drawing line boxes |
| 11 | // rather than rasterizing them lives, and it is not repeated here. |
| 12 | // |
| 13 | // Real is the whole point, and it is why this is a sink on the |
| 14 | // exporter's own layout rather than a second, simpler one beside it. |
| 15 | // The line breaking here is done by the same code that breaks lines for |
| 16 | // the PDF, measuring with the same font programs the browser is served, |
| 17 | // against the same page geometry views/show.html sets — so a thumbnail |
| 18 | // shows the document's actual first page, wrapped where it actually |
| 19 | // wraps. A separate estimator ("about sixty characters to a line") |
| 20 | // would have been a second opinion about the document, free to drift |
| 21 | // from the first, and drift is exactly what the list would show. |
| 22 | // |
| 23 | // What is elided, and should be said plainly: the glyphs. This draws |
| 24 | // where the text is, not the text. |
| 25 | // |
| 26 | // This is page one of a PDF of THIS Document, title included — it runs |
| 27 | // the same layout.document PDF does, so whatever the exporter puts on a |
| 28 | // first page is what this reports. A caller that wants the paper |
| 29 | // without the title passes a Document with an empty Title; the document |
| 30 | // list does exactly that, and preview.go says why. |
| 31 | // |
| 32 | // What it bounds is the LAYOUT: stopAfterPage ends that after the first |
| 33 | // page, so a four-hundred-page document costs one page of it. The rest |
| 34 | // of the work is not bounded and it would be wrong to imply otherwise — |
| 35 | // the caller has already replayed and serialised the whole document to |
| 36 | // get here, Parse decodes all of it, and the paragraph in progress is |
| 37 | // measured in full before its first line is drawn. Measured on the seed |
| 38 | // documents, the two halves are the same order: about 0.5-3ms to reach |
| 39 | // the tree and 0.3-2ms to lay the page out. |
| 40 | func PageOne(doc Document) []LineBox { |
| 41 | var lines []LineBox |
| 42 | l := &layout{ |
| 43 | b: pdf.New(PageWidthPt, PageHeightPt), |
| 44 | missing: map[rune]bool{}, |
| 45 | stopAfterPage: 1, |
| 46 | sink: func(b LineBox) { lines = append(lines, b) }, |
| 47 | } |
| 48 | l.newPage() |
| 49 | l.document(doc) |
| 50 | return lines |
| 51 | } |
| 52 | |
| 53 | // LineBox is one laid-out line of text, in points from the top-left of |
| 54 | // the page. It carries no text: a consumer of this is drawing where the |
| 55 | // words are, and the words themselves never leave the layout. |
| 56 | // |
| 57 | // Lines only. A list's bullets and numbers are drawn beside their |
| 58 | // item's first line rather than as lines of their own, so they never |
| 59 | // reach the sink and a list reports as indented lines with no markers. |
| 60 | // At thumbnail scale a marker would be well under a pixel, so this is |
| 61 | // not worth fixing — but it is worth knowing before someone reads a |
| 62 | // list preview as a run of short paragraphs. |
| 63 | // |
| 64 | // That is a property worth keeping rather than an accident. It is what |
| 65 | // lets the document list cache a preview in memory without holding any |
| 66 | // of the document's prose, and it is why a preview built from these is |
| 67 | // not a plaintext derivative of the document (see preview.go's own |
| 68 | // note on what it is, and is not, safe to write down). |
| 69 | type LineBox struct { |
| 70 | // Left and Top are the top-left corner of the line box. Left |
| 71 | // already accounts for alignment, list and quote indents — it is |
| 72 | // where the line starts, not where its column does. |
| 73 | Left, Top float64 |
| 74 | |
| 75 | // Width is the measured width of the text on the line, which for a |
| 76 | // wrapped line is short of the column and for the last line of a |
| 77 | // paragraph is usually well short of it. This is what makes a |
| 78 | // thumbnail read as prose rather than as a solid block. |
| 79 | Width float64 |
| 80 | |
| 81 | // Height is the line box's height — the leading, not the glyphs. |
| 82 | Height float64 |
| 83 | |
| 84 | // Size is the largest type size on the line, in points. A heading |
| 85 | // reports a bigger one than body text, which lets a drawing |
| 86 | // consumer weight it accordingly without knowing what a heading is. |
| 87 | Size float64 |
| 88 | } |
| 89 | |