Debian Codegraph @

jade/HtmlFOTBuilder.cxx

raw
1 // Copyright (c) 1996, 1997 James Clark
2 // See the file copying.txt for copying permission.
3
4 #include "config.h"
5
6 #ifdef JADE_HTML
7
8 #include "HtmlFOTBuilder.h"
9 #include "FOTBuilder.h"
10 #include <OpenSP/Link.h>
11 #include <OpenSP/IList.h>
12 #include <OpenSP/IListIter.h>
13 #include <OpenSP/OutputCharStream.h>
14 #include <OpenSP/Ptr.h>
15 #include <OpenSP/Resource.h>
16 #include <OpenSP/macros.h>
17 #include "HtmlMessages.h"
18 #include <OpenSP/MessageArg.h>
19 #include <OpenSP/ErrnoMessageArg.h>
20 #include <OpenSP/StringResource.h>
21 #include <OpenSP/OwnerTable.h>
22 #include <OpenSP/Hash.h>
23 #include <OpenSP/OutputByteStream.h>
24 #include "dsssl_ns.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 #ifdef DSSSL_NAMESPACE
31 namespace DSSSL_NAMESPACE {
32 #endif
33
34 class DiscardOutputCharStream : public OutputCharStream {
35 public:
36 DiscardOutputCharStream() { }
37 void flush() { }
38 void flushBuf(Char) { }
39 };
40
41 const char RE = '\r';
42
43 class HtmlFOTBuilder : public SerialFOTBuilder {
44 public:
45 class OutputState;
46 struct CharProps {
47 CharProps();
48 static unsigned long hash(const CharProps &) { return 17; }
49 bool operator==(const CharProps &cp) const {
50 return (fontWeight == cp.fontWeight
51 && fontStyle == cp.fontStyle
52 && fontSize == cp.fontSize
53 && fontFamily == cp.fontFamily
54 && color == cp.color
55 && textTransform == cp.textTransform);
56 }
57 bool operator!=(const CharProps &cp) const { return !(*this == cp); }
58 char fontWeight;
59 enum { styleNormal, styleItalic, styleOblique };
60 char fontStyle;
61 unsigned color;
62 Length fontSize;
63 StringC fontFamily;
64 enum { ttNone, ttUppercase, ttLowercase, ttCapitalize };
65 char textTransform;
66 };
67 struct InheritParaProps {
68 InheritParaProps();
69 enum {
70 alignLeft,
71 alignCenter,
72 alignRight,
73 alignJustify
74 };
75 Length leftMargin;
76 Length rightMargin;
77 Length lineHeight;
78 Length textIndent;
79 char align;
80 };
81 struct ParaProps : public InheritParaProps {
82 ParaProps(const InheritParaProps &props)
83 : InheritParaProps(props), topMargin(0) { }
84 static unsigned long hash(const ParaProps &) { return 17; }
85 bool operator==(const ParaProps &pp) const {
86 return (leftMargin == pp.leftMargin
87 && rightMargin == pp.rightMargin
88 && lineHeight == pp.lineHeight
89 && textIndent == pp.textIndent
90 && topMargin == pp.topMargin
91 && align == pp.align);
92 }
93 bool operator!=(const ParaProps &pp) const { return !(*this == pp); }
94 Length topMargin;
95 };
96
97 struct ClassPrefix {
98 ClassPrefix(const StringC &s) : prefix(s), nCharClasses(0), nParaClasses(0) { }
99 StringC prefix;
100 unsigned nCharClasses;
101 unsigned nParaClasses;
102 static const StringC &key(const ClassPrefix &cp) { return cp.prefix; }
103 };
104
105 struct StyleClass : public Link {
106 StyleClass(const StringC &, ClassPrefix *);
107 void outputName(OutputCharStream &) const;
108 StringC gi;
109 const ClassPrefix *prefix;
110 unsigned prefixIndex;
111 };
112
113 struct CharStyle;
114
115 struct CharStyleClass : public StyleClass {
116 CharStyleClass(const StringC &, ClassPrefix *, const CharStyle *);
117 const CharStyle *style;
118 };
119
120 struct CharStyle : public CharProps {
121 CharStyle(const CharProps &cp) : CharProps(cp) { }
122 void output(OutputCharStream &) const;
123 static const CharProps &key(const CharStyle &style) { return style; }
124 IList<CharStyleClass> classes;
125 };
126
127 struct ParaStyle;
128
129 struct ParaStyleClass : public StyleClass {
130 ParaStyleClass(const StringC &, ClassPrefix *, const ParaStyle *);
131 const ParaStyle *style;
132 };
133
134 struct ParaStyle : public ParaProps {
135 ParaStyle(const ParaProps &pp) : ParaProps(pp) { }
136 void output(OutputCharStream &) const;
137 static const ParaProps &key(const ParaStyle &style) { return style; }
138 IList<ParaStyleClass> classes;
139 };
140
141 struct FlowObjectInfo : public CharProps, public InheritParaProps {
142 FlowObjectInfo();
143 unsigned docIndex;
144 ConstPtr<StringResource<Char> > scrollTitle;
145 Length parentLeftMargin;
146 Length parentRightMargin;
147 };
148
149 // An address that has been referenced.
150 class AddressRef {
151 public:
152 virtual void outputRef(bool end, OutputCharStream &, OutputState &) const = 0;
153 virtual ~AddressRef();
154 };
155
156 class UriRefAddressRef : public AddressRef, public Link {
157 public:
158 UriRefAddressRef(const StringC &uriRef)
159 : uriRef_(uriRef) { }
160 void outputRef(bool end, OutputCharStream &, OutputState &) const;
161 private:
162 StringC uriRef_;
163 };
164
165 class OutputState {
166 public:
167 OutputState(const String<CmdLineApp::AppChar> *outputFilename,
168 const StringC &styleSheetFilename,
169 CmdLineApp *app)
170 : outputFilename_(outputFilename),
171 styleSheetFilename_(styleSheetFilename),
172 app_(app),
173 curAref_(0),
174 specAref_(0),
175 curCharStyleClass_(0),
176 outputDocIndex_(unsigned(-1)) { }
177 unsigned setOutputDocIndex(unsigned i) {
178 unsigned tem = outputDocIndex_;
179 outputDocIndex_ = i;
180 return tem;
181 }
182 const AddressRef *setAddress(const AddressRef *aref) {
183 const AddressRef *tem = specAref_;
184 specAref_ = aref;
185 return tem;
186 }
187 void syncChar(const CharStyleClass *, OutputCharStream &);
188 unsigned outputDocIndex() const { return outputDocIndex_; }
189 String<CmdLineApp::AppChar> outputFilename(unsigned i) const;
190 const String<CmdLineApp::AppChar> &outputFilename() const {
191 return *outputFilename_;
192 }
193 CmdLineApp &app() { return *app_; }
194 const StringC &styleSheetFilename() const { return styleSheetFilename_; }
195 const OutputCodingSystem *codingSystem() const { return app_->outputCodingSystem(); }
196 Messenger &messenger() { return *app_; }
197 private:
198 const String<CmdLineApp::AppChar> *outputFilename_;
199
200 CmdLineApp *app_;
201 unsigned outputDocIndex_;
202 const AddressRef *curAref_;
203 const AddressRef *specAref_;
204 const CharStyleClass *curCharStyleClass_;
205 StringC styleSheetFilename_;
206 };
207 class Item : public Link {
208 public:
209 Item() { }
210 virtual ~Item();
211 virtual void output(OutputCharStream &, OutputState &) { }
212 private:
213 void operator=(const Item &);
214 Item(const Item &);
215 };
216 // Corresponds to <A NAME="..."></A>
217 class Addressable : public Item, public AddressRef {
218 public:
219 Addressable(size_t g, size_t e)
220 : groveIndex_(g), elementIndex_(e), referenced_(0), docIndex_(unsigned(-1)) { }
221 void outputRef(bool end, OutputCharStream &, OutputState &) const;
222 void output(OutputCharStream &, OutputState &);
223 bool defined() const { return docIndex_ != unsigned(-1); }
224 bool referenced() const { return referenced_; }
225 void setDefined(unsigned docIndex, bool wholeDocument = 0) {
226 docIndex_ = docIndex;
227 if (wholeDocument)
228 elementIndex_ = (unsigned)-1;
229 }
230 void setReferenced() { referenced_ = 1; }
231 private:
232 size_t groveIndex_;
233 // -1 if the whole document
234 size_t elementIndex_;
235 // unsigned(-1) if not defined
236 unsigned docIndex_;
237 bool referenced_;
238 };
239 class Markup : public Item {
240 public:
241 Markup(const StringC &str) : str_(str) { }
242 void output(OutputCharStream &, OutputState &);
243 private:
244 StringC str_;
245 };
246 class Pcdata : public Item {
247 public:
248 Pcdata(const CharStyleClass *styleClass) : styleClass_(styleClass) { }
249 void output(OutputCharStream &, OutputState &);
250 private:
251 const CharStyleClass *styleClass_;
252 virtual const Char *data(size_t &) const = 0;
253 };
254 class ImmediatePcdata : public Pcdata {
255 public:
256 ImmediatePcdata(const Char *s, size_t n, const CharStyleClass *styleClass)
257 : Pcdata(styleClass), str_(s, n) { }
258 private:
259 const Char *data(size_t &n) const { n = str_.size(); return str_.data(); }
260 StringC str_;
261 };
262 class NodePcdata : public Pcdata {
263 public:
264 NodePcdata(const NodePtr &node, const Char *s, size_t n, const CharStyleClass *styleClass)
265 : Pcdata(styleClass), node_(node), s_(s), n_(n) { }
266 private:
267 const Char *data(size_t &n) const { n = n_; return s_; }
268 NodePtr node_;
269 const Char *s_;
270 size_t n_;
271 };
272 class Container : public Item {
273 public:
274 Container() { }
275 IList<Item> *contentPtr() { return &content_; }
276 void output(OutputCharStream &, OutputState &);
277 protected:
278 void reverse();
279 // In reverse order while being built.
280 IList<Item> content_;
281 private:
282 Container(const Container &);
283 void operator=(const Container &);
284 };
285 class Ref : public Container {
286 public:
287 Ref(AddressRef *aref) : aref_(aref) { }
288 void output(OutputCharStream &, OutputState &);
289 private:
290 AddressRef *aref_;
291 };
292 class Block : public Container {
293 public:
294 Block(const ParaStyleClass *styleClass) : styleClass_(styleClass) { }
295 void output(OutputCharStream &, OutputState &);
296 private:
297 const ParaStyleClass *styleClass_;
298 };
299 class Document : public Container {
300 public:
301 Document(unsigned index, const ConstPtr<StringResource<Char> > &title)
302 : index_(index), title_(title) { }
303 ~Document();
304 void output(OutputCharStream &, OutputState &);
305 private:
306 ConstPtr<StringResource<Char> > title_;
307 unsigned index_;
308 };
309 HtmlFOTBuilder(const String<CmdLineApp::AppChar> &, CmdLineApp *);
310 ~HtmlFOTBuilder();
311 void setFontWeight(Symbol);
312 void setFontPosture(Symbol);
313 void setFontFamilyName(const StringC &);
314 void setFontSize(Length);
315 void setColor(const DeviceRGBColor &);
316 void setCharMap(Symbol);
317 void setQuadding(Symbol);
318 void setLineSpacing(const LengthSpec &);
319 void setFirstLineStartIndent(const LengthSpec &);
320 void setStartIndent(const LengthSpec &);
321 void setEndIndent(const LengthSpec &);
322 void setScrollTitle(const StringC &);
323 void formattingInstruction(const StringC &);
324 void start();
325 void end();
326 void atomic();
327 void characters(const Char *s, size_t n);
328 void charactersFromNode(const NodePtr &, const Char *s, size_t n);
329 void startParagraph(const ParagraphNIC &nic);
330 void endParagraph();
331 void startLink(const Address &);
332 void endLink();
333 void startScroll();
334 void endScroll();
335 void startNode(const NodePtr &, const StringC &);
336 void endNode();
337 void flushPendingAddresses();
338 static void outputCdata(const Char *, size_t, OutputCharStream &);
339 static void outputCdata(const StringC &s, OutputCharStream &os) {
340 outputCdata(s.data(), s.size(), os);
341 }
342 static void outputLength(FOTBuilder::Length n, OutputCharStream &);
343 private:
344 HtmlFOTBuilder(const HtmlFOTBuilder &);
345 void operator=(const HtmlFOTBuilder &);
346
347 struct DestInfo : public Link {
348 DestInfo(IList<Item> *p) : list(p) { }
349 IList<Item> *list;
350 };
351
352 void insertAddr(size_t, size_t);
353 void outputStyleSheet(StringC &);
354 void startDisplay(const DisplayNIC &);
355 void endDisplay();
356 Addressable *elementAddress(size_t g, size_t e);
357
358 const CharStyleClass *makeCharStyleClass();
359 const ParaStyleClass *makeParaStyleClass();
360 ClassPrefix *makeClassPrefix(const StringC &);
361
362 OwnerTable<CharStyle, CharProps, CharProps, CharStyle> charStyleTable_;
363 OwnerTable<ParaStyle, ParaProps, ParaProps, ParaStyle> paraStyleTable_;
364 OwnerTable<ClassPrefix, StringC, Hash, ClassPrefix> prefixTable_;
365 Vector<FlowObjectInfo> flowObjectStack_;
366 FlowObjectInfo nextFlowObject_;
367 IList<DestInfo> destStack_;
368 IList<Item> *dest_;
369 Container root_;
370 // Number of HTML documents
371 unsigned nDocuments_;
372 Vector<Vector<Addressable *> > elements_;
373 Vector<Vector<size_t> > pendingAddr_;
374 String<CmdLineApp::AppChar> outputFilename_;
375 CmdLineApp *app_;
376 Vector<StringC> giStack_;
377 Length topMargin_;
378 Vector<Length> spaceAfterStack_;
379 // For URI Ref links. This is just to get them deleted.
380 IList<UriRefAddressRef> uriRefs_;
381 };
382
383 FOTBuilder *makeHtmlFOTBuilder(const String<CmdLineApp::AppChar> &outputFilename,
384 CmdLineApp *app,
385 const FOTBuilder::Description *&descr) {
386 static const FOTBuilder::Extension extensions[] = {
387 { "UNREGISTERED::James Clark//Characteristic::scroll-title", 0,
388 (void (FOTBuilder::*)(const StringC &))&HtmlFOTBuilder::setScrollTitle },
389 { 0, 0, 0}
390 };
391
392 static const FOTBuilder::Feature features[] = {
393 { "online", 1},
394 { 0, 0}
395 };
396
397 static const char *media[] = { "screen", 0 };
398
399 static const FOTBuilder::Description description = {
400 extensions,
401 features,
402 false,
403 media
404 };
405 descr = &description;
406
407 return new HtmlFOTBuilder(outputFilename, app);
408 }
409
410 static
411 void reverse(IList<HtmlFOTBuilder::Item> &list)
412 {
413 IList<HtmlFOTBuilder::Item> tem;
414 while (!list.empty())
415 tem.insert(list.get());
416 tem.swap(list);
417 }
418
419 HtmlFOTBuilder::HtmlFOTBuilder(const String<CmdLineApp::AppChar> &outputFilename,
420 CmdLineApp *app)
421 : outputFilename_(outputFilename),
422 app_(app),
423 nDocuments_(0),
424 giStack_(1),
425 topMargin_(0)
426 {
427 flowObjectStack_.push_back(nextFlowObject_);
428 dest_ = root_.contentPtr();
429 giStack_[0] += 'S';
430 }
431
432 HtmlFOTBuilder::~HtmlFOTBuilder()
433 {
434 reverse(*dest_);
435 while (!destStack_.empty()) {
436 reverse(*destStack_.head()->list);
437 delete destStack_.get();
438 }
439 StringC styleSheetFilename;
440 outputStyleSheet(styleSheetFilename);
441 OutputState state(&outputFilename_, styleSheetFilename, app_);
442 DiscardOutputCharStream os;
443 root_.output(os, state);
444 for (size_t i = 0; i < elements_.size(); i++) {
445 for (size_t j = 0; j < elements_[i].size(); j++)
446 if (elements_[i][j] && !elements_[i][j]->defined())
447 delete elements_[i][j];
448 }
449 }
450
451 void HtmlFOTBuilder::outputStyleSheet(StringC &styleSheetFilename)
452 {
453 String<CmdLineApp::AppChar> filename(outputFilename_);
454 for (size_t j = 0; j < 5; j++) {
455 if (filename.size() < j + 1)
456 break;
457 Char c = filename[filename.size() - j - 1];
458 if (c == '/' || c == '\\')
459 break;
460 if (c == '.') {
461 filename.resize(filename.size() - j - 1);
462 break;
463 }
464 }
465 filename += '.';
466 filename += 'c';
467 filename += 's';
468 filename += 's';
469 filename += 0;
470 FileOutputByteStream file;
471 if (!file.open(filename.data())) {
472 app_->message(HtmlMessages::cannotOpenOutputError,
473 StringMessageArg(app_->convertInput(filename.data())),
474 ErrnoMessageArg(errno));
475 return;
476 }
477 styleSheetFilename = app_->convertInput(filename.data());
478 RecordOutputCharStream os(new EncodeOutputCharStream(&file,
479 app_->outputCodingSystem()));
480
481 {
482 OwnerTableIter<CharStyle, CharProps, CharProps, CharStyle> iter(charStyleTable_);
483 for (;;) {
484 const CharStyle *style = iter.next();
485 if (!style)
486 break;
487 style->output(os);
488 }
489 }
490 os << "DIV { margin-top: 0pt; margin-bottom: 0pt; margin-left: 0pt; margin-right: 0pt }"
491 << RE;
492 {
493 OwnerTableIter<ParaStyle, ParaProps, ParaProps, ParaStyle> iter(paraStyleTable_);
494 for (;;) {
495 const ParaStyle *style = iter.next();
496 if (!style)
497 break;
498 style->output(os);
499 }
500 }
501 }
502
503 void HtmlFOTBuilder::CharStyle::output(OutputCharStream &os) const
504 {
505 bool first = 1;
506 for (IListIter<CharStyleClass> iter(classes); !iter.done(); iter.next()) {
507 if (first)
508 first = 0;
509 else
510 os << ", ";
511 os << "SPAN.";
512 iter.cur()->outputName(os);
513 }
514 if (!first) {
515 os << " {" << RE;
516 static const char indent[] = " ";
517 // Netscape Communicator preview release 2 can't handle quoted font family names.
518 os << indent << "font-family: " << fontFamily << ';' << RE;
519 os << indent << "font-weight: " << int(fontWeight) << "00;" << RE;
520 static const char *styleNames[3] = {
521 "normal", "italic", "oblique"
522 };
523 os << indent << "font-style: " << styleNames[fontStyle] << ';' << RE;
524 os << indent << "font-size: ";
525 outputLength(fontSize, os);
526 os << ';' << RE;
527 os << indent << "color: #";
528 for (int i = 20; i >= 0; i -= 4)
529 os << "0123456789abcdef"[(color >> i) & 0xf];
530 os << ';' << RE;
531 static const char *textTransformNames[4] = {
532 "none", "uppercase", "lowercase", "capitalize"
533 };
534 os << indent << "text-transform: " <<
535 textTransformNames[textTransform] << ';' << RE;
536 os << '}' << RE;
537 }
538 }
539
540 void HtmlFOTBuilder::ParaStyle::output(OutputCharStream &os) const
541 {
542 bool first = 1;
543 for (IListIter<ParaStyleClass> iter(classes); !iter.done(); iter.next()) {
544 if (first)
545 first = 0;
546 else
547 os << ", ";
548 os << "DIV.";
549 iter.cur()->outputName(os);
550 }
551 if (!first) {
552 os << " {" << RE;
553 static const char indent[] = " ";
554 if (leftMargin) {
555 os << indent << "margin-left: ";
556 outputLength(leftMargin, os);
557 os << ';' << RE;
558 }
559 if (rightMargin) {
560 os << indent << "margin-right: ";
561 outputLength(rightMargin, os);
562 os << ';' << RE;
563 }
564 if (topMargin) {
565 os << indent << "margin-top: ";
566 outputLength(topMargin, os);
567 os << ';' << RE;
568 }
569 static const char *alignNames[4] = {
570 "left", "center", "right", "justify"
571 };
572 os << indent << "text-align: " << alignNames[align] << ';' << RE;
573 os << indent << "line-height: ";
574 outputLength(lineHeight, os);
575 os << ';' << RE;
576 os << indent << "text-indent: ";
577 outputLength(textIndent, os);
578 os << ';' << RE;
579 os << '}' << RE;
580 }
581 }
582
583 void HtmlFOTBuilder::outputLength(FOTBuilder::Length n, OutputCharStream &os)
584 {
585 char buf[32];
586 sprintf(buf, "%04ld", long(n));
587 char *end = strchr(buf, '\0');
588 for (int i = 0; i < 4; i++)
589 end[1 - i] = end[-i];
590 end[-3] = '.';
591 while (*end == '0')
592 *end-- = '\0';
593 if (*end == '.')
594 *end = '\0';
595 strcat(buf, "pt");
596 os << buf;
597 }
598
599 void HtmlFOTBuilder::start()
600 {
601 flowObjectStack_.push_back(nextFlowObject_);
602 }
603
604 void HtmlFOTBuilder::end()
605 {
606 flowObjectStack_.resize(flowObjectStack_.size() - 1);
607 nextFlowObject_ = flowObjectStack_.back();
608 }
609
610 void HtmlFOTBuilder::atomic()
611 {
612 nextFlowObject_ = flowObjectStack_.back();
613 }
614
615 void HtmlFOTBuilder::formattingInstruction(const StringC &s)
616 {
617 dest_->insert(new Markup(s));
618 atomic();
619 }
620
621 const HtmlFOTBuilder::CharStyleClass *HtmlFOTBuilder::makeCharStyleClass()
622 {
623 CharStyle *style = charStyleTable_.lookup(nextFlowObject_);
624 if (!style) {
625 style = new CharStyle(nextFlowObject_);
626 charStyleTable_.insert(style);
627 }
628 for (IListIter<CharStyleClass> iter(style->classes); !iter.done(); iter.next())
629 if (iter.cur()->gi == giStack_.back())
630 return iter.cur();
631 ClassPrefix *prefix = makeClassPrefix(giStack_.back());
632 CharStyleClass *sc = new CharStyleClass(giStack_.back(), prefix, style);
633 style->classes.insert(sc);
634 return sc;
635 }
636
637 const HtmlFOTBuilder::ParaStyleClass *HtmlFOTBuilder::makeParaStyleClass()
638 {
639 ParaProps props(nextFlowObject_);
640 props.topMargin = topMargin_;
641 topMargin_ = 0;
642 ParaStyle *style = paraStyleTable_.lookup(props);
643 if (!style) {
644 style = new ParaStyle(props);
645 paraStyleTable_.insert(style);
646 }
647 for (IListIter<ParaStyleClass> iter(style->classes); !iter.done(); iter.next())
648 if (iter.cur()->gi == giStack_.back())
649 return iter.cur();
650 ClassPrefix *prefix = makeClassPrefix(giStack_.back());
651 ParaStyleClass *sc = new ParaStyleClass(giStack_.back(), prefix, style);
652 style->classes.insert(sc);
653 return sc;
654 }
655
656 HtmlFOTBuilder::StyleClass::StyleClass(const StringC &g, ClassPrefix *pfx)
657 : gi(g), prefix(pfx), prefixIndex(pfx->nCharClasses + pfx->nParaClasses + 1)
658 {
659
660
661 }
662
663 HtmlFOTBuilder::CharStyleClass::CharStyleClass(const StringC &g, ClassPrefix *pfx,
664 const CharStyle *s)
665 : StyleClass(g, pfx), style(s)
666 {
667 pfx->nCharClasses += 1;
668 }
669
670 HtmlFOTBuilder::ParaStyleClass::ParaStyleClass(const StringC &g, ClassPrefix *pfx,
671 const ParaStyle *s)
672 : StyleClass(g, pfx), style(s)
673 {
674 pfx->nParaClasses += 1;
675 }
676
677 HtmlFOTBuilder::ClassPrefix *HtmlFOTBuilder::makeClassPrefix(const StringC &gi)
678 {
679 // FIXME strip illegal characters
680 ClassPrefix *prefix = prefixTable_.lookup(gi);
681 if (!prefix) {
682 prefix = new ClassPrefix(gi);
683 prefixTable_.insert(prefix);
684 }
685 return prefix;
686 }
687
688 void HtmlFOTBuilder::charactersFromNode(const NodePtr &node, const Char *s, size_t n)
689 {
690 flushPendingAddresses();
691 dest_->insert(new NodePcdata(node, s, n, makeCharStyleClass()));
692 }
693
694 void HtmlFOTBuilder::characters(const Char *s, size_t n)
695 {
696 flushPendingAddresses();
697 dest_->insert(new ImmediatePcdata(s, n, makeCharStyleClass()));
698 }
699
700 void HtmlFOTBuilder::startParagraph(const ParagraphNIC &nic)
701 {
702 startDisplay(nic);
703 Block *block = new Block(makeParaStyleClass());
704 nextFlowObject_.parentLeftMargin += nextFlowObject_.leftMargin;
705 nextFlowObject_.parentRightMargin += nextFlowObject_.rightMargin;
706 nextFlowObject_.leftMargin = 0;
707 nextFlowObject_.rightMargin = 0;
708 dest_->insert(block);
709 destStack_.insert(new DestInfo(dest_));
710 dest_ = block->contentPtr();
711 start();
712 }
713
714 void HtmlFOTBuilder::endParagraph()
715 {
716 reverse(*dest_);
717 dest_ = destStack_.head()->list;
718 delete destStack_.get();
719 end();
720 endDisplay();
721 }
722
723 void HtmlFOTBuilder::startDisplay(const DisplayNIC &nic)
724 {
725 Length spaceBefore = nic.spaceBefore.nominal.length;
726 if (spaceBefore > topMargin_)
727 topMargin_ = spaceBefore;
728 spaceAfterStack_.push_back(nic.spaceAfter.nominal.length);
729 }
730
731 void HtmlFOTBuilder::endDisplay()
732 {
733 const Length &spaceAfter = spaceAfterStack_.back();
734 if (spaceAfter > topMargin_)
735 topMargin_ = spaceAfter;
736 spaceAfterStack_.resize(spaceAfterStack_.size() - 1);
737 }
738
739 void HtmlFOTBuilder::startScroll()
740 {
741 nextFlowObject_.docIndex = nDocuments_++;
742 start();
743 Document *doc = new Document(nextFlowObject_.docIndex,
744 nextFlowObject_.scrollTitle);
745 dest_->insert(doc);
746 destStack_.insert(new DestInfo(dest_));
747 dest_ = doc->contentPtr();
748 if (pendingAddr_.size()) {
749 Vector<size_t> &v = pendingAddr_.back();
750 for (size_t i = 0; i < v.size(); i += 2) {
751 Addressable *tem = elementAddress(v[i], v[i + 1]);
752 if (!tem->defined()) {
753 dest_->insert(tem);
754 tem->setDefined(nextFlowObject_.docIndex, 1);
755 }
756 }
757 v.resize(0);
758 }
759 }
760
761 void HtmlFOTBuilder::endScroll()
762 {
763 reverse(*dest_);
764 dest_ = destStack_.head()->list;
765 delete destStack_.get();
766 end();
767 }
768
769 void HtmlFOTBuilder::startLink(const Address &addr)
770 {
771 start();
772 AddressRef *aref = 0;
773 switch (addr.type) {
774 case Address::resolvedNode:
775 {
776 unsigned long n;
777 if (addr.node->elementIndex(n) == accessOK) {
778 Addressable *tem = elementAddress(addr.node->groveIndex(), n);
779 tem->setReferenced();
780 aref = tem;
781 }
782 break;
783 }
784 case Address::idref:
785 {
786 const StringC &id = addr.params[0];
787 size_t i;
788 for (i = 0; i < id.size(); i++)
789 if (id[i] == ' ')
790 break;
791 NodePtr node;
792 NamedNodeListPtr elements;
793 unsigned long n;
794 if (addr.node->getGroveRoot(node) == accessOK
795 && node->getElements(elements) == accessOK
796 && elements->namedNode(GroveString(id.data(), i), node) == accessOK
797 && node->elementIndex(n) == accessOK) {
798 Addressable *tem = elementAddress(addr.node->groveIndex(), n);
799 tem->setReferenced();
800 aref = tem;
801 }
802 break;
803 }
804 case Address::html:
805 uriRefs_.insert(new UriRefAddressRef(addr.params[0]));
806 aref = uriRefs_.head();
807 break;
808 default:
809 break;
810 }
811 Ref *ref = new Ref(aref);
812 dest_->insert(ref);
813 destStack_.insert(new DestInfo(dest_));
814 dest_ = ref->contentPtr();
815 }
816
817 void HtmlFOTBuilder::endLink()
818 {
819 reverse(*dest_);
820 dest_ = destStack_.head()->list;
821 delete destStack_.get();
822 end();
823 }
824
825 HtmlFOTBuilder::Addressable *HtmlFOTBuilder::elementAddress(size_t g, size_t e)
826 {
827 if (g >= elements_.size())
828 elements_.resize(g + 1);
829 Vector<Addressable *> &v = elements_[g];
830 for (size_t i = v.size(); i <= e; i++)
831 v.push_back((Addressable *)0);
832 if (!v[e])
833 v[e] = new Addressable(g, e);
834 return v[e];
835 }
836
837 void HtmlFOTBuilder::startNode(const NodePtr &node, const StringC &mode)
838 {
839 pendingAddr_.resize(pendingAddr_.size() + 1);
840 if (mode.size() == 0) {
841 if (pendingAddr_.size() > 1)
842 pendingAddr_.back() = pendingAddr_[pendingAddr_.size() - 2];
843 unsigned long n;
844 if (node->elementIndex(n) == accessOK) {
845 pendingAddr_.back().push_back(node->groveIndex());
846 pendingAddr_.back().push_back(size_t(n));
847 }
848 }
849 giStack_.resize(giStack_.size() + 1);
850 GroveString str;
851 if (node->getGi(str) == accessOK)
852 giStack_.back().assign(str.data(), str.size());
853 else if (giStack_.size() >= 2)
854 giStack_.back() = giStack_[giStack_.size() - 2];
855 }
856
857 void HtmlFOTBuilder::endNode()
858 {
859 pendingAddr_.resize(pendingAddr_.size() - 1);
860 giStack_.resize(giStack_.size() - 1);
861 }
862
863 void HtmlFOTBuilder::insertAddr(size_t g, size_t e)
864 {
865 Addressable *tem = elementAddress(g, e);
866 if (!tem->defined() && nextFlowObject_.docIndex != (unsigned)-1) {
867 dest_->insert(tem);
868 tem->setDefined(nextFlowObject_.docIndex);
869 }
870 }
871
872 void HtmlFOTBuilder::setLineSpacing(const LengthSpec &val)
873 {
874 nextFlowObject_.lineHeight = val.length;
875 }
876
877 void HtmlFOTBuilder::setQuadding(Symbol sym)
878 {
879 switch (sym) {
880 case symbolStart:
881 nextFlowObject_.align = ParaProps::alignLeft;
882 break;
883 case symbolEnd:
884 nextFlowObject_.align = ParaProps::alignRight;
885 break;
886 case symbolCenter:
887 nextFlowObject_.align = ParaProps::alignCenter;
888 break;
889 case symbolJustify:
890 nextFlowObject_.align = ParaProps::alignJustify;
891 break;
892 default:
893 break;
894 }
895 }
896
897 void HtmlFOTBuilder::setFirstLineStartIndent(const LengthSpec &ls)
898 {
899 nextFlowObject_.textIndent = ls.length;
900 }
901
902 void HtmlFOTBuilder::setStartIndent(const LengthSpec &ls)
903 {
904 nextFlowObject_.leftMargin = ls.length - nextFlowObject_.parentLeftMargin;
905 }
906
907 void HtmlFOTBuilder::setEndIndent(const LengthSpec &ls)
908 {
909 nextFlowObject_.rightMargin = ls.length - nextFlowObject_.parentRightMargin;
910 }
911
912 void HtmlFOTBuilder::setFontFamilyName(const StringC &name)
913 {
914 nextFlowObject_.fontFamily = name;
915 }
916
917 void HtmlFOTBuilder::setFontWeight(Symbol weight)
918 {
919 switch (weight) {
920 case symbolUltraLight:
921 nextFlowObject_.fontWeight = 1;
922 break;
923 case symbolExtraLight:
924 nextFlowObject_.fontWeight = 2;
925 break;
926 case symbolLight:
927 nextFlowObject_.fontWeight = 3;
928 break;
929 case symbolSemiLight:
930 nextFlowObject_.fontWeight = 4;
931 break;
932 case symbolMedium:
933 nextFlowObject_.fontWeight = 5;
934 break;
935 case symbolSemiBold:
936 nextFlowObject_.fontWeight = 6;
937 break;
938 case symbolBold:
939 nextFlowObject_.fontWeight = 7;
940 break;
941 case symbolExtraBold:
942 nextFlowObject_.fontWeight = 8;
943 break;
944 case symbolUltraBold:
945 nextFlowObject_.fontWeight = 9;
946 break;
947 default:
948 break;
949 }
950 }
951
952 void HtmlFOTBuilder::setFontPosture(Symbol posture)
953 {
954 switch (posture) {
955 case symbolOblique:
956 nextFlowObject_.fontStyle = CharProps::styleOblique;
957 break;
958 case symbolItalic:
959 nextFlowObject_.fontStyle = CharProps::styleItalic;
960 break;
961 default:
962 nextFlowObject_.fontStyle = CharProps::styleNormal;
963 break;
964 }
965 }
966
967 void HtmlFOTBuilder::setFontSize(Length n)
968 {
969 nextFlowObject_.fontSize = n;
970 }
971
972 void HtmlFOTBuilder::setColor(const DeviceRGBColor &color)
973 {
974 nextFlowObject_.color = (color.red << 16) | (color.green << 8) | color.blue;
975 }
976
977 void HtmlFOTBuilder::setCharMap(Symbol sym)
978 {
979 switch(sym) {
980 case symbolFalse:
981 nextFlowObject_.textTransform = CharProps::ttNone;
982 break;
983 case symbolUppercase:
984 nextFlowObject_.textTransform = CharProps::ttUppercase;
985 break;
986 case symbolLowercase:
987 nextFlowObject_.textTransform = CharProps::ttLowercase;
988 break;
989 case symbolCapitalize:
990 nextFlowObject_.textTransform = CharProps::ttCapitalize;
991 break;
992 default:
993 CANNOT_HAPPEN();
994 }
995 }
996
997 void HtmlFOTBuilder::setScrollTitle(const StringC &s)
998 {
999 nextFlowObject_.scrollTitle = new StringResource<Char>(s);
1000 }
1001
1002 void HtmlFOTBuilder::flushPendingAddresses()
1003 {
1004 if (pendingAddr_.size()) {
1005 for (size_t i = 0; i < pendingAddr_.back().size(); i += 2)
1006 insertAddr(pendingAddr_.back()[i], pendingAddr_.back()[i + 1]);
1007 pendingAddr_.back().resize(0);
1008 }
1009 }
1010
1011 void HtmlFOTBuilder::outputCdata(const Char *s, size_t n, OutputCharStream &os)
1012 {
1013 for (; n > 0; n--, s++) {
1014 switch (*s) {
1015 case '\n':
1016 break;
1017 case '&':
1018 os << "&amp;";
1019 break;
1020 case '<':
1021 os << "&lt;";
1022 break;
1023 case '>':
1024 os << "&gt;";
1025 break;
1026 case '\r':
1027 os << RE;
1028 break;
1029 default:
1030 if (*s >= 255)
1031 os << "&#" << (unsigned long)*s << ';';
1032 else
1033 os.put(*s);
1034 break;
1035 }
1036 }
1037 }
1038
1039 void HtmlFOTBuilder::Document::output(OutputCharStream &, OutputState &oldState)
1040 {
1041 OutputState state(&oldState.outputFilename(),
1042 oldState.styleSheetFilename(),
1043 &oldState.app());
1044 FileOutputByteStream file;
1045 String<CmdLineApp::AppChar> filename(state.outputFilename(index_));
1046 filename += 0;
1047 if (!file.open(filename.data())) {
1048 state.messenger()
1049 .message(HtmlMessages::cannotOpenOutputError,
1050 StringMessageArg(state.app().convertInput(filename.data())),
1051 ErrnoMessageArg(errno));
1052 return;
1053 }
1054 RecordOutputCharStream os(new EncodeOutputCharStream(&file,
1055 state.app().outputCodingSystem()));
1056 // FIXME setEscaper
1057 unsigned oldDocIndex = state.setOutputDocIndex(index_);
1058 os << "<HTML>" << RE;
1059 const StringC &styleSheetFilename = state.styleSheetFilename();
1060 if (styleSheetFilename.size()) {
1061 os << "<LINK REL=STYLESHEET TYPE=\"text/css\" HREF=\"";
1062 size_t i = styleSheetFilename.size();
1063 for (; i > 0; i--)
1064 if (styleSheetFilename[i - 1] == '\\' || styleSheetFilename[i - 1] == '/')
1065 break;
1066 os.write(styleSheetFilename.data() + i, styleSheetFilename.size() - i);
1067 os << "\">" << RE;
1068 }
1069 if (!title_.isNull()) {
1070 os << "<TITLE>";
1071 HtmlFOTBuilder::outputCdata(*title_, os);
1072 os << "</TITLE>";
1073 }
1074 os << "<BODY>" << RE;
1075 Container::output(os, state);
1076 os << "</BODY>" << RE;
1077 os << "</HTML>" << RE;
1078 state.setOutputDocIndex(oldDocIndex);
1079 }
1080
1081 void HtmlFOTBuilder::Container::output(OutputCharStream &os, OutputState &state)
1082 {
1083 for (IListIter<Item> iter(content_); !iter.done(); iter.next())
1084 iter.cur()->output(os, state);
1085 }
1086
1087 void HtmlFOTBuilder::Block::output(OutputCharStream &os, OutputState &state)
1088 {
1089 const AddressRef *addr = state.setAddress(0);
1090 state.syncChar(0, os);
1091 os << "<DIV CLASS=";
1092 styleClass_->outputName(os);
1093 os << RE <<'>';
1094 state.setAddress(addr);
1095 Container::output(os, state);
1096 state.setAddress(0);
1097 state.syncChar(0, os);
1098 os << "</DIV>";
1099 state.setAddress(addr);
1100 }
1101
1102 void HtmlFOTBuilder::Ref::output(OutputCharStream &os, OutputState &state)
1103 {
1104 const AddressRef *oldAref = state.setAddress(aref_);
1105 Container::output(os, state);
1106 state.setAddress(oldAref);
1107 }
1108
1109 void HtmlFOTBuilder::Pcdata::output(OutputCharStream &os, OutputState &state)
1110 {
1111 size_t n;
1112 const Char *s = data(n);
1113 state.syncChar(styleClass_, os);
1114 HtmlFOTBuilder::outputCdata(s, n, os);
1115 }
1116
1117 void HtmlFOTBuilder::Markup::output(OutputCharStream &os, OutputState &state)
1118 {
1119 state.syncChar(0, os);
1120 os << str_;
1121 }
1122
1123 void HtmlFOTBuilder::Addressable::output(OutputCharStream &os, OutputState &)
1124 {
1125 ASSERT(defined());
1126 if (referenced_ && elementIndex_ != (unsigned)-1) {
1127 os << "<A NAME=";
1128 if (groveIndex_)
1129 os << (unsigned long)groveIndex_ << '.';
1130 os << (unsigned long)elementIndex_ << "></A>";
1131 }
1132 }
1133
1134 void HtmlFOTBuilder::Addressable::outputRef(bool end, OutputCharStream &os,
1135 OutputState &state) const
1136 {
1137 ASSERT(referenced_);
1138 if (defined()) {
1139 if (end)
1140 os << "</A>";
1141 else {
1142 os << "<A HREF=\"";
1143 if (state.outputDocIndex() != docIndex_ || elementIndex_ == (unsigned)-1) {
1144 const String<CmdLineApp::AppChar> &outputFilename = state.outputFilename(docIndex_);
1145 size_t i = outputFilename.size();
1146 for (; i > 0; i--)
1147 if (outputFilename[i - 1] == '\\' || outputFilename[i - 1] == '/')
1148 break;
1149 for (; i < outputFilename.size(); i++)
1150 os.put(outputFilename[i]);
1151 }
1152 if (elementIndex_ != (unsigned)-1) {
1153 os << "#";
1154 if (groveIndex_)
1155 os << (unsigned long)groveIndex_ << '.';
1156 os << (unsigned long)elementIndex_;
1157 }
1158 os << "\">";
1159 }
1160 }
1161 }
1162
1163 String<CmdLineApp::AppChar> HtmlFOTBuilder::OutputState::outputFilename(unsigned index) const
1164 {
1165 if (index == 0)
1166 return *outputFilename_;
1167 String<CmdLineApp::AppChar> base(*outputFilename_);
1168 String<CmdLineApp::AppChar> ext;
1169 for (size_t j = 0; j < 5; j++) {
1170 if (base.size() < j + 1)
1171 break;
1172 Char c = base[base.size() - j - 1];
1173 if (c == '/' || c == '\\')
1174 break;
1175 if (c == '.') {
1176 ext.assign(base.data() + (base.size() - j - 1), j + 1);
1177 base.resize(base.size() - j - 1);
1178 break;
1179 }
1180 }
1181 char buf[32];
1182 sprintf(buf, "%u", index);
1183 for (const char *p = buf; *p; p++)
1184 base += CmdLineApp::AppChar(*p);
1185 base += ext;
1186 return base;
1187 }
1188
1189 void HtmlFOTBuilder::OutputState::syncChar(const CharStyleClass *styleClass,
1190 OutputCharStream &os)
1191 {
1192 // <A><SPAN>...</SPAN></A>
1193 if (curAref_ != specAref_) {
1194 if (curCharStyleClass_) {
1195 os << "</SPAN>";
1196 curCharStyleClass_ = 0;
1197 }
1198 if (curAref_)
1199 curAref_->outputRef(1, os, *this);
1200 if (specAref_)
1201 specAref_->outputRef(0, os, *this);
1202 curAref_ = specAref_;
1203 }
1204 if (curCharStyleClass_ != styleClass) {
1205 if (curCharStyleClass_)
1206 os << "</SPAN>";
1207 if (styleClass) {
1208 os << "<SPAN CLASS=";
1209 styleClass->outputName(os);
1210 os << '>';
1211 }
1212 curCharStyleClass_ = styleClass;
1213 }
1214 }
1215
1216 HtmlFOTBuilder::AddressRef::~AddressRef()
1217 {
1218 }
1219
1220 void HtmlFOTBuilder::UriRefAddressRef::outputRef(bool end, OutputCharStream &os,
1221 OutputState &) const
1222 {
1223 if (end)
1224 os << "</A>";
1225 else
1226 os << "<A HREF=\"" << uriRef_ << "\">";
1227 }
1228
1229 HtmlFOTBuilder::Item::~Item()
1230 {
1231 }
1232
1233 HtmlFOTBuilder::Document::~Document()
1234 {
1235 }
1236
1237 HtmlFOTBuilder::CharProps::CharProps()
1238 : fontWeight(5), color(0), fontStyle(styleNormal), fontSize(10*1000),
1239 textTransform(ttNone)
1240 {
1241 for (const char *p = "Times New Roman,serif"; *p; p++)
1242 fontFamily += *p;
1243 }
1244
1245 HtmlFOTBuilder::InheritParaProps::InheritParaProps()
1246 : leftMargin(0), rightMargin(0), align(alignLeft), lineHeight(12*1000),
1247 textIndent(0)
1248 {
1249 }
1250
1251 HtmlFOTBuilder::FlowObjectInfo::FlowObjectInfo()
1252 : docIndex(unsigned(-1)), parentLeftMargin(0), parentRightMargin(0)
1253 {
1254 }
1255
1256 void HtmlFOTBuilder::StyleClass::outputName(OutputCharStream &os) const
1257 {
1258 os << prefix->prefix;
1259 if (prefix->nCharClasses > 1 || prefix->nParaClasses > 1)
1260 os << (unsigned long)prefixIndex;
1261 }
1262
1263 #ifdef DSSSL_NAMESPACE
1264 }
1265 #endif
1266
1267 #include "HtmlFOTBuilder_inst.cxx"
1268
1269 #endif /* JADE_HTML */