Determination of the charge distribution in nonmetallic crystals. I. Theory and application to tetrahedrally coordinated solids