From f93b2f1c2950d2d818997608263ada16ae7b43fc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 1 Feb 2009 23:58:05 -0600 Subject: [PATCH] Clean up line-metrics code --- basis/core-text/core-text.factor | 8 ++++---- basis/fonts/fonts-docs.factor | 4 +++- basis/fonts/fonts.factor | 5 ++++- basis/ui/text/core-text/core-text.factor | 4 +++- basis/ui/text/freetype/freetype.factor | 4 ++-- basis/ui/text/text-docs.factor | 1 + basis/ui/text/text.factor | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/basis/core-text/core-text.factor b/basis/core-text/core-text.factor index 753b31d508..20f2a9e803 100644 --- a/basis/core-text/core-text.factor +++ b/basis/core-text/core-text.factor @@ -46,7 +46,7 @@ TUPLE: line font line metrics dim bitmap age refs disposed ; : compute-line-metrics ( line -- line-metrics ) 0 0 0 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ - line-metrics boa ; + metrics boa ; : bounds>dim ( bounds -- dim ) [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi @@ -58,16 +58,16 @@ TUPLE: line font line metrics dim bitmap age refs disposed ; [let* | open-font [ font cache-font CFRetain |CFRelease ] line [ string open-font font foreground>> |CFRelease ] metrics [ line compute-line-metrics ] - dim [ bounds bounds>dim ] | + dim [ metrics bounds>dim ] | dim [ { [ font background>> >rgba-components CGContextSetRGBFillColor ] [ 0 0 dim first2 CGContextFillRect ] - [ 0 metrics descent>> CGContextSetTextPosition ] + [ 0 metrics descent>> ceiling CGContextSetTextPosition ] [ line swap CTLineDraw ] } cleave ] with-bitmap-context - [ open-font line bounds dim ] dip 0 0 f + [ open-font line metrics dim ] dip 0 0 f ] line boa ] with-destructors ; diff --git a/basis/fonts/fonts-docs.factor b/basis/fonts/fonts-docs.factor index bfd67d8fbf..c529efc100 100644 --- a/basis/fonts/fonts-docs.factor +++ b/basis/fonts/fonts-docs.factor @@ -34,6 +34,8 @@ ARTICLE: "fonts" "Fonts" "Useful constants:" { $subsection monospace-font } { $subsection sans-serif-font } -{ $subsection serif-font } ; +{ $subsection serif-font } +"A data type for font metrics. The " { $vocab-link "fonts" } " vocabulary does not provide any means of computing font metrics, it simply defines a common data type that other vocabularies, such as " { $vocab-link "ui.text" } " may use:" +{ $subsection metrics } ; ABOUT: "fonts" diff --git a/basis/fonts/fonts.factor b/basis/fonts/fonts.factor index 7df292c0c5..fa139a35f2 100644 --- a/basis/fonts/fonts.factor +++ b/basis/fonts/fonts.factor @@ -16,6 +16,9 @@ TUPLE: font name size bold? italic? foreground background ; : font-with-background ( font color -- font' ) [ clone ] dip >>background ; inline +: font-with-size ( font size -- font' ) + [ clone ] dip >>size ; inline + : reverse-video-font ( font -- font ) clone dup [ foreground>> ] [ background>> ] bi @@ -46,4 +49,4 @@ TUPLE: font name size bold? italic? foreground background ; "monospace" >>name 12 >>size ; -TUPLE: line-metrics width ascent descent leading ; \ No newline at end of file +TUPLE: 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 5db2a3e80c..54fa2d40a8 100644 --- a/basis/ui/text/core-text/core-text.factor +++ b/basis/ui/text/core-text/core-text.factor @@ -72,7 +72,9 @@ M: core-text-renderer offset>x ( n font string -- x ) CTLineGetOffsetForStringIndex ; M: core-text-renderer line-metrics ( font string -- metrics ) - cached-line metrics>> ; + [ " " line-metrics clone 0 >>width ] + [ cached-line metrics>> ] + if-empty ; M: core-text-renderer free-fonts ( fonts -- ) values dispose-each ; diff --git a/basis/ui/text/freetype/freetype.factor b/basis/ui/text/freetype/freetype.factor index c84dbcce17..71c0d6ed9a 100644 --- a/basis/ui/text/freetype/freetype.factor +++ b/basis/ui/text/freetype/freetype.factor @@ -86,10 +86,10 @@ SYMBOL: dpi face-size face-size-y-scale FT_MulFix ; : init-ascent ( font face -- font ) - dup face-y-max swap font-units>pixels >>ascent ; inline + [ face-y-max ] keep font-units>pixels >>ascent ; inline : init-descent ( font face -- font ) - dup face-y-min swap font-units>pixels >>descent ; inline + [ face-y-min ] keep font-units>pixels >>descent ; inline : init-font ( font -- font ) dup handle>> init-ascent diff --git a/basis/ui/text/text-docs.factor b/basis/ui/text/text-docs.factor index a4fee0764e..468e43191d 100644 --- a/basis/ui/text/text-docs.factor +++ b/basis/ui/text/text-docs.factor @@ -55,6 +55,7 @@ ARTICLE: "text-rendering" "Rendering text" { $subsection text-dim } { $subsection text-width } { $subsection text-height } +{ $subsection line-metrics } "Converting screen locations to string offsets, and vice versa:" { $subsection x>offset } { $subsection offset>x } diff --git a/basis/ui/text/text.factor b/basis/ui/text/text.factor index 3b6f91bcd7..4acf892ae0 100644 --- a/basis/ui/text/text.factor +++ b/basis/ui/text/text.factor @@ -50,7 +50,7 @@ M: sequence text-dim : text-height ( font text -- h ) text-dim second ; -HOOK: text-metrics font-renderer ( font string -- metrics ) +HOOK: line-metrics font-renderer ( font string -- metrics ) GENERIC# draw-text 1 ( font text loc -- )