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
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "swindow/SGPrimitive.h"
#include "stoolkit/SString.h"
#include "stoolkit/STypes.h"
#define SS_SHORT_KEY 0
/**
* This is the graphic primitives.
* This is what happens between a SPen'ed newpath and stroke or fill.
*/
SGPrimitive::SGPrimitive (void)
{
type = NONE;
}
SGPrimitive::SGPrimitive (const SGPrimitive& p)
{
type = p.type;
for (unsigned int i=0; i<MAX_SG_PARAMS; i++)
{
params[i] = p.params[i];
}
}
SGPrimitive
SGPrimitive::operator = (const SGPrimitive& p)
{
type = p.type;
for (unsigned int i=0; i<MAX_SG_PARAMS; i++)
{
params[i] = p.params[i];
}
return *this;
}
SGPrimitive::~SGPrimitive ()
{
}
SObject*
SGPrimitive::clone() const
{
SGPrimitive* p = new SGPrimitive();
CHECK_NEW (p);
p->type = type;
for (unsigned int i=0; i<MAX_SG_PARAMS; i++)
{
p->params[i] = params[i];
}
return p;
}
/**
* return the keys, that is all the drawing parameters
* moved to the origin
*/
SString
SGPrimitive::getKey (double originx, double originy) const
{
char arr[256];
arr[0] = 0;
#if SS_SHORT_KEY
int s[MAX_SG_PARAMS+1];
unsigned int sLen=0;
#endif
switch (type)
{
case CURVETO:
#if SS_SHORT_KEY
sLen = 7;
s[0] = (int) type;
s[1] = (int) (params[0] - originx); s[2] = (int) (params[1] - originy);
s[3] = (int) (params[2] - originx); s[4] = (int) (params[3] - originy);
s[5] = (int) (params[4] - originx); s[6] = (int) (params[5] - originy);
#else
snprintf (arr, 256, "%d=%g,%g,%g,%g,%g,%g;", (int) type,
params[0] - originx, params[1] - originy,
params[2] - originx, params[3] - originy,
params[4] - originx, params[5] - originy);
#endif
break;
case TRANSLATE:
#if SS_SHORT_KEY
sLen = 3;
s[0] = (int) type;
s[1] = (int) (params[0]); s[2] = (int) (params[1]);
#else
snprintf (arr, 256, "%d=%g,%g;", (int) type,
params[0], params[1]);
#endif
break;
case LINETO:
case MOVETO:
#if SS_SHORT_KEY
sLen = 3;
s[0] = (int) type;
s[1] = (int) (params[0] - originx); s[2] = (int) (params[1] - originy);
#else
snprintf (arr, 256, "%d=%g,%g;", (int) type,
params[0] - originx, params[1] - originy);
#endif
break;
case CLOSEPATH:
case PUSHMATRIX:
case POPMATRIX:
case STROKE:
case FILL:
case SCALE:
case NEWPATH:
#if SS_SHORT_KEY
sLen = 1;
s[0] = (int) type;
#else
snprintf (arr, 256, "%d;", (int) type);
#endif
break;
/*
* TODO: BUG: totate probably will not work because it can
* not be tied to origin
*/
case ROTATE:
#if SS_SHORT_KEY
sLen = 2;
s[0] = (int) type;
s[1] = (int) (params[0]);
#else
snprintf (arr, 256, "%d=%g;", (int) type, params[0]);
#endif
break;
default:
break;
}
#if SS_SHORT_KEY
#ifdef GASPAR
SString a((char*) s, sLen * sizeof (int));
fprintf (stderr, ">");
for (unsigned int i=0; i<a.size(); i++)
{
fprintf (stderr, "0x%02x ", (int)(unsigned char)a[i]);
}
fprintf (stderr, "\n");
#endif
return SString((char*) s, sLen * sizeof (int));
#else
return SString(arr);
#endif
}
double
SGPrimitive::getOriginX (double originx)
{
switch (type)
{
case MOVETO:
case LINETO:
if (params[0] < originx) return params[0];
break;
case CURVETO:
if (params[0] < originx) return params[0];
if (params[2] < originx) return params[2];
if (params[4] < originx) return params[4];
break;
break;
default:
break;
}
return originx;
}
double
SGPrimitive::getOriginY (double originy)
{
switch (type)
{
case MOVETO:
case LINETO:
if (params[1] < originy) return params[1];
break;
case CURVETO:
if (params[1] < originy) return params[1];
if (params[3] < originy) return params[3];
if (params[5] < originy) return params[5];
break;
break;
default:
break;
}
return originy;
}
void
SGPrimitive::newpath ()
{
type = NEWPATH;
}
void
SGPrimitive::moveto (double x, double y)
{
type = MOVETO;
params[0] = x;
params[1] = y;
}
void
SGPrimitive::lineto (double x, double y)
{
type = LINETO;
params[0] = x;
params[1] = y;
}
void
SGPrimitive::stroke (int x, int y, unsigned int w, unsigned int h)
{
type = STROKE;
params[0] = (double) w;
params[1] = (double) h;
}
void
SGPrimitive::fill (int x, int y, unsigned int w, unsigned int h)
{
type = FILL;
params[0] = (double) w;
params[1] = (double) h;
}
void
SGPrimitive::curveto (double x0, double y0,
double x1, double y1, double x2, double y2)
{
type = CURVETO;
params[0] = x0;
params[1] = y0;
params[2] = x1;
params[3] = y1;
params[4] = x2;
params[5] = y2;
}
void
SGPrimitive::closepath()
{
type = CLOSEPATH;
}
void
SGPrimitive::pushmatrix()
{
type = PUSHMATRIX;
}
void
SGPrimitive::popmatrix()
{
type = POPMATRIX;
}
void
SGPrimitive::scale (double x, double y)
{
type = SCALE;
params[0] = x;
params[1] = y;
}
void
SGPrimitive::translate (double x, double y)
{
type = TRANSLATE;
params[0] = x;
params[1] = y;
}
void
SGPrimitive::rotate (double angle)
{
type = ROTATE;
params[0] = angle;
}
|