/* * [FontResize.java] * * Summary: Magnify or shrink all the fonts is a group of CSS files. * * Copyright: (c) 2016-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2016-07-03 initial version */ package com.mindprod.repair; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Magnify or shrink all the fonts is a group of CSS files. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2016-07-03 initial version * @since 2016-07-03 */ public class FontResize { private static final boolean DEBUGGING = false; private static final Pattern extract = Pattern.compile( "font-size\\s*:\\s*([\\d\\.]+)\\s*(px|pt)([; ])" ); private static final DecimalFormat FORMAT0 = new DecimalFormat( "###0" ); private static final DecimalFormat FORMAT1 = new DecimalFormat( "#0.0" ); private static final DecimalFormat FORMAT2 = new DecimalFormat( "#0.00" ); private static final DecimalFormat FORMAT3 = new DecimalFormat( "#0.000" ); /** * process one file, magnifying its prices * * @param fileToProcess file to modify * @param magnifier multiplier. Less than 1 shrinks. * * @throws IOException */ private static void process( File fileToProcess, double magnifier ) throws IOException { // We modify lines like: // font-size:16px; // font-size: 30pt; // font-size: 1.15em; leave // font-size: .9em !important; leave // font-size: 120%; leave // font-size: small; leave // font-size: larger; leave // font-size: smaller; leave final String big = HunkIO.readEntireFile( fileToProcess, HunkIO.UTF8 ); // where we collect the replacement. Must be a StringBuffer, not a StringBuilder. Regex has a legacy problem. final StringBuffer sb = new StringBuffer( big.length() ); final Matcher m = extract.matcher( big ); // Matchers are used both for matching and finding. while ( m.find() ) { final int gc = m.groupCount(); if ( gc != 3 ) { continue; } final double number = Double.parseDouble( m.group( 1 ) ); final String units = m.group( 2 ); final String tail = m.group( 3 ); final double magnified = magnifier * number; String magnifiedString; if ( magnified >= 100 ) { magnifiedString = FORMAT0.format( magnified ); } else if ( magnified >= 10 ) { magnifiedString = FORMAT1.format( magnified ); } else if ( magnified >= 1 ) { magnifiedString = FORMAT2.format( magnified ); } else { magnifiedString = FORMAT3.format( magnified ); } // get rid of trailing zeroes int oldLength; int newLength; do { oldLength = magnifiedString.length(); magnifiedString = ST.chopTrailingString( magnifiedString, "0" ); newLength = magnifiedString.length(); } while ( oldLength != newLength ); magnifiedString = ST.chopTrailingString( magnifiedString, "." ); final String replacement = "font-size: " + magnifiedString + units + tail; if ( DEBUGGING ) { out.println( "old: " + m.group( 0 ) ); out.println( "new: " + replacement ); } // You don't HAVE to call appendReplacement for every match. If you don't, // that matched text will remain as is. // If replacement contains strings of the form $1 they are commands to include groups. // To turn this off, $ must be encoded as \$ // \ needs to be coded as \\ // The material between matches will also be automatically included, unchanged. // If you want to modify that text, appendReplacement is not the tool you want. m.appendReplacement( sb, Matcher.quoteReplacement( replacement ) ); // also appends junk between hits. } // we have now processed two replacements. m.appendTail( sb ); /* save temp file then rename */ final File out = HunkIO.createTempFile( "fontresize", ".temp", fileToProcess ); HunkIO.writeEntireFile( out, sb.toString(), HunkIO.UTF8 ); HunkIO.deleteAndRename( out, fileToProcess ); } /** * Magnify or shrink all the fonts is a group of CSS files. * * @param args magnify-ratio xxx.css yyy.css ... */ public static void main( String[] args ) { double magnifier = Double.parseDouble( args[ 0 ] ); args[ 0 ] = null; final CommandLine files = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "css" ) ); for ( File fileToProcess : files ) { try { process( fileToProcess, magnifier ); } catch ( IOException e ) { err.println( "problem with file " + EIO.getCanOrAbsPath( fileToProcess ) + " " + e.getMessage() ); } } } }