[kandinsky | code] Coding style improvement in the new font (part 1)

This commit is contained in:
Laury
2022-07-01 21:11:45 +02:00
parent 99b070a30d
commit d87c67d35b
7 changed files with 36 additions and 107 deletions

View File

@@ -31,95 +31,6 @@ bool isItalic(mp_token_kind_t tokenKind) {
if (tokenKind == MP_TOKEN_STRING) {
return true;
}
static_assert(MP_TOKEN_ELLIPSIS + 1 == MP_TOKEN_KW_FALSE
&& MP_TOKEN_KW_FALSE + 1 == MP_TOKEN_KW_NONE
&& MP_TOKEN_KW_NONE + 1 == MP_TOKEN_KW_TRUE
&& MP_TOKEN_KW_TRUE + 1 == MP_TOKEN_KW___DEBUG__
&& MP_TOKEN_KW___DEBUG__ + 1 == MP_TOKEN_KW_AND
&& MP_TOKEN_KW_AND + 1 == MP_TOKEN_KW_AS
&& MP_TOKEN_KW_AS + 1 == MP_TOKEN_KW_ASSERT
/* Here there are keywords that depend on MICROPY_PY_ASYNC_AWAIT, we do
* not test them */
&& MP_TOKEN_KW_BREAK + 1 == MP_TOKEN_KW_CLASS
&& MP_TOKEN_KW_CLASS + 1 == MP_TOKEN_KW_CONTINUE
&& MP_TOKEN_KW_CONTINUE + 1 == MP_TOKEN_KW_DEF
&& MP_TOKEN_KW_DEF + 1 == MP_TOKEN_KW_DEL
&& MP_TOKEN_KW_DEL + 1 == MP_TOKEN_KW_ELIF
&& MP_TOKEN_KW_ELIF + 1 == MP_TOKEN_KW_ELSE
&& MP_TOKEN_KW_ELSE + 1 == MP_TOKEN_KW_EXCEPT
&& MP_TOKEN_KW_EXCEPT + 1 == MP_TOKEN_KW_FINALLY
&& MP_TOKEN_KW_FINALLY + 1 == MP_TOKEN_KW_FOR
&& MP_TOKEN_KW_FOR + 1 == MP_TOKEN_KW_FROM
&& MP_TOKEN_KW_FROM + 1 == MP_TOKEN_KW_GLOBAL
&& MP_TOKEN_KW_GLOBAL + 1 == MP_TOKEN_KW_IF
&& MP_TOKEN_KW_IF + 1 == MP_TOKEN_KW_IMPORT
&& MP_TOKEN_KW_IMPORT + 1 == MP_TOKEN_KW_IN
&& MP_TOKEN_KW_IN + 1 == MP_TOKEN_KW_IS
&& MP_TOKEN_KW_IS + 1 == MP_TOKEN_KW_LAMBDA
&& MP_TOKEN_KW_LAMBDA + 1 == MP_TOKEN_KW_NONLOCAL
&& MP_TOKEN_KW_NONLOCAL + 1 == MP_TOKEN_KW_NOT
&& MP_TOKEN_KW_NOT + 1 == MP_TOKEN_KW_OR
&& MP_TOKEN_KW_OR + 1 == MP_TOKEN_KW_PASS
&& MP_TOKEN_KW_PASS + 1 == MP_TOKEN_KW_RAISE
&& MP_TOKEN_KW_RAISE + 1 == MP_TOKEN_KW_RETURN
&& MP_TOKEN_KW_RETURN + 1 == MP_TOKEN_KW_TRY
&& MP_TOKEN_KW_TRY + 1 == MP_TOKEN_KW_WHILE
&& MP_TOKEN_KW_WHILE + 1 == MP_TOKEN_KW_WITH
&& MP_TOKEN_KW_WITH + 1 == MP_TOKEN_KW_YIELD
&& MP_TOKEN_KW_YIELD + 1 == MP_TOKEN_OP_ASSIGN
&& MP_TOKEN_OP_ASSIGN + 1 == MP_TOKEN_OP_TILDE,
"MP_TOKEN order changed, so Code::PythonTextArea::TokenColor might need to change too.");
if (tokenKind >= MP_TOKEN_KW_FALSE && tokenKind <= MP_TOKEN_KW_YIELD) {
return true;
}
static_assert(MP_TOKEN_OP_TILDE + 1 == MP_TOKEN_OP_LESS
&& MP_TOKEN_OP_LESS + 1 == MP_TOKEN_OP_MORE
&& MP_TOKEN_OP_MORE + 1 == MP_TOKEN_OP_DBL_EQUAL
&& MP_TOKEN_OP_DBL_EQUAL + 1 == MP_TOKEN_OP_LESS_EQUAL
&& MP_TOKEN_OP_LESS_EQUAL + 1 == MP_TOKEN_OP_MORE_EQUAL
&& MP_TOKEN_OP_MORE_EQUAL + 1 == MP_TOKEN_OP_NOT_EQUAL
&& MP_TOKEN_OP_NOT_EQUAL + 1 == MP_TOKEN_OP_PIPE
&& MP_TOKEN_OP_PIPE + 1 == MP_TOKEN_OP_CARET
&& MP_TOKEN_OP_CARET + 1 == MP_TOKEN_OP_AMPERSAND
&& MP_TOKEN_OP_AMPERSAND + 1 == MP_TOKEN_OP_DBL_LESS
&& MP_TOKEN_OP_DBL_LESS + 1 == MP_TOKEN_OP_DBL_MORE
&& MP_TOKEN_OP_DBL_MORE + 1 == MP_TOKEN_OP_PLUS
&& MP_TOKEN_OP_PLUS + 1 == MP_TOKEN_OP_MINUS
&& MP_TOKEN_OP_MINUS + 1 == MP_TOKEN_OP_STAR
&& MP_TOKEN_OP_STAR + 1 == MP_TOKEN_OP_AT
&& MP_TOKEN_OP_AT + 1 == MP_TOKEN_OP_DBL_SLASH
&& MP_TOKEN_OP_DBL_SLASH + 1 == MP_TOKEN_OP_SLASH
&& MP_TOKEN_OP_SLASH + 1 == MP_TOKEN_OP_PERCENT
&& MP_TOKEN_OP_PERCENT + 1 == MP_TOKEN_OP_DBL_STAR
&& MP_TOKEN_OP_DBL_STAR + 1 == MP_TOKEN_DEL_PIPE_EQUAL
&& MP_TOKEN_DEL_PIPE_EQUAL + 1 == MP_TOKEN_DEL_CARET_EQUAL
&& MP_TOKEN_DEL_CARET_EQUAL + 1 == MP_TOKEN_DEL_AMPERSAND_EQUAL
&& MP_TOKEN_DEL_AMPERSAND_EQUAL + 1 == MP_TOKEN_DEL_DBL_LESS_EQUAL
&& MP_TOKEN_DEL_DBL_LESS_EQUAL + 1 == MP_TOKEN_DEL_DBL_MORE_EQUAL
&& MP_TOKEN_DEL_DBL_MORE_EQUAL + 1 == MP_TOKEN_DEL_PLUS_EQUAL
&& MP_TOKEN_DEL_PLUS_EQUAL + 1 == MP_TOKEN_DEL_MINUS_EQUAL
&& MP_TOKEN_DEL_MINUS_EQUAL + 1 == MP_TOKEN_DEL_STAR_EQUAL
&& MP_TOKEN_DEL_STAR_EQUAL + 1 == MP_TOKEN_DEL_AT_EQUAL
&& MP_TOKEN_DEL_AT_EQUAL + 1 == MP_TOKEN_DEL_DBL_SLASH_EQUAL
&& MP_TOKEN_DEL_DBL_SLASH_EQUAL + 1 == MP_TOKEN_DEL_SLASH_EQUAL
&& MP_TOKEN_DEL_SLASH_EQUAL + 1 == MP_TOKEN_DEL_PERCENT_EQUAL
&& MP_TOKEN_DEL_PERCENT_EQUAL + 1 == MP_TOKEN_DEL_DBL_STAR_EQUAL
&& MP_TOKEN_DEL_DBL_STAR_EQUAL + 1 == MP_TOKEN_DEL_PAREN_OPEN
&& MP_TOKEN_DEL_PAREN_OPEN + 1 == MP_TOKEN_DEL_PAREN_CLOSE
&& MP_TOKEN_DEL_PAREN_CLOSE + 1 == MP_TOKEN_DEL_BRACKET_OPEN
&& MP_TOKEN_DEL_BRACKET_OPEN + 1 == MP_TOKEN_DEL_BRACKET_CLOSE
&& MP_TOKEN_DEL_BRACKET_CLOSE + 1 == MP_TOKEN_DEL_BRACE_OPEN
&& MP_TOKEN_DEL_BRACE_OPEN + 1 == MP_TOKEN_DEL_BRACE_CLOSE
&& MP_TOKEN_DEL_BRACE_CLOSE + 1 == MP_TOKEN_DEL_COMMA
&& MP_TOKEN_DEL_COMMA + 1 == MP_TOKEN_DEL_COLON
&& MP_TOKEN_DEL_COLON + 1 == MP_TOKEN_DEL_PERIOD
&& MP_TOKEN_DEL_PERIOD + 1 == MP_TOKEN_DEL_SEMICOLON
&& MP_TOKEN_DEL_SEMICOLON + 1 == MP_TOKEN_DEL_EQUAL
&& MP_TOKEN_DEL_EQUAL + 1 == MP_TOKEN_DEL_MINUS_MORE,
"MP_TOKEN order changed, so Code::PythonTextArea::TokenColor might need to change too.");
return false;
}
@@ -393,8 +304,8 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char
// If the token is being autocompleted, use DefaultColor/Font
KDColor color = (tokenFrom <= autocompleteStart && autocompleteStart < tokenEnd) ? Palette::CodeText : TokenColor(lex->tok_kind);
bool font = (tokenFrom <= autocompleteStart && autocompleteStart < tokenEnd) ? false:isItalic(lex->tok_kind);
bool italic = (tokenFrom <= autocompleteStart && autocompleteStart < tokenEnd) ? false : isItalic(lex->tok_kind);
LOG_DRAW("Draw \"%.*s\" for token %d\n", tokenLength, tokenFrom, lex->tok_kind);
drawStringAt(ctx, line,
UTF8Helper::GlyphOffsetAtCodePoint(text, tokenFrom),
@@ -405,7 +316,8 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char
selectionStart,
selectionEnd,
HighlightColor,
font);
italic
);
mp_lexer_to_next(lex);
LOG_DRAW("Pop token %d\n", lex->tok_kind);

