VFontsImpl.mesa
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Doug Wyatt, May 29, 1985 12:06:05 pm PDT
Russ Atkinson (RRA) June 19, 1985 2:03:52 pm PDT
DIRECTORY
Convert USING [AppendInt],
Imager USING [Error],
ImagerFont USING [Extents, Find, FontBoundingBox, RopeWidth, VEC, Width],
Real USING [RoundI],
RefText USING [AppendChar, AppendRope, ObtainScratch, ReleaseScratch, TrustTextAsRope],
Rope USING [ROPE],
UserProfile USING [Number, Token],
VFonts USING [DefaultFont, defaultFont, ErrorCode, Font];
VFontsImpl: CEDAR PROGRAM
IMPORTS Convert, Imager, ImagerFont, Real, RefText, UserProfile, VFonts
EXPORTS VFonts
~ BEGIN OPEN VFonts;
VEC: TYPE ~ ImagerFont.VEC;
ROPE: TYPE = Rope.ROPE;
Error: PUBLIC ERROR [code: VFonts.ErrorCode] ~ CODE;
defaultFont: PUBLIC VFonts.Font ← NIL;
EstablishFont: PUBLIC PROC [family: ROPE, size: CARDINAL,
bold: BOOLFALSE, italic: BOOLFALSE, defaultOnFailure: BOOLTRUE
] RETURNS [font: Font ← NIL] ~ {
scratch: REF TEXT ~ RefText.ObtainScratch[100];
text: REF TEXT ← scratch;
text ← RefText.AppendRope[text, "Xerox/TiogaFonts/"];
text ← RefText.AppendRope[text, family];
text ← Convert.AppendInt[text, size];
IF bold THEN text ← RefText.AppendChar[text, 'B];
IF italic THEN text ← RefText.AppendChar[text, 'I];
font ← ImagerFont.Find[RefText.TrustTextAsRope[text] !
Imager.Error => IF defaultOnFailure THEN CONTINUE];
IF font=NIL THEN font ← defaultFont;
RefText.ReleaseScratch[scratch];
};
CharWidth: PUBLIC PROC [char: CHAR, font: Font ← NIL] RETURNS [NAT] ~ {
width: VEC ~ ImagerFont.Width[DefaultFont[font], [set: 0, code: ORD[char]]];
RETURN[Real.RoundI[width.x]];
};
StringWidth: PUBLIC PROC [string: ROPE, font: Font ← NIL] RETURNS [NAT] ~ {
width: VEC ~ ImagerFont.RopeWidth[DefaultFont[font], string];
RETURN[Real.RoundI[width.x]];
};
FontHeight: PUBLIC PROC [font: Font ← NIL] RETURNS [NAT] ~ {
extents: ImagerFont.Extents ~ ImagerFont.FontBoundingBox[DefaultFont[font]];
RETURN[Real.RoundI[extents.descent+extents.ascent]];
};
FontAscent: PUBLIC PROC [font: Font ← NIL] RETURNS [NAT] ~ {
extents: ImagerFont.Extents ~ ImagerFont.FontBoundingBox[DefaultFont[font]];
RETURN[Real.RoundI[extents.ascent]];
};
CreateDefaultFont: PROC ~ {
family: ROPE ~ UserProfile.Token["Viewers.DefaultFontFamily", "Tioga"];
size: INT ~ UserProfile.Number["Viewers.DefaultFontSize", 10];
err: BOOLFALSE;
defaultFont ← EstablishFont[family: family, size: size, defaultOnFailure: FALSE !
Imager.Error => { err ← TRUE; CONTINUE }];
IF err THEN defaultFont ← EstablishFont[family: "Tioga", size: 10, defaultOnFailure: FALSE];
substitute Tioga10 for user's failed choice
};
CreateDefaultFont[];
END.