Add ui.text:line-metrics word, and core-text implementation
parent
790dee3d7e
commit
ecf3d4f447
|
@ -41,31 +41,29 @@ FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
|
||||||
CTLineCreateWithAttributedString
|
CTLineCreateWithAttributedString
|
||||||
] with-destructors ;
|
] with-destructors ;
|
||||||
|
|
||||||
TUPLE: line font line bounds dim bitmap age refs disposed ;
|
TUPLE: line font line metrics dim bitmap age refs disposed ;
|
||||||
|
|
||||||
TUPLE: typographic-bounds width ascent descent leading ;
|
: compute-line-metrics ( line -- line-metrics )
|
||||||
|
|
||||||
: line-typographic-bounds ( line -- typographic-bounds )
|
|
||||||
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
|
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
|
||||||
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
||||||
typographic-bounds boa ;
|
line-metrics boa ;
|
||||||
|
|
||||||
: bounds>dim ( bounds -- dim )
|
: bounds>dim ( bounds -- dim )
|
||||||
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
||||||
[ ceiling >fixnum ]
|
[ ceiling >fixnum ]
|
||||||
bi@ 2array ;
|
bi@ 2array ;
|
||||||
|
|
||||||
:: <line> ( string font -- line )
|
:: <line> ( font string -- line )
|
||||||
[
|
[
|
||||||
[let* | open-font [ font cache-font CFRetain |CFRelease ]
|
[let* | open-font [ font cache-font CFRetain |CFRelease ]
|
||||||
line [ string open-font font foreground>> <CTLine> |CFRelease ]
|
line [ string open-font font foreground>> <CTLine> |CFRelease ]
|
||||||
bounds [ line line-typographic-bounds ]
|
metrics [ line compute-line-metrics ]
|
||||||
dim [ bounds bounds>dim ] |
|
dim [ bounds bounds>dim ] |
|
||||||
dim [
|
dim [
|
||||||
{
|
{
|
||||||
[ font background>> >rgba-components CGContextSetRGBFillColor ]
|
[ font background>> >rgba-components CGContextSetRGBFillColor ]
|
||||||
[ 0 0 dim first2 <CGRect> CGContextFillRect ]
|
[ 0 0 dim first2 <CGRect> CGContextFillRect ]
|
||||||
[ 0 bounds descent>> CGContextSetTextPosition ]
|
[ 0 metrics descent>> CGContextSetTextPosition ]
|
||||||
[ line swap CTLineDraw ]
|
[ line swap CTLineDraw ]
|
||||||
} cleave
|
} cleave
|
||||||
] with-bitmap-context
|
] with-bitmap-context
|
||||||
|
@ -84,7 +82,7 @@ M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
|
||||||
|
|
||||||
SYMBOL: cached-lines
|
SYMBOL: cached-lines
|
||||||
|
|
||||||
: cached-line ( string font -- line )
|
: cached-line ( font string -- line )
|
||||||
cached-lines get [ <line> ] 2cache ;
|
cached-lines get [ <line> ] 2cache ;
|
||||||
|
|
||||||
CONSTANT: max-line-age 10
|
CONSTANT: max-line-age 10
|
||||||
|
|
|
@ -34,14 +34,16 @@ TUPLE: font name size bold? italic? foreground background ;
|
||||||
: serif-font ( -- font )
|
: serif-font ( -- font )
|
||||||
<font>
|
<font>
|
||||||
"serif" >>name
|
"serif" >>name
|
||||||
12 >>size ; foldable
|
12 >>size ;
|
||||||
|
|
||||||
: sans-serif-font ( -- font )
|
: sans-serif-font ( -- font )
|
||||||
<font>
|
<font>
|
||||||
"sans-serif" >>name
|
"sans-serif" >>name
|
||||||
12 >>size ; foldable
|
12 >>size ;
|
||||||
|
|
||||||
: monospace-font ( -- font )
|
: monospace-font ( -- font )
|
||||||
<font>
|
<font>
|
||||||
"monospace" >>name
|
"monospace" >>name
|
||||||
12 >>size ; foldable
|
12 >>size ;
|
||||||
|
|
||||||
|
TUPLE: line-metrics width ascent descent leading ;
|
|
@ -10,7 +10,7 @@ IN: ui.text.core-text
|
||||||
SINGLETON: core-text-renderer
|
SINGLETON: core-text-renderer
|
||||||
|
|
||||||
M: core-text-renderer string-dim
|
M: core-text-renderer string-dim
|
||||||
[ " " string-dim { 0 1 } v* ] [ swap cached-line dim>> ] if-empty ;
|
[ " " string-dim { 0 1 } v* ] [ cached-line dim>> ] if-empty ;
|
||||||
|
|
||||||
TUPLE: rendered-line line texture display-list age disposed ;
|
TUPLE: rendered-line line texture display-list age disposed ;
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ M: rendered-line dispose*
|
||||||
[ texture>> delete-texture ]
|
[ texture>> delete-texture ]
|
||||||
[ display-list>> delete-dlist ] tri ;
|
[ display-list>> delete-dlist ] tri ;
|
||||||
|
|
||||||
: rendered-line ( string font -- rendered-line )
|
: rendered-line ( font string -- rendered-line )
|
||||||
world get fonts>>
|
world get fonts>>
|
||||||
[ cached-line <rendered-line> ] 2cache 0 >>age ;
|
[ cached-line <rendered-line> ] 2cache 0 >>age ;
|
||||||
|
|
||||||
|
@ -58,20 +58,22 @@ M: core-text-renderer finish-text-rendering
|
||||||
|
|
||||||
M: core-text-renderer draw-string ( font string loc -- )
|
M: core-text-renderer draw-string ( font string loc -- )
|
||||||
[
|
[
|
||||||
swap rendered-line
|
rendered-line display-list>> glCallList
|
||||||
display-list>> glCallList
|
|
||||||
] with-translation ;
|
] with-translation ;
|
||||||
|
|
||||||
M: core-text-renderer x>offset ( x font string -- n )
|
M: core-text-renderer x>offset ( x font string -- n )
|
||||||
[ 2drop 0 ] [
|
[ 2drop 0 ] [
|
||||||
swap cached-line line>>
|
cached-line line>>
|
||||||
swap 0 <CGPoint> CTLineGetStringIndexForPosition
|
swap 0 <CGPoint> CTLineGetStringIndexForPosition
|
||||||
] if-empty ;
|
] if-empty ;
|
||||||
|
|
||||||
M: core-text-renderer offset>x ( n font string -- x )
|
M: core-text-renderer offset>x ( n font string -- x )
|
||||||
swap cached-line line>> swap f
|
cached-line line>> swap f
|
||||||
CTLineGetOffsetForStringIndex ;
|
CTLineGetOffsetForStringIndex ;
|
||||||
|
|
||||||
|
M: core-text-renderer line-metrics ( font string -- metrics )
|
||||||
|
cached-line metrics>> ;
|
||||||
|
|
||||||
M: core-text-renderer free-fonts ( fonts -- )
|
M: core-text-renderer free-fonts ( fonts -- )
|
||||||
values dispose-each ;
|
values dispose-each ;
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,10 @@ HELP: offset>x
|
||||||
{ $values { "n" integer } { "font" font } { "string" string } { "x" real } }
|
{ $values { "n" integer } { "font" font } { "string" string } { "x" real } }
|
||||||
{ $contract "Outputs the x co-ordinate of the character at the given index." } ;
|
{ $contract "Outputs the x co-ordinate of the character at the given index." } ;
|
||||||
|
|
||||||
|
HELP: line-metrics
|
||||||
|
{ $values { "font" font } { "string" string } { "metrics" line-metrics } }
|
||||||
|
{ $contract "Outputs a " { $link line-metrics } " object with text measurements." } ;
|
||||||
|
|
||||||
ARTICLE: "text-rendering" "Rendering text"
|
ARTICLE: "text-rendering" "Rendering text"
|
||||||
"The " { $vocab-link "ui.text" } " vocabulary provides a cross-platform interface to the operating system's native font rendering engine. Currently, it uses Core Text on Mac OS X and FreeType on Windows and X11."
|
"The " { $vocab-link "ui.text" } " vocabulary provides a cross-platform interface to the operating system's native font rendering engine. Currently, it uses Core Text on Mac OS X and FreeType on Windows and X11."
|
||||||
{ $subsection "fonts" }
|
{ $subsection "fonts" }
|
||||||
|
|
|
@ -50,6 +50,8 @@ M: sequence text-dim
|
||||||
|
|
||||||
: text-height ( font text -- h ) text-dim second ;
|
: text-height ( font text -- h ) text-dim second ;
|
||||||
|
|
||||||
|
HOOK: text-metrics font-renderer ( font string -- metrics )
|
||||||
|
|
||||||
GENERIC# draw-text 1 ( font text loc -- )
|
GENERIC# draw-text 1 ( font text loc -- )
|
||||||
|
|
||||||
M: string draw-text draw-string ;
|
M: string draw-text draw-string ;
|
||||||
|
|
Loading…
Reference in New Issue