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 <CGFloat> 0 <CGFloat> 0 <CGFloat>
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
- typographic-bounds boa ;
+ line-metrics boa ;
: bounds>dim ( bounds -- dim )
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
[ ceiling >fixnum ]
bi@ 2array ;
-:: <line> ( string font -- line )
+:: <line> ( font string -- line )
[
[let* | open-font [ font cache-font CFRetain |CFRelease ]
line [ string open-font font foreground>> <CTLine> |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 <CGRect> CGContextFillRect ]
- [ 0 bounds descent>> CGContextSetTextPosition ]
+ [ 0 metrics descent>> CGContextSetTextPosition ]
[ line swap CTLineDraw ]
} cleave
] with-bitmap-context
SYMBOL: cached-lines
-: cached-line ( string font -- line )
+: cached-line ( font string -- line )
cached-lines get [ <line> ] 2cache ;
CONSTANT: max-line-age 10
: serif-font ( -- font )
<font>
"serif" >>name
- 12 >>size ; foldable
+ 12 >>size ;
: sans-serif-font ( -- font )
<font>
"sans-serif" >>name
- 12 >>size ; foldable
+ 12 >>size ;
: monospace-font ( -- 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
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 ;
[ 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 <rendered-line> ] 2cache 0 >>age ;
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 <CGPoint> 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 ;
{ $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" }
: 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 ;