domenica 28 settembre 2014

Trim Marks at distance

FitPlot 5.1

Trim Mark Offset Implementation

As requested from a user, It has been easy to add an offset distance value to be used together when trim marks style is selected.

Trim marks are thin lines that when printed facilitate the cutting.


Now these thin lines can be distanced from the border of a user given value (in the current unit of measure).
Negative values move lines inside (sliding on relative border fixed distance).
Positive values move outside.

 offset = -1(cm)     offset = +1(cm)

To insert the trim marks offset value, it may be necessary to expand the info panel.
The field is enabled when trim marks style is selected.



Don't worry about packing or aligning, trim marks are considered in the bound of an image (since version 5).

Here's the code (called by - (void) drawElement: (NSRect) inRect inContext: (CGContextRef) contextCG in the FP_Image class).

#pragma mark DRAW STYLES
- (void) drawStyles
{
    NSInteger trueStyle = [self imageStyle];
    if ([self linkIsBroken]) //trattamento speciale per fileNotFound
        [self setImageStyle:2];
    
    [self impostaStile: imageStyle];
    
    //ricavo l'indice dash
    NSInteger iDash = styleDash/8-1;
    NSInteger iStroke = styleStroke/32-1;
    
    float scala = [self imageScale];
    float spessore = 0;
    if (iStroke >=0) spessore = [[[[NSApp delegate] strokes ] objectAtIndex: iStroke] floatValue] / scala;
    
    //trim
    if (styleTrim) {
        [[NSColor blackColor] set];
        NSBezierPath *path = [NSBezierPath bezierPath];
        
        float trimSliding = imageTrimOffset/scala;
        float trimOffset = imageTrimOffset > 0 ? trimSliding:0;
        
        // crocino alto / sx
        [path moveToPoint: NSMakePoint([self cropRect].origin.x-trimOffset-CM2PX(0.2)/scala,
                                       [self cropRect].origin.y-trimSliding)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x-trimOffset-CM2PX(0.7)/scala,
                                       [self cropRect].origin.y-trimSliding)];

        [path moveToPoint: NSMakePoint([self cropRect].origin.x-trimSliding,
                                       [self cropRect].origin.y-trimOffset-CM2PX(0.2)/scala)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x-trimSliding,
                                       [self cropRect].origin.y-trimOffset-CM2PX(0.7)/scala)];

        //crocino basso / sx
        [path moveToPoint: NSMakePoint([self cropRect].origin.x-trimOffset-CM2PX(0.2)/scala,
                                       [self cropRect].origin.y+trimSliding+[self cropRect].size.height)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x-trimOffset-CM2PX(0.7)/scala,
                                       [self cropRect].origin.y+trimSliding+[self cropRect].size.height)];
        
        [path moveToPoint: NSMakePoint([self cropRect].origin.x-trimSliding,
                                       [self cropRect].origin.y+trimOffset+[self cropRect].size.height+CM2PX(0.2)/scala)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x-trimSliding,
                                       [self cropRect].origin.y+trimOffset+[self cropRect].size.height+CM2PX(0.7)/scala)];

        // crocino alto / dx
        [path moveToPoint: NSMakePoint([self cropRect].origin.x+trimOffset+[self cropRect].size.width+CM2PX(0.2)/scala,
                                       [self cropRect].origin.y-trimSliding)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x+trimOffset+[self cropRect].size.width+CM2PX(0.7)/scala,
                                       [self cropRect].origin.y-trimSliding)];
        
        [path moveToPoint: NSMakePoint([self cropRect].origin.x+trimSliding+[self cropRect].size.width,
                                       [self cropRect].origin.y-trimOffset-CM2PX(0.2)/scala)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x+trimSliding+[self cropRect].size.width,
                                       [self cropRect].origin.y-trimOffset- CM2PX(0.7)/scala)];
        
        // crocino basso / dx
        [path moveToPoint: NSMakePoint([self cropRect].origin.x+[self cropRect].size.width+trimOffset+CM2PX(0.2)/scala,
                                       [self cropRect].origin.y+[self cropRect].size.height+trimSliding)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x+[self cropRect].size.width+trimOffset+CM2PX(0.7)/scala,
                                       [self cropRect].origin.y+[self cropRect].size.height+trimSliding)];
        
        [path moveToPoint: NSMakePoint([self cropRect].origin.x+trimSliding+[self cropRect].size.width,
                                       [self cropRect].origin.y+trimOffset+[self cropRect].size.height+CM2PX(0.2)/scala)];
        [path lineToPoint: NSMakePoint([self cropRect].origin.x+trimSliding+[self cropRect].size.width,
                                       [self cropRect].origin.y+trimOffset+[self cropRect].size.height+CM2PX(0.7)/scala)];

        [path setLineWidth: HAIRLINE/scala];
        [path stroke];
        
        NSBezierPath * tempPath = [NSBezierPath bezierPathWithRect:NSMakeRect(cropRect.origin.x-CM2PX(0.7)/scala-trimOffset,
                                                                              cropRect.origin.y-CM2PX(0.7)/scala-trimOffset,
                                                                              cropRect.size.width+2*CM2PX(0.7)/scala+trimOffset*2,
                                                                              cropRect.size.height+2*CM2PX(0.7)/scala+trimOffset*2)] ;

        NSAffineTransform * transform = [ NSAffineTransform transform] ;
        [transform appendTransform:localTF];
        [tempPath transformUsingAffineTransform:transform];
        
        [self setBounds:NSUnionRect([tempPath bounds], [self bounds])]; //aumento i bounds considerando le linee di taglio
    }
    
    if (iDash >= 0)
    {
        [[NSColor blackColor] set];
        NSBezierPath *contourRect = [NSBezierPath bezierPathWithRect:[self cropRect]];
        
        if (styleDash == 32) {
            CGFloat array [4];
            array[0] = [[[[NSApp delegate] dashes ] objectAtIndex: iDash] floatValue]/scala;
            array[1] =  [[[[NSApp delegate] dashgaps ] objectAtIndex: iDash] floatValue]/scala;
            array[2] = array[0]/4;
            array[3] =  array[1];
            [contourRect setLineDash: array count: 4 phase: 0.0];
        }
        else
        {
            CGFloat array [2];
            array[0] = [[[[NSApp delegate] dashes ] objectAtIndex: iDash] floatValue]/scala;
            array[1] =  [[[[NSApp delegate] dashgaps ] objectAtIndex: iDash] floatValue]/scala;
            [contourRect setLineDash: array count: 2 phase: 0.0];
        }
        
        [contourRect setLineWidth: spessore];
        [contourRect stroke];
    }
    
    if (iStroke >= 0 && iDash <0)
    {
        [[NSColor blackColor] set];
        NSBezierPath *contourRect = [NSBezierPath bezierPathWithRect:[self cropRect]];
        
        [contourRect setLineWidth: spessore];
        [contourRect stroke];
    }
    
    
    NSBezierPath * tempPath = [NSBezierPath bezierPathWithRect:NSMakeRect(cropRect.origin.x-spessore/2,
                                                                          cropRect.origin.y-spessore/2,
                                                                          cropRect.size.width+spessore,
                                                                          cropRect.size.height+spessore)] ;
    
    
    NSAffineTransform * transform = [ NSAffineTransform transform] ;
    //[ transform scaleBy: 1/imageGroupScale];

    [transform appendTransform:localTF];
    
    [tempPath transformUsingAffineTransform:transform];
    
    [self setBounds:NSUnionRect([tempPath bounds], [self bounds])];  //aumento i bounds considerando lo spessore
    //NSLog(@"setBounds in drawStyle");
    
    
    //NSGraphicsContext *contextStyle = [NSGraphicsContext currentContext];
    //[contextStyle saveGraphicsState];
    
    [self setImageStyle:trueStyle];
    
    //[contextStyle restoreGraphicsState];
}



Nessun commento:

Posta un commento