• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 14958605596

11 May 2025 06:34PM UTC coverage: 31.311% (-25.4%) from 56.689%
14958605596

push

github

web-flow
Merge 3e53926d5 into 443551a3e

6301 of 20124 relevant lines covered (31.31%)

4203.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/grid/init.lua
1
local base = require("packages.base")
×
2

3
local package = pl.class(base)
×
4
package._name = "grid"
×
5

6
local oldPagebuilderType, oldTypesetterType
7

8
local function startGridInFrame (typesetter)
9
   if not SILE.typesetter.state.grid then
×
10
      return
×
11
   end -- Ensure the frame hook isn't effective when grid is off
12
   local queue = typesetter.state.outputQueue
×
13
   typesetter.frame.state.totals.gridCursor = SILE.types.measurement(0)
×
14
   if #queue == 0 then
×
15
      typesetter.state.previousVbox = typesetter:pushVbox()
×
16
      return
×
17
   end
18
   while queue[1] and (queue[1].discardable or queue[1].gridleading) do
×
19
      table.remove(queue, 1)
×
20
   end
21
   if queue[1] then
×
22
      table.insert(queue, 1, SILE.types.node.vbox())
×
23
      table.insert(queue, 2, SILE.typesetter:leadingFor(queue[2], queue[1]))
×
24
   end
25
end
26

27
function package:_init (options)
×
28
   self.spacing = SU.cast("measurement", options.spacing or "1bs"):absolute()
×
29
   base._init(self)
×
30
end
31

32
function package:registerCommands ()
×
33
   self:registerCommand("grid:debug", function (options, _)
×
34
      local spacing = SU.cast("measurement", options.spacing or self.spacing):absolute()
×
35
      local debugGrid = function ()
36
         local frame = SILE.typesetter.frame
×
37
         local gridCursor = spacing
×
38
         while gridCursor < frame:height() do
×
39
            SILE.outputter:drawRule(frame:left(), frame:top() + gridCursor, frame:width(), 0.1)
×
40
            gridCursor = gridCursor + spacing
×
41
         end
42
      end
43
      debugGrid()
×
44
      SILE.typesetter:registerNewFrameHook(debugGrid)
×
45
   end)
46

47
   self:registerCommand("grid", function (options, _)
×
48
      if options.spacing then
×
49
         self.spacing = SU.cast("measurement", options.spacing):absolute()
×
50
      end
51
      SILE.typesetter.state.grid = true
×
52
      oldPagebuilderType = SILE.pagebuilder._name
×
53
      oldTypesetterType = SILE.typesetter._name
×
54
      SILE.pagebuilders.grid:cast(SILE.pagebuilder)
×
55
      SILE.typesetters.grid:cast(SILE.typesetter)
×
56
      SILE.typesetter.options = { spacing = self.spacing }
×
57
      if SILE.typesetter.frame then
×
58
         startGridInFrame(SILE.typesetter)
×
59
      end
60
      SILE.typesetter:registerNewFrameHook(startGridInFrame)
×
61
   end, "Begins typesetting on a grid spaced at <spacing> intervals.")
×
62

63
   self:registerCommand("no-grid", function (_, _)
×
64
      SILE.typesetter.state.grid = false
×
65
      SILE.typesetters[oldTypesetterType]:cast(SILE.typesetter)
×
66
      SILE.pagebuilders[oldPagebuilderType]:cast(SILE.pagebuilder)
×
67
   end, "Stops grid typesetting.")
×
68
end
69

70
package.documentation = [[
71
\begin{document}
72
\use[module=packages.grid]
73
\grid[spacing=15pt]
74
In normal typesetting, SILE determines the spacing between lines of type according to the following two rules:
75

76
\begin{itemize}
77
\item{SILE tries to insert space between two successive lines so that their baselines are separated by a fixed distance called the \code{baselineskip}.}
78
\item{If this first rule would mean that the bottom and the top of the lines are less than two points apart, then they are forced to be two points apart.
79
      (This distance is configurable, and called the \code{lineskip}.)}
80
\end{itemize}
81

82
The second rule is designed to avoid the situation where the first line has a long descender (letters such as g, q, j, p, etc.) which abuts a high ascender on the second line (k, l, capitals, etc.).
83

84
In addition, the \code{baselineskip} contains a certain amount of “stretch,” so that the lines can expand if this would help with producing a page break at an optimal location, and similarly spacing between paragraphs can stretch or shrink.
85

86
The combination of all of these rules means that a line may begin at practically any point on the page.
87

88
An alternative way of typesetting is to require that lines begin at fixed points on a regular grid.
89
Some people prefer the “color” of pages produced by grid typesetting, and the method is often used when typesetting on very thin paper, as lining up the lines of type on both sides of a page ensures that ink does not bleed through from the back to the front.
90
Compare the following examples: on the left, the lines are guaranteed to fall in the same places on the recto (front) and the verso (back) of the paper; on the right, no such guarantee is made.
91

92
\img[src=documentation/grid-1.png,height=130]
93
\img[src=documentation/grid-2.png,height=130]
94

95
The \autodoc:package{grid} package alters the operation of SILE’s typesetter so that the two rules above do not apply; lines are always aligned on a fixed grid, and spaces between paragraphs, etc., are adjusted to conform to the grid.
96
Loading the package adds two new commands to SILE: \autodoc:command{\grid[spacing=<dimension>]} and \autodoc:command{\no-grid}.
97
The first turns on grid typesetting for the remainder of the document; the second turns it off again.
98

99
At the start of this section, we issued the command \autodoc:command{\grid[spacing=15pt]} to set up a regular 15-point grid.
100
Here is some text typeset with the grid set up:
101

102
\smallskip
103
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
104
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
105
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
106
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
107
\smallskip
108

109
And here is the same text after we issue \autodoc:command{\no-grid}:
110

111
\no-grid\smallskip
112
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
113
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
114
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
115
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
116
\end{document}
117
]]
×
118

119
return package
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc