1/* 2 * Copyright (c) 2007 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23/* 24 * testcgtext.c 25 * bless 26 * 27 * Created by Shantonu Sen on 9/7/06. 28 * Copyright 2006 __MyCompanyName__. All Rights Reserved. 29 * 30 */ 31 32#include <ApplicationServices/ApplicationServices.h> 33 34int main1(int argc, char *argv[]) { 35 36 char *path; 37 char *str; 38 CFStringRef s1; 39 CFAttributedStringRef s2; 40 CTLineRef ct1; 41 42 CGContextRef context; 43 CFURLRef url; 44 CGRect rect; 45 46 if(argc != 3) { 47 fprintf(stderr, "Usage: %s file string\n", getprogname()); 48 exit(1); 49 } 50 51 path = argv[1]; 52 str = argv[2]; 53 54 s1 = CFStringCreateWithCString(kCFAllocatorDefault, str, kCFStringEncodingUTF8); 55 s2 = CFAttributedStringCreate(kCFAllocatorDefault,s1,NULL); 56 CFRelease(s1); 57 58 CFShow(s2); 59 60 ct1 = CTLineCreateWithAttributedString(s2); 61 CFRelease(s2); 62 63 CFShow(ct1); 64 65 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)path, strlen(path), false); 66 CFShow(url); 67 68 bzero(&rect, sizeof(rect)); 69 rect.size.width = 500; 70 rect.size.height = 400; 71 72 //context = CGPDFContextCreateWithURL(url, &rect, NULL); 73 char *data = calloc(100*100, 1); 74 CGColorSpaceRef colorSpace = NULL; 75 76 colorSpace = CGColorSpaceCreateDeviceGray(); 77 78 context = CGBitmapContextCreate( data, 79 100, 80 100, 81 8, 82 100*1, 83 colorSpace, 84 kCGImageAlphaNone); 85 CGColorSpaceRelease(colorSpace); 86 87 CFShow(context); 88 89 //CGPDFContextBeginPage(context, NULL); 90 91 rect.origin.x = 10; 92 rect.origin.y = 15; 93 rect.size.width = 10; 94 rect.size.height = 10; 95 96// CGContextSetGrayFillColor(context, 1.0, 1.0); 97 CGContextFillRect(context, rect); 98 99 CFRelease(url); 100 101 CGContextSetTextDrawingMode(context, kCGTextFill); 102 CGContextSetTextPosition(context, 4.0, 4.0); 103 CGContextSetFontSize(context, 10.0); 104 // CGContextSelectFont(context, "Helvetica", 10.0, kCGEncodingFontSpecific); 105 CGContextSetShouldAntialias(context, 1); 106 CGContextSetCharacterSpacing(context, 0.5); 107 108 rect = CTLineGetImageBounds(ct1, context); 109 printf("[%f,%f] (%f,%f)\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); 110 111 112// CTLineDraw(ct1, context); 113 CGContextShowTextAtPoint(context, 2.0, 2.0, str, strlen(str)); 114 115 CGContextFlush(context); 116 117// CGPDFContextEndPage(context); 118// CGPDFContextClose(context); 119 120 CFRelease(context); 121 CFRelease(ct1); 122 123 write(1, data, 100*100); 124 125 return 0; 126} 127 128static int makeLabelOfSize(const char *label, unsigned char *bitmapData, 129uint16_t width, uint16_t height, uint16_t *newwidth) { 130 131 int bitmapByteCount; 132 int bitmapBytesPerRow; 133 134 CGContextRef context = NULL; 135 CGColorSpaceRef colorSpace = NULL; 136 137 138 bitmapBytesPerRow = width*1; 139 bitmapByteCount = bitmapBytesPerRow * height; 140 141 colorSpace = CGColorSpaceCreateDeviceGray(); 142 143 144 context = CGBitmapContextCreate( bitmapData, 145 width, 146 height, 147 8, 148 bitmapBytesPerRow, 149 colorSpace, 150 kCGImageAlphaNone); 151 152 if(context == NULL) { 153 fprintf(stderr, "Could not init CoreGraphics context\n"); 154 return 1; 155 } 156 157// CGContextSetTextDrawingMode(context, kCGTextFill); 158 // CGContextSelectFont(context, "Helvetica", 10.0, kCGEncodingFontSpecific); 159 // CGContextSetGrayFillColor(context, 1.0, 1.0); 160 // CGContextSetShouldAntialias(context, 1); 161 // CGContextSetCharacterSpacing(context, 0.5); 162 163 const CGFloat components[] = { 164 1.0, 1.0 165 }; 166// CGContextSetFillColor(context, components); 167 168 169#define USE_CORETEXT 1 170#if USE_CORETEXT 171 { 172 CFStringRef s1; 173 CFAttributedStringRef s2; 174 CTLineRef ct1; 175 CGRect rect; 176 CFMutableDictionaryRef dict; 177 178 CGColorRef color = CGColorCreate(colorSpace, components); 179 180 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 181 CFDictionarySetValue(dict, kCTForegroundColorAttributeName, color); 182 CFRelease(color); 183 184 s1 = CFStringCreateWithCString(kCFAllocatorDefault, label, kCFStringEncodingUTF8); 185 s2 = CFAttributedStringCreate(kCFAllocatorDefault,s1,dict); 186 CFRelease(s1); 187 188 ct1 = CTLineCreateWithAttributedString(s2); 189 CFRelease(s2); 190 191 rect = CTLineGetImageBounds(ct1, context); 192 193 CGContextSetTextPosition(context, 2.0, 2.0); 194 CFShow(ct1); 195 CTLineDraw(ct1, context); 196 197// CGContextFlush(context); 198 199 CFRelease(ct1); 200 201 if(newwidth) { *newwidth = (int)rect.size.width + 4; } 202 printf("[%f,%f] (%f,%f)\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); 203 204 } 205#else 206 { 207 CGPoint pt; 208 209 pt = CGContextGetTextPosition(context); 210 211 CGContextShowTextAtPoint(context, 2.0, 2.0, label, strlen(label)); 212 213 pt = CGContextGetTextPosition(context); 214 215 if(newwidth) { *newwidth = (int)pt.x + 2; } 216 217 } 218#endif 219 220 221 222 223 CGColorSpaceRelease(colorSpace); 224 CGContextRelease(context); 225 226 227 return 0; 228 229} 230 231 232int main(int argc, char *argv[]) { 233 char *path; 234 char *str; 235 int fd; 236 unsigned char *bitmapData = NULL; 237 238 uint16_t width = 40; 239 uint16_t height = 12; 240 uint16_t newwidth; 241 242 if(argc != 3) { 243 fprintf(stderr, "Usage: %s file string\n", getprogname()); 244 exit(1); 245 } 246 247 path = argv[1]; 248 str = argv[2]; 249 250 fd = open(path, O_WRONLY|O_TRUNC, 0600); 251 if(fd < 0) 252 err(1, "open"); 253 254 bitmapData = calloc(width*height*4, 1); 255 256 makeLabelOfSize(str, bitmapData, width, height, &newwidth); 257 258 write(fd, bitmapData, height*width*4); 259 260 261 close(fd); 262 263 return 0; 264}