diff options
author | sverrir@google.com <sverrir@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-07 21:31:39 +0000 |
---|---|---|
committer | sverrir@google.com <sverrir@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-07 21:31:39 +0000 |
commit | 8ff1d42631e79e842669dc3051d91ed7db80f1dc (patch) | |
tree | 55c1987549e982bfbf60a33fad3b60b5113d7864 /printing/page_overlays.cc | |
parent | 395f9295b3e370746846dbe6073a0d43ab7e4af5 (diff) | |
download | chromium_src-8ff1d42631e79e842669dc3051d91ed7db80f1dc.zip chromium_src-8ff1d42631e79e842669dc3051d91ed7db80f1dc.tar.gz chromium_src-8ff1d42631e79e842669dc3051d91ed7db80f1dc.tar.bz2 |
Move printing related stuff to the root printing project from the browser project. This simplifies further refactoring and eases understanding of the printing part of Chrome.
Also renamed win_printing_context to printing_context_win (correct naming convention) and added stub implementations for _linux and mac.
Now all but one file is compiling on all platforms.
TEST=none (no functional change).
BUG=none
Review URL: http://codereview.chromium.org/149212
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20086 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing/page_overlays.cc')
-rw-r--r-- | printing/page_overlays.cc | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/printing/page_overlays.cc b/printing/page_overlays.cc new file mode 100644 index 0000000..946186d --- /dev/null +++ b/printing/page_overlays.cc @@ -0,0 +1,206 @@ +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/page_overlays.h" + +#include "app/gfx/text_elider.h" +#include "base/logging.h" +#include "base/string_util.h" +#include "printing/printed_document.h" +#include "printing/printed_page.h" + +namespace { + +// Replaces a subpart of a string by other value, and returns the position right +// after the new value. +size_t ReplaceKey(std::wstring* string, + size_t offset, + size_t old_string_len, + const std::wstring& new_string) { + string->replace(offset, old_string_len, new_string); + return offset + new_string.size(); +} + +} // namespace + +namespace printing { + +const wchar_t* const PageOverlays::kTitle = L"{title}"; +const wchar_t* const PageOverlays::kTime = L"{time}"; +const wchar_t* const PageOverlays::kDate = L"{date}"; +const wchar_t* const PageOverlays::kPage = L"{page}"; +const wchar_t* const PageOverlays::kPageCount = L"{pagecount}"; +const wchar_t* const PageOverlays::kPageOnTotal = L"{pageontotal}"; +const wchar_t* const PageOverlays::kUrl = L"{url}"; + +PageOverlays::PageOverlays() + : top_left(kDate), + top_center(kTitle), + top_right(), + bottom_left(kUrl), + bottom_center(), + bottom_right(kPageOnTotal) { +} + +bool PageOverlays::Equals(const PageOverlays& rhs) const { + return top_left == rhs.top_left && + top_center == rhs.top_center && + top_right == rhs.top_right && + bottom_left == rhs.bottom_left && + bottom_center == rhs.bottom_center && + bottom_right == rhs.bottom_right; +} + +const std::wstring& PageOverlays::GetOverlay(HorizontalPosition x, + VerticalPosition y) const { + switch (x) { + case LEFT: + switch (y) { + case TOP: + return top_left; + case BOTTOM: + return bottom_left; + } + break; + case CENTER: + switch (y) { + case TOP: + return top_center; + case BOTTOM: + return bottom_center; + } + break; + case RIGHT: + switch (y) { + case TOP: + return top_right; + case BOTTOM: + return bottom_right; + } + break; + } + NOTREACHED(); + return EmptyWString(); +} + +void PageOverlays::SetOverlay(HorizontalPosition x, VerticalPosition y, + std::wstring& input) { + switch (x) { + case LEFT: + switch (y) { + case TOP: + top_left = input; + break; + case BOTTOM: + bottom_left = input; + break; + default: + NOTREACHED(); + break; + } + break; + case CENTER: + switch (y) { + case TOP: + top_center = input; + break; + case BOTTOM: + bottom_center = input; + break; + default: + NOTREACHED(); + break; + } + break; + case RIGHT: + switch (y) { + case TOP: + top_right = input; + break; + case BOTTOM: + bottom_right = input; + break; + default: + NOTREACHED(); + break; + } + break; + default: + NOTREACHED(); + break; + } +} + +//static +std::wstring PageOverlays::ReplaceVariables(const std::wstring& input, + const PrintedDocument& document, + const PrintedPage& page) { + std::wstring output(input); + for (size_t offset = output.find(L'{', 0); + offset != std::wstring::npos; + offset = output.find(L'{', offset)) { + + if (0 == output.compare(offset, + wcslen(kTitle), + kTitle)) { + offset = ReplaceKey(&output, + offset, + wcslen(kTitle), + document.name()); + } else if (0 == output.compare(offset, + wcslen(kTime), + kTime)) { + offset = ReplaceKey(&output, + offset, + wcslen(kTime), + document.time()); + } else if (0 == output.compare(offset, + wcslen(kDate), + kDate)) { + offset = ReplaceKey(&output, + offset, + wcslen(kDate), + document.date()); + } else if (0 == output.compare(offset, + wcslen(kPage), + kPage)) { + offset = ReplaceKey(&output, + offset, + wcslen(kPage), + IntToWString(page.page_number())); + } else if (0 == output.compare(offset, + wcslen(kPageCount), + kPageCount)) { + offset = ReplaceKey(&output, + offset, + wcslen(kPageCount), + IntToWString(document.page_count())); + } else if (0 == output.compare(offset, + wcslen(kPageOnTotal), + kPageOnTotal)) { + std::wstring replacement; + replacement = IntToWString(page.page_number()); + replacement += L"/"; + replacement += IntToWString(document.page_count()); + offset = ReplaceKey(&output, + offset, + wcslen(kPageOnTotal), + replacement); + } else if (0 == output.compare(offset, + wcslen(kUrl), + kUrl)) { + // TODO(maruel): http://b/1126373 gfx::ElideUrl(document.url(), ...) + offset = ReplaceKey(&output, + offset, + wcslen(kUrl), + UTF8ToWide(document.url().spec())); + } else { + // There is just a { in the string. + ++offset; + } + } + return output; +} + +} // namespace printing |