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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
|
// Copyright (c) 1996 James Clark
// See the file copying.txt for copying permission.
#include "stylelib.h"
#include "Style.h"
#include "VM.h"
#include "Interpreter.h"
#include "InterpreterMessages.h"
#include "SosofoObj.h"
#include <OpenSP/macros.h>
#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif
StyleStack::StyleStack()
: level_(0)
{
}
void StyleStack::pushContinue(StyleObj *style,
const ProcessingMode::Rule *rule,
const NodePtr &nodePtr,
Messenger *mgr)
{
StyleObjIter iter;
style->appendIter(iter);
for (;;) {
const VarStyleObj *varStyle;
ConstPtr<InheritedC> spec(iter.next(varStyle));
if (spec.isNull())
break;
size_t ind = spec->index();
if (ind >= inheritedCInfo_.size())
inheritedCInfo_.resize(ind + 1);
Ptr<InheritedCInfo> &info = inheritedCInfo_[ind];
if (!info.isNull() && info->valLevel == level_) {
if (rule) {
ASSERT(info->rule != 0);
if (rule->compareSpecificity(*info->rule) == 0) {
mgr->setNextLocation(info->rule->location());
mgr->message(InterpreterMessages::ambiguousStyle,
StringMessageArg(info->spec->identifier()->name()),
rule->location());
}
}
}
else {
popList_->list.push_back(ind);
info = new InheritedCInfo(spec, varStyle, level_, level_, rule, info);
}
}
}
void StyleStack::pushEnd(VM &vm, FOTBuilder &fotb)
{
const PopList *oldPopList = popList_->prev.pointer();
if (oldPopList) {
for (size_t i = 0; i < oldPopList->dependingList.size(); i++) {
size_t d = oldPopList->dependingList[i];
// d is the index of a characteristic that depends on the actual
// value of another characteritistic
if (inheritedCInfo_[d]->valLevel != level_) {
const Vector<size_t> &dependencies = inheritedCInfo_[d]->dependencies;
bool changed = 0;
for (size_t j = 0; j < dependencies.size(); j++) {
const InheritedCInfo *p = inheritedCInfo_[dependencies[j]].pointer();
if (p && p->valLevel == level_) {
inheritedCInfo_[d] = new InheritedCInfo(inheritedCInfo_[d]->spec,
inheritedCInfo_[d]->style,
level_,
inheritedCInfo_[d]->specLevel,
inheritedCInfo_[d]->rule,
inheritedCInfo_[d]);
popList_->list.push_back(d);
changed = 1;
break;
}
}
// If it changed, then doing set() on the new value will add
// it to the dependingList for this level.
if (!changed)
popList_->dependingList.push_back(d);
}
}
}
vm.styleStack = this;
for (size_t i = 0; i < popList_->list.size(); i++) {
InheritedCInfo &info = *inheritedCInfo_[popList_->list[i]];
vm.specLevel = info.specLevel;
info.spec->set(vm, info.style, fotb, info.cachedValue, info.dependencies);
if (info.dependencies.size())
popList_->dependingList.push_back(popList_->list[i]);
}
vm.styleStack = 0;
}
void StyleStack::pop()
{
for (size_t i = 0; i < popList_->list.size(); i++) {
size_t ind = popList_->list[i];
ASSERT(inheritedCInfo_[ind]->valLevel == level_);
Ptr<InheritedCInfo> tem(inheritedCInfo_[ind]->prev);
inheritedCInfo_[ind] = tem;
}
level_--;
Ptr<PopList> tem(popList_->prev);
popList_ = tem;
}
ELObj *StyleStack::inherited(const ConstPtr<InheritedC> &ic, unsigned specLevel,
Interpreter &interp, Vector<size_t> &dependencies)
{
ASSERT(specLevel != unsigned(-1));
size_t ind = ic->index();
ConstPtr<InheritedC> spec;
const VarStyleObj *style = 0;
unsigned newSpecLevel = unsigned(-1);
if (ind >= inheritedCInfo_.size())
spec = ic;
else {
const InheritedCInfo *p = inheritedCInfo_[ind].pointer();
while (p != 0) {
if (p->specLevel < specLevel)
break;
p = p->prev.pointer();
}
if (!p)
spec = ic;
else {
if (p->cachedValue) {
// We can only use the cached value if none of the values
// we depended on changed since we computed it.
bool cacheOk = 1;
for (size_t i = 0; i < p->dependencies.size(); i++) {
size_t d = p->dependencies[i];
if (d < inheritedCInfo_.size()
&& inheritedCInfo_[d]->valLevel > p->valLevel) {
cacheOk = 0;
break;
}
}
if (cacheOk)
return p->cachedValue;
}
style = p->style;
spec = p->spec;
newSpecLevel = p->specLevel;
}
}
VM vm(interp);
vm.styleStack = this;
vm.specLevel = newSpecLevel;
return spec->value(vm, style, dependencies);
}
ELObj *StyleStack::actual(const ConstPtr<InheritedC> &ic, const Location &loc,
Interpreter &interp, Vector<size_t> &dependencies)
{
size_t ind = ic->index();
for (size_t i = 0; i < dependencies.size(); i++) {
if (dependencies[i] == ind) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::actualLoop,
StringMessageArg(ic->identifier()->name()));
return interp.makeError();
}
}
dependencies.push_back(ind);
ConstPtr<InheritedC> spec;
const VarStyleObj *style = 0;
if (ind >= inheritedCInfo_.size())
spec = ic;
else {
const InheritedCInfo *p = inheritedCInfo_[ind].pointer();
if (!p)
spec = ic;
else if (p->cachedValue) {
const Vector<size_t> &dep = p->dependencies;
for (size_t i = 0; i < dep.size(); i++)
dependencies.push_back(dep[i]);
return p->cachedValue;
}
else {
style = p->style;
spec = p->spec;
}
}
VM vm(interp);
vm.styleStack = this;
vm.specLevel = level_;
return spec->value(vm, style, dependencies);
}
void StyleStack::trace(Collector &c) const
{
for (size_t i = 0; i < inheritedCInfo_.size(); i++) {
for (const InheritedCInfo *p = inheritedCInfo_[i].pointer();
p;
p = p->prev.pointer()) {
c.trace(p->style);
c.trace(p->cachedValue);
}
}
}
InheritedCInfo::InheritedCInfo(const ConstPtr<InheritedC> &sp,
const VarStyleObj *so,
unsigned vl,
unsigned sl,
const ProcessingMode::Rule *r,
const Ptr<InheritedCInfo> &p)
: spec(sp), style(so), valLevel(vl), specLevel(sl), rule(r), prev(p), cachedValue(0)
{
}
StyleObj *StyleObj::asStyle()
{
return this;
}
VarStyleObj::VarStyleObj(const ConstPtr<StyleSpec> &styleSpec, StyleObj *use, ELObj **display,
const NodePtr &node)
: styleSpec_(styleSpec), use_(use), display_(display), node_(node)
{
hasSubObjects_ = 1;
}
VarStyleObj::~VarStyleObj()
{
delete [] display_;
}
void VarStyleObj::traceSubObjects(Collector &c) const
{
c.trace(use_);
if (display_)
for (ELObj **pp = display_; *pp; pp++)
c.trace(*pp);
}
void VarStyleObj::appendIterForce(StyleObjIter &iter) const
{
if (styleSpec_->forceSpecs.size())
iter.append(&styleSpec_->forceSpecs, this);
}
void VarStyleObj::appendIterNormal(StyleObjIter &iter) const
{
if (styleSpec_->specs.size())
iter.append(&styleSpec_->specs, this);
if (use_)
use_->appendIter(iter);
}
void VarStyleObj::appendIter(StyleObjIter &iter) const
{
VarStyleObj::appendIterForce(iter);
VarStyleObj::appendIterNormal(iter);
}
OverriddenStyleObj::OverriddenStyleObj(BasicStyleObj *basic, StyleObj *override)
: basic_(basic), override_(override)
{
hasSubObjects_ = 1;
}
void OverriddenStyleObj::traceSubObjects(Collector &c) const
{
c.trace(basic_);
c.trace(override_);
}
void OverriddenStyleObj::appendIter(StyleObjIter &iter) const
{
basic_->appendIterForce(iter);
override_->appendIter(iter);
basic_->appendIterNormal(iter);
}
MergeStyleObj::MergeStyleObj()
{
hasSubObjects_ = 1;
}
void MergeStyleObj::append(StyleObj *obj)
{
styles_.push_back(obj);
}
void MergeStyleObj::appendIter(StyleObjIter &iter) const
{
for (size_t i = 0; i < styles_.size(); i++)
styles_[i]->appendIter(iter);
}
void MergeStyleObj::traceSubObjects(Collector &c) const
{
for (size_t i = 0; i < styles_.size(); i++)
c.trace(styles_[i]);
}
ColorObj *ColorObj::asColor()
{
return this;
}
DeviceRGBColorObj::DeviceRGBColorObj(unsigned char red, unsigned char green,
unsigned char blue)
{
color_.red = red;
color_.green = green;
color_.blue = blue;
}
void DeviceRGBColorObj::set(FOTBuilder &fotb) const
{
fotb.setColor(color_);
}
void DeviceRGBColorObj::setBackground(FOTBuilder &fotb) const
{
fotb.setBackgroundColor(color_);
}
ColorSpaceObj *ColorSpaceObj::asColorSpace()
{
return this;
}
// invert a 3x3 matrix A. result is returned in B
// both must be arrays of length 9.
static void
invert(double *A, double *B)
{
B[0] = (A[4]*A[8] - A[5]*A[7]);
B[3] = - (A[3]*A[8] - A[5]*A[6]);
B[6] = (A[3]*A[7] - A[4]*A[6]);
B[1] = - (A[1]*A[8] - A[2]*A[7]);
B[4] = (A[0]*A[8] - A[2]*A[6]);
B[7] = - (A[0]*A[7] - A[1]*A[6]);
B[2] = (A[1]*A[5] - A[2]*A[4]);
B[5] = - (A[0]*A[5] - A[2]*A[3]);
B[8] = (A[0]*A[4] - A[1]*A[3]);
double det = A[0]*B[0] + A[1]*B[3] + A[2]*B[6];
if (det < 0.0001) {
//FIXME message
}
B[0] /= det; B[1] /= det; B[2] /= det;
B[3] /= det; B[4] /= det; B[5] /= det;
B[6] /= det; B[7] /= det; B[8] /= det;
}
/*
FIXME make color handling more flexible:
* pass different color types to backends
* move the conversion code to a separate
class ColorConverter, which could then
be used by backends to do the needed
conversions.
* make phosphors settable
* make highlight color for KX settable
* whitepoint correction, making the device
whitepoint/blackpoint settable
for the formulas used here, see:
* Computer Graphics, Principles and Practice, Second Edition,
Foley, van Damme, Hughes,
Addison-Wesley, 1987
* Principles of Color Technology, Second Edition,
Fred W. Billmeyer, Jr. and Max Saltzman,
John Wiley & Sons, Inc., 1981
* The color faq
*/
CIEXYZColorSpaceObj::CIEXYZColorSpaceObj(const double *wp, const double *bp)
{
xyzData_ = new XYZData;
for (int i = 0; i < 3; i++)
xyzData_->white_[i] = wp[i];
double tmp = wp[0] + 15*wp[1] + 3*wp[2];
xyzData_->white_u = 4*wp[0]/tmp;
xyzData_->white_v = 9*wp[1]/tmp;
// from the color faq
double xr = .64; double yr = .33;
double xg = .30; double yg = .60;
double xb = .15; double yb = .06;
double U[9];
U[0] = xr; U[1] = xg; U[2] = xb;
U[3] = yr; U[4] = yg; U[5] = yb;
U[6] = 1.0 - xr - yr; U[7] = 1.0 - xg - yg; U[8] = 1.0 - xb - yb;
double Uinv[9];
invert(U, Uinv);
double C[3];
for (int i = 0; i < 3; i++)
C[i] = Uinv[3*i]*wp[0] + Uinv[3*i+1]*wp[1] + Uinv[3*i+2]*wp[2];
double Minv[9];
Minv[0] = U[0]*C[0]; Minv[1] = U[1]*C[1]; Minv[2] = U[2]*C[2];
Minv[3] = U[3]*C[0]; Minv[4] = U[4]*C[1]; Minv[5] = U[5]*C[2];
Minv[6] = U[6]*C[0]; Minv[7] = U[7]*C[1]; Minv[8] = U[8]*C[2];
invert(Minv, xyzData_->M_);
}
CIEXYZColorSpaceObj::~CIEXYZColorSpaceObj()
{
delete xyzData_;
}
ELObj *CIEXYZColorSpaceObj::makeColor (const double *h, Interpreter &interp)
{
unsigned char c[3];
for (int i = 0; i < 3; i++)
c[i] = (unsigned char) ((xyzData_->M_[3*i]*h[0]
+ xyzData_->M_[3*i+1]*h[1]
+ xyzData_->M_[3*i+2]*h[2])*255.0 + .5);
return new (interp) DeviceRGBColorObj(c[0], c[1], c[2]);
}
CIELUVColorSpaceObj::CIELUVColorSpaceObj(const double *wp, const double *bp, const double *r)
: CIEXYZColorSpaceObj(wp, bp)
{
luvData_ = new LUVData;
for (int i = 0; i < 6; i++)
luvData_->range_[i] = r ? r[i] : ((i % 2) ? 1.0 : .0);
}
CIELUVColorSpaceObj::~CIELUVColorSpaceObj()
{
delete luvData_;
}
ELObj *CIELUVColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 3) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("CIE LUV")));
return interp.makeError();
}
double d[3];
for (int i = 0; i < 3; i++) {
if (!argv[i]->realValue(d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("CIE LUV")));
return interp.makeError();
}
if (d[i] < luvData_->range_[2*i] || d[i] > luvData_->range_[2*i+1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE LUV")));
return interp.makeError();
}
}
double h[3];
if (d[0] == 0.0) {
h[0] = h[1] = h[2] = 0.0;
} else {
if (d[0] <= 7.996968)
h[1] = d[0] / 903.0;
else {
h[1] = (d[0] + 16.0) / 116.0;
h[1] = h[1] * h[1] * h[1];
}
double uu = d[1] / (13.0 * d[0]) + xyzData_->white_u;
double vv = d[2] / (13.0 * d[0]) + xyzData_->white_v;
double tmp = 9.0 * h[1] / vv;
h[0] = uu * tmp / 4.0;
h[2] = (tmp - 15.0 * h[1] - h[0]) / 3.0;
}
return CIEXYZColorSpaceObj::makeColor(h, interp);
}
CIELABColorSpaceObj::CIELABColorSpaceObj(const double *wp, const double *bp, const double *r)
: CIEXYZColorSpaceObj(wp, bp)
{
labData_ = new LABData;
if (r)
for (int i = 0; i < 6; i++)
labData_->range_[i] = r[i];
else {
labData_->range_[0] = .0;
labData_->range_[1] = 100.0;
labData_->range_[2] = .0;
labData_->range_[3] = 1.0;
labData_->range_[4] = .0;
labData_->range_[5] = 1.0;
}
}
CIELABColorSpaceObj::~CIELABColorSpaceObj()
{
delete labData_;
}
ELObj *CIELABColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 3) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("CIE LAB")));
return interp.makeError();
}
double d[3];
for (int i = 0; i < 3; i++) {
if (!argv[i]->realValue(d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("CIE LAB")));
return interp.makeError();
}
if (d[i] < labData_->range_[2*i] || d[i] > labData_->range_[2*i+1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE LAB")));
return interp.makeError();
}
}
d[0] /= 100.0;
double h[3];
double tmp = (d[0] + 16.0) / 116.0;
h[1] = tmp * tmp * tmp;
if (h[1] < 0.008856) {
tmp = d[0] / 9.03292;
h[0] = xyzData_->white_[0] * ((d[1] / 3893.5) + tmp);
h[1] = tmp;
h[2] = xyzData_->white_[2] * (tmp - (d[2] / 1557.4));
} else {
double tmp2 = tmp + (d[1] / 5.0);
h[0] = xyzData_->white_[0] * tmp2 * tmp2 * tmp2;
tmp2 = tmp - (d[2] / 2.0);
h[2] = xyzData_->white_[2] * tmp2 * tmp2 * tmp2;
}
return CIEXYZColorSpaceObj::makeColor(h, interp);
}
CIEABCColorSpaceObj::CIEABCColorSpaceObj(const double *wp, const double *bp,
const double *rabc, FunctionObj **dabc, const double *mabc,
const double *rlmn, FunctionObj **dlmn, const double *mlmn)
: CIEXYZColorSpaceObj(wp, bp)
{
abcData_ = new ABCData;
int i;
for (i = 0; i < 6; i++)
abcData_->rangeAbc_[i] = rabc ? rabc[i] : ((i % 2) ? 1.0 : 0.0);
for (i = 0; i < 3; i++)
abcData_->decodeAbc_[i] = dabc ? dabc[i] : 0;
for (i = 0; i < 9; i++)
abcData_->matrixAbc_[i] = mabc ? mabc[i] : ((i % 4) ? 0.0 : 1.0);
for (i = 0; i < 6; i++)
abcData_->rangeLmn_[i] = rlmn ? rlmn[i] : ((i % 2) ? 1.0 : 0.0);
for (i = 0; i < 3; i++)
abcData_->decodeLmn_[i] = dlmn ? dlmn[i] : 0;
for (i = 0; i < 9; i++)
abcData_->matrixLmn_[i] = mlmn ? mlmn[i] : ((i % 4) ? 0.0 : 1.0);
}
CIEABCColorSpaceObj::~CIEABCColorSpaceObj()
{
delete abcData_;
}
void CIEABCColorSpaceObj::traceSubObjects(Collector &c) const
{
for (int i = 0; i < 3; i++)
if (abcData_->decodeAbc_[i])
c.trace(abcData_->decodeAbc_[i]);
for (int i = 0; i < 3; i++)
if (abcData_->decodeLmn_[i])
c.trace(abcData_->decodeLmn_[i]);
}
static
bool applyFunc(Interpreter &interp, FunctionObj *f, double &d)
{
InsnPtr insns[2];
insns[1] = f->makeCallInsn(1, interp, Location(), InsnPtr());
insns[0] = InsnPtr(new ConstantInsn(new (interp) RealObj(d),insns[1]));
VM vm(interp);
ELObj *res = vm.eval(insns[0].pointer());
if (!res || !res->realValue(d))
return 0;
return 1;
}
ELObj *CIEABCColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 3) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
double d[3];
for (int i = 0; i < 3; i++) {
if (!argv[i]->realValue(d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
if (d[i] < abcData_->rangeAbc_[2*i] || d[i] > abcData_->rangeAbc_[2*i+1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
if (abcData_->decodeAbc_[i] && !applyFunc(interp, abcData_->decodeAbc_[i], d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorProcResType,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
}
double l[3];
for (int i = 0; i < 3; i++) {
l[i] = abcData_->matrixAbc_[i]*d[0]
+ abcData_->matrixAbc_[3+i]*d[1]
+ abcData_->matrixAbc_[6+i]*d[2];
if (l[i] < abcData_->rangeLmn_[2*i] || l[i] > abcData_->rangeLmn_[2*i+1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
if (abcData_->decodeLmn_[i] && !applyFunc(interp, abcData_->decodeLmn_[i], l[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorProcResType,
StringMessageArg(interp.makeStringC("CIE Based ABC")));
return interp.makeError();
}
}
double h[3];
for (int i = 0; i < 3; i++)
h[i] = abcData_->matrixLmn_[i]*l[0]
+ abcData_->matrixLmn_[3+i]*l[1]
+ abcData_->matrixLmn_[6+i]*l[2];
return CIEXYZColorSpaceObj::makeColor(h, interp);
}
CIEAColorSpaceObj::CIEAColorSpaceObj(const double *wp, const double *bp,
const double *ra, FunctionObj *da, const double *ma,
const double *rlmn, FunctionObj **dlmn, const double *mlmn)
: CIEXYZColorSpaceObj(wp, bp)
{
aData_ = new AData;
int i;
for (i = 0; i < 2; i++)
aData_->rangeA_[i] = ra ? ra[i] : ((i % 2) ? 1.0 : 0.0);
aData_->decodeA_ = da ? da : 0;
for (i = 0; i < 3; i++)
aData_->matrixA_[i] = ma ? ma[i] : 1.0;
for (i = 0; i < 6; i++)
aData_->rangeLmn_[i] = rlmn ? rlmn[i] : ((i % 2) ? 1.0 : 0.0);
for (i = 0; i < 3; i++)
aData_->decodeLmn_[i] = dlmn ? dlmn[i] : 0;
for (i = 0; i < 9; i++)
aData_->matrixLmn_[i] = mlmn ? mlmn[i] : ((i % 4) ? 0.0 : 1.0);
}
CIEAColorSpaceObj::~CIEAColorSpaceObj()
{
delete aData_;
}
void CIEAColorSpaceObj::traceSubObjects(Collector &c) const
{
if (aData_->decodeA_)
c.trace(aData_->decodeA_);
for (int i = 0; i < 3; i++)
if (aData_->decodeLmn_[i])
c.trace(aData_->decodeLmn_[i]);
}
ELObj *CIEAColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 1) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
double d;
if (!argv[0]->realValue(d)) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
if (d < aData_->rangeA_[0] || d > aData_->rangeA_[1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
if (aData_->decodeA_ && !applyFunc(interp, aData_->decodeA_, d)) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorProcResType,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
double l[3];
for (int i = 0; i < 3; i++) {
l[i] = aData_->matrixA_[i]*d;
if (l[i] < aData_->rangeLmn_[2*i] || l[i] > aData_->rangeLmn_[2*i+1]) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
if (aData_->decodeLmn_[i] && !applyFunc(interp, aData_->decodeLmn_[i], l[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorProcResType,
StringMessageArg(interp.makeStringC("CIE Based A")));
return interp.makeError();
}
}
double h[3];
for (int i = 0; i < 3; i++) {
h[i] = aData_->matrixLmn_[i]*l[0]
+ aData_->matrixLmn_[3+i]*l[1]
+ aData_->matrixLmn_[6+i]*l[2];
}
return CIEXYZColorSpaceObj::makeColor(h, interp);
}
ELObj *DeviceRGBColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 3) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("Device RGB")));
return interp.makeError();
}
unsigned char c[3];
for (int i = 0; i < 3; i++) {
double d;
if (!argv[i]->realValue(d)) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("Device RGB")));
return interp.makeError();
}
if (d < 0.0 || d > 1.0) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("Device RGB")));
return interp.makeError();
}
c[i] = (unsigned char)(d*255.0 + .5);
}
return new (interp) DeviceRGBColorObj(c[0], c[1], c[2]);
}
ELObj *DeviceGrayColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 1) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("Device Gray")));
return interp.makeError();
}
unsigned char c;
double d;
if (!argv[0]->realValue(d)) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("Device Gray")));
return interp.makeError();
}
if (d < 0.0 || d > 1.0) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("Device Gray")));
return interp.makeError();
}
c = (unsigned char)(d*255.0 + .5);
return new (interp) DeviceRGBColorObj(c, c, c);
}
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
ELObj *DeviceCMYKColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 4) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("Device CMYK")));
return interp.makeError();
}
double d[4];
for (int i = 0; i < 4; i++) {
if (!argv[i]->realValue(d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("Device CMYK")));
return interp.makeError();
}
if (d[i] < 0.0 || d[i] > 1.0) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("Device CMYK")));
return interp.makeError();
}
}
unsigned char c[3];
for (int i = 0; i < 3; i++)
c[i] = (unsigned char)((1 - MIN(1, d[i] + d[3]))*255.0 + .5);
return new (interp) DeviceRGBColorObj(c[0], c[1], c[2]);
}
ELObj *DeviceKXColorSpaceObj::makeColor(int argc, ELObj **argv,
Interpreter &interp, const Location &loc)
{
if (argc == 0)
return new (interp) DeviceRGBColorObj(0, 0, 0);
if (argc != 2) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgCount,
StringMessageArg(interp.makeStringC("Device KX")));
return interp.makeError();
}
double d[2];
for (int i = 0; i < 2; i++) {
if (!argv[i]->realValue(d[i])) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgType,
StringMessageArg(interp.makeStringC("Device KX")));
return interp.makeError();
}
if (d[i] < 0.0 || d[i] > 1.0) {
interp.setNextLocation(loc);
interp.message(InterpreterMessages::colorArgRange,
StringMessageArg(interp.makeStringC("Device KX")));
return interp.makeError();
}
}
unsigned char c;
c = (unsigned char)((1 - MIN(1, d[0] + d[1]))*255.0 + .5);
return new (interp) DeviceRGBColorObj(c, c, c);
}
VarInheritedC::VarInheritedC(const ConstPtr<InheritedC> &ic,
const InsnPtr &code, const Location &loc)
: InheritedC(ic->identifier(), ic->index()), inheritedC_(ic), code_(code), loc_(loc)
{
}
void VarInheritedC::set(VM &vm, const VarStyleObj *style, FOTBuilder &fotb,
ELObj *&cacheObj, Vector<size_t> &dependencies) const
{
if (!cacheObj) {
EvalContext::CurrentNodeSetter cns(style->node(), 0, vm);
vm.actualDependencies = &dependencies;
cacheObj = vm.eval(code_.pointer(), style->display());
ASSERT(cacheObj != 0);
vm.actualDependencies = 0;
}
if (!vm.interp->isError(cacheObj)) {
ConstPtr<InheritedC> c(inheritedC_->make(cacheObj, loc_, *vm.interp));
if (!c.isNull())
c->set(vm, 0, fotb, cacheObj, dependencies);
}
}
ConstPtr<InheritedC> VarInheritedC::make(ELObj *obj, const Location &loc, Interpreter &interp)
const
{
return inheritedC_->make(obj, loc, interp);
}
ELObj *VarInheritedC::value(VM &vm, const VarStyleObj *style,
Vector<size_t> &dependencies) const
{
EvalContext::CurrentNodeSetter cns(style->node(), 0, vm);
vm.actualDependencies = &dependencies;
return vm.eval(code_.pointer(), style->display());
}
StyleObjIter::StyleObjIter()
: i_(0), vi_(0)
{
}
void StyleObjIter::append(const Vector<ConstPtr<InheritedC> > *v, const VarStyleObj *obj)
{
styleVec_.push_back(obj);
vecs_.push_back(v);
}
ConstPtr<InheritedC> StyleObjIter::next(const VarStyleObj *&style)
{
for (; vi_ < vecs_.size(); vi_++, i_ = 0) {
if (i_ < vecs_[vi_]->size()) {
style = styleVec_[vi_];
return (*vecs_[vi_])[i_++];
}
}
return ConstPtr<InheritedC>();
}
StyleSpec::StyleSpec(Vector<ConstPtr<InheritedC> > &fs, Vector<ConstPtr<InheritedC> > &s)
{
fs.swap(forceSpecs);
s.swap(specs);
}
#ifdef DSSSL_NAMESPACE
}
#endif
|