1 // 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.
5 #include "printing/units.h"
7 #include "base/logging.h"
8 #include "printing/print_job_constants.h"
12 int 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.
19 return ((value
* new_unit
) + (old_unit
/ 2)) / old_unit
;
21 return ((value
* new_unit
) - (old_unit
/ 2)) / old_unit
;
25 double 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
;
31 int ConvertMilliInchToHundredThousanthMeter(int milli_inch
) {
36 return ConvertUnit(milli_inch
, 100, 254);
39 int ConvertHundredThousanthMeterToMilliInch(int cmm
) {
40 return ConvertUnit(cmm
, 254, 100);
43 int ConvertPixelsToPoint(int pixels
) {
44 return ConvertUnit(pixels
, kPixelsPerInch
, kPointsPerInch
);
47 double ConvertPixelsToPointDouble(double pixels
) {
48 return ConvertUnitDouble(pixels
, kPixelsPerInch
, kPointsPerInch
);
51 double ConvertPointsToPixelDouble(double points
) {
52 return ConvertUnitDouble(points
, kPointsPerInch
, kPixelsPerInch
);
55 } // namespace printing