Skip to content

Commit 45c7ebd

Browse files
committed
added FontGroup
1 parent c1deab2 commit 45c7ebd

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

h2d/Font.hx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ enum FontType {
141141
@param smoothing The smoothing of edge. Lower value lead to sharper edges. Value of -1 sets it to automatic.
142142
**/
143143
SignedDistanceField(channel : SDFChannel, alphaCutoff : Float, smoothing : Float);
144+
/**
145+
A font group is a virtual font that contains one or several sub fonts that can be selected with h2d.Text.resolveSubFont.
146+
**/
147+
FontGroup;
144148
}
145149

146150
/**
@@ -185,6 +189,12 @@ class Font {
185189
Defaults to `hxd.Charset.getDefault()`.
186190
**/
187191
public var charset : hxd.Charset;
192+
193+
/**
194+
Subfonts will be selected when assigned with the `h2d.Text.resolveSubFont` method.
195+
**/
196+
public var subFonts : Array<Font>;
197+
188198
var glyphs : Map<Int,FontChar>;
189199
var nullChar : FontChar;
190200
var defaultChar : FontChar;

h2d/HtmlText.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class HtmlText extends Text {
177177
public dynamic function loadFont( name : String ) : Font {
178178
var f = defaultLoadFont(name);
179179
if (f == null) return this.font;
180-
else return f;
180+
return resolveFont(f);
181181
}
182182

183183
/**

h2d/Text.hx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,24 @@ class Text extends Drawable {
137137
**/
138138
public function new( font : Font, ?parent : h2d.Object ) {
139139
super(parent);
140-
this.font = font;
140+
this.font = resolveFont(font);
141141
textAlign = Left;
142142
text = "";
143143
currentText = "";
144144
textColor = 0xFFFFFF;
145145
}
146146

147-
function set_font(font) {
147+
function resolveFont(font:Font) {
148+
while( true ) {
149+
if( font == null || font.type != FontGroup ) return font;
150+
var sub = resolveSubFont(font, this);
151+
if( font == sub ) throw "Recursive font";
152+
font = sub;
153+
}
154+
}
155+
156+
function set_font(font:Font) {
157+
font = resolveFont(font);
148158
if( this.font == font ) return font;
149159
this.font = font;
150160
if ( font != null ) {
@@ -165,6 +175,8 @@ class Text extends Drawable {
165175
sdfShader.smoothing = smoothing;
166176
sdfShader.channel = channel;
167177
sdfShader.autoSmoothing = smoothing == -1;
178+
case FontGroup:
179+
throw "assert";
168180
}
169181
}
170182
if( glyphs != null ) glyphs.remove();
@@ -541,4 +553,8 @@ class Text extends Drawable {
541553
addBounds(relativeTo, out, x, y, w, h);
542554
}
543555

556+
public static dynamic function resolveSubFont( fnt : Font, text : Text ) : Font {
557+
return fnt.subFonts[0];
558+
}
559+
544560
}

h2d/domkit/BaseComponents.hx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ class CustomParser extends domkit.CssValue.ValueParser {
198198
#else
199199
return hxd.res.DefaultFont.get();
200200
#end
201+
case VCall("group",fonts):
202+
#if macro
203+
for( f in fonts )
204+
parseFont(f);
205+
#else
206+
var fnt = @:privateAccess new h2d.Font("group",0,FontGroup);
207+
fnt.subFonts = [for( f in fonts ) parseFont(f)];
208+
return fnt;
209+
#end
201210
case VGroup(args):
202211
var args = args.copy();
203212
path = parsePath(args[0]);

0 commit comments

Comments
 (0)