//
//  GSLayer+Autohinting.h
//  GlyphsCore
//
//  Created by Georg Seifert on 30/03/16.
//  Copyright © 2016 schriftgestaltung.de. All rights reserved.
//

#import <GlyphsCore/GSLayer.h>

NS_ASSUME_NONNULL_BEGIN

@class GSNode;

@interface GSLayer (Autohinting)

/// Adds generated PostScript hints to the layer.
///
/// Existing PostScript hints are replaced.
/// - Returns: `YES` if autohinting finished.
- (BOOL)autohintError:(NSError **)error;

/// Adds generated PostScript hints to the layer.
///
/// Existing PostScript hints are replaced.
/// - Returns: `YES` if autohinting finished.
- (BOOL)autohint;

/// Generates PostScript hints for the layer.
///
/// The returned hints are not added to the layer.
/// - Parameter doHorizontal: Whether to generate horizontal hints.
/// - Parameter doVertical: Whether to generate vertical hints.
/// - Returns: The generated hints.
- (nullable NSArray *)getAutohintsDoHorizontal:(BOOL)doHorizontal doVertical:(BOOL)doVertical error:(out NSError **)error;

/// Extracts PostScript hints from Type 2 charstring text.
///
/// - Parameter doLinks: Whether to attach hints to matching nodes when possible.
- (NSArray *)hintsFromCharString:(NSString *)result doLinks:(BOOL)doLinks;

/// Extracts PostScript hints from Type 2 charstring text.
///
/// - Parameter doLinks: Whether to attach hints to matching nodes when possible.
/// - Parameter ignoreVerticalStems: Whether vertical stem hints should be ignored.
- (NSArray *)hintsFromCharString:(NSString *)result doLinks:(BOOL)doLinks ignoreVerticalStems:(BOOL)ignoreVerticalStems;

/// Returns the index path of an on-curve node near the given x position.
///
/// The actual matched position is written to `pos` when provided.
/// - Returns: The node index path, or `nil` if no matching node or hint position is found.
- (nullable NSIndexPath *)indexPathOfNodeAtX:(CGFloat)X actualPosition:(NSPoint *_Nullable)pos NS_REFINED_FOR_SWIFT;

/// Returns the index path of an on-curve node near the given y position.
///
/// The actual matched position is written to `pos` when provided.
/// - Returns: The node index path, or `nil` if no matching node or hint position is found.
- (nullable NSIndexPath *)indexPathOfNodeAtY:(CGFloat)Y actualPosition:(NSPoint *_Nullable)pos NS_REFINED_FOR_SWIFT;

/// A PostScript autohinting font-info representation of a font master.
///
/// - Parameter fontMaster: The font master whose metrics and stems to encode.
/// - Parameter error: On failure, information about the error.
+ (nullable NSString *)fontInfoStringFontMaster:(GSFontMaster *)fontMaster error:(NSError **)error;

/// Builds a PostScript hint from a charstring stem pair.
///
/// - Parameter value1: The stem position.
/// - Parameter value2: The stem width.
/// - Parameter vertical: Whether the stem pair describes a vertical stem.
/// - Parameter doLinks: Whether to attach the hint to matching nodes when possible.
/// - Returns: The generated hint.
- (nullable GSHint *)hintsFromValue1:(CGFloat)value1 value2:(CGFloat)value2 vertical:(BOOL)vertical doLinks:(BOOL)doLinks;

@end

NS_ASSUME_NONNULL_END
