File: pstext.c

package info (click to toggle)
libforms 1.0.93sp1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,548 kB
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (393 lines) | stat: -rw-r--r-- 9,814 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * This file is part of XForms.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XForms. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file pstext.c
 *
 *  This file is part of XForms package
 *  Copyright (c) 1997-2000  by T.C. Zhao
 *  All rights reserved.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include/forms.h"
#include "fd2ps.h"
#include <stdlib.h>

char *ul_magic_char = "\010";


/***************************************
 ***************************************/

void
ps_text_init( void )
{
    ps_output( "/point {SX SY add 2 div div} BD\n" );
    ps_output( "/SetFont {findfont exch scalefont setfont} BD\n" );
    ps_output( "/Lshow {show} BD\n" );
    ps_output( "/Cshow {dup stringwidth pop -2 div 0 rmoveto show} BD\n" );
    ps_output( "/Rshow {dup stringwidth pop neg 0 rmoveto show} BD\n" );
    ps_output( "/CP {currentpoint} bind def /SW {stringwidth} BD\n" );
}


/*
 * Text stuff. Default fonts follows the XFORM built-in
 */

static const char *fnts[ FL_MAXFONTS ] =
{
    "Helvetica",
    "Helvetica-Bold",
    "Helvetica-Oblique",
    "Helvetica-BoldOblique",
    "Courier",
    "Courier-Bold",
    "Courier-Oblique",
    "Courier-BoldOblique",
    "Times-Roman",
    "Times-Bold",
    "Times-Italic",
    "Times-BoldItalic",

 /* xforms users charter */
    "Times-Roman",
    "Times-Bold",
    "Times-Italic",
    "Times-BoldItalic",

    "Symbol"
};

static int cur_style = -1, cur_size = -1;


/***************************************
 ***************************************/

void
ps_invalidate_font_cache( void )
{
    cur_style = cur_size = -1;
}


/***************************************
 ***************************************/

void
ps_set_font( int style,
             int size )
{
    if ( style >= FL_SHADOW_STYLE )
        style %= FL_SHADOW_STYLE;

    if ( style == cur_style && cur_size == size )
        return;

    ps_output( "%d point /%s SetFont\n", size, fnts[ style ] );
    ps_output( "/H %d point def\n", size );
    cur_style = style;
    cur_size = size;
}


#define has_desc(s)    (strchr(s,'g')||strchr(s,'j')||\
                        strchr(s,'q')||strchr(s,'y')||strchr(s,'p'))
#define is_desc(c)     (c=='g'||(c=='j')||(c=='q')||(c=='y')||(c=='p'))


/***************************************
 ***************************************/

static void
do_underline( const char * s,
              int          n )
{
    ps_output( "/len (%s) SW pop def\n", s );

    if ( n == -1 )
    {
        ps_output( "/ty CP exch pop %d sub def ", 1 + has_desc( s ) * 2 );
        ps_output( "/tx CP pop len sub def " );
        ps_output( "tx ty M len tx add ty LT S\n" );
    }
    else
    {
        char *t = fl_strdup( s );
        *( t + n ) = '\0';
        ps_output( "/ul (%c) SW pop def\n", *( s + n ) );
        ps_output( "/ty CP exch pop %d sub def ", 1 + is_desc(s[ n ] ) * 2 );
        ps_output( "/tx CP pop len sub (%s) SW pop add def\n", t );
        ps_output( "tx ty M ul tx add ty LT S\n" );
        free( t );
    }
}


/***************************************
 ***************************************/

#define Quote( c )  ( c == '(' || c == ')' || c == '%' )

static char *
ps_quote( char *str )
{
    static char buf[ 1024 ];
    char *s, *q = buf;

    for ( s = str; *s; s++ )
    {
        if ( Quote( *s ) )
            *q++ = '\\';
        *q++ = *s;
    }
    *q = '\0';
    return buf;
}


/***************************************
 * This routine is always called by other routines. Once we get here,
 * the text would always remain within the box specified by
 * {x,y,w,h}.
 ***************************************/

