Mercurial > projects > dwt-mac
comparison dwt/graphics/TextLayout.d @ 7:e831403a80a9
Add 'cast' to casts
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 14:30:35 +0200 |
parents | 1a8b3cb347e0 |
children | db5a898b2119 |
comparison
equal
deleted
inserted
replaced
6:b903c16b6f48 | 7:e831403a80a9 |
---|---|
109 } | 109 } |
110 | 110 |
111 void computeRuns() { | 111 void computeRuns() { |
112 if (textStorage !is null) return; | 112 if (textStorage !is null) return; |
113 NSString str = NSString.stringWith(text); | 113 NSString str = NSString.stringWith(text); |
114 textStorage = ((NSTextStorage)new NSTextStorage().alloc()); | 114 textStorage = (cast(NSTextStorage)new NSTextStorage().alloc()); |
115 textStorage.initWithString_(str); | 115 textStorage.initWithString_(str); |
116 layoutManager = (NSLayoutManager)new NSLayoutManager().alloc().init(); | 116 layoutManager = cast(NSLayoutManager)new NSLayoutManager().alloc().init(); |
117 textContainer = (NSTextContainer)new NSTextContainer().alloc(); | 117 textContainer = cast(NSTextContainer)new NSTextContainer().alloc(); |
118 NSSize size = new NSSize(); | 118 NSSize size = new NSSize(); |
119 size.width = wrapWidth !is -1 ? wrapWidth : Float.MAX_VALUE; | 119 size.width = wrapWidth !is -1 ? wrapWidth : Float.MAX_VALUE; |
120 size.height = Float.MAX_VALUE; | 120 size.height = Float.MAX_VALUE; |
121 textContainer.initWithContainerSize(size); | 121 textContainer.initWithContainerSize(size); |
122 textStorage.addLayoutManager(layoutManager); | 122 textStorage.addLayoutManager(layoutManager); |
126 Font defaultFont = font !is null ? font : device.systemFont; | 126 Font defaultFont = font !is null ? font : device.systemFont; |
127 NSRange range = new NSRange(); | 127 NSRange range = new NSRange(); |
128 range.length = str.length(); | 128 range.length = str.length(); |
129 textStorage.addAttribute(OS.NSFontAttributeName(), defaultFont.handle, range); | 129 textStorage.addAttribute(OS.NSFontAttributeName(), defaultFont.handle, range); |
130 | 130 |
131 NSMutableParagraphStyle paragraph = (NSMutableParagraphStyle)new NSMutableParagraphStyle().alloc().init(); | 131 NSMutableParagraphStyle paragraph = cast(NSMutableParagraphStyle)new NSMutableParagraphStyle().alloc().init(); |
132 int align = OS.NSLeftTextAlignment; | 132 int align = OS.NSLeftTextAlignment; |
133 if (justify) { | 133 if (justify) { |
134 align = OS.NSJustifiedTextAlignment; | 134 align = OS.NSJustifiedTextAlignment; |
135 } else { | 135 } else { |
136 switch (alignment) { | 136 switch (alignment) { |
421 Font font = this.font !is null ? this.font : device.systemFont; | 421 Font font = this.font !is null ? this.font : device.systemFont; |
422 NSFont nsFont = font.handle; | 422 NSFont nsFont = font.handle; |
423 rect.height = Math.max(rect.height, layoutManager.defaultLineHeightForFont(nsFont)); | 423 rect.height = Math.max(rect.height, layoutManager.defaultLineHeightForFont(nsFont)); |
424 } | 424 } |
425 rect.height = Math.max(rect.height, ascent + descent); | 425 rect.height = Math.max(rect.height, ascent + descent); |
426 return new Rectangle(0, 0, (int)rect.width, (int)rect.height); | 426 return new Rectangle(0, 0, cast(int)rect.width, cast(int)rect.height); |
427 } | 427 } |
428 | 428 |
429 /** | 429 /** |
430 * Returns the bounds for the specified range of characters. The | 430 * Returns the bounds for the specified range of characters. The |
431 * bounds is the smallest rectangle that encompasses all characters | 431 * bounds is the smallest rectangle that encompasses all characters |
452 end = translateOffset(end); | 452 end = translateOffset(end); |
453 NSRange range = new NSRange(); | 453 NSRange range = new NSRange(); |
454 range.location = layoutManager.glyphIndexForCharacterAtIndex(start); | 454 range.location = layoutManager.glyphIndexForCharacterAtIndex(start); |
455 range.length = layoutManager.glyphIndexForCharacterAtIndex(end + 1) - range.location; | 455 range.length = layoutManager.glyphIndexForCharacterAtIndex(end + 1) - range.location; |
456 NSRect rect = layoutManager.boundingRectForGlyphRange(range, textContainer); | 456 NSRect rect = layoutManager.boundingRectForGlyphRange(range, textContainer); |
457 return new Rectangle((int)rect.x, (int)rect.y, (int)Math.ceil(rect.width), (int)Math.ceil(rect.height)); | 457 return new Rectangle(cast(int)rect.x, cast(int)rect.y, cast(int)Math.ceil(rect.width), cast(int)Math.ceil(rect.height)); |
458 } | 458 } |
459 | 459 |
460 /** | 460 /** |
461 * Returns the descent of the receiver. | 461 * Returns the descent of the receiver. |
462 * | 462 * |
613 public Rectangle getLineBounds(int lineIndex) { | 613 public Rectangle getLineBounds(int lineIndex) { |
614 checkLayout(); | 614 checkLayout(); |
615 computeRuns(); | 615 computeRuns(); |
616 if (!(0 <= lineIndex && lineIndex < lineBounds.length)) DWT.error(DWT.ERROR_INVALID_RANGE); | 616 if (!(0 <= lineIndex && lineIndex < lineBounds.length)) DWT.error(DWT.ERROR_INVALID_RANGE); |
617 NSRect rect = lineBounds[lineIndex]; | 617 NSRect rect = lineBounds[lineIndex]; |
618 return new Rectangle((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height); | 618 return new Rectangle(cast(int)rect.x, cast(int)rect.y, cast(int)rect.width, cast(int)rect.height); |
619 } | 619 } |
620 | 620 |
621 /** | 621 /** |
622 * Returns the receiver's line count. This includes lines caused | 622 * Returns the receiver's line count. This includes lines caused |
623 * by wrapping. | 623 * by wrapping. |
654 if (!(0 <= lineIndex && lineIndex < lineCount)) DWT.error(DWT.ERROR_INVALID_RANGE); | 654 if (!(0 <= lineIndex && lineIndex < lineCount)) DWT.error(DWT.ERROR_INVALID_RANGE); |
655 int length = text.length(); | 655 int length = text.length(); |
656 if (length is 0) { | 656 if (length is 0) { |
657 Font font = this.font !is null ? this.font : device.systemFont; | 657 Font font = this.font !is null ? this.font : device.systemFont; |
658 NSFont nsFont = font.handle; | 658 NSFont nsFont = font.handle; |
659 int ascent = (int)(0.5f + nsFont.ascender()); | 659 int ascent = cast(int)(0.5f + nsFont.ascender()); |
660 int descent = (int)(0.5f + (-nsFont.descender() + nsFont.leading())); | 660 int descent = cast(int)(0.5f + (-nsFont.descender() + nsFont.leading())); |
661 ascent = Math.max(ascent, this.ascent); | 661 ascent = Math.max(ascent, this.ascent); |
662 descent = Math.max(descent, this.descent); | 662 descent = Math.max(descent, this.descent); |
663 return FontMetrics.cocoa_new(ascent, descent, 0, 0, ascent + descent); | 663 return FontMetrics.cocoa_new(ascent, descent, 0, 0, ascent + descent); |
664 } | 664 } |
665 Rectangle rect = getLineBounds(lineIndex); | 665 Rectangle rect = getLineBounds(lineIndex); |
666 int baseline = (int)layoutManager.typesetter().baselineOffsetInLayoutManager(layoutManager, getLineOffsets()[lineIndex]); | 666 int baseline = cast(int)layoutManager.typesetter().baselineOffsetInLayoutManager(layoutManager, getLineOffsets()[lineIndex]); |
667 return FontMetrics.cocoa_new(rect.height - baseline, baseline, 0, 0, rect.height); | 667 return FontMetrics.cocoa_new(rect.height - baseline, baseline, 0, 0, rect.height); |
668 } | 668 } |
669 | 669 |
670 /** | 670 /** |
671 * Returns the location for the specified character offset. The | 671 * Returns the location for the specified character offset. The |
698 range.location = glyphIndex; | 698 range.location = glyphIndex; |
699 range.length = 1; | 699 range.length = 1; |
700 NSRect bounds = layoutManager.boundingRectForGlyphRange(range, textContainer); | 700 NSRect bounds = layoutManager.boundingRectForGlyphRange(range, textContainer); |
701 point.x += bounds.width; | 701 point.x += bounds.width; |
702 } | 702 } |
703 return new Point((int)point.x, (int)rect.y); | 703 return new Point(cast(int)point.x, cast(int)rect.y); |
704 } | 704 } |
705 | 705 |
706 /** | 706 /** |
707 * Returns the next offset for the specified offset and movement | 707 * Returns the next offset for the specified offset and movement |
708 * type. The movement is one of <code>DWT.MOVEMENT_CHAR</code>, | 708 * type. The movement is one of <code>DWT.MOVEMENT_CHAR</code>, |