I want to achieve a perfect alignment of the right (and/or left) margins of all the lines of paragraph (to the ink), among the lines themselves and eventually with other geometric elements of a page.
It can be done in xetex provided that you know the text in advance, and then to use the information provided by the bounding box of the last (or the first) glyph to offset the gap, using the proper sidebearing. Precision copyfitting in xcoffin
But the solution is unsuitable with unknown material that might appear in templates or to implement in chapter’s styles, where you need to let the TeX linebreaking finish its job before being able to see the final layout.
A general approach is to use the post_linebreak_filter in LuaTex. It was provided by Marcel Krüger in sidebearings and precision left/right alignment
“In LuaTeX, you can use the post_linebreak_filter to intercept the broken lines and add some offsets. The sidebearings can be extracted from rawdata saved by luaotfload. After adding the offsets, the hboxes have to be repacked to determine the new glue settings.”
Unfortunately Marcel's solution used xadvance, an undocumented "field", which was dropped in Luatex 1.08.0.
I must say that the effect (or defect) it is only noticeable in long lines with large fonts, or when you want to align text with geometric elements present in the page. See the MWE that follows.
You should notice that even in “normal”, justified text, the uneven gap is not suppressed.
Is there an alternative solution?
% !TeX TS-program = lualatex
%%% IT DOES NOT WORK WITH LUATEX 1.08 or LATER !!!
%%% IT FAILS WITH LUATEX 1.08 or LATER !!!
%%% >> warning (node filter): error: [\directlua]:11: attempt to perform
%%% arithmetic on field 'xadvance' (a nil value)
\documentclass[12pt]{article}
\RequirePackage{xcolor}
\RequirePackage{luacode}
\newcount\dropsidebearings
\begin{luacode*}
local function drop_sidebearing(head, groupcode)
if tex.count['dropsidebearings'] == 0 then
return true
end
for n in node.traverse_id(node.id'hlist', head) do
local char = node.has_glyph(n.head)
if char then
local f = font.getfont(char.font)
if f.shared then
local off = f.shared.rawdata.descriptions[char.char].boundingbox[1]*f.size/1000
char.xadvance = char.xadvance - off
char.xoffset = char.xoffset - off
end
end
for ch in node.traverse_id(node.id'glyph', n.head) do
char = ch
end
if char then
local f = font.getfont(char.font)
if f.shared then
local desc = f.shared.rawdata.descriptions[char.char]
char.xadvance = char.xadvance - (desc.width-desc.boundingbox[3])*f.size/1000
end
end
local new_list = node.hpack(n.head, n.width, 'exactly')
new_list.head = nil
n.glue_order = new_list.glue_order
n.glue_set = new_list.glue_set
n.glue_sign = new_list.glue_sign
node.free(new_list)
end
return true
end
luatexbase.add_to_callback('post_linebreak_filter', drop_sidebearing, 'Drop sidebearings after linebreaking')
\end{luacode*}
\newcommand{\hairlineiv}[1][green]{%
\leavevmode%
\kern-0.1pt %
\smash{\color{#1}\vrule height 6\baselineskip depth 5pt width 0.1pt}%
\kern-0.1pt
}
\begin{document}
\pagestyle{empty}
\newcommand{\longtitles}{Long titles must be exactly aligned with the vertical green bar.}% the main title
Note the {\bfseries uneven} gap between the text and the green line.
\vspace{3\baselineskip}
\begin{minipage}{5in}
\dropsidebearings=0 %correction OFF
\raggedleft\sffamily\fontsize{60}{72}\selectfont\bfseries \longtitles\hairlineiv
\end{minipage}
\newpage
Perfect aligment! Each whole line was right shifted by a different amount.
\vspace{3\baselineskip}
\begin{minipage}{5in}
\dropsidebearings=1 %correction ON
\raggedleft\sffamily\fontsize{60}{72}\selectfont\bfseries \longtitles\hairlineiv
\end{minipage}
\end{document}

Certainly improves the ragged margins to improve their optical appearance, see pages 39–50 of Hàn Thế Thành’s dissertation. pragma-ade.com/pdftex/thesis.pdf
From page 46:
"Sample 5.4: Justified text with margin kerning. Both left and right columns were typeset with level 1 character protruding. Margin edges were put to the right column to show that positions of certain characters are slightly adjusted. The margins are mechanically subtly ragged but they appear more smooth to the human eye."
– Simon Dispa Jan 16 '19 at 18:36