简体   繁体   English

如何翻转一个字母?

[英]How to flip a letter upside down?

Here I'm trying to flip an English letter upside down for my mail template. 在这里,我试图将英文字母颠倒翻转为我的邮件模板。

Well I can get it manually. 好吧,我可以手动获取它。 What I'm doing is now manually 我正在做的是现在手动

just like 就像

 content.append("ɥ"); //actual h letter.
 content.append("ǝ")// actual e letter.

So my question is that , is there any trick/method/any clue to do it in programmatic way ? 所以我的问题是,是否有任何技巧/方法/任何线索以程序化的方式做到这一点?

What I'm doing now is 我现在正在做的是

switch(letter) {

case 'e':
   return 'ǝ';
}

That looks odd and looking for hints. 这看起来很奇怪,并寻找提示。 Thanks for any help. 谢谢你的帮助。

The fastest way to do character replacement is an array. 更换字符的最快方法是使用数组。

create an array of upside down letters. 创建一系列倒置字母。 Then, do a check of the value coming in and find it's match. 然后,检查进入的值并找到它匹配。

Java allows you to get the integer representation of a character by simply casting int x = char a; Java允许您通过简单地转换int x = char a;来获取字符的整数表示x = char a; So, it stands to reason that your upsidedown a would be at the index of your char minus the lowest char you are mapping (usually lowercase a). 因此,理所当然,你的倒置a将在你的char的索引减去你映射的最低char(通常是小写a)。

char[] updown = {'ɐ','q','ɔ','p','ə','ɟ','ƃ','ɥ','ı','ɾ','ʞ','l','ɯ','u','o','d','b','ɹ','s','ʇ','n','ʌ','ʍ','x','ʎ','z'};
//this example uses 26 chars, all lower case;
int a = 'a';
int z = 'z';

String newString = "";
for(int i=0; i<string.length; i++){
    int ch = string.charAt(i);
    if(ch>= a && ch <=z){
        newString = (updown[ch-a]) + newString;
    }
}

Here's a link to IDEONE where you can compile and test for yourself. 这是IDEONE的链接,您可以自己编译和测试。 http://ideone.com/LombFE http://ideone.com/LombFE

I'm not sure of a java-specific solution, but seeing as Strings in java use unicode, I checked out the unicode characters for their upside down counterparts here: http://www.fileformat.info/convert/text/upside-down-map.htm 我不确定特定于java的解决方案,但是看到java中的字符串使用unicode,我在这里检查了颠倒对应的unicode字符: http//www.fileformat.info/convert/text/upside-下map.htm

It doesn't look there's a good programmatic method for finding these. 它看起来不是一个很好的编程方法来找到这些。 However, there are a variety of converters, like this one http://www.fileformat.info/convert/text/upside-down.htm You could generate config files based on input and output of one of these types of converters and use those config files to drive your upside-down letter selection 但是,有各种各样的转换器,比如这个http://www.fileformat.info/convert/text/upside-down.htm您可以根据这些类型的转换器之一的输入和输出生成配置文件并使用那些配置文件来驱动你颠倒的字母选择

If you are looking for a generic way to create upside-down plain text, there is no way. 如果您正在寻找一种创建颠倒文本的通用方法,那就没办法了。

All the sites that do that, simply substitute adequate unicode characters that do not have anything in common with the original ones, and for some they don't find anything. 所有这样做的网站,只需替换与原始网站没有任何共同点的足够的unicode字符,对于某些人来说,他们找不到任何东西。 There is no way to have anything that looks like upside-down j , so as you can see in other answers, the supposed "upside-down j " looks ugly. 没有办法让任何看起来像颠倒的j ,所以你可以在其他答案中看到,所谓的“颠倒的j ”看起来很难看。 Were you looking for upside-down ß or @ or 5 , you wouldn't find anything either. 如果您正在寻找颠倒的ß@5 ,您也找不到任何东西。 The sites that do upside-down text either ignore such symbols, use something really dissimilar, or simply fail to do anything. 做颠倒文本的网站要么忽略这些符号,要么使用非常不相似的东西,要么根本不做任何事情。

Unicode deals with contents of the text, not its orientation. Unicode处理文本内容,而不是其方向。

You mention mail templates. 你提到邮件模板。 If you are using HTML, you can use this bit of CSS: 如果您使用的是HTML,则可以使用以下CSS:

.upsidedown {
    -moz-transform: rotate(180deg);
    -webkit-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    transform: rotate(180deg);
}

Not all mail clients will display it correctly though. 并非所有邮件客户端都会正确显示它。

Try this script, here : http://ideone.com/szPg6V 试试这个脚本,请访问: http//ideone.com/szPg6V

Input : 输入:

  • 'So my question is that , is there any trick/method/any clue to do it in programmatic way ?' “所以我的问题是,是否有任何技巧/方法/任何线索以编程方式进行?”

Ouput : 输出:

  • ,¿ ʎɐʍ ɔıʇɐɯɯɐɹboɹd uı ʇı op oʇ ǝnןɔ ʎuɐ/poɥʇǝɯ/ʞɔıɹʇ ʎuɐ ǝɹǝɥʇ sı 'ʇɐɥʇ sı uoıʇsǝnb ʎɯ oS, ,¿ʎɐʍɔʇɐɯɯɐɹɹɹu u op op op opɐɐɐɥʇǝɯɥʇǝɯɥʇǝɯɥʇǝɯɥʇǝɯɥʇǝɯɥʇǝɯıııııʇɐɥʇııoʇb b b b S

