You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/04/17 05:49:11 UTC

[07/50] [abbrv] incubator-weex git commit: * [ios] draw line-through

* [ios] draw line-through


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/a95a0623
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/a95a0623
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/a95a0623

Branch: refs/heads/dev
Commit: a95a0623f1fb185009b55da27326d3c056bf8b70
Parents: 2b548d4
Author: acton393 <zh...@gmail.com>
Authored: Wed Apr 12 01:57:22 2017 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Wed Apr 12 01:57:22 2017 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXTextComponent.m | 82 ++++++++++++++++++--
 1 file changed, 74 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/a95a0623/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
index 2a81b9e..09e61e1 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
@@ -136,7 +136,7 @@ static BOOL textRenderUsingCoreText;
     self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance];
     if (self) {
         // just for coretext and textkit render replacement
-        if (attributes[@"coretext"]) {
+        if ([attributes objectForKey:@"coretext"]) {
             _coretext = [WXConvert BOOL:attributes[@"coretext"]];
         } else {
             _coretext = YES;
@@ -265,11 +265,15 @@ do {\
 {
     __weak typeof(self) weakSelf = self;
     return ^CGSize (CGSize constrainedSize) {
+        CGSize computedSize = CGSizeZero;
         NSTextStorage *textStorage = [weakSelf textStorageWithWidth:constrainedSize.width];
-        
-        NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
-        NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
-        CGSize computedSize = [layoutManager usedRectForTextContainer:textContainer].size;
+        if (!(weakSelf.coretext && [WXTextComponent textRenderUsingCoreText]) ) {
+            NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
+            NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
+            computedSize = [layoutManager usedRectForTextContainer:textContainer].size;
+        } else {
+            computedSize = [[weakSelf buildCTAttributeString] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
+        }
         
         //TODO:more elegant way to use max and min constrained size
         if (!isnan(weakSelf.cssNode->style.minDimensions[CSS_WIDTH])) {
@@ -562,7 +566,7 @@ do {\
         CGRect textFrame = UIEdgeInsetsInsetRect(bounds, padding);
         // sufficient height for text to draw, or frame lines will be empty
         textFrame.size.height = bounds.size.height*2;
-        
+        CGContextSaveGState(context);
         //flip the coordinate system
         CGContextSetTextMatrix(context, CGAffineTransformIdentity);
         CGContextTranslateCTM(context, 0, textFrame.size.height);
@@ -580,7 +584,6 @@ do {\
         CFIndex lineCount = CFArrayGetCount(lines);
         CGPoint lineOrigins[lineCount];
         CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);
-        
         for (CFIndex index = 0; index < lineCount; index ++) {
             CTLineRef lineRef = CFArrayGetValueAtIndex(lines, index);
             CGFloat lineAscent;
@@ -592,17 +595,80 @@ do {\
             lineOrigin.x += padding.left;
             lineOrigin.y -= padding.top;
             CGContextSetTextPosition(context, lineOrigin.x, lineOrigin.y);
-            //            CTLineDraw(lineRef, context);
             CFArrayRef runs = CTLineGetGlyphRuns(lineRef);
+            CGFloat xHeight = 0, underLinePosition = 0, lineThickness = 0 ;
+            WXTextGetRunsMaxMetric(runs, &xHeight, &underLinePosition, &lineThickness);
+            CGPoint strikethroughStart;
+            strikethroughStart.x =  lineOrigin.x - underLinePosition;
+            strikethroughStart.y = lineOrigin.y + xHeight/2;
             for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runs); runIndex ++) {
                 CTRunRef run = CFArrayGetValueAtIndex(runs, runIndex);
                 CTRunDraw(run, context, CFRangeMake(0, 0));
+                CFDictionaryRef attr = CTRunGetAttributes(run);
+                NSUnderlineStyle strikethrough = (NSUnderlineStyle)CFDictionaryGetValue(attr, NSStrikethroughStyleAttributeName);
+                
+                if (strikethrough) {
+                    // currently draw strikethrough
+                    CGPoint runPosition = CGPointZero;
+                    CTRunGetPositions(run, CFRangeMake(0, 1), &runPosition);
+                    strikethroughStart.x = lineOrigin.x + runPosition.x;
+                    CGContextSetLineWidth(context, 1.5);
+                    double length = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), NULL, NULL, NULL);
+                    CGContextMoveToPoint(context, strikethroughStart.x, strikethroughStart.y);
+                    CGContextAddLineToPoint(context, strikethroughStart.x + length, strikethroughStart.y);
+                    CGContextStrokePath(context);
+                }
+                
             }
         }
         
         CFRelease(frame);
         CFRelease(framesetter);
         CGPathRelease(path);
+        
+        CGContextRestoreGState(context);
+    }
+}
+
+static void WXTextGetRunsMaxMetric(CFArrayRef runs, CGFloat *xHeight, CGFloat *underlinePosition, CGFloat *lineThickness) {
+    CGFloat maxXHeight = 0;
+    CGFloat maxUnderlinePos = 0;
+    CGFloat maxLineThickness = 0;
+    for (NSUInteger index = 0, runsCount = CFArrayGetCount(runs); index < runsCount; index ++) {
+        CTRunRef run = CFArrayGetValueAtIndex(runs, index);
+        CFDictionaryRef attrs = CTRunGetAttributes(run);
+        if (attrs) {
+            CTFontRef font = CFDictionaryGetValue(attrs, kCTFontAttributeName);
+            if (font) {
+                
+                CGFloat xHeight = CTFontGetXHeight(font);
+                if (xHeight > maxXHeight) {
+                    maxXHeight = xHeight;
+                }
+                
+                CGFloat underlinePos = CTFontGetUnderlinePosition(font);
+                if (underlinePos < maxUnderlinePos) {
+                    maxUnderlinePos = underlinePos;
+                }
+                
+                CGFloat lineThickness = CTFontGetUnderlineThickness(font);
+                if (lineThickness > maxLineThickness) {
+                    maxLineThickness = lineThickness;
+                }
+            }
+        }
+    }
+    
+    if (xHeight) {
+        *xHeight = maxXHeight;
+    }
+    
+    if (underlinePosition) {
+        *underlinePosition = maxUnderlinePos;
+    }
+    
+    if (lineThickness) {
+        *lineThickness = maxLineThickness;
     }
 }