Documentation
¶
Overview ¶
Package layout turns the text runs of a page into blocks in reading order.
The interpreter (internal/extract) says what a page draws and where. This package decides what a reader makes of it:
- what the file declares about how each run is shown (display.go);
- which runs form a line, and where its spaces are (lines.go);
- which lines form a paragraph (para.go);
- in which order the paragraphs are read (order.go).
Everything is worked out in displayed space: the visible box of the page after its rotation, in points, origin at the top left, y growing downwards. That is the space of Block.Loc.BBox.
Runs with different display reasons never meet. The runs without one are laid out by themselves, so their blocks are what they would be if the text the file declares not shown were not in it; each other set is laid out by itself and its blocks are put among the others by position.
The order in which a file draws its text is evidence of the order in which it is read, and usually the best there is: most programs write a page as it is read. It is kept wherever the geometry of the page does not speak against it, and it decides between readings that the geometry leaves open, such as a table read by rows against two columns read one after the other.
Index ¶
- func EndsEntry[T string | []byte](t T) bool
- func HyphenLen(text string) int
- func ItemMarker(text string) (marker, rest string, ordered, ok bool)
- func ListMarker(text string) (marker, rest string, ordered, ok bool)
- type Line
- type Link
- type Options
- type Page
- type Result
- type Stats
- type Style
- type Word
- type Worker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EndsEntry ¶ added in v0.4.0
EndsEntry reports whether text ends as an entry of a table of contents does: in a leader, with more points than an ellipsis has, and a page label. A label is a short word that ends in a digit or in a digit and a letter, or a Roman numeral.
func HyphenLen ¶ added in v0.5.0
HyphenLen returns the length of the hyphen that text starts with, the hyphen-minus or U+2010, 0 for none.
func ItemMarker ¶
ItemMarker reads the marker of a text that is known to be a list item, as the structure tree of a tagged PDF says one is: as ListMarker, and also a capital letter or a capital Roman number before a full stop ("C.", "IV."), which ListMarker leaves to names, whatever tabs the text has after it.
func ListMarker ¶
ListMarker returns the marker a text starts with, the text after it, and whether the marker counts (a number or a letter). ok is false when the text does not start with one, or nothing follows it.
Types ¶
type Line ¶
type Line struct {
// Block is the index in Result.Blocks of the block the line is in.
Block int
// Text is the text of the line by itself.
Text string
BBox gofastcore.Rect
// Font is the name of the font that most of the line is set in,
// without the tag of a subset, and Size the size it is displayed at.
Font string
Size float64
Words []Word
}
Line is one line of a text block.
type Link ¶
type Link struct {
Boxes []gofastcore.Rect
Href string
}
Link is a link of the page: the boxes it covers, displayed, and its target.
type Options ¶
type Options struct {
// Worker, if set, keeps the builder for the next page of the same
// goroutine (Worker).
Worker *Worker
// Images puts an image block where each image is.
Images bool
// Lines returns the lines of the text blocks with their words, in
// Result.Lines. The words are placed exactly when the content was
// read with extract.Options.Words.
Lines bool
// Spans gives the text blocks spans of bold and italic, and of the
// links of the page, Links.
Spans bool
Links []Link
// Heading, when set, gives the level of the heading of a tagged PDF
// that the content under a marked-content identifier is part of, 0
// for none (see Style.Heading).
Heading func(mcid int32) uint8
// Cell, when set, gives the cell of a table of a tagged PDF that the
// content under a marked-content identifier is in: an index into
// CellTable, -1 for none. CellTable gives the table of each cell, an
// index from 0 to Tables-1. The text of each cell, without a display
// reason, is laid out
// by itself, and each table that has some is one block of kind table,
// with its cells' text in Result.CellText (tables.go).
Cell func(mcid int32) int32
CellTable []int32
Tables int
// Item, when set, gives the list item of a tagged PDF that the
// content under a marked-content identifier is in, 0 for none, and
// whether it is the item's label. A paragraph never joins lines of two
// items, or of an item and content outside one (see Style.Item).
Item func(mcid int32) (item int64, label bool)
// Para, when set, gives the paragraph that the file declares the
// content under an identifier to be in, from 1; 0 for none. Lines of
// one such paragraph that stand one under the other are one
// paragraph, however they are set, and a paragraph never joins lines
// of two, or of one and content outside any. A PDF declares no such
// thing that is followed; the story fragments of an XPS document do.
Para func(mcid int32) int32
// Rank, when set, gives the place of the content under a
// marked-content identifier in the order of the structure tree of a
// tagged PDF, from 1; 0 for none. The blocks are then ordered with
// the tree as the evidence that the order of drawing is otherwise
// (order.go).
Rank func(mcid int32) int32
// NoTables leaves out the tables found by how their text lines up
// (aligned.go).
NoTables bool
// OnPage leaves out the text that is wholly outside the page: no
// part of the box of its run is inside the displayed box.
OnPage bool
}
Options says what to produce besides text blocks.
type Page ¶
type Page struct {
// M takes the page's coordinates to displayed space.
M [6]float64
// Width and Height are the displayed size in points.
Width, Height float64
}
Page is the geometry of the page being laid out.
type Result ¶
type Result struct {
// Blocks are in reading order. Text blocks are paragraphs with
// OriginInferred; image blocks have Image set to a description
// without an ID or data, and Images gives the image each stands for.
Blocks []gofastcore.Block
// Images has one entry per block: the index in Content.Images of the
// image an image block shows, -1 for a text block. It is nil when
// there is no image block.
Images []int32
// HasText reports whether there is a text block, with a display
// reason or not.
HasText bool
// Controls is the number of control characters that were replaced.
Controls int
// Removed is the number of control characters removed from the
// names that the blocks have in their attributes.
Removed int
// Lines are the lines of the text blocks, block by block, when
// Options.Lines is set.
Lines []Line
// Styles has one entry per block: how its text is set. An image
// block has the zero Style.
Styles []Style
Stats Stats
// CellText is the text of each cell of Options.Cell, "" for a cell
// with none, and Tables the table of each block of kind table, in
// the order of the blocks.
CellText []string
Tables []int32
// CellBox is the box of the text of each cell, and CellSize the size
// of its largest text; zero for a cell without text.
CellBox []gofastcore.Rect
CellSize []float64
}
Result is the page as blocks.
type Stats ¶
type Stats struct {
Runs, Lines int
// Duplicates is the number of runs left out because the same text
// is printed in the same place by another run.
Duplicates int
// Rejoined is the number of lines put together from pieces that the
// page draws at different times.
Rejoined int
// Tabs is the number of wide gaps written as a tab, and LetterSpaced
// the number of lines whose letters stood apart.
Tabs, LetterSpaced int
// Reordered reports that the blocks are not in the order drawn.
Reordered bool
}
Stats counts what the layout did. It is for measurement.
type Style ¶
type Style struct {
// Size is the size the text is displayed at.
Size float64
// Bold is set when the font of that line is bold.
Bold bool
// Lines is the number of lines of the block.
Lines int
// Heading is the level that Options.Heading gives every run of the
// block, 0 when it gives none or not the same to all.
Heading uint8
// Marked is one more than the marked-content identifier (MCID) of
// the first run of the block, on a page that Options.Heading is set
// for; 0 for none.
Marked int32
// Item is the list item that Options.Item gives every run of a
// paragraph, 0 when it gives none or not the same to all; Label is
// then the text of the runs at its start that are the item's label.
Item int64
Label string
// Hyphens are the places in the block's text, as byte offsets in
// order, of the hyphens that end a line inside the block between two
// letters. The lines are joined there without a space and with the
// hyphen, which belongs to the word or to the line break; the page
// does not say which.
Hyphens []int32
}
Style is how the text of a block is set: by its line with the most text.
type Word ¶
type Word struct {
Text string
BBox gofastcore.Rect
// Style is bold and italic as the font of the word's first letter
// is.
Style gofastcore.Style
}
Word is a stretch of a line between two spaces.
type Worker ¶ added in v0.2.1
type Worker struct {
// contains filtered or unexported fields
}
A Worker keeps, for one goroutine that lays out the pages of a document, the builder that one page leaves for the next to reuse, as extract.Worker keeps the state of the reading. The zero Worker is ready; Release gives the builder to the pool when the document is laid out. A Worker is not for two goroutines at once.