There are essentially four ways to do calculations for roof rafters. You can do the basic math, which can be complex. You can refer to professional rafter tables, which show lengths, angles, even variations of types of wood. You can use a basic framing square, which has all the information you need. Or you can use a speed square, a variation of the framing square. The basic framing square is easiest for a homeowner.
Pitch and Run Define Rafters

Rafters are defined by length and pitch, how great the distance each one must support (the run) and the angle of slope of the roof, figured in inches of rise per foot, as in 5/12 for a 5inch rise every foot of run. Common rafters, the basic type, start with calculating a top or plumb cut, where two rafters meet at a peak. You figure that by knowing the pitch and putting the point of the framing square at the bottom of a rafter board with the pitch mark on the thin leg of the square and the 12inch mark on the fat leg at the top of the board. That forms a plumb cut angle at the end of the board.
Use a Rafter Table for Length

Calculating length requires using a table on the fat leg (blade) of the square. It has six lines, starting with "length of common rafters per foot of run." Checking the pitch will show how long a rafter must be to cover each foot of run. For a 5 pitch roof, that's 13, or 13 inches for every foot of run. It will vary with the pitch  a 4 pitch is 12.65 inches per foot, a 6 pitch 13.42 inches. Multiply that number by the run (half the width or span of the roof) to get the length for a rafter from the peak to the outside wall.
Figure a "Birdsmouth"

Each rafter also must have a "birdsmouth," a triangle cut into the bottom of the rafter to fit exactly over the 2by4inch cap board on the top of the wall. That's calculated by measuring the run length from the bottom of the plumb cut line. A 5 pitch rafter on a 12foot run, for instance, will be 13 feet or 156 inches. The birdsmouth is calculated from that point on the bottom of the rafter to 3 1/2 inches back up the board and 1 inch up into the board, to make the triangle.
Add an Eave Overhang

Rafters also must have an overhang to provide an eave to extend beyond the wall. That can be any distance from a foot or more, but typically is about 18 inches. That is added down from the birdsmouth angle. Typically an end cut is made at the bottom of the rafter, a mirror of the plumb cut, figured with the framing square point at the top rather than the bottom of the rafter to form the angle. Once that is measured, another 3/4 inch is taken off the plumb cut to make space for a ridge board to go between each pair of rafters.
Follow an Example

As an example, a 5/12 rafter on a 24foot wide building (with a12foot run) will have a plumb cut, a birdsmouth at 156 inches, an overhang of a 18 inches, a ridge cut of 3/4inch, for a total rafter length of 173 1/4 inches, or roughly 14 1/2 feet. That is for a common rafter, which goes from a peak to a wall. You can use the framing square tables to make similar calculations for hip rafters, which run from a peak to a corner, and jack rafters, short rafters of varying lengths between hip and common rafters on a hip roof. The tables also show how to calculate angle cuts for hip and jack rafters.
Board Width and Lumber Affect Calculations

Rafter calculations also are affected by the width of the rafter board (usually 2by4, but up to 2by10 in some applications) and the type of lumber. Local building codes will show the size of rafter needed for a specific type of building. Lumber industry tables can provide the maximum length for rafters by type of wood and dimension. These are useful in designing a roof.