SwingOSC – Java-based GUI classes

This class is meant as an emulation of Font. last mod: 16-Jan-08 sciss

no-op / not working
antiAliasing_has no effect
smoothing_has no effect
different behaviour
availableFontsneeds to be called asynchronously the first time
extended functionality
styleextra field for plain / bold / italic

 

JFont

Note: please use the abstraction layer GUI.font if possible! (see GUI)

JFont is a typeface description that can used with some JSCView objects to define the type of font used for text rendering. JSCStaticText, JSCButton, JSCNumberBox, JSCDragView, JSCDragSink, JSCDragBoth, JSCCheckBox can set their fonts.

Besides, fonts can be used in user generated views : see JSCUserView and JPen for details.

Instantiation

	JFont.new( <name>, <size>, [ <style> ] )
	

 

Example:

(
var w,f;

w = JSCWindow( "Fonts", Rect( 128, 64, 340, 360 ));
w.view.decorator = f = FlowLayout( w.view.bounds, 4 @ 4, 4 @ 2 );

[
"Helvetica",
"Futura",
"Courier",
"Times",
"Lucida Grande",
"Monotype Corsiva"

].do({ arg name;
    var v, s, n, spec, p, height = 16;

    v = JSCStaticText( w, Rect( 0, 0, 56, height + 2 ));
    v.font = JFont( name, 13 );
    v.string = name;

    s = JSCButton( w, Rect( 0, 0, 140, height + 2 ));
    s.font = JFont( name, 13 );
    s.states = [[ name ]];
    
    n = JSCNumberBox( w, Rect( 0, 0, 56, height + 2 ));
    n.font = JFont( name, 13 );
    n.object = pi;

    f.nextLine;
});

w.front;
)

Retrieving All Available Fonts

    JFont.availableFonts
	

Note: on SwingOSC this call is asynchronous the first time, because the names must be fetched from the server. this requires that the call is placed inside a Routine. Successive calls can be carried out normally. You can avoid the extra hassle by calling *availableFonts once in your startup procedure like this:

    { JFont.availableFonts }.fork( AppClock );

Example:

(
var w, f, t, c, names, range, char, start, stop, lastStop, fontSize = 14; // change fontSize if you like

w       = JSCWindow( "Fonts", Rect( 128, 128, 800, (fontSize + 9) * 20 + 80 ), resizable: false );
t       = JSCTabbedPane( w, w.view.bounds );
names   = JFont.availableFonts;
stop        = 0;
lastStop    = stop;
while({ stop < names.size }, {
    if( names[ stop ].first != char, {
        char    = names[ stop ].first;
        start   = stop;
    });
    stop = stop + 1;
    if( (((stop - lastStop) % 60) == 0) || (stop == names.size), {
        if( (stop < names.size) && (start > lastStop), {
            stop = start;
        });
        c = JSCCompositeView( t, t.bounds );
        c.decorator = f = FlowLayout( c.bounds, 4 @ 4, 4 @ 2 );
        range = names.copyRange( lastStop, stop - 1 );
        t.setTitleAt( t.numTabs - 1, range.first.first ++ " ... " ++ range.last.first );
        range.do({ arg name, i; var font;
            font = JFont( name, 13 );
            
            JSCStaticText( c, Rect( 0, 0, 56, fontSize + 7 ))
                .font_( font )
                .string_( name );
        
            JSCButton( c, Rect( 0, 0, 140, fontSize + 7 ))
                .font_( font )
                .states_([[ name ]])
                .action_({ font.postcs });
            
            JSCNumberBox( c, Rect( 0, 0, 56, fontSize + 7 ))
                .font_( font )
                .object_( pi );
                
            if( (i = i + 1) % 3 == 0, { f.nextLine });
        });
        lastStop = stop;
    });
});
t.value = 0;
w.front;
)

Logical Names and Variants

Since it typically depends on your platform which fonts are available, convenient methods are provided to access some standard types of fonts and to get variants of fonts:

    JFont.defaultSansFace
    JFont.defaultSerifFace
    JFont.defaultMonoFace
    <aFont>.boldVariant

Example:

(
var w, f, font;

w = JSCWindow( "Fonts", Rect( 256, 256, 240, 120 ), resizable: false );
w.view.decorator = f = FlowLayout( w.view.bounds, 4 @ 4, 4 @ 2 );

[
"sans",  JFont.defaultSansFace,
"serif", JFont.defaultSerifFace,
"mono",  JFont.defaultMonoFace
].pairsDo({ arg label, name;
    font = JFont( name, 14 );
    JSCStaticText( w, Rect( 0, 0, 100, 22 ))
        .font_( font )
        .string_( label );
    font = font.boldVariant;
    JSCStaticText( w, Rect( 0, 0, 100, 22 ))
        .font_( font )
        .string_( label ++ "-bold" );
    f.nextLine;
});

w.front;

)