summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-10-07 08:49:42 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-10-07 08:49:42 +0300
commitc9ded71342f4e47b48151782a48fc488a5f63e56 (patch)
tree11b0b470f58bc3c39ad3b7d5b1715c6aed98d9b5 /src/ui
parentfa79abfe8bb9fcbdd4cf9cec46e0d1856f4e7020 (diff)
DocumentWidget: Line width based on font glyph width
The idea is that the amount of content should be the same per line regardless of the font.
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/command.h4
-rw-r--r--src/ui/documentwidget.c3
-rw-r--r--src/ui/text.c5
-rw-r--r--src/ui/text.h1
-rw-r--r--src/ui/util.c34
5 files changed, 31 insertions, 16 deletions
diff --git a/src/ui/command.h b/src/ui/command.h
index 8dbaafad..8dcae892 100644
--- a/src/ui/command.h
+++ b/src/ui/command.h
@@ -23,6 +23,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
23#pragma once 23#pragma once
24 24
25#include <the_Foundation/range.h> 25#include <the_Foundation/range.h>
26#include <the_Foundation/string.h>
26#include <the_Foundation/vec2.h> 27#include <the_Foundation/vec2.h>
27 28
28iBool equal_Command (const char *commandWithArgs, const char *command); 29iBool equal_Command (const char *commandWithArgs, const char *command);
@@ -45,3 +46,6 @@ iString * suffix_Command (const char *, const char *label); /* until
45iLocalDef iBool hasLabel_Command(const char *d, const char *label) { 46iLocalDef iBool hasLabel_Command(const char *d, const char *label) {
46 return suffixPtr_Command(d, label) != NULL; 47 return suffixPtr_Command(d, label) != NULL;
47} 48}
49iLocalDef const char *cstr_Command(const char *d, const char *label) {
50 return cstr_Rangecc(range_Command(d, label));
51}
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index b83490f9..2ba2fa12 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -496,7 +496,8 @@ static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
496 -1.0f, 10.0f); /* adapt to width */ 496 -1.0f, 10.0f); /* adapt to width */
497 //printf("%f\n", adjust); fflush(stdout); 497 //printf("%f\n", adjust); fflush(stdout);
498 return iMini(iMax(minWidth, bounds.size.x - gap_UI * (d->pageMargin + adjust) * 2), 498 return iMini(iMax(minWidth, bounds.size.x - gap_UI * (d->pageMargin + adjust) * 2),
499 fontSize_UI * prefs->lineWidth * prefs->zoomPercent / 100); 499 fontSize_UI * emRatio_Text(paragraph_FontId) * /* dependent on avg. glyph width */
500 prefs->lineWidth * prefs->zoomPercent / 100);
500} 501}
501 502
502static iRect documentBounds_DocumentWidget_(const iDocumentWidget *d) { 503static iRect documentBounds_DocumentWidget_(const iDocumentWidget *d) {
diff --git a/src/ui/text.c b/src/ui/text.c
index 25c79e23..8adfa019 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -1882,6 +1882,11 @@ int lineHeight_Text(int fontId) {
1882 return font_Text_(fontId)->height; 1882 return font_Text_(fontId)->height;
1883} 1883}
1884 1884
1885float emRatio_Text(int fontId) {
1886 const iFont *font = font_Text_(fontId);
1887 return font->emAdvance / font->height;
1888}
1889
1885iTextMetrics measureRange_Text(int fontId, iRangecc text) { 1890iTextMetrics measureRange_Text(int fontId, iRangecc text) {
1886 if (isEmpty_Range(&text)) { 1891 if (isEmpty_Range(&text)) {
1887 return (iTextMetrics){ init_Rect(0, 0, 0, lineHeight_Text(fontId)), zero_I2() }; 1892 return (iTextMetrics){ init_Rect(0, 0, 0, lineHeight_Text(fontId)), zero_I2() };
diff --git a/src/ui/text.h b/src/ui/text.h
index f76c8125..af69795a 100644
--- a/src/ui/text.h
+++ b/src/ui/text.h
@@ -148,6 +148,7 @@ void setContentFontSize_Text (iText *, float fontSizeFactor); /* affects all
148void resetFonts_Text (iText *); 148void resetFonts_Text (iText *);
149 149
150int lineHeight_Text (int fontId); 150int lineHeight_Text (int fontId);
151float emRatio_Text (int fontId); /* em advance to line height ratio */
151iRect visualBounds_Text (int fontId, iRangecc text); 152iRect visualBounds_Text (int fontId, iRangecc text);
152 153
153iDeclareType(TextMetrics) 154iDeclareType(TextMetrics)
diff --git a/src/ui/util.c b/src/ui/util.c
index a4411ea5..354b1467 100644
--- a/src/ui/util.c
+++ b/src/ui/util.c
@@ -2169,6 +2169,15 @@ iWidget *makePreferences_Widget(void) {
2169 { "${prefs.imagestyle.preformat}", 0, 0, format_CStr("imagestyle.set arg:%d", preformatColorized_ImageStyle) }, 2169 { "${prefs.imagestyle.preformat}", 0, 0, format_CStr("imagestyle.set arg:%d", preformatColorized_ImageStyle) },
2170 { NULL } 2170 { NULL }
2171 }; 2171 };
2172 const iMenuItem lineWidthItems[] = {
2173 { "button id:prefs.linewidth.50 text:\u20132", 0, 0, "linewidth.set arg:50" },
2174 { "button id:prefs.linewidth.58 text:\u20131", 0, 0, "linewidth.set arg:58" },
2175 { "button id:prefs.linewidth.66 label:prefs.linewidth.normal", 0, 0, "linewidth.set arg:66" },
2176 { "button id:prefs.linewidth.76 text:+1", 0, 0, "linewidth.set arg:76" },
2177 { "button id:prefs.linewidth.86 text:+2", 0, 0, "linewidth.set arg:86" },
2178 { "button id:prefs.linewidth.1000 label:prefs.linewidth.fill", 0, 0, "linewidth.set arg:1000" },
2179 { NULL }
2180 };
2172 /* Create the Preferences UI. */ 2181 /* Create the Preferences UI. */
2173 if (isUsingPanelLayout_Mobile()) { 2182 if (isUsingPanelLayout_Mobile()) {
2174 const iMenuItem pinSplitItems[] = { 2183 const iMenuItem pinSplitItems[] = {
@@ -2206,15 +2215,6 @@ iWidget *makePreferences_Widget(void) {
2206 { "button id:prefs.boldlink.light" }, 2215 { "button id:prefs.boldlink.light" },
2207 { NULL } 2216 { NULL }
2208 }; 2217 };
2209 const iMenuItem lineWidthItems[] = {
2210 { "button id:prefs.linewidth.30 text:\u20132", 0, 0, "linewidth.set arg:30" },
2211 { "button id:prefs.linewidth.34 text:\u20131", 0, 0, "linewidth.set arg:34" },
2212 { "button id:prefs.linewidth.38 label:prefs.linewidth.normal", 0, 0, "linewidth.set arg:38" },
2213 { "button id:prefs.linewidth.43 text:+1", 0, 0, "linewidth.set arg:43" },
2214 { "button id:prefs.linewidth.48 text:+2", 0, 0, "linewidth.set arg:48" },
2215 { "button id:prefs.linewidth.1000 label:prefs.linewidth.fill", 0, 0, "linewidth.set arg:1000" },
2216 { NULL }
2217 };
2218 const iMenuItem quoteItems[] = { 2218 const iMenuItem quoteItems[] = {
2219 { "button id:prefs.quoteicon.1 label:prefs.quoteicon.icon", 0, 0, "quoteicon.set arg:1" }, 2219 { "button id:prefs.quoteicon.1 label:prefs.quoteicon.icon", 0, 0, "quoteicon.set arg:1" },
2220 { "button id:prefs.quoteicon.0 label:prefs.quoteicon.line", 0, 0, "quoteicon.set arg:0" }, 2220 { "button id:prefs.quoteicon.0 label:prefs.quoteicon.line", 0, 0, "quoteicon.set arg:0" },
@@ -2541,12 +2541,16 @@ iWidget *makePreferences_Widget(void) {
2541 addChild_Widget(headings, iClob(makeHeading_Widget("${prefs.linewidth}"))); 2541 addChild_Widget(headings, iClob(makeHeading_Widget("${prefs.linewidth}")));
2542 iWidget *widths = new_Widget(); 2542 iWidget *widths = new_Widget();
2543 /* Line widths. */ { 2543 /* Line widths. */ {
2544 addRadioButton_(widths, "prefs.linewidth.30", "\u20132", "linewidth.set arg:30"); 2544 /* TODO: Make this a utility function to build radio buttons from items. */
2545 addRadioButton_(widths, "prefs.linewidth.34", "\u20131", "linewidth.set arg:34"); 2545 for (size_t i = 0; lineWidthItems[i].label; i++) {
2546 addRadioButton_(widths, "prefs.linewidth.38", "${prefs.linewidth.normal}", "linewidth.set arg:38"); 2546 const iMenuItem *lw = &lineWidthItems[i];
2547 addRadioButton_(widths, "prefs.linewidth.43", "+1", "linewidth.set arg:43"); 2547 addRadioButton_(widths,
2548 addRadioButton_(widths, "prefs.linewidth.48", "+2", "linewidth.set arg:48"); 2548 cstr_Command(lw->label, "id"),
2549 addRadioButton_(widths, "prefs.linewidth.1000", "${prefs.linewidth.fill}", "linewidth.set arg:1000"); 2549 hasLabel_Command(lw->label, "label")
2550 ? cstr_Lang(cstr_Command(lw->label, "label"))
2551 : cstr_Command(lw->label, "text"),
2552 lw->command);
2553 }
2550 } 2554 }
2551 addChildFlags_Widget(values, iClob(widths), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag); 2555 addChildFlags_Widget(values, iClob(widths), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
2552 addPrefsInputWithHeading_(headings, values, "prefs.linespacing", iClob(new_InputWidget(5))); 2556 addPrefsInputWithHeading_(headings, values, "prefs.linespacing", iClob(new_InputWidget(5)));