Permalink
Browse files

Merge pull request #2476 from daboe01/New-CGContextText

fixed: CoreGraphics does not have basic text drawing support
  • Loading branch information...
2 parents 4395c07 + b26c788 commit 315a7b9e6a7f542bd83ae0c35932c0dff82d53c8 @Dogild Dogild committed on GitHub Aug 30, 2016
@@ -24,6 +24,7 @@
@import "CPCompatibility.j"
@import "CGGeometry.j"
@import "CGPath.j"
+@import "CGContextText.j"
@typedef CGContext
@@ -0,0 +1,86 @@
+/*
+ * CGContextText.j
+ * CoreText
+ *
+ * Created by Nicholas Small.
+ * Copyright 2011, 280 North, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+kCGTextFill = 0;
+kCGTextStroke = 1;
+kCGTextFillStroke = 2;
+kCGTextInvisible = 3;
+
+function CGContextGetTextMatrix(/* CGContext */ aContext)
+{
+ return aContext._textMatrix;
+}
+
+function CGContextSetTextMatrix(/* CGContext */ aContext, /* CGAffineTransform */ aTransform)
+{
+ aContext._textMatrix = aTransform;
+}
+
+function CGContextGetTextPosition(/* CGContext */ aContext)
+{
+ return aContext._textPosition || _CGPointMakeZero();
+}
+
+function CGContextSetTextPosition(/* CGContext */ aContext, /* float */ x, /* float */ y)
+{
+ aContext._textPosition = CGPointMake(x, y);
+}
+
+function CGContextGetFont(/* CGContext */ aContext)
+{
+ return aContext._CPFont;
+}
+
+function CGContextSelectFont(/* CGContext */ aContext, /* CPFont */ aFont)
+{
+ aContext.font = [aFont cssString];
+ aContext._CPFont = aFont;
+}
+
+function CGContextSetTextDrawingMode(/* CGContext */ aContext, /* CGTextDrawingMode */ aMode)
+{
+ aContext._textDrawingMode = aMode;
+}
+
+function CGContextShowText(/* CGContext */ aContext, /* CPString */ aString)
+{
+ CGContextShowTextAtPoint(aContext, aContext._textPosition.x, aContext._textPosition.y, aString);
+}
+
+function CGContextShowTextAtPoint(/* CGContext */ aContext, /* float */ x, /* float */ y, /* CPString */ aString)
+{
+ aContext.textBaseline = @"middle";
+ aContext.textAlign = @"left";
+
+ var mode = aContext._textDrawingMode;
+ if (!mode && mode !== 0)
+ mode = kCGTextFill;
+
+ var width = aContext.measureText(aString).width;
+
+ if (mode === kCGTextFill || mode === kCGTextFillStroke)
+ aContext.fillText(aString, x, y);
+ if (mode === kCGTextStroke || mode === kCGTextFillStroke)
+ aContext.strokeText(aString, x, y);
+
+ aContext._textPosition = CGPointMake(x + width, y);
+}
@@ -50,6 +50,10 @@
var innerRect = CGRectInset(aRect, CGRectGetWidth(aRect)/2 - 10, CGRectGetHeight(aRect)/2 - 10);
CGContextStrokeRectWithWidth(context, innerRect, 4);
+ CGContextSetTextPosition(context, innerRect.origin.x + 10, innerRect.origin.x + 10);
+ CGContextSetFillColor(context, [CPColor blueColor]);
+ CGContextShowText(context, 'Hello World Canvas!');
+
[self unlockFocus];
}
@@ -67,7 +71,7 @@
var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
- [label setStringValue:@"Hello World!"];
+ [label setStringValue:@"Do you see the Hello World Canvas?"];
[label setFont:[CPFont boldSystemFontOfSize:24.0]];
[label sizeToFit];

0 comments on commit 315a7b9

Please sign in to comment.