File: LineSpacingDemo.h

package info (click to toggle)
juce 8.0.10%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,768 kB
  • sloc: cpp: 526,464; ansic: 159,952; java: 3,038; javascript: 847; xml: 269; python: 224; sh: 167; makefile: 84
file content (216 lines) | stat: -rw-r--r-- 8,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
  ==============================================================================

   This file is part of the JUCE framework examples.
   Copyright (c) Raw Material Software Limited

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   to use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
   REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
   AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
   INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
   PERFORMANCE OF THIS SOFTWARE.

  ==============================================================================
*/

/*******************************************************************************
 The block below describes the properties of this PIP. A PIP is a short snippet
 of code that can be read by the Projucer and used to generate a JUCE project.

 BEGIN_JUCE_PIP_METADATA

 name:             LineSpacingDemo
 version:          1.0.0
 vendor:           JUCE
 website:          http://juce.com
 description:      Demonstrates the line spacing options of GlyphArrangement.

 dependencies:     juce_core, juce_events, juce_data_structures, juce_graphics,
                   juce_gui_basics
 exporters:        xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone

 moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1

 type:             Component
 mainClass:        LineSpacingDemo

 useLocalCopy:     1

 END_JUCE_PIP_METADATA

*******************************************************************************/

#pragma once

//==============================================================================
struct LineSpacingDemo final : public Component
{
    LineSpacingDemo()
    {
        lineSpacingSlider.setRange (0.0, 40.0, 0.1);
        lineHeightMultipleSlider.setRange (1.0, 3.0, 0.1);

        Slider* sliders[] { &lineSpacingSlider, &lineHeightMultipleSlider };

        for (auto* s : sliders)
            s->onValueChange = [this] { update(); };

        lineSpacingLabel.attachToComponent (&lineSpacingSlider, false);
        lineHeightMultipleLabel.attachToComponent (&lineHeightMultipleSlider, false);

        demoDescription.setJustificationType (Justification::centredLeft);

        Component* components[] { &lineSpacingLabel,
                                  &lineHeightMultipleLabel,
                                  &lineSpacingSlider,
                                  &lineHeightMultipleSlider,
                                  &demoDescription };

        for (auto* c : components)
            addAndMakeVisible (c);

        setSize (700, 500);
    }

    String justified = "addJustifiedText() places the baseline at the y argument. It will wrap lines "
                       "to enforce the maximum width, but it cannot be "
                       "vertically constrained. The specified Font options will always be respected. "
                       "Alignment and line spacing can be adjusted.";

    String fitted = "addFittedText() places the top of the first line at the y argument. It can be "
                    "vertically constrained. It uses the specified Font as a default, but it will "
                    "reduce the font size and squash the text if necessary to fit it in the available "
                    "space.";

    static constexpr int demoAreaPadding = 10;

    Rectangle<int> getDemoBounds() const
    {
        return getLocalBounds().withTrimmedTop (220).reduced (demoAreaPadding).withTrimmedBottom (40);
    }

    Rectangle<int> getJustifiedBounds() const
    {
        auto bounds = getDemoBounds();
        auto half = bounds.removeFromLeft (bounds.getWidth() / 2);
        half.removeFromRight (25);
        return half;
    }

    Rectangle<int> getFittedBounds() const
    {
        auto bounds = getDemoBounds();
        auto half = bounds.removeFromRight (bounds.getWidth() / 2);
        half.removeFromLeft (25);
        return half;
    }

    void paintGuideLines (Graphics& g)
    {
        const auto textColour = getLookAndFeel().findColour (Label::textColourId);
        const auto lineColour = textColour.withSaturation (0.4f).withRotatedHue (0.1f);

        g.setColour (lineColour);
        const auto demoBounds = getDemoBounds().toFloat();
        g.drawLine (demoBounds.getCentreX() - 90.0f,
                    demoBounds.getY(),
                    demoBounds.getCentreX() + 90.0f,
                    demoBounds.getY(),
                    1.5f);

        const auto jb = getJustifiedBounds().toFloat();
        const auto jbMin = jb.getY() - font.getAscent();
        const auto jbMax = (float) jb.getBottom();
        g.drawLine (jb.getX(), jbMin, jb.getX(), jbMax, 1.5f);
        g.drawLine (jb.getRight(), jbMin, jb.getRight(), jbMax, 1.5f);

        const auto fb = getFittedBounds().toFloat();
        g.drawLine (fb.getX(), fb.getY(), fb.getX(), fb.getBottom(), 1.5f);
        g.drawLine (fb.getRight(), fb.getY(), fb.getRight(), fb.getBottom(), 1.5f);
        g.drawLine (fb.getX(), fb.getBottom(), fb.getX() + 10.0f, fb.getBottom(), 1.5f);
        g.drawLine (fb.getRight(), fb.getBottom(), fb.getRight() - 10.0f, fb.getBottom(), 1.5f);

        g.setColour (textColour);
        g.drawText ("y",
                    Rectangle { 40.0f, 20.0f }.withCentre ({ demoBounds.getCentreX(), demoBounds.getY() - 6.0f }),
                    Justification::centredTop);
    }

    void paint (Graphics& g) override
    {
        paintGuideLines (g);
        paintGlyphArrangement (g);
    }

    void paintGlyphArrangement (Graphics& g)
    {
        g.setColour (getLookAndFeel().findColour (Label::textColourId));
        ga.draw (g);
    }

    void resized() override
    {
        auto bounds = getLocalBounds().reduced (demoAreaPadding);

        auto sliderBounds = bounds.removeFromRight (260);
        sliderBounds.removeFromTop (30);
        lineSpacingSlider.setBounds (sliderBounds.removeFromTop (35));
        sliderBounds.removeFromTop (25);
        lineHeightMultipleSlider.setBounds (sliderBounds.removeFromTop (45));

        bounds.removeFromRight (10);
        demoDescription.setBounds (bounds.removeFromTop (lineHeightMultipleSlider.getBottom()));
        update();
    }

    void update()
    {
        ga.clear();

        const auto options = GlyphArrangement::Options{}.withLineSpacing ((float) lineSpacingSlider.getValue())
                                                        .withLineHeightMultiple ((float) lineHeightMultipleSlider.getValue());

        const auto leftBounds = getJustifiedBounds().toFloat();
        ga.addJustifiedText (font,
                             justified,
                             leftBounds.getX(),
                             leftBounds.getY(),
                             leftBounds.getWidth(),
                             Justification::centredTop,
                             options.getLineSpacing());

        const auto rightBounds = getFittedBounds().toFloat();
        ga.addFittedText (font,
                          fitted,
                          rightBounds.getX(),
                          rightBounds.getY(),
                          rightBounds.getWidth(),
                          rightBounds.getHeight(),
                          Justification::centredTop,
                          20,
                          0.0f,
                          options);

        repaint();
    }

    Font font = FontOptions{}.withPointHeight (16.0f);
    GlyphArrangement ga;
    Slider lineSpacingSlider { Slider::LinearHorizontal, Slider::TextBoxLeft };
    Slider lineHeightMultipleSlider { Slider::LinearHorizontal, Slider::TextBoxLeft };
    Label lineSpacingLabel { {}, "Line spacing:" };
    Label lineHeightMultipleLabel { {}, "Line height multiple (fitted text only):" };
    Label demoDescription { {}, "This demo showcases the GlyphArrangement class. Once constructed it "
                                "can be redrawn efficiently. Two important functions are addJustifiedText "
                                "and addFittedText." };

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LineSpacingDemo)
};