001/* 002 * Copyright 2024 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.transformation.xslt; 017 018import java.util.regex.Matcher; 019import java.util.regex.Pattern; 020 021import org.slf4j.Logger; 022 023/** 024 * Helper for converting XML to FO 025 */ 026public final class FOXSLTHelper 027{ 028 private static final Pattern __SIZE_PATTERN = Pattern.compile("^\\s*([0-9.]+)\\s*(%|[a-zA-Z]+)\\s*$"); 029 private static final Logger __LOGGER = org.slf4j.LoggerFactory.getLogger(FOXSLTHelper.class); 030 031 private static final double __THE_CONSTANT = 72 / 2.54; // 72 dpi 032 private static final double __THE_PT_CONSTANT = 1.33; 033 034 private FOXSLTHelper() 035 { 036 // empty 037 } 038 039 /** 040 * Divide the given size in equal parts. 041 * @param size the input size, with its own unit 042 * @param parts number of parts to divide the total size into 043 * @return the converted size in pixels 044 */ 045 public static String divide(String size, int parts) 046 { 047 Matcher matcher = __SIZE_PATTERN.matcher(size); 048 if (matcher.matches()) 049 { 050 String number = matcher.group(1); 051 double doubleNumber = Double.valueOf(number); 052 053 String unit = matcher.group(2); 054 055 return String.valueOf(doubleNumber / parts) + unit; 056 } 057 else 058 { 059 __LOGGER.debug("Unknown size format {}", size); 060 return size; 061 } 062 } 063 064 /** 065 * Convert the given size to pixels and divide it in equal parts. 066 * @param size the input size, with its own unit 067 * @param parts number of parts to divide the total size into 068 * @param blockSize the surrounding block size, in cm 069 * @return the converted size in pixels 070 */ 071 public static String divideAndConvertToPx(String size, int parts, double blockSize) 072 { 073 Matcher matcher = __SIZE_PATTERN.matcher(size); 074 if (matcher.matches()) 075 { 076 String number = matcher.group(1); 077 double doubleNumber = Double.valueOf(number); 078 079 String unit = matcher.group(2); 080 081 switch (unit) 082 { 083 case "%": 084 return String.valueOf(doubleNumber * __THE_CONSTANT * blockSize / parts / 100) + "px"; 085 case "pt": 086 return String.valueOf(doubleNumber * __THE_PT_CONSTANT / parts) + "px"; 087 case "px": 088 return String.valueOf(doubleNumber / parts) + "px"; 089 default: 090 __LOGGER.debug("Unknown unit for size {}", size); 091 return String.valueOf(doubleNumber / parts) + unit; 092 } 093 } 094 else 095 { 096 __LOGGER.debug("Unknown size format {}", size); 097 return size; 098 } 099 } 100}