| Flutter Impeller |

text_frame.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/typographer/text_frame.h"
6
7 namespace impeller {
8
9 TextFrame::TextFrame() = default;
10
11 TextFrame::TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color)
12 : runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {}
13
14 TextFrame::~TextFrame() = default;
15
16 Rect TextFrame::GetBounds() const {
17 return bounds_;
18 }
19
20 size_t TextFrame::GetRunCount() const {
21 return runs_.size();
22 }
23
24 const std::vector<TextRun>& TextFrame::GetRuns() const {
25 return runs_;
26 }
27
28 GlyphAtlas::Type TextFrame::GetAtlasType() const {
29 return has_color_ ? GlyphAtlas::Type::kColorBitmap
30 : GlyphAtlas::Type::kAlphaBitmap;
31 }
32
33 bool TextFrame::MaybeHasOverlapping() const {
34 if (runs_.size() > 1) {
35 return true;
36 }
37 auto glyph_positions = runs_[0].GetGlyphPositions();
38 if (glyph_positions.size() > 10) {
39 return true;
40 }
41 if (glyph_positions.size() == 1) {
42 return false;
43 }
44 // To avoid quadradic behavior the overlapping is checked against an
45 // accumulated bounds rect. This gives faster but less precise information
46 // on text runs.
47 auto first_position = glyph_positions[0];
48 auto overlapping_rect = Rect::MakeOriginSize(
49 first_position.position + first_position.glyph.bounds.GetOrigin(),
50 first_position.glyph.bounds.GetSize());
51 for (auto i = 1u; i < glyph_positions.size(); i++) {
52 auto glyph_position = glyph_positions[i];
53 auto glyph_rect = Rect::MakeOriginSize(
54 glyph_position.position + glyph_position.glyph.bounds.GetOrigin(),
55 glyph_position.glyph.bounds.GetSize());
56 auto intersection = glyph_rect.Intersection(overlapping_rect);
57 if (intersection.has_value()) {
58 return true;
59 }
60 overlapping_rect = overlapping_rect.Union(glyph_rect);
61 }
62 return false;
63 }
64
65 // static
66 Scalar TextFrame::RoundScaledFontSize(Scalar scale, Scalar point_size) {
67 return std::round(scale * 100) / 100;
68 }
69
70 void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,
71 Scalar scale) const {
72 for (const TextRun& run : GetRuns()) {
73 const Font& font = run.GetFont();
74 auto rounded_scale =
75 RoundScaledFontSize(scale, font.GetMetrics().point_size);
76 auto& set = glyph_map[{font, rounded_scale}];
77 for (const TextRun::GlyphPosition& glyph_position :
78 run.GetGlyphPositions()) {
79 #if false
80 // Glyph size error due to RoundScaledFontSize usage above.
81 if (rounded_scale != scale) {
82 auto delta = std::abs(rounded_scale - scale);
83 FML_LOG(ERROR) << glyph_position.glyph.bounds.size * delta;
84 }
85 #endif
86 set.insert(glyph_position.glyph);
87 }
88 }
89 }
90
91 } // namespace impeller
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:22
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::TextFrame::TextFrame
TextFrame()
impeller::TextFrame::~TextFrame
~TextFrame()
impeller::TextFrame::CollectUniqueFontGlyphPairs
void CollectUniqueFontGlyphPairs(FontGlyphMap &glyph_map, Scalar scale) const
Definition: text_frame.cc:70
impeller::FontGlyphMap
std::unordered_map< ScaledFont, std::unordered_set< Glyph > > FontGlyphMap
Definition: font_glyph_pair.h:29
impeller::TextFrame::GetRuns
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:24
impeller::TextFrame::GetRunCount
size_t GetRunCount() const
The number of runs in this text frame.
Definition: text_frame.cc:20
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:66
impeller::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
impeller::TextFrame::MaybeHasOverlapping
bool MaybeHasOverlapping() const
Whether any of the glyphs of this run are potentially overlapping.
Definition: text_frame.cc:33
impeller::TRect< Scalar >::MakeOriginSize
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:38
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:33
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:35
impeller::Font::GetMetrics
const Metrics & GetMetrics() const
Definition: font.cc:37
impeller::TextFrame::GetBounds
Rect GetBounds() const
The conservative bounding box for this text frame.
Definition: text_frame.cc:16
std
Definition: comparable.h:95
impeller::TextFrame::GetAtlasType
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be emplaced in.
Definition: text_frame.cc:28
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
text_frame.h
impeller
Definition: aiks_context.cc:10
impeller::TRect< Scalar >