summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/impnattypo/impnattypo.sty
blob: 92f3a4317b2273716b48171bb0788eda15e5377f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
%%
%% This is file `impnattypo.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% impnattypo.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2011 by Raphaël Pinson <raphink@gmail.com>
%% --------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{impnattypo}
    [2011/09/19 0.4 Recommendations typographiques de l'Imprimerie Nationale Française]
\ProvidesPackage{impnattypo}
\RequirePackage{ifluatex}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
   family=impnattypo,
   prefix=impnattypo,
}
\DeclareBoolOption{draft}
\DeclareBoolOption{frenchchapters}
\DeclareBoolOption{hyphenation}
\DeclareBoolOption{nosingleletter}
\DeclareBoolOption{parindent}
\DeclareBoolOption{lastparline}
\DeclareBoolOption{homeoarchy}
\DeclareStringOption[red]{homeoarchywordcolor}
\DeclareStringOption[orange]{homeoarchycharcolor}
\DeclareStringOption[brown]{nosinglelettercolor}
\DeclareStringOption[teal]{lastparlinecolor}
\DeclareStringOption[1]{homeoarchymaxwords}
\DeclareStringOption[3]{homeoarchymaxchars}
\ProcessKeyvalOptions*
\RequirePackage{xcolor}
\def\usecolor#1{\csname\string\color@#1\endcsname\space}
\ifimpnattypohyphenation
   \brokenpenalty=10000
   \doublehyphendemerits=1000000000
\fi
\ifimpnattypofrenchchapters
   \let\frenchchapter\Roman
   \renewcommand{\thechapter}{%
     \ifnum\value{chapter}=1
       premier%
     \else
       \frenchchapter{chapter}%
     \fi
   }
\fi
\ifimpnattyponosingleletter
   \ifluatex
      \RequirePackage{luatexbase,luacode}
      \begin{luacode}

      local prevent_single_letter = function (head)
        while head do
          if head.id == 37 then                                                                          -- glyph
            if (head.char >= 65 and head.char <= 122) or (head.char >= 192 and head.char <= 255) then    -- some kind of letter
               if head.prev.id == 10 and head.next.id == 10 then                                         -- only if we are at a one letter word

                 local p = node.new("penalty")
                 p.penalty = 10000

                 \ifimpnattypodraft
                    local w = node.new("whatsit","pdf_literal")
                    w.data = "q \usecolor{\impnattyponosinglelettercolor} 0 0 m 0 5 l 2 5 l 2 0 l b Q"

                    node.insert_after(head,head,w)
                    node.insert_after(head,w,p)
                 \else
                    node.insert_after(head,head,p)
                 \fi
               end
            end
          end
          head = head.next
        end
        return true
      end

      luatexbase.add_to_callback("pre_linebreak_filter",prevent_single_letter,"~")
      \end{luacode}
   \else
      \PackageError{The nosingleletter option only works with LuaTeX}
   \fi
\fi
\ifimpnattypoparindent
\setlength{\parindent}{1em}
\fi
\ifimpnattypolastparline
   \ifluatex
      \RequirePackage{luatexbase,luacode}
      \begin{luacode}
      last_line_twice_parindent = function (head)
        while head do
          local _w,_h,_d = node.dimensions(head)
          if head.id == 10 and head.subtype ~= 15 and (_w < 2 * tex.parindent) then

              -- we are at a glue and have less then 2*\parindent to go
              local p = node.new("penalty")
              p.penalty = 10000

              \ifimpnattypodraft
                 local w = node.new("whatsit","pdf_literal")
                 w.data = "q \usecolor{\impnattypolastparlinecolor} 0 0 m 0 5 l 2 5 l 2 0 l b Q"

                 node.insert_after(head,head.prev,w)
                 node.insert_after(head,w,p)
              \else
                 node.insert_after(head,head.prev,p)
              \fi
          end

          head = head.next
        end
        return true
      end

      luatexbase.add_to_callback("pre_linebreak_filter",last_line_twice_parindent,"lastparline")
      \end{luacode}
   \else
      \setlength{\parfillskip}{0pt plus\dimexpr\textwidth-2\parindent}
   \fi
\fi
\ifimpnattypohomeoarchy
 \ifimpnattypodraft
   \ifluatex
      \RequirePackage{luatexbase,luacode}
      \begin{luacode}
      compare_lines = function (line1,line2)
        local head1 = line1.head
        local head2 = line2.head

        local char_count = 0
        local word_count = 0

        while head1 and head2 do
           if (head1.id == 37 and head2.id == 37
                  and head1.char == head2.char)          -- identical glyph
              or (head1.id == 10 and head2.id == 10) then  -- glue

              if head1.id == 37 then -- glyph
                 char_count = char_count + 1
              elseif head1.id == 10 then -- glue
                 word_count = word_count + 1
              end
              head1 = head1.next
              head2 = head2.next
           elseif (head1.id == 0 or head2.id == 0) then -- end of line
              break
           elseif (head1.id ~= 37 and head1.id ~= 10) then -- some other kind of node
              head1 = head1.next
           elseif (head2.id ~= 37 and head2.id ~= 10) then -- some other kind of node
              head2 = head2.next
           else -- no match, no special node
              break
           end
        end
        -- analyze last non-matching node, check for punctuation
        if ((head1 and head1.id == 37 and head1.char > 49)
             or (head2 and head2.id == 37 and head2.char > 49)) then
           -- not a word
        elseif char_count > 0 then
           word_count = word_count + 1
        end
        return char_count,word_count,head1,head2
      end

      highlight = function (line,nend,color)
         local n = node.new("whatsit","pdf_literal")

         -- get dimensions
         local w,h,d = node.dimensions(line.head,nend)
         local w_pts = w/65536 -- scaled points to points

         -- set data
         n.data = "q " .. color .. " 0 0 m 0 5 l " .. w_pts .. " 5 l " .. w_pts .. " 0 l b Q"

         -- insert node
         n.next = line.head
         line.head = n
         node.slide(line.head)
      end

      homeoarchy = function (head)
        local cur_line = head
        local prev_line -- initiate prev_head

        local max_char = tonumber(\impnattypohomeoarchymaxchars)
        local max_word = tonumber(\impnattypohomeoarchymaxwords)

        while head do
          if head.id == 0 then -- new line
            prev_line = cur_line
            cur_line = head
            if prev_line.id == 0 then
               char_count,word_count,prev_head,cur_head = compare_lines(prev_line,cur_line)
               if char_count >= max_char or word_count >= max_word then
                  local color
                  if word_count >= max_word then
                     color = "q \usecolor{\impnattypohomeoarchywordcolor}"
                  else
                     color = "q \usecolor{\impnattypohomeoarchycharcolor}"
                  end

                  -- highlight both lines
                  highlight(prev_line,prev_head,color)
                  highlight(cur_line,cur_head,color)
               end
            end
          end
          head = head.next
        end
        return true
      end

      luatexbase.add_to_callback("post_linebreak_filter",homeoarchy,"homeoarchy")
      \end{luacode}
   \else
      \PackageError{The homeoarchy option only works with LuaTeX}
   \fi
 \fi
\fi
\endinput
%%
%% End of file `impnattypo.sty'.