• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • khtml
  • platform
  • graphics
FloatRect.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "FloatRect.h"
29
30#include "FloatConversion.h"
31#include "IntRect.h"
32#include <algorithm>
33
34using std::max;
35using std::min;
36
37namespace WebCore {
38
39FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
40{
41}
42
43FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
44{
45 return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
46}
47
48bool FloatRect::intersects(const FloatRect& other) const
49{
50 // Checking emptiness handles negative widths as well as zero.
51 return !isEmpty() && !other.isEmpty()
52 && x() < other.right() && other.x() < right()
53 && y() < other.bottom() && other.y() < bottom();
54}
55
56bool FloatRect::contains(const FloatRect& other) const
57{
58 return x() <= other.x() && right() >= other.right()
59 && y() <= other.y() && bottom() >= other.bottom();
60}
61
62void FloatRect::intersect(const FloatRect& other)
63{
64 float l = max(x(), other.x());
65 float t = max(y(), other.y());
66 float r = min(right(), other.right());
67 float b = min(bottom(), other.bottom());
68
69 // Return a clean empty rectangle for non-intersecting cases.
70 if (l >= r || t >= b) {
71 l = 0;
72 t = 0;
73 r = 0;
74 b = 0;
75 }
76
77 m_location.setX(l);
78 m_location.setY(t);
79 m_size.setWidth(r - l);
80 m_size.setHeight(b - t);
81}
82
83void FloatRect::unite(const FloatRect& other)
84{
85 // Handle empty special cases first.
86 if (other.isEmpty())
87 return;
88 if (isEmpty()) {
89 *this = other;
90 return;
91 }
92
93 float l = min(x(), other.x());
94 float t = min(y(), other.y());
95 float r = max(right(), other.right());
96 float b = max(bottom(), other.bottom());
97
98 m_location.setX(l);
99 m_location.setY(t);
100 m_size.setWidth(r - l);
101 m_size.setHeight(b - t);
102}
103
104void FloatRect::scale(float s)
105{
106 m_location.setX(x() * s);
107 m_location.setY(y() * s);
108 m_size.setWidth(width() * s);
109 m_size.setHeight(height() * s);
110}
111
112IntRect enclosingIntRect(const FloatRect& rect)
113{
114 int l = static_cast<int>(rect.x());
115 int t = static_cast<int>(rect.y());
116 // FIXME: These two need to be a "ceiling" operation, not rounding.
117 // We changed them to do "+ 0.5f" to compile on Win32 where there's
118 // no ceilf, but they should be changed back to "ceiling" at some point
119 // and we should provide an implementation of ceilf for Win32.
120 int r = static_cast<int>(rect.right() + 0.5f);
121 int b = static_cast<int>(rect.bottom() + 0.5f);
122 return IntRect(l, t, r - l, b - t);
123}
124
125}
FloatConversion.h
FloatRect.h
IntRect.h
WebCore::FloatPoint::setY
void setY(float y)
Definition: FloatPoint.h:73
WebCore::FloatPoint::setX
void setX(float x)
Definition: FloatPoint.h:72
WebCore::FloatRect
Definition: FloatRect.h:59
WebCore::FloatRect::width
float width() const
Definition: FloatRect.h:78
WebCore::FloatRect::intersects
bool intersects(const FloatRect &) const
Definition: FloatRect.cpp:48
WebCore::FloatRect::right
float right() const
Definition: FloatRect.h:88
WebCore::FloatRect::narrowPrecision
static FloatRect narrowPrecision(double x, double y, double width, double height)
Definition: FloatRect.cpp:43
WebCore::FloatRect::unite
void unite(const FloatRect &)
Definition: FloatRect.cpp:83
WebCore::FloatRect::isEmpty
bool isEmpty() const
Definition: FloatRect.h:86
WebCore::FloatRect::height
float height() const
Definition: FloatRect.h:79
WebCore::FloatRect::bottom
float bottom() const
Definition: FloatRect.h:89
WebCore::FloatRect::y
float y() const
Definition: FloatRect.h:77
WebCore::FloatRect::x
float x() const
Definition: FloatRect.h:76
WebCore::FloatRect::FloatRect
FloatRect()
Definition: FloatRect.h:61
WebCore::FloatRect::intersect
void intersect(const FloatRect &)
Definition: FloatRect.cpp:62
WebCore::FloatRect::scale
void scale(float s)
Definition: FloatRect.cpp:104
WebCore::FloatRect::contains
bool contains(const FloatRect &) const
Definition: FloatRect.cpp:56
WebCore::FloatSize::setWidth
void setWidth(float width)
Definition: FloatSize.h:59
WebCore::FloatSize::setHeight
void setHeight(float height)
Definition: FloatSize.h:60
WebCore::IntRect
Definition: IntRect.h:65
WebCore
Definition: CSSHelper.h:7
WebCore::narrowPrecisionToFloat
float narrowPrecisionToFloat(T)
WebCore::enclosingIntRect
IntRect enclosingIntRect(const FloatRect &rect)
Definition: FloatRect.cpp:112
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal