com.dua3.utility.swing.StyledDocumentConverter Maven / Gradle / Ivy
// Copyright (c) 2019 Axel Howind
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package com.dua3.utility.swing;
import com.dua3.utility.data.Pair;
import com.dua3.utility.text.AttributeBasedConverter;
import com.dua3.utility.text.Font;
import com.dua3.utility.text.RichText;
import com.dua3.utility.text.TextAttributes;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* A {@link AttributeBasedConverter} implementation for translating
* {@code RichText} to StyledDocument.
*
* @author Axel Howind ([email protected])
*/
public final class StyledDocumentConverter extends AttributeBasedConverter {
private static final Font DEFAULT_FONT = new Font();
private final SimpleAttributeSet defaultStyledAttributes = new SimpleAttributeSet();
// some settings controlling the conversion
private Font defaultFont = DEFAULT_FONT;
private Map defaultAttributes = new HashMap<>();
private double scale = 1.0;
// -- define a dictionary to map StyleConstants attribute keys to calls to Font getters
@SuppressWarnings("NumericCastThatLosesPrecision")
private final Map