View File

@@ -111,12 +111,10 @@ protected:
m_cursorLocation = m_text.text();
}
void drawRect(KDContext * ctx, KDRect rect) const override;
void drawStringAt(KDContext * ctx, int line, int column, const char * text, int length, KDColor textColor, KDColor backgroundColor, const char * selectionStart, const char * selectionEnd, KDColor backgroundHighlightColor,bool isItalic = false) const;
void drawStringAt(KDContext * ctx, int line, int column, const char * text, int length, KDColor textColor, KDColor backgroundColor, const char * selectionStart, const char * selectionEnd, KDColor backgroundHighlightColor, bool isItalic = false) const;
virtual void drawLine(KDContext * ctx, int line, const char * text, size_t length, int fromColumn, int toColumn, const char * selectionStart, const char * selectionEnd) const = 0;
virtual void clearRect(KDContext * ctx, KDRect rect) const = 0;
KDSize minimalSizeForOptimalDisplay() const override;
KDFont * usedFont;
KDFont * ItalicFont;
void setText(char * textBuffer, size_t textBufferSize);
const char * text() const override { return m_text.text(); }
const char * editedText() const override { return m_text.text(); }

View File

@@ -516,13 +516,15 @@ void TextArea::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
void TextArea::ContentView::drawStringAt(KDContext * ctx, int line, int column, const char * text, int length, KDColor textColor, KDColor backgroundColor, const char * selectionStart, const char * selectionEnd, KDColor backgroundHighlightColor, bool isItalic) const {
if (length < 0) {
return;
}
const KDFont * ItalicFont = (m_font == KDFont::LargeFont) ? KDFont::ItalicLargeFont : KDFont::ItalicSmallFont;
const KDFont * usedFont = isItalic ? ItalicFont : m_font;
}
const KDFont * usedFont = m_font;
if (isItalic) {
usedFont = m_font->toItalic();
}
KDSize glyphSize = usedFont->glyphSize();
bool drawSelection = selectionStart != nullptr && selectionEnd > text && selectionStart < text + length;
KDPoint nextPoint = ctx->drawString(
@@ -563,7 +565,7 @@ KDSize TextArea::ContentView::minimalSizeForOptimalDisplay() const {
return KDSize(
/* We take into account the space required to draw a cursor at the end of
* line by adding glyphSize.width() to the width. */
span.width() + m_font->glyphSize().width() + 4,
span.width() + m_font->glyphSize().width(),
span.height()
);
}

View File

@@ -87,12 +87,13 @@ simple_kandinsky_src += $(addprefix kandinsky/fonts/, \
$(eval $(call raster_font,SmallFont,SmallFontExtended,1,12,7,14))
$(eval $(call raster_font,ItalicSmallFont,ItalicSmallFontExtended,1,12,7,14))
$(eval $(call raster_font,LargeFont,LargeFontExtended,1,16,10,18))
$(eval $(call raster_font,ItalicSmallFont,ItalicSmallFontExtended,1,12,7,14))
$(eval $(call raster_font,ItalicLargeFont,ItalicLargeFontExtended,1,16,10,18))
$(eval $(call raster_font,SmallFont,SmallFontSimple,0,12,7,14))
$(eval $(call raster_font,LargeFont,LargeFontSimple,0,16,10,18))
else
kandinsky_src += $(addprefix kandinsky/fonts/, \
@@ -106,7 +107,7 @@ default_kandinsky_src = $(kandinsky_src)
simple_kandinsky_src = $(kandinsky_src)
$(eval $(call raster_font,SmallFont,SmallFontSimple,0,12,7,14))
$(eval $(call raster_font,ItalicSmallFont,ItalicSmallFontSimple,0,12,7,14))
$(eval $(call raster_font,LargeFont,LargeFontSimple,0,16,10,18))
$(eval $(call raster_font,ItalicSmallFont,ItalicSmallFontSimple,0,12,7,14))
$(eval $(call raster_font,ItalicLargeFont,ItalicLargeFontSimple,0,16,10,18))
endif

View File

@@ -123,15 +123,20 @@ int main(int argc, char * argv[]) {
}
int glyph_width = maxWidth-1;
if (glyph_width == 9) { glyph_width = 10; } /* This was made to avoid a problem, the ratio of the width by the height was not */
if (glyph_width == 8) { glyph_width = 7; } /* adequate and the fonts couldn't be compiled, this is useless with other fonts */
if (glyph_width == 9) { // FIXME: This is a TEMPORARY FIX, we should instead fix the ttf file
glyph_width += 1;
} else if (glyph_width == 8) {
glyph_width -= 1;
}
if (packed_glyph_width != 0) {
ENSURE(glyph_width == packed_glyph_width, "Expecting a packed glyph width of %d but got %d instead", packed_glyph_width, glyph_width);
} else {
printf("Computed packed_glyph_width = %d\n", glyph_width);
}
int glyph_height = maxAboveBaseline+maxBelowBaseline;
if (glyph_height == 13) { glyph_height = 14;} /* Same problem */
if (glyph_height == 13) { // FIXME: Same here
glyph_height += 1;
}
if (packed_glyph_height != 0) {
ENSURE(glyph_height == packed_glyph_height, "Expecting a packed glyph height of %d but got %d instead", packed_glyph_height, glyph_height);
} else {

View File

@@ -78,6 +78,7 @@ public:
return RenderPalette::Gradient(textColor, backgroundColor);
}
KDSize glyphSize() const { return m_glyphSize; }
const KDFont * toItalic() const; // Return the font in italic style, or the font itself if it is already italic
constexpr KDFont(size_t tableLength, const CodePointIndexPair * table, KDCoordinate glyphWidth, KDCoordinate glyphHeight, const uint16_t * glyphDataOffset, const uint8_t * data) :
m_tableLength(tableLength), m_table(table), m_glyphSize(glyphWidth, glyphHeight), m_glyphDataOffset(glyphDataOffset), m_data(data) { }

View File

@@ -41,6 +41,16 @@ KDSize KDFont::stringSizeUntil(const char * text, const char * limit) const {
return stringSize;
}
const KDFont * KDFont::toItalic() const {
if (this == KDFont::LargeFont) {
return KDFont::ItalicLargeFont;
} else if (this == KDFont::SmallFont) {
return KDFont::ItalicSmallFont;
}
return this;
}
void KDFont::setGlyphGrayscalesForCodePoint(CodePoint codePoint, GlyphBuffer * glyphBuffer) const {
fetchGrayscaleGlyphAtIndex(indexForCodePoint(codePoint), glyphBuffer->grayscaleBuffer());
}