Placing a text at the end of a hairpin (decrescendo)

The book Music Notation on the Twentieth Century, by Kurt Stone, suggests to use "niente" or its abbreviation "n." instead of a hairpin ending on a "o" (page 18). Lilypond actually supports the latter but the former is not implemented. I defined "n." as a dynamic text and also created one of my own: "pd." as an abbreviation of "perdendosi", a term I feel more appropriated for winds than niente. However, there was a situation where I couldn't place the new dynamics: at the end of a measure, right before the bar line. A popular solution is to use a second voice with spacers to place the dynamics correctly but I found this is hardly a good solution. I first attempted to tweak the Scheme code to center text under a hairpin and managed to get a decent result but, as I know nothing of Scheme, the solution was far from perfect. I then created a post in the Nabble forum and the user named Thomas Morley kindly provided a solution in the form of the Scheme function copied below. All credit belongs to him. I'm adding this snippet to the repository at his request.