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