iface.c 17.3 KB
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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
/* $Xorg: iface.c,v 1.3 2000/08/17 19:46:25 cpqbld Exp $ */

/*

Copyright 1989-1991, Bitstream Inc., Cambridge, MA.
You are hereby granted permission under all Bitstream propriety rights to
use, copy, modify, sublicense, sell, and redistribute the Bitstream Speedo
software and the Bitstream Charter outline font for any purpose and without
restrictions; provided, that this notice is left intact on all copies of such
software or font and that Bitstream's trademark is acknowledged as shown below
on all unmodified copies of such font.

BITSTREAM CHARTER is a registered trademark of Bitstream Inc.


BITSTREAM INC. DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE.  BITSTREAM SHALL NOT BE LIABLE FOR ANY DIRECT OR INDIRECT
DAMAGES, INCLUDING BUT NOT LIMITED TO LOST PROFITS, LOST DATA, OR ANY OTHER
INCIDENTAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF OR IN ANY WAY CONNECTED
WITH THE SPEEDO SOFTWARE OR THE BITSTREAM CHARTER OUTLINE FONT.

*/
/* $XFree86: xc/lib/font/Speedo/iface.c,v 1.2 1998/07/25 06:56:53 dawes Exp $ */

/***************************** I F A C E . C *********************************
 *                                                                           *
 * This module provides a layer to make Speedo function calls to and         *
 * from it compatible with Fontware 2.X function calls.                      *
 *                                                                           *
 ****************************************************************************/

#include "speedo.h"               /* General definitions for Speedo */
#ifndef FONTMODULE
#include <math.h>
#else
#include "xf86_ansic.h"
#endif

#define   DEBUG      0

#if DEBUG
#include <stdio.h>
#define SHOW(X) printf("X = %d\n", X)
#else
#define SHOW(X)
#endif

#define  PI     3.1415926536       /* pi */
#define  PTPERINCH   72.2892       /* nbr points per inch, exactly! */

#define  BIT8           0x0100
#define  BIT9           0x0200
#define  BIT10          0x0400
#define  BIT11          0x0800
#define  BIT12          0x1000
#define  BIT13          0x2000
#define  BIT14          0x4000
#define  BIT15          0x8000

#define   READ      0

typedef   short     bool16;
typedef   int       bool;

typedef
struct
    {
    bool16  left;
    bool16  right;
    bool16  top;
    bool16  bottom;
    }   lrtb;


typedef
struct
    {
    buff_t *pfont;              /* Pointer to font data                    */
    ufix16  mode;               /* what mode is the font generator in      */
    real    point_size_x;       /* Point size in X dimension               */
    real    point_size_y;       /* Point size in Y dimension               */
    real    res_hor;            /* Horizontal resolution of output device  */
    real    res_ver;            /* Vertical resolution of output device    */
    real    rot_angle;          /* Rotation angle in degrees (clockwise)   */
    real    obl_angle;          /* Obliquing angle in degrees (clockwise)  */
    bool16  whitewrite;         /* if T, generate bitmaps for whitewriters */
    fix15   thresh;             /* Scan conversion threshold               *
                                 * Thickens characters on each edge by     *
                                 * <thresh> sub-pixels                     */  
    bool16  import_widths;      /* Use imported width table                */
    lrtb    clip;               /* Clip to standard character cell         */
    lrtb    squeeze;            /* Squeeze to standard character cell      */
    bool16  bogus_mode;         /* if T, ignore plaid data                 */
    }   comp_char_desc;         /* character attributes for scan conv      */

/***** GLOBAL VARIABLES *****/

/*****  GLOBAL FUNCTIONS *****/
     fw_reset();                /* Fontware 2.X reset call                 */
     fw_set_specs();            /* Fontware 2.X set specs call             */
bool fw_make_char();            /* Fontware 2.X make character call        */
       
/***** EXTERNAL VARIABLES *****/

