<commit_before>! Copyright (C) 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays sequences math math.order cache opengl
opengl.gl strings fonts colors accessors namespaces
ui.gadgets.worlds ;
IN: ui.text

<PRIVATE

SYMBOL: font-renderer

: world-text-handle ( world -- handle )
    dup text-handle>> [ <cache-assoc> >>text-handle ] unless
    text-handle>> ;

HOOK: flush-layout-cache font-renderer ( -- )

[ flush-layout-cache ] flush-layout-cache-hook set-global

HOOK: string-dim font-renderer ( font string -- dim )

HOOK: string-width font-renderer ( font string -- w )

HOOK: string-height font-renderer ( font string -- h )

M: object string-dim [ string-width ] [ string-height ] 2bi 2array ;

M: object string-width string-dim first ;

M: object string-height string-dim second ;

HOOK: draw-string font-renderer ( font string -- )

HOOK: free-fonts font-renderer ( world -- )

: combine-text-dim ( dim1 dim2 -- dim3 )
    [ [ first ] bi@ max ]
    [ [ second ] bi@ + ]
    2bi 2array ;

PRIVATE>

HOOK: x>offset font-renderer ( x font string -- n )

HOOK: offset>x font-renderer ( n font string -- x )

GENERIC: text-dim ( font text -- dim )

M: string text-dim string-dim ;

M: array text-dim
    [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;

: text-width ( font text -- w ) text-dim first ;

: text-height ( font text -- h ) text-dim second ;

HOOK: font-metrics font-renderer ( font -- metrics )

HOOK: line-metrics font-renderer ( font string -- metrics )

GENERIC: draw-text ( font text -- )

M: string draw-text draw-string ;

M: selection draw-text draw-string ;

M: array draw-text
    [
        [
            [ draw-string ]
            [ [ 0.0 ] 2dip string-height 0.0 glTranslated ] 2bi
        ] with each
    ] do-matrix ;

USING: vocabs.loader namespaces system combinators ;

"ui-backend" get [
    {
        { [ os macosx? ] [ "core-text" ] }
        { [ os windows? ] [ "uniscribe" ] }
        { [ os unix? ] [ "pango" ] }
    } cond
] unless* "ui.text." prepend require<commit_msg>"ui-backend" variable can't be used to choose text backend<commit_after>! Copyright (C) 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays sequences math math.order cache opengl
opengl.gl strings fonts colors accessors namespaces
ui.gadgets.worlds ;
IN: ui.text

<PRIVATE

SYMBOL: font-renderer

: world-text-handle ( world -- handle )
    dup text-handle>> [ <cache-assoc> >>text-handle ] unless
    text-handle>> ;

HOOK: flush-layout-cache font-renderer ( -- )

[ flush-layout-cache ] flush-layout-cache-hook set-global

HOOK: string-dim font-renderer ( font string -- dim )

HOOK: string-width font-renderer ( font string -- w )

HOOK: string-height font-renderer ( font string -- h )

M: object string-dim [ string-width ] [ string-height ] 2bi 2array ;

M: object string-width string-dim first ;

M: object string-height string-dim second ;

HOOK: draw-string font-renderer ( font string -- )

HOOK: free-fonts font-renderer ( world -- )

: combine-text-dim ( dim1 dim2 -- dim3 )
    [ [ first ] bi@ max ]
    [ [ second ] bi@ + ]
    2bi 2array ;

PRIVATE>

HOOK: x>offset font-renderer ( x font string -- n )

HOOK: offset>x font-renderer ( n font string -- x )

GENERIC: text-dim ( font text -- dim )

M: string text-dim string-dim ;

M: array text-dim
    [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;

: text-width ( font text -- w ) text-dim first ;

: text-height ( font text -- h ) text-dim second ;

HOOK: font-metrics font-renderer ( font -- metrics )

HOOK: line-metrics font-renderer ( font string -- metrics )

GENERIC: draw-text ( font text -- )

M: string draw-text draw-string ;

M: selection draw-text draw-string ;

M: array draw-text
    [
        [
            [ draw-string ]
            [ [ 0.0 ] 2dip string-height 0.0 glTranslated ] 2bi
        ] with each
    ] do-matrix ;

USING: vocabs.loader namespaces system combinators ;

{
    { [ os macosx? ] [ "core-text" ] }
    { [ os windows? ] [ "uniscribe" ] }
    { [ os unix? ] [ "pango" ] }
} cond "ui.text." prepend require
{'fraction_non_alphanumeric': 0.13468992248062014, 'fraction_numerical': 0.009689922480620155, 'mean_word_length': 2.6734875444839856, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '7287344', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1402, 'n_words': 410}