def select_named_pairs(pair_data):
    """Returns a list of name, base id, target id tuples
    for the codepoint and primary pairs in pair_data.
    Generates a name for each selected pair.  If the pair is matched
    by codepoint, use the 'u(ni)XXXX' name of the codepoint.  Else use
    a name formed from the glyph id(s). This handles unmatched
    pairs (where one glyph id is -1)."""

    named_pairs = []
    if pair_data.cp_pairs is not None:
        for b, t, cp in pair_data.cp_pairs:
            name = "%s%04X" % ("uni" if cp < 0x10000 else "u", cp)
            named_pairs.append((name, b, t))
    if pair_data.pri_pairs is not None:
        for b, t, _ in pair_data.pri_pairs:
            if b == t:
                name = "g_%05d" % b
            elif t == -1:
                name = "g_b%05d" % b
            elif b == -1:
                name = "g_t%05d" % t
            else:
                name = "g_b%05d_t%05d" % (b, t)
            named_pairs.append((name, b, t))
    return named_pairs