Permalink
Browse files

Improved angle of text on ways. Fixes #9.

  • Loading branch information...
1 parent 5bef7a2 commit c3747ea149b7db0fe1f6515ad1236b12e07c0bf5 @beelsebob committed Jan 22, 2012
Showing with 35 additions and 11 deletions.
  1. +1 −2 OpenStreetPad/Model/API/OSPWay.h
  2. +2 −2 OpenStreetPad/Model/API/OSPWay.m
  3. +32 −7 OpenStreetPad/OSPMetaTileView.m
@@ -18,7 +18,6 @@
- (OSPCoordinate2D)projectedCentroid;
- (double)length;
-- (CGPoint)positionOnWayWithOffset:(double)xOffset heightAboveWay:(double)yOffset backwards:(BOOL)backwards;
-- (CGFloat)angleOnWayWithOffset:(CGFloat)xOffset backwards:(BOOL)backwards;
+- (OSPCoordinate2D)positionOnWayWithOffset:(double)xOffset heightAboveWay:(double)yOffset backwards:(BOOL)backwards;
@end
@@ -239,7 +239,7 @@ - (double)length
return l;
}
-- (CGPoint)positionOnWayWithOffset:(double)xOffset heightAboveWay:(double)yOffset backwards:(BOOL)backwards
+- (OSPCoordinate2D)positionOnWayWithOffset:(double)xOffset heightAboveWay:(double)yOffset backwards:(BOOL)backwards
{
[self createEdgeLengthsIfNeeded];
@@ -289,7 +289,7 @@ - (CGPoint)positionOnWayWithOffset:(double)xOffset heightAboveWay:(double)yOffse
double xCrawl = prop * dx;
double yCrawl = prop * dy;
- return CGPointMake(prevPointLocation.x + xCrawl, prevPointLocation.y + yCrawl);
+ return OSPCoordinate2DMake(prevPointLocation.x + xCrawl, prevPointLocation.y + yCrawl);
}
- (CGFloat)angleOnWayWithOffset:(CGFloat)xOffset backwards:(BOOL)backwards
@@ -736,18 +736,41 @@ - (void)drawText:(NSString *)text onWay:(OSPWay *)textWay inContext:(CGContextRe
CTRunRef run = CFArrayGetValueAtIndex(runs, runNumber);
CFIndex numGlyphs = CTRunGetGlyphCount(run);
const CGGlyph *glyphs = CTRunGetGlyphsPtr(run);
- const CGPoint *glyphPositions = CTRunGetPositionsPtr(run);
+ const CGPoint *glyphOffsets = CTRunGetPositionsPtr(run);
+ OSPCoordinate2D *glyphPositions = malloc((numGlyphs + 1) * sizeof(OSPCoordinate2D));
+ CGFloat *glyphAngles = malloc(numGlyphs * sizeof(CGFloat));
+
+ CGPoint currentGlyphOffset;
+ for (CFIndex glyphNumber = 0; glyphNumber < numGlyphs; glyphNumber++)
+ {
+ currentGlyphOffset = glyphOffsets[glyphNumber];
+ glyphPositions[glyphNumber] = [textWay positionOnWayWithOffset:wayOffset + currentGlyphOffset.x heightAboveWay:currentGlyphOffset.y - scaledOffset backwards:backwards];
+ }
+ glyphPositions[numGlyphs] = [textWay positionOnWayWithOffset:wayOffset + lineWidth heightAboveWay:-scaledOffset backwards:backwards];
+ OSPCoordinate2D currentGlyphPosition = glyphPositions[0];
+ for (CFIndex glyphNumber = 0; glyphNumber < numGlyphs; glyphNumber++)
+ {
+ OSPCoordinate2D nextGlyphPosition = glyphPositions[glyphNumber+1];
+
+ double dx = nextGlyphPosition.x - currentGlyphPosition.x;
+ double dy = nextGlyphPosition.y - currentGlyphPosition.y;
+ glyphAngles[glyphNumber] = dx > 0.0 ? (dy > 0.0 ? atan(dy / dx) : -atan(-dy / dx))
+ : dx < 0.0 ? (dy > 0.0 ? M_PI - atan(dy / -dx) : M_PI + atan(-dy / -dx))
+ : (dy < 0.0 ? 3 * M_PI_2 : M_PI_2);
+
+
+ currentGlyphPosition = nextGlyphPosition;
+ }
if (hasHalo)
{
CGContextSetTextDrawingMode(ctx, kCGTextStroke);
for (CFIndex glyphNumber = 0; glyphNumber < numGlyphs; glyphNumber++)
{
CGGlyph glyph = glyphs[glyphNumber];
- CGPoint glyphPosition = glyphPositions[glyphNumber];
+ OSPCoordinate2D p = glyphPositions[glyphNumber];
- CGPoint p = [textWay positionOnWayWithOffset:wayOffset + glyphPosition.x heightAboveWay:glyphPosition.y - scaledOffset backwards:backwards];
- CGContextSetTextMatrix(ctx, CGAffineTransformConcat(CGAffineTransformMakeRotation(-[textWay angleOnWayWithOffset:wayOffset + glyphPosition.x backwards:backwards]), CGAffineTransformMakeScale(1.0, -1.0)));
+ CGContextSetTextMatrix(ctx, CGAffineTransformConcat(CGAffineTransformMakeRotation(-glyphAngles[glyphNumber]), CGAffineTransformMakeScale(1.0, -1.0)));
CGContextSetTextPosition(ctx, p.x, p.y);
CGContextShowGlyphs(ctx, &glyph, 1);
}
@@ -756,13 +779,15 @@ - (void)drawText:(NSString *)text onWay:(OSPWay *)textWay inContext:(CGContextRe
for (CFIndex glyphNumber = 0; glyphNumber < numGlyphs; glyphNumber++)
{
CGGlyph glyph = glyphs[glyphNumber];
- CGPoint glyphPosition = glyphPositions[glyphNumber];
+ OSPCoordinate2D p = glyphPositions[glyphNumber];
- CGPoint p = [textWay positionOnWayWithOffset:wayOffset + glyphPosition.x heightAboveWay:glyphPosition.y - scaledOffset backwards:backwards];
- CGContextSetTextMatrix(ctx, CGAffineTransformConcat(CGAffineTransformMakeRotation(-[textWay angleOnWayWithOffset:wayOffset + glyphPosition.x backwards:backwards]), CGAffineTransformMakeScale(1.0, -1.0)));
+ CGContextSetTextMatrix(ctx, CGAffineTransformConcat(CGAffineTransformMakeRotation(-glyphAngles[glyphNumber]), CGAffineTransformMakeScale(1.0, -1.0)));
CGContextSetTextPosition(ctx, p.x, p.y);
CGContextShowGlyphs(ctx, &glyph, 1);
}
+
+ free(glyphPositions);
+ free(glyphAngles);
}
}

0 comments on commit c3747ea

Please sign in to comment.