com.itextpdf.html2pdf.attach.impl.layout.PageTargetCountRenderer Maven / Gradle / Ivy
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2020 iText Group NV
Authors: iText Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package com.itextpdf.html2pdf.attach.impl.layout;
import com.itextpdf.html2pdf.LogMessageConstant;
import com.itextpdf.html2pdf.css.resolve.func.counter.CounterDigitsGlyphStyle;
import com.itextpdf.html2pdf.html.HtmlUtils;
import com.itextpdf.io.util.MessageFormatUtil;
import com.itextpdf.layout.layout.LayoutContext;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.TargetCounterHandler;
import com.itextpdf.layout.renderer.TextRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* {@link TextRenderer} implementation for the page target-counter.
*/
public class PageTargetCountRenderer extends TextRenderer {
private static final Logger LOGGER = LoggerFactory.getLogger(PageTargetCountRenderer.class);
private static final String UNDEFINED_VALUE = "0";
private final String target;
private final CounterDigitsGlyphStyle digitsGlyphStyle;
/**
* Instantiates a new {@link PageTargetCountRenderer}.
*
* @param textElement the text element
*/
PageTargetCountRenderer(PageTargetCountElement textElement) {
super(textElement);
digitsGlyphStyle = textElement.getDigitsGlyphStyle();
target = textElement.getTarget();
}
/**
* {@inheritDoc}
*/
@Override
public LayoutResult layout(LayoutContext layoutContext) {
final String previousText = getText().toString();
final Integer page = TargetCounterHandler.getPageByID(this, target);
if (page == null) {
setText(UNDEFINED_VALUE);
} else {
setText(HtmlUtils.convertNumberAccordingToGlyphStyle(digitsGlyphStyle, (int) page));
}
final LayoutResult result = super.layout(layoutContext);
setText(previousText);
return result;
}
/**
* {@inheritDoc}
*/
@Override
public void draw(DrawContext drawContext) {
if (!TargetCounterHandler.isValueDefinedForThisId(this, target)) {
LOGGER.warn(MessageFormatUtil.format(LogMessageConstant.CANNOT_RESOLVE_TARGET_COUNTER_VALUE, target));
}
super.draw(drawContext);
}
/**
* {@inheritDoc}
*/
@Override
public IRenderer getNextRenderer() {
return this;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean resolveFonts(List addTo) {
final List dummyList = new ArrayList<>();
super.resolveFonts(dummyList);
setProperty(Property.FONT, dummyList.get(0).