org.apache.batik.bridge.SVGKernElementBridge Maven / Gradle / Ivy
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.bridge;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import org.apache.batik.gvt.font.Kern;
import org.apache.batik.gvt.font.UnicodeRange;
import org.w3c.dom.Element;
/**
* A base Bridge class for the kerning elements.
*
* @author Dean Jackson
* @version $Id: SVGKernElementBridge.java 1733416 2016-03-03 07:07:13Z gadams $
*/
public abstract class SVGKernElementBridge extends AbstractSVGBridge {
/**
* Creates a Kern object that repesents the specified kerning element.
*
* @param ctx The bridge context.
* @param kernElement The kerning element. Should be either a <hkern>
* or <vkern> element.
* @param font The font the kerning is related to.
*
* @return kern The new Kern object
*/
public Kern createKern(BridgeContext ctx,
Element kernElement,
SVGGVTFont font) {
// read all of the kern attributes
String u1 = kernElement.getAttributeNS(null, SVG_U1_ATTRIBUTE);
String u2 = kernElement.getAttributeNS(null, SVG_U2_ATTRIBUTE);
String g1 = kernElement.getAttributeNS(null, SVG_G1_ATTRIBUTE);
String g2 = kernElement.getAttributeNS(null, SVG_G2_ATTRIBUTE);
String k = kernElement.getAttributeNS(null, SVG_K_ATTRIBUTE);
if (k.length() == 0) {
k = SVG_KERN_K_DEFAULT_VALUE;
}
// get the kern float value
float kernValue = Float.parseFloat(k);
// set up the first and second glyph sets and unicode ranges
int firstGlyphLen = 0, secondGlyphLen = 0;
int [] firstGlyphSet = null;
int [] secondGlyphSet = null;
List firstUnicodeRanges = new ArrayList();
List secondUnicodeRanges = new ArrayList();
// process the u1 attribute
StringTokenizer st = new StringTokenizer(u1, ",");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.startsWith("U+")) { // its a unicode range
firstUnicodeRanges.add(new UnicodeRange(token));
} else {
int[] glyphCodes = font.getGlyphCodesForUnicode(token);
if (firstGlyphSet == null) {
firstGlyphSet = glyphCodes;
firstGlyphLen = glyphCodes.length;
}else {
if ((firstGlyphLen + glyphCodes.length) >
firstGlyphSet.length) {
int sz = firstGlyphSet.length*2;
if (sz
secondGlyphSet.length) {
int sz = secondGlyphSet.length*2;
if (sz
firstGlyphSet.length) {
int sz = firstGlyphSet.length*2;
if (sz
secondGlyphSet.length) {
int sz = secondGlyphSet.length*2;
if (sz