001package squidpony.squidgrid.gui.gdx;
002
003import com.badlogic.gdx.assets.AssetManager;
004
005/**
006 * A variant of {@link TextCellFactory} that allows switching between regular, bold, italic, and bold italic styles.
007 * It adds only one field to TextCellFactory, and is interchangeable except that (potentially importantly) it does not
008 * correctly render chars above {@code '\\u3fff'} due to using some of the bits that normally represent late-in-Unicode
009 * character codes to instead represent bold and italic modes. Two TextFamily values are present in DefaultResources,
010 * {@link DefaultResources#getLeanFamily()} and {@link DefaultResources#getSlabFamily()}; using them is currently the
011 * recommended way to use this class.
012 * <br>
013 * You may want to use {@link GDXMarkup#colorString(CharSequence)} to produce an {@link squidpony.panel.IColoredString}
014 * that contains the specially-altered chars that store bold and italic mode data.
015 * <br>
016 * Created by Tommy Ettinger on 10/26/2017.
017 */
018public class TextFamily extends TextCellFactory {
019
020    // constants used by some fonts for format information; this stores their order for reference.
021    public static final int REGULAR = 0, BOLD = 1, ITALIC = 2, BOLD_ITALIC = 3;
022    /**
023     * How many styles are supported by this TextCellFactory; always 4 in TextFamily.
024     */
025    public int supportedStyles() {
026        return 4;
027    }
028    /**
029     * Creates a default valued factory. One of the initialization methods must
030     * be called before this factory can be used!
031     */
032    public TextFamily() {
033        super(null);
034    }
035
036    /**
037     * A default valued factory that uses the given {@link AssetManager} to load
038     * the font file. Use this constructor if you are likely to load the same
039     * font over and over (recall that, without an {@link AssetManager}, each
040     * instance of {@link TextCellFactory} will load its font from disk). This
041     * primarily matters if you are using fonts not bundled with SquidLib, since
042     * accessing a BitmapFont with a method (not a String) from DefaultResources
043     * caches the BitmapFont already.
044     *
045     * @param assetManager an ordinary libGDX AssetManager
046     */
047    public TextFamily(/* Nullable */ AssetManager assetManager) {
048        super(assetManager);
049    }
050    @Override
051    public TextFamily copy()
052    {
053        TextFamily next = new TextFamily(assetManager);
054        if(bmpFont == null)
055            bmpFont = DefaultResources.getIncludedFont();
056        next.bmpFont = DefaultResources.copyFont(bmpFont);
057        next.block = block;
058        next.swap = swap.clone(); // explicitly implemented by CharCharMap
059        next.swap.defaultReturnValue('\uffff'); // ... but it forgets to copy this field
060        next.distanceField = distanceField;
061        next.msdf = msdf;
062        next.distanceFieldScaleX = distanceFieldScaleX;
063        next.distanceFieldScaleY = distanceFieldScaleY;
064        next.shader = null;
065        next.fitting = fitting;
066        next.height = height;
067        next.width = width;
068        next.actualCellWidth = actualCellWidth;
069        next.actualCellHeight = actualCellHeight;
070        next.descent = descent;
071        next.lineHeight = lineHeight;
072        next.smoothingMultiplier = smoothingMultiplier;
073        next.scc = scc;
074        next.directionGlyph = directionGlyph;
075        if(initializedBySize)
076            next.initBySize();
077        else if(initializedByFont)
078            next.initByFont();
079        return next;
080    }
081    public TextFamily defaultFamilyLeanDistance()
082    {
083        fontDistanceField("Iosevka-Family-distance.fnt", "Iosevka-Family-distance.png");
084        setSmoothingMultiplier(2.1f);
085        return this;
086    }
087    public TextFamily defaultFamilySlabDistance()
088    {
089        fontDistanceField("Iosevka-Slab-Family-distance.fnt", "Iosevka-Slab-Family-distance.png");
090        setSmoothingMultiplier(2.1f);
091        return this;
092    }
093
094}