pango: compute x-height and cap-height metrics

db4
Slava Pestov 2009-03-01 20:10:19 -06:00
parent 5edb226fb0
commit 1cc4a013da
4 changed files with 77 additions and 68 deletions

View File

@ -3,9 +3,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
! !
! pangocairo bindings, from pango/pangocairo.h ! pangocairo bindings, from pango/pangocairo.h
USING: cairo.ffi alien.c-types math alien.syntax system destructors USING: alien alien.syntax combinators system cairo.ffi ;
memoize accessors kernel combinators alien arrays fonts pango
pango.fonts namespaces ;
IN: pango.cairo IN: pango.cairo
<< "pangocairo" { << "pangocairo" {
@ -87,40 +85,4 @@ FUNCTION: void
pango_cairo_layout_path ( cairo_t* cr, PangoLayout* layout ) ; pango_cairo_layout_path ( cairo_t* cr, PangoLayout* layout ) ;
FUNCTION: void FUNCTION: void
pango_cairo_error_underline_path ( cairo_t* cr, double x, double y, double width, double height ) ; pango_cairo_error_underline_path ( cairo_t* cr, double x, double y, double width, double height ) ;
SYMBOL: dpi
72 dpi set-global
: dummy-pango-context ( -- context )
\ dummy-pango-context [
pango_context_new
] initialize-alien ;
MEMO: (cache-font) ( font -- open-font )
[
pango_cairo_font_map_get_default
dup dpi get pango_cairo_font_map_set_resolution
dummy-pango-context
] dip
cache-font-description
pango_font_map_load_font ;
: cache-font ( font -- open-font )
strip-font-colors (cache-font) ;
: get-font-metrics ( font -- metrics )
(cache-font) f pango_font_get_metrics &pango_font_metrics_unref ;
: parse-font-metrics ( metrics -- metrics' )
[ metrics new ] dip
[ pango_font_metrics_get_ascent pango>float >>ascent ]
[ pango_font_metrics_get_descent pango>float >>descent ] bi
compute-height ;
MEMO: (cache-font-metrics) ( font -- metrics )
[ get-font-metrics parse-font-metrics ] with-destructors ;
: cache-font-metrics ( font -- metrics )
strip-font-colors (cache-font-metrics) ;

View File

@ -5,7 +5,7 @@ USING: arrays sequences alien alien.c-types alien.destructors
alien.syntax math math.functions math.vectors destructors combinators alien.syntax math math.functions math.vectors destructors combinators
colors fonts accessors assocs namespaces kernel pango pango.fonts colors fonts accessors assocs namespaces kernel pango pango.fonts
pango.cairo cairo cairo.ffi glib unicode.data images cache init pango.cairo cairo cairo.ffi glib unicode.data images cache init
math.rectangles fry ; math.rectangles fry memoize ;
IN: pango.layouts IN: pango.layouts
LIBRARY: pango LIBRARY: pango
@ -31,8 +31,8 @@ pango_layout_get_font_description ( PangoLayout* layout ) ;
FUNCTION: void FUNCTION: void
pango_layout_get_pixel_size ( PangoLayout* layout, int* width, int* height ) ; pango_layout_get_pixel_size ( PangoLayout* layout, int* width, int* height ) ;
FUNCTION: int FUNCTION: void
pango_layout_get_baseline ( PangoLayout* layout ) ; pango_layout_get_extents ( PangoLayout* layout, PangoRectangle* ink_rect, PangoRectangle* logical_rect ) ;
FUNCTION: void FUNCTION: void
pango_layout_get_pixel_extents ( PangoLayout* layout, PangoRectangle* ink_rect, PangoRectangle* logical_rect ) ; pango_layout_get_pixel_extents ( PangoLayout* layout, PangoRectangle* ink_rect, PangoRectangle* logical_rect ) ;
@ -59,20 +59,35 @@ DESTRUCTOR: pango_layout_iter_free
TUPLE: layout font string layout metrics ink-rect logical-rect image disposed ; TUPLE: layout font string layout metrics ink-rect logical-rect image disposed ;
: layout-dim ( layout -- dim ) SYMBOL: dpi
0 <int> 0 <int> [ pango_layout_get_pixel_size ] 2keep
[ *int ] bi@ 2array ;
: layout-extents ( layout -- ink-rect logical-rect ) 72 dpi set-global
"PangoRectangle" <c-object>
"PangoRectangle" <c-object>
[ pango_layout_get_pixel_extents ] 2keep
[ PangoRectangle>rect ] bi@ ;
: layout-baseline ( layout -- baseline ) : dummy-pango-context ( -- context )
pango_layout_get_iter &pango_layout_iter_free \ dummy-pango-context [
pango_layout_iter_get_baseline pango_context_new
pango>float ; ] initialize-alien ;
MEMO: (cache-font) ( font -- open-font )
[
pango_cairo_font_map_get_default
dup dpi get pango_cairo_font_map_set_resolution
dummy-pango-context
] dip
cache-font-description
pango_font_map_load_font ;
: cache-font ( font -- open-font )
strip-font-colors (cache-font) ;
: get-font-metrics ( font -- metrics )
f pango_font_get_metrics &pango_font_metrics_unref ;
: parse-font-metrics ( metrics -- metrics' )
[ metrics new ] dip
[ pango_font_metrics_get_ascent pango>float >>ascent ]
[ pango_font_metrics_get_descent pango>float >>descent ] bi
compute-height ;
: set-layout-font ( str layout -- ) : set-layout-font ( str layout -- )
swap pango_layout_set_font_description ; swap pango_layout_set_font_description ;
@ -90,6 +105,40 @@ TUPLE: layout font string layout metrics ink-rect logical-rect image disposed ;
[ set-layout-font ] keep [ set-layout-font ] keep
[ set-layout-text ] keep ; [ set-layout-text ] keep ;
: layout-extents ( layout -- ink-rect logical-rect )
"PangoRectangle" <c-object>
"PangoRectangle" <c-object>
[ pango_layout_get_extents ] 2keep
[ PangoRectangle>rect ] bi@ ;
: glyph-height ( font string -- y )
swap <PangoLayout> &g_object_unref layout-extents drop dim>> second ;
: missing-font-metrics ( metrics font -- metrics )
#! Pango doesn't provide these, but Core Text does, so we
#! simulate them on Pango.
[ "x" glyph-height >>x-height ]
[ "Y" glyph-height >>cap-height ]
bi ;
MEMO: (cache-font-metrics) ( font -- metrics )
[
(cache-font)
[
get-font-metrics
parse-font-metrics
] keep
missing-font-metrics
] with-destructors ;
: cache-font-metrics ( font -- metrics )
strip-font-colors (cache-font-metrics) ;
: layout-baseline ( layout -- baseline )
pango_layout_get_iter &pango_layout_iter_free
pango_layout_iter_get_baseline
pango>float ;
: set-foreground ( cr font -- ) : set-foreground ( cr font -- )
foreground>> set-source-color ; foreground>> set-source-color ;
@ -130,17 +179,18 @@ TUPLE: layout font string layout metrics ink-rect logical-rect image disposed ;
: set-text-position ( cr loc -- ) : set-text-position ( cr loc -- )
first2 cairo_move_to ; first2 cairo_move_to ;
: layout-metrics ( dim baseline -- metrics ) : layout-metrics ( layout -- metrics )
metrics new dup font>> cache-font-metrics clone
swap >>ascent swap
swap first2 [ >>width ] [ >>height ] bi* [ layout>> layout-baseline >>ascent ]
[ logical-rect>> dim>> [ first >>width ] [ second >>height ] bi ] bi
dup [ height>> ] [ ascent>> ] bi - >>descent ; dup [ height>> ] [ ascent>> ] bi - >>descent ;
: text-position ( layout -- loc ) : text-position ( layout -- loc )
[ logical-rect>> ] [ ink-rect>> ] bi [ loc>> ] bi@ v- ; [ logical-rect>> ] [ ink-rect>> ] bi [ loc>> ] bi@ v- ;
: draw-layout ( layout -- image ) : draw-layout ( layout -- image )
dup ink-rect>> dim>> [ dup ink-rect>> dim>> [ >fixnum ] map [
swap { swap {
[ layout>> pango_cairo_update_layout ] [ layout>> pango_cairo_update_layout ]
[ [ font>> ] [ ink-rect>> dim>> ] bi fill-background ] [ [ font>> ] [ ink-rect>> dim>> ] bi fill-background ]
@ -158,7 +208,7 @@ TUPLE: layout font string layout metrics ink-rect logical-rect image disposed ;
swap >>font swap >>font
dup [ string>> ] [ font>> cache-font-description ] bi <PangoLayout> >>layout dup [ string>> ] [ font>> cache-font-description ] bi <PangoLayout> >>layout
dup layout>> layout-extents [ >>ink-rect ] [ >>logical-rect ] bi* dup layout>> layout-extents [ >>ink-rect ] [ >>logical-rect ] bi*
dup [ logical-rect>> dim>> ] [ layout>> layout-baseline ] bi layout-metrics >>metrics dup layout-metrics >>metrics
dup draw-layout >>image dup draw-layout >>image
] with-destructors ; ] with-destructors ;

View File

@ -32,6 +32,6 @@ C-STRUCT: PangoRectangle
{ "int" "height" } ; { "int" "height" } ;
: PangoRectangle>rect ( PangoRectangle -- rect ) : PangoRectangle>rect ( PangoRectangle -- rect )
[ [ PangoRectangle-x ] [ PangoRectangle-y ] bi 2array ] [ [ PangoRectangle-x pango>float ] [ PangoRectangle-y pango>float ] bi 2array ]
[ [ PangoRectangle-width ] [ PangoRectangle-height ] bi 2array ] bi [ [ PangoRectangle-width pango>float ] [ PangoRectangle-height pango>float ] bi 2array ] bi
<rect> ; <rect> ;

View File

@ -30,13 +30,10 @@ M: pango-renderer x>offset ( x font string -- n )
M: pango-renderer offset>x ( n font string -- x ) M: pango-renderer offset>x ( n font string -- x )
cached-line swap line-offset>x ; cached-line swap line-offset>x ;
: missing-metrics ( metrics -- metrics )
5 >>cap-height 5 >>x-height ;
M: pango-renderer font-metrics ( font -- metrics ) M: pango-renderer font-metrics ( font -- metrics )
cache-font-metrics missing-metrics ; cache-font-metrics ;
M: pango-renderer line-metrics ( font string -- metrics ) M: pango-renderer line-metrics ( font string -- metrics )
cached-layout metrics>> missing-metrics ; cached-layout metrics>> ;
pango-renderer font-renderer set-global pango-renderer font-renderer set-global