<commit_before>! Copyright (C) 2009, 2010 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data alien.strings arrays assocs
cache cairo cairo.ffi classes.struct combinators destructors fonts fry
gobject.ffi init io.encodings.utf8 kernel math math.rectangles
math.vectors memoize namespaces pango.cairo.ffi pango.ffi sequences
ui.text ui.text.private ;
IN: ui.text.pango

: pango>float ( n -- x ) PANGO_SCALE /f ; inline
: float>pango ( x -- n ) PANGO_SCALE * >integer ; inline

MEMO: (cache-font-description) ( font -- description )
    [
        [ pango_font_description_new |pango_font_description_free ] dip {
            [ name>> utf8 string>alien pango_font_description_set_family ]
            [ size>> float>pango pango_font_description_set_size ]
            [ bold?>> PANGO_WEIGHT_BOLD PANGO_WEIGHT_NORMAL ? pango_font_description_set_weight ]
            [ italic?>> PANGO_STYLE_ITALIC PANGO_STYLE_NORMAL ? pango_font_description_set_style ]
            [ drop ]
        } 2cleave
    ] with-destructors ;

: cache-font-description ( font -- description )
    strip-font-colors (cache-font-description) ;


TUPLE: layout < disposable font string selection layout metrics ink-rect logical-rect image ;

SYMBOL: dpi

72 dpi set-global

: set-layout-font ( font layout -- )
    swap cache-font-description pango_layout_set_font_description ;

: set-layout-text ( str layout -- )
    swap utf8 string>alien -1 pango_layout_set_text ;
<commit_msg>ui.text.pango: don't create unnecessary font objects.<commit_after>! Copyright (C) 2009, 2010 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data alien.strings arrays assocs
cache cairo cairo.ffi classes.struct combinators destructors fonts fry
gobject.ffi init io.encodings.utf8 kernel locals math math.rectangles
math.vectors memoize namespaces pango.cairo.ffi pango.ffi sequences
ui.text ui.text.private ;
IN: ui.text.pango

: pango>float ( n -- x ) PANGO_SCALE /f ; inline
: float>pango ( x -- n ) PANGO_SCALE * >integer ; inline

MEMO:: (cache-font-description) ( name size bold? italic? -- description )
    [
        pango_font_description_new |pango_font_description_free {
            [ name utf8 string>alien pango_font_description_set_family ]
            [ size float>pango pango_font_description_set_size ]
            [ bold? PANGO_WEIGHT_BOLD PANGO_WEIGHT_NORMAL ? pango_font_description_set_weight ]
            [ italic? PANGO_STYLE_ITALIC PANGO_STYLE_NORMAL ? pango_font_description_set_style ]
            [ ]
        } cleave
    ] with-destructors ;

: cache-font-description ( font -- description )
    { [ name>> ] [ size>> ] [ bold?>> ] [ italic?>> ] } cleave
    (cache-font-description) ;

TUPLE: layout < disposable font string selection layout metrics ink-rect logical-rect image ;

SYMBOL: dpi

72 dpi set-global

: set-layout-font ( font layout -- )
    swap cache-font-description pango_layout_set_font_description ;

: set-layout-text ( str layout -- )
    swap utf8 string>alien -1 pango_layout_set_text ;

{'fraction_non_alphanumeric': 0.09551468215553405, 'fraction_numerical': 0.00935785737334624, 'mean_word_length': 3.5454545454545454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916256', 'n_tokens_mistral': 1042, 'n_tokens_neox': 994, 'n_words': 239}