/* * Utils.java * * ... * ... * @author Oystein Reigem */ package aksis.alignment; import java.text.*; // AttributedString, AttributedCharacterIterator import java.awt.font.*; // LineBreakMeasurer import java.awt.*; import java.lang.Math.*; class Utils { // ### hadde jeg problemer med å ha denne metoden her? // ### fordi jeg ikke skjønte at den måtte deklareres static? // ### se do to andre metodene /* public boolean hasHoles(Set[] integerSet) { // used to check if the n latest finished alignments // (the n bottom alignments) // have any holes. // for each text see if there are holes for (int t=0; t. // #######men finnes det virkelig ikke en slik metode i java? finner den ikke public static String stringToHTMLString(String string) { StringBuffer sb = new StringBuffer(string.length()); // true if last char was blank // #######jeg trenger sikkert ikke whitespcehåndteringen, men boolean lastWasBlankChar = false; int len = string.length(); char c; for (int i = 0; i < len; i++) { c = string.charAt(i); if (c == ' ') { // blank gets extra work, // this solves the problem you get if you replace all // blanks with  , if you do that you loss // word breaking if (lastWasBlankChar) { lastWasBlankChar = false; sb.append(" "); } else { lastWasBlankChar = true; sb.append(' '); } } else { lastWasBlankChar = false; // // HTML Special Chars if (c == '"') sb.append("""); else if (c == '&') sb.append("&"); else if (c == '<') sb.append("<"); else if (c == '>') sb.append(">"); else if (c == '\n') // Handle Newline sb.append("<br/>"); else { int ci = 0xffff & c; if (ci < 160 ) // nothing special only 7 Bit sb.append(c); else { // Not 7 Bit use the unicode system sb.append("&#"); sb.append(new Integer(ci).toString()); sb.append(';'); } } } } return sb.toString(); } // ###modifisert fra kode funnet på //public static int wrappedTextHeight(String text, Font font, int width) { public static int wrappedTextHeight(String text, Graphics2D g, int width) { // 2006-09-15 //System.out.println("\ntext=" + text); // 2006-09-15 Font font = g.getFont(); // construct an attributed string instance with the given text java.text.AttributedString attrString = new java.text.AttributedString(text); // add font attribute to the entire text attrString.addAttribute(java.awt.font.TextAttribute.FONT, font); // get iterator for the attributed string java.text.AttributedCharacterIterator it = attrString.getIterator(); int itStart = it.getBeginIndex(); int itEnd = it.getEndIndex(); // construct a LineBreakMeasurer for the text. // #############grrrrrrrrrrrr bug in LineBreakMeasurer? breaks at space // but also after other characters like "'.!!!!!!!! java.awt.font.LineBreakMeasurer lineMeasurer = new java.awt.font.LineBreakMeasurer( //Utils.LineBreakMeasurer lineMeasurer = new Utils.LineBreakMeasurer( it, //new java.awt.font.FontRenderContext(null, false, false) // ######dette er suspekt. må heller skaffe en FontRenderContext // ved å bruke getFontRenderContext() på et Graphics2D-objekt g.getFontRenderContext() ); lineMeasurer.setPosition(itStart); //FontMetrics fontMetrics = new FontMetrics(font); // oystein. ###abstract //int lineHeight = fontMetrics.getHeight(); // oystein // calculate number of lines and full layout height int lines = 0; int layHeight = 0; //float layWidth = 0; // ###trenger ikke denne java.awt.font.TextLayout layout; while(lineMeasurer.getPosition() < itEnd) { layout = lineMeasurer.nextLayout(width); lines++; //System.out.println("\nlayout=" + layout); // 2006-09-15 //System.out.println("Descent=" + layout.getDescent()); //System.out.println("Leading=" + layout.getLeading()); //System.out.println("Ascent=" + layout.getAscent()); // did a test and got // Descent=2.2719727 // Leading=0.0 // Ascent=11.005371 // which yields a total of 13.277... // 14 pixels is correct. // ): needs to round upwards (i.e, ceiling). #####but what? total? or each contribution? //layHeight += layout.getDescent() + layout.getLeading() + layout.getAscent(); // ###får liksom 1 for lite layHeight += Math.ceil(layout.getDescent() + layout.getLeading() + layout.getAscent()); //System.out.println("layHeight=" + layHeight); //layHeight += lineHeight; //float advance; //advance = layout.getVisibleAdvance(); //if(layWidth < advance) { // layWidth = advance; //} } if (lines > 0) { layHeight += 3; // oystein ############################## } //System.out.println("final layHeight=" + layHeight); return layHeight; } }