Better error checking for labels and text rendering

db4
Slava Pestov 2009-02-09 00:22:41 -06:00
parent 112c94cda2
commit df62cb8edb
3 changed files with 28 additions and 6 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Slava Pestov. ! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays alien alien.c-types alien.syntax kernel USING: arrays alien alien.c-types alien.syntax kernel
destructors accessors fry words hashtables destructors accessors fry words hashtables strings
sequences memoize assocs math math.functions locals init sequences memoize assocs math math.functions locals init
namespaces combinators fonts colors core-foundation namespaces combinators fonts colors core-foundation
core-foundation.strings core-foundation.attributed-strings core-foundation.strings core-foundation.attributed-strings
@ -32,9 +32,14 @@ FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, C
FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ; FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
ERROR: not-a-string object ;
: <CTLine> ( string open-font color -- line ) : <CTLine> ( string open-font color -- line )
[ [
[ dup selection? [ string>> ] when ] 2dip [
dup selection? [ string>> ] when
dup string? [ not-a-string ] unless
] 2dip
[ [
kCTForegroundColorAttributeName set kCTForegroundColorAttributeName set
kCTFontAttributeName set kCTFontAttributeName set

View File

@ -3,7 +3,7 @@
USING: accessors arrays hashtables io kernel math math.functions USING: accessors arrays hashtables io kernel math math.functions
namespaces make opengl sequences strings splitting ui.gadgets namespaces make opengl sequences strings splitting ui.gadgets
ui.gadgets.tracks ui.gadgets.packs fonts ui.render ui.text ui.gadgets.tracks ui.gadgets.packs fonts ui.render ui.text
colors colors.constants models ; colors colors.constants models combinators ;
IN: ui.gadgets.labels IN: ui.gadgets.labels
! A label gadget draws a string. ! A label gadget draws a string.
@ -14,8 +14,25 @@ SLOT: string
M: label string>> ( label -- string ) M: label string>> ( label -- string )
text>> dup string? [ "\n" join ] unless ; inline text>> dup string? [ "\n" join ] unless ; inline
<PRIVATE
PREDICATE: string-array < array [ string? ] all? ;
PRIVATE>
: ?string-lines ( string -- string/array )
CHAR: \n over memq? [ string-lines ] when ;
ERROR: not-a-string object ;
M: label (>>string) ( string label -- ) M: label (>>string) ( string label -- )
[ CHAR: \n over memq? [ string-lines ] when ] dip (>>text) ; inline [
{
{ [ dup string-array? ] [ ] }
{ [ dup string? ] [ ?string-lines ] }
[ not-a-string ]
} cond
] dip (>>text) ; inline
: label-theme ( gadget -- gadget ) : label-theme ( gadget -- gadget )
sans-serif-font >>font ; inline sans-serif-font >>font ; inline

View File

@ -43,7 +43,7 @@ GENERIC: text-dim ( font text -- dim )
M: string text-dim string-dim ; M: string text-dim string-dim ;
M: sequence text-dim M: array text-dim
[ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ; [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;
: text-width ( font text -- w ) text-dim first ; : text-width ( font text -- w ) text-dim first ;
@ -58,7 +58,7 @@ M: string draw-text draw-string ;
M: selection draw-text draw-string ; M: selection draw-text draw-string ;
M: sequence draw-text M: array draw-text
[ [
[ [
2dup { 0 0 } draw-string 2dup { 0 0 } draw-string