def _apply_bracket_layers(self):
if not self._designspace.axes:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>bracket_axis = self._designspace.axes[<NUM_LIT:0>]<EOL>if bracket_axis.map:<EOL><INDENT>axis_scale = [design_location for _, design_location in bracket_axis.map]<EOL>bracket_axis_min = min(axis_scale)<EOL>bracket_axis_max = max(axis_scale)<EOL><DEDENT>else:  <EOL><INDENT>bracket_axis_min = bracket_axis.minimum<EOL>bracket_axis_max = bracket_axis.maximum<EOL><DEDENT>bracket_layer_map = defaultdict(list)  <EOL>glyph_crossovers = defaultdict(set)  <EOL>glyph_sanity_counter = defaultdict(<EOL>list<EOL>)  <EOL>for layer in self.bracket_layers:<EOL><INDENT>glyph_name = layer.parent.name<EOL>n = layer.name<EOL>try:<EOL><INDENT>bracket_crossover = int(n[n.index("<STR_LIT:[>") + <NUM_LIT:1> : n.index("<STR_LIT:]>")])<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not bracket_axis_min <= bracket_crossover <= bracket_axis_max:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>glyph_name=glyph_name,<EOL>layer_name=n,<EOL>bracket_axis_name=bracket_axis.name,<EOL>bracket_axis_minimum=bracket_axis_min,<EOL>bracket_axis_maximum=bracket_axis_max,<EOL>)<EOL>)<EOL><DEDENT>bracket_layer_map[bracket_crossover].append(layer)<EOL>glyph_crossovers[glyph_name].add(bracket_crossover)<EOL>glyph_sanity_counter[(glyph_name, bracket_crossover)].append(<EOL>layer.associatedMasterId<EOL>)<EOL><DEDENT>unbalanced_bracket_layers = []<EOL>n_masters = len(list(self.masters))<EOL>for ((glyph_name, _), master_layer_ids) in glyph_sanity_counter.items():<EOL><INDENT>if not len(master_layer_ids) == n_masters:<EOL><INDENT>unbalanced_bracket_layers.append(glyph_name)<EOL><DEDENT><DEDENT>if unbalanced_bracket_layers:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>unbalanced_glyphs=unbalanced_bracket_layers<EOL>)<EOL>)<EOL><DEDENT>rule_bucket = defaultdict(list)  <EOL>for glyph_name, crossovers in sorted(glyph_crossovers.items()):<EOL><INDENT>for crossover_min, crossover_max in util.pairwise(<EOL>sorted(crossovers) + [bracket_axis_max]<EOL>):<EOL><INDENT>rule_bucket[(int(crossover_min), int(crossover_max))].append(glyph_name)<EOL><DEDENT><DEDENT>for (axis_range_min, axis_range_max), glyph_names in sorted(<EOL>rule_bucket.items()<EOL>):<EOL><INDENT>rule_name = "<STR_LIT>".format(axis_range_min, axis_range_max)<EOL>glyph_sub_suffix = "<STR_LIT>".format(axis_range_min)<EOL>rule = designspaceLib.RuleDescriptor()<EOL>rule.name = rule_name<EOL>rule.conditionSets.append(<EOL>[<EOL>{<EOL>"<STR_LIT:name>": bracket_axis.name,<EOL>"<STR_LIT>": axis_range_min,<EOL>"<STR_LIT>": axis_range_max,<EOL>}<EOL>]<EOL>)<EOL>rule.subs.extend(<EOL>[<EOL>(glyph_name, glyph_name + glyph_sub_suffix)<EOL>for glyph_name in glyph_names<EOL>]<EOL>)<EOL>self._designspace.addRule(rule)<EOL><DEDENT>for location, layers in bracket_layer_map.items():<EOL><INDENT>for layer in layers:<EOL><INDENT>ufo_font = self._sources[<EOL>layer.associatedMasterId or layer.layerId<EOL>].font.layers.defaultLayer<EOL>ufo_glyph_name = "<STR_LIT>".format(<EOL>glyph_name=layer.parent.name, location=location<EOL>)<EOL>ufo_glyph = ufo_font.newGlyph(ufo_glyph_name)<EOL>self.to_ufo_glyph(ufo_glyph, layer, layer.parent)<EOL>ufo_glyph.unicodes = []  <EOL>ufo_glyph.lib[GLYPHLIB_PREFIX + "<STR_LIT>"] = layer.name<EOL><DEDENT><DEDENT>
Extract bracket layers in a GSGlyph into free-standing UFO glyphs with
        Designspace substitution rules.

        As of Glyphs.app 2.6, only single axis bracket layers are supported, we
        assume the axis to be the first axis in the Designspace. Bracket layer
        backgrounds are not round-tripped.

        A glyph can have more than one bracket layer but Designspace
        rule/OpenType variation condition sets apply all substitutions in a rule
        in a range, so we have to potentially sort bracket layers into rule
        buckets. Example: if a glyph "x" has two bracket layers [300] and [600]
        and glyph "a" has bracket layer [300] and the bracket axis tops out at
        1000, we need the following Designspace rules:

        - BRACKET.300.600  # min 300, max 600 on the bracket axis.
          - x -> x.BRACKET.300
        - BRACKET.600.1000
          - x -> x.BRACKET.600
        - BRACKET.300.1000
          - a -> a.BRACKET.300