File: helper.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (298 lines) | stat: -rw-r--r-- 9,808 bytes parent folder | download | duplicates (4)
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
/*
 * This file is part of the CSS implementation for KDE.
 *
 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
 *           (C) David Carson  <dacarson@gmail.com>
 *
 * 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 "helper.h"
#include "khtmllayout.h"
#include <QtCore/QMap>
#include <QtGui/QPainter>
#include <dom/dom_string.h>
#include <xml/dom_stringimpl.h>
#include <rendering/render_object.h>
#include <kconfig.h>
#include <ksharedconfig.h>
#include <kconfiggroup.h>
#include <kglobalsettings.h>
#include <QtGui/QToolTip>
#include "css/cssvalues.h"

using namespace DOM;
using namespace khtml;

namespace khtml {

QPainter *printpainter;

void setPrintPainter( QPainter *printer )
{
    printpainter = printer;
}

void findWordBoundary(QChar *chars, int len, int position, int *start, int *end)
{
    if (chars[position].isSpace()) {
        int pos = position;
        while (pos >= 0 && chars[pos].isSpace())
            pos--;
        *start = pos+1;
        pos = position;
        while (pos < (int)len && chars[pos].isSpace())
            pos++;
        *end = pos;
    } else if (chars[position].isPunct()) {
        int pos = position;
        while (pos >= 0 && chars[pos].isPunct())
            pos--;
        *start = pos+1;
        pos = position;
        while (pos < (int)len && chars[pos].isPunct())
            pos++;
        *end = pos;
    } else {
        int pos = position;
        while (pos >= 0 && !chars[pos].isSpace() && !chars[pos].isPunct())
            pos--;
        *start = pos+1;
        pos = position;
        while (pos < (int)len && !chars[pos].isSpace() && !chars[pos].isPunct())
            pos++;
        *end = pos;
    }
}

}

// color mapping code
struct colorMap {
    int css_value;
    QRgb color;
};

static const colorMap cmap[] = {
    { CSS_VAL_AQUA, 0xFF00FFFF },
    { CSS_VAL_BLACK, 0xFF000000 },
    { CSS_VAL_BLUE, 0xFF0000FF },
    { CSS_VAL_CRIMSON, 0xFFDC143C },
    { CSS_VAL_FUCHSIA, 0xFFFF00FF },
    { CSS_VAL_GRAY, 0xFF808080 },
    { CSS_VAL_GREEN, 0xFF008000  },
    { CSS_VAL_INDIGO, 0xFF4B0082 },
    { CSS_VAL_LIME, 0xFF00FF00 },
    { CSS_VAL_MAROON, 0xFF800000 },
    { CSS_VAL_NAVY, 0xFF000080 },
    { CSS_VAL_OLIVE, 0xFF808000  },
    { CSS_VAL_ORANGE, 0xFFFFA500 },
    { CSS_VAL_PURPLE, 0xFF800080 },
    { CSS_VAL_RED, 0xFFFF0000 },
    { CSS_VAL_SILVER, 0xFFC0C0C0 },
    { CSS_VAL_TEAL, 0xFF008080  },
    { CSS_VAL_WHITE, 0xFFFFFFFF },
    { CSS_VAL_YELLOW, 0xFFFFFF00 },
    { CSS_VAL_TRANSPARENT, transparentColor },
    { CSS_VAL_GREY, 0xff808080 },
    { 0, 0 }
};

struct uiColors {
    int css_value;
    QPalette::ColorGroup group;
    QPalette::ColorRole role;
};

// CSS 2.1 system color mapping
static const uiColors uimap[] = {
    // MDI background color
    { CSS_VAL_APPWORKSPACE, QPalette::Normal, QPalette::Mid },
    // Button colors
    { CSS_VAL_BUTTONFACE, QPalette::Normal, QPalette::Button },
    { CSS_VAL_BUTTONHIGHLIGHT, QPalette::Normal, QPalette::Light },
    { CSS_VAL_BUTTONSHADOW, QPalette::Normal, QPalette::Dark },
    { CSS_VAL_BUTTONTEXT, QPalette::Normal, QPalette::ButtonText },
    // Disabled text
    { CSS_VAL_GRAYTEXT, QPalette::Disabled, QPalette::Text },
    // Selected items
    { CSS_VAL_HIGHLIGHTTEXT, QPalette::Normal, QPalette::HighlightedText },
    { CSS_VAL_HIGHLIGHT, QPalette::Normal, QPalette::Highlight },
    // Tooltips
    { CSS_VAL_INFOBACKGROUND, QPalette::Normal, QPalette::ToolTipBase },
    { CSS_VAL_INFOTEXT, QPalette::Normal, QPalette::ToolTipText },
    // Menu colors
    { CSS_VAL_MENU, QPalette::Normal, QPalette::Window },
    { CSS_VAL_MENUTEXT, QPalette::Normal, QPalette::Text },
    // Scroll bar color
    { CSS_VAL_SCROLLBAR, QPalette::Normal, QPalette::Window },
    // 3D elements 
    { CSS_VAL_THREEDDARKSHADOW, QPalette::Normal, QPalette::Dark },
    { CSS_VAL_THREEDFACE, QPalette::Normal, QPalette::Button },
    { CSS_VAL_THREEDHIGHLIGHT, QPalette::Normal, QPalette::Light },
    { CSS_VAL_THREEDLIGHTSHADOW, QPalette::Normal, QPalette::Midlight },
    { CSS_VAL_THREEDSHADOW, QPalette::Normal, QPalette::Mid },
    // Window background
    { CSS_VAL_WINDOW, QPalette::Normal, QPalette::Base },
    // Window frame
    { CSS_VAL_WINDOWFRAME, QPalette::Normal, QPalette::Window },
    // WindowText
    { CSS_VAL_WINDOWTEXT, QPalette::Normal, QPalette::Text },
    { CSS_VAL_TEXT, QPalette::Normal, QPalette::Text },
    { 0, QPalette::NColorGroups, QPalette::NColorRoles }
};

QColor khtml::colorForCSSValue( int css_value )
{
    // try the regular ones first
    const colorMap *col = cmap;
    while ( col->css_value && col->css_value != css_value )
	++col;
    if ( col->css_value )
	return QColor::fromRgba(col->color);
    else if ( css_value == CSS_VAL_INVERT )
        return QColor();

    const uiColors *uicol = uimap;
    while ( uicol->css_value && uicol->css_value != css_value )
	++uicol;
#ifndef APPLE_CHANGES
    if ( !uicol->css_value ) {
        switch ( css_value ) {
            case CSS_VAL_ACTIVEBORDER:
                return qApp->palette().color(QPalette::Normal, QPalette::Window);
            case CSS_VAL_ACTIVECAPTION:
                return KGlobalSettings::activeTitleColor();
            case CSS_VAL_CAPTIONTEXT:
                return KGlobalSettings::activeTextColor();
            case CSS_VAL_INACTIVEBORDER:
                return qApp->palette().color(QPalette::Inactive, QPalette::Window);
            case CSS_VAL_INACTIVECAPTION:
                return KGlobalSettings::inactiveTitleColor();
            case CSS_VAL_INACTIVECAPTIONTEXT:
                return KGlobalSettings::inactiveTextColor();
            case CSS_VAL_BACKGROUND: // Desktop background - no way to get this information from Plasma
                return qApp->palette().color(QPalette::Normal, QPalette::Highlight);
            default:
	        return QColor();
        }
    }
#endif

    const QPalette &pal = qApp->palette();
    return pal.color( uicol->group, uicol->role );
}


double calcHue(double temp1, double temp2, double hueVal)
{
    if (hueVal < 0)
        hueVal++;
    else if (hueVal > 1)
        hueVal--;
    if (hueVal * 6 < 1)
        return temp1 + (temp2 - temp1) * hueVal * 6;
    if (hueVal * 2 < 1)
        return temp2;
    if (hueVal * 3 < 2)
        return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6;
    return temp1;
}

// Explanation of this algorithm can be found in the CSS3 Color Module
// specification at http://www.w3.org/TR/css3-color/#hsl-color with further
// explanation available at http://en.wikipedia.org/wiki/HSL_color_space

// all values are in the range of 0 to 1.0
QRgb khtml::qRgbaFromHsla(double h, double s, double l, double a)
{
    double temp2 = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
    double temp1 = 2.0 * l - temp2;

    return qRgba(static_cast<int>(calcHue(temp1, temp2, h + 1.0 / 3.0) * 255),
                 static_cast<int>(calcHue(temp1, temp2, h) * 255),
                 static_cast<int>(calcHue(temp1, temp2, h - 1.0 / 3.0) * 255),
                 static_cast<int>(a * 255));
}

/** finds out the background color of an element
 * @param obj render object
 * @return the background color. It is guaranteed that a valid color is returned.
 */
