1 /*
2 * Copyright (C) 2017 Google, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "styled_runs.h"
18
19 #include "flutter/fml/logging.h"
20 #include "utils/WindowsUtils.h"
21
22 namespace txt {
23
24 StyledRuns::StyledRuns() = default;
25
26 StyledRuns::~StyledRuns() = default;
27
StyledRuns(StyledRuns && other)28 StyledRuns::StyledRuns(StyledRuns&& other) {
29 styles_.swap(other.styles_);
30 runs_.swap(other.runs_);
31 }
32
operator =(StyledRuns && other)33 const StyledRuns& StyledRuns::operator=(StyledRuns&& other) {
34 styles_.swap(other.styles_);
35 runs_.swap(other.runs_);
36 return *this;
37 }
38
swap(StyledRuns & other)39 void StyledRuns::swap(StyledRuns& other) {
40 styles_.swap(other.styles_);
41 runs_.swap(other.runs_);
42 }
43
AddStyle(const TextStyle & style)44 size_t StyledRuns::AddStyle(const TextStyle& style) {
45 const size_t style_index = styles_.size();
46 styles_.push_back(style);
47 return style_index;
48 }
49
GetStyle(size_t style_index) const50 const TextStyle& StyledRuns::GetStyle(size_t style_index) const {
51 return styles_[style_index];
52 }
53
StartRun(size_t style_index,size_t start)54 void StyledRuns::StartRun(size_t style_index, size_t start) {
55 EndRunIfNeeded(start);
56 runs_.push_back(IndexedRun{style_index, start, start});
57 }
58
EndRunIfNeeded(size_t end)59 void StyledRuns::EndRunIfNeeded(size_t end) {
60 if (runs_.empty())
61 return;
62 IndexedRun& run = runs_.back();
63 if (run.start == end) {
64 // The run is empty. We can skip it.
65 runs_.pop_back();
66 } else {
67 run.end = end;
68 }
69 }
70
GetRun(size_t index) const71 StyledRuns::Run StyledRuns::GetRun(size_t index) const {
72 const IndexedRun& run = runs_[index];
73 return Run{styles_[run.style_index], run.start, run.end};
74 }
75
76 } // namespace txt
77