/***** EXTERNAL FUNCTIONS *****/
void    _open_bitmap();
void    _close_bitmap();
void    _set_bitmap_bits();
void    _open_outline();
void    _open_outline();
void    _start_new_char();
void    _start_curve();
void    _line_to();
void    _close_curve();
void    _close_outline();

/***** STATIC VARIABLES *****/
static specs_t *pspecs;
static buff_t  *pfont;
static buff_t   char_data;
static fix15    set_width_x;
static specs_t  specsarg;

/***** STATIC FUNCTIONS *****/
static fix31 make_mult();


FUNCTION fw_reset()
{
sp_reset();
}

FUNCTION fw_set_specs(pspecs)
comp_char_desc  *pspecs;  /* Pointer to scan conversion parameters structure */

/*  Fontware 2.X character generator call to set font specifications
 *  compc -- pointer to structure containing scan conversion parameters.
 *   ->compf -- compressed font data structure
 *   ->point_size_x -- x pointsize
 *   ->point_size_y -- y pointsize
 *   ->res_hor -- horizontal pixels per inch
 *   ->res_ver -- vertical pixels per inch
 *   ->rot_angle -- rotation angle in degrees (clockwise)
 *   ->obl_angle -- obliquing angle in degrees (clockwise)
 *   ->whitewrite -- if true, generate bitmaps for whitewriters
 *   ->thresh -- scan-conversion threshold
 *   ->import_widths -- if true, use external width table
 *   ->clip.left -- clips min x at left of emsquare
 *   ->clip.right -- clips max x at right of emsquare
 *   ->clip.bottom -- clips min x at bottom of emsquare
 *   ->clip.top -- clips max x at top of emsquare
 *   ->squeeze.left -- squeezes min x at left of emsquare
 *   ->squeeze.right, .top, .bottom  &c
 *   ->sw_fixed -- if TRUE, match pixel widths to scaled outline widths
 *   ->bogus_mode -- ignore plaid data if TRUE
 */

