From 4ff3aaab44353f35507c908a48078a5781ac8cc0 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 7 May 2022 15:47:09 +0000 Subject: [PATCH] jvcllaz: TJvMarkupLabel|Viewer support sub- and superscripts. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8277 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/jvcllaz/examples/JvMarkup/main.lfm | 3 +- .../jvcllaz/run/JvJans/jvmarkupcommon.pas | 4 ++ .../jvcllaz/run/JvJans/jvmarkuplabel.pas | 41 +++++++++++++++- .../jvcllaz/run/JvJans/jvmarkupviewer.pas | 48 +++++++++++++++++-- 4 files changed, 88 insertions(+), 8 deletions(-) diff --git a/components/jvcllaz/examples/JvMarkup/main.lfm b/components/jvcllaz/examples/JvMarkup/main.lfm index 48abdc294..3ba3e3cd9 100644 --- a/components/jvcllaz/examples/JvMarkup/main.lfm +++ b/components/jvcllaz/examples/JvMarkup/main.lfm @@ -457,8 +457,7 @@ object MainForm: TMainForm '
small text and large text' '
A different font face: Times' '
HTML entities: & ™ √α β γ δ 90°' - '
Subscripts and superscripts are not supported:' - '10 cm3 H2O ' + '
Subscripts and superscripts: 10 cm3 H2O ' ) VisibleSpecialChars = [vscSpace, vscTabAtLast] SelectedColor.BackPriority = 50 diff --git a/components/jvcllaz/run/JvJans/jvmarkupcommon.pas b/components/jvcllaz/run/JvJans/jvmarkupcommon.pas index 1b844c577..df8aa2718 100644 --- a/components/jvcllaz/run/JvJans/jvmarkupcommon.pas +++ b/components/jvcllaz/run/JvJans/jvmarkupcommon.pas @@ -35,6 +35,8 @@ uses Controls, Graphics, SysUtils, Classes; type + TJvHTMLTextPos = (tpNormal, tpSubScript, tpSuperScript); + TJvHTMLElement = class(TObject) private FFontSize: Integer; @@ -49,6 +51,7 @@ type FSolText: string; FEolText: string; FBreakLine: Boolean; + FPosition: TJvHTMLTextPos; procedure SetFontName(const Value: string); procedure SetFontSize(const Value: Integer); procedure SetFontStyle(const Value: TFontStyles); @@ -75,6 +78,7 @@ type property Width: Integer read FWidth write SetWidth; property Ascent: Integer read FAscent write SetAscent; property BreakLine: Boolean read FBreakLine write SetBreakLine; + property Position: TJvHtmlTextPos read FPosition write FPosition; end; TJvHTMLElementStack = class(TList) diff --git a/components/jvcllaz/run/JvJans/jvmarkuplabel.pas b/components/jvcllaz/run/JvJans/jvmarkuplabel.pas index 008faae23..5a2541d76 100644 --- a/components/jvcllaz/run/JvJans/jvmarkuplabel.pas +++ b/components/jvcllaz/run/JvJans/jvmarkuplabel.pas @@ -215,6 +215,7 @@ var lName: string; lSize: Integer; lBreakLine: Boolean; + lPosition: TJvHtmlTextPos; AColor, lColor: TColor; Element: TJvHTMLElement; @@ -283,6 +284,7 @@ var Element.FontSize := lSize; Element.FontStyle := lStyle; Element.FontColor := lColor; + Element.Position := lPosition; Element.BreakLine := lBreakLine; lBreakLine := False; FElementStack.Push(Element); @@ -350,7 +352,16 @@ var PushTag else if ATag = '/font' then - PopTag; + PopTag + else + if ATag = 'sub' then + lPosition := tpSubscript + else + if ATag = 'sup' then + lPosition := tpSuperscript + else if (ATag = '/sub') or (ATag = '/sup') then + lPosition := tpNormal + else if HaveParams then begin repeat @@ -393,6 +404,7 @@ begin lSize := Font.Size; lColor := Font.Color; lBreakLine := False; + lPosition := tpNormal; repeat P := Pos('<', S); if P = 0 then @@ -447,15 +459,40 @@ var var SS: string; WW: Integer; + yy: Integer; + oldFontHeight: Integer; + fd: TFontData; begin SetFont(EE); if EE.SolText <> '' then begin + oldFontHeight := Canvas.Font.Height; + SS := TrimLeft(EE.SolText); + + case EE.Position of + tpNormal: + yy := Y + BaseLine - EE.Ascent; + tpSubscript: + begin + fd := GetFontData(Canvas.Font.Reference.Handle); + Canvas.Font.Height := fd.Height * 7 div 10; + yy := Y + MaxHeight - abs(fd.Height); + end; + tpSuperscript: + begin + fd := GetFontData(Canvas.Font.Reference.Handle); + Canvas.Font.Height := fd.Height * 7 div 10; + yy := Y; + end; + end; WW := Canvas.TextWidth(SS); if not Test then - Canvas.TextOut(X, Y + BaseLine - EE.Ascent, SS); + Canvas.TextOut(X, yy, SS); X := X + WW; + + if EE.Position <> tpNormal then + Canvas.Font.Height := oldFontHeight; end; end; diff --git a/components/jvcllaz/run/JvJans/jvmarkupviewer.pas b/components/jvcllaz/run/JvJans/jvmarkupviewer.pas index 52cea70c1..470d94e39 100644 --- a/components/jvcllaz/run/JvJans/jvmarkupviewer.pas +++ b/components/jvcllaz/run/JvJans/jvmarkupviewer.pas @@ -222,6 +222,7 @@ var FName: string; FSize: Integer; LBreakLine: Boolean; + LPosition: TJvHtmlTextpos; AColor, FColor: TColor; Element: TJvHTMLElement; @@ -282,6 +283,7 @@ var Element.FontSize := FSize; Element.FontStyle := FStyle; Element.FontColor := FColor; + Element.Position := LPosition; Element.BreakLine := LBreakLine; LBreakLine := False; FElementStack.Push(Element); @@ -349,7 +351,17 @@ var PushTag else if LTag = '/font' then - PopTag; + PopTag + else + if LTag = 'sub' then + LPosition := tpSubscript + else + if LTag = 'sup' then + LPosition := tpSuperscript + else + if (LTag = '/sub') or (LTag = '/sup') then + LPosition := tpNormal + else if HavePar then begin repeat @@ -391,6 +403,7 @@ begin FName := Font.Name; FSize := Font.Size; FColor := Font.Color; + LPosition := tpNormal; LBreakLine := False; repeat p := Pos('<', s); @@ -433,14 +446,20 @@ var PendingBreak: Boolean; procedure SetFont(AElem: TJvHTMLElement); + var + fd: TFontData; begin with FBmp.Canvas do begin if SameText(AElem.FontName, 'default') then - Font.Name := Screen.MenuFont.Name else + Font.Name := Screen.MenuFont.Name + else Font.Name := AElem.FontName; if AElem.FontSize = 0 then - Font.Size := 10 else + begin + fd := GetFontData(Font.Reference.Handle); + Font.Height := fd.Height; + end else Font.Size := AElem.FontSize; Font.Style := AElem.FontStyle; Font.Color := AElem.FontColor; @@ -451,14 +470,35 @@ var var SS: string; w: Integer; + yy: Integer; + oldFontSize: Integer; begin SetFont(ee); if ee.SolText <> '' then begin + oldFontSize := FBmp.Canvas.Font.Size; SS := ee.SolText; + case ee.Position of + tpNormal: + begin + yy := Y + Baseline - ee.Ascent - FrameTop; + end; + tpSubscript: + begin + FBmp.Canvas.Font.Height := FBmp.Canvas.Font.Height * 7 div 10; + yy := Y + MaxHeight - abs(FBmp.Canvas.Font.Height) - FrameTop; + end; + tpSuperscript: + begin + yy := Y - FrameTop; + FBmp.Canvas.Font.Height := FBmp.Canvas.Font.Height * 7 div 10; + end; + end; w := FBmp.Canvas.TextWidth(SS); - FBmp.Canvas.TextOut(X, Y + BaseLine - ee.Ascent - FrameTop, SS); + FBmp.Canvas.TextOut(X, yy, SS); X := X + w; + if ee.Position <> tpNormal then + FBmp.Canvas.Font.Size := oldFontSize; end; end;