?? bidiline.java
字號:
/* * * Copyright 2002 Paulo Soares * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.util.ArrayList;import java.awt.font.TextLayout;import java.text.AttributedString;import java.awt.font.TextAttribute;import java.awt.font.FontRenderContext;import java.awt.geom.AffineTransform;import java.util.HashMap;import com.lowagie.text.*;import java.io.*;/** Does all the line bidirectional processing with PdfChunk assembly. * * @author Paulo Soares (psoares@consiste.pt) */public class BidiLine { protected int runDirection; protected int pieceSize = 2048; protected char text[] = new char[pieceSize]; protected PdfChunk detailChunks[] = new PdfChunk[pieceSize]; protected int totalTextLength = 0; protected byte orderLevels[] = new byte[pieceSize]; protected int indexChars[] = new int[pieceSize]; protected ArrayList chunks = new ArrayList(); protected int indexChunk = 0; protected int indexChunkChar = 0; protected int currentChar = 0; protected int storedRunDirection; protected char storedText[] = new char[0]; protected PdfChunk storedDetailChunks[] = new PdfChunk[0]; protected int storedTotalTextLength = 0; protected byte storedOrderLevels[] = new byte[0]; protected int storedIndexChars[] = new int[0]; protected int storedIndexChunk = 0; protected int storedIndexChunkChar = 0; protected int storedCurrentChar = 0; protected boolean shortStore; protected ArabicShaping arabic = new ArabicShaping(ArabicShaping.LETTERS_SHAPE | ArabicShaping.LENGTH_GROW_SHRINK | ArabicShaping.TEXT_DIRECTION_LOGICAL); protected static final IntHashtable mirrorChars = new IntHashtable(); /** Creates new BidiLine */ public BidiLine() { } public boolean isEmpty() { return (currentChar >= totalTextLength && indexChunk >= chunks.size()); } public void clearChunks() { chunks.clear(); totalTextLength = 0; currentChar = 0; } public boolean getParagraph(int runDirection) { this.runDirection = runDirection; currentChar = 0; totalTextLength = 0; boolean hasText = false; char c; for (; indexChunk < chunks.size(); ++indexChunk) { PdfChunk ck = (PdfChunk)chunks.get(indexChunk); String s = ck.toString(); int len = s.length(); for (; indexChunkChar < len; ++indexChunkChar) { c = s.charAt(indexChunkChar); if (c == '\r' || c == '\n') { if (c == '\r' && indexChunkChar + 1 < len && s.charAt(indexChunkChar + 1) == '\n') ++indexChunkChar; ++indexChunkChar; if (indexChunkChar >= len) { indexChunkChar = 0; ++indexChunk; } hasText = true; if (totalTextLength == 0) detailChunks[0] = ck; break; } addPiece(c, ck); } if (hasText) break; indexChunkChar = 0; } if (totalTextLength == 0) return hasText; // remove trailing WS totalTextLength = trimRight(0, totalTextLength - 1) + 1; if (totalTextLength == 0) return true; if (runDirection == PdfWriter.RUN_DIRECTION_LTR || runDirection == PdfWriter.RUN_DIRECTION_RTL) { if (orderLevels.length < totalTextLength) { orderLevels = new byte[pieceSize]; indexChars = new int[pieceSize]; } HashMap attr = new HashMap(); if (runDirection == PdfWriter.RUN_DIRECTION_LTR) attr.put(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_LTR); else attr.put(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); TextLayout t = new TextLayout(new String(text, 0, totalTextLength), attr, new FontRenderContext(new AffineTransform(), false, true)); for (int k = 0; k < totalTextLength; ++k) { orderLevels[k] = t.getCharacterLevel(k); indexChars[k] = k; } doArabicShapping(); mirrorGlyphs(); } totalTextLength = trimRightEx(0, totalTextLength - 1) + 1; return true; } public void addChunk(PdfChunk chunk) { chunks.add(chunk); } public void addChunks(ArrayList chunks) { this.chunks.addAll(chunks); } public void addPiece(char c, PdfChunk chunk) { if (totalTextLength >= pieceSize) { char tempText[] = text; PdfChunk tempDetailChunks[] = detailChunks; pieceSize *= 2; text = new char[pieceSize]; detailChunks = new PdfChunk[pieceSize]; System.arraycopy(tempText, 0, text, 0, totalTextLength); System.arraycopy(tempDetailChunks, 0, detailChunks, 0, totalTextLength); } text[totalTextLength] = c; detailChunks[totalTextLength++] = chunk; } public void save() { if (indexChunk > 0) { if (indexChunk >= chunks.size()) chunks.clear(); else { for (--indexChunk; indexChunk >= 0; --indexChunk) chunks.remove(indexChunk); } indexChunk = 0; } storedRunDirection = runDirection; storedTotalTextLength = totalTextLength; storedIndexChunk = indexChunk; storedIndexChunkChar = indexChunkChar; storedCurrentChar = currentChar; shortStore = (currentChar < totalTextLength); if (!shortStore) { // long save if (storedText.length < totalTextLength) { storedText = new char[totalTextLength]; storedDetailChunks = new PdfChunk[totalTextLength]; } System.arraycopy(text, 0, storedText, 0, totalTextLength); System.arraycopy(detailChunks, 0, storedDetailChunks, 0, totalTextLength); } if (runDirection == PdfWriter.RUN_DIRECTION_LTR || runDirection == PdfWriter.RUN_DIRECTION_RTL) { if (storedOrderLevels.length < totalTextLength) { storedOrderLevels = new byte[totalTextLength]; storedIndexChars = new int[totalTextLength]; } System.arraycopy(orderLevels, currentChar, storedOrderLevels, currentChar, totalTextLength - currentChar); System.arraycopy(indexChars, currentChar, storedIndexChars, currentChar, totalTextLength - currentChar); } } public void restore() { runDirection = storedRunDirection; totalTextLength = storedTotalTextLength; indexChunk = storedIndexChunk; indexChunkChar = storedIndexChunkChar; currentChar = storedCurrentChar; if (!shortStore) { // long restore System.arraycopy(storedText, 0, text, 0, totalTextLength); System.arraycopy(storedDetailChunks, 0, detailChunks, 0, totalTextLength); } if (runDirection == PdfWriter.RUN_DIRECTION_LTR || runDirection == PdfWriter.RUN_DIRECTION_RTL) { System.arraycopy(storedOrderLevels, currentChar, orderLevels, currentChar, totalTextLength - currentChar); System.arraycopy(storedIndexChars, currentChar, indexChars, currentChar, totalTextLength - currentChar); } } public void mirrorGlyphs() { for (int k = 0; k < totalTextLength; ++k) { if ((orderLevels[k] & 1) == 1) { int mirror = mirrorChars.get(text[k]); if (mirror != 0) text[k] = (char)mirror; } } } public void doArabicShapping() { int src = 0; int dest = 0; for (;;) { while (src < totalTextLength) { char c = text[src]; if (c >= 0x0600 && c <= 0x06ff) break; if (src != dest) { text[dest] = text[src]; detailChunks[dest] = detailChunks[src]; orderLevels[dest] = orderLevels[src]; } ++src; ++dest; } if (src >= totalTextLength) { totalTextLength = dest; return; } int startArabicIdx = src; ++src; while (src < totalTextLength) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -