forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting1.html
executable file
·997 lines (806 loc) · 37.7 KB
/
listing1.html
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>ATSUIDirectAccessDemo - /DirectAccessCallbacks.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Carbon/index.html">Carbon</a> > <a href="../../samplecode/Carbon/idxTextFonts-date.html">Text & Fonts</a> > <A HREF="javascript:location.replace('index.html');">ATSUIDirectAccessDemo</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">ATSUIDirectAccessDemo</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/DirectAccessCallbacks.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/DirectAccessCallbacks.c</option>
<option value="listing2.html">/DirectAccessCallbacks.h</option>
<option value="listing3.html">/HIElements.h</option>
<option value="listing4.html">/main.c</option>
<option value="listing5.html">/MenuHandler.c</option>
<option value="listing6.html">/MenuHandler.h</option>
<option value="listing7.html">/Window.c</option>
<option value="listing8.html">/Window.h</option></select>
</p>
</form>
<p><strong><a href="ATSUIDirectAccessDemo.zip">Download Sample</a></strong> (“ATSUIDirectAccessDemo.zip”, 80.7K)<BR>
<strong><a href="ATSUIDirectAccessDemo.dmg">Download Sample</a></strong> (“ATSUIDirectAccessDemo.dmg”, 148.7K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: DirectAccessCallbacks.c
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, 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 Computer, 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 © 2002 Apple Computer, Inc., All Rights Reserved
*/
// This code will run on Mac OS X 10.2 (or later) ONLY!!!
#include <Carbon/Carbon.h>
#include "HIElements.h"
#include "MenuHandler.h"
#include "DirectAccessCallbacks.h"
extern float sinf( float x ); // definition for libstdc sinf
// ------------------------------------------------------------------------------
// Data Structures
// ------------------------------------------------------------------------------
typedef struct ReplacementGlyphInfoStruct {
ATSGlyphRef glyphID;
Fixed glyphHalfAdvance;
ATSUStyleSettingRef styleRef;
ATSUStyleSettingRef *styleSettingArray;
ATSUTextLayout replacementLayout;
ATSUStyle replacementStyle;
} ReplacementGlyphInfoStruct;
// ------------------------------------------------------------------------------
// Global Variables
// ------------------------------------------------------------------------------
static UniChar gReplacementChar = 0x2022;
static RGBColor gReplacementCharRGBColor = {0,0,0xFFFF};
static UniCharCount gReplacementCharLength = 1;
static ReplacementGlyphInfoStruct gReplacementGlyph;
static Boolean gReplacementGlyphInitialized = false;
// ------------------------------------------------------------------------------
// Function Prototypes
// ------------------------------------------------------------------------------
#define FixedToFloat(a) ((float)(a) / fixed1)
#define FloatToFixed(a) ((Fixed)((float)(a) * fixed1))
static OSStatus GlyphReplacementCallback(
ATSULayoutOperationSelector iCurrentOperation, ATSULineRef iLineRef,
UInt32 iRefCon, void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus );
static OSStatus StretchyGlyphCallback(
ATSULayoutOperationSelector iCurrentOperation, ATSULineRef iLineRef,
UInt32 iRefCon, void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus );
static OSStatus ShrinkyGlyphCallback(
ATSULayoutOperationSelector iCurrentOperation, ATSULineRef iLineRef,
UInt32 iRefCon, void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus );
static OSStatus GlyphWaveCallback(
ATSULayoutOperationSelector iCurrentOperation, ATSULineRef iLineRef,
UInt32 iRefCon, void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus );
static OSStatus GetReplacementGlyphStruct(
ReplacementGlyphInfoStruct **replacementStruct );
// ------------------------------------------------------------------------------
// InstallGlyphReplacementCallback [EXPORT]
// ------------------------------------------------------------------------------
extern
OSStatus InstallGlyphReplacementCallback(
ATSUTextLayout textLayout )
{
OSStatus err;
ATSULayoutOperationOverrideSpecifier overrideSpec;
ATSUAttributeTag attrTag;
ByteCount attrSize;
ATSUAttributeValuePtr attrPtr;
// setup the post layout adjustment
overrideSpec.operationSelector = kATSULayoutOperationPostLayoutAdjustment;
overrideSpec.overrideUPP = GlyphReplacementCallback;
// set the override spec in the layout object
attrTag = kATSULayoutOperationOverrideTag;
attrSize = sizeof( ATSULayoutOperationOverrideSpecifier );
attrPtr = &overrideSpec;
// set the override spec in the layout
err = ATSUSetLayoutControls( textLayout, 1, &attrTag, &attrSize, &attrPtr );
require_noerr( err, InstallGlyphReplacementCallback_err );
InstallGlyphReplacementCallback_err:
return err;
}
// ------------------------------------------------------------------------------
// InstallStrechyGlyphCallback [EXPORT]
// ------------------------------------------------------------------------------
extern
OSStatus InstallStrechyGlyphCallback(
ATSUTextLayout textLayout )
{
OSStatus err;
ATSULayoutOperationOverrideSpecifier overrideSpec;
ATSUAttributeTag attrTag;
ByteCount attrSize;
ATSUAttributeValuePtr attrPtr;
// setup the post layout adjustment
overrideSpec.operationSelector = kATSULayoutOperationJustification;
overrideSpec.overrideUPP = StretchyGlyphCallback;
// set the override spec in the layout object
attrTag = kATSULayoutOperationOverrideTag;
attrSize = sizeof( ATSULayoutOperationOverrideSpecifier );
attrPtr = &overrideSpec;
// set the override spec in the layout
err = ATSUSetLayoutControls( textLayout, 1, &attrTag, &attrSize, &attrPtr );
require_noerr( err, InstallStrechyGlyphCallback_err );
InstallStrechyGlyphCallback_err:
return err;
}
// ------------------------------------------------------------------------------
// InstallShrinkyGlyphCallback [EXPORT]
// ------------------------------------------------------------------------------
extern
OSStatus InstallShrinkyGlyphCallback(
ATSUTextLayout textLayout )
{
OSStatus err;
ATSULayoutOperationOverrideSpecifier overrideSpec;
ATSUAttributeTag attrTag;
ByteCount attrSize;
ATSUAttributeValuePtr attrPtr;
// setup the post layout adjustment
overrideSpec.operationSelector = kATSULayoutOperationJustification;
overrideSpec.overrideUPP = ShrinkyGlyphCallback;
// set the override spec in the layout object
attrTag = kATSULayoutOperationOverrideTag;
attrSize = sizeof( ATSULayoutOperationOverrideSpecifier );
attrPtr = &overrideSpec;
// set the override spec in the layout
err = ATSUSetLayoutControls( textLayout, 1, &attrTag, &attrSize, &attrPtr );
require_noerr( err, InstallShrinkyGlyphCallback_err );
InstallShrinkyGlyphCallback_err:
return err;
}
// ------------------------------------------------------------------------------
// UpdateCallbackContexts [EXPORT]
// ------------------------------------------------------------------------------
extern
OSStatus UpdateCallbackContexts( void )
{
OSStatus err = noErr;
// if the override struct is invalid, make sure we invalidate it, this
// installation could have come as the result of a font or size change.
if ( gReplacementGlyphInitialized == true )
{
// dispose of the style object in it
err = ATSUDisposeStyle( gReplacementGlyph.replacementStyle );
require_noerr( err, UpdateCallbackContexts_err );
// dispose of the layout object in it
err = ATSUDisposeTextLayout( gReplacementGlyph.replacementLayout );
require_noerr( err, UpdateCallbackContexts_err );
// dipose of the style ref array inside of it
err = ATSUDirectReleaseLayoutDataArrayPtr( NULL,
kATSUDirectDataStyleSettingATSUStyleSettingRefArray,
(void **) &gReplacementGlyph.styleSettingArray );
require_noerr( err, UpdateCallbackContexts_err );
// the glyph is no longer initialize, so flag it as such
gReplacementGlyphInitialized = false;
}
UpdateCallbackContexts_err:
return err;
}
// ------------------------------------------------------------------------------
// InstallGlyphWaveCallback [INTERNAL]
// ------------------------------------------------------------------------------
extern
OSStatus InstallGlyphWaveCallback(
ATSUTextLayout textLayout )
{
OSStatus err;
ATSULayoutOperationOverrideSpecifier overrideSpec;
ATSUAttributeTag attrTag;
ByteCount attrSize;
ATSUAttributeValuePtr attrPtr;
// setup the post layout adjustment
overrideSpec.operationSelector = kATSULayoutOperationPostLayoutAdjustment;
overrideSpec.overrideUPP = GlyphWaveCallback;
// set the override spec in the layout object
attrTag = kATSULayoutOperationOverrideTag;
attrSize = sizeof( ATSULayoutOperationOverrideSpecifier );
attrPtr = &overrideSpec;
// set the override spec in the layout
err = ATSUSetLayoutControls( textLayout, 1, &attrTag, &attrSize, &attrPtr );
require_noerr( err, InstallGlyphWaveCallback_err );
InstallGlyphWaveCallback_err:
return err;
}
// ------------------------------------------------------------------------------
// GlyphWaveCallback [INTERNAL]
// ------------------------------------------------------------------------------
static
OSStatus GlyphWaveCallback(
ATSULayoutOperationSelector iCurrentOperation,
ATSULineRef iLineRef,
UInt32 iRefCon,
void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus )
{
OSStatus err;
Fixed *deltaYArray;
Fixed *deltaXArray;
Fixed slack;
ATSLayoutRecord *layoutRecordArray;
ItemCount recordArrayCount;
ItemCount i;
Fixed scaleFactor = 0;
float amplitude;
// grab the glyph array for the line. We're going to calculate the sine
// based on the real positions of the glyphs for a much smoother layout.
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent, false,
(void **) &layoutRecordArray, &recordArrayCount );
require_noerr( err, GlyphWaveCallback_err );
// grab the advance deltas for the line
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, true,
(void **) &deltaXArray, &recordArrayCount );
require_noerr( err, GlyphWaveCallbackDeltaXArray_err );
// grab the baseline deltas for the line
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataBaselineDeltaFixedArray, true,
(void **) &deltaYArray, &recordArrayCount );
require_noerr( err, GlyphWaveCallbackDeltaYArray_err );
// run through and find the largest positional difference in the entire
// line. This will help us set the scale factor.
for ( i = 1; i < recordArrayCount; i++ )
{
// get the slack (really the difference between the two realpositions,
// I'm just too lazy to add another variable to the stack with a
// different name.
slack = (layoutRecordArray[i].realPos - layoutRecordArray[i-1].realPos);
// check to see if this is the greatest difference. If it is, then
// set our scale factor
if ( slack > scaleFactor )
{
scaleFactor = slack;
}
}
// workaround for bug #2913628. If the scale factor is still zero after
// that, it means that proper realPos aren't being returned, so just fix
// it at the point size.
if ( scaleFactor == 0 )
{
scaleFactor = gFontSize << 16;
}
// set the amplitude to be the scale factor
amplitude = FixedToFloat( scaleFactor );
// we're going to anchor the first glyph, so start at the second glyph and
// start streatching. We don't care too much about the terminator glyph, so
// don't include that in the wave.
for ( i = 1; i < recordArrayCount - 1; i++ )
{
// calculate the glyph slack. We're trying to impose a fixed width on
// all of the glyphs of at least their point size
slack = scaleFactor - (layoutRecordArray[i].realPos -
layoutRecordArray[i-1].realPos);
// add the slack value into the previous glyph's deltaX and this
// glyph's realPosition. This is done to impose a fixed width on
// all of the glyphs.
layoutRecordArray[i].realPos += slack;
deltaXArray[i-1] += slack;
// calculate the sin deltaY for the current glyph
deltaYArray[i] = FloatToFixed( sinf( i ) * amplitude );
};
// dispose of the base line delta array
ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataBaselineDeltaFixedArray, (void **) &deltaYArray );
GlyphWaveCallbackDeltaYArray_err:
// dispose of the delta x array
ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, (void **) &deltaXArray );
GlyphWaveCallbackDeltaXArray_err:
// dispose of the layout record array
ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
(void **) &layoutRecordArray );
GlyphWaveCallback_err:
// return as handled so ATSUI won't do anything to the line
*oCallbackStatus = kATSULayoutOperationCallbackStatusHandled;
return err;
}
// ------------------------------------------------------------------------------
// StretchyGlyphCallback [INTERNAL]
// ------------------------------------------------------------------------------
static
OSStatus StretchyGlyphCallback(
ATSULayoutOperationSelector iCurrentOperation,
ATSULineRef iLineRef,
UInt32 iRefCon,
void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus )
{
OSStatus err;
Fixed *deltaXArray;
ItemCount recordArrayCount;
ItemCount i;
// grab the advance deltas for the line
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, true,
(void **) &deltaXArray, &recordArrayCount );
require_noerr( err, StretchyGlyphCallback_err );
// we're going to anchor the first glyph, so start at the second glyph and
// start streatching!
for ( i = 1; i < recordArrayCount; i++ ) {
deltaXArray[i] += (Fixed) ((gFontSize * i) << 16);
};
// dispose of the array
err = ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, (void **) &deltaXArray );
StretchyGlyphCallback_err:
// return as handled so ATSUI won't do anything to the line
*oCallbackStatus = kATSULayoutOperationCallbackStatusHandled;
return err;
}
// ------------------------------------------------------------------------------
// ShrinkyGlyphCallback [INTERNAL]
// ------------------------------------------------------------------------------
static
OSStatus ShrinkyGlyphCallback(
ATSULayoutOperationSelector iCurrentOperation,
ATSULineRef iLineRef,
UInt32 iRefCon,
void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus )
{
OSStatus err;
Fixed *deltaXArray;
ItemCount recordArrayCount;
ItemCount i;
Fixed shrinkFactor;
// set the shrink factor to a factor of the current font size
shrinkFactor = ( gFontSize / kFontSizeMinimum ) << 16;
// grab the advance deltas for the line
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, true,
(void **) &deltaXArray, &recordArrayCount );
require_noerr( err, ShrinkyGlyphCallback_err );
// we're going to anchor the first glyph, so start at the second glyph and
// start streatching!
for ( i = 1; i < recordArrayCount; i++ ) {
deltaXArray[i] -= shrinkFactor;
};
// dispose of the array
err = ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, (void **) &deltaXArray );
ShrinkyGlyphCallback_err:
// return as handled so ATSUI won't do anything to the line
*oCallbackStatus = kATSULayoutOperationCallbackStatusHandled;
return err;
}
// ------------------------------------------------------------------------------
// GetReplacementGlyphStruct [INTERNAL]
// ------------------------------------------------------------------------------
static
OSStatus GetReplacementGlyphStruct(
ReplacementGlyphInfoStruct **replacementStruct )
{
OSStatus err = noErr;
// assume that there ain't no replacement glyph
*replacementStruct = NULL;
// check to see if the replacement glyph has been initialized. If it hadn't,
// then create the text layout and copy the style in
if ( gReplacementGlyphInitialized == false )
{
UInt16 *styleIndexArray;
ATSLayoutRecord *layoutRecordArray;
UInt16 styleIndex;
ItemCount recordCount;
ATSUAttributeTag tag = kATSUColorTag;
ByteCount tagValueSize = sizeof(RGBColor);
ATSUAttributeValuePtr tagValuePtr = &gReplacementCharRGBColor;
// copy the style into the replacement struct. Once I fix some bugs in
// the DirectAccess code, we won't need to keep the style around anymore.
err = ATSUCreateAndCopyStyle( gGlobalStyle,
&gReplacementGlyph.replacementStyle );
require_noerr( err, GetReplacementGlyphStruct_err );
// set the attributes in the style object. This should change the glyph
// color to a pretty blue. I like blue glyphs.
err = ATSUSetAttributes( gReplacementGlyph.replacementStyle, 1, &tag,
&tagValueSize, &tagValuePtr );
check_noerr( err );
// create a new layout
err = ATSUCreateTextLayoutWithTextPtr( &gReplacementChar,
kATSUFromTextBeginning, kATSUToTextEnd, 1, 1,
&gReplacementCharLength, &gReplacementGlyph.replacementStyle,
&gReplacementGlyph.replacementLayout );
require_noerr( err, GetReplacementGlyphStruct_err );
// make sure that we turn on full font substitution in case the font
// doesn't have a glyph for the codepoint
err = ATSUSetTransientFontMatching( gReplacementGlyph.replacementLayout,
true );
require_noerr( err, GetReplacementGlyphStruct_err );
// use the direct access call to get a pointer to the glyph array from
// the replacement text layout.
err = ATSUDirectGetLayoutDataArrayPtrFromTextLayout(
gReplacementGlyph.replacementLayout, 0,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
(void **) &layoutRecordArray, &recordCount );
require_noerr( err, GetReplacementGlyphStruct_err );
// make sure that we got an array back
require_action( layoutRecordArray != NULL,
GetReplacementGlyphStruct_err, err = paramErr );
// since we only have one character in the replacement buffer, we can
// assume that the first glyph in the glyph array is the replacement
// character we are looking for
gReplacementGlyph.glyphID = layoutRecordArray[0].glyphID;
// set the glyph half advance to be the difference between the real positions
// divided in half. This isn't the real advance. To calculate that, you would
// need to take into account the advance deltas.
gReplacementGlyph.glyphHalfAdvance = ( layoutRecordArray[1].realPos -
layoutRecordArray[0].realPos ) / 2;
// now, dispose of the layout record array
err = ATSUDirectReleaseLayoutDataArrayPtr( NULL,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
(void **) &layoutRecordArray );
require_noerr( err, GetReplacementGlyphStruct_err );
// now, try to get a style index array. This will probably be NULL,
// but you never know. We just need the style index for the glyph at
// index 0
err = ATSUDirectGetLayoutDataArrayPtrFromTextLayout(
gReplacementGlyph.replacementLayout, 0,
kATSUDirectDataStyleIndexUInt16Array,
(void **) &styleIndexArray, &recordCount );
require_noerr( err, GetReplacementGlyphStruct_err );
// check to see if we got an array back
if ( styleIndexArray != NULL )
{
// we got an array back, so make sure that we set the the style
// index as the style index for glyph zero
styleIndex = styleIndexArray[0];
// dispose of the style index array
err = ATSUDirectReleaseLayoutDataArrayPtr( NULL,
kATSUDirectDataStyleIndexUInt16Array, (void **) &styleIndexArray );
require_noerr( err, GetReplacementGlyphStruct_err );
}
else
{
// otherwise, if there is no style index array, the style index is
// assumed to be zero, as there aren't
styleIndex = 0;
}
// finally, we to use the direct access call to get a pointer to the
// style settings ref array. We need to hang onto this array in the
// structure because if it's disposed of, then our retained style
// ref will be disposed of as well. It doesn't matter that much since
// this is a copy anyway.
err = ATSUDirectGetLayoutDataArrayPtrFromTextLayout(
gReplacementGlyph.replacementLayout, 0,
kATSUDirectDataStyleSettingATSUStyleSettingRefArray,
(void **) &gReplacementGlyph.styleSettingArray, &recordCount );
require_noerr( err, GetReplacementGlyphStruct_err );
// make sure that we got an array back, too!
require_action( gReplacementGlyph.styleSettingArray != NULL,
GetReplacementGlyphStruct_err, err = paramErr );
// now, the last piece of data can be filled in - the style setting
// for the replacement glyph struct
gReplacementGlyph.styleRef =
gReplacementGlyph.styleSettingArray[styleIndex];
// everything should be initialized, so set the flag saying so
gReplacementGlyphInitialized = true;
}
// all done, so return the global struct
*replacementStruct = &gReplacementGlyph;
GetReplacementGlyphStruct_err:
return err;
}
// ------------------------------------------------------------------------------
// GlyphReplacementCallback [INTERNAL]
// ------------------------------------------------------------------------------
static
OSStatus GlyphReplacementCallback(
ATSULayoutOperationSelector iCurrentOperation,
ATSULineRef iLineRef,
UInt32 iRefCon,
void *iOperationExtraParameter,
ATSULayoutOperationCallbackStatus *oCallbackStatus )
{
OSStatus err;
ReplacementGlyphInfoStruct *replacementGlyph;
// grab the replacment glyph
err = GetReplacementGlyphStruct( &replacementGlyph );
require_noerr( err, GlyphReplacementCallback_err );
// make sure that we're only in here for the post-layout callback.
if ( iCurrentOperation == kATSULayoutOperationPostLayoutAdjustment )
{
OSStatus disposalErr;
ATSLayoutRecord *recordArray;
Fixed *advanceDeltaArray = NULL;
ItemCount recordArrayCount;
Boolean styleAdded = false;
UInt16 styleIndex;
UInt16 *styleIndexArray = NULL;
ItemCount i;
// Get a direct pointer to the glyph array. This should be sweet!
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent, false,
(void **) &recordArray, &recordArrayCount );
require_noerr( err, GlyphReplacementCallback_err );
// make sure that we got some glyphs
require_action( recordArray != NULL, GlyphReplacementCallback_err,
err = paramErr );
// loop through all of the glyphs, looking for whitespace. We don't
// really care about the terminator glyph here
for ( i = 0; i < recordArrayCount - 1; i++ )
{
// make sure that this isn't a deleted glyph. We don't want to be
// replacing these with anything, no matter what their flags are
if ( recordArray[i].glyphID != kATSDeletedGlyphcode )
{
// check to see if this glyph is a whitespace character.
if ( recordArray[i].flags & kATSGlyphInfoIsWhiteSpace )
{
Fixed minDistance;
Fixed adjustAmount;
ItemCount j;
// this is a whitespace character. It's a candidate for
// replacement, so replace it. First, check to see if
// the style has been added yet.
if ( styleAdded == false )
{
ItemCount styleIndexCount;
// get the style index array. Create it if it's not there!
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataStyleIndexUInt16Array, true,
(void **) &styleIndexArray, &styleIndexCount );
require_noerr( err,
GlyphReplacementCallbackStyleSetting_err );
// make sure that we got an array
require_action( styleIndexArray != NULL,
GlyphReplacementCallbackStyleSetting_err,
err = paramErr );
// make sure that the array is parallel to the record array
require_action( styleIndexCount == recordArrayCount,
GlyphReplacementCallbackStyleSetting_err,
err = paramErr );
// add the style to the style settings array for this line
err = ATSUDirectAddStyleSettingRef( iLineRef,
replacementGlyph->styleRef, &styleIndex );
require_noerr( err,
GlyphReplacementCallbackStyleSetting_err );
// the style's been added. No need to do it again.
styleAdded = true;
}
// change the glyph's ID to the replacement glyph's ID
recordArray[i].glyphID = replacementGlyph->glyphID;
// change the style index to index the style setting that
// we added for the replacement glyph's style setting
styleIndexArray[i] = styleIndex;
// now, we need to position the glyph by messing with the
// real position. The real position is currently set to the
// real position of the whitespace character. This won't
// look very good for non-monospaced fonts.
// if we don't already have a pointer to the advance delta
// array, go ahead and get one now. Make sure that it's
// created.
err = ATSUDirectGetLayoutDataArrayPtrFromLineRef( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, true,
(void **) &advanceDeltaArray, &recordArrayCount );
require_noerr( err, GlyphReplacementCallbackStyleSetting_err );
// find the next non-deleted glyph. It may turn out that
// it's the final, terminator glyph
for ( j = i + 1; j < recordArrayCount - 1; j++ )
{
if ( recordArray[j].glyphID != kATSDeletedGlyphcode )
{
break;
}
}
// To avoid a collision with the next glyph, calculate the distance
// between the current real position and the next glyph.
minDistance = recordArray[j].realPos - recordArray[i].realPos;
if ( ( recordArray[i].realPos + replacementGlyph->glyphHalfAdvance ) >
( recordArray[i].realPos + minDistance ) )
{
// we need to back it up a bit so that the replacement glyph
// doesn't collide with the next glyph
adjustAmount = ( ( recordArray[i].realPos + replacementGlyph->glyphHalfAdvance )
- ( recordArray[i].realPos + minDistance ) );
}
else
{
adjustAmount = 0;
}
// back the new real position up a bit to properly center the
// replacement glyph over the new position.
adjustAmount -= replacementGlyph->glyphHalfAdvance;
// now, we're going to try to avoid a really bad looking collision with the
// previous glyph, if there is one. A slight collision is okay.
if ( i > 0 )
{
Fixed prevAdvancePos;
Fixed overHang;
// calculate the previous glyph's advance
prevAdvancePos = recordArray[i].realPos - advanceDeltaArray[i-1];
// calculate the overhang
overHang = prevAdvancePos - ( recordArray[i].realPos + adjustAmount );
// check to see if we have an overhang. We're not going to do anything
// if we don't
if ( overHang > 0 )
{
// allow no more than a quarter of the replacement glyph to collide
if ( overHang > ( replacementGlyph->glyphHalfAdvance / 4 ) )
{
adjustAmount += ( replacementGlyph->glyphHalfAdvance / 4 );
}
else
{
adjustAmount += overHang;
}
}
}
// if the adjust amount is greater than zero, then we need to make sure that
// the real position and the delta X for the glyphs are adjust
// adjust the real position in the glyph
recordArray[i].realPos += adjustAmount;
// adjust the advance delta to take up any of the slack that was caused by
// our adjustment amount.
advanceDeltaArray[i] -= adjustAmount;
}
}
}
GlyphReplacementCallbackStyleSetting_err:
// if we got the style index array, make sure that we dispose of it
if ( styleIndexArray != NULL )
{
disposalErr = ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataStyleIndexUInt16Array, (void **) &styleIndexArray );
check_noerr( disposalErr );
}
// if we've got an advance delta array, make sure that we dispose of it
if ( advanceDeltaArray != NULL )
{
disposalErr = ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataAdvanceDeltaFixedArray, (void **) &advanceDeltaArray );
check_noerr( disposalErr );
}
// dispose of the layout record array
disposalErr = ATSUDirectReleaseLayoutDataArrayPtr( iLineRef,
kATSUDirectDataLayoutRecordATSLayoutRecordCurrent,
(void **) &recordArray );
check_noerr( disposalErr );
} else {
// oops. We've been called for something we weren't setup to handle.
// Make sure that we tell ATSUI that's been bad.
err = paramErr;
}
GlyphReplacementCallback_err:
// if we didn't get an error, make sure that we return the status as
// handled since we did all of the replacement here. It actually doesn't
// matter for the post-layout callback.
if ( err == noErr ) {
*oCallbackStatus = kATSULayoutOperationCallbackStatusHandled;
} else {
*oCallbackStatus = kATSULayoutOperationCallbackStatusContinue;
}
return err;
}
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/ATSUIDirectAccessDemo/listing1.html%3Fid%3DDTS10001092-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/ATSUIDirectAccessDemo/listing1.html%3Fid%3DDTS10001092-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/ATSUIDirectAccessDemo/listing1.html%3Fid%3DDTS10001092-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>