1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CANVAS_TEXT_METRICS_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CANVAS_TEXT_METRICS_H_
28 
29 #include "third_party/blink/renderer/bindings/core/v8/v8_baselines.h"
30 #include "third_party/blink/renderer/core/core_export.h"
31 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
32 #include "third_party/blink/renderer/platform/fonts/font.h"
33 #include "third_party/blink/renderer/platform/graphics/graphics_types.h"
34 #include "third_party/blink/renderer/platform/heap/handle.h"
35 
36 namespace blink {
37 
38 class CORE_EXPORT TextMetrics final : public ScriptWrappable {
39   DEFINE_WRAPPERTYPEINFO();
40 
41  public:
42   TextMetrics();
43   TextMetrics(const Font& font,
44               const TextDirection& direction,
45               const TextBaseline& baseline,
46               const TextAlign& align,
47               const String& text);
48 
width()49   double width() const { return width_; }
advances()50   const Vector<double>& advances() const { return advances_; }
actualBoundingBoxLeft()51   double actualBoundingBoxLeft() const { return actual_bounding_box_left_; }
actualBoundingBoxRight()52   double actualBoundingBoxRight() const { return actual_bounding_box_right_; }
fontBoundingBoxAscent()53   double fontBoundingBoxAscent() const { return font_bounding_box_ascent_; }
fontBoundingBoxDescent()54   double fontBoundingBoxDescent() const { return font_bounding_box_descent_; }
actualBoundingBoxAscent()55   double actualBoundingBoxAscent() const { return actual_bounding_box_ascent_; }
actualBoundingBoxDescent()56   double actualBoundingBoxDescent() const {
57     return actual_bounding_box_descent_;
58   }
emHeightAscent()59   double emHeightAscent() const { return em_height_ascent_; }
emHeightDescent()60   double emHeightDescent() const { return em_height_descent_; }
getBaselines()61   Baselines* getBaselines() const { return baselines_; }
62 
63   static float GetFontBaseline(const TextBaseline&, const SimpleFontData&);
64 
65   void Trace(Visitor*) override;
66 
67  private:
68   void Update(const Font&,
69               const TextDirection&,
70               const TextBaseline&,
71               const TextAlign&,
72               const String&);
73 
74   // x-direction
75   double width_ = 0.0;
76   Vector<double> advances_;
77   double actual_bounding_box_left_ = 0.0;
78   double actual_bounding_box_right_ = 0.0;
79 
80   // y-direction
81   double font_bounding_box_ascent_ = 0.0;
82   double font_bounding_box_descent_ = 0.0;
83   double actual_bounding_box_ascent_ = 0.0;
84   double actual_bounding_box_descent_ = 0.0;
85   double em_height_ascent_ = 0.0;
86   double em_height_descent_ = 0.0;
87   Member<Baselines> baselines_;
88 };
89 
90 }  // namespace blink
91 
92 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CANVAS_TEXT_METRICS_H_
93