Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / pdf / number_image_generator.cc
blob289691513cf1fe34e580e5feb99ee4d776b5a0f8
1 // Copyright (c) 2012 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.
5 #include "pdf/number_image_generator.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "pdf/draw_utils.h"
10 #include "pdf/instance.h"
12 namespace chrome_pdf {
14 const int kPageNumberSeparator = 0;
15 const int kPageNumberOriginX = 8;
16 const int kPageNumberOriginY = 4;
18 NumberImageGenerator::NumberImageGenerator(Instance* instance)
19 : instance_(instance),
20 device_scale_(1.0f) {
23 NumberImageGenerator::~NumberImageGenerator() {
26 void NumberImageGenerator::Configure(const pp::ImageData& number_background,
27 const std::vector<pp::ImageData>& number_images, float device_scale) {
28 number_background_ = number_background;
29 number_images_ = number_images;
30 device_scale_ = device_scale;
33 void NumberImageGenerator::GenerateImage(
34 int page_number, pp::ImageData* image) {
35 char buffer[12];
36 base::snprintf(buffer, sizeof(buffer), "%u", page_number);
37 int extra_width = 0;
38 DCHECK(number_images_.size() >= 10);
39 DCHECK(!number_background_.is_null());
40 for (size_t i = 1; i < strlen(buffer); ++i) {
41 int index = buffer[i] - '0';
42 extra_width += number_images_[index].size().width();
43 extra_width += static_cast<int>(kPageNumberSeparator * device_scale_);
46 *image = pp::ImageData(
47 instance_,
48 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
49 pp::Size(number_background_.size().width() + extra_width,
50 number_background_.size().height()),
51 false);
53 int stretch_point = number_background_.size().width() / 2;
55 pp::Rect src_rc(0, 0, stretch_point, number_background_.size().height());
56 pp::Rect dest_rc(src_rc);
57 CopyImage(number_background_, src_rc, image, dest_rc, false);
58 src_rc.Offset(number_background_.size().width() - stretch_point, 0);
59 dest_rc.Offset(image->size().width() - stretch_point, 0);
60 CopyImage(number_background_, src_rc, image, dest_rc, false);
61 src_rc = pp::Rect(stretch_point, 0, 1, number_background_.size().height());
62 dest_rc = src_rc;
63 dest_rc.set_width(extra_width + 1);
64 CopyImage(number_background_, src_rc, image, dest_rc, true);
66 pp::Point origin(static_cast<int>(kPageNumberOriginX * device_scale_),
67 static_cast<int>(kPageNumberOriginY * device_scale_));
68 for (size_t i = 0; i < strlen(buffer); ++i) {
69 int index = buffer[i] - '0';
70 CopyImage(
71 number_images_[index],
72 pp::Rect(pp::Point(), number_images_[index].size()),
73 image, pp::Rect(origin, number_images_[index].size()), false);
74 origin += pp::Point(
75 number_images_[index].size().width() +
76 static_cast<int>(kPageNumberSeparator * device_scale_), 0);
80 } // namespace chrome_pdf