From 784ca2d73117c044cfb20ce75e51c4269c9cbfa8 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 4 Jan 2022 08:23:47 +0200 Subject: Text: Light and regular font weight via ANSI escapes SGR codes 2 and 10. --- src/gmdocument.c | 18 ++++++++++++------ src/ui/text.c | 25 ++++++++++++++++++++++++- src/ui/text.h | 3 +++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/gmdocument.c b/src/gmdocument.c index fd13bc82..60773e0b 100644 --- a/src/gmdocument.c +++ b/src/gmdocument.c @@ -556,16 +556,22 @@ static const int maxLedeLines_ = 10; static void applyAttributes_RunTypesetter_(iRunTypesetter *d, iTextAttrib attrib) { /* WARNING: This is duplicated in run_Font_(). Make sure they behave identically. */ - if (attrib.bold) { - d->run.font = fontWithStyle_Text(d->baseFont, bold_FontStyle); - d->run.color = tmFirstParagraph_ColorId; + if (attrib.monospace) { + d->run.font = fontWithFamily_Text(d->baseFont, monospace_FontId); + d->run.color = tmPreformatted_ColorId; } else if (attrib.italic) { d->run.font = fontWithStyle_Text(d->baseFont, italic_FontStyle); } - else if (attrib.monospace) { - d->run.font = fontWithFamily_Text(d->baseFont, monospace_FontId); - d->run.color = tmPreformatted_ColorId; + else if (attrib.regular) { + d->run.font = fontWithStyle_Text(d->baseFont, regular_FontStyle); + } + else if (attrib.bold) { + d->run.font = fontWithStyle_Text(d->baseFont, bold_FontStyle); + d->run.color = tmFirstParagraph_ColorId; + } + else if (attrib.light) { + d->run.font = fontWithStyle_Text(d->baseFont, light_FontStyle); } else { d->run.font = d->baseFont; diff --git a/src/ui/text.c b/src/ui/text.c index 116c5eba..4b43f872 100644 --- a/src/ui/text.c +++ b/src/ui/text.c @@ -453,6 +453,10 @@ void setAnsiFlags_Text(int ansiFlags) { activeText_->ansiFlags = ansiFlags; } +int ansiFlags_Text(void) { + return activeText_->ansiFlags; +} + void setDocumentFontSize_Text(iText *d, float fontSizeFactor) { fontSizeFactor *= contentScale_Text_; iAssert(fontSizeFactor > 0); @@ -834,7 +838,6 @@ static void prepare_AttributedText_(iAttributedText *d, int overrideBaseDir, iCh .font = d->font, }; const int *logToSource = constData_Array(&d->logicalToSourceOffset); - const int * logToVis = constData_Array(&d->logicalToVisual); const iChar * logicalText = constData_Array(&d->logical); iBool isRTL = d->isBaseRTL; int numNonSpace = 0; @@ -874,17 +877,34 @@ static void prepare_AttributedText_(iAttributedText *d, int overrideBaseDir, iCh /* Note: This styling is hardcoded to match `typesetOneLine_RunTypesetter_()`. */ if (ansi & allowFontStyle_AnsiFlag && equal_Rangecc(sequence, "1")) { run.attrib.bold = iTrue; + run.attrib.regular = iFalse; + run.attrib.light = iFalse; if (d->baseFgColorId == tmParagraph_ColorId) { setFgColor_AttributedRun_(&run, tmFirstParagraph_ColorId); } attribFont = font_Text_(fontWithStyle_Text(fontId_Text_(d->baseFont), bold_FontStyle)); } + else if (ansi & allowFontStyle_AnsiFlag && equal_Rangecc(sequence, "2")) { + run.attrib.light = iTrue; + run.attrib.regular = iFalse; + run.attrib.bold = iFalse; + attribFont = font_Text_(fontWithStyle_Text(fontId_Text_(d->baseFont), + light_FontStyle)); + } else if (ansi & allowFontStyle_AnsiFlag && equal_Rangecc(sequence, "3")) { run.attrib.italic = iTrue; attribFont = font_Text_(fontWithStyle_Text(fontId_Text_(d->baseFont), italic_FontStyle)); } + else if (ansi & allowFontStyle_AnsiFlag && equal_Rangecc(sequence, "10")) { + run.attrib.regular = iTrue; + run.attrib.bold = iFalse; + run.attrib.light = iFalse; + run.attrib.italic = iFalse; + attribFont = font_Text_(fontWithStyle_Text(fontId_Text_(d->baseFont), + regular_FontStyle)); + } else if (ansi & allowFontStyle_AnsiFlag && equal_Rangecc(sequence, "11")) { run.attrib.monospace = iTrue; setFgColor_AttributedRun_(&run, tmPreformatted_ColorId); @@ -892,7 +912,9 @@ static void prepare_AttributedText_(iAttributedText *d, int overrideBaseDir, iCh monospace_FontId)); } else if (equal_Rangecc(sequence, "0")) { + run.attrib.regular = iFalse; run.attrib.bold = iFalse; + run.attrib.light = iFalse; run.attrib.italic = iFalse; run.attrib.monospace = iFalse; attribFont = run.font = d->baseFont; @@ -973,6 +995,7 @@ static void prepare_AttributedText_(iAttributedText *d, int overrideBaseDir, iCh pushBack_Array(&d->runs, &run); } #if 0 + const int *logToVis = constData_Array(&d->logicalToVisual); printf("[AttributedText] %zu runs:\n", size_Array(&d->runs)); iConstForEach(Array, i, &d->runs) { const iAttributedRun *run = i.value; diff --git a/src/ui/text.h b/src/ui/text.h index cb29adad..c8bb6f85 100644 --- a/src/ui/text.h +++ b/src/ui/text.h @@ -161,6 +161,7 @@ enum iAnsiFlag { void setOpacity_Text (float opacity); void setBaseAttributes_Text (int fontId, int fgColorId); /* current "normal" text attributes */ void setAnsiFlags_Text (int ansiFlags); +int ansiFlags_Text (void); void cache_Text (int fontId, iRangecc text); /* pre-render glyphs */ @@ -192,7 +193,9 @@ struct Impl_TextAttrib { int16_t fgColorId; int16_t bgColorId; struct { + uint16_t regular : 1; uint16_t bold : 1; + uint16_t light : 1; uint16_t italic : 1; uint16_t monospace : 1; uint16_t isBaseRTL : 1; -- cgit v1.2.3