-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathArcView.c
479 lines (380 loc) · 16.2 KB
/
ArcView.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
File: ArcView.c
Abstract: Defines and implements the ArcView custom HIView subclass to
draw text on a curve and illustrate best practices with CoreText.
Version: 1.0
Disclaimer: IMPORTANT: This Apple software is supplied to you by
Apple Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms. If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc.
may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple. Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2007 Apple Inc. All Rights Reserved.
*/
#include "ArcView.h"
#define ARCVIEW_DEFAULT_FONT_NAME CFSTR("Didot")
#define ARCVIEW_DEFAULT_FONT_SIZE 64.0
#define ARCVIEW_DEFAULT_RADIUS 150.0
struct ArcView {
HIObjectRef object;
CTFontRef font; // retained
CFStringRef string; // retained
CGFloat radius;
OptionBits options;
CGContextRef context; // not retained
};
typedef struct GlyphArcInfo {
CGFloat width;
CGFloat angle; // in radians
} GlyphArcInfo;
static CTFontRef ArcViewCreateDefaultFont(void);
static void ArcViewDispose(ArcView *arcView);
static ArcView *ArcViewCreate(HIObjectRef object);
static void PrepareGlyphArcInfo(CTLineRef line, CFIndex glyphCount, GlyphArcInfo *glyphArcInfo);
static CFAttributedStringRef ArcViewCreateAttributedString(ArcView *arcView);
static OSStatus ArcViewEventHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon);
static HIObjectClassRef sArcViewClass = NULL;
// Register the arc view class. This should be done very early, before the nib is loaded.
OSStatus ArcViewRegisterClass(void)
{
static const EventTypeSpec kArcViewEvents[] = {
{ kEventClassHIObject, kEventHIObjectConstruct },
{ kEventClassHIObject, kEventHIObjectDestruct },
{ kEventClassControl, kEventControlDraw }
};
return HIObjectRegisterSubclass(kArcViewClassID, kHIViewClassID, 0, (EventHandlerUPP)ArcViewEventHandler, GetEventTypeCount(kArcViewEvents), kArcViewEvents, NULL, &sArcViewClass);
}
// Convenience to get the arc view for the specified window.
ArcView *GetArcViewForWindow(HIWindowRef window)
{
static const HIViewID arcID = { kArcViewControlSignature, kArcViewControlID };
HIViewRef view;
verify_noerr(HIViewFindByID(HIViewGetRoot(window), arcID, &view));
return HIObjectDynamicCast((HIObjectRef)view, kArcViewClassID);
}
// Set the font for the arc view. This properly releases the existing font and marks the view as needing display.
void ArcViewSetFont(ArcView *arcView, CTFontRef font)
{
if (arcView->font != font) {
if (arcView->font != NULL)
CFRelease(arcView->font);
arcView->font = (CTFontRef)CFRetain(font);
HIViewSetNeedsDisplay((HIViewRef)arcView->object, true);
}
}
// Get the current font.
CTFontRef ArcViewGetFont(const ArcView *arcView)
{
return arcView->font;
}
// Set the content string for the arc view. This properly releases the existing string and marks the view as needing display.
void ArcViewSetString(ArcView *arcView, CFStringRef string)
{
if (arcView->string != string) {
if (arcView->string != NULL)
CFRelease(arcView->string);
arcView->string = (CFStringRef)CFRetain(string);
HIViewSetNeedsDisplay((HIViewRef)arcView->object, true);
}
}
// Get the current content string.
CFStringRef ArcViewGetString(const ArcView *arcView)
{
return arcView->string;
}
// Set the state for the specified option(s)
void ArcViewSetOptions(ArcView *arcView, ArcViewOptions optionsMask, Boolean state)
{
if (state) {
arcView->options |= optionsMask;
} else {
arcView->options &= ~optionsMask;
}
HIViewSetNeedsDisplay((HIViewRef)arcView->object, true);
}
// Returns true if specified options are enabled
Boolean ArcViewGetOptions(ArcView *arcView, ArcViewOptions optionsMask)
{
return (arcView->options & optionsMask) == optionsMask;
}
// Draw the view in the provided context. This should not be called directly as it draws in response events.
void ArcViewDraw(ArcView *arcView, CGContextRef context)
{
HIRect bounds;
CFIndex glyphCount;
GlyphArcInfo * glyphArcInfo;
// Don't draw if we don't have a font or string
if (arcView->font == NULL || arcView->string == NULL)
return;
verify_noerr(HIViewGetBounds((HIViewRef)arcView->object, &bounds));
// Transform HIView coordinates to Quartz coordinates
CGContextTranslateCTM(context, 0, bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Draw a white background
CGContextSetFillColorWithColor(context, CGColorGetConstantColor(kCGColorWhite));
CGContextFillRect(context, bounds);
// Create the attributed string
CFAttributedStringRef attrString = ArcViewCreateAttributedString(arcView);
assert(attrString != NULL);
CTLineRef line = CTLineCreateWithAttributedString(attrString);
CFRelease(attrString);
assert(line != NULL);
glyphCount = CTLineGetGlyphCount(line);
if (glyphCount == 0) {
CFRelease(line);
return;
}
glyphArcInfo = (GlyphArcInfo*)calloc(glyphCount, sizeof(GlyphArcInfo));
PrepareGlyphArcInfo(line, glyphCount, glyphArcInfo);
// Move the origin from the lower left of the view nearer to its center.
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMidX(bounds), CGRectGetMidY(bounds) - arcView->radius / 2.0);
// Stroke the arc in red for verification.
CGContextBeginPath(context);
CGContextAddArc(context, 0.0, 0.0, arcView->radius, M_PI, 0.0, 1);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextStrokePath(context);
// Rotate the context 90 degrees counterclockwise.
CGContextRotateCTM(context, M_PI_2);
// Now for the actual drawing. The angle offset for each glyph relative to the previous glyph has already been calculated; with that information in hand, draw those glyphs overstruck and centered over one another, making sure to rotate the context after each glyph so the glyphs are spread along a semicircular path.
CGPoint textPosition = CGPointMake(0.0, arcView->radius);
CGContextSetTextPosition(context, textPosition.x, textPosition.y);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
CFIndex runCount = CFArrayGetCount(runArray);
CFIndex glyphOffset = 0;
CFIndex runIndex = 0;
for (; runIndex < runCount; runIndex++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CFIndex runGlyphCount = CTRunGetGlyphCount(run);
Boolean drawSubstitutedGlyphsManually = false;
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
// Determine if we need to draw substituted glyphs manually. Do so if the runFont is not the same as the overall font.
if ((arcView->options & kArcViewDimSubstitutedGlyphsOption) !=0 && !CFEqual(arcView->font, runFont)) {
drawSubstitutedGlyphsManually = true;
}
CFIndex runGlyphIndex = 0;
for (; runGlyphIndex < runGlyphCount; runGlyphIndex++) {
CFRange glyphRange = CFRangeMake(runGlyphIndex, 1);
CGContextRotateCTM(context, -(glyphArcInfo[runGlyphIndex + glyphOffset].angle));
// Center this glyph by moving left by half its width.
CGFloat glyphWidth = glyphArcInfo[runGlyphIndex + glyphOffset].width;
CGFloat halfGlyphWidth = glyphWidth / 2.0;
CGPoint positionForThisGlyph = CGPointMake(textPosition.x - halfGlyphWidth, textPosition.y);
// Glyphs are positioned relative to the text position for the line, so offset text position leftwards by this glyph's width in preparation for the next glyph.
textPosition.x -= glyphWidth;
CGAffineTransform textMatrix = CTRunGetTextMatrix(run);
textMatrix.tx = positionForThisGlyph.x;
textMatrix.ty = positionForThisGlyph.y;
CGContextSetTextMatrix(context, textMatrix);
if (!drawSubstitutedGlyphsManually) {
CTRunDraw(run, context, glyphRange);
}
else {
// We need to draw the glyphs manually in this case because we are effectively applying a graphics operation by setting the context fill color. Normally we would use kCTForegroundColorAttributeName, but this does not apply as we don't know the ranges for the colors in advance, and we wanted demonstrate how to manually draw.
CGFontRef cgFont = CTFontCopyGraphicsFont(runFont, NULL);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, glyphRange, &glyph);
CTRunGetPositions(run, glyphRange, &position);
CGContextSetFont(context, cgFont);
CGContextSetFontSize(context, CTFontGetSize(runFont));
CGContextSetRGBFillColor(context, 0.25, 0.25, 0.25, 0.5);
CGContextShowGlyphsAtPositions(context, &glyph, &position, 1);
CFRelease(cgFont);
}
// Draw the glyph bounds
if ((arcView->options & kArcViewShowGlyphBoundsOption) != 0) {
CGRect glyphBounds = CTRunGetImageBounds(run, context, glyphRange);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, glyphBounds);
}
// Draw the bounding boxes defined by the line metrics
if ((arcView->options & kArcViewShowLineMetricsOption) != 0) {
CGRect lineMetrics;
CGFloat ascent, descent;
CTRunGetTypographicBounds(run, glyphRange, &ascent, &descent, NULL);
// The glyph is centered around the y-axis
lineMetrics.origin.x = -halfGlyphWidth;
lineMetrics.origin.y = positionForThisGlyph.y - descent;
lineMetrics.size.width = glyphWidth;
lineMetrics.size.height = ascent + descent;
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRect(context, lineMetrics);
}
}
glyphOffset += runGlyphCount;
}
CGContextRestoreGState(context);
free(glyphArcInfo);
CFRelease(line);
}
// Handles events for the ArcView HIObject subclass. The only event we really care about is the control drawing event to trigger drawing of content.
static OSStatus ArcViewEventHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon)
{
OSStatus result = eventNotHandledErr;
ArcView *arcView = (ArcView *)inRefcon;
OSType class = GetEventClass(inEvent);
UInt32 kind = GetEventKind(inEvent);
switch (class) {
case kEventClassHIObject:
{
switch (kind) {
case kEventHIObjectConstruct:
{
HIObjectRef object;
// Get the HIObject from the event
result = GetEventParameter(inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, sizeof(HIObjectRef), NULL, &object);
require_noerr(result, ParameterMissing);
// Create the ArcView
arcView = ArcViewCreate(object);
require_action(arcView != NULL, CantCreateArcView, result = memFullErr);
// Set the ArcView as the data for the object
verify_noerr(SetEventParameter(inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof(void *), &arcView));
break;
}
case kEventHIObjectDestruct:
{
// Dispose the ArcView
ArcViewDispose(arcView);
break;
}
default:
break;
}
break;
}
case kEventClassControl:
{
switch (kind) {
case kEventControlDraw:
{
CGContextRef context;
// Get the context from the event
verify_noerr(GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(CGContextRef), NULL, &context));
// Draw the ArcView
ArcViewDraw(arcView, context);
break;
}
default:
break;
}
break;
}
default:
break;
}
CantCreateArcView:
ParameterMissing:
return result;
}
// Create an attributed string with the current font and string.
static CFAttributedStringRef ArcViewCreateAttributedString(ArcView *arcView)
{
assert(arcView->font != NULL);
int zero = 0;
CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &zero);
const CFStringRef keys[] = {
kCTFontAttributeName,
kCTLigatureAttributeName
};
const CFTypeRef values[] = {
arcView->font,
number
};
// Create our attributes
CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys, (const void**)&values, sizeof(keys)/sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
assert(attributes != NULL);
CFRelease(number);
// Create the attributed string
CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, arcView->string, attributes);
CFRelease(attributes);
return attrString;
}
// Precompute glyph positioning information
static void PrepareGlyphArcInfo(CTLineRef line, CFIndex glyphCount, GlyphArcInfo *glyphArcInfo)
{
CFArrayRef runArray = CTLineGetGlyphRuns(line);
CFIndex runCount = CFArrayGetCount(runArray);
// Examine each run in the line, updating glyphOffset to track how far along the run is in terms of glyphCount.
CFIndex glyphOffset = 0;
CFIndex runIndex = 0;
for (; runIndex < runCount; runIndex++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CFIndex runGlyphCount = CTRunGetGlyphCount(run);
// Ask for the width of each glyph in turn.
CFIndex runGlyphIndex = 0;
for (; runGlyphIndex < runGlyphCount; runGlyphIndex++) {
glyphArcInfo[runGlyphIndex + glyphOffset].width = CTRunGetTypographicBounds(run, CFRangeMake(runGlyphIndex, 1), NULL, NULL, NULL);
}
glyphOffset += runGlyphCount;
}
double lineLength = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
CGFloat prevHalfWidth = glyphArcInfo[0].width / 2.0;
glyphArcInfo[0].angle = (prevHalfWidth / lineLength) * M_PI;
// Divide the arc into slices such that each one covers the distance from one glyph's center to the next.
CFIndex lineGlyphIndex = 1;
for (; lineGlyphIndex < glyphCount; lineGlyphIndex++) {
CGFloat halfWidth = glyphArcInfo[lineGlyphIndex].width / 2.0;
CGFloat prevCenterToCenter = prevHalfWidth + halfWidth;
glyphArcInfo[lineGlyphIndex].angle = (prevCenterToCenter / lineLength) * M_PI;
prevHalfWidth = halfWidth;
}
}
// Create a default font
static CTFontRef ArcViewCreateDefaultFont(void)
{
return CTFontCreateWithName(ARCVIEW_DEFAULT_FONT_NAME, ARCVIEW_DEFAULT_FONT_SIZE, NULL);
}
// Create the ArcView object
static ArcView *ArcViewCreate(HIObjectRef object)
{
ArcView *arcView = NULL;
require(object != NULL, ParameterMissing);
arcView = (ArcView *)malloc(sizeof(ArcView));
require(arcView != NULL, MallocFailed);
arcView->object = object;
arcView->font = ArcViewCreateDefaultFont();
arcView->string = NULL;
arcView->radius = ARCVIEW_DEFAULT_RADIUS;
arcView->options = 0;
MallocFailed:
ParameterMissing:
return arcView;
}
// Dispose of the ArcView object
static void ArcViewDispose(ArcView *arcView)
{
if (arcView->font != NULL)
CFRelease(arcView->font);
if (arcView->string != NULL)
CFRelease(arcView->string);
free(arcView);
}