JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
FontScale.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.font;
29
30/**
31 * Simple static font scale methods for unit conversions.
32 *
33 * PostScript - current DTP point system used e.g in CSS (Cascading Style Sheets).
34 * - 1 point = 1pt = 1/72in (cala) = 0.3528 mm
35 * - 1 pica = 1pc = 12pt= 1/6in (cala) = 4.233(3) mm
36 */
37public class FontScale {
38 /**
39 * Converts the the given points size to inch, dividing by {@code 72} points per inch.
40 * <pre>
41 1 points = 1/72 inch
42 *
43 * </pre>
44 */
45 public static float ptToInch(final float points) {
46 return points / 72f /* points per inch */;
47 }
48
49 /**
50 * Converts the the given points size to mm, dividing by {@code 72 * 25.4} points per inch.
51 * <pre>
52 1 inch = 25.4 mm
53 1 points = 1/72 inch
54 1 points = 1/72 * 25.4 mm
55 *
56 * </pre>
57 */
58 public static float ptToMM(final float points) {
59 return points / 72f /* points per inch */ * 25.4f /* mm_per_inch */;
60 }
61
62 /**
63 * Converts typical font size in points and screen resolution in dpi (pixels-per-inch) to font size in pixels,
64 * which can be used for pixel-size font scaling operations.
65 * <pre>
66 Font Scale Formula:
67 1 points = 1/72 inch
68
69 pixels = points / 72 * res_dpi
70 * </pre>
71 * @param points in points
72 * @param res_dpi display resolution in pixels-per-inch
73 * @return pixelSize scale factor for font operations.
74 * @see #toPixels2(float, float)
75 */
76 public static float toPixels(final float points /* points */, final float res_dpi /* pixels per inch */) {
77 return ptToInch( points ) * res_dpi;
78 }
79
80 /**
81 * Converts typical font size in points and screen resolution in pixels (pixels-per-mm) to font size in pixels,
82 * which can be used for pixel-size font scaling operations.
83 * <pre>
84 Font Scale Formula:
85 1 inch = 25.4 mm
86 1 points = 1/72 inch
87 1 points = 1/72 * 25.4 mm
88
89 pixels = points / 72 * 25.4 * res_ppmm
90 * </pre>
91 * @param points in points
92 * @param res_ppmm display resolution in pixels-per-mm
93 * @return pixelSize scale factor for font operations.
94 * @see #toPixels(float, float)
95 */
96 public static float toPixels2(final float points /* points */, final float res_ppmm /* pixels per mm */) {
97 return ptToMM( points ) * res_ppmm;
98 }
99
100 /**
101 * Converts [1/mm] to [1/inch] in place
102 * @param ppmm float[2] [1/mm] value
103 * @return return [1/inch] value
104 */
105 public static float[/*2*/] ppmmToPPI(final float[/*2*/] ppmm) {
106 ppmm[0] *= 25.4f;
107 ppmm[1] *= 25.4f;
108 return ppmm;
109 }
110
111 /**
112 * Converts [1/mm] to [1/inch] into res storage
113 * @param ppmm float[2] [1/mm] value
114 * @param res the float[2] result storage
115 * @return return [1/inch] value, i.e. the given res storage
116 */
117 public static float[/*2*/] ppmmToPPI(final float[/*2*/] ppmm, final float[/*2*/] res) {
118 res[0] = ppmm[0] * 25.4f;
119 res[1] = ppmm[1] * 25.4f;
120 return res;
121 }
122
123 /**
124 * Converts [1/inch] to [1/mm] in place
125 * @param ppi float[2] [1/inch] value
126 * @return return [1/mm] value
127 */
128 public static float[/*2*/] ppiToPPMM(final float[/*2*/] ppi) {
129 ppi[0] /= 25.4f;
130 ppi[1] /= 25.4f;
131 return ppi;
132 }
133
134 /**
135 * Converts [1/inch] to [1/mm] into res storage
136 * @param ppi float[2] [1/inch] value
137 * @param res the float[2] result storage
138 * @return return [1/mm] value, i.e. the given res storage
139 */
140 public static float[/*2*/] ppiToPPMM(final float[/*2*/] ppi, final float[/*2*/] res) {
141 res[0] = ppi[0] / 25.4f;
142 res[1] = ppi[1] / 25.4f;
143 return res;
144 }
145}
Simple static font scale methods for unit conversions.
Definition: FontScale.java:37
static float[] ppmmToPPI(final float[] ppmm)
Converts [1/mm] to [1/inch] in place.
Definition: FontScale.java:105
static float[] ppiToPPMM(final float[] ppi)
Converts [1/inch] to [1/mm] in place.
Definition: FontScale.java:128
static float ptToInch(final float points)
Converts the the given points size to inch, dividing by 72 points per inch.
Definition: FontScale.java:45
static float ptToMM(final float points)
Converts the the given points size to mm, dividing by 72 * 25.4 points per inch.
Definition: FontScale.java:58
static float[] ppiToPPMM(final float[] ppi, final float[] res)
Converts [1/inch] to [1/mm] into res storage.
Definition: FontScale.java:140
static float[] ppmmToPPI(final float[] ppmm, final float[] res)
Converts [1/mm] to [1/inch] into res storage.
Definition: FontScale.java:117
static float toPixels(final float points, final float res_dpi)
Converts typical font size in points and screen resolution in dpi (pixels-per-inch) to font size in p...
Definition: FontScale.java:76
static float toPixels2(final float points, final float res_ppmm)
Converts typical font size in points and screen resolution in pixels (pixels-per-mm) to font size in ...
Definition: FontScale.java:96