1 ! Copyright (C) 2008 Matthew Willis.
2 ! Copyright (C) 2009 Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
5 ! pangocairo bindings, from pango/pangocairo.h
6 USING: arrays sequences alien alien.c-types alien.data
7 alien.destructors alien.libraries alien.syntax math
8 math.functions math.vectors destructors combinators colors fonts
9 accessors assocs namespaces kernel pango pango.fonts
10 pango.layouts glib unicode.data images cache init system
11 math.rectangles fry memoize io.encodings.utf8 classes.struct
16 { [ os winnt? ] [ "pangocairo" "libpangocairo-1.0-0.dll" cdecl add-library ] }
17 { [ os macosx? ] [ "pangocairo" "/opt/local/lib/libpangocairo-1.0.0.dylib" cdecl add-library ] }
23 C-TYPE: PangoCairoFontMap
24 C-TYPE: PangoCairoFont
26 FUNCTION: PangoFontMap*
27 pango_cairo_font_map_new ( ) ;
29 FUNCTION: PangoFontMap*
30 pango_cairo_font_map_new_for_font_type ( cairo_font_type_t fonttype ) ;
32 FUNCTION: PangoFontMap*
33 pango_cairo_font_map_get_default ( ) ;
35 FUNCTION: cairo_font_type_t
36 pango_cairo_font_map_get_font_type ( PangoCairoFontMap* fontmap ) ;
39 pango_cairo_font_map_set_resolution ( PangoCairoFontMap* fontmap, double dpi ) ;
42 pango_cairo_font_map_get_resolution ( PangoCairoFontMap* fontmap ) ;
44 FUNCTION: PangoContext*
45 pango_cairo_font_map_create_context ( PangoCairoFontMap* fontmap ) ;
47 FUNCTION: cairo_scaled_font_t*
48 pango_cairo_font_get_scaled_font ( PangoCairoFont* font ) ;
50 ! Update a Pango context for the current state of a cairo context
52 pango_cairo_update_context ( cairo_t* cr, PangoContext* context ) ;
55 pango_cairo_context_set_font_options ( PangoContext* context, cairo_font_options_t* options ) ;
57 FUNCTION: cairo_font_options_t*
58 pango_cairo_context_get_font_options ( PangoContext* context ) ;
61 pango_cairo_context_set_resolution ( PangoContext* context, double dpi ) ;
64 pango_cairo_context_get_resolution ( PangoContext* context ) ;
67 FUNCTION: PangoLayout*
68 pango_cairo_create_layout ( cairo_t* cr ) ;
71 pango_cairo_update_layout ( cairo_t* cr, PangoLayout* layout ) ;
75 pango_cairo_show_glyph_string ( cairo_t* cr, PangoFont* font, PangoGlyphString* glyphs ) ;
78 pango_cairo_show_layout_line ( cairo_t* cr, PangoLayoutLine* line ) ;
81 pango_cairo_show_layout ( cairo_t* cr, PangoLayout* layout ) ;
84 pango_cairo_show_error_underline ( cairo_t* cr, double x, double y, double width, double height ) ;
88 pango_cairo_glyph_string_path ( cairo_t* cr, PangoFont* font, PangoGlyphString* glyphs ) ;
91 pango_cairo_layout_line_path ( cairo_t* cr, PangoLayoutLine* line ) ;
94 pango_cairo_layout_path ( cairo_t* cr, PangoLayout* layout ) ;
97 pango_cairo_error_underline_path ( cairo_t* cr, double x, double y, double width, double height ) ;
99 TUPLE: layout < disposable font string selection layout metrics ink-rect logical-rect image ;
105 : set-layout-font ( font layout -- )
106 swap cache-font-description pango_layout_set_font_description ;
108 : set-layout-text ( str layout -- )
109 #! Replace nulls with something else since Pango uses null-terminated
111 swap -1 pango_layout_set_text ;
113 : layout-extents ( layout -- ink-rect logical-rect )
114 PangoRectangle <struct>
115 PangoRectangle <struct>
116 [ pango_layout_get_extents ] 2keep
117 [ PangoRectangle>rect ] bi@ ;
119 : layout-baseline ( layout -- baseline )
120 pango_layout_get_iter &pango_layout_iter_free
121 pango_layout_iter_get_baseline
124 : set-foreground ( cr font -- )
125 foreground>> set-source-color ;
127 : fill-background ( cr font dim -- )
128 [ background>> set-source-color ]
129 [ [ { 0 0 } ] dip <rect> fill-rect ] bi-curry* bi ;
131 : rect-translate-x ( rect x -- rect' )
132 '[ _ 0 2array v- ] change-loc ;
134 : first-line ( layout -- line )
135 layout>> 0 pango_layout_get_line_readonly ;
137 : line-offset>x ( layout n -- x )
138 #! n is an index into the UTF8 encoding of the text
139 [ drop first-line ] [ swap string>> >utf8-index ] 2bi
140 0 { int } [ pango_layout_line_index_to_x ] [ ] with-out-parameters
143 : x>line-offset ( layout x -- n )
144 #! n is an index into the UTF8 encoding of the text
149 [ pango_layout_line_x_to_index drop ] [ ] with-out-parameters
151 ] [ drop string>> ] 2bi utf8-index> + ;
153 : selection-start/end ( selection -- start end )
154 selection>> [ start>> ] [ end>> ] bi ;
156 : selection-rect ( layout -- rect )
157 [ ink-rect>> dim>> ] [ ] [ selection-start/end ] tri [ line-offset>x ] bi-curry@ bi
158 [ drop nip 0 2array ] [ swap - swap second 2array ] 3bi <rect> ;
160 : fill-selection-background ( cr layout -- )
162 [ selection>> color>> set-source-color ]
164 [ selection-rect ] [ ink-rect>> loc>> first ] bi
170 : text-position ( layout -- loc )
171 [ logical-rect>> ] [ ink-rect>> ] bi [ loc>> ] bi@ v- ;
173 : set-text-position ( cr loc -- )
174 first2 cairo_move_to ;
176 : draw-layout ( layout -- image )
177 dup ink-rect>> dim>> [ >fixnum ] map [
179 [ layout>> pango_cairo_update_layout ]
180 [ [ font>> ] [ ink-rect>> dim>> ] bi fill-background ]
181 [ fill-selection-background ]
182 [ text-position set-text-position ]
183 [ font>> set-foreground ]
184 [ layout>> pango_cairo_show_layout ]
186 ] make-bitmap-image ;
188 : escape-nulls ( str -- str' )
189 { { 0 CHAR: zero-width-no-break-space } } substitute ;
191 : unpack-selection ( layout string/selection -- layout )
193 [ string>> escape-nulls >>string ] [ >>selection ] bi
194 ] [ escape-nulls >>string ] if ; inline
196 : set-layout-resolution ( layout -- )
197 pango_layout_get_context dpi get pango_cairo_context_set_resolution ;
199 : <PangoLayout> ( text font -- layout )
200 dummy-cairo pango_cairo_create_layout |g_object_unref
201 [ set-layout-resolution ] keep
202 [ set-layout-font ] keep
203 [ set-layout-text ] keep ;
205 : glyph-height ( font string -- y )
206 swap <PangoLayout> &g_object_unref layout-extents drop dim>> second ;
208 MEMO: missing-font-metrics ( font -- metrics )
209 #! Pango doesn't provide x-height and cap-height but Core Text does, so we
210 #! simulate them on Pango.
213 [ "x" glyph-height >>x-height ]
214 [ "Y" glyph-height >>cap-height ] bi
217 : layout-metrics ( layout -- metrics )
218 dup font>> missing-font-metrics clone
220 [ layout>> layout-baseline >>ascent ]
221 [ logical-rect>> dim>> [ first >>width ] [ second >>height ] bi ] bi
222 dup [ height>> ] [ ascent>> ] bi - >>descent ;
224 : <layout> ( font string -- line )
226 layout new-disposable
227 swap unpack-selection
229 dup [ string>> ] [ font>> ] bi <PangoLayout> >>layout
230 dup layout>> layout-extents [ >>ink-rect ] [ >>logical-rect ] bi*
231 dup layout-metrics >>metrics
232 dup draw-layout >>image
235 M: layout dispose* layout>> g_object_unref ;
237 SYMBOL: cached-layouts
239 : cached-layout ( font string -- layout )
240 cached-layouts get [ <layout> ] 2cache ;
242 : cached-line ( font string -- line )
243 cached-layout layout>> first-line ;
245 [ <cache-assoc> cached-layouts set-global ] "pango.cairo" add-startup-hook