1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "tools/render/table.h"
16
17 namespace quic_trace {
18 namespace render {
19
Table(const ProgramState * state,TextRenderer * text_factory,RectangleRenderer * rectangles)20 Table::Table(const ProgramState* state,
21 TextRenderer* text_factory,
22 RectangleRenderer* rectangles)
23 : state_(state), text_renderer_(text_factory), rectangles_(rectangles) {}
24
AddHeader(const std::string & text)25 void Table::AddHeader(const std::string& text) {
26 rows_.push_back(Row{/*.header=*/text_renderer_->RenderTextBold(text),
27 /*.label=*/nullptr,
28 /*.value=*/nullptr});
29 }
30
AddRow(const std::string & label,const std::string & value)31 void Table::AddRow(const std::string& label, const std::string& value) {
32 rows_.push_back(Row{
33 /*.header=*/nullptr,
34 /*.label=*/text_renderer_->RenderTextBold(label),
35 /*.value=*/text_renderer_->RenderText(value),
36 });
37 }
38
Layout()39 vec2 Table::Layout() {
40 spacing_ = state_->ScaleForDpi(8);
41
42 int y_offset = 0;
43 width_ = height_ = 0;
44 for (Row& row : rows_) {
45 int row_width = 2 * spacing_;
46 int row_height = spacing_;
47 if (row.header != nullptr) {
48 row_width += row.header->width();
49 row_height += row.header->height();
50
51 // Add extra margin for all headers except the first one.
52 if (y_offset > 0) {
53 y_offset += spacing_ / 2;
54 }
55 } else {
56 // A label-value combination
57 row_width += row.label->width() + spacing_ + row.value->width();
58 row_height += std::max(row.label->height(), row.value->height());
59 }
60 width_ = std::max(row_width, width_);
61
62 row.y_offset = y_offset;
63 row.height = row_height;
64 y_offset += row_height;
65 }
66 height_ = y_offset + spacing_;
67 return vec2(width_, height_);
68 }
69
Draw(vec2 position)70 void Table::Draw(vec2 position) {
71 vec2 base_offset = position + vec2(spacing_, spacing_);
72 rectangles_->AddRectangleWithBorder(Box{position, vec2(width_, height_)},
73 0xffffffff);
74 for (const Row& row : rows_) {
75 vec2 row_offset =
76 base_offset + vec2(0, height_ - row.y_offset - row.height - spacing_);
77 if (row.header != nullptr) {
78 row_offset += vec2((width_ - spacing_ * 2 - row.header->width()) / 2, 0);
79 text_renderer_->AddText(row.header, row_offset.x, row_offset.y);
80 } else {
81 text_renderer_->AddText(row.label, row_offset.x, row_offset.y);
82 text_renderer_->AddText(row.value,
83 row_offset.x + row.label->width() + spacing_,
84 row_offset.y);
85 }
86 }
87 }
88
89 } // namespace render
90 } // namespace quic_trace
91