Search in sources :

Example 6 with Segment

use of javax.swing.text.Segment in project platform_frameworks_base by android.

the class StaticLayout_Delegate method nComputeLineBreaks.

@LayoutlibDelegate
static /*package*/
int nComputeLineBreaks(long nativeBuilder, LineBreaks recycle, int[] recycleBreaks, float[] recycleWidths, int[] recycleFlags, int recycleLength) {
    Builder builder = sBuilderManager.getDelegate(nativeBuilder);
    if (builder == null) {
        return 0;
    }
    // compute all possible breakpoints.
    int length = builder.mWidths.length;
    BreakIterator it = BreakIterator.getLineInstance(new ULocale(builder.mLocale));
    it.setText(new Segment(builder.mText, 0, length));
    // average word length in english is 5. So, initialize the possible breaks with a guess.
    List<Integer> breaks = new ArrayList<Integer>((int) Math.ceil(length / 5d));
    int loc;
    it.first();
    while ((loc = it.next()) != BreakIterator.DONE) {
        breaks.add(loc);
    }
    List<Primitive> primitives = computePrimitives(builder.mText, builder.mWidths, length, breaks);
    switch(builder.mBreakStrategy) {
        case Layout.BREAK_STRATEGY_SIMPLE:
            builder.mLineBreaker = new GreedyLineBreaker(primitives, builder.mLineWidth, builder.mTabStopCalculator);
            break;
        case Layout.BREAK_STRATEGY_HIGH_QUALITY:
        //                break;
        case Layout.BREAK_STRATEGY_BALANCED:
            builder.mLineBreaker = new OptimizingLineBreaker(primitives, builder.mLineWidth, builder.mTabStopCalculator);
            break;
        default:
            throw new AssertionError("Unknown break strategy: " + builder.mBreakStrategy);
    }
    builder.mLineBreaker.computeBreaks(recycle);
    return recycle.breaks.length;
}
Also used : ULocale(android.icu.util.ULocale) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint) Segment(javax.swing.text.Segment) BreakIterator(android.icu.text.BreakIterator) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 7 with Segment

use of javax.swing.text.Segment in project android_frameworks_base by AOSPA.

the class StaticLayout_Delegate method nComputeLineBreaks.

@LayoutlibDelegate
static /*package*/
int nComputeLineBreaks(long nativeBuilder, LineBreaks recycle, int[] recycleBreaks, float[] recycleWidths, int[] recycleFlags, int recycleLength) {
    Builder builder = sBuilderManager.getDelegate(nativeBuilder);
    if (builder == null) {
        return 0;
    }
    // compute all possible breakpoints.
    int length = builder.mWidths.length;
    BreakIterator it = BreakIterator.getLineInstance(new ULocale(builder.mLocale));
    it.setText(new Segment(builder.mText, 0, length));
    // average word length in english is 5. So, initialize the possible breaks with a guess.
    List<Integer> breaks = new ArrayList<Integer>((int) Math.ceil(length / 5d));
    int loc;
    it.first();
    while ((loc = it.next()) != BreakIterator.DONE) {
        breaks.add(loc);
    }
    List<Primitive> primitives = computePrimitives(builder.mText, builder.mWidths, length, breaks);
    switch(builder.mBreakStrategy) {
        case Layout.BREAK_STRATEGY_SIMPLE:
            builder.mLineBreaker = new GreedyLineBreaker(primitives, builder.mLineWidth, builder.mTabStopCalculator);
            break;
        case Layout.BREAK_STRATEGY_HIGH_QUALITY:
        //                break;
        case Layout.BREAK_STRATEGY_BALANCED:
            builder.mLineBreaker = new OptimizingLineBreaker(primitives, builder.mLineWidth, builder.mTabStopCalculator);
            break;
        default:
            throw new AssertionError("Unknown break strategy: " + builder.mBreakStrategy);
    }
    builder.mLineBreaker.computeBreaks(recycle);
    return recycle.breaks.length;
}
Also used : ULocale(android.icu.util.ULocale) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint) Segment(javax.swing.text.Segment) BreakIterator(android.icu.text.BreakIterator) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 8 with Segment