{
fix15   irot;
fix15   iobl;
fix15   x_trans_type;
fix15   y_trans_type;
fix31   xx_mult;
fix31   xy_mult;
fix31   yx_mult;
fix31   yy_mult;
real    sinrot, cosrot, tanobl;
real    x_distortion;
real    pixperem_h;
real    pixperem_v;
real    point_size_x;
real    point_size_y;
real    res_hor;
real    res_ver;
fix15   mode;

specsarg.pfont = pspecs->pfont;

irot = floor(pspecs->rot_angle + 0.5);
iobl = floor(pspecs->obl_angle + 0.5);
if (iobl > 85)
    iobl = 85;
if (iobl < -85)
    iobl = -85;
if ((irot % 90) == 0)
    {
    x_trans_type = y_trans_type = irot / 90 & 0x0003;
    if (iobl != 0)
        {
        if (x_trans_type & 0x01)
            y_trans_type = 4;
        else
            x_trans_type = 4;
        }
    }
else if (((irot + iobl) % 90) == 0)
    {
    x_trans_type = y_trans_type = (irot + iobl) / 90 & 0x0003;
    if (iobl != 0)
        {
        if (x_trans_type & 0x01)
            x_trans_type = 4;
        else
            y_trans_type = 4;
        }
    }
else
    {
    x_trans_type = y_trans_type = 4;
    }

point_size_x = pspecs->point_size_x;
point_size_y = pspecs->point_size_y;
res_hor = pspecs->res_hor;
res_ver = pspecs->res_ver;

switch (x_trans_type)
    {
case 0: 
    xx_mult = make_mult(point_size_x, res_hor);
    xy_mult = 0;
    break;

case 1:
    xx_mult = 0;
    xy_mult = make_mult(point_size_y, res_hor);
    break;

case 2: 
    xx_mult = -make_mult(point_size_x, res_hor);
    xy_mult = 0;
    break;

case 3:
    xx_mult = 0;
    xy_mult = -make_mult(point_size_y, res_hor);
    break;

default:
    sinrot = sin((real)irot * PI / 180.);
    cosrot = cos((real)irot * PI / 180.);
    tanobl = tan((real)iobl * PI / 180.);
    x_distortion = point_size_x / point_size_y;
    pixperem_h = point_size_y * res_hor / (real)PTPERINCH;   /* this is NOT a bug */
    xx_mult = floor(cosrot * x_distortion * pixperem_h * 65536.0 + 0.5);
    xy_mult = floor((sinrot + cosrot * tanobl) * pixperem_h * 65536.0 + 0.5);
    break;
    }

switch (y_trans_type)
    {
case 0: 
    yx_mult = 0;
    yy_mult = make_mult(point_size_y, res_ver);
    break;

case 1:
    yx_mult = -make_mult(point_size_x, res_hor);
    yy_mult = 0;
    break;

case 2: 
    yx_mult = 0;
    yy_mult = -make_mult(point_size_y, res_ver);
    break;

case 3:
    yx_mult = make_mult(point_size_x, res_ver);
    yy_mult = 0;
    break;

default:
    sinrot = sin((real)irot * PI / 180.);
    cosrot = cos((real)irot * PI / 180.);
    tanobl = tan((real)iobl * PI / 180.);
    x_distortion = point_size_x / point_size_y;
    pixperem_v = point_size_y * res_ver / (real)PTPERINCH;
    yx_mult = floor(-sinrot * x_distortion * pixperem_v * 65536.0 + 0.5);
    yy_mult = floor((cosrot - sinrot * tanobl) * pixperem_v * 65536.0 + 0.5);
    break;
    }

specsarg.xxmult = xx_mult;
specsarg.xymult = xy_mult;
specsarg.xoffset = 0;
specsarg.yxmult = yx_mult;
specsarg.yymult = yy_mult;
specsarg.yoffset = 0;
specsarg.out_info = 0;

/* Select processing mode */
switch (pspecs->mode)
    {
case 1:
    if (pspecs->whitewrite)           /* White-write requested? */
        {
        mode = 1;
        }
    else
        {
        mode = 0;
        }
    break;
    
case 2:
    mode = 2;
    break;

default:
    mode = pspecs->mode;
    break;
    }

if (pspecs->bogus_mode)        /* Linear transformation requested? */
    {
    mode |= BIT4;              /* Set linear tranformation flag */
    }

if (pspecs->import_widths)     /* Imported widths requested? */
    {
    mode |= BIT6;              /* Set imported width flag */
    }

if (pspecs->clip.left)         /* Clip left requested? */
    {
    mode |= BIT8;              /* Set clip left flag */
    }

if (pspecs->clip.right)        /* Clip right requested? */
    {
    mode |= BIT9;              /* Set clip right flag */
    }

if (pspecs->clip.top)          /* Clip top requested? */
    {
    mode |= BIT10;             /* Set clip top flag */
    }

if (pspecs->clip.bottom)       /* Clip bottom requested? */
    {
    mode |= BIT11;             /* Set clip bottom flag */
    }

if (pspecs->squeeze.left)      /* Squeeze left requested? */
    {
    mode |= BIT12;             /* Set squeeze left flag */
    }

if (pspecs->squeeze.right)     /* Squeeze right requested? */
    {
    mode |= BIT13;             /* Set squeeze right flag */
    }

if (pspecs->squeeze.top)       /* Squeeze top requested? */
    {
    mode |= BIT14;             /* Set squeeze top flag */
    }

if (pspecs->squeeze.bottom)    /* Squeeze bottom requested? */
    {
    mode |= BIT15;             /* Set squeeze bottom flag */
    }

specsarg.flags = mode;

sp_set_specs(&specsarg);
}

FUNCTION static fix31 make_mult(point_size, resolution)
real point_size;
real resolution;
{
real ms_factor;

return (fix31)floor((point_size * resolution * 65536.0) / (real)PTPERINCH + 0.5);
}

