[Openmcl-devel] Need Some ObjC Bridge Help
Brent Fulgham
bfulg at pacbell.net
Tue Mar 20 22:09:16 PDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I'm trying to determine the width of a string based on a specific
font (eventually it will default to the current system font, but
first I want to get this case working).
Based on the Apple documentation, something like this should work:
NSFont* font = [NSFont fontWithName: @"Helvetica" size: 16];
NSString* text = [[NSString alloc] initWithString: @"Hello"];
NSDictionary* fontAttrs = [[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: font, nil]
forKeys: [NSArray arrayWithObjects:
NSFontAttributeName, nil]] copy]
NSSize extent = [text sizeWithAttributes: fontAttrs];
I'm attempting to perform the same with OpenMCL:
;;
;; Some AD3D stuff. A Font-Spec is a list: (list "font-name" font-
size :font-style)
;; e.g., ("helvetica" 16 :plain)
;;
(defun STRING-WIDTH (String &optional Font-Spec)
"Returns the width in pixels of String, as if it were displayed in
the font,
size, and style of font-spec."
(if (null font-spec)
(format t "No spec!")
;; NSDictionary* attributes = [NSDictionary
dictionaryWithObjectsAndKeys: font, nil];
;; NSString foo = String;
;; Size = foo :sizeWithAttributes attributes
(let ((size (float (second font-spec))))
(with-cstrs ((c-font-name (first font-spec))
(c-string String))
(with-autorelease-pool
(let* ((tempname (ccl::send (@class ns-
string) :string-with-c-string c-font-name))
(fontname (ccl::send tempname 'capitalized-
string))
(nsstring (ccl::send (@class ns-
string) :string-with-c-string c-string))
(font (ccl::send (@class ns-
font) :font-with-name fontname :size size)))
(ccl::%stack-block ((paramptrs (ash 1 target::word-
shift)))
(setf (ccl::%get-ptr paramptrs 0) font)
(let ((val-array (ccl::send (ccl::send (@class
"NSArray") 'alloc) :init-with-objects paramptrs :count 1)))
(rlet ((glyph-size :<NSS>ize))
(let ((dict (ccl::send (@class ns-
dictionary) :dictionary-with-objects-and-keys val-array)))
(ccl::send/stret glyph-size
nsstring :size-with-attributes dict)
(let ((width (ccl::pref glyph-
size :<NSS>ize.width)))
width)))))))))))
This crashes horribly:
? (string-width "Hello" (list "Helvetica" 16 :plain))
Unhandled exception 11 at 0xfffeff18, context->regs at #xf0135478
Read operation to unmapped address 0xbad00020
In foreign code at address 0xfffeff18
? for help
[26533] OpenMCL kernel debugger: b
current thread: tcr = 0x301ab0, native thread ID = 0x3603, interrupts
enabled
(#xf0135960) #x90A411D4 : _objc_msgSend + 244
(#xf0135a00) #x92BCCCA4 : +[NSDictionary
dictionaryWithObjectsAndKeys:] + 480
(#xf0135ac0) #x00006654 : _SPpoweropen_ffcall + 204
(#xF0135B00) #x08CD4C2C : #<Function STRING-WIDTH #x08cd4f16> + 3632
(#xf0135b10) #x00000000 : (null) + 0
(#xF0135B60) #x08CD4C54 : #<Function STRING-WIDTH #x08cd4f16> + 3672
(#xF0135B70) #x08CD4A54 : #<Function STRING-WIDTH #x08cd4f16> + 3160
(#xF0135B80) #x08CD3F20 : #<Function STRING-WIDTH #x08cd4f16> + 292
(#xF0135B90) #x000060F8 : (subprimitive _ret1valn)
(#xF0135BA0) #x0426179C : #<Function CALL-CHECK-REGS #x081c97ae> + 72
(#xF0135BB0) #x000060F8 : (subprimitive _ret1valn)
I'm clearly passing something non-kosher to the cocoa API, but I'm
not sure how to get it to do what I want.
Does anyone have a better alternative, or can see anything obvious
I'm doing wrong?
Thanks,
- -Brent
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)
iD8DBQFGAL3+zGDdrzfvUpURAph+AKCDzEPDY4Z9oVKZgqgO+w/3YQgyTACfUtLc
WkoaR1oNAVLFxMa6+SKneWM=
=tdqp
-----END PGP SIGNATURE-----
More information about the Openmcl-devel
mailing list