| 1 | /* |
|---|
| 2 | * Copyright 2009, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j; |
|---|
| 22 | |
|---|
| 23 | import java.text.DecimalFormat; |
|---|
| 24 | import java.text.DecimalFormatSymbols; |
|---|
| 25 | import java.util.Locale; |
|---|
| 26 | |
|---|
| 27 | import org.apache.log4j.Logger; |
|---|
| 28 | |
|---|
| 29 | public class UnitsOfMeasurement { |
|---|
| 30 | private final static Logger log = Logger.getLogger(UnitsOfMeasurement.class); |
|---|
| 31 | |
|---|
| 32 | public final static DecimalFormat format2DP; |
|---|
| 33 | static { |
|---|
| 34 | format2DP = new DecimalFormat("##.##", |
|---|
| 35 | new DecimalFormatSymbols(Locale.ENGLISH)); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | public static long twipToEMU(double twips) { |
|---|
| 39 | return Math.round(635 * twips); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | public static int inchToTwip(float inch ) { |
|---|
| 43 | // 1440 twip = 1 inch; |
|---|
| 44 | return Math.round(inch*1440); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public static float twipToInch(int twip) { |
|---|
| 48 | return twip/1440.00f; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public static int mmToTwip(float mm ) { |
|---|
| 52 | float inch = mm*0.0394f; |
|---|
| 53 | return inchToTwip(inch); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * 1440 twip = 1 inch;Try to guess whether inches or mm looks nicer |
|---|
| 59 | * @param left |
|---|
| 60 | * @return |
|---|
| 61 | */ |
|---|
| 62 | public static String twipToBest(int leftL ) { |
|---|
| 63 | |
|---|
| 64 | float inch4f = 80*twipToInch(leftL); |
|---|
| 65 | float inch4fabit = inch4f + 0.49f; |
|---|
| 66 | int inch4 = Math.round(inch4f); |
|---|
| 67 | int inch4next = Math.round( inch4fabit); |
|---|
| 68 | float inches = twipToInch(leftL); |
|---|
| 69 | if (inch4==inch4next) { |
|---|
| 70 | log.debug(leftL + " twips -> " + inches + "inches"); |
|---|
| 71 | // inches work |
|---|
| 72 | return format2DP.format(inches) + "in"; |
|---|
| 73 | } else { |
|---|
| 74 | float mm = inches/0.0394f; |
|---|
| 75 | log.debug(leftL + " twips -> " + mm + "mm ("+ format2DP.format(inches) + "inches)"); |
|---|
| 76 | return Math.round(mm) + "mm"; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public static String rgbTripleToHex(float red, float green, float blue) { |
|---|
| 81 | return getHex(red) + getHex(green) + getHex(blue); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | private static String getHex(float f) { |
|---|
| 85 | |
|---|
| 86 | int i = Math.round(f); |
|---|
| 87 | |
|---|
| 88 | if (i<=16) { |
|---|
| 89 | // Pad so we have 2 digits |
|---|
| 90 | return "0" + Integer.toHexString( i ); |
|---|
| 91 | } else { |
|---|
| 92 | return Integer.toHexString( i ); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // public static void main(String[] args) throws Exception { |
|---|
| 97 | // System.out.println(format2DP.format(twipToInch(2235))); |
|---|
| 98 | // System.out.println(twipToBest(2235) ); |
|---|
| 99 | // } |
|---|
| 100 | |
|---|
| 101 | } |
|---|