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 941 942 943 944 945 946 947 948 949 950 951 952 953 954
|
// TextFOTBuilder - a formatter for plain text output.
// Copyright (c) 1999 Avi Kivity
#include <stack>
#include <stdlib.h>
#include "config.h"
#include "TextFOTBuilder.h"
#include <OpenSP/macros.h>
#include "dsssl_ns.h"
#ifdef JADE_TEXT
#undef DBG_TEXT_BACKEND
#ifndef DBG_TEXT_BACKEND
# define DBG(x) void(0)
#else
# define DBG(x) x
# include <iostream.h>
#endif
#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif
#ifdef DSSSL_NAMESPACE
namespace TextBackend {
# define TEXTBACKEND_SCOPE TextBackend::
#else
# define TEXTBACKEND_SCOPE
#endif
#ifdef DBG_TEXT_BACKEND
class Debugger {
public:
Debugger(const char *func) : context_(func) { indent() << ">> " << context_ << endl; ++level_; }
~Debugger() { --level_; indent() << "<< " << context_ << endl; }
ostream& indent() { for (int i = 0; i < level_; ++i) os() << " "; return os(); }
ostream& os() { return cout; }
private:
static int level_;
const char* context_;
};
int Debugger::level_ = 0;
template<class T> ostream& operator << (Debugger& d, const T& v) {
return d.indent() << "-- " << v;
}
#endif
class Uncopyable {
public:
Uncopyable() {}
private:
Uncopyable(const Uncopyable&); // undefined
void operator=(const Uncopyable&); // undefined
};
class Abstract {
public:
virtual ~Abstract() {}
};
typedef long Ordinate;
struct BoundBox {
BoundBox() : left(0), right(0), top(0), bottom(0) {}
Ordinate left, right, top, bottom;
};
class LineWidthProvider : Abstract
{
public:
virtual int& numberOfSpaces() = 0;
virtual Ordinate& freeLineWidth() = 0;
virtual void nextLine() = 0;
};
struct TextArea {
class Area : Abstract {
public:
virtual Ordinate width() const = 0;
virtual Ordinate height() const = 0;
virtual void chars(Ordinate x, Ordinate y, const Char* chars, unsigned n) = 0;
};
class Page : public Area {
public:
virtual void flush() = 0;
};
class RealPage : public Page {
public:
RealPage(OutputByteStream& os);
virtual Ordinate width() const;
virtual Ordinate height() const;
virtual void chars(Ordinate x, Ordinate y, const Char* chars, unsigned n);
virtual void flush();
private:
Char page_[66][80];
OutputByteStream& os_;
};
class DummyPage : public Page {
public:
virtual Ordinate width() const;
virtual Ordinate height() const;
virtual void chars(Ordinate x, Ordinate y, const Char* chars, unsigned n);
virtual void flush();
};
class SubArea : public Area {
public:
SubArea(Area& container, BoundBox bbox);
virtual Ordinate width() const;
virtual Ordinate height() const;
virtual void chars(Ordinate x, Ordinate y, const Char* chars, unsigned n);
private:
BoundBox bbox_;
Area& area_;
};
class MeasuredArea : public Area {
public:
MeasuredArea(Ordinate width, Ordinate max_height = 66);
Ordinate measuredWidth() const;
Ordinate measuredHeight() const;
virtual Ordinate width() const;
virtual Ordinate height() const;
virtual void chars(Ordinate x, Ordinate y, const Char* chars, unsigned n);
private:
Ordinate width_;
Ordinate height_;
Ordinate measuredHeight_;
Ordinate measuredWidth_;
};
class PageFactory : Abstract {
public:
virtual Page* create() const = 0;
};
class RealPageFactory : public PageFactory {
public:
RealPageFactory(OutputByteStream* os) : os_(os) {}
virtual Page* create() const { return new RealPage(*os_); }
private:
OutputByteStream* os_;
};
class DummyPageFactory : public PageFactory {
public:
virtual Page* create() const { return new DummyPage; }
};
};
const double xCellSize = (1.0/80)*6.5*72000;
Ordinate
length2xordinate(Ordinate displaySize, const FOTBuilder::LengthSpec& ls)
{
return ls.length / xCellSize + ls.displaySizeFactor * displaySize;
}
Ordinate
length2xordinate(TextArea::Area& area, const FOTBuilder::LengthSpec& ls)
{
return length2xordinate(area.width(), ls);
}
class TextFOTBuilder
: Uncopyable, public FOTBuilder
{
public:
TextFOTBuilder(TextArea::PageFactory& pageFactory, LineWidthProvider& lineWidthProvider);
~TextFOTBuilder();
private:
virtual void startSimplePageSequence(FOTBuilder* headerFooter[nHF]);
virtual void endSimplePageSequenceHeaderFooter();
virtual void endSimplePageSequence();
virtual void startParagraph(const ParagraphNIC &);
virtual void endParagraph();
virtual void characters(const Char *, size_t);
virtual void setQuadding(Symbol);
virtual void setStartIndent(const LengthSpec &);
virtual void setEndIndent(const LengthSpec &);
virtual void setFirstLineStartIndent(const LengthSpec &);
public:
struct Style {
Style();
Symbol quadding;
LengthSpec startIndent;
LengthSpec endIndent;
LengthSpec firstLineStartIndent;
};
struct Model {
struct Generated {
Generated() : height(0), height_specified(0) {}
SaveFOTBuilder content;
Ordinate height;
Boolean height_specified;
};
struct Region {
Generated header;
Generated footer;
BoundBox boundBox;
};
struct PageModel {
PageModel() : principalRegion(0) {}
Vector<Region> regions;
size_t principalRegion;
};
struct PageSequence {
Vector<PageModel> initial;
Vector<PageModel> repeat;
};
};
struct Output {
class FlowPort : Abstract {
public:
// can be defined in terms of next function
virtual Ordinate currentWidth() const = 0;
virtual void chars(Ordinate x, const Char* chars, unsigned n) = 0;
virtual void verticalSpace(Ordinate height) = 0;
};
class ParaBuilder : Abstract {
public:
virtual void chars(const Char* chars, unsigned n) = 0;
virtual void pbreak() = 0;
};
class NestedParaBuilder : public ParaBuilder {
public:
virtual void chars(const Char* chars, unsigned n);
virtual void pbreak();
void push(ParaBuilder* inner);
void pop();
ParaBuilder* top();
private:
Vector<ParaBuilder*> stack_;
};
class FlowParaBuilder : public ParaBuilder {
public:
FlowParaBuilder(FlowPort& flow, TextFOTBuilder& fotb);
~FlowParaBuilder();
virtual void chars(const Char* chars, unsigned n);
virtual void pbreak();
private:
void allocLine();
void flushLine();
Ordinate currentWidth();
LengthSpec currentStartIndent();
private:
FlowPort& flow_;
TextFOTBuilder& fotb_;
Ordinate x_;
Ordinate limit_;
Ordinate offset_;
Boolean isFirstLine_;
};
class AreaFlowPort : public FlowPort {
public:
AreaFlowPort(TextArea::Area& area);
virtual Ordinate currentWidth() const;
virtual void chars(Ordinate x, const Char* chars, unsigned n);
virtual void verticalSpace(Ordinate height);
private:
TextArea::Area& area_;
Ordinate y_;
};
class PageSeqFlowPort : public FlowPort {
public:
PageSeqFlowPort(TextFOTBuilder& fotb, Model::PageSequence model);
~PageSeqFlowPort();
virtual Ordinate currentWidth() const;
virtual void chars(Ordinate x, const Char* chars, unsigned n);
virtual void verticalSpace(Ordinate height);
private:
const TextArea::Area* getArea() const;
TextArea::Area* getArea();
TextArea::Area* createPrinicipalArea(TextArea::Area& page);
private:
TextFOTBuilder& fotb_;
TextArea::PageFactory& pageFactory_;
Vector<TextArea::Page*> output_;
TextArea::Area* currentArea_;
Model::PageSequence model_;
Ordinate y_;
};
};
public:
Ordinate measureGenerated(const Model::Generated& gen, const TextArea::Area& area);
Ordinate quaddingOffset(Symbol quadding, Ordinate freeLineWidth);
const Style& style() const { return styleStack_.top(); }
Style& style() { return styleStack_.top(); }
virtual void start();
virtual void end();
public:
TextArea::PageFactory& pageFactory_;
LineWidthProvider& lineWidthProvider_;
FOTBuilder bitBucket_;
Output::FlowPort* principalPort_;
Output::NestedParaBuilder* para_;
SaveFOTBuilder* spsHF;
std::stack<Style> styleStack_;
Style style_;
};
Ordinate
length2xordinate(const TextFOTBuilder::Output::FlowPort& flow, const FOTBuilder::LengthSpec& ls)
{
return length2xordinate(flow.currentWidth(), ls);
}
TextFOTBuilder::Style::Style()
: quadding ( symbolStart )
{
}
TextFOTBuilder::TextFOTBuilder(TextArea::PageFactory& pageFactory, LineWidthProvider& lineWidthProvider)
: pageFactory_(pageFactory)
, lineWidthProvider_(lineWidthProvider)
, para_(new Output::NestedParaBuilder)
, principalPort_(0)
{
styleStack_.push(Style());
}
TextFOTBuilder::~TextFOTBuilder()
{
delete para_;
}
void
TextFOTBuilder::startSimplePageSequence(FOTBuilder* headerFooter[nHF])
{
DBG(Debugger dbg("TextFOTBuilder::startSimplePageSequence()"));
start();
// FIXME: check PagePointer != 0
spsHF = new SaveFOTBuilder[nHF];
for (int i = 0; i < nHF; ++i)
headerFooter[i] = &spsHF[i];
}
void
TextFOTBuilder::endSimplePageSequenceHeaderFooter()
{
DBG(Debugger dbg("TextFOTBuilder::endSimplePageSequenceHeaderFooter()"));
Model::PageSequence ps;
Model::PageModel pm;
Model::Region r;
r.boundBox.left = 0;
r.boundBox.right = 80;
r.boundBox.top = 0;
r.boundBox.bottom = 66;
pm.regions.push_back(r);
ps.repeat.push_back(pm);
HF lcr[3] = { leftHF, centerHF, rightHF };
HF hf[2] = { headerHF, footerHF };
Symbol quadding[3] = { symbolStart, symbolCenter, symbolEnd };
Model::Generated (Model::Region::*gen[2])
= { &Model::Region::header, &Model::Region::footer };
for (int i = 0; i < 2; ++i) {
Model::Generated& g = ps.repeat[0].regions[0].*(gen[i]);
g.content.startSideBySide(DisplayNIC());
for ( unsigned j = 0; j < 3; ++j ) {
g.content.startSideBySideItem();
g.content.setQuadding(quadding[j]);
g.content.startParagraph(ParagraphNIC());
spsHF[hf[i] | lcr[j] | firstHF | otherHF].emit(g.content);
g.content.endParagraph();
g.content.endSideBySideItem();
}
g.content.endSideBySide();
}
principalPort_ = new Output::PageSeqFlowPort(*this, ps);
}
void
TextFOTBuilder::endSimplePageSequence()
{
DBG(Debugger dbg("TextFOTBuilder::endSimplePageSequence()"));
delete principalPort_;
principalPort_ = 0;
end();
}
void
TextFOTBuilder::startParagraph(const ParagraphNIC &)
{
DBG(Debugger dbg("TextFOTBuilder::startParagraph()"));
start();
para_->push(new Output::FlowParaBuilder(*principalPort_, *this));
}
void
TextFOTBuilder::endParagraph()
{
DBG(Debugger dbg("TextFOTBuilder::endParagraph()"));
delete para_->top();
para_->pop();
end();
}
void
TextFOTBuilder::characters(const Char* chars, size_t n)
{
atomic();
DBG(Debugger dbg("TextFOTBuilder::characters()"));
DBG(dbg << "data = \"");
DBG(for (size_t i = 0; i<n; ++i) dbg.os() << char(chars[i]));
DBG(dbg.os() << "\"" << endl);
para_->chars(chars, n);
}
TextArea::RealPage::RealPage(OutputByteStream& os)
: os_(os)
{
DBG(Debugger dbg("TextArea::RealPage::RealPage()"));
for (unsigned y = 0; y < height(); ++y)
for (unsigned x = 0; x < width(); ++x)
page_[y][x] = ' ';
}
Ordinate
TextArea::RealPage::width() const
{
return 80;
}
Ordinate
TextArea::RealPage::height() const
{
return 66;
}
void
TextArea::RealPage::chars(Ordinate x, Ordinate y, const Char* chars, unsigned n)
{
DBG(Debugger dbg("TextArea::RealPage::chars()"));
DBG(dbg << "x = " << x << ", y = " << y << endl);
for (unsigned i = 0; i < n; ++i)
page_[y][x+i] = chars[i];
}
void
TextArea::RealPage::flush()
{
DBG(Debugger dbg("TextArea::RealPage::flush()"));
for (Ordinate y = 0; y < height(); ++y) {
Ordinate end = width();
while (end > 0 && page_[y][end-1] == ' ')
--end;
for (Ordinate x = 0; x < end; ++x)
os_.sputc(char(page_[y][x])); // hope for ascii
os_.sputc('\n');
}
os_.sputc('\f');
}
Ordinate
TextArea::DummyPage::width() const
{
return 80;
}
Ordinate
TextArea::DummyPage::height() const
{
return 66;
}
void
TextArea::DummyPage::chars(Ordinate x, Ordinate y, const Char* chars, unsigned n)
{
DBG(Debugger dbg("TextArea::DummyPage::chars()"));
DBG(dbg << "x = " << x << ", y = " << y << endl);
}
void
TextArea::DummyPage::flush()
{
}
TextArea::SubArea::SubArea(Area& container, BoundBox bbox)
: bbox_(bbox)
, area_(container)
{
}
Ordinate
TextArea::SubArea::width() const
{
return bbox_.right - bbox_.left;
}
Ordinate
TextArea::SubArea::height() const
{
return bbox_.bottom - bbox_.top;
}
void
TextArea::SubArea::chars(Ordinate x, Ordinate y, const Char* chars, unsigned n)
{
area_.chars(x+bbox_.left, y+bbox_.top, chars, n);
}
TextArea::MeasuredArea::MeasuredArea(Ordinate width, Ordinate max_height)
: width_(width)
, height_(max_height)
, measuredHeight_(0)
, measuredWidth_(0)
{
}
Ordinate
TextArea::MeasuredArea::measuredHeight() const
{
return measuredHeight_;
}
Ordinate
TextArea::MeasuredArea::measuredWidth() const
{
return measuredWidth_;
}
Ordinate
TextArea::MeasuredArea::height() const
{
return height_;
}
Ordinate
TextArea::MeasuredArea::width() const
{
return width_;
}
template <class T>
const T&
stdmax(const T& a, const T& b) {
return a < b ? b : a;
}
void
TextArea::MeasuredArea::chars(Ordinate x, Ordinate y, const Char* chars, unsigned n)
{
measuredHeight_ = stdmax(Ordinate(y+1), measuredHeight_);
measuredWidth_ = stdmax(Ordinate(x+n), measuredWidth_);
}
TextFOTBuilder::Output::FlowParaBuilder::FlowParaBuilder(FlowPort& flow, TextFOTBuilder& fotb)
: flow_(flow)
, fotb_(fotb)
, x_(0)
, limit_(0)
, isFirstLine_(1)
{
DBG(Debugger dbg("TextFOTBuilder::Output::FlowParaBuilder::FlowParaBuilder()"));
}
TextFOTBuilder::Output::FlowParaBuilder::~FlowParaBuilder()
{
DBG(Debugger dbg("TextFOTBuilder::Output::FlowParaBuilder::~FlowParaBuilder()"));
flushLine();
}
TextFOTBuilder::LengthSpec
TextFOTBuilder::Output::FlowParaBuilder::currentStartIndent()
{
LengthSpec result; // why not operator +?
result.length = fotb_.style().startIndent.length
+ isFirstLine_ * fotb_.style().firstLineStartIndent.length;
result.displaySizeFactor = fotb_.style().startIndent.displaySizeFactor
+ isFirstLine_ * fotb_.style().firstLineStartIndent.displaySizeFactor;
return result;
}
Ordinate
TextFOTBuilder::Output::FlowParaBuilder::currentWidth()
{
return flow_.currentWidth()
- length2xordinate(flow_, currentStartIndent())
- length2xordinate(flow_, fotb_.style().endIndent);
}
void
TextFOTBuilder::Output::FlowParaBuilder::allocLine()
{
DBG(Debugger dbg("TextFOTBuilder::Output::FlowParaBuilder::allocLine()"));
x_ = 0;
currentWidth(); // Ensure there is an area allocated. FIXME.
fotb_.lineWidthProvider_.nextLine();
limit_ = currentWidth() - fotb_.lineWidthProvider_.freeLineWidth();
offset_ = length2xordinate(flow_, currentStartIndent())
+ fotb_.quaddingOffset(fotb_.style().quadding, currentWidth() - limit_);
DBG(dbg << "limit = " << limit_ << "; setting quadding offset at " << offset_ << endl);
}
void
TextFOTBuilder::Output::FlowParaBuilder::flushLine()
{
DBG(Debugger dbg("TextFOTBuilder::Output::FlowParaBuilder::flushLine()"));
if (x_ > 0) {
DBG(Debugger dbg("flushLine active"));
flow_.verticalSpace(1);
fotb_.lineWidthProvider_.freeLineWidth() = limit_ - x_;
}
x_ = limit_ = 0;
isFirstLine_ = 0;
}
void
TextFOTBuilder::Output::FlowParaBuilder::chars(const Char* chars, unsigned n)
{
DBG(Debugger dbg("TextFOTBuilder::Output::FlowParaBuilder::chars()"));
// break lines rather tastelessly.
while (n > 0) {
if (x_ >= limit_)
allocLine();
size_t done = limit_ - x_;
if (done > n)
done = n;
flow_.chars(offset_ + x_, chars, done);
x_ += done;
n -= done;
chars += done;
if (x_ == limit_)
flushLine();
}
}
void
TextFOTBuilder::Output::FlowParaBuilder::pbreak()
{
flushLine();
}
void
TextFOTBuilder::Output::NestedParaBuilder::chars(const Char* chars, unsigned n)
{
if (stack_.size() == 0)
CANNOT_HAPPEN();
top()->chars(chars, n);
}
void
TextFOTBuilder::Output::NestedParaBuilder::pbreak()
{
if (stack_.size() == 0)
CANNOT_HAPPEN();
top()->pbreak();
}
void
TextFOTBuilder::Output::NestedParaBuilder::push(ParaBuilder* inner)
{
if (stack_.size())
top()->pbreak();
stack_.push_back(inner);
}
void
TextFOTBuilder::Output::NestedParaBuilder::pop()
{
stack_.erase(stack_.begin() + stack_.size()-1, stack_.begin() + stack_.size());
}
TextFOTBuilder::Output::ParaBuilder*
TextFOTBuilder::Output::NestedParaBuilder::top()
{
return stack_.back();
}
TextFOTBuilder::Output::AreaFlowPort::AreaFlowPort(TextArea::Area& area)
: area_ ( area )
, y_ ( 0 )
{
}
Ordinate
TextFOTBuilder::Output::AreaFlowPort::currentWidth() const
{
return area_.width();
}
void
TextFOTBuilder::Output::AreaFlowPort::chars(Ordinate x, const Char* chars, unsigned n)
{
DBG(Debugger dbg("TextFOTBuilder::Output::AreaFlowPort::chars()"));
area_.chars(x, y_, chars, n);
}
void
TextFOTBuilder::Output::AreaFlowPort::verticalSpace(Ordinate height)
{
y_ += height;
}
TextFOTBuilder::Output::PageSeqFlowPort::PageSeqFlowPort
(TextFOTBuilder& fotb, Model::PageSequence model)
: fotb_(fotb)
, pageFactory_(fotb.pageFactory_)
, model_(model)
, currentArea_(0)
, y_(0)
{
}
TextFOTBuilder::Output::PageSeqFlowPort::~PageSeqFlowPort()
{
for (unsigned i = 0; i < output_.size(); ++i) {
output_[i]->flush();
delete output_[i];
}
}
TextArea::Area*
TextFOTBuilder::Output::PageSeqFlowPort::getArea()
{
if (!currentArea_ || (y_+1 >= currentArea_->height())) {
DBG(Debugger dbg("TextFOTBuilder::Output::PageSeqFlowPort::getArea()"));
DBG(dbg << "Creating a new page." << endl);
output_.push_back(pageFactory_.create());
currentArea_ = createPrinicipalArea(*output_.back());
y_ = 0;
}
return currentArea_;
}
const TextArea::Area*
TextFOTBuilder::Output::PageSeqFlowPort::getArea() const
{
return const_cast<PageSeqFlowPort*>(this)->getArea();
}
TextArea::Area*
TextFOTBuilder::Output::PageSeqFlowPort::createPrinicipalArea(TextArea::Area& page)
{
DBG(Debugger dbg("TextFOTBuilder::Output::PageSeqFlowPort::createPrinicipalArea()"));
FlowPort* save = fotb_.principalPort_;
TextFOTBuilder::Output::NestedParaBuilder* save_para = fotb_.para_;
fotb_.para_ = new NestedParaBuilder;
DBG(dbg << "Measuring header" << endl);
Ordinate headerHeight = fotb_.measureGenerated(model_.repeat[0].regions[0].header, page);
DBG(dbg << "Measuring footer" << endl);
Ordinate footerHeight = fotb_.measureGenerated(model_.repeat[0].regions[0].footer, page);
DBG(dbg << "header = " << headerHeight << ", footer = " << footerHeight << endl);
BoundBox bbox;
bbox.left = 0;
bbox.right = page.width();
DBG(dbg << "Emitting header" << endl);
bbox.top = 0;
bbox.bottom = headerHeight;
TextArea::SubArea header(page, bbox);
Output::AreaFlowPort fp_h(header);
fotb_.principalPort_ = &fp_h;
model_.repeat[0].regions[0].header.content.emit(fotb_);
DBG(dbg << "Emitting footer" << endl);
bbox.top = page.height() - footerHeight;
bbox.bottom = page.height();
TextArea::SubArea footer(page, bbox);
Output::AreaFlowPort fp_f(footer);
fotb_.principalPort_ = &fp_f;
model_.repeat[0].regions[0].footer.content.emit(fotb_);
DBG(dbg << "Creating principal port subarea" << endl);
delete fotb_.para_;
fotb_.para_ = save_para;
fotb_.principalPort_ = save;
bbox.top = headerHeight;
bbox.bottom = page.height() - footerHeight;
return new TextArea::SubArea(page, bbox);
}
Ordinate
TextFOTBuilder::measureGenerated(const Model::Generated& g, const TextArea::Area& p)
{
Output::FlowPort* save = principalPort_;
TextArea::MeasuredArea ma ( p.width(), p.height() );
Output::AreaFlowPort fp ( ma );
principalPort_ = &fp;
g.content.emit(*this);
Ordinate height = ma.measuredHeight();
principalPort_ = save;
return height;
}
Ordinate
TextFOTBuilder::Output::PageSeqFlowPort::currentWidth() const
{
return getArea()->width();
}
void
TextFOTBuilder::Output::PageSeqFlowPort::chars(Ordinate x, const Char* chars, unsigned n)
{
DBG(Debugger dbg("TextFOTBuilder::Output::PageSeqFlowPort::chars()"));
getArea()->chars(x, y_, chars, n);
}
void
TextFOTBuilder::Output::PageSeqFlowPort::verticalSpace(Ordinate height)
{
y_ += height;
}
void
TextFOTBuilder::setQuadding(Symbol quadding)
{
style_.quadding = quadding;
}
void
TextFOTBuilder::setStartIndent(const LengthSpec& l)
{
style_.startIndent = l;
}
void
TextFOTBuilder::setEndIndent(const LengthSpec& l)
{
style_.endIndent = l;
}
void
TextFOTBuilder::setFirstLineStartIndent(const LengthSpec& l)
{
style_.firstLineStartIndent = l;
}
void
TextFOTBuilder::start()
{
styleStack_.push(style_);
}
void
TextFOTBuilder::end()
{
styleStack_.pop();
style_ = styleStack_.top();
}
Ordinate
TextFOTBuilder::quaddingOffset(Symbol quadding, long freeLineWidth)
{
switch (quadding)
{
case symbolStart: return 0;
case symbolCenter: return freeLineWidth/2;
case symbolEnd: return freeLineWidth;
}
return 0;
}
class LineWidthCache
{
public:
LineWidthCache() : fill_(cache_), use_() {}
LineWidthProvider& fill() { return fill_; }
LineWidthProvider& use() { use_.reset(cache_); return use_; }
private:
struct Entry {
Entry() : nspaces(0), width(0) {}
int nspaces;
Ordinate width;
};
class Fill : public LineWidthProvider {
public:
Fill(Vector<Entry>& cache) : cache_(cache) { nextLine(); }
virtual int& numberOfSpaces() { return cache_.back().nspaces; }
virtual Ordinate& freeLineWidth() { return cache_.back().width; }
virtual void nextLine() { cache_.push_back(Entry()); }
private:
Vector<Entry>& cache_;
};
class Use : public LineWidthProvider {
public:
virtual int& numberOfSpaces() { return (*line_).nspaces; }
virtual Ordinate& freeLineWidth() { return (*line_).width; }
virtual void nextLine() { ++line_; }
void reset(Vector<Entry>& cache) { line_ = cache.begin(); }
private:
Vector<Entry>::iterator line_;
};
private:
Vector<Entry> cache_;
Fill fill_;
Use use_;
friend class Fill;
friend class Use;
};
class MultipassTextFOTBuilder
: Uncopyable, public SaveFOTBuilder
{
public:
MultipassTextFOTBuilder(OutputByteStream* os);
~MultipassTextFOTBuilder();
private:
void process();
private:
OutputByteStream* os_;
};
MultipassTextFOTBuilder::MultipassTextFOTBuilder(OutputByteStream* os)
: os_(os)
{
}
MultipassTextFOTBuilder::~MultipassTextFOTBuilder()
{
process();
}
void
MultipassTextFOTBuilder::process()
{
DBG(Debugger dbg("MultipassTextFOTBuilder::process()"));
DBG(dbg << "Measurement pass" << endl);
LineWidthCache lwc;
TextArea::DummyPageFactory nooutput;
TextFOTBuilder pass1(nooutput, lwc.fill());
emit(pass1);
DBG(dbg << "Output pass" << endl);
TextArea::RealPageFactory output(os_);
TextFOTBuilder pass2(output, lwc.use());
emit(pass2);
}
#ifdef DSSSL_NAMESPACE
} // namespace TextBackend
#endif
FOTBuilder *makeTextFOTBuilder(OutputByteStream* os, Messenger *,
const FOTBuilder::Description *&descr)
{
static const FOTBuilder::Feature features[] = {
{ "simple-page", 0},
{ 0, 0}
};
static const FOTBuilder::Description description = {
0, // Extensions
features,
false
};
descr = &description;
return new TEXTBACKEND_SCOPE MultipassTextFOTBuilder(os);
}
#ifdef DSSSL_NAMESPACE
}
#endif
#endif /* JADE_TEXT */
|