Skip to content

Commit

Permalink
Merge pull request #295 from pharo-graphics/text_pass_2023_08
Browse files Browse the repository at this point in the history
Refactoring in text package
  • Loading branch information
tinchodias authored Aug 6, 2023
2 parents 945a945 + 3652b67 commit 78fb25d
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 79 deletions.
6 changes: 3 additions & 3 deletions src/Bloc-Alexandrie/BAMockedTextMeasurer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ BAMockedTextMeasurer >> measureTextParagraph: aBlTextParagraph [

{ #category : #measurement }
BAMockedTextMeasurer >> measureTextSpan: aBlSpan [

"Measure a given text span and return paragraph span with my fixed metrics"

measuredWidth := aBlSpan size * self glyphWidth.
measuredHeight := self height * aBlSpan size isZero not asBit.

^ self createParagraphSpan setupFromHostTextMeasurer: self forSpan: aBlSpan
^ self newParagraphSpanFor: aBlSpan
]

{ #category : #metrics }
Expand All @@ -108,7 +107,8 @@ BAMockedTextMeasurer >> measuredWidth [

{ #category : #'paragraph span instance creation' }
BAMockedTextMeasurer >> newParagraphSpan [
^ BAMockedTextParagraphSpan new

^ BAMockedTextParagraphSpan new
]

{ #category : #accessing }
Expand Down
10 changes: 5 additions & 5 deletions src/Bloc-Alexandrie/BAMockedTextParagraphSpan.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ BAMockedTextParagraphSpan >> aeDrawOn: aeCanvas [
]

{ #category : #building }
BAMockedTextParagraphSpan >> measureWith: aBATextMeasurer [
BAMockedTextParagraphSpan >> measure [

width := aBATextMeasurer measuredWidth.
height := aBATextMeasurer measuredHeight.
width := hostMeasurer measuredWidth.
height := hostMeasurer measuredHeight.

ascent := aBATextMeasurer ascent.
descent := aBATextMeasurer descent.
ascent := hostMeasurer ascent.
descent := hostMeasurer descent.

left := 0.
top := 0.
Expand Down
4 changes: 3 additions & 1 deletion src/Bloc-Alexandrie/BATextElementMockedMeasurer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ BATextElementMockedMeasurer >> glyphWidth: aNumber [

{ #category : #initialization }
BATextElementMockedMeasurer >> initialize [

super initialize.

measurer := BAMockedTextMeasurer new.
measurer ascent: -10.
measurer descent: 5.
Expand All @@ -60,6 +61,7 @@ BATextElementMockedMeasurer >> initialize [

{ #category : #initialization }
BATextElementMockedMeasurer >> measureTextParagraph: aBlTextParagraph of: aTextElement [

measurer measureTextParagraph: aBlTextParagraph
]

Expand Down
4 changes: 2 additions & 2 deletions src/Bloc-Alexandrie/BATextMeasurer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ BATextMeasurer >> measureTextParagraph: aBlTextParagraph [

{ #category : #measurement }
BATextMeasurer >> measureTextSpan: aBlSpan [

"Measure a given text span and return paragraph span with computed metrics"
^ self createParagraphSpan setupFromHostTextMeasurer: self forSpan: aBlSpan

^ self newParagraphSpanFor: aBlSpan
]

{ #category : #'paragraph span instance creation' }
Expand Down
32 changes: 2 additions & 30 deletions src/Bloc-Alexandrie/BATextParagraphLeaf.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ Class {
#superclass : #BlTextParagraphSegmentLeaf,
#instVars : [
'cairoScaledFont',
'cairoGlyphsArray',
'fontAndStyleBuilder'
'cairoGlyphsArray'
],
#classVars : [
'TabStopWidth'
Expand All @@ -24,35 +23,8 @@ BATextParagraphLeaf class >> tabStopWidth: anInteger [

{ #category : #drawing }
BATextParagraphLeaf >> aeDrawOn: aeCanvas [
self subclassResponsibility
]

{ #category : #accessing }
BATextParagraphLeaf >> fontAndStyleBuilder: aBlTextFontAndStyleBuilder [
fontAndStyleBuilder := aBlTextFontAndStyleBuilder
]

{ #category : #building }
BATextParagraphLeaf >> measureWith: aBATextMeasurer [
^ self subclassResponsibility
]

{ #category : #measurement }
BATextParagraphLeaf >> normalize: aScale [
"Normalize this segment to have rounded measurements"
self flag: 'Do we need this ?'.

]

{ #category : #building }
BATextParagraphLeaf >> setupFromHostTextMeasurer: aBATextMeasurer forSpan: aBlSpan [

self fontAndStyleBuilder: BlTextFontAndStyleBuilder new.
self span: aBlSpan.

"Collect font properties and text style from the attributes"
self span attributes do: [ :eachAttribute | eachAttribute applyOnFontAndStyleBuilder: fontAndStyleBuilder ].
self measureWith: aBATextMeasurer
self subclassResponsibility
]

{ #category : #accessing }
Expand Down
2 changes: 1 addition & 1 deletion src/Bloc-Alexandrie/BATextParagraphSpan.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BATextParagraphSpan >> aeDrawOn: aeCanvas [
]

{ #category : #building }
BATextParagraphSpan >> measureWith: aBATextMeasurer [
BATextParagraphSpan >> measure [

"build an abstract font and resolve not yet resolved properties"

Expand Down
1 change: 1 addition & 0 deletions src/Bloc-Text-Elements/BlTextElement.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ BlTextElement >> onParagraphMeasure: anExtentMeasurementSpec [
We have to recreate a text paragraph with new parameters"

| aMaxWidth aMaxHeight |

aMaxWidth := self computeMaxWidth: anExtentMeasurementSpec.
aMaxHeight := self computeMaxHeight: anExtentMeasurementSpec.

Expand Down
2 changes: 1 addition & 1 deletion src/Bloc-Text-Examples/BlTextParagraphExamples.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ BlTextParagraphExamples >> measuredHelloWorld [
{ #category : #'instance creation' }
BlTextParagraphExamples >> measurer [
<gtExample>

| aMeasurer |

aMeasurer := BAMockedTextMeasurer new
ascent: self ascent;
descent: self descent;
Expand Down
20 changes: 9 additions & 11 deletions src/Bloc-Text/BlTextParagraph.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Internal Representation and Key Implementation Points.
Class {
#name : #BlTextParagraph,
#superclass : #Object,
#traits : 'TBlDebug',
#classTraits : 'TBlDebug classTrait',
#instVars : [
'text',
'line',
Expand Down Expand Up @@ -97,7 +95,6 @@ BlTextParagraph >> baselineMeasurer: aBlTextParagraphBaselineMeasurer [

{ #category : #'paragraph - measuring' }
BlTextParagraph >> basicMeasureOn: aHostSpanMeasurer [

"Measure and create lines with text spans from provided text based on attributes and layout properties (maximum width / height)"

| anIterator paragraphMeasurer |
Expand All @@ -107,13 +104,14 @@ BlTextParagraph >> basicMeasureOn: aHostSpanMeasurer [
text isEmpty ifTrue: [ paragraphMeasurer measureSpan: self emptySpan ].

anIterator := text iterator.
[
anIterator hasNext and: [
line isEmpty or: [ paragraphMeasurer hasAvailableSpace ] ] ]
whileTrue: [ "If we pass #hasNext check we will definitely move to the next span,
so can do the same with position"
paragraphMeasurer movePosition: anIterator position + 1.
paragraphMeasurer measureSpan: anIterator nextSpan ].
[ anIterator hasNext and: [
line isEmpty or: [
paragraphMeasurer hasAvailableSpace ] ] ] whileTrue: [
"If we pass #hasNext check we will definitely move to the next span,
so can do the same with position"
paragraphMeasurer
movePosition: anIterator position + 1;
measureSpan: anIterator nextSpan ].

"measurer creates a line with baseline set to 0@0"
line := paragraphMeasurer line.
Expand Down Expand Up @@ -195,6 +193,7 @@ BlTextParagraph >> initialize [

{ #category : #testing }
BlTextParagraph >> isEmpty [

^ self line isEmpty
]

Expand Down Expand Up @@ -344,7 +343,6 @@ BlTextParagraph >> spanAtPoint: aPoint ifFound: aFoundBlock ifNone: anExceptionB
with the closest span to a point"
<return: #BlTextParagraphSegmentLeaf>


^ line spans
findBinary: [ :aParagraphSpan |
| originX cornerX |
Expand Down
9 changes: 5 additions & 4 deletions src/Bloc-Text/BlTextParagraphLine.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Class {

{ #category : #adding }
BlTextParagraphLine >> addSpan: aBlTextParagraphLeaf [

spans add: aBlTextParagraphLeaf.
spans size = 1
ifTrue: [ self spanAdded: spans first ]
Expand All @@ -23,6 +24,7 @@ BlTextParagraphLine >> addSpan: aBlTextParagraphLeaf [

{ #category : #accessing }
BlTextParagraphLine >> baseline: aPoint [

self moveSpansBy: self baseline negated.
super baseline: aPoint.
self moveSpansBy: self baseline
Expand All @@ -37,26 +39,26 @@ BlTextParagraphLine >> finalize [

{ #category : #initialization }
BlTextParagraphLine >> initialize [

super initialize.
spans := OrderedCollection new
]

{ #category : #testing }
BlTextParagraphLine >> isEmpty [

^ self spans isEmpty
^ spans isEmpty
]

{ #category : #private }
BlTextParagraphLine >> moveSpansBy: aPoint [
"Move baseline of all spans by a given delta as a point"

self spans do: [ :aSpan | aSpan baseline: aSpan baseline + aPoint ]
spans do: [ :aSpan | aSpan baseline: aSpan baseline + aPoint ]
]

{ #category : #measurement }
BlTextParagraphLine >> normalize: aScale [

"Normalize this segment to have rounded measurements.
I don't change the baseline"

Expand Down Expand Up @@ -126,7 +128,6 @@ BlTextParagraphLine >> spanAdded: aLastSpan after: aPreviousSpan [

{ #category : #accessing }
BlTextParagraphLine >> spans [
<return: #SequenceableCollection of: #BlTextParagraphSegmentLeaf>

^ spans
]
7 changes: 6 additions & 1 deletion src/Bloc-Text/BlTextParagraphMeasurer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ Class {

{ #category : #measurement }
BlTextParagraphMeasurer class >> hostTextMeasurer: aHostSpanMeasurer maxWidth: aWidth maxHeight: aHeight [
^ self new hostTextMeasurer: aHostSpanMeasurer maxWidth: aWidth maxHeight: aHeight

^ self new
hostTextMeasurer: aHostSpanMeasurer
maxWidth: aWidth
maxHeight: aHeight;
yourself
]

{ #category : #accessing }
Expand Down
6 changes: 1 addition & 5 deletions src/Bloc-Text/BlTextParagraphSegment.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ I an already measured abstract paragraph segment. I'm a composite to design line
Class {
#name : #BlTextParagraphSegment,
#superclass : #Object,
#traits : 'TBlDebug',
#classTraits : 'TBlDebug classTrait',
#instVars : [
'from',
'to',
Expand Down Expand Up @@ -63,12 +61,11 @@ BlTextParagraphSegment >> descent [
{ #category : #accessing }
BlTextParagraphSegment >> extent [

^ self width @ self height
^ width @ height
]

{ #category : #accessing }
BlTextParagraphSegment >> from [
<return: #Number>

^ from
]
Expand Down Expand Up @@ -124,7 +121,6 @@ BlTextParagraphSegment >> right [

{ #category : #accessing }
BlTextParagraphSegment >> to [
<return: #Number>

^ to
]
Expand Down
29 changes: 22 additions & 7 deletions src/Bloc-Text/BlTextParagraphSegmentLeaf.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@ Class {
#superclass : #BlTextParagraphSegment,
#instVars : [
'span',
'hostMeasurer'
'hostMeasurer',
'fontAndStyleBuilder'
],
#category : #'Bloc-Text-Text-Paragraph'
}

{ #category : #'accessing - span' }
BlTextParagraphSegmentLeaf >> attributes [
"Return a set of attributes applied on this span"
<return: #Set of: #BlTextAttribute>

^ span attributes
]

{ #category : #accessing }
BlTextParagraphSegmentLeaf >> from: aGlobalTextStart to: aGlobalTextEnd [
"Assign provided indices so that we could always determine which part of text I measure"

from := aGlobalTextStart.
to := aGlobalTextEnd
]

{ #category : #initialization }
BlTextParagraphSegmentLeaf >> hostMeasurer: ahostSpanMeasurer [
hostMeasurer := ahostSpanMeasurer
BlTextParagraphSegmentLeaf >> hostMeasurer: aHostMeasurer [

hostMeasurer := aHostMeasurer
]

{ #category : #accessing }
BlTextParagraphSegmentLeaf >> indexAtPosition: aPoint [

"I return a cursor index at a given position as a Point provided in local coordinates of this span"
"Return a cursor index at a given position as a Point provided in local coordinates of this span"

| aWidth |
aWidth := aPoint x max: 0.
Expand Down Expand Up @@ -100,6 +101,13 @@ BlTextParagraphSegmentLeaf >> indexAtPosition: aPoint [
self from + spanIndex - 1 ]
]

{ #category : #building }
BlTextParagraphSegmentLeaf >> measure [
"Calculate span measurement using hostTextMeasurer."

^ self subclassResponsibility
]

{ #category : #measurement }
BlTextParagraphSegmentLeaf >> normalize: aScale [
"Normalize this segment to have rounded measurements"
Expand Down Expand Up @@ -147,7 +155,14 @@ BlTextParagraphSegmentLeaf >> span [

{ #category : #accessing }
BlTextParagraphSegmentLeaf >> span: aBlSpan [
span := aBlSpan

span := aBlSpan.

"Collect font properties and text style from span attributes"
fontAndStyleBuilder := BlTextFontAndStyleBuilder new.
self span attributes do: [ :eachAttribute |
eachAttribute applyOnFontAndStyleBuilder: fontAndStyleBuilder ].

]

{ #category : #'accessing - span' }
Expand Down
Loading

0 comments on commit 78fb25d

Please sign in to comment.