+ type = bidi_resolve_weak (bidi_it);
+ if (type == NEUTRAL_ON)
+ {
+ /* bracket_pairing_pos == eob means this bracket does not
+ need to be resolved as a bracket, but as a neutral, see
+ the optimization trick we play near the end of
+ bidi_find_bracket_pairs. */
+ if (bidi_it->bracket_pairing_pos == eob)
+ {
+ /* If this is the outermost closing bracket of a run of
+ characters in which we decided to resolve brackets as
+ neutrals, use the embedding level's type, recorded in
+ bracket_enclosed_type, to resolve the bracket. */
+ if (bidi_it->next_for_neutral.charpos == bidi_it->charpos
+ && bidi_paired_bracket_type (bidi_it->ch) == BIDI_BRACKET_CLOSE)
+ type = bidi_it->bracket_enclosed_type;
+ }
+ else if (bidi_find_bracket_pairs (bidi_it))
+ resolve_bracket = true;
+ }
+ }
+ else if (bidi_it->bracket_pairing_pos != eob)
+ {
+ eassert (bidi_it->resolved_level == -1);
+ /* If the cached state shows an increase of embedding level due
+ to an isolate initiator, we need to update the 1st cached
+ state of the next run of the current isolating sequence with
+ the prev_for_neutral and next_for_neutral information, so
+ that it will be picked up when we advance to that next run. */
+ if (bidi_it->level_stack[bidi_it->stack_idx].level > prev_level
+ && ISOLATE_STATUS (bidi_it, bidi_it->stack_idx))
+ {
+ bidi_record_type_for_neutral (&prev_for_neutral, prev_level, 0);
+ bidi_record_type_for_neutral (&next_for_neutral, prev_level, 1);
+ }
+ if (type == NEUTRAL_ON
+ && bidi_paired_bracket_type (ch) == BIDI_BRACKET_OPEN)
+ {
+ if (bidi_it->bracket_pairing_pos > bidi_it->charpos)
+ {
+ /* A cached opening bracket that wasn't completely
+ resolved yet. */
+ resolve_bracket = true;
+ }
+ else if (bidi_it->bracket_pairing_pos == -1)
+ {
+ /* Higher levels were not BPA-resolved yet, even if
+ cached by bidi_find_bracket_pairs. Force application
+ of BPA to the new level now. */
+ if (bidi_find_bracket_pairs (bidi_it))
+ resolve_bracket = true;
+ }
+ }
+ /* Keep track of the prev_for_neutral and next_for_neutral
+ types, needed for resolving brackets below and for resolving
+ neutrals in bidi_resolve_neutral. */
+ if (bidi_it->level_stack[bidi_it->stack_idx].level == prev_level)
+ {
+ bidi_it->prev_for_neutral = prev_for_neutral;
+ if (next_for_neutral.charpos > 0)
+ bidi_it->next_for_neutral = next_for_neutral;
+ }
+ }
+
+ /* If needed, resolve the bracket type according to N0. */
+ if (resolve_bracket)
+ {
+ int embedding_level = bidi_it->level_stack[bidi_it->stack_idx].level;
+ bidi_type_t embedding_type = (embedding_level & 1) ? STRONG_R : STRONG_L;
+
+ eassert (bidi_it->prev_for_neutral.type != UNKNOWN_BT);
+ eassert (bidi_it->bracket_pairing_pos > bidi_it->charpos);
+ if (bidi_it->bracket_enclosed_type == embedding_type) /* N0b */
+ type = embedding_type;