home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xhyper10.zip / XHyper_v1.0 / src / PSFigItem.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  5KB  |  195 lines

  1. /*
  2.  * Copyright (c) 1991 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * PSFigItem
  25.  */
  26.  
  27. #include "PSFigItem.h"
  28. #include "IdrawImage.h"
  29. #include "TextView.h"
  30.  
  31. #include <InterViews/border.h>
  32. #include <InterViews/box.h>
  33. #include <InterViews/center.h>
  34. #include <InterViews/fixedspan.h>
  35. #include <InterViews/label.h>
  36. #include <InterViews/margin.h>
  37. #include <InterViews/target.h>
  38. #include <InterViews/tformsetter.h>
  39. #include <InterViews/transformer.h>
  40. #include <InterViews/world.h>
  41. #include <OS/math.h>
  42.  
  43. #include <strstream.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. static void read_header (
  48.     FILE* file, char* creator,
  49.     float& left, float& bottom, float& right, float& top
  50. ) {
  51.     char line[1000];
  52.     boolean done = false;
  53.     boolean creator_known = false;
  54.     boolean bb_known = false;
  55.     int l, b, r, t;
  56.     while (!done && fgets(line, 1000, file) != NULL) {
  57.         if (sscanf(line, "%%%%Creator: %s", creator) == 1) {
  58.             creator_known = true;
  59.         } else if (
  60.             sscanf(line, "%%%%BoundingBox: %d %d %d %d", &l, &b, &r, &t) == 4)
  61.         {
  62.             bb_known = true;
  63.         } else if (strcmp(line, "%%EndProlog\n") == 0) {
  64.             done = true;
  65.         }
  66.     }
  67.     if (!creator_known) {
  68.         strcpy(creator, "idraw");
  69.     }
  70.     if (bb_known) {
  71.         left = float(l);
  72.         bottom = float(b);
  73.         right = float(r);
  74.         top = float(t);
  75.     } else {
  76.         left = 0; 
  77.         bottom = 0;
  78.         right = 50;
  79.         top = 50;
  80.     }
  81. }
  82.  
  83. PSFigItem::PSFigItem (const char* file)
  84.     {
  85.     _parameters = new char[256];
  86.     _filename = new char[100];
  87.     _creator = new char[100];
  88.     _width = 0;
  89.     _height = 0;
  90.     _hscale = 0;
  91.     _vscale = 0;
  92.     _hoffset = 0;
  93.     _voffset = 0;
  94.  
  95.     strcpy(_filename, file);
  96.     create_graphic();
  97.     }
  98.  
  99. PSFigItem::~PSFigItem () {
  100.     delete _parameters;
  101.     delete _filename;
  102.     delete _creator;
  103. }
  104.  
  105. void PSFigItem::parameters (const char* params) {
  106.     strcpy(_parameters, params);
  107.     create_graphic();
  108. }
  109.  
  110. const char* PSFigItem::parameters () {
  111.     return _parameters;
  112. }
  113.  
  114. void PSFigItem::create_graphic () 
  115.    {
  116.    _width = 0;
  117.    _height = 0;
  118.    _hscale = 0;
  119.    _vscale = 0;
  120.    _hoffset = 0;
  121.    _voffset = 0;
  122.    _rotate = 0;
  123.  
  124.    FILE* file = fopen(_filename, "r");
  125.    if (file != nil) 
  126.       {
  127.       read_header(file, _creator, _left, _bottom, _right, _top);
  128.       float x1, y1, x2, y2, x3, y3, x4, y4;
  129.       Transformer t;
  130.       t.rotate(_rotate);
  131.       t.transform(_left, _bottom, x1, y1);
  132.       t.transform(_left, _top, x2, y2);
  133.       t.transform(_right, _top, x3, y3);
  134.       t.transform(_right, _bottom, x4, y4);
  135.       _left = Math::min(x1, x2, x3, x4);
  136.       _right = Math::max(x1, x2, x3, x4);
  137.       _bottom = Math::min(y1, y2, y3, y4);
  138.       _top = Math::max(y1, y2, y3, y4);
  139.  
  140.       if (_width != 0) 
  141.            _hscale = _width / (_right - _left);
  142.  
  143.       if (_height != 0) 
  144.            _vscale = _height / (_top - _bottom);
  145.  
  146.       if (_hscale == 0 && _vscale != 0) 
  147.            _hscale = _vscale;
  148.  
  149.       if (_vscale == 0 && _hscale != 0)
  150.            _vscale = _hscale;
  151.  
  152.       if (_hscale == 0)
  153.            _hscale = 1.0;
  154.  
  155.       if (_vscale == 0)
  156.            _vscale = 1.0;
  157.  
  158.        _width = (_right - _left) * _hscale;
  159.        _height = (_top - _bottom) * _vscale;
  160.       }
  161.    }
  162.  
  163. Glyph* PSFigItem::graphic () {
  164.     World* w = World::current();
  165.     const Font* font = w->font();
  166.     const Color* fg = w->foreground();
  167.     const Color* bg = w->background();
  168.     boolean idraw_font_metrics = true;
  169.  
  170.     Glyph *g;
  171.     FILE* file = fopen(_filename, "r");
  172.     Transformer t;
  173.     if (file != nil) 
  174.        {
  175.        if (strcmp(_creator, "idraw") == 0) 
  176.           {
  177.           g = new IdrawImage(file, idraw_font_metrics);
  178.           t.translate(- _left, - _bottom);
  179.           }
  180.        fclose(file);
  181.        }
  182.     t.rotate(_rotate);
  183.     t.scale(_hscale, _vscale);
  184.     g = new Center(new TransformSetter(g, t));
  185.  
  186.     g = new FixedSpan(
  187.             new FixedSpan(
  188.                 g, Dimension_X, _width
  189.                 ), Dimension_Y, _height
  190.             );
  191.     g->ref();
  192.     return g;
  193.     }
  194.  
  195.