blob: dcee9ea56afd3821d9c8555467f3f35c89701369 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2011 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/units.h"
6
7#include "base/logging.h"
8#include "printing/print_job_constants.h"
9
10namespace printing {
11
12int ConvertUnit(int value, int old_unit, int new_unit) {
13 DCHECK_GT(new_unit, 0);
14 DCHECK_GT(old_unit, 0);
15 // With integer arithmetic, to divide a value with correct rounding, you need
16 // to add half of the divisor value to the dividend value. You need to do the
17 // reverse with negative number.
18 if (value >= 0) {
19 return ((value * new_unit) + (old_unit / 2)) / old_unit;
20 } else {
21 return ((value * new_unit) - (old_unit / 2)) / old_unit;
22 }
23}
24
25double ConvertUnitDouble(double value, double old_unit, double new_unit) {
26 DCHECK_GT(new_unit, 0);
27 DCHECK_GT(old_unit, 0);
28 return value * new_unit / old_unit;
29}
30
31int ConvertMilliInchToHundredThousanthMeter(int milli_inch) {
32 // 1" == 25.4 mm
33 // 1" == 25400 um
34 // 0.001" == 25.4 um
35 // 0.001" == 2.54 cmm
36 return ConvertUnit(milli_inch, 100, 254);
37}
38
39int ConvertHundredThousanthMeterToMilliInch(int cmm) {
40 return ConvertUnit(cmm, 254, 100);
41}
42
43int ConvertPixelsToPoint(int pixels) {
44 return ConvertUnit(pixels, kPixelsPerInch, kPointsPerInch);
45}
46
47double ConvertPixelsToPointDouble(double pixels) {
48 return ConvertUnitDouble(pixels, kPixelsPerInch, kPointsPerInch);
49}
50
51double ConvertPointsToPixelDouble(double points) {
52 return ConvertUnitDouble(points, kPointsPerInch, kPixelsPerInch);
53}
54
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055} // namespace printing