summaryrefslogtreecommitdiff
path: root/xfa_test/pdf/number_image_generator.cc
blob: 289691513cf1fe34e580e5feb99ee4d776b5a0f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2012 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 "pdf/number_image_generator.h"

#include "base/logging.h"
#include "base/strings/string_util.h"
#include "pdf/draw_utils.h"
#include "pdf/instance.h"

namespace chrome_pdf {

const int kPageNumberSeparator = 0;
const int kPageNumberOriginX = 8;
const int kPageNumberOriginY = 4;

NumberImageGenerator::NumberImageGenerator(Instance* instance)
    : instance_(instance),
      device_scale_(1.0f) {
}

NumberImageGenerator::~NumberImageGenerator() {
}

void NumberImageGenerator::Configure(const pp::ImageData& number_background,
    const std::vector<pp::ImageData>& number_images, float device_scale) {
  number_background_ = number_background;
  number_images_ = number_images;
  device_scale_ = device_scale;
}

void NumberImageGenerator::GenerateImage(
    int page_number, pp::ImageData* image) {
  char buffer[12];
  base::snprintf(buffer, sizeof(buffer), "%u", page_number);
  int extra_width = 0;
  DCHECK(number_images_.size() >= 10);
  DCHECK(!number_background_.is_null());
  for (size_t i = 1; i < strlen(buffer); ++i) {
    int index = buffer[i] - '0';
    extra_width += number_images_[index].size().width();
    extra_width += static_cast<int>(kPageNumberSeparator * device_scale_);
  }

  *image = pp::ImageData(
      instance_,
      PP_IMAGEDATAFORMAT_BGRA_PREMUL,
      pp::Size(number_background_.size().width() + extra_width,
               number_background_.size().height()),
      false);

  int stretch_point = number_background_.size().width() / 2;

  pp::Rect src_rc(0, 0, stretch_point, number_background_.size().height());
  pp::Rect dest_rc(src_rc);
  CopyImage(number_background_, src_rc, image, dest_rc, false);
  src_rc.Offset(number_background_.size().width() - stretch_point, 0);
  dest_rc.Offset(image->size().width() - stretch_point, 0);
  CopyImage(number_background_, src_rc, image, dest_rc, false);
  src_rc = pp::Rect(stretch_point, 0, 1, number_background_.size().height());
  dest_rc = src_rc;
  dest_rc.set_width(extra_width + 1);
  CopyImage(number_background_, src_rc, image, dest_rc, true);

  pp::Point origin(static_cast<int>(kPageNumberOriginX * device_scale_),
                   static_cast<int>(kPageNumberOriginY * device_scale_));
  for (size_t i = 0; i < strlen(buffer); ++i) {
    int index = buffer[i] - '0';
    CopyImage(
        number_images_[index],
        pp::Rect(pp::Point(), number_images_[index].size()),
        image, pp::Rect(origin, number_images_[index].size()), false);
    origin += pp::Point(
        number_images_[index].size().width() +
        static_cast<int>(kPageNumberSeparator * device_scale_), 0);
  }
}

}  // namespace chrome_pdf