001/* ===============
002 * SkijaGraphics2D
003 * ===============
004 *
005 * (C)opyright 2021, by Object Refinery Limited.
006 *
007 * The SkijaGraphics2D class has been developed by Object Refinery Limited 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 org.jetbrains.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    private org.jetbrains.skija.Font skijaFont;
052
053    /** Skija font metrics. */
054    private FontMetrics metrics;
055
056    /**
057     * Creates a new instance.
058     *
059     * @param skijaFont  the Skija font ({@code null} not permitted).
060     * @param awtFont  the AWT font ({@code null} not permitted).
061     */
062    public SkijaFontMetrics(org.jetbrains.skija.Font skijaFont, Font awtFont) {
063        super(awtFont);
064        this.metrics = skijaFont.getMetrics();
065        this.skijaFont = skijaFont;
066    }
067
068    /**
069     * Returns the leading.
070     *
071     * @return The leading.
072     */
073    @Override
074    public int getLeading() {
075        int result = (int) this.metrics.getLeading();
076        LOGGER.debug("getLeading() -> {}", result);
077        return result;
078    }
079
080    /**
081     * Returns the ascent for the font.
082     *
083     * @return The ascent.
084     */
085    @Override
086    public int getAscent() {
087        int result = (int) -this.metrics.getAscent();
088        LOGGER.debug("getAscent() -> {}", result);
089        return result;
090    }
091
092    /**
093     * Returns the descent for the font.
094     *
095     * @return The descent.
096     */
097    @Override
098    public int getDescent() {
099        int result = (int) this.metrics.getDescent();
100        LOGGER.debug("getDescent() -> {}", result);
101        return result;
102    }
103
104    /**
105     * Returns the width of the specified character.
106     *
107     * @param ch  the character.
108     *
109     * @return The width.
110     */
111    @Override
112    public int charWidth(char ch) {
113        int result = (int) this.skijaFont.measureTextWidth(Character.toString(ch));
114        LOGGER.debug("charWidth({}) -> {}", ch, result);
115        return result;
116    }
117
118    /**
119     * Returns the width of a character sequence.
120     *
121     * @param data  the characters.
122     * @param off  the offset.
123     * @param len  the length.
124     *
125     * @return The width of the character sequence.
126     */
127    @Override
128    public int charsWidth(char[] data, int off, int len) {
129        int result = (int) this.skijaFont.measureTextWidth(new String(data, off, len));
130        LOGGER.debug("charsWidth({}, {}, {}) -> {}", data, off, len, result);
131        return result;
132    }
133}