File: page_boundary_intersect.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (87 lines) | stat: -rw-r--r-- 3,372 bytes parent folder | download | duplicates (9)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "pdf/draw_utils/page_boundary_intersect.h"

#include "base/check.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"

namespace chrome_pdf {

namespace {

// gfx::Rect considers points on its bottom and right border to be not contained
// within the rect. So to make sure CalculatePageBoundaryIntersectPoint()
// returns points within the page, use this constant to move them inwards a bit.
constexpr float kBoundaryEpsilon = 0.0001f;

}  // namespace

gfx::PointF CalculatePageBoundaryIntersectPoint(
    const gfx::Rect& page_rect,
    const gfx::PointF& inside_point,
    const gfx::PointF& outside_point) {
  const gfx::RectF rect(page_rect);
  CHECK(!rect.IsEmpty());
  CHECK(rect.Contains(inside_point));
  CHECK(!rect.Contains(outside_point));

  const float x_diff = outside_point.x() - inside_point.x();
  const float y_diff = outside_point.y() - inside_point.y();
  // Handle the special case where calculating the slope would divide by 0.
  if (x_diff == 0) {
    if (y_diff > 0) {
      return {inside_point.x(), rect.bottom() - kBoundaryEpsilon};
    }
    return {inside_point.x(), rect.y()};
  }

  // Handle the special case where dividing by the slope would be dividing by 0.
  if (y_diff == 0) {
    if (x_diff > 0) {
      return {rect.right() - kBoundaryEpsilon, inside_point.y()};
    }
    return {rect.x(), inside_point.y()};
  }

  // For all other cases, calculate where the line between `inside_point` and
  // `outside_point` would intersect `rect` on its horizontal and vertical
  // boundaries, assuming the line going towards `outside_point` is infinitely
  // long in that direction, and the boundary lines also extends infinitely.
  const float slope = y_diff / x_diff;
  gfx::PointF left_or_right_boundary_intersection_point;
  if (x_diff > 0) {
    float y_distance = (rect.right() - inside_point.x()) * slope;
    left_or_right_boundary_intersection_point = {
        rect.right() - kBoundaryEpsilon, inside_point.y() + y_distance};
  } else {
    float y_distance = (inside_point.x() - rect.x()) * slope;
    left_or_right_boundary_intersection_point = {rect.x(),
                                                 inside_point.y() - y_distance};
  }

  gfx::PointF top_or_bottom_boundary_intersection_point;
  if (y_diff > 0) {
    float x_distance = (rect.bottom() - inside_point.y()) / slope;
    top_or_bottom_boundary_intersection_point = {
        inside_point.x() + x_distance, rect.bottom() - kBoundaryEpsilon};
  } else {
    float x_distance = (inside_point.y() - rect.y()) / slope;
    top_or_bottom_boundary_intersection_point = {inside_point.x() - x_distance,
                                                 rect.y()};
  }

  // Then return the result closest to `inside_point`.
  float left_or_right_point_length =
      (left_or_right_boundary_intersection_point - inside_point).Length();
  float top_or_bottom_point_length =
      (top_or_bottom_boundary_intersection_point - inside_point).Length();
  return left_or_right_point_length < top_or_bottom_point_length
             ? left_or_right_boundary_intersection_point
             : top_or_bottom_boundary_intersection_point;
}

}  // namespace chrome_pdf