static void
ps_drw_text( int          align,
             float        x,
             float        y,
             float        w,
             float        h,
             long         c,
             int          style,
             int          size,
             const char * sstr )
{
    int ulpos = -1, lnumb;
    int i;
    char *lines[ 512 ];
    char *str;
    char newlabel[ 255 ], *p;
    float bw = 2;
    int halign, valign;

    ps_color( c );
    ps_set_font( style, size );

    str = fl_strdup( sstr );

    lines[ 0 ] = str;

    /* Split (multi-line)string into lines */

    for ( i = 0, lnumb = 1; str[ i ] != '\0'; i++ )
        if ( str[ i ] == '\n' )
        {
            str[ i ] = '\0';
            lines[ lnumb ] = str + i + 1;
            lnumb++;
        }

    fl_get_hv_align( align, &halign, &valign );

    /* Figure out where the starting point is */

    switch ( halign )
    {
        case FL_ALIGN_CENTER:
            ps_output( "/x %.1f def ", x + 0.5 * w );
            break;

        case FL_ALIGN_LEFT:
            ps_output( "/x %.1f def ", bw + x );
            break;

        case FL_ALIGN_RIGHT:
            ps_output( "/x %.1f def ", x + w - 1 - bw );
            break;
    }

    switch ( valign )
    {
        case FL_ALIGN_TOP:
            ps_output( "/y %.1f H sub def\n", y + h - bw );
            break;

        case FL_ALIGN_BOTTOM:
            ps_output( "/y %.1f %.1f H mul add def\n",
                       y + bw, lnumb - 0.9 );
            break;

        case FL_ALIGN_CENTER:
            ps_output( "/y %.1f %.1f H mul add def\n",
                       y + 0.5 * h, 0.5 * lnumb - 0.9 );
            break;
    }

    for ( i = 0; i < lnumb; i++ )
    {
        /* Check if have underline request */

        if ( ( p = strchr( lines[ i ], *ul_magic_char ) ) )
        {
            char *q;

            ulpos = p - lines[ i ];
            q = newlabel;
            p = lines[ i ];
            for ( ; *p; p++ )
                if ( *p != *ul_magic_char )
                    *q++ = *p;
            *q = 0;
            lines[ i ] = newlabel;
        }

        ps_output( "x y M " );

        switch ( halign )
        {
            case FL_ALIGN_CENTER:
                ps_output( "(%s) Cshow\n", ps_quote( lines[ i ] ) );
                break;

            case FL_ALIGN_LEFT:
                ps_output( "(%s) Lshow\n", ps_quote( lines[ i ] ) );
                break;

            case FL_ALIGN_RIGHT:
                ps_output( "(%s) Rshow\n", ps_quote( lines[ i ] ) );
                break;
        }

        if ( ulpos >= 0 )
            do_underline( lines[ i ], ulpos - 1 );
        ulpos = -1;
        if ( i != lnumb - 1 )
            ps_output( "/y y H sub def\n" );
    }

    free( str );
}


/***************************************
 ***************************************/

#define D( x, y, c )  ps_drw_text( align, x, y, w, h, c, style, size, str )

void
ps_draw_text( int          align,
              float        x,
              float        y,
              float        w,
              float        h,
              long         col,
              int          style,
              int          size,
              const char * str)
{
    int special = FL_INVALID_STYLE;

    if ( ! str || ! *str )
        return;

    if ( *str == '@' )
    {
        ps_draw_symbol( str, x, y, w, h, col );
        return;
    }

    if ( special_style( style ) )
    {
        special = ( style / FL_SHADOW_STYLE ) * FL_SHADOW_STYLE;
        style %= FL_SHADOW_STYLE;
    }

    if ( special == FL_SHADOW_STYLE )
        D( x + 2, y - 2, FL_BOTTOM_BCOL );
    else if ( special == FL_ENGRAVED_STYLE )
    {
        D( x - 1, y,     FL_RIGHT_BCOL );
        D( x,     y + 1, FL_RIGHT_BCOL );
        D( x - 1, y + 1, FL_RIGHT_BCOL );
        D( x + 1, y,     FL_LEFT_BCOL );
        D( x,     y - 1, FL_LEFT_BCOL );
        D( x + 1, y - 1, FL_LEFT_BCOL );
    }
    else if ( special == FL_EMBOSSED_STYLE )
    {
        D( x - 1, y,     FL_TOP_BCOL );
        D( x,     y + 1, FL_TOP_BCOL );
        D( x - 1, y + 1, FL_TOP_BCOL );
        D( x + 1, y,     FL_RIGHT_BCOL );
        D( x,     y - 1, FL_RIGHT_BCOL );
        D( x + 1, y - 1, FL_RIGHT_BCOL );
    }

    ps_drw_text( align, x, y, w, h, col, style, size, str );
}


/***************************************
 ***************************************/

void
ps_draw_text_beside( int          align,
                     float        x,
                     float        y,
                     float        w,
                     float        h,
                     long         col,
                     int          style,
                     int          size,
                     const char * str )
{
    int newa, newx, newy;

    if ( ! str || ! *str )
        return;

    align &= ~FL_ALIGN_INSIDE;
#if 1
    fl_get_outside_align( align, x, y, w, h, &newa, &newx, &newy );
    ps_draw_text( newa, newx, newy, w, h, col, style, size, str );
#else
    if ( align == FL_ALIGN_LEFT )
        ps_draw_text( FL_ALIGN_RIGHT, x - h, y, h, h, col, style, size, str );
    else if ( align == FL_ALIGN_RIGHT )
        ps_draw_text( FL_ALIGN_LEFT, x + w, y, h, h, col, style, size, str );
    else if ( align == FL_ALIGN_TOP )
        ps_draw_text( FL_ALIGN_BOTTOM, x, y + h, w, h, col, style, size, str );
    else if ( align == FL_ALIGN_BOTTOM )
        ps_draw_text( FL_ALIGN_TOP, x, y - h, w, h, col, style, size, str );
    else
        ps_draw_text( FL_ALIGN_CENTER, x, y, w, h, col, style, size, str );
#endif
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */