home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / g / gs241j11.zip / ZKFPC98.C < prev    next >
C/C++ Source or Header  |  1992-05-09  |  4KB  |  172 lines

  1. /*
  2.  * zkfpc98.c --- Kanji font operator for PC98 ROM
  3.  *
  4.  * Copyright (C) 1991 Norio Katayama.
  5.  * Sep. 28, 1991 Programmed by N.Katayama (katayama@nacsis.ac.jp)
  6.  */
  7.  
  8. #include <ctype.h>
  9. #include <dos.h>
  10. #include <pc.h>
  11. #include "memory_.h"
  12. #include "ghost.h"
  13. #include "oper.h"
  14. #include "errors.h"
  15. #include "gsmatrix.h"
  16. #include "state.h"
  17. #include "store.h"
  18. #include "alloc.h"
  19.  
  20. #define B_X_CL    500    /* horizontal center in BuildChar */
  21. #define B_Y_CL    400    /* vertical center in BuildChar */
  22.  
  23. /* Imported procedures */
  24.  
  25. extern int kf_is_vchar(P1(int));
  26. extern int kf_vmatrix(P5(int, floatp, floatp, gs_rect *, gs_matrix *));
  27. extern int gs_imagebbox(P6(int width, int height, int bool, 
  28.                gs_matrix *pmat, byte *image, gs_rect *));
  29.  
  30. /* Forward declaration */
  31.  
  32. private int pc98image(P8(int, int *, int *, byte *, int,
  33.              gs_matrix *, floatp *, floatp *));
  34.  
  35. /*
  36.  * zkfpc98
  37.  */
  38.  
  39. int
  40. zkfpc98(register os_ptr op)
  41. {
  42.     int code, width, height, length;
  43.     int jis_code, wmode;
  44.     byte *bitmap;
  45.     floatp w0x, w0y;
  46.     gs_matrix *pmat, vmat, imat;
  47.     gs_rect bbox;
  48.  
  49.     check_type(op[-3], t_integer);        /* JIS Code */
  50.     check_type(op[-2], t_integer);        /* WMode */
  51.     if((code = write_matrix(op - 1)) < 0)    /* ImageMatrix */
  52.         return code;
  53.     check_type(op[0], t_string);        /* ImageString */
  54.  
  55.     jis_code = op[-3].value.intval;
  56.     wmode = op[-2].value.intval;
  57.  
  58.     bitmap = op[0].value.bytes;
  59.     length = r_size(op);
  60.     pmat = (gs_matrix *)op[-1].value.refs;
  61.  
  62.     if((code = pc98image(jis_code,
  63.                  &width, &height, bitmap, length,
  64.                  pmat, &w0x, &w0y)) < 0)
  65.         return code;
  66.  
  67.     if((code = gs_imagebbox(width, height, 1, pmat, bitmap, &bbox)) < 0)
  68.         return code;
  69.  
  70.     if(wmode && kf_is_vchar(jis_code)) {
  71.         kf_vmatrix(jis_code,
  72.                (floatp)B_X_CL, (floatp)B_Y_CL, &bbox, &vmat);
  73.         gs_matrix_invert(&vmat, &imat);
  74.         gs_matrix_multiply(&imat, pmat, pmat);
  75.     }
  76.  
  77.     /* Push results */
  78.     /* w0x w0y llx lly urx ury width height bool matrix bitmap */
  79.     push(7);
  80.     make_real(op - 10, w0x);
  81.     make_real(op - 9, w0y);
  82.     make_real(op - 8, bbox.p.x);
  83.     make_real(op - 7, bbox.p.y);
  84.     make_real(op - 6, bbox.q.x);
  85.     make_real(op - 5, bbox.q.y);
  86.     make_int(op - 4, width);
  87.     make_int(op - 3, height);
  88.     make_bool(op - 2, 1);
  89.     make_tasv(op - 1, t_array, a_all, 6, refs, (ref *)pmat);
  90.     make_tasv(op, t_string, a_all, length, bytes, bitmap);
  91.     return 0;
  92. }
  93.  
  94. /* -------- Initialization procedure -------- */
  95.  
  96. op_def zkfpc98_op_defs[] = {
  97.     {"4kfpc98", zkfpc98},
  98.     op_def_end(0)
  99. };
  100.  
  101. /* -------- Internal routines -------- */
  102.  
  103. /*
  104.  * Read Image from the Font ROM
  105.  */
  106.  
  107. private int
  108. read_font_rom(byte *bitmap, int jis_code)
  109. {
  110.     int y;
  111.     
  112.     outportb(0x68, 0x0B);
  113.     outportb(0xA3, (jis_code >> 8) - 0x20);
  114.     outportb(0xA1, jis_code & 0xFF);
  115.     for(y=0; y<16; y++) {
  116.         outportb(0xA5, 0x20 + y);
  117.         bitmap[y*2] = inportb(0xA9);
  118.         outportb(0xA5, y);
  119.         bitmap[y*2 + 1] = inportb(0xA9);
  120.     }
  121.     outportb(0x68, 0x0A);
  122.     
  123.     return 0;
  124. }
  125.  
  126. /*
  127.  * Retrieve Font Image
  128.  */
  129.  
  130. private int
  131. pc98image(int jis_code,
  132.       int *width, int *height, byte *bitmap, int length,
  133.       gs_matrix *pmat, floatp *wx, floatp *wy)
  134. {
  135.     int num_bytes;
  136.     int pixel_size, font_ascent, font_descent;
  137.     floatp dx, dy, scale;
  138.     gs_matrix smat, tmat;
  139.  
  140.     /* Read Font Bitmap */
  141.  
  142.     pixel_size = *width = *height = 16;
  143.     font_ascent = 14;
  144.     font_descent = 2;
  145.  
  146.     num_bytes = pixel_size / 8 * pixel_size;
  147.     if(num_bytes > length)
  148.         return e_rangecheck;
  149.  
  150.     read_font_rom(bitmap, jis_code);
  151.  
  152.     /* Transform Image */
  153.  
  154.     gs_make_identity(pmat);
  155.  
  156.     scale = (floatp)B_Y_CL * 2.0 / (floatp)font_ascent;
  157.     gs_make_scaling(1.0 / scale, -1.0 / scale, &smat);
  158.  
  159.     dx = (floatp)pixel_size / 2.0 - (floatp)B_X_CL / scale;
  160.     dy = (floatp)font_ascent;
  161.     gs_make_translation(dx, dy, &tmat);
  162.  
  163.     gs_matrix_multiply(pmat, &smat, pmat);
  164.     gs_matrix_multiply(pmat, &tmat, pmat);
  165.  
  166.     *wx = (floatp)B_X_CL * 2.0;
  167.     *wy = 0;
  168.  
  169.     return 0;
  170. }
  171.  
  172.