Changeset 8006 in josm for trunk


Ignore:
Timestamp:
2015-02-04T15:03:54+01:00 (5 years ago)
Author:
bastiK
Message:

see #10989 - I18n display support for major Asian scripts (Tamil, Bengali, ...)

add support for the following charsets on Windows: devanagari, gurmuhi,
gujarati, tamil, telugu, kannada, bengali

Adds a couple of new advanced preference values:

  • "fontconfig.properties" - allows to install a custom fontconfig.properties file (for advanced users). Default value: <unset>
  • "font.extended-unicode" - allows to disable the new experimental code introduced with this changeset. Default value: "true" (enabled)
  • "font.extended-unicode.added-items" - specify the charsets and fonts that get added to the default Java font configuration.

This can be changed by the user to add different or more fallback fonts.
Format: 3 entries per line; 1st: character subset, 2nd: Font name, 3rd: Font file name.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r7834 r8006  
    2929
    3030import java.awt.GraphicsEnvironment;
     31import java.io.BufferedWriter;
    3132import java.io.File;
     33import java.io.FileInputStream;
    3234import java.io.IOException;
     35import java.io.OutputStream;
     36import java.io.OutputStreamWriter;
     37import java.io.Writer;
     38import java.nio.file.FileSystems;
     39import java.nio.file.Files;
     40import java.nio.file.Path;
    3341import java.security.InvalidKeyException;
    3442import java.security.KeyFactory;
     
    4351import java.security.spec.X509EncodedKeySpec;
    4452import java.util.ArrayList;
     53import java.util.Arrays;
    4554import java.util.Collection;
    4655import java.util.Enumeration;
     56import java.util.List;
     57import java.util.Properties;
    4758
    4859import javax.swing.JOptionPane;
    49 
    5060import org.openstreetmap.josm.Main;
    5161
     
    8595    private static final String WINDOWS_ROOT = "Windows-ROOT";
    8696
     97    @Override
     98    public void preStartupHook() {
     99        extendFontconfig();
     100    }
     101   
     102    /**
     103     * Add more fallback fonts to the Java runtime, in order to get wider
     104     * unicode support.
     105     *
     106     * The font configuration in Java doesn't include some Indic scripts,
     107     * even though MS Windows ships with fonts that cover these unicode
     108     * ranges.
     109     *
     110     * To fix this, the fontconfig.properties template is copied to the JOSM
     111     * cache folder. Then, the additional entries are added to the font
     112     * configuration. Finally the system property "sun.awt.fontconfig" is set
     113     * to the customized fontconfig.properties file.
     114     *
     115     * This is a crude hack, but better than no font display at all for these
     116     * languages.
     117     * There is no guarantee, that the template file
     118     * ($JAVA_HOME/lib/fontconfig.properties.src) matches the default
     119     * configuration (which is in a binary format).
     120     * Furthermore, the system property "sun.awt.fontconfig" is undocumented and
     121     * may no longer work in future versions of Java.
     122     */
     123    protected void extendFontconfig() {
     124        String customFontconfigFile = Main.pref.get("fontconfig.properties", null);
     125        if (customFontconfigFile != null) {
     126            Utils.updateSystemProperty("sun.awt.fontconfig", customFontconfigFile);
     127            return;
     128        }
     129        if (!Main.pref.getBoolean("font.extended-unicode", true))
     130            return;
     131        String javaLibPath = System.getProperty("java.home") + File.separator + "lib";
     132        Path templateFile = FileSystems.getDefault().getPath(javaLibPath, "fontconfig.properties.src");
     133        if (!Files.isReadable(templateFile)) {
     134            Main.warn("extended unicode - unable to find font config template file "+templateFile.toString());
     135            return;
     136        }
     137        try {
     138            Properties props = new Properties();
     139            props.load(new FileInputStream(templateFile.toFile()));
     140            byte[] content = Files.readAllBytes(templateFile);
     141            File cachePath = Main.pref.getCacheDirectory();
     142            Path fontconfigFile = cachePath.toPath().resolve("fontconfig.properties");
     143            OutputStream os = Files.newOutputStream(fontconfigFile);
     144            os.write(content);
     145            try (Writer w = new BufferedWriter(new OutputStreamWriter(os))) {
     146                Collection<Collection<String>> def = new ArrayList<>();
     147                def.add(Arrays.asList("devanagari", "", "")); // just include in fallback list
     148                                                              // no font definition needed
     149                // all of the following fonts are available in Win XP and later
     150                def.add(Arrays.asList("gujarati", "Shruti", "SHRUTI.TTF"));
     151                def.add(Arrays.asList("kannada", "Tunga", "TUNGA.TTF"));
     152                def.add(Arrays.asList("gurmuhi", "Raavi", "RAAVI.TTF"));
     153                def.add(Arrays.asList("tamil", "Latha", "LATHA.TTF"));
     154                def.add(Arrays.asList("telugu", "Gautami", "GAUTAMI.TTF"));
     155                def.add(Arrays.asList("bengali", "Vrinda", "VRINDA.TTF"));
     156                Collection<Collection<String>> additions =
     157                        Main.pref.getArray("font.extended-unicode.added-items", def);
     158                w.append("\n\n# Added by JOSM to extend unicode coverage of Java font support:\n\n");
     159                List<String> allCharSubsets = new ArrayList<>();
     160                for (Collection<String> entry: additions) {
     161                    List<String> lentry = new ArrayList<>(entry);
     162                    String charSubset = lentry.get(0);
     163                    allCharSubsets.add(charSubset);
     164                    String platformFontName = lentry.get(1);
     165                    if ("".equals(platformFontName)) {
     166                        continue;
     167                    }
     168                    String key = "allfonts." + charSubset;
     169                    String value = platformFontName;
     170                    String prevValue = props.getProperty(key);
     171                    if (prevValue != null && !prevValue.equals(value)) {
     172                        Main.warn("extended unicode - overriding " + key + "=" + prevValue + " with " + value);
     173                    }
     174                    w.append(key + "=" + value + "\n");
     175                }
     176                w.append("\n");
     177                for (Collection<String> entry: additions) {
     178                    List<String> lentry = new ArrayList<>(entry);
     179                    String platformFontName = lentry.get(1);
     180                    String fontFile = lentry.get(2);
     181                    if ("".equals(platformFontName) || "".equals(fontFile)) {
     182                        continue;
     183                    }
     184                    String key = "filename." + platformFontName;
     185                    String value = fontFile;
     186                    String prevValue = props.getProperty(key);
     187                    if (prevValue != null && !prevValue.equals(value)) {
     188                        Main.warn("extended unicode - overriding " + key + "=" + prevValue + " with " + value);
     189                    }
     190                    w.append(key + "=" + value + "\n");
     191                }
     192                w.append("\n");
     193                String fallback = props.getProperty("sequence.fallback");
     194                if (fallback != null) {
     195                    w.append("sequence.fallback=" + fallback + "," + Utils.join(",", allCharSubsets) + "\n");
     196                } else {
     197                    w.append("sequence.fallback=" + Utils.join(",", allCharSubsets) + "\n");
     198                }
     199            }
     200            Utils.updateSystemProperty("sun.awt.fontconfig", fontconfigFile.toString());
     201        } catch (IOException ex) {
     202            Main.error(ex);
     203        }
     204    }
     205   
    87206    @Override
    88207    public void openUrl(String url) throws IOException {
Note: See TracChangeset for help on using the changeset viewer.