comparison dwtx/draw2d/text/SimpleTextLayout.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.text.SimpleTextLayout;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17
18 import dwt.graphics.Font;
19 import dwtx.draw2d.text.TextLayout;
20 import dwtx.draw2d.text.TextFlow;
21 import dwtx.draw2d.text.TextFragmentBox;
22 import dwtx.draw2d.text.FlowUtilities;
23
24 /**
25 * @author hudsonr
26 * @since 2.1
27 */
28 public class SimpleTextLayout : TextLayout {
29
30 private static const String[] DELIMITERS = [
31 "\r\n", //$NON-NLS-1$
32 "\n", //$NON-NLS-1$
33 "\r"];//$NON-NLS-1$
34
35 private static int result;
36 private static int delimeterLength;
37
38 /**
39 * Creates a new SimpleTextLayout with the given TextFlow
40 * @param flow the TextFlow
41 */
42 public this(TextFlow flow) {
43 super (flow);
44 }
45
46 /**
47 * @see dwtx.draw2d.text.FlowFigureLayout#layout()
48 */
49 protected void layout() {
50 TextFlow textFlow = cast(TextFlow)getFlowFigure();
51 String text = textFlow.getText();
52 List fragments = textFlow.getFragments();
53 Font font = textFlow.getFont();
54 TextFragmentBox fragment;
55 int i = 0;
56 int offset = 0;
57 FlowUtilities flowUtilities = textFlow.getFlowUtilities_package();
58
59 do {
60 nextLineBreak(text, offset);
61 fragment = getFragment(i++, fragments);
62 fragment.length = result - offset;
63 fragment.offset = offset;
64 fragment.setWidth(-1);
65 flowUtilities.setupFragment_package(fragment, font, text.substring(offset, result));
66 getContext().addToCurrentLine(fragment);
67 getContext().endLine();
68 offset = result + delimeterLength;
69 } while (offset < text.length);
70 //Remove the remaining unused fragments.
71 while (i < fragments.size())
72 fragments.remove(i++);
73 }
74
75 private int nextLineBreak(String text, int offset) {
76 result = text.length;
77 delimeterLength = 0;
78 int current;
79 for (int i = 0; i < DELIMITERS.length; i++) {
80 current = text.indexOf(DELIMITERS[i], offset);
81 if (current !is -1 && current < result) {
82 result = current;
83 delimeterLength = DELIMITERS[i].length;
84 }
85 }
86 return result;
87 }
88
89 }