@@ -517,15 +517,15 @@ STATIC const unsigned char *get_font_data_from_char(char c) {
517517
518518STATIC mp_int_t get_pixel_from_font_data (const unsigned char *data, int x, int y) {
519519 /* The following logic belongs in MicroBitFont */
520- return ((data[y]>>(4 -x))&1 )*MAX_BRIGHTNESS ;
520+ return ((data[y]>>(4 -x))&1 );
521521}
522522
523523microbit_image_obj_t *microbit_image_for_char (char c) {
524524 const unsigned char *data = get_font_data_from_char (c);
525525 greyscale_t *result = greyscale_new (5 ,5 );
526526 for (int x = 0 ; x < 5 ; ++x) {
527527 for (int y = 0 ; y < 5 ; ++y) {
528- result->setPixelValue (x, y, get_pixel_from_font_data (data, x, y));
528+ result->setPixelValue (x, y, get_pixel_from_font_data (data, x, y)*MAX_BRIGHTNESS );
529529 }
530530 }
531531 return (microbit_image_obj_t *)result;
@@ -702,6 +702,7 @@ typedef struct _scrolling_string_t {
702702 char const *str;
703703 mp_uint_t len;
704704 mp_obj_t ref;
705+ bool monospace;
705706} scrolling_string_t ;
706707
707708typedef struct _scrolling_string_iterator_t {
@@ -711,21 +712,44 @@ typedef struct _scrolling_string_iterator_t {
711712 char const *next_char;
712713 char const *end;
713714 uint8_t offset;
715+ uint8_t offset_limit;
716+ bool monospace;
714717 char right;
715718} scrolling_string_iterator_t ;
716719
717720extern const mp_obj_type_t microbit_scrolling_string_type;
718721extern const mp_obj_type_t microbit_scrolling_string_iterator_type;
719722
720- mp_obj_t scrolling_string_image_iterable (const char * str, mp_uint_t len, mp_obj_t ref) {
723+ mp_obj_t scrolling_string_image_iterable (const char * str, mp_uint_t len, mp_obj_t ref, bool monospace ) {
721724 scrolling_string_t *result = m_new_obj (scrolling_string_t );
722725 result->base .type = µbit_scrolling_string_type;
723726 result->str = str;
724727 result->len = len;
725728 result->ref = ref;
729+ result->monospace = monospace;
726730 return result;
727731}
728732
733+ STATIC int font_column_non_blank (const unsigned char *font_data, unsigned int col) {
734+ for (int y = 0 ; y < 5 ; ++y) {
735+ if (get_pixel_from_font_data (font_data, col, y)) {
736+ return 1 ;
737+ }
738+ }
739+ return 0 ;
740+ }
741+
742+ /* Not strictly the rightmost non-blank column, but the rightmost in columns 2,3 or 4. */
743+ STATIC unsigned int rightmost_non_blank_column (const unsigned char *font_data) {
744+ if (font_column_non_blank (font_data, 4 )) {
745+ return 4 ;
746+ }
747+ if (font_column_non_blank (font_data, 3 )) {
748+ return 3 ;
749+ }
750+ return 2 ;
751+ }
752+
729753STATIC mp_obj_t get_microbit_scrolling_string_iter (mp_obj_t o_in) {
730754 scrolling_string_t *str = (scrolling_string_t *)o_in;
731755 scrolling_string_iterator_t *result = m_new_obj (scrolling_string_iterator_t );
@@ -734,11 +758,19 @@ STATIC mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in) {
734758 result->offset = 0 ;
735759 result->next_char = str->str ;
736760 result->ref = str->ref ;
761+ result->monospace = str->monospace ;
737762 result->end = result->next_char + str->len ;
738- if (str->len )
763+ if (str->len ) {
739764 result->right = *result->next_char ;
740- else
765+ if (result->monospace ) {
766+ result->offset_limit = 5 ;
767+ } else {
768+ result->offset_limit = rightmost_non_blank_column (get_font_data_from_char (result->right )) + 1 ;
769+ }
770+ } else {
741771 result->right = ' ' ;
772+ result->offset_limit = 5 ;
773+ }
742774 return result;
743775}
744776
@@ -749,19 +781,30 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) {
749781 }
750782 greyscale_t *result = iter->img ->shiftLeft (1 );
751783 iter->img = (microbit_image_obj_t *)result;
752- const unsigned char *font_data = get_font_data_from_char (iter->right );
753- if (iter->offset < 5 ) {
784+ const unsigned char *font_data;
785+ if (iter->offset < iter->offset_limit ) {
786+ font_data = get_font_data_from_char (iter->right );
754787 for (int y = 0 ; y < 5 ; ++y) {
755- int pix = get_pixel_from_font_data (font_data, iter->offset , y);
788+ int pix = get_pixel_from_font_data (font_data, iter->offset , y)*MAX_BRIGHTNESS ;
756789 result->setPixelValue (4 , y, pix);
757790 }
758- } else if (iter->offset == 6 ) {
791+ } else if (iter->offset == iter-> offset_limit ) {
759792 ++iter->next_char ;
760- if (iter->next_char == iter->end )
793+ if (iter->next_char == iter->end ) {
761794 iter->right = ' ' ;
762- else
795+ iter->offset_limit = 5 ;
796+ iter->offset = 0 ;
797+ } else {
763798 iter->right = *iter->next_char ;
764- iter->offset = -1 ;
799+ font_data = get_font_data_from_char (iter->right );
800+ if (iter->monospace ) {
801+ iter->offset = -1 ;
802+ iter->offset_limit = 5 ;
803+ } else {
804+ iter->offset = -font_column_non_blank (font_data, 0 );
805+ iter->offset_limit = rightmost_non_blank_column (font_data)+1 ;
806+ }
807+ }
765808 }
766809 ++iter->offset ;
767810 return result;
0 commit comments