Menu

UTF-8 Problem

Vitaliy
2012-09-10
2012-11-11
  • Vitaliy

    Vitaliy - 2012-09-10

    Hi there. I'm a new in the sourceforge, and do not know how to send a
    patch. So I just created a new post.
    Joxy LookAndFeel do not draw UTF-8 strings properly as QString(char
    chars) constructor creates a new
    string from ANCII charset. To create QString from UTF-8 charset use
    QString::fromUtf8(char
    utf8)
    instead. Here is a fixed joxy_utils_JoxyGraphics.cpp file:

    /**
     * This file is a part of the Joxy Look and Feel for Java Swing.
     */
    #include "joxy_utils_JoxyGraphics.h"
    #include <QtGui/QApplication>
    #include <QtGui/qcolor.h>
    #include <QtGui/qimage.h>
    #include <QtGui/qpainter.h>
    #include <QtCore/qstring.h>
    int argc = 0;
    char** argv = NULL;
    QApplication a(argc, argv);
    jmethodID mid;
    JNIEXPORT void JNICALL Java_joxy_utils_JoxyGraphics_initializeNative
      (JNIEnv *env, jclass cl) {
        jclass cls = env->FindClass("java/awt/image/BufferedImage");
        if (cls == NULL) {
            printf("Couldn't find class BufferedImage\n");
            return;
        }
        mid = env->GetMethodID(cls, "setRGB", "(III)V");
        if (mid == NULL) {
            printf("Couldn't find method setRGB(int)\n");
            return;
        }
    }
    JNIEXPORT void JNICALL Java_joxy_utils_JoxyGraphics_drawStringNative
      (JNIEnv *env, jclass cl, jstring str, jobject image, jint width,
    jint height, jstring fontname, jint fontsize, jint style, jint color)
    {
        QImage qimage(width, height, QImage::Format_ARGB32);
        QColor qcolor = QColor::fromRgb(color);
        qcolor.setAlpha(0);
        qimage.fill(qcolor);
        QPainter painter(&qimage);
        painter.setPen(QColor::fromRgb(color));
        const char* cfontname = env->GetStringUTFChars(fontname, JNI_FALSE);
        bool italic = (style >= 2);
        bool bold = (style % 2 == 1);
        painter.setFont(QFont(cfontname, fontsize, (bold ? 75 : 50), italic));
        const char* cstr = env->GetStringUTFChars(str, JNI_FALSE);
        painter.drawText(0, 0, width + 10, height, Qt::AlignLeft,
    QString::fromUtf8(cstr));
        env->ReleaseStringUTFChars(str, cfontname);
        env->ReleaseStringUTFChars(str, cstr);
        // TODO do this with the int[] version, so the for-loop is unnecessary
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int pixel = qimage.pixel(i, j);
                // if it is transparent, it is useless to copy it...
                if (pixel != 0) {
                    env->CallVoidMethod(image, mid, i, j, pixel);
                }
            }
        }
    }
    

    Kind regards,
    Votaily

     

    Last edit: Thom Castermans 2012-09-10
    • Thom Castermans

      Thom Castermans - 2012-09-10

      Hi Vitaliy,

      As you can see I edited your post, so that the code is better readable :)

      Thanks a bunch for your patch! I will test this tomorrow and add it to the unstable version of Joxy if it works, but I guess you are a more experienced C-programmer than both Willem and I are, so there will probably be no problem. Of course, if it works, I will add your name to the file, because you deserve some credit. Can I just use "Vitaliy" or "Votaily" with a link to your SourceForge profile?

      Kind regards,
      Thom

       

      Last edit: Thom Castermans 2012-09-10
  • Willem3141

    Willem3141 - 2012-10-06

    First of all, sorry for the delay.

    I compiled the new file in, and indeed it works perfectly. So we will commit it and the fix will appear in Joxy 0.1.1 :-)

    (Side note: We also don't know how people are supposed to send in patches on SourceForge, we are new here too... But this method works fine I think.)

    Again, thank you very much for your help!

    Willem

     
  • Lukas Jirkovsky

    Lukas Jirkovsky - 2012-11-09

    Willem: the bugtracker can be used as a patch tracker too.

    Anyway, it would be great if the users can also report bugs in the bugtracker, because in my opinion tracking bugs using discussion is not very handy.

     
    • Willem3141

      Willem3141 - 2012-11-10

      Hello Lukas,

      It was intended that every logged-in user is able to file bug reports, but it seems we set the permissions incorrectly for the bug tracker.
      Could you please verify that you are now also able to send reports?

      Thanks!
      Willem

       
      • Lukas Jirkovsky

        Lukas Jirkovsky - 2012-11-11

        Thank you Willem,
        I can confirm there's now an option to create a new ticket. I haven't tried to report one though.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.