diff --git a/basis/core-text/core-text.factor b/basis/core-text/core-text.factor index 9f1e77758c..753b31d508 100644 --- a/basis/core-text/core-text.factor +++ b/basis/core-text/core-text.factor @@ -41,31 +41,29 @@ FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ; CTLineCreateWithAttributedString ] 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 ; - -: line-typographic-bounds ( line -- typographic-bounds ) +: compute-line-metrics ( line -- line-metrics ) 0 0 0 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ - typographic-bounds boa ; + line-metrics boa ; : bounds>dim ( bounds -- dim ) [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi [ ceiling >fixnum ] bi@ 2array ; -:: ( string font -- line ) +:: ( font string -- line ) [ [let* | open-font [ font cache-font CFRetain |CFRelease ] line [ string open-font font foreground>> |CFRelease ] - bounds [ line line-typographic-bounds ] + metrics [ line compute-line-metrics ] dim [ bounds bounds>dim ] | dim [ { [ font background>> >rgba-components CGContextSetRGBFillColor ] [ 0 0 dim first2 CGContextFillRect ] - [ 0 bounds descent>> CGContextSetTextPosition ] + [ 0 metrics descent>> CGContextSetTextPosition ] [ line swap CTLineDraw ] } cleave ] with-bitmap-context @@ -84,7 +82,7 @@ M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ; SYMBOL: cached-lines -: cached-line ( string font -- line ) +: cached-line ( font string -- line ) cached-lines get [ ] 2cache ; CONSTANT: max-line-age 10 diff --git a/basis/fonts/fonts.factor b/basis/fonts/fonts.factor index 4cec03b949..7df292c0c5 100644 --- a/basis/fonts/fonts.factor +++ b/basis/fonts/fonts.factor @@ -34,14 +34,16 @@ TUPLE: font name size bold? italic? foreground background ; : serif-font ( -- font ) "serif" >>name - 12 >>size ; foldable + 12 >>size ; : sans-serif-font ( -- font ) "sans-serif" >>name - 12 >>size ; foldable + 12 >>size ; : monospace-font ( -- font ) "monospace" >>name - 12 >>size ; foldable \ No newline at end of file + 12 >>size ; + +TUPLE: line-metrics width ascent descent leading ; \ No newline at end of file diff --git a/basis/ui/text/core-text/core-text.factor b/basis/ui/text/core-text/core-text.factor index 35a9f9aed9..5db2a3e80c 100644 --- a/basis/ui/text/core-text/core-text.factor +++ b/basis/ui/text/core-text/core-text.factor @@ -10,7 +10,7 @@ IN: ui.text.core-text SINGLETON: core-text-renderer 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 ; @@ -46,7 +46,7 @@ M: rendered-line dispose* [ texture>> delete-texture ] [ display-list>> delete-dlist ] tri ; -: rendered-line ( string font -- rendered-line ) +: rendered-line ( font string -- rendered-line ) world get fonts>> [ cached-line ] 2cache 0 >>age ; @@ -58,20 +58,22 @@ M: core-text-renderer finish-text-rendering M: core-text-renderer draw-string ( font string loc -- ) [ - swap rendered-line - display-list>> glCallList + rendered-line display-list>> glCallList ] with-translation ; M: core-text-renderer x>offset ( x font string -- n ) [ 2drop 0 ] [ - swap cached-line line>> + cached-line line>> swap 0 CTLineGetStringIndexForPosition ] if-empty ; M: core-text-renderer offset>x ( n font string -- x ) - swap cached-line line>> swap f + cached-line line>> swap f CTLineGetOffsetForStringIndex ; +M: core-text-renderer line-metrics ( font string -- metrics ) + cached-line metrics>> ; + M: core-text-renderer free-fonts ( fonts -- ) values dispose-each ; diff --git a/basis/ui/text/text-docs.factor b/basis/ui/text/text-docs.factor index b89d1f7196..a4fee0764e 100644 --- a/basis/ui/text/text-docs.factor +++ b/basis/ui/text/text-docs.factor @@ -44,6 +44,10 @@ HELP: offset>x { $values { "n" integer } { "font" font } { "string" string } { "x" real } } { $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" "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" } diff --git a/basis/ui/text/text.factor b/basis/ui/text/text.factor index 062a20adab..3b6f91bcd7 100644 --- a/basis/ui/text/text.factor +++ b/basis/ui/text/text.factor @@ -50,6 +50,8 @@ M: sequence text-dim : text-height ( font text -- h ) text-dim second ; +HOOK: text-metrics font-renderer ( font string -- metrics ) + GENERIC# draw-text 1 ( font text loc -- ) M: string draw-text draw-string ;