code: def compact(font: TTFont, level: int) -> TTFont:
      # Ideal plan:
      #  1. Find lookups of Lookup Type 2: Pair Adjustment Positioning Subtable
      #     https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#lookup-type-2-pair-adjustment-positioning-subtable
      #  2. Extract glyph-glyph kerning and class-kerning from all present subtables
      #  3. Regroup into different subtable arrangements
      #  4. Put back into the lookup
      #
      # Actual implementation:
      #  2. Only class kerning is optimized currently
      #  3. If the input kerning is already in several subtables, the subtables
      #     are not grouped together first; instead each subtable is treated
      #     independently, so currently this step is:
      #     Split existing subtables into more smaller subtables
      gpos = font["GPOS"]
      for lookup in gpos.table.LookupList.Lookup:
          if lookup.LookupType == 2:
              compact_lookup(font, level, lookup)
          elif (
              lookup.LookupType == 9
              and lookup.SubTable[0].ExtensionLookupType == 2
          ):
              compact_ext_lookup(font, level, lookup)
      return font
  
imports: import logging
  import os
  from collections import defaultdict, namedtuple
  from functools import reduce
  from itertools import chain
  from math import log2
  from typing import DefaultDict, Dict, Iterable, List, Sequence, Tuple
  from fontTools.config import OPTIONS
  from fontTools.misc.intTools import bit_count, bit_indices
  from fontTools.ttLib import TTFont
  from fontTools.ttLib.tables import otBase, otTables
label_desc: Use image node_id 2 for calling a global function with example usage: compact(font, level) and returns: font