QColor khtml::retrieveBackgroundColor(const RenderObject *obj)
{
  QColor result;
  while (!obj->isCanvas()) {
    result = obj->style()->backgroundColor();
    if (result.isValid()) return result;

    obj = obj->container();
  }/*wend*/

  // everything transparent? Use base then.
  return obj->style()->palette().color( QPalette::Active, QPalette::Base );
}

/** checks whether the given colors have enough contrast
 * @returns @p true if contrast is ok.
 */
bool khtml::hasSufficientContrast(const QColor &c1, const QColor &c2)
{
// New version from Germain Garand, better suited for contrast measurement
#if 1

#define HUE_DISTANCE 40
#define CONTRAST_DISTANCE 10

  int h1, s1, v1, h2, s2, v2;
  int hdist = -CONTRAST_DISTANCE;
  c1.getHsv(&h1,&s1,&v1);
  c2.getHsv(&h2,&s2,&v2);
  if(h1!=-1 && h2!=-1) { // grey values have no hue
      hdist = qAbs(h1-h2);
      if (hdist > 180) hdist = 360-hdist;
      if (hdist < HUE_DISTANCE) {
          hdist -= HUE_DISTANCE;
          // see if they are high key or low key colours
          bool hk1 = h1>=45 && h1<=225;
          bool hk2 = h2>=45 && h2<=225;
          if (hk1 && hk2)
              hdist = (5*hdist)/3;
          else if (!hk1 && !hk2)
              hdist = (7*hdist)/4;
      }
      hdist = qMin(hdist, HUE_DISTANCE*2);
  }
  return hdist + (qAbs(s1-s2)*128)/(160+qMin(s1,s2)) + qAbs(v1-v2) > CONTRAST_DISTANCE;

#undef CONTRAST_DISTANCE
#undef HUE_DISTANCE

#else	// orginal fast but primitive version by me (LS)

// ### arbitrary value, to be adapted if necessary (LS)
#define CONTRAST_DISTANCE 32

  if (qAbs(c1.Qt::red() - c2.Qt::red()) > CONTRAST_DISTANCE) return true;
  if (qAbs(c1.Qt::green() - c2.Qt::green()) > CONTRAST_DISTANCE) return true;
  if (qAbs(c1.Qt::blue() - c2.Qt::blue()) > CONTRAST_DISTANCE) return true;

  return false;

#undef CONTRAST_DISTANCE

#endif
}