001/* ===============
002 * SkijaGraphics2D
003 * ===============
004 *
005 * (C)opyright 2021, by David Gilbert.
006 *
007 * The SkijaGraphics2D class has been developed by David Gilbert for
008 * use with Orson Charts (http://www.object-refinery.com/orsoncharts) and
009 * JFreeChart (http://www.jfree.org/jfreechart).  It may be useful for other
010 * code that uses the Graphics2D API provided by Java2D.
011 *
012 * Redistribution and use in source and binary forms, with or without
013 * modification, are permitted provided that the following conditions are met:
014 *   - Redistributions of source code must retain the above copyright
015 *     notice, this list of conditions and the following disclaimer.
016 *   - Redistributions in binary form must reproduce the above copyright
017 *     notice, this list of conditions and the following disclaimer in the
018 *     documentation and/or other materials provided with the distribution.
019 *   - Neither the name of the Object Refinery Limited nor the
020 *     names of its contributors may be used to endorse or promote products
021 *     derived from this software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
024 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
026 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
027 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
030 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 *
034 */
035
036package org.jfree.skija;
037
038import io.github.humbleui.skija.FontMetrics;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042import java.awt.*;
043
044/**
045 * Returns font metrics.
046 */
047public class SkijaFontMetrics extends java.awt.FontMetrics {
048
049    private static final Logger LOGGER = LoggerFactory.getLogger(SkijaFontMetrics.class);
050
051    /** Skija font. */
052    private io.github.humbleui.skija.Font skijaFont;
053
054    /** Skija font metrics. */
055    private FontMetrics metrics;
056
057    /**
058     * Creates a new instance.
059     *
060     * @param skijaFont  the Skija font ({@code null} not permitted).
061     * @param awtFont  the AWT font ({@code null} not permitted).
062     */
063    public SkijaFontMetrics(io.github.humbleui.skija.Font skijaFont, Font awtFont) {
064        super(awtFont);
065        this.metrics = skijaFont.getMetrics();
066        this.skijaFont = skijaFont;
067    }
068
069    /**
070     * Returns the leading.
071     *
072     * @return The leading.
073     */
074    @Override
075    public int getLeading() {
076        int result = (int) this.metrics.getLeading();
077        LOGGER.debug("getLeading() -> {}", result);
078        return result;
079    }
080
081    /**
082     * Returns the ascent for the font.
083     *
084     * @return The ascent.
085     */
086    @Override
087    public int getAscent() {
088        int result = (int) -this.metrics.getAscent();
089        LOGGER.debug("getAscent() -> {}", result);
090        return result;
091    }
092
093    /**
094     * Returns the descent for the font.
095     *
096     * @return The descent.
097     */
098    @Override
099    public int getDescent() {
100        int result = (int) this.metrics.getDescent();
101        LOGGER.debug("getDescent() -> {}", result);
102        return result;
103    }
104
105    /**
106     * Returns the width of the specified character.
107     *
108     * @param ch  the character.
109     *
110     * @return The width.
111     */
112    @Override
113    public int charWidth(char ch) {
114        int result = (int) this.skijaFont.measureTextWidth(Character.toString(ch));
115        LOGGER.debug("charWidth({}) -> {}", ch, result);
116        return result;
117    }
118
119    /**
120     * Returns the width of a character sequence.
121     *
122     * @param data  the characters.
123     * @param off  the offset.
124     * @param len  the length.
125     *
126     * @return The width of the character sequence.
127     */
128    @Override
129    public int charsWidth(char[] data, int off, int len) {
130        int result = (int) this.skijaFont.measureTextWidth(new String(data, off, len));
131        LOGGER.debug("charsWidth({}, {}, {}) -> {}", data, off, len, result);
132        return result;
133    }
134}