| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium 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 "printing/page_overlays.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/string_util.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "printing/printed_document.h" | |
| 12 #include "printing/printed_page.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Replaces a subpart of a string by other value, and returns the position right | |
| 17 // after the new value. | |
| 18 size_t ReplaceKey(std::wstring* string, | |
| 19 size_t offset, | |
| 20 size_t old_string_len, | |
| 21 const std::wstring& new_string) { | |
| 22 string->replace(offset, old_string_len, new_string); | |
| 23 return offset + new_string.size(); | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 namespace printing { | |
| 29 | |
| 30 const wchar_t* const PageOverlays::kTitle = L"{title}"; | |
| 31 const wchar_t* const PageOverlays::kTime = L"{time}"; | |
| 32 const wchar_t* const PageOverlays::kDate = L"{date}"; | |
| 33 const wchar_t* const PageOverlays::kPage = L"{page}"; | |
| 34 const wchar_t* const PageOverlays::kPageCount = L"{pagecount}"; | |
| 35 const wchar_t* const PageOverlays::kPageOnTotal = L"{pageontotal}"; | |
| 36 const wchar_t* const PageOverlays::kUrl = L"{url}"; | |
| 37 | |
| 38 PageOverlays::PageOverlays() | |
| 39 : top_left(kDate), | |
| 40 top_center(kTitle), | |
| 41 top_right(), | |
| 42 bottom_left(kUrl), | |
| 43 bottom_center(), | |
| 44 bottom_right(kPageOnTotal) { | |
| 45 } | |
| 46 | |
| 47 PageOverlays::~PageOverlays() {} | |
| 48 | |
| 49 bool PageOverlays::Equals(const PageOverlays& rhs) const { | |
| 50 return top_left == rhs.top_left && | |
| 51 top_center == rhs.top_center && | |
| 52 top_right == rhs.top_right && | |
| 53 bottom_left == rhs.bottom_left && | |
| 54 bottom_center == rhs.bottom_center && | |
| 55 bottom_right == rhs.bottom_right; | |
| 56 } | |
| 57 | |
| 58 const std::wstring& PageOverlays::GetOverlay(HorizontalPosition x, | |
| 59 VerticalPosition y) const { | |
| 60 switch (x) { | |
| 61 case LEFT: | |
| 62 switch (y) { | |
| 63 case TOP: | |
| 64 return top_left; | |
| 65 case BOTTOM: | |
| 66 return bottom_left; | |
| 67 } | |
| 68 break; | |
| 69 case CENTER: | |
| 70 switch (y) { | |
| 71 case TOP: | |
| 72 return top_center; | |
| 73 case BOTTOM: | |
| 74 return bottom_center; | |
| 75 } | |
| 76 break; | |
| 77 case RIGHT: | |
| 78 switch (y) { | |
| 79 case TOP: | |
| 80 return top_right; | |
| 81 case BOTTOM: | |
| 82 return bottom_right; | |
| 83 } | |
| 84 break; | |
| 85 } | |
| 86 NOTREACHED(); | |
| 87 return EmptyWString(); | |
| 88 } | |
| 89 | |
| 90 void PageOverlays::SetOverlay(HorizontalPosition x, | |
| 91 VerticalPosition y, | |
| 92 const std::wstring& input) { | |
| 93 switch (x) { | |
| 94 case LEFT: | |
| 95 switch (y) { | |
| 96 case TOP: | |
| 97 top_left = input; | |
| 98 break; | |
| 99 case BOTTOM: | |
| 100 bottom_left = input; | |
| 101 break; | |
| 102 default: | |
| 103 NOTREACHED(); | |
| 104 break; | |
| 105 } | |
| 106 break; | |
| 107 case CENTER: | |
| 108 switch (y) { | |
| 109 case TOP: | |
| 110 top_center = input; | |
| 111 break; | |
| 112 case BOTTOM: | |
| 113 bottom_center = input; | |
| 114 break; | |
| 115 default: | |
| 116 NOTREACHED(); | |
| 117 break; | |
| 118 } | |
| 119 break; | |
| 120 case RIGHT: | |
| 121 switch (y) { | |
| 122 case TOP: | |
| 123 top_right = input; | |
| 124 break; | |
| 125 case BOTTOM: | |
| 126 bottom_right = input; | |
| 127 break; | |
| 128 default: | |
| 129 NOTREACHED(); | |
| 130 break; | |
| 131 } | |
| 132 break; | |
| 133 default: | |
| 134 NOTREACHED(); | |
| 135 break; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 std::wstring PageOverlays::ReplaceVariables(const std::wstring& input, | |
| 141 const PrintedDocument& document, | |
| 142 const PrintedPage& page) { | |
| 143 std::wstring output(input); | |
| 144 for (size_t offset = output.find(L'{', 0); | |
| 145 offset != std::wstring::npos; | |
| 146 offset = output.find(L'{', offset)) { | |
| 147 if (0 == output.compare(offset, | |
| 148 wcslen(kTitle), | |
| 149 kTitle)) { | |
| 150 offset = ReplaceKey(&output, | |
| 151 offset, | |
| 152 wcslen(kTitle), | |
| 153 UTF16ToWideHack(document.name())); | |
| 154 } else if (0 == output.compare(offset, | |
| 155 wcslen(kTime), | |
| 156 kTime)) { | |
| 157 offset = ReplaceKey(&output, | |
| 158 offset, | |
| 159 wcslen(kTime), | |
| 160 UTF16ToWideHack(document.time())); | |
| 161 } else if (0 == output.compare(offset, | |
| 162 wcslen(kDate), | |
| 163 kDate)) { | |
| 164 offset = ReplaceKey(&output, | |
| 165 offset, | |
| 166 wcslen(kDate), | |
| 167 UTF16ToWideHack(document.date())); | |
| 168 } else if (0 == output.compare(offset, | |
| 169 wcslen(kPage), | |
| 170 kPage)) { | |
| 171 offset = ReplaceKey(&output, | |
| 172 offset, | |
| 173 wcslen(kPage), | |
| 174 UTF8ToWide(base::IntToString(page.page_number()))); | |
| 175 } else if (0 == output.compare(offset, | |
| 176 wcslen(kPageCount), | |
| 177 kPageCount)) { | |
| 178 offset = ReplaceKey(&output, | |
| 179 offset, | |
| 180 wcslen(kPageCount), | |
| 181 UTF8ToWide(base::IntToString(document.page_count()))); | |
| 182 } else if (0 == output.compare(offset, | |
| 183 wcslen(kPageOnTotal), | |
| 184 kPageOnTotal)) { | |
| 185 std::wstring replacement; | |
| 186 replacement = UTF8ToWide(base::IntToString(page.page_number())); | |
| 187 replacement += L"/"; | |
| 188 replacement += UTF8ToWide(base::IntToString(document.page_count())); | |
| 189 offset = ReplaceKey(&output, | |
| 190 offset, | |
| 191 wcslen(kPageOnTotal), | |
| 192 replacement); | |
| 193 } else if (0 == output.compare(offset, | |
| 194 wcslen(kUrl), | |
| 195 kUrl)) { | |
| 196 // TODO(maruel): http://b/1126373 ui::ElideUrl(document.url(), ...) | |
| 197 offset = ReplaceKey(&output, | |
| 198 offset, | |
| 199 wcslen(kUrl), | |
| 200 UTF8ToWide(document.url().spec())); | |
| 201 } else { | |
| 202 // There is just a { in the string. | |
| 203 ++offset; | |
| 204 } | |
| 205 } | |
| 206 return output; | |
| 207 } | |
| 208 | |
| 209 } // namespace printing | |
| OLD | NEW |