Enable WorkerDevToolsSanityTest.InspectSharedWorker
[chromium-blink-merge.git] / printing / units.cc
blob976fa802c97e0f1a69c261be4e238214f6b7d104
1 // Copyright (c) 2006-2008 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"
9 namespace printing {
11 int ConvertUnit(int value, int old_unit, int new_unit) {
12 DCHECK_GT(new_unit, 0);
13 DCHECK_GT(old_unit, 0);
14 // With integer arithmetic, to divide a value with correct rounding, you need
15 // to add half of the divisor value to the dividend value. You need to do the
16 // reverse with negative number.
17 if (value >= 0) {
18 return ((value * new_unit) + (old_unit / 2)) / old_unit;
19 } else {
20 return ((value * new_unit) - (old_unit / 2)) / old_unit;
24 double ConvertUnitDouble(double value, double old_unit, double new_unit) {
25 DCHECK_GT(new_unit, 0);
26 DCHECK_GT(old_unit, 0);
27 return value * new_unit / old_unit;
30 int ConvertMilliInchToHundredThousanthMeter(int milli_inch) {
31 // 1" == 25.4 mm
32 // 1" == 25400 um
33 // 0.001" == 25.4 um
34 // 0.001" == 2.54 cmm
35 return ConvertUnit(milli_inch, 100, 254);
38 int ConvertHundredThousanthMeterToMilliInch(int cmm) {
39 return ConvertUnit(cmm, 254, 100);
42 int ConvertPixelsToPoint(int pixels) {
43 return ConvertUnit(pixels, kPixelsPerInch, kPointsPerInch);
46 double ConvertPixelsToPointDouble(double pixels) {
47 return ConvertUnitDouble(pixels, kPixelsPerInch, kPointsPerInch);
50 } // namespace printing