FUNCTION bool fw_make_char(char_index)
ufix16 char_index;
{
return sp_make_char(char_index);
}

FUNCTION buff_t *sp_load_char_data(file_offset, no_bytes, cb_offset)
fix31    file_offset;
fix15    no_bytes;
fix15    cb_offset;
/* 
 * Called by Speedo character generator to request that character
 * data be loaded from the font file.
 * This is a dummy function that assumes that the entire font has
 * been loaded.
 */
{
#if DEBUG
printf("load_char_data(%d, %d, %d)\n", file_offset, no_bytes, char_offset);
#endif
char_data.org = pfont->org + file_offset;
char_data.no_bytes = no_bytes;
return &char_data;
}

FUNCTION void sp_report_error(n)
fix15 n;        /* Error identification number */
/*
 * Called by Speedo character generator to report an error.
 */
{
switch(n)
    {
case 1:
    printf("Insufficient font data loaded\n");
    break;

case 3:
    printf("Transformation matrix out of range\n");
    break;

case 4:
    printf("Font format error\n");
    break;
                 
case 5:
    printf("Requested specs not compatible with output module\n");
    break;

case 7:
    printf("Intelligent transformation requested but not supported\n");
    break;

case 8:
    printf("Unsupported output mode requested\n");
    break;

case 9:
    printf("Extended font loaded but only compact fonts supported\n");
    break;

case 10:
    printf("Font specs not set prior to use of font\n");
    break;

case 12:
    printf("Character data not available()\n");
    break;

case 13:
    printf("Track kerning data not available()\n");
    break;

case 14:
    printf("Pair kerning data not available()\n");
    break;

default:
    printf("report_error(%d)\n", n);
    break;
    }
}



FUNCTION void sp_open_bitmap(sw_x, sw_y, xorg, yorg, xsize, ysize)
fix31  sw_x;                           /* X component of escapement vector */
fix31  sw_y;                           /* Y component of escapement vector */
fix31  xorg;                           /* X origin */
fix31  yorg;                           /* Y origin */
fix15 xsize;                           /* width of bitmap */
fix15 ysize;                           /* height of bitmap */
/* 
 * Called by Speedo character generator to initialize a buffer prior
 * to receiving bitmap data.
 */
{

fix15 xmin,xmax,ymin,ymax;

#if DEBUG
printf("sp_open_bitmap:\n");
printf("    X component of set width vector = %3.1f\n", (real)sw_x / 65536.0);
printf("    Y component of set width vector = %3.1f\n", (real)sw_y / 65536.0);
printf("    Bounding box is (%d, %d, %d, %d)\n", xmin, ymin, xmax, ymax);
#endif

xmin = xorg >> 16;
ymin = yorg >> 16;
xmax = xmin + xsize;
ymax = ymin + ysize;

set_width_x = ((sw_x >> 15) + 1) >> 1;
open_bitmap(set_width_x, xmin, xmax, ymin, ymax);
}

FUNCTION void sp_set_bitmap_bits(y, x1, x2)
fix15 y, x1, x2;
/* 
 * Called by Speedo character generator to write one row of pixels 
 * into the generated bitmap character.                               
 */
{
#if DEBUG
printf("set_bitmap_bits(%d, %d, %d)\n", y, x1, x2);
#endif

set_bitmap_bits(y, x1, x2);
}

FUNCTION void sp_close_bitmap()
/* 
 * Called by Speedo character generator to indicate all bitmap data
 * has been generated.
 */
{
#if DEBUG
printf("close_bitmap()\n");
#endif

close_bitmap();
}

