units.cc 945 B

12345678910111213141516171819202122232425262728293031
  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.
  4. #include "printing/units.h"
  5. #include "base/check_op.h"
  6. #include "printing/print_job_constants.h"
  7. namespace printing {
  8. int ConvertUnit(float value, int old_unit, int new_unit) {
  9. DCHECK_GT(new_unit, 0);
  10. DCHECK_GT(old_unit, 0);
  11. // With integer arithmetic, to divide a value with correct rounding, you need
  12. // to add half of the divisor value to the dividend value. You need to do the
  13. // reverse with negative number.
  14. if (value >= 0) {
  15. return ((value * new_unit) + (old_unit / 2)) / old_unit;
  16. } else {
  17. return ((value * new_unit) - (old_unit / 2)) / old_unit;
  18. }
  19. }
  20. float ConvertUnitFloat(float value, float old_unit, float new_unit) {
  21. DCHECK_GT(new_unit, 0);
  22. DCHECK_GT(old_unit, 0);
  23. return value * new_unit / old_unit;
  24. }
  25. } // namespace printing