굴림 9pt(12px)의 글자 크기에 맞춰서 Width로 글자 자르기
굴림체는 맨 아래 byte 글자 자르기로 하면 되고 가변 글자체일 경우에는 해당 width를 수정하여 사용하면 된다.
출처 (PHP) : http://www.xpressengine.com/zb4_tip/854296
출처 첨부파일 :cutstring.php
Java 소스로 변환 (junit test) :TestSubWidth.java
package test;
import java.text.StringCharacterIterator; import java.util.HashMap; import java.util.Map;
import org.junit.Test;
public class TestSubByte { private final static String[] strTemp = new String[10]; private final static int[] strWidth = new int[10]; private static Map<Integer, Integer> asciiMap = new HashMap<Integer, Integer>(); static { strTemp[0] = "@#&*@※☆★○●◎◇◆△▽→←↔〓◁◀▷▶♤♡♧⊙◈▣◐◑▒▤▥▨▧▦▩♨☏☎☜☞↗↙↖↘♭♩♬㉿㈜№㏇™㏂㏘℡"; strWidth[0] = 12; strTemp[1] = "\\mM"; strWidth[1] = 11; strTemp[2] = "%wW□■▲▼♣"; strWidth[2] = 10; strTemp[3] = "~QONGC↑↓♠♥↕®"; strWidth[3] = 9; strTemp[4] = "<>&ZXYRSTUVKHEDBAP♪"; strWidth[4] = 8; strTemp[5] = "zyxusqponhgedcbaLF¶"; strWidth[5] = 7; strTemp[6] = "#$*+-/0123456789=?[]^_{|}vkJ†‡§"; strWidth[6] = 6; strTemp[7] = "()"; strWidth[7] = 5; strTemp[8] = " :;.,!\"'`rª"; strWidth[8] = 4; strTemp[9] = "tljifIº·"; strWidth[9] = 3; try { StringCharacterIterator sci = null; int ascii = 0; for(int i=0, s=strTemp.length; i<s; i++) { sci = new StringCharacterIterator(strTemp[i]); ascii = sci.first(); while(ascii < 65535) { asciiMap.put(ascii, strWidth[i]); ascii = sci.next(); } } } catch (Exception e) { } } private String subWidth(String str, int maxWidth, String tail) throws Exception { if (str == null || str.length() < 1) { return ""; } if(maxWidth < 1) { return ""; } StringCharacterIterator sci = new StringCharacterIterator(str); StringBuffer buffer = new StringBuffer(); int ascii = sci.first(); Object widthObj = null; int width = 0; while(ascii < 65535) { widthObj = asciiMap.get(ascii); if(widthObj != null) { width += (Integer)widthObj; } else { if(ascii > 127) { width += 12; } else { width += 6; } } if(width > maxWidth) { if(tail != null && tail.length() > 0) { buffer.append(tail); } break; } else { buffer.append((char)ascii); } ascii = sci.next(); } return buffer.toString(); } @Test public void test1() throws Exception { System.out.println( subWidth("대 korea", 100, "..") ); System.out.println( subWidth("대 korea 한민국1 넘버 입니다", 100, "..") ); System.out.println( subWidth("abcdEFASDFASDFASDFASDFADSASD", 100, "..") ); } }
|
Java 소스로 변환 (TagLibrary) :TagUtility.java
public static String subByte(String str, int len, String encoding) { try { if (StringUtil.isEmpty(str)) { return str; } byte[] strBytes = str.getBytes(encoding); int strLength = strBytes.length; int minusByteNum = 0; int offset = 0; int hangulByteNum = encoding.equals("UTF-8") ? 3 : 2; if (strLength > len) { minusByteNum = 0; offset = len; for (int j = 0; j < offset; j++) { if (((int) strBytes[j] & 0x80) != 0) { minusByteNum++; } } if (minusByteNum % hangulByteNum != 0) { offset -= minusByteNum % hangulByteNum; } return new String(strBytes, 0, offset, encoding); } else { return str; } } catch (Exception e) { throw new BaseException(e); } } |
Byte(바이트)로 글자 자르기
출처 : http://blog.naver.com/PostView.nhn?blogId=nackhwa7&logNo=140103352320
public static String parseStringByBytes(String str, int len, String encoding) { try { if (StringUtil.isEmpty(str)) { return str; } byte[] strBytes = str.getBytes(encoding); int strLength = strBytes.length; int index = 0; int minusByteNum = 0; int offset = 0; int hangulByteNum = encoding.equals("UTF-8") ? 3 : 2; if (strLength > len) { minusByteNum = 0; offset = len; if (index + offset > strLength) { offset = strLength - index; } for (int j = 0; j < offset; j++) { if (((int) strBytes[index + j] & 0x80) != 0) { minusByteNum++; } } if (minusByteNum % hangulByteNum != 0) { offset -= minusByteNum % hangulByteNum; } return new String(strBytes, index, offset, encoding); } else { return str; } } catch (Exception e) { e.printStackTrace(); } } |
- 원본 소스
public String[] parseStringByBytes(String raw, int len, String encoding) { if (raw == null) return null; String[] ary = null; try { // raw 의 byte byte[] rawBytes = raw.getBytes(encoding); int rawLength = rawBytes.length;
int index = 0; int minus_byte_num = 0; int offset = 0;
int hangul_byte_num = encoding.equals("UTF-8") ? 3 : 2;
if (rawLength > len) { int aryLength = (rawLength / len) + (rawLength % len != 0 ? 1 : 0); ary = new String[aryLength];
for (int i = 0; i < aryLength; i++) { minus_byte_num = 0; offset = len; if (index + offset > rawBytes.length) { offset = rawBytes.length - index; } for (int j = 0; j < offset; j++) { if (((int) rawBytes[index + j] & 0x80) != 0) { minus_byte_num++; } } if (minus_byte_num % hangul_byte_num != 0) { offset -= minus_byte_num % hangul_byte_num; } ary[i] = new String(rawBytes, index, offset, encoding); index += offset;
} } else { ary = new String[] { raw }; } } catch (Exception e) {
} return ary; } |