Home | History | Annotate | Download | only in transcoder
      1 /*
      2  * Copyright (c) 2010, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "FontTranscoder.h"
     33 
     34 #include "FontDescription.h"
     35 #include "TextEncoding.h"
     36 #include <wtf/unicode/CharacterNames.h>
     37 
     38 namespace WebCore {
     39 
     40 FontTranscoder::FontTranscoder()
     41 {
     42     m_converterTypes.add("MS PGothic", BackslashToYenSign);
     43     UChar unicodeNameMSPGothic[] = {0xFF2D, 0xFF33, 0x0020, 0xFF30, 0x30B4, 0x30B7, 0x30C3, 0x30AF};
     44     m_converterTypes.add(AtomicString(unicodeNameMSPGothic, WTF_ARRAY_LENGTH(unicodeNameMSPGothic)), BackslashToYenSign);
     45 
     46     m_converterTypes.add("MS PMincho", BackslashToYenSign);
     47     UChar unicodeNameMSPMincho[] = {0xFF2D, 0xFF33, 0x0020, 0xFF30, 0x660E, 0x671D};
     48     m_converterTypes.add(AtomicString(unicodeNameMSPMincho, WTF_ARRAY_LENGTH(unicodeNameMSPMincho)), BackslashToYenSign);
     49 
     50     m_converterTypes.add("MS Gothic", BackslashToYenSign);
     51     UChar unicodeNameMSGothic[] = {0xFF2D, 0xFF33, 0x0020, 0x30B4, 0x30B7, 0x30C3, 0x30AF};
     52     m_converterTypes.add(AtomicString(unicodeNameMSGothic, WTF_ARRAY_LENGTH(unicodeNameMSGothic)), BackslashToYenSign);
     53 
     54     m_converterTypes.add("MS Mincho", BackslashToYenSign);
     55     UChar unicodeNameMSMincho[] = {0xFF2D, 0xFF33, 0x0020, 0x660E, 0x671D};
     56     m_converterTypes.add(AtomicString(unicodeNameMSMincho, WTF_ARRAY_LENGTH(unicodeNameMSMincho)), BackslashToYenSign);
     57 
     58     m_converterTypes.add("Meiryo", BackslashToYenSign);
     59     UChar unicodeNameMeiryo[] = {0x30E1, 0x30A4, 0x30EA, 0x30AA};
     60     m_converterTypes.add(AtomicString(unicodeNameMeiryo, WTF_ARRAY_LENGTH(unicodeNameMeiryo)), BackslashToYenSign);
     61 }
     62 
     63 FontTranscoder::ConverterType FontTranscoder::converterType(const FontDescription& fontDescription, const TextEncoding* encoding) const
     64 {
     65     const AtomicString& fontFamily = fontDescription.family().family().string();
     66     if (!fontFamily.isNull()) {
     67         HashMap<AtomicString, ConverterType>::const_iterator found = m_converterTypes.find(fontFamily);
     68         if (found != m_converterTypes.end())
     69             return found->second;
     70     }
     71 
     72     // IE's default fonts for Japanese encodings change backslashes into yen signs.
     73     // We emulate this behavior only when no font is explicitly specified.
     74     if (encoding && encoding->backslashAsCurrencySymbol() != '\\' && !fontDescription.isSpecifiedFont())
     75         return BackslashToYenSign;
     76 
     77     return NoConversion;
     78 }
     79 
     80 void FontTranscoder::convert(String& text, const FontDescription& fontDescription, const TextEncoding* encoding) const
     81 {
     82     switch (converterType(fontDescription, encoding)) {
     83     case BackslashToYenSign: {
     84         // FIXME: TextEncoding.h has similar code. We need to factor them out.
     85         text.replace('\\', yenSign);
     86         break;
     87     }
     88     case NoConversion:
     89     default:
     90         ASSERT_NOT_REACHED();
     91     }
     92 }
     93 
     94 bool FontTranscoder::needsTranscoding(const FontDescription& fontDescription, const TextEncoding* encoding) const
     95 {
     96     ConverterType type = converterType(fontDescription, encoding);
     97     return type != NoConversion;
     98 }
     99 
    100 FontTranscoder& fontTranscoder()
    101 {
    102     static FontTranscoder* transcoder = new FontTranscoder;
    103     return *transcoder;
    104 }
    105 
    106 } // namespace WebCore
    107