Java code : Java代码:

String normal = "abcdefghijklmnopqrstuvwxyz_,;.?!/\\'";
String split  = "ɐqɔpǝɟbɥıظʞןɯuodbɹsʇnʌʍxʎz‾'؛˙¿¡/\\,";
//maj
normal += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
split  += "∀qϽᗡƎℲƃHIſʞ˥WNOԀὉᴚS⊥∩ΛMXʎZ";
//number
normal += "0123456789";
split  += "0ƖᄅƐㄣϛ9ㄥ86";

String str = "So my question is that , is there any trick/method/any clue to do it in programmatic way ?";
String newstr = ""; 
char letter;
for (int i=0; i< str.length(); i++) {
    letter = str.charAt(i);

    int a = normal.indexOf(letter);
    newstr += (a != -1) ? split.charAt(a) : letter;
}
System.out.println(new StringBuilder(newstr).reverse().toString());

I use this tools : http://www.upsidedowntext.com/ 我使用这个工具: http//www.upsidedowntext.com/

If you don't like any of the examples above, you can always use an enum. 如果您不喜欢上述任何示例,则可以始终使用枚举。 This way you can add other characters you may want with their respective upside down equivalent. 这样,您可以添加其他可能需要的字符以及它们各自的颠倒等效字符。

Example : 示例

//only lower case in this example, but new values can be added
enum upsideDown{
    a('ɐ'),
    b('q'),
    c('ɔ'),
    d('p'),
    e('ə'),
    f('ɟ'),
    g('ƃ'),
    h('ɥ'),
    i('ı'),
    j('ɾ'),
    k('ʞ'),
    l('l'),
    m('ɯ'),
    n('u'),
    o('o'),
    p('d'),
    q('b'),
    r('ɹ'),
    s('s'),
    t('ʇ'),
    u('n'),
    v('ʌ'),
    w('ʍ'),
    x('x'),
    y('ʎ'),
    z('z');
    private char value;

    private upsideDown(char value) {
        this.value = value;
    }
    private char getValue(){
        return value;
    }
};


public static void main(String[] args) {

    String hello = "helloworld";
    StringBuilder str = new StringBuilder(hello.length());
    for (int i = hello.length()-1; i >= 0; i--) {
        str.append(upsideDown.valueOf(String.valueOf(hello.charAt(i))).getValue());
    }
    System.out.println(str.toString());

}

I wrote a code to do it in programmatic way, but the code is very slow. 我编写了一个以编程方式执行此操作的代码,但代码非常慢。

The code computes the differences between the shapes of all letters and the flipped character's shape and choose the one with minimal area. 代码计算所有字母的形状和翻转字符的形状之间的差异,并选择具有最小面积的字母。

Example: 例:

a ɐ
e ǝ
i ı
o o
u n

Code: 码:

import java.awt.Font;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;

class Main {
    private static final Font FONT = new Font(Font.MONOSPACED, Font.PLAIN, 24);

    private static Shape moveToOrigin(Shape s) {
        Rectangle2D r = s.getBounds2D();
        return AffineTransform.getTranslateInstance(-r.getX(), -r.getY())
                .createTransformedShape(s);
    }

    private static Shape flip(Shape s) {
        Rectangle2D r = s.getBounds2D();
        return AffineTransform.getRotateInstance(Math.PI, r.getCenterX(),
                r.getCenterY()).createTransformedShape(s);
    }

    private static double getArea(Shape s) {
        PathIterator path = s.getPathIterator(new AffineTransform());
        double area0 = 0;
        while (!path.isDone()) {
            double area = 0;
            double[] darray = new double[6];
            path.currentSegment(darray);
            double x0 = darray[0], y0 = darray[1];
            double x1 = x0, y1 = y0;
            path.next();
            while (path.currentSegment(darray) != PathIterator.SEG_CLOSE) {
                double x2 = darray[0], y2 = darray[1];
                area += x1 * y2 - y1 * x2;
                x1 = x2;
                y1 = y2;
                path.next();
            }
            path.next();
            area0 += Math.abs((area + x1 * y0 - y1 * x0) / 2);
        }
        return area0;
    }

    public static char findOpposite(char c) {
        FontRenderContext frc = new FontRenderContext(new AffineTransform(),
                true, false);
        GlyphVector gv1 = FONT.createGlyphVector(frc, String.valueOf(c));
        Area area1 = new Area(flip(moveToOrigin(gv1.getOutline())));

        double minDiff = 1.0 / 0.0;
        char bestChar = ' ';
        for (int i = 0; i <= Character.MAX_VALUE; i++) {
            GlyphVector gv2 = FONT.createGlyphVector(frc,
                    String.valueOf((char) i));
            Area area2 = new Area(moveToOrigin(gv2.getOutline()));
            area2.exclusiveOr(area1);

            double diff = getArea(area2);

            if (diff < minDiff) {
                minDiff = diff;
                bestChar = (char) i;
                System.err.println(bestChar + " : " + minDiff);
            }
            if (i % (Character.MAX_VALUE / 100) == 0) {
                System.err.println(i / (Character.MAX_VALUE / 100) + "%");
            }
        }
        return bestChar;
    }

    public static void main(String[] args) {
        System.out.println('a' + " " + findOpposite('a'));
        System.out.println('e' + " " + findOpposite('e'));
        System.out.println('i' + " " + findOpposite('i'));
        System.out.println('o' + " " + findOpposite('o'));
        System.out.println('u' + " " + findOpposite('u'));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM