summaryrefslogtreecommitdiffstats
path: root/filters/kword/html/export/ExportCss.cc
blob: 7af3dcdff8ad224080a45c4f04c31ecd02d8d52d (plain)
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
/*
   This file is part of the KDE project
   Copyright (C) 2001, 2002, 2004 Nicolas GOUTTE <goutte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqstring.h>
#include <tqtextcodec.h>
#include <tqfile.h>

#include <klocale.h>
#include <kdebug.h>

#include <KWEFUtil.h>
#include <KWEFBaseWorker.h>

#include "ExportFilter.h"
#include "ExportCss.h"

TQString HtmlCssWorker::escapeCssIdentifier(const TQString& strText) const
{
    // Reference: section 4.1.3 of the CSS2 recommendation
    // However most HTML user agents support this section only in a restrictive way, so we cannot use any CSS escape

    // NOTE: we do not guarantee anymore that the style name is unique! (### FIXME)

    TQString strReturn;

    // Taken in the restrictive way, an identifier can only start with a letter.
    const TQChar qch0(strText[0]);
    if (((int)qch0<'a' || (int)qch0>'z') && ((int)qch0<'A' || (int)qch0>'Z'))
    {
        // Not a letter, so we have to add a prefix
        strReturn+="kWoRd_"; // The curious spelling is for allowing a HTML import to identfy it and to remove it.
        // The processing of the character itself is done below
    }

    for (uint i=0; i<strText.length(); i++)
    {
        const TQChar qch(strText.at(i));
        const ushort ch=qch.unicode();

        if (((ch>='a') && (ch<='z'))
            || ((ch>='A') && (ch<='Z'))
            || ((ch>='0') && (ch<='9'))
            || (ch=='-') || (ch=='_')) // The underscore is allowed by the CSS2 errata
        {
            // Normal allowed characters (without any problem)
            strReturn+=qch;
        }
        else if ((ch<=' ') || (ch>=128 && ch<=160)) // space (breaking or not) and control characters
        {
            // CSS2 would allow to escape it but not any HTML user agent supports this
            strReturn+='_';
        }
        else if ((ch>=161) && (getCodec()->canEncode(qch)))
        {
            // Any Unicode character greater or egual to 161 is allowed
            // except if it cannot be written in the encoding
            strReturn+=qch;
        }
        else // if ch >= 33 && ch <=127 with holes (or not in encoding)
        {
            // Either CSS2 does not allow this character unescaped or it is not in the encoding
            // but a CSS escape would break some HTML user agents (e.g. Mozilla 1.4)
            // So we have to do our own incompatible cooking. :-(
            strReturn+="--"; // start our private escape
            strReturn+=TQString::number(ch,16);
            strReturn+="--"; // end our private escape
        }
    }
    return strReturn;
}

TQString HtmlCssWorker::textFormatToCss(const TextFormatting& formatOrigin,
    const TextFormatting& formatData, const bool force) const
{
    // TODO: as this method comes from the AbiWord filter,
    // TODO:   verify that it is working for HTML

    // TODO: rename variable formatData
    TQString strElement; // TODO: rename this variable

    // Font name
    TQString fontName = formatData.fontName;
    if (!fontName.isEmpty()
        && (force || (formatOrigin.fontName!=formatData.fontName)))
    {
        strElement+="font-family: ";
        if (fontName.find(' ')==-1)
            strElement+=escapeHtmlText(fontName);
        else
        {   // If the font name contains a space, it should be quoted.
            strElement+='\'';
            strElement+=escapeHtmlText(fontName);
            strElement+='\'';
        }
        // ### TODO: add alternative font names
        strElement+="; ";
    }

    if (force || (formatOrigin.italic!=formatData.italic))
    {
        // Font style
        strElement+="font-style: ";
        if ( formatData.italic )
        {
            strElement+="italic";
        }
        else
        {
            strElement+="normal";
        }
        strElement+="; ";
    }

    if (force || ((formatOrigin.weight>=75)!=(formatData.weight>=75)))
    {
        strElement+="font-weight: ";
        if ( formatData.weight >= 75 )
        {
            strElement+="bold";
        }
        else
        {
            strElement+="normal";
        }
        strElement+="; ";
    }

    if (force || (formatOrigin.fontSize!=formatData.fontSize))
    {
        const int size=formatData.fontSize;
        if (size>0)
        {
            // We use absolute font sizes.
            strElement+="font-size: ";
            strElement+=TQString::number(size,10);
            strElement+="pt; ";
        }
    }

    if (force || (formatOrigin.fgColor!=formatData.fgColor))
    {
        if ( formatData.fgColor.isValid() )
        {
            // Give colour
            strElement+="color: ";
            strElement+=formatData.fgColor.name();
            strElement+="; ";
        }
    }

    if (force || (formatOrigin.bgColor!=formatData.bgColor))
    {
        if ( formatData.bgColor.isValid() )
        {
            // Give background colour
            strElement+="background-color: ";
            strElement+=formatData.bgColor.name();
            strElement+="; ";
        }
    }

    if (force || (formatOrigin.underline!=formatData.underline)
        || (formatOrigin.strikeout!=formatData.strikeout))
    {
        strElement+="text-decoration: ";
        if ( formatData.underline )
        {
            strElement+="underline";
        }
        else if ( formatData.strikeout )
        {
            strElement+="line-through";
        }
        else
        {
            strElement+="none";
        }
        strElement+="; ";
    }

    if (force || (formatOrigin.fontAttribute!=formatData.fontAttribute))
    {
        bool smallcaps=false;
        strElement+="text-transform: ";
        if ( formatData.fontAttribute=="uppercase" )
        {
            strElement+="uppercase";
        }
        else if ( formatData.fontAttribute=="lowercase" )
        {
            strElement+="lowercase";
        }
        else if ( formatData.fontAttribute=="smallcaps" )
        {
            strElement+="none";
            smallcaps=true;
        }
        else
        {
            strElement+="none";
        }
        strElement+="; ";
        // ### TODO: mostly issuing font-variant is not necessary.
        strElement+="font-variant:";
        if (smallcaps)
            strElement+="small-caps";
        else
            strElement+="normal";
        strElement+="; ";
    }

    // TODO: As this is the last property, do not put a semi-colon

    return strElement;
}

TQString HtmlCssWorker::getStartOfListOpeningTag(const CounterData::Style typeList, bool& ordered)
{
    TQString strResult;
    switch (typeList)
    {
    case CounterData::STYLE_CUSTOMBULLET: // We cannot keep the custom type/style
    default:
        {
            ordered=false;
            strResult="<ul>\n";
            break;
        }
    case CounterData::STYLE_NONE:
        {
            ordered=false;
            strResult="<ul style=\"list-style-type:none\">\n";
            break;
        }
    case CounterData::STYLE_CIRCLEBULLET:
        {
            ordered=false;
            strResult="<ul style=\"list-style-type:circle\">\n";
            break;
        }
    case CounterData::STYLE_SQUAREBULLET:
        {
            ordered=false;
            strResult="<ul style=\"list-style-type:square\">\n";
            break;
        }
    case CounterData::STYLE_DISCBULLET:
        {
            ordered=false;
            strResult="<ul style=\"list-style-type:disc\">\n";
            break;
        }
    case CounterData::STYLE_NUM:
        {
            ordered=true;
            strResult="<ol style=\"list-style-type:decimal\">\n";
            break;
        }
    case CounterData::STYLE_ALPHAB_L:
        {
            ordered=true;
            strResult="<ol style=\"list-style-type:lower-alpha\">\n";
            break;
        }
    case CounterData::STYLE_ALPHAB_U:
        {
            ordered=true;
            strResult="<ol style=\"list-style-type:upper-alpha\">\n";
            break;
        }
    case CounterData::STYLE_ROM_NUM_L:
        {
            ordered=true;
            strResult="<ol style=\"list-style-type:lower-roman\">\n";
            break;
        }
    case CounterData::STYLE_ROM_NUM_U:
        {
            ordered=true;
            strResult="<ol style=\"list-style-type:upper-roman\">\n";
            break;
        }
    case CounterData::STYLE_CUSTOM:
        {
            // We cannot keep the custom type/style
            ordered=true;
            strResult="<ol>\n";
            break;
        }
    }
    return strResult;
}

TQString HtmlCssWorker::layoutToCss(const LayoutData& layoutOrigin,
    const LayoutData& layout, const bool force) const
{
    TQString strLayout;

    if (force || (layoutOrigin.alignment!=layout.alignment))
    {
        if ( (layout.alignment=="left") || (layout.alignment== "right")
            || (layout.alignment=="center") || (layout.alignment=="justify"))
        {
            strLayout += TQString("text-align:%1; ").arg(layout.alignment);
        }
        else if ( layout.alignment=="auto")
        {
            // Do nothing, the user-agent should be more intelligent than us.
        }
        else
        {
            kdWarning(30503) << "Unknown alignment: " << layout.alignment << endl;
        }
    }

    if ((layout.indentLeft>=0.0)
        && (force || (layoutOrigin.indentLeft!=layout.indentLeft)))
    {
            strLayout += TQString("margin-left:%1pt; ").arg(layout.indentLeft);
    }

    if ((layout.indentRight>=0.0)
        && (force || (layoutOrigin.indentRight!=layout.indentRight)))
    {
        strLayout += TQString("margin-right:%1pt; ").arg(layout.indentRight);
    }

    if (force || (layoutOrigin.indentLeft!=layout.indentLeft))
    {
        strLayout += TQString("text-indent:%1pt; ").arg(layout.indentFirst);
    }

    if ((layout.marginBottom>=0.0)
        && ( force || ( layoutOrigin.marginBottom != layout.marginBottom ) ) )
    {
       strLayout += TQString("margin-bottom:%1pt; ").arg(layout.marginBottom);
    }

    if ((layout.marginTop>=0.0)
        && ( force || ( layoutOrigin.marginTop != layout.marginTop ) ) )
    {
       strLayout += TQString("margin-top:%1pt; ").arg(layout.marginTop);
    }

    if (force
        || ( layoutOrigin.lineSpacingType != layout.lineSpacingType )
        || ( layoutOrigin.lineSpacing != layout.lineSpacing ) )
    {
        switch ( layout.lineSpacingType )
        {
        case LayoutData::LS_CUSTOM:
            { 
                // ### TODO: CSS 2 does not known "at-least".
#if 0
                // We have a custom line spacing (in points)
                const TQString height ( TQString::number(layout.lineSpacing) ); // ### TODO: rounding?
                strLayout += "style:line-spacing:";
                strLayout += height;
                strLayout += "pt; ";
#endif
                break;          
            }
        case LayoutData::LS_SINGLE:
            {
                strLayout += "line-height:normal; "; // One
                break;
            }
        case LayoutData::LS_ONEANDHALF:
            {
                strLayout += "line-height:150%; "; // One-and-half
                break;
            }
        case LayoutData::LS_DOUBLE:
            {
                strLayout += "line-height:200%; "; // Two
                break;
            }
        case LayoutData::LS_MULTIPLE:
            {
                const TQString mult ( TQString::number( tqRound( layout.lineSpacing * 100 ) ) );
                strLayout += "line-height:";
                strLayout += mult;
                strLayout += "%; ";
                break;
            }
        case LayoutData::LS_FIXED:
            {
                // We have a fixed line height (in points)
                const TQString height ( TQString::number(layout.lineSpacing) ); // ### TODO: rounding?
                strLayout += "line-height:";
                strLayout += height;
                strLayout += "pt; ";
                break;
            }
        case LayoutData::LS_ATLEAST:
            {
                // ### TODO: CSS 2 does not known "at-least".
                // ### TODO:  however draft CCS3 (module 'line') has 'line-stacking-strategy' to tweak this behaviour
                // We have a at-least line height (in points)
                const TQString height ( TQString::number(layout.lineSpacing) ); // ### TODO: rounding?
                strLayout += "line-height:";
                strLayout += height;
                strLayout += "pt; ";
                break;
            }
        default:
            {
                kdWarning(30503) << "Unsupported lineSpacingType: " << layout.lineSpacingType << " (Ignoring!)" << endl;
                break;
            }
        }
    }

    // TODO: Konqueror/KHTML does not support "text-shadow"
    if (!force
        && ( layoutOrigin.shadowDirection == layout.shadowDirection )
        && ( layoutOrigin.shadowDistance == layout.shadowDistance ) )
    {
        // Do nothing!
    }
    else if ((!layout.shadowDirection) || (!layout.shadowDistance))
    {
        strLayout += "text-shadow:";
        strLayout+="none; ";
    }
    else
    {
        double xDistance,yDistance;
        const double distance=layout.shadowDistance;
        switch (layout.shadowDirection)
        {
        case 1: // SD_LEFT_UP
            {
                xDistance= (-distance);
                yDistance= (-distance);
                break;
            }
        case 2: // SD_UP
            {
                xDistance= 0;
                yDistance= (-distance);
                break;
            }
        case 3: // SD_RIGHT_UP
            {
                xDistance= (distance);
                yDistance= (-distance);
                break;
            }
        case 4: // SD_RIGHT
            {
                xDistance= (distance);
                yDistance= 0;
                break;
            }
        case 5: // SD_RIGHT_BOTTOM
            {
                xDistance= (distance);
                yDistance= (distance);
                break;
            }
        case 6: // SD_BOTTOM
            {
                xDistance= 0;
                yDistance= (distance);
                break;
            }
        case 7: // SD_LEFT_BOTTOM
            {
                xDistance= (-distance);
                yDistance= (distance);
                break;
            }
        case 8: // SD_LEFT
            {
                xDistance= (distance);
                yDistance= 0;
                break;
            }
        default:
            {
                xDistance=0;
                yDistance=0;
                break;
            }
        }
        if ( (!xDistance) && (!yDistance) )
        {
            strLayout += "text-shadow:";
            strLayout+="none; ";
        }
        else
        {
            strLayout += "text-shadow:";
            strLayout+=TQString("%1 %2pt %3pt; ").arg(layout.shadowColor.name())
                .arg(xDistance,0,'f',0).arg(yDistance,0,'f',0);
                // We do not want any scientific notation or any decimal
        }
    }

    // TODO: borders

    // This must remain last, as the last property does not have a semi-colon
    strLayout+=textFormatToCss(layoutOrigin.formatData.text,
        layout.formatData.text,force);

    return strLayout;
}

void HtmlCssWorker::openParagraph(const TQString& strTag,
    const LayoutData& layout, TQChar::Direction direction)
{
    const LayoutData& styleLayout=m_styleMap[layout.styleName];

    *m_streamOut << '<' << strTag;

    // Opening elements
    *m_streamOut << " class=\"" << escapeCssIdentifier(layout.styleName);
    *m_streamOut << "\"";

    TQString strStyle=layoutToCss(styleLayout,layout,false);
    if (!strStyle.isEmpty())
    {
        *m_streamOut << " style=\"" << strStyle;
        if (direction == TQChar::DirRLE) {
            *m_streamOut << "direction: rtl; unicode-bidi: embed; ";
        } else if (direction == TQChar::DirRLO) {
            *m_streamOut << "direction: rtl; unicode-bidi: override; ";
        }
        *m_streamOut<< "\"";
    }

    *m_streamOut << ">";

    if ( 1==layout.formatData.text.verticalAlignment )
    {
        *m_streamOut << "<sub>"; //Subscript
    }
    else if ( 2==layout.formatData.text.verticalAlignment )
    {
        *m_streamOut << "<sup>"; //Superscript
    }
    if ( layout.alignment == "center" ) *m_streamOut << "<center>";
}

void HtmlCssWorker::closeParagraph(const TQString& strTag,
    const LayoutData& layout)
{
    if ( 2==layout.formatData.text.verticalAlignment )
    {
        *m_streamOut << "</sup>"; //Superscript
    }
    else if ( 1==layout.formatData.text.verticalAlignment )
    {
        *m_streamOut << "</sub>"; //Subscript
    }

    if ( layout.alignment == "center" ) *m_streamOut << "</center>";
    *m_streamOut << "</" << strTag << ">\n";
}

void HtmlCssWorker::openSpan(const FormatData& formatOrigin, const FormatData& format)
{
    *m_streamOut << "<span style=\"";
    *m_streamOut << textFormatToCss(formatOrigin.text,format.text,false);
    *m_streamOut << "\">"; // close span opening tag

    if ( 1==format.text.verticalAlignment )
    {
        *m_streamOut << "<sub>"; //Subscript
    }
    else if ( 2==format.text.verticalAlignment )
    {
        *m_streamOut << "<sup>"; //Superscript
    }
}

void HtmlCssWorker::closeSpan(const FormatData& formatOrigin, const FormatData& format)
{
    if ( 2==format.text.verticalAlignment )
    {
        *m_streamOut << "</sup>"; //Superscript
    }
    else if ( 1==format.text.verticalAlignment )
    {
        *m_streamOut << "</sub>"; //Subscript
    }

    *m_streamOut << "</span>";
}

bool HtmlCssWorker::doFullPaperFormat(const int format,
            const double width, const double height, const int orientation)
{
    TQString strWidth, strHeight, strUnits;
    KWEFUtil::GetNativePaperFormat(format, strWidth, strHeight, strUnits);

    if ((strWidth.isEmpty())||(strHeight.isEmpty())||(strUnits.isEmpty()))
    {
        // page format is unknown, so we need the size information
        strUnits="pt";
        strWidth=TQString::number(width);
        strHeight=TQString::number(height);
    }
    if (orientation==1)
    {
        // Landscape, so we must swap the sizes
        TQString strTemp(strWidth);
        strWidth=strHeight;
        strHeight=strTemp;
    }

    m_strPageSize="size: ";
    m_strPageSize+=strWidth;
    m_strPageSize+=strUnits;
    m_strPageSize+=" ";
    m_strPageSize+=strHeight;
    m_strPageSize+=strUnits;
    m_strPageSize+=";";
    return true;
}

bool HtmlCssWorker::doFullPaperBorders (const double top, const double left,
    const double bottom, const double right)
{
    m_strPaperBorders="  margin-top: ";
    m_strPaperBorders+=TQString::number(top);
    m_strPaperBorders+="pt;\n";
    m_strPaperBorders+="  margin-left: ";
    m_strPaperBorders+=TQString::number(left);
    m_strPaperBorders+="pt;\n";
    m_strPaperBorders+="  margin-bottom: ";
    m_strPaperBorders+=TQString::number(bottom);
    m_strPaperBorders+="pt;\n";
    m_strPaperBorders+="  margin-right: ";
    m_strPaperBorders+=TQString::number(right);
    m_strPaperBorders+="pt;\n";

    return true;
}

bool HtmlCssWorker::doOpenStyles(void)
{
    *m_streamOut << "<style type=\"text/css\">\n";
    if (!isXML())
    {
        // Put the style under comment to increase the compatibility with old browsers
        // However in XHTML 1.0, you cannot put the style definition into HTML comments
        *m_streamOut << "<!--\n";
    }

    // Say who we are (with the CVS revision number)
    const TQString strVersion("$Revision: 483471 $");
    // Eliminate the dollar signs
    //  (We don't want that the version number changes if the HTML file is itself put in a CVS storage.)
    *m_streamOut << "/* KWORD_CSS_EXPORT ="
              << strVersion.mid(10).remove('$')
              << "*/\n";

    // TODO: does KWord gives a paper colour?
    *m_streamOut << "BODY\n{\n  background-color: #FFFFFF\n}\n";

    return true;
}

bool HtmlCssWorker::doFullDefineStyle(LayoutData& layout)
{
    //Register style in the style map
    m_styleMap[layout.styleName]=layout;

    // We do not limit (anymore) any style to <h1> ... <h6>, because
    //   the style could be forced on <p> by the layout.

    *m_streamOut << "." << escapeCssIdentifier(layout.styleName);
    *m_streamOut << "\n{\n  " << layoutToCss(layout,layout,true) << "\n}\n";

    return true;
}

bool HtmlCssWorker::doCloseStyles(void)
{
    if (!m_strPageSize.isEmpty())
    {
        *m_streamOut << "@page\n{\n  ";
        *m_streamOut << m_strPageSize;
        *m_streamOut << "\n";
        *m_streamOut << m_strPaperBorders; // ends with a LF
        *m_streamOut << "}\n";
    }

    if (!isXML())
    {
        // Put the style under comment to increase the compatibility with old browsers
        // However in XHTML 1.0, you cannot put the style definition into HTML comments
        *m_streamOut << "-->\n";
    }
    *m_streamOut << "</style>\n";

    return true;
}