use of javax.swing.text.Segment in project chipKIT32-MAX by chipKIT32.

the class DiscourseFormat method appendFormattedLine.

// A terrible headache...
public void appendFormattedLine(StringBuffer cf, int line) {
    Segment segment = new Segment();
    TextAreaPainter painter = textarea.getPainter();
    TokenMarker tokenMarker = textarea.getTokenMarker();
    // Use painter's cached info for speed
    //    FontMetrics fm = painter.getFontMetrics();
    // get line text from parent text area
    textarea.getLineText(line, segment);
    char[] segmentArray = segment.array;
    int limit = segment.getEndIndex();
    int segmentOffset = segment.offset;
    int segmentCount = segment.count;
    // If syntax coloring is disabled, do simple translation
    if (tokenMarker == null) {
        for (int j = 0; j < segmentCount; j++) {
            char c = segmentArray[j + segmentOffset];
            appendToHTML(c, cf);
        //        int charWidth;
        //        if (c == '\t') {
        //          charWidth = (int) painter.nextTabStop(width, j) - width;
        //        } else {
        //          charWidth = fm.charWidth(c);
        //        }
        //        width += charWidth;
        }
    } else {
        // If syntax coloring is enabled, we have to do this
        // because tokens can vary in width
        Token tokens;
        if ((painter.getCurrentLineIndex() == line) && (painter.getCurrentLineTokens() != null)) {
            tokens = painter.getCurrentLineTokens();
        } else {
            painter.setCurrentLineIndex(line);
            painter.setCurrentLineTokens(tokenMarker.markTokens(segment, line));
            tokens = painter.getCurrentLineTokens();
        }
        int offset = 0;
        //      Font defaultFont = painter.getFont();
        SyntaxStyle[] styles = painter.getStyles();
        for (; ; ) {
            byte id = tokens.id;
            if (id == Token.END) {
                char c = segmentArray[segmentOffset + offset];
                if (segmentOffset + offset < limit) {
                    appendToHTML(c, cf);
                } else {
                    cf.append('\n');
                }
                // cf.toString();
                return;
            }
            if (id == Token.NULL) {
            //          fm = painter.getFontMetrics();
            } else {
                // Place open tags []
                cf.append(html ? "<span style=\"color: #" : "[color=#");
                cf.append(PApplet.hex(styles[id].getColor().getRGB() & 0xFFFFFF, 6));
                cf.append(html ? ";\">" : "]");
                if (styles[id].isBold())
                    cf.append(html ? "<b>" : "[b]");
            //          fm = styles[id].getFontMetrics(defaultFont);
            }
            int length = tokens.length;
            for (int j = 0; j < length; j++) {
                char c = segmentArray[segmentOffset + offset + j];
                if (offset == 0 && c == ' ') {
                    // Works on Safari but not Camino 1.6.3 or Firefox 2.x on OS X.
                    // &nbsp;
                    cf.append(html ? "&nbsp;" : ' ');
                //            if ((j % 2) == 1) {
                //              cf.append("[b] [/b]");
                //            } else {
                //              cf.append(' ');
                //            }
                } else {
                    appendToHTML(c, cf);
                }
                // Place close tags [/]
                if (j == (length - 1) && id != Token.NULL && styles[id].isBold())
                    cf.append(html ? "</b>" : "[/b]");
                if (j == (length - 1) && id != Token.NULL)
                    cf.append(html ? "</span>" : "[/color]");
            //          int charWidth;
            //          if (c == '\t') {
            //            charWidth = (int) painter
            //              .nextTabStop(width, offset + j)
            //              - width;
            //          } else {
            //            charWidth = fm.charWidth(c);
            //          }
            //          width += charWidth;
            }
            offset += length;
            tokens = tokens.next;
        }
    }
}
Also used : Segment(javax.swing.text.Segment)

Aggregations

Segment (javax.swing.text.Segment)8 Paint (android.graphics.Paint)5 BreakIterator (android.icu.text.BreakIterator)5 ULocale (android.icu.util.ULocale)5 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)5 ArrayList (java.util.ArrayList)5 Document (javax.swing.text.Document)1