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 >
Wrap
C/C++ Source or Header
|
1992-12-08
|
5KB
|
195 lines
/*
* Copyright (c) 1991 Stanford University
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Stanford not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Stanford makes no representations about
* the suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* PSFigItem
*/
#include "PSFigItem.h"
#include "IdrawImage.h"
#include "TextView.h"
#include <InterViews/border.h>
#include <InterViews/box.h>
#include <InterViews/center.h>
#include <InterViews/fixedspan.h>
#include <InterViews/label.h>
#include <InterViews/margin.h>
#include <InterViews/target.h>
#include <InterViews/tformsetter.h>
#include <InterViews/transformer.h>
#include <InterViews/world.h>
#include <OS/math.h>
#include <strstream.h>
#include <stdio.h>
#include <string.h>
static void read_header (
FILE* file, char* creator,
float& left, float& bottom, float& right, float& top
) {
char line[1000];
boolean done = false;
boolean creator_known = false;
boolean bb_known = false;
int l, b, r, t;
while (!done && fgets(line, 1000, file) != NULL) {
if (sscanf(line, "%%%%Creator: %s", creator) == 1) {
creator_known = true;
} else if (
sscanf(line, "%%%%BoundingBox: %d %d %d %d", &l, &b, &r, &t) == 4)
{
bb_known = true;
} else if (strcmp(line, "%%EndProlog\n") == 0) {
done = true;
}
}
if (!creator_known) {
strcpy(creator, "idraw");
}
if (bb_known) {
left = float(l);
bottom = float(b);
right = float(r);
top = float(t);
} else {
left = 0;
bottom = 0;
right = 50;
top = 50;
}
}
PSFigItem::PSFigItem (const char* file)
{
_parameters = new char[256];
_filename = new char[100];
_creator = new char[100];
_width = 0;
_height = 0;
_hscale = 0;
_vscale = 0;
_hoffset = 0;
_voffset = 0;
strcpy(_filename, file);
create_graphic();
}
PSFigItem::~PSFigItem () {
delete _parameters;
delete _filename;
delete _creator;
}
void PSFigItem::parameters (const char* params) {
strcpy(_parameters, params);
create_graphic();
}
const char* PSFigItem::parameters () {
return _parameters;
}
void PSFigItem::create_graphic ()
{
_width = 0;
_height = 0;
_hscale = 0;
_vscale = 0;
_hoffset = 0;
_voffset = 0;
_rotate = 0;
FILE* file = fopen(_filename, "r");
if (file != nil)
{
read_header(file, _creator, _left, _bottom, _right, _top);
float x1, y1, x2, y2, x3, y3, x4, y4;
Transformer t;
t.rotate(_rotate);
t.transform(_left, _bottom, x1, y1);
t.transform(_left, _top, x2, y2);
t.transform(_right, _top, x3, y3);
t.transform(_right, _bottom, x4, y4);
_left = Math::min(x1, x2, x3, x4);
_right = Math::max(x1, x2, x3, x4);
_bottom = Math::min(y1, y2, y3, y4);
_top = Math::max(y1, y2, y3, y4);
if (_width != 0)
_hscale = _width / (_right - _left);
if (_height != 0)
_vscale = _height / (_top - _bottom);
if (_hscale == 0 && _vscale != 0)
_hscale = _vscale;
if (_vscale == 0 && _hscale != 0)
_vscale = _hscale;
if (_hscale == 0)
_hscale = 1.0;
if (_vscale == 0)
_vscale = 1.0;
_width = (_right - _left) * _hscale;
_height = (_top - _bottom) * _vscale;
}
}
Glyph* PSFigItem::graphic () {
World* w = World::current();
const Font* font = w->font();
const Color* fg = w->foreground();
const Color* bg = w->background();
boolean idraw_font_metrics = true;
Glyph *g;
FILE* file = fopen(_filename, "r");
Transformer t;
if (file != nil)
{
if (strcmp(_creator, "idraw") == 0)
{
g = new IdrawImage(file, idraw_font_metrics);
t.translate(- _left, - _bottom);
}
fclose(file);
}
t.rotate(_rotate);
t.scale(_hscale, _vscale);
g = new Center(new TransformSetter(g, t));
g = new FixedSpan(
new FixedSpan(
g, Dimension_X, _width
), Dimension_Y, _height
);
g->ref();
return g;
}