tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsPoint.h (3454B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef NSPOINT_H
      8 #define NSPOINT_H
      9 
     10 #include <cstdint>
     11 #include "nsCoord.h"
     12 #include "mozilla/gfx/BasePoint.h"
     13 #include "mozilla/gfx/Point.h"
     14 
     15 // nsIntPoint represents a point in one of the types of pixels.
     16 // Uses of nsIntPoint should eventually be converted to CSSIntPoint,
     17 // LayoutDeviceIntPoint, etc. (see layout/base/Units.h).
     18 typedef mozilla::gfx::IntPoint nsIntPoint;
     19 
     20 // nsPoint represents a point in app units.
     21 
     22 struct nsPoint : public mozilla::gfx::BasePoint<nscoord, nsPoint> {
     23  typedef mozilla::gfx::BasePoint<nscoord, nsPoint> Super;
     24 
     25  nsPoint() = default;
     26  nsPoint(const nsPoint& aPoint) = default;
     27  nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {}
     28 
     29  inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale,
     30                                         nscoord aAppUnitsPerPixel) const;
     31  inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const;
     32 
     33  /**
     34   * Return this point scaled to a different appunits per pixel (APP) ratio.
     35   * @param aFromAPP the APP to scale from
     36   * @param aToAPP the APP to scale to
     37   */
     38  [[nodiscard]] inline nsPoint ScaleToOtherAppUnits(int32_t aFromAPP,
     39                                                    int32_t aToAPP) const;
     40 
     41  [[nodiscard]] inline nsPoint RemoveResolution(const float resolution) const;
     42  [[nodiscard]] inline nsPoint ApplyResolution(const float resolution) const;
     43 };
     44 
     45 inline nsPoint ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel);
     46 
     47 inline nsIntPoint nsPoint::ScaleToNearestPixels(
     48    float aXScale, float aYScale, nscoord aAppUnitsPerPixel) const {
     49  return nsIntPoint(
     50      NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale),
     51      NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale));
     52 }
     53 
     54 inline nsIntPoint nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const {
     55  return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
     56 }
     57 
     58 inline nsPoint nsPoint::ScaleToOtherAppUnits(int32_t aFromAPP,
     59                                             int32_t aToAPP) const {
     60  if (aFromAPP != aToAPP) {
     61    nsPoint point;
     62    point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP));
     63    point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP));
     64    return point;
     65  }
     66  return *this;
     67 }
     68 
     69 inline nsPoint nsPoint::RemoveResolution(const float resolution) const {
     70  if (resolution != 1.0f) {
     71    nsPoint point;
     72    point.x = NSToCoordRound(NSCoordToFloat(x) / resolution);
     73    point.y = NSToCoordRound(NSCoordToFloat(y) / resolution);
     74    return point;
     75  }
     76  return *this;
     77 }
     78 
     79 inline nsPoint nsPoint::ApplyResolution(const float resolution) const {
     80  if (resolution != 1.0f) {
     81    nsPoint point;
     82    point.x = NSToCoordRound(NSCoordToFloat(x) * resolution);
     83    point.y = NSToCoordRound(NSCoordToFloat(y) * resolution);
     84    return point;
     85  }
     86  return *this;
     87 }
     88 
     89 // app units are integer multiples of pixels, so no rounding needed
     90 inline nsPoint ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel) {
     91  return nsPoint(NSIntPixelsToAppUnits(aPoint.x, aAppUnitsPerPixel),
     92                 NSIntPixelsToAppUnits(aPoint.y, aAppUnitsPerPixel));
     93 }
     94 
     95 #endif /* NSPOINT_H */