Mesh generation and mesh enhancement algorithms often require a mesh size function to specify the desired size of the elements. We present algorithms for automatic generation of a size function, discretized on a background grid, by using distance functions and numerical PDE solvers. The size function is adapted to the geometry, taking into account the local feature size and the boundary curvature. It also obeys a grading constraint that limits the size ratio of neighboring elements. We formulate the feature size in terms of the medial axis transform, and show how to compute it accurately from a distance function. We propose a new Gradient Limiting Equation for the mesh grading requirement, and we show how to solve it numerically with Hamilton-Jacobi solvers. We show examples of the techniques using Cartesian and unstructured background grids in 2-D and 3-D, and applications with numerical adaptation and mesh generation for images.