//
//  GSGlyph+TrueTypeCurves.h
//  GlyphsCore
//
//  Created by Georg Seifert on 11/09/16.
//  Copyright © 2016 schriftgestaltung.de. All rights reserved.
//

#import <GlyphsCore/GSGlyph.h>

@class GSPath;

@interface GSGlyph (TrueTypeCurves)

/// Separates Alternative layer groups into glyphs for variable-font substitution.
///
/// The receiver retains its first layer group.
/// Other Alternative layer groups become returned glyphs, including required variants of composite glyphs.
///
/// - Parameter substitutions: A collection to which the substitution rules are appended.
/// - Parameter error: On return, an error that prevented the layer groups from being separated.
/// - Returns: The added glyphs, an empty array when no separation is needed, or `nil` on failure.
- (NSArray *)decomposeBracketLayersSubstitutions:(NSMutableArray *)substitutions error:(out NSError **)error;

/// Prepares compatible interpolation layers for TrueType curve conversion.
///
/// Components that cannot remain compatible are decomposed, component anchors are materialized when needed, and paths are cleaned up and validated.
/// Hints are retained for the subsequent conversion.
///
/// - Parameter error: On return, an error caused by invalid path structure.
/// - Returns: Whether the layers were compatible and preparation succeeded.
- (BOOL)decomposeAndCleanupError:(out NSError **)error;

/// Prepares selected interpolation layers for TrueType curve conversion.
///
/// Components that cannot remain compatible are decomposed, component anchors are materialized when needed, and paths are cleaned up and validated.
/// Hints are retained for the subsequent conversion.
/// Layers incompatible with the default master are omitted when `ignoreIncompatible` is `YES`.
///
/// - Parameter error: On return, an error caused by invalid path structure.
/// - Parameter ignoreIncompatible: Whether to omit layers whose shapes are incompatible with the default master.
/// - Returns: Whether the selected layers were compatible and preparation succeeded.
- (BOOL)decomposeAndCleanupError:(out NSError **)error ignoreIncompatible:(BOOL)ignoreIncompatible;

/// Converts the glyph's compatible interpolation layers to quadratic curves.
///
/// Corresponding cubic segments use the same number of quadratic segments across the participating layers.
/// Incompatible active layers are cleared and omitted when `ignoreIncompatible` is `YES`.
///
/// - Parameter curveError: The maximum permitted deviation of the quadratic approximation in font units.
/// - Parameter ignoreIncompatible: Whether to clear and omit layers whose shapes are incompatible with the first participating layer.
/// - Parameter error: On return, an error that prevented conversion.
/// - Returns: Whether conversion succeeded.
- (BOOL)convertToCompatibleTrueTypeWithError:(CGFloat)curveError ignoreIncompatible:(BOOL)ignoreIncompatible error:(out NSError **)error;

/// Converts specified compatible layers to quadratic curves.
///
/// Corresponding cubic segments use the same number of quadratic segments across all specified layers.
/// Only closed paths are converted.
///
/// - Parameter masterIDs: The identifiers of the layers to convert.
/// - Parameter curveError: The maximum permitted deviation of the quadratic approximation in font units.
/// - Parameter error: On return, an error that prevented conversion.
/// - Returns: Whether conversion succeeded.
- (BOOL)convertToCompatibleTrueType:(NSArray <NSString *> *)masterIDs curveError:(CGFloat)curveError error:(out NSError **)error;

/// Converts cubic segments that can be approximated within a tolerance to quadratic segments.
///
/// The path is modified in place.
///
/// - Parameter path: The path to convert.
/// - Parameter curveError: The maximum permitted deviation of the quadratic approximation in font units.
/// - Parameter error: On return, an error that prevented conversion.
/// - Returns: Whether conversion succeeded.
+ (BOOL)convertPath:(GSPath *)path toTrueTypeWithError:(CGFloat)curveError error:(out NSError **)error;

@end
