Skip to content

Commit c7a09ed

Browse files
committed
Modify CSS generation to enable user specified font size.
Adjustments had to be made to preserve ratio of sizes of baseline content font in relation to heading a mono font styles.
1 parent 410543e commit c7a09ed

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

Src/FrDetailView.pas

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ implementation
107107

108108
uses
109109
// Delphi
110-
SysUtils, Graphics, Menus,
110+
SysUtils, Graphics, Menus, Math,
111111
// Project
112112
ActiveText.UHTMLRenderer, Browser.UHighlighter, Hiliter.UAttrs, Hiliter.UCSS,
113113
Hiliter.UGlobals, UColours, UCSSUtils, UFontHelper, UPreferences, UQuery,
@@ -119,27 +119,53 @@ implementation
119119

120120
procedure TDetailViewFrame.BuildCSS(const CSSBuilder: TCSSBuilder);
121121
var
122-
HiliteAttrs: IHiliteAttrs; // syntax highlighter used to build CSS
123-
CSSFont: TFont; // font used to set CSS properties
122+
HiliteAttrs: IHiliteAttrs; // syntax highlighter used to build CSS
123+
ContentFont: TFont; // default content font sized per preferences
124+
MonoFont: TFont; // default mono font sized per preferences
125+
CSSFont: TFont; // font used to set CSS properties
126+
ContentFontScaleFactor: Single; // amount to increase font size by to get
127+
// proportionally same increase as adding 1 to
128+
// default content font size
129+
MonoToContentFontRatio: Single; // ratio of size of mono font to content font
130+
DefContentFontSize: Integer; // default size of content font
131+
DefMonoFontSize: Integer; // default size of mono font
124132
begin
125133
// NOTE:
126134
// We only set CSS properties that may need to use system colours or fonts
127135
// that may be changed by user or changing program defaults. CSS that controls
128136
// layout remains in a CSS file embedded in resources.
129137
inherited;
138+
ContentFont := nil;
139+
MonoFont := nil;
130140
CSSFont := TFont.Create;
131141
try
142+
MonoFont := TFont.Create;
143+
ContentFont := TFont.Create;
144+
TFontHelper.SetDefaultMonoFont(MonoFont);
145+
TFontHelper.SetContentFont(ContentFont);
146+
// Must do next two lines before changing content & mono font sizes
147+
DefContentFontSize := ContentFont.Size;
148+
DefMonoFontSize := MonoFont.Size;
149+
ContentFontScaleFactor := 1.0 / DefContentFontSize;
150+
MonoToContentFontRatio := DefMonoFontSize / DefContentFontSize;
151+
ContentFont.Size := Preferences.DetailFontSize;
152+
MonoFont.Size := Round(ContentFont.Size * MonoToContentFontRatio);
132153
// Set body style to use program's font and window colour
133154
with CSSBuilder.AddSelector('body') do
134155
begin
135-
TFontHelper.SetContentFont(CSSFont);
156+
CSSFont.Assign(ContentFont);
136157
AddProperty(TCSS.FontProps(CSSFont));
137158
AddProperty(TCSS.BackgroundColorProp(clWindow));
138159
end;
160+
with CSSBuilder.Selectors['code'] do
161+
begin
162+
CSSFont.Assign(MonoFont);
163+
AddProperty(TCSS.FontProps(CSSFont));
164+
end;
139165
// Set table to use required font
140166
with CSSBuilder.AddSelector('table') do
141167
begin
142-
TFontHelper.SetContentFont(CSSFont);
168+
CSSFont.Assign(ContentFont);
143169
AddProperty(TCSS.FontProps(CSSFont));
144170
AddProperty(TCSS.BackgroundColorProp(clBorder));
145171
end;
@@ -149,31 +175,53 @@ procedure TDetailViewFrame.BuildCSS(const CSSBuilder: TCSSBuilder);
149175
// Sets H1 heading font size and border
150176
with CSSBuilder.AddSelector('h1') do
151177
begin
152-
TFontHelper.SetContentFont(CSSFont);
153-
CSSFont.Size := CSSFont.Size + 2;
178+
CSSFont.Assign(ContentFont);
179+
CSSFont.Size := CSSFont.Size + Max(
180+
Round(2 * ContentFontScaleFactor * CSSFont.Size), 2
181+
);
154182
CSSFont.Style := [fsBold];
155183
AddProperty(TCSS.FontProps(CSSFont));
156184
AddProperty(TCSS.BorderProp(cssBottom, 1, cbsSolid, clBorder));
157185
end;
158186
// Sets H2 heading font size and border
159187
with CSSBuilder.AddSelector('h2') do
160188
begin
161-
TFontHelper.SetContentFont(CSSFont);
189+
CSSFont.Assign(ContentFont);
162190
CSSFont.Style := [fsBold];
163191
AddProperty(TCSS.FontProps(CSSFont));
164192
end;
165193
// Set H2 heading font for use in rendered active text
166194
with CSSBuilder.AddSelector('.active-text h2') do
167195
begin
168-
TFontHelper.SetContentFont(CSSFont);
196+
CSSFont.Assign(ContentFont);
169197
CSSFont.Style := [fsBold];
170-
CSSFont.Size := CSSFont.Size + 1;
198+
CSSFont.Size := CSSFont.Size + Max(
199+
Round(ContentFontScaleFactor * CSSFont.Size), 1
200+
);
201+
AddProperty(TCSS.FontProps(CSSFont));
202+
end;
203+
// Set CODE tag within H2 heading for use in rendered active text
204+
with CSSBuilder.AddSelector('.active-text h2 code') do
205+
begin
206+
CSSFont.Assign(MonoFont);
207+
CSSFont.Style := [fsBold];
208+
CSSFont.Size := CSSFont.Size + Max(
209+
Round(ContentFontScaleFactor * CSSFont.Size), 1
210+
);
171211
AddProperty(TCSS.FontProps(CSSFont));
172212
end;
173213
// Set H2 heading font for use in rendered active text in snippet list table
174214
with CSSBuilder.AddSelector('.snippet-list .active-text h2') do
175215
begin
176-
TFontHelper.SetContentFont(CSSFont);
216+
CSSFont.Assign(ContentFont);
217+
CSSFont.Style := [fsBold];
218+
AddProperty(TCSS.FontProps(CSSFont));
219+
end;
220+
// Set CODE within H2 heading font for use in rendered active text in
221+
// snippet list table
222+
with CSSBuilder.AddSelector('.snippet-list .active-text h2 code') do
223+
begin
224+
CSSFont.Assign(MonoFont);
177225
CSSFont.Style := [fsBold];
178226
AddProperty(TCSS.FontProps(CSSFont));
179227
end;
@@ -187,8 +235,8 @@ procedure TDetailViewFrame.BuildCSS(const CSSBuilder: TCSSBuilder);
187235
// Sets CSS for style of New Tab text
188236
with CSSBuilder.AddSelector('#newtab') do
189237
begin
190-
TFontHelper.SetContentFont(CSSFont);
191-
CSSFont.Size := 36;
238+
CSSFont.Assign(ContentFont);
239+
CSSFont.Size := 36 + Round(36 * ContentFontScaleFactor);
192240
CSSFont.Color := clNewTabText;
193241
AddProperty(TCSS.FontProps(CSSFont));
194242
end;
@@ -218,6 +266,8 @@ procedure TDetailViewFrame.BuildCSS(const CSSBuilder: TCSSBuilder);
218266
AddProperty(TCSS.FontWeightProp(cfwNormal));
219267
end;
220268
finally
269+
ContentFont.Free;
270+
MonoFont.Free;
221271
CSSFont.Free;
222272
end;
223273
end;

0 commit comments

Comments
 (0)