Axis Label Placement

This means I will have to write a different algorithm to determine the placement of the labels as the current one doesn’t work in all cases. Can you point me to where the current automatic placement algorithm is so I have a starting point? Could we simply change the current algorithm such that the offset is not so large that it is placed off the pad? Something along the lines of this in pseudo code?

if (offest > pad_edge) offset = pad_edge;