View Javadoc
1   package org.djutils.math.functions;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   /**
7    * Draw text in unicode superscript glyphs. Much of this is based on
8    * <a href="https://rupertshepherd.info/resource_pages/superscript-letters-in-unicode">Superscript letters in unicode</a>
9    * <p>
10   * Copyright (c) 2024-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
12   * distributed under a three-clause BSD-style license, which can be found at
13   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   */
19  public final class SuperScript
20  {
21      /** The translation map. */
22      private final Map<Character, Character> translate;
23      
24      /**
25       * Create the SuperScript converter.
26       */
27      public SuperScript()
28      {
29          // @formatter:off
30          char[][] table = new char[][] {
31              // Capital letters are missing a lot, but we have an E (needed to write numbers in scientific notation)
32              {'A', '\u1d2c'},
33              {'B', 'ᴮ'}, // from https://lingojam.com/SuperscriptGenerator
34              {'C', '\u1d9c'}, // lower case - there is no superscript upper case C
35              {'D', '\u1d30'},
36              {'E', '\u1d31'},
37              {'F', '\u1da0'}, // lower case - there is no superscript upper case F
38              {'G', '\u1d33'},
39              {'H', '\u1d34'},
40              {'I', '\u1d35'},
41              {'J', '\u1d36'},
42              {'K', '\u1d37'},
43              {'L', '\u1d38'},
44              {'M', '\u1d39'},
45              {'N', '\u1d3a'},
46              {'O', '\u1d3c'},
47              {'P', '\u1d3e'},
48              {'Q', 'ᑫ'}, // approximation of lower case q from https://lingojam.com/SuperscriptGenerator
49              {'R', '\u1d3f'},
50              {'S', 'ˢ'}, // approximation of lower case s from https://lingojam.com/SuperscriptGenerator
51              {'T', '\u1d40'},
52              {'U', '\u1d41'},
53              {'V', '\u2c7d'},
54              {'W', '\u1d42'},
55              {'X', '\u157d'}, // Canadian symbol
56              {'Y', '\u1d31'},
57              {'Z', '\u1d31'},
58              {'a', '\u1d43'},
59              {'b', '\u1d47'},
60              {'c', '\u1d9c'},
61              {'d', '\u1d48'},
62              {'e', '\u1d49'},
63              {'f', '\u1da0'},
64              {'g', '\u1d4d'},
65              {'h', '\u02b0'},
66              {'i', '\u2071'},
67              {'j', '\u02b2'},
68              {'k', '\u1d4f'},
69              {'l', '\u02e1'},
70              {'m', '\u1d50'},
71              {'n', '\u207f'},
72              {'o', '\u1d52'},
73              {'p', '\u1d56'},
74              {'q', 'ᑫ'}, // Does not exist; this approximation is from https://lingojam.com/SuperscriptGenerator
75              {'r', '\u02b3'},
76              {'s', '\u02e2'},
77              {'t', '\u1d57'},
78              {'u', '\u1d58'},
79              {'v', '\u1d5b'},
80              {'w', '\u02b7'},
81              {'x', '\u02e3'},
82              {'y', '\u02b8'},
83              {'z', '\u1dbb'},
84              {'0', '\u2070'},
85              {'1', '\u00b9'},
86              {'2', '\u00b2'},
87              {'3', '\u00b3'},
88              {'4', '\u2074'},
89              {'5', '\u2075'},
90              {'6', '\u2076'},
91              {'7', '\u2077'},
92              {'8', '\u2078'},
93              {'9', '\u2079'},
94              {'+', '\u207a'},
95              {'-', '\u207b'},
96              {'.', '\u00b7'}, // https://www.compart.com/en/unicode/U+00B7 middle dot character
97              {',', '\u02be'}, // https://stackoverflow.com/questions/34350441/is-there-an-unicode-symbol-for-superscript-comma
98              {'(', '\u207d'},
99              {')', '\u207e'}
100             };
101         // @formatter:on
102 
103         this.translate = new HashMap<>();
104         for (int i = 0; i < table.length; i++)
105         {
106             this.translate.put(table[i][0], table[i][1]);
107         }
108 
109     }
110 
111     /**
112      * Translate one character to superscript.
113      * @param in the character to translate
114      * @return the character in superscript, some approximation thereof, or the input character if no translation was available
115      */
116     public char translate(final char in)
117     {
118         Character result = this.translate.get(in);
119         if (null == result)
120         {
121             return in; // Sorry
122         }
123         return result;
124     }
125 
126     /**
127      * Translate a String into superscript.
128      * @param in text to translate
129      * @return superscripted text
130      */
131     public String translate(final String in)
132     {
133         StringBuilder result = new StringBuilder();
134         for (int pos = 0; pos < in.length(); pos++)
135         {
136             result.append(translate(in.charAt(pos)));
137         }
138         return result.toString();
139     }
140 
141 }