Surface scattering of x rays in thin films. Part I. Theoretical treatment