FUNCTION void sp_open_outline(sw_x, sw_y, xmin, xmax, ymin, ymax)
fix31  sw_x;                           /* X component of escapement vector */
fix31  sw_y;                           /* Y component of escapement vector */
fix31  xmin;                           /* Minimum X value in outline */
fix31  xmax;                           /* Maximum X value in outline */
fix31  ymin;                           /* Minimum Y value in outline */
fix31  ymax;                           /* Maximum Y value in outline */
/*
 * Called by Speedo character generator to initialize prior to
 * outputting scaled outline data.
 */
{
#if DEBUG
printf("open_outline(%3.1f, %3.1f, %3.1f, %3.1f, %3.1f, %3.1f)\n",
    (real)sw_x / 65536.0, (real)sw_y / 65536.0,
    (real)xmin / 65536.0, (real)xmax / 65536.0, (real)ymin / 65536.0, (real)ymax / 65536.0);
#endif

set_width_x = ((sw_x >> 15) + 1) >> 1;
open_outline(set_width_x);
}

FUNCTION void sp_start_new_char()
/*
 * Called by Speedo character generator to initialize prior to
 * outputting scaled outline data for a sub-character in a compound
 * character.
 */
{
#if DEBUG
printf("start_new_char()\n");
#endif

start_new_char();
}

FUNCTION void sp_start_contour(x, y, outside)
fix31    x;       /* X coordinate of start point in 1/65536 pixels */
fix31    y;       /* Y coordinate of start point in 1/65536 pixels */
boolean outside;  /* TRUE if curve encloses ink (Counter-clockwise) */
/*
 * Called by Speedo character generator at the start of each contour
 * in the outline data of the character.
 */
{
real realx, realy;

realx = (real)x / 65536.0;
realy = (real)y / 65536.0;

#if DEBUG
printf("start_curve(%3.1f, %3.1f, %s)\n", 
    realx, realy, 
    outside? "outside": "inside");
#endif

start_curve(realx, realy, outside);
}

FUNCTION void sp_curve_to(x1, y1, x2, y2, x3, y3)
fix31 x1;               /* X coordinate of first control point in 1/65536 pixels */
fix31 y1;               /* Y coordinate of first control  point in 1/65536 pixels */
fix31 x2;               /* X coordinate of second control point in 1/65536 pixels */
fix31 y2;               /* Y coordinate of second control point in 1/65536 pixels */
fix31 x3;               /* X coordinate of curve end point in 1/65536 pixels */
fix31 y3;               /* Y coordinate of curve end point in 1/65536 pixels */
/*
 * Called by Speedo character generator once for each curve in the
 * scaled outline data of the character. This function is only called if curve
 * output is enabled in the set_specs() call.
 */
{
#if DEBUG
printf("curve_to(%3.1f, %3.1f, %3.1f, %3.1f, %3.1f, %3.1f)\n", 
    (real)x1 / 65536.0, (real)y1 / 65536.0,
    (real)x2 / 65536.0, (real)y2 / 65536.0,
    (real)x3 / 65536.0, (real)y3 / 65536.0);
#endif
}

FUNCTION void sp_line_to(x, y)
fix31 x;               /* X coordinate of vector end point in 1/65536 pixels */
fix31 y;               /* Y coordinate of vector end point in 1/65536 pixels */
/*
 * Called by Speedo character generator onece for each vector in the
 * scaled outline data for the character. This include curve data that has
 * been sub-divided into vectors if curve output has not been enabled
 * in the set_specs() call.
 */
{
real realx, realy;

realx = (real)x / 65536.0;
realy = (real)y / 65536.0;

#if DEBUG
printf("line_to(%3.1f, %3.1f)\n", 
    realx, realy); 
#endif            

line_to(realx, realy);
}

FUNCTION void sp_close_contour()
/*
 * Called by Speedo character generator at the end of each contour
 * in the outline data of the character.
 */
{
#if DEBUG
printf("close_curve()\n");
#endif

close_curve();
}

FUNCTION void sp_close_outline()
/*
 * Called by Speedo character generator at the end of output of the
 * scaled outline of the character.
 */
{
#if DEBUG
printf("close_outline()\n");
#endif

close_outline(set_width_x);
}