How to write extended charset as string literal?

Former Member
Former Member
I'd like to dc.drawText() a degree symbol in my app. How can I do that in Monkey C?

I've tried:

°C => nothing is printed
°C => literal "°C" is printed
\u00B0C => compiler error "Unknown string sequence"

Thanks
  • I think there are several issues at play here.

    First, it seems that compiler doesn't accept hexadecimal or octal escaped character codes (i.e., "\x31" and "\061" are equivalent to "1"). This is the convention in most programming languages for describing non-printable or extended character codes, and it would be nice if the monkeyc compiler would support them as well.

    Second, it appears that the source character set supports extended characters if you write them out, but the execution character set does not. In other words, you can put the degree symbol into the source code and compile it without issue, but running the program produces unusual behavior. If you use the built-in fonts, they don't appear to have support for the extended characters, and you get no text displayed at all. If you follow the documentation and built your own font, you get output, but the extended characters are rendered as jibberish.

    Third, I noticed that while testing this using custom fonts, that they are rendered using the top-left corner as the origin, whereas the built-in fonts use the bottom-left corner. The former is consistent with what I'd expect for rendering a font (since the coordinate system origin is at the top left corner of the display). This is a pain in the rear if you want to switch to a custom font because all of your layout positioning will need to change. The following test case illustrates the problem.

    using Toybox.WatchUi as Ui;
    using Toybox.Application as App;
    using Toybox.System as Sys;
    using Toybox.Graphics as Gfx;

    class DataField extends Ui.DataField
    {
    var fonts;
    var index;

    //! Constructor
    function initialize()
    {
    index = 0;

    fonts = [
    Gfx.FONT_LARGE,
    Ui.loadResource(Rez.Fonts.id_font_arial_30),
    Ui.loadResource(Rez.Fonts.id_font_blackdiamond),
    Ui.loadResource(Rez.Fonts.id_font_tahoma_30),
    Ui.loadResource(Rez.Fonts.id_font_veranda_30)
    ];
    }

    function compute(info)
    {
    }

    //! Handle the update event
    function onUpdate(dc)
    {
    if (index == 0) {
    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
    }
    else {
    dc.setColor(Gfx.COLOR_YELLOW, Gfx.COLOR_BLACK);
    }
    dc.clear();

    var font = fonts[index];
    index += 1;
    index %= fonts.size();

    var text = "01234";

    var height = dc.getFontHeight(font);
    var width = dc.getTextWidthInPixels(text, font);

    var x = 4;
    var y = 4;

    // when rendering built-in fonts, the y coordinate is always the base
    // of the text, so we have to add the text height to our offset
    dc.drawText(x, y + height, font, text, Gfx.TEXT_JUSTIFY_LEFT);

    // draw a rectangle where we expect the text to appear
    dc.drawRectangle(x, y, width, height);
    }
    }


    As you'd expect, this breaks rendering of text using Ui.Text, which would make using layouts with text really frustrating.

    Travis
  • Former Member
    Former Member over 10 years ago
    Thanks for looking at these issues. We will investigate custom font characters and the positioning of text.
  • Yup, that's an unimplemented feature in the compiler. We are on it.

    -Alpha Monkey