jdk/src/java.desktop/unix/classes/sun/font/X11KSC5601.java
author prr
Mon, 13 Apr 2015 17:06:04 -0700
changeset 30456 2a753e3fc714
parent 28969 jdk/src/java.desktop/unix/classes/sun/awt/motif/X11KSC5601.java@f980bee32887
permissions -rw-r--r--
8035302: Eliminate dependency on jdk.charsets from 2D font code. Reviewed-by: mchung, alanb, sherman, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
30456
2a753e3fc714 8035302: Eliminate dependency on jdk.charsets from 2D font code.
prr
parents: 28969
diff changeset
    26
package sun.font;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.*;
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    31
import sun.nio.cs.*;
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    32
import static sun.nio.cs.CharsetMapping.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class X11KSC5601 extends Charset {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public X11KSC5601 () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        super("X11KSC5601", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return cs instanceof X11KSC5601;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    49
    private class Encoder extends CharsetEncoder {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    50
        private DoubleByte.Encoder enc = (DoubleByte.Encoder)new EUC_KR().newEncoder();
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    51
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public Encoder(Charset cs) {
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    53
            super(cs, 2.0f, 2.0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (c <= 0x7F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    60
            return enc.canEncode(c);
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    61
        }
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    62
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    63
        protected int encodeDouble(char c) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
    64
            return enc.encodeChar(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    if (c <= '\u007f')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    int ncode = encodeDouble(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    if (ncode != 0 && c != '\u0000' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        da[dp++] = (byte) (ncode & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public boolean isLegalReplacement(byte[] repl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   100
    private class Decoder extends  CharsetDecoder {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   101
        private DoubleByte.Decoder dec = (DoubleByte.Decoder)new EUC_KR().newDecoder();
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public Decoder(Charset cs) {
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   104
            super(cs, 0.5f, 1.0f);
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   105
        }
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   106
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   107
        protected char decodeDouble(int b1, int b2) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   108
            return dec.decodeDouble(b1, b2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if ( sl - sp < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    int b1 = sa[sp] & 0xFF | 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    int b2 = sa[sp + 1] & 0xFF | 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    char c = decodeDouble(b1, b2);
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2
diff changeset
   132
                    if (c == UNMAPPABLE_DECODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        return CoderResult.unmappableForLength(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    sp +=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}