Skip to content
Browse files

Implemented proper underline and stroke-out position and thickness

  • Loading branch information...
1 parent 2532160 commit 1afd065adda357d34a9f85085cde37220cd3306d @odrobnik odrobnik committed Mar 16, 2013
Showing with 48 additions and 15 deletions.
  1. +45 −14 Core/Source/DTCoreTextLayoutFrame.m
  2. +3 −1 Demo/Resources/CurrentTest.html
View
59 Core/Source/DTCoreTextLayoutFrame.m
@@ -1046,24 +1046,55 @@ - (void)drawInContext:(CGContextRef)context drawImages:(BOOL)drawImages drawLink
CGContextFillRect(context, runStrokeBounds);
}
- if (drawStrikeOut)
+ if (drawStrikeOut || drawUnderline)
{
- CGFloat y = roundf(runStrokeBounds.origin.y + oneRun.frame.size.height/2.0f + 1)+0.5f;
+ CTFontRef usedFont = (__bridge CTFontRef)([oneRun.attributes objectForKey:(id)kCTFontAttributeName]);
- CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
- CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
-
- CGContextStrokePath(context);
- }
-
- if (drawUnderline)
- {
- CGFloat y = roundf(runStrokeBounds.origin.y + runStrokeBounds.size.height - oneRun.descent + 1)+0.5f;
+ if (usedFont)
+ {
+ CGFloat underlineThickness = CTFontGetUnderlineThickness(usedFont);
+ CGContextSetLineWidth(context, underlineThickness);
+ }
- CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
- CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
+ if (drawStrikeOut)
+ {
+ CGFloat y;
+
+ if (usedFont)
+ {
+ CGFloat strokePosition = CTFontGetXHeight(usedFont)/2.0;
+ y = runStrokeBounds.origin.y + oneRun.ascent - strokePosition;
+ }
+ else
+ {
+ y = roundf(runStrokeBounds.origin.y + oneRun.frame.size.height/2.0f + 1)+0.5f;
+ }
+
+ CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
+ CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
+
+ CGContextStrokePath(context);
+ }
- CGContextStrokePath(context);
+ if (drawUnderline)
+ {
+ CGFloat y;
+
+ if (usedFont)
+ {
+ CGFloat underlinePosition = CTFontGetUnderlinePosition(usedFont);
+ y = runStrokeBounds.origin.y + runStrokeBounds.size.height - oneRun.descent - underlinePosition;
+ }
+ else
+ {
+ y = roundf(runStrokeBounds.origin.y + runStrokeBounds.size.height - oneRun.descent + 1)+0.5f;
+ }
+
+ CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
+ CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
+
+ CGContextStrokePath(context);
+ }
}
}
}
View
4 Demo/Resources/CurrentTest.html
@@ -1,6 +1,8 @@
<!-- shows different unline position in a hyperlink that has a part with superscript text -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">
<html>
- <body><a href="https://www.cocoanetics.com"><img class="Bla" style="width:150px; height:150px" src="Oliver.jpg"></a> <a href="https://www.cocoanetics.com">Normal</a>
+ <p style="font-family:Arial">Some <u>underlined</u> and <del>stroked</del> text</p>
+ <p style="font-family:Arial;font-size:20px">Some <u>underlined</u> and <del>stroked</del> text</p>
+ <p style="font-family:Arial;font-size:40px">Some <u>underlined</u> and <del>stroked</del> text</p>
</body>
</html>

0 comments on commit 1afd065

Please sign in to comment.
Something went wrong with